public void WebSocketContainerHostTests()
        {
            int total  = 0;
            int passed = 0;

            foreach (var mi in typeof(ContainerHostTests).GetMethods())
            {
                if (mi.GetCustomAttributes(typeof(TestMethodAttribute), false).Length > 0 &&
                    mi.GetCustomAttributes(typeof(IgnoreAttribute), false).Length == 0)
                {
                    total++;

                    ContainerHostTests test = new ContainerHostTests();
                    test.Uri = new System.Uri(address);
                    test.Initialize();

                    try
                    {
                        mi.Invoke(test, new object[0]);
                        System.Diagnostics.Trace.WriteLine(mi.Name + " passed");
                        passed++;
                    }
                    catch (Exception exception)
                    {
                        System.Diagnostics.Trace.WriteLine(mi.Name + " failed: " + exception.Message);
                    }

                    test.Cleanup();
                }
            }

            Assert.AreEqual(total, passed, string.Format("Not all tests passed {0}/{1}", passed, total));
        }
Esempio n. 2
0
        public void WebSocketContainerHostTests()
        {
            int total = 0;
            int passed = 0;

            foreach (var mi in typeof(ContainerHostTests).GetMethods())
            {
                if (mi.GetCustomAttributes(typeof(TestMethodAttribute), false).Length > 0 &&
                    mi.GetCustomAttributes(typeof(IgnoreAttribute), false).Length == 0)
                {
                    total++;

                    ContainerHostTests test = new ContainerHostTests();
                    test.Uri = new System.Uri(address);
                    test.Initialize();

                    try
                    {
                        mi.Invoke(test, new object[0]);
                        System.Diagnostics.Trace.WriteLine(mi.Name + " passed");
                        passed++;
                    }
                    catch (Exception exception)
                    {
                        System.Diagnostics.Trace.WriteLine(mi.Name + " failed: " + exception.ToString());
                    }

                    test.Cleanup();
                }
            }

            Assert.AreEqual(total, passed, string.Format("Not all tests passed {0}/{1}", passed, total));
        }
Esempio n. 3
0
        public async Task WebSocketSslMutalAuthTest()
        {
            string testName      = "WebSocketSslMutalAuthTest";
            string listenAddress = "wss://localhost:18081/" + testName + "/";
            Uri    uri           = new Uri(listenAddress);

            X509Certificate2 cert = ContainerHostTests.GetCertificate(StoreLocation.LocalMachine, StoreName.My, "localhost");

            string output;
            int    code = Exec("netsh.exe", string.Format("http show sslcert hostnameport={0}:{1}", uri.Host, uri.Port), out output);

            if (code != 0)
            {
                string args = string.Format("http add sslcert hostnameport={0}:{1} certhash={2} certstorename=MY appid={{{3}}} clientcertnegotiation=enable",
                                            uri.Host, uri.Port, cert.Thumbprint, Guid.NewGuid());
                code = Exec("netsh.exe", args, out output);
                Assert.AreEqual(0, code, "failed to add ssl cert: " + output);
            }

            X509Certificate serviceCert  = null;
            X509Certificate clientCert   = null;
            ListenerLink    listenerLink = null;

            var linkProcessor = new TestLinkProcessor()
            {
                OnLinkAttached = c => listenerLink = c
            };
            var host = new ContainerHost(new List <Uri>()
            {
                uri
            }, null, uri.UserInfo);

            host.Listeners[0].SASL.EnableExternalMechanism            = true;
            host.Listeners[0].SSL.ClientCertificateRequired           = true;
            host.Listeners[0].SSL.CheckCertificateRevocation          = true;
            host.Listeners[0].SSL.RemoteCertificateValidationCallback = (a, b, c, d) => { clientCert = b; return(true); };
            host.RegisterLinkProcessor(linkProcessor);
            host.Open();

            try
            {
                ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => { serviceCert = b; return(true); };
                var wssFactory = new WebSocketTransportFactory();
                wssFactory.Options = o =>
                {
                    o.ClientCertificates.Add(ContainerHostTests.GetCertificate(StoreLocation.LocalMachine, StoreName.My, uri.Host));
                };

                ConnectionFactory connectionFactory = new ConnectionFactory(new TransportProvider[] { wssFactory });
                connectionFactory.SASL.Profile = SaslProfile.External;
                Connection connection = await connectionFactory.CreateAsync(new Address(listenAddress));

                Session    session = new Session(connection);
                SenderLink sender  = new SenderLink(session, "sender-" + testName, "q1");
                await sender.SendAsync(new Message("test") { Properties = new Properties()
                                                             {
                                                                 MessageId = testName
                                                             } });

                await connection.CloseAsync();

                Assert.IsTrue(serviceCert != null, "service cert not received");
                Assert.IsTrue(clientCert != null, "client cert not received");
                Assert.IsTrue(listenerLink != null, "link not attached");

                IPrincipal principal = ((ListenerConnection)listenerLink.Session.Connection).Principal;
                Assert.IsTrue(principal != null, "connection pricipal is null");
                Assert.IsTrue(principal.Identity is X509Identity, "identify should be established by client cert");
            }
            finally
            {
                host.Close();
            }
        }