< Summary

Class:Microsoft.Azure.ServiceBus.Amqp.AmqpConnectionHelper
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Amqp\AmqpConnectionHelper.cs
Covered lines:0
Uncovered lines:67
Coverable lines:67
Total lines:142
Line coverage:0% (0 of 67)
Covered branches:0
Total branches:22
Branch coverage:0% (0 of 22)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
CreateAmqpSettings(...)-0%0%
CreateTcpTransportSettings(...)-0%0%
CreateWebSocketTransportSettings(...)-0%0%
CreateAmqpConnectionSettings(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Amqp\AmqpConnectionHelper.cs

#LineLine coverage
 1// Copyright (c) Microsoft. All rights reserved.
 2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 3
 4namespace Microsoft.Azure.ServiceBus.Amqp
 5{
 6    using System;
 7    using System.Net;
 8    using Microsoft.Azure.Amqp;
 9    using Microsoft.Azure.Amqp.Sasl;
 10    using Microsoft.Azure.Amqp.Transport;
 11    using Microsoft.Azure.ServiceBus.Primitives;
 12
 13    internal class AmqpConnectionHelper
 14    {
 15        const string CbsSaslMechanismName = "MSSBCBS";
 16
 17        public static AmqpSettings CreateAmqpSettings(
 18            Version amqpVersion,
 19            bool useSslStreamSecurity,
 20            bool hasTokenProvider,
 21            string sslHostName = null,
 22            bool useWebSockets = false,
 23            bool sslStreamUpgrade = false,
 24            System.Net.NetworkCredential networkCredential = null,
 25            bool forceTokenProvider = true)
 26        {
 027            var amqpSettings = new AmqpSettings();
 028            if (useSslStreamSecurity && !useWebSockets && sslStreamUpgrade)
 29            {
 030                var tlsSettings = new TlsTransportSettings
 031                {
 032                    TargetHost = sslHostName
 033                };
 34
 035                var tlsProvider = new TlsTransportProvider(tlsSettings);
 036                tlsProvider.Versions.Add(new AmqpVersion(amqpVersion));
 037                amqpSettings.TransportProviders.Add(tlsProvider);
 38            }
 39
 040            if (hasTokenProvider || networkCredential != null)
 41            {
 042                var saslTransportProvider = new SaslTransportProvider();
 043                saslTransportProvider.Versions.Add(new AmqpVersion(amqpVersion));
 044                amqpSettings.TransportProviders.Add(saslTransportProvider);
 45
 046                if (forceTokenProvider)
 47                {
 048                    saslTransportProvider.AddHandler(new SaslAnonymousHandler(CbsSaslMechanismName));
 49                }
 050                else if (networkCredential != null)
 51                {
 052                    var plainHandler = new SaslPlainHandler
 053                    {
 054                        AuthenticationIdentity = networkCredential.UserName,
 055                        Password = networkCredential.Password
 056                    };
 057                    saslTransportProvider.AddHandler(plainHandler);
 58                }
 59                else
 60                {
 61                    // old client behavior: keep it for validation only
 062                    saslTransportProvider.AddHandler(new SaslExternalHandler());
 63                }
 64            }
 65
 066            var amqpTransportProvider = new AmqpTransportProvider();
 067            amqpTransportProvider.Versions.Add(new AmqpVersion(amqpVersion));
 068            amqpSettings.TransportProviders.Add(amqpTransportProvider);
 69
 070            return amqpSettings;
 71        }
 72
 73        public static TransportSettings CreateTcpTransportSettings(
 74            string networkHost,
 75            string hostName,
 76            int port,
 77            bool useSslStreamSecurity,
 78            bool sslStreamUpgrade = false,
 79            string sslHostName = null)
 80        {
 081            var tcpTransportSettings = new TcpTransportSettings
 082            {
 083                Host = networkHost,
 084                Port = port < Constants.WellKnownPublicPortsLimit ? AmqpConstants.DefaultSecurePort : port,
 085                ReceiveBufferSize = AmqpConstants.TransportBufferSize,
 086                SendBufferSize = AmqpConstants.TransportBufferSize
 087            };
 88
 089            TransportSettings tpSettings = tcpTransportSettings;
 090            if (useSslStreamSecurity && !sslStreamUpgrade)
 91            {
 092                var tlsTransportSettings = new TlsTransportSettings(tcpTransportSettings)
 093                {
 094                    TargetHost = sslHostName ?? hostName
 095                };
 096                tpSettings = tlsTransportSettings;
 97            }
 98
 099            return tpSettings;
 100        }
 101
 102        public static TransportSettings CreateWebSocketTransportSettings(
 103            string networkHost,
 104            string hostName,
 105            int port,
 106            IWebProxy proxy)
 107        {
 0108            var uriBuilder = new UriBuilder(
 0109                WebSocketConstants.WebSocketSecureScheme,
 0110                networkHost,
 0111                port < Constants.WellKnownPublicPortsLimit ? WebSocketConstants.WebSocketSecurePort : port,
 0112                WebSocketConstants.WebSocketDefaultPath);
 0113            var webSocketTransportSettings = new WebSocketTransportSettings
 0114            {
 0115                Uri = uriBuilder.Uri,
 0116                ReceiveBufferSize = AmqpConstants.TransportBufferSize,
 0117                SendBufferSize = AmqpConstants.TransportBufferSize,
 0118                Proxy = proxy
 0119            };
 120
 121            TransportSettings tpSettings = webSocketTransportSettings;
 0122            return tpSettings;
 123        }
 124
 125        public static AmqpConnectionSettings CreateAmqpConnectionSettings(uint maxFrameSize, string containerId, string 
 126        {
 0127            var connectionSettings = new AmqpConnectionSettings
 0128            {
 0129                MaxFrameSize = maxFrameSize,
 0130                ContainerId = containerId,
 0131                HostName = hostName,
 0132                IdleTimeOut = (uint)Constants.DefaultOperationTimeout.TotalMilliseconds
 0133            };
 134
 0135            connectionSettings.AddProperty("product", ClientInfo.Product);
 0136            connectionSettings.AddProperty("version", ClientInfo.Version);
 0137            connectionSettings.AddProperty("framework", ClientInfo.Framework);
 0138            connectionSettings.AddProperty("platform", ClientInfo.Platform);
 0139            return connectionSettings;
 140        }
 141    }
 142}