예제 #1
0
        public async void TestStartAsyncDoesNotThrowIfCreateConnectionThrows()
        {
            AmqpSettings amqpSettings    = AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>(), Mock.Of <ILinkHandlerProvider>(), Mock.Of <IConnectionProvider>(), new NullCredentialsCache());
            var          runtimeProvider = new Mock <IRuntimeProvider>();

            amqpSettings.RuntimeProvider = runtimeProvider.Object;

            runtimeProvider.Setup(rp => rp.CreateConnection(It.IsAny <TransportBase>(), It.IsAny <ProtocolHeader>(), false, It.IsAny <AmqpSettings>(), It.IsAny <AmqpConnectionSettings>()))
            .Throws(new ApplicationException("No donuts"));

            var tcpTransportListener  = new Mock <TransportListener>("TCP");
            var amqpTransportSettings = new Mock <TransportSettings>();

            amqpTransportSettings.Setup(ts => ts.CreateListener()).Returns(tcpTransportListener.Object);
            var transportSettings = new Mock <ITransportSettings>();

            transportSettings.SetupGet(sp => sp.Settings).Returns(amqpTransportSettings.Object);

            var amqpTransportListener = new TestHelperTransportListener(
                "AMQP",
                new TransportAsyncCallbackArgs()
            {
                CompletedSynchronously = false
            });

            var transportListenerProvider = new Mock <ITransportListenerProvider>();

            transportListenerProvider.Setup(tlp => tlp.Create(
                                                It.Is <IEnumerable <TransportListener> >(listeners => listeners.Contains(tcpTransportListener.Object)),
                                                amqpSettings
                                                )).Returns(amqpTransportListener);

            var protocolHead = new AmqpProtocolHead(transportSettings.Object, amqpSettings, transportListenerProvider.Object, Mock.Of <IWebSocketListenerRegistry>(), Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>());
            await protocolHead.StartAsync();
        }
예제 #2
0
        protected override void Load(ContainerBuilder builder)
        {
            // ITransportSettings
            builder.Register(c => new DefaultTransportSettings(this.scheme, HostName, this.port, this.tlsCertificate))
            .As <ITransportSettings>()
            .SingleInstance();

            // ITransportListenerProvider
            builder.Register(c => new AmqpTransportListenerProvider())
            .As <ITransportListenerProvider>()
            .SingleInstance();

            // ILinkHandlerProvider
            builder.Register(
                c =>
            {
                IMessageConverter <AmqpMessage> messageConverter             = new AmqpMessageConverter();
                IMessageConverter <AmqpMessage> twinMessageConverter         = new AmqpTwinMessageConverter();
                IMessageConverter <AmqpMessage> directMethodMessageConverter = new AmqpDirectMethodMessageConverter();
                var identityProvider = c.Resolve <IIdentityProvider>();
                ILinkHandlerProvider linkHandlerProvider = new LinkHandlerProvider(messageConverter, twinMessageConverter, directMethodMessageConverter, identityProvider);
                return(linkHandlerProvider);
            })
            .As <ILinkHandlerProvider>()
            .SingleInstance();

            // Task<AmqpProtocolHead>
            builder.Register(
                async c =>
            {
                var identityFactory                    = c.Resolve <IClientCredentialsFactory>();
                var transportSettings                  = c.Resolve <ITransportSettings>();
                var transportListenerProvider          = c.Resolve <ITransportListenerProvider>();
                var linkHandlerProvider                = c.Resolve <ILinkHandlerProvider>();
                var credentialsCacheTask               = c.Resolve <Task <ICredentialsCache> >();
                var authenticatorTask                  = c.Resolve <Task <IAuthenticator> >();
                var connectionProviderTask             = c.Resolve <Task <IConnectionProvider> >();
                ICredentialsCache credentialsCache     = await credentialsCacheTask;
                IAuthenticator authenticator           = await authenticatorTask;
                IConnectionProvider connectionProvider = await connectionProviderTask;
                var webSocketListenerRegistry          = c.Resolve <IWebSocketListenerRegistry>();
                AmqpSettings amqpSettings              = AmqpSettingsProvider.GetDefaultAmqpSettings(
                    this.iotHubHostName,
                    authenticator,
                    identityFactory,
                    linkHandlerProvider,
                    connectionProvider,
                    credentialsCache);

                return(new AmqpProtocolHead(
                           transportSettings,
                           amqpSettings,
                           transportListenerProvider,
                           webSocketListenerRegistry));
            })
            .As <Task <AmqpProtocolHead> >()
            .SingleInstance();

            base.Load(builder);
        }
예제 #3
0
        public async void TestStartAsyncThrowsIfTransportListenerCallbackArgsHasException()
        {
            AmqpSettings amqpSettings = AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>(), Mock.Of <ILinkHandlerProvider>(), Mock.Of <IConnectionProvider>(), new NullCredentialsCache());

            var tcpTransportListener  = new Mock <TransportListener>("TCP");
            var amqpTransportSettings = new Mock <TransportSettings>();

            amqpTransportSettings.Setup(ts => ts.CreateListener()).Returns(tcpTransportListener.Object);
            var transportSettings = new Mock <ITransportSettings>();

            transportSettings.SetupGet(sp => sp.Settings).Returns(amqpTransportSettings.Object);

            var amqpTransportListener = new TestHelperTransportListener(
                "AMQP",
                new TransportAsyncCallbackArgs()
            {
                Exception = new ApplicationException("No donuts"),
                CompletedSynchronously = false
            });

            var transportListenerProvider = new Mock <ITransportListenerProvider>();

            transportListenerProvider.Setup(tlp => tlp.Create(
                                                It.Is <IEnumerable <TransportListener> >(listeners => listeners.Contains(tcpTransportListener.Object)),
                                                amqpSettings
                                                )).Returns(amqpTransportListener);

            var protocolHead = new AmqpProtocolHead(transportSettings.Object, amqpSettings, transportListenerProvider.Object, Mock.Of <IWebSocketListenerRegistry>(), Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>());
            await Assert.ThrowsAsync <ApplicationException>(() => protocolHead.StartAsync());
        }
예제 #4
0
        public async void TestStartAsyncDoesNotThrowIfConnectionOpenAsyncThrows()
        {
            AmqpSettings amqpSettings    = AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>(), Mock.Of <ILinkHandlerProvider>(), Mock.Of <IConnectionProvider>(), new NullCredentialsCache());
            var          runtimeProvider = new Mock <IRuntimeProvider>();

            amqpSettings.RuntimeProvider = runtimeProvider.Object;

            var tcpTransport = new Mock <TransportBase>("TCP");

            TestHelperAmqpConnection amqpConnection = null;

            runtimeProvider.Setup(rp => rp.CreateConnection(tcpTransport.Object, It.IsAny <ProtocolHeader>(), false, It.IsAny <AmqpSettings>(), It.IsAny <AmqpConnectionSettings>()))
            .Callback(
                (
                    TransportBase transport,
                    ProtocolHeader protocolHeader,
                    bool isInitiator,
                    AmqpSettings settings,
                    AmqpConnectionSettings connectionSettings) =>
            {
                amqpConnection = new TestHelperAmqpConnection(transport, protocolHeader, isInitiator, settings, connectionSettings);
                amqpConnection.OnOpenInternal = () => throw new OperationCanceledException("No donuts for you");
            })
            .Returns(() => amqpConnection);

            var tcpTransportListener  = new Mock <TransportListener>("TCP");
            var amqpTransportSettings = new Mock <TransportSettings>();

            amqpTransportSettings.Setup(ts => ts.CreateListener()).Returns(tcpTransportListener.Object);
            var transportSettings = new Mock <ITransportSettings>();

            transportSettings.SetupGet(sp => sp.Settings).Returns(amqpTransportSettings.Object);

            var amqpTransportListener = new TestHelperTransportListener(
                "AMQP",
                new TransportAsyncCallbackArgs()
            {
                CompletedSynchronously = false,
                Transport = tcpTransport.Object
            });

            var transportListenerProvider = new Mock <ITransportListenerProvider>();

            transportListenerProvider.Setup(
                tlp => tlp.Create(
                    It.Is <IEnumerable <TransportListener> >(listeners => listeners.Contains(tcpTransportListener.Object)),
                    amqpSettings)).Returns(amqpTransportListener);

            var protocolHead = new AmqpProtocolHead(transportSettings.Object, amqpSettings, transportListenerProvider.Object, Mock.Of <IWebSocketListenerRegistry>(), Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>());
            await protocolHead.StartAsync();

            // check if close on the connection was called
            Assert.NotNull(amqpConnection);
            Assert.True(amqpConnection.WasClosed);
        }
예제 #5
0
        public void TestInvalidConstructorInputs()
        {
            X509Certificate2 tlsCertificate        = CertificateHelper.GenerateSelfSignedCert("TestCert");
            var          transportSettings         = new DefaultTransportSettings(Scheme, HostName, Port, tlsCertificate);
            AmqpSettings amqpSettings              = AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>(), Mock.Of <ILinkHandlerProvider>(), Mock.Of <IConnectionProvider>(), new NullCredentialsCache());
            var          transportListenerProvider = new Mock <ITransportListenerProvider>();

            Assert.Throws <ArgumentNullException>(() => new AmqpProtocolHead(null, amqpSettings, transportListenerProvider.Object));
            Assert.Throws <ArgumentNullException>(() => new AmqpProtocolHead(transportSettings, null, transportListenerProvider.Object));
            Assert.Throws <ArgumentNullException>(() => new AmqpProtocolHead(transportSettings, amqpSettings, null));
            Assert.NotNull(new AmqpProtocolHead(transportSettings, amqpSettings, transportListenerProvider.Object));
        }
예제 #6
0
        public void TestInvalidInputsForGetDefaultAmqpSettings()
        {
            const string IotHubHostName      = "restaurantatendofuniverse.azure-devices.net";
            var          authenticator       = new Mock <IAuthenticator>();
            var          identityFactory     = new Mock <IClientCredentialsFactory>();
            var          linkHandlerProvider = Mock.Of <ILinkHandlerProvider>();
            var          connectionProvider  = Mock.Of <IConnectionProvider>();

            Assert.Throws <ArgumentException>(() => AmqpSettingsProvider.GetDefaultAmqpSettings(null, authenticator.Object, identityFactory.Object, linkHandlerProvider, connectionProvider, new NullCredentialsCache()));
            Assert.Throws <ArgumentNullException>(() => AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, null, identityFactory.Object, linkHandlerProvider, connectionProvider, new NullCredentialsCache()));
            Assert.Throws <ArgumentNullException>(() => AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, authenticator.Object, null, linkHandlerProvider, connectionProvider, new NullCredentialsCache()));

            Assert.NotNull(AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, authenticator.Object, identityFactory.Object, linkHandlerProvider, connectionProvider, new NullCredentialsCache()));
        }
예제 #7
0
        public async void TestStartAsyncThrowsIfCreateListenerThrows()
        {
            AmqpSettings amqpSettings = AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>(), Mock.Of <ILinkHandlerProvider>(), Mock.Of <IConnectionProvider>(), new NullCredentialsCache());

            var amqpTransportSettings = new Mock <TransportSettings>();

            amqpTransportSettings.Setup(ts => ts.CreateListener()).Throws(new ApplicationException("No donuts for you"));

            var transportSettings = new Mock <ITransportSettings>();

            transportSettings.SetupGet(sp => sp.Settings).Returns(amqpTransportSettings.Object);

            var protocolHead = new AmqpProtocolHead(transportSettings.Object, amqpSettings, Mock.Of <ITransportListenerProvider>(), Mock.Of <IWebSocketListenerRegistry>(), Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>());
            await Assert.ThrowsAsync <ApplicationException>(() => protocolHead.StartAsync());
        }
예제 #8
0
        public async void TestStartAsyncThrowsIfOpenAsyncOrListenThrows(TransportListener amqpTransportListener)
        {
            AmqpSettings amqpSettings = AmqpSettingsProvider.GetDefaultAmqpSettings(IotHubHostName, Mock.Of <IAuthenticator>(), Mock.Of <IClientCredentialsFactory>(), Mock.Of <ILinkHandlerProvider>(), Mock.Of <IConnectionProvider>(), new NullCredentialsCache());

            var tcpTransportListener  = new Mock <TransportListener>("TCP");
            var amqpTransportSettings = new Mock <TransportSettings>();

            amqpTransportSettings.Setup(ts => ts.CreateListener()).Returns(tcpTransportListener.Object);
            var transportSettings = new Mock <ITransportSettings>();

            transportSettings.SetupGet(sp => sp.Settings).Returns(amqpTransportSettings.Object);

            var transportListenerProvider = new Mock <ITransportListenerProvider>();

            transportListenerProvider.Setup(tlp => tlp.Create(
                                                It.Is <IEnumerable <TransportListener> >(listeners => listeners.Contains(tcpTransportListener.Object)),
                                                amqpSettings
                                                )).Returns(amqpTransportListener);

            var protocolHead = new AmqpProtocolHead(transportSettings.Object, amqpSettings, transportListenerProvider.Object);
            await Assert.ThrowsAsync <ApplicationException>(() => protocolHead.StartAsync());
        }
예제 #9
0
        public void ValidateSettingsTest()
        {
            // Arrange
            string iotHubHostName      = "foo.azure-devices.net";
            var    authenticator       = Mock.Of <IAuthenticator>();
            var    identityFactory     = Mock.Of <IClientCredentialsFactory>();
            var    linkHandlerProvider = Mock.Of <ILinkHandlerProvider>();
            var    connectionProvider  = Mock.Of <IConnectionProvider>();

            // Act
            AmqpSettings settings = AmqpSettingsProvider.GetDefaultAmqpSettings(iotHubHostName, authenticator, identityFactory, linkHandlerProvider, connectionProvider, new NullCredentialsCache());

            // Assert
            Assert.NotNull(settings);
            Assert.Equal(2, settings.TransportProviders.Count);

            var saslTransportProvider = settings.GetTransportProvider <SaslTransportProvider>();

            Assert.NotNull(saslTransportProvider);

            SaslHandler anonHandler = saslTransportProvider.GetHandler("ANONYMOUS", false);

            Assert.NotNull(anonHandler);

            SaslHandler plainHandler = saslTransportProvider.GetHandler("PLAIN", false);

            Assert.NotNull(plainHandler);

            SaslHandler cbsHandler = saslTransportProvider.GetHandler(Amqp.Constants.ServiceBusCbsSaslMechanismName, false);

            Assert.NotNull(cbsHandler);

            var amqpTransportProvider = settings.GetTransportProvider <AmqpTransportProvider>();

            Assert.NotNull(amqpTransportProvider);

            Assert.Equal(Amqp.Constants.AmqpVersion100, amqpTransportProvider.Versions[0]);
        }