Ejemplo n.º 1
0
        public void DuplexEcho()
        {
            string   testString = new string('a', 3000);
            IWebHost host       = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ServiceContract.IDuplexTestService> factory = null;
                ServiceContract.IDuplexTestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding();
                    var callback = new ServiceContract.DuplexTestCallback();
                    factory = new System.ServiceModel.DuplexChannelFactory <ServiceContract.IDuplexTestService>(
                        new System.ServiceModel.InstanceContext(callback),
                        binding,
                        new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.DuplexRelativeAddress));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    var registerSuccess = channel.RegisterDuplexChannel();
                    Assert.True(registerSuccess, "Registration was not successful");
                    channel.SendMessage(testString);
                    Assert.Equal(1, callback.ReceivedMessages.Count);
                    Assert.Equal(testString, callback.ReceivedMessages[0]);
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
        private void BasicUserNameAuth(bool isError, string userName)
        {
            string   testString = new string('a', 3000);
            IWebHost host       = ServiceHelper.CreateWebHostBuilder <StartUpPermissionBaseForTC>(_output).Build();

            using (host)
            {
                host.Start();
                System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.TransportWithMessageCredential);
                binding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName;
                UriBuilder uriBuilder = new UriBuilder(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath);
                uriBuilder.Host = "localhost"; // Replace 127.0.0.1 with localhost so Identity has correct value
                var factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(uriBuilder.ToString()));
                System.ServiceModel.Description.ClientCredentials clientCredentials = (System.ServiceModel.Description.ClientCredentials)factory.Endpoint.EndpointBehaviors[typeof(System.ServiceModel.Description.ClientCredentials)];
                factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication
                {
                    CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None
                };
                clientCredentials.UserName.UserName = userName;
                clientCredentials.UserName.Password = RandomString(10);
                var channel = factory.CreateChannel();
                try
                {
                    if (isError)
                    {
                        Assert.ThrowsAny <System.ServiceModel.CommunicationException>(() =>
                        {
                            ((IChannel)channel).Open();
                        });

                        ((IChannel)channel).Abort();
                    }
                    else
                    {
                        ((IChannel)channel).Open();
                        string result = channel.EchoString(testString);
                        Assert.Equal(testString, result);
                        ((IChannel)channel).Close();
                        factory.Close();
                    }
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Ejemplo n.º 3
0
        public void SettingIPAdressProperty()
        {
            string   testString          = new string('a', 3000);
            int      port                = 11808;
            string   expectedBaseAddress = $"net.tcp://{IPAddress.Loopback}:{port}";
            IWebHost host                = ServiceHelper.CreateWebHostBuilder <Startup>(_output, IPAddress.Loopback, port).Build();

            using (host)
            {
                host.Start();
                Assert.Equal(expectedBaseAddress, host.GetNetTcpAddressInUse());
                var netTcpBinding = ClientHelper.GetBufferedModeBinding();
                var factory       = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(netTcpBinding,
                                                                                                         new System.ServiceModel.EndpointAddress(new Uri($"{expectedBaseAddress}/EchoService.svc")));
                var channel = factory.CreateChannel();

                var result = channel.EchoString(testString);
                Assert.Equal(testString, result);
            }
        }
        public async Task NetTCPRequestReplyWithTransportMessageEchoStringDemuxFailure()
        {
            string   testString = new string('a', 3000);
            IWebHost host       = ServiceHelper.CreateWebHostBuilder <StartUpPermissionBaseForTCDemuxFailure>(_output).Build();

            using (host)
            {
                host.Start();
                System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.TransportWithMessageCredential);
                binding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName;
                UriBuilder uriBuilder = new UriBuilder(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath);
                uriBuilder.Host = "localhost"; // Replace 127.0.0.1 with localhost so Identity has correct value
                var factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(uriBuilder.ToString()));
                System.ServiceModel.Description.ClientCredentials clientCredentials = (System.ServiceModel.Description.ClientCredentials)factory.Endpoint.EndpointBehaviors[typeof(System.ServiceModel.Description.ClientCredentials)];
                factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication
                {
                    CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None
                };
                clientCredentials.UserName.UserName = "******";
                clientCredentials.UserName.Password = RandomString(10);
                var channel = factory.CreateChannel();
                try
                {
                    ((IChannel)channel).Open();
                    await Task.Delay(6000);

                    string result = channel.EchoString(testString);
                }
                catch (Exception ex)
                {
                    Assert.IsAssignableFrom <System.ServiceModel.FaultException>(ex.InnerException);
                    Assert.Contains("expired security context token", ex.InnerException.Message);
                }
            }
        }
Ejemplo n.º 5
0
 private void assertForCommon(String sourceString, IWebHost host)
 {
     using (host)
     {
         System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
         ClientContract.ITestService channel = null;
         host.Start();
         try
         {
             var binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport);
             factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                            new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath));
             channel = factory.CreateChannel();
             ((IChannel)channel).Open();
             var result = channel.EchoForPermission(sourceString);
             Assert.Equal(sourceString, result);
             ((IChannel)channel).Close();
             factory.Close();
         }
         finally
         {
             ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
         }
     }
 }
Ejemplo n.º 6
0
        public void RemoteEndpointMessageProperty()
        {
            IWebHost host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                host.Start();
                System.ServiceModel.NetTcpBinding nettcpBinding = ClientHelper.GetBufferedModeBinding();
                var factory = new System.ServiceModel.ChannelFactory <ClientContract.IRemoteEndpointMessageProperty>(nettcpBinding,
                                                                                                                     new System.ServiceModel.EndpointAddress(new Uri(host.GetNetTcpAddressInUse() + "/RemoteEndpointMessagePropertyService.svc")));
                ClientContract.IRemoteEndpointMessageProperty channel = factory.CreateChannel();

                Message request  = Message.CreateMessage(nettcpBinding.MessageVersion, "echo", "PASS");
                Message response = channel.Echo(request);

                string[] results = response.GetBody <string>().Split(';');
                Assert.Equal(3, results.Length);
                Assert.Equal("PASS", results[0]);

                string clientIP = results[1];
                CheckIP(clientIP);
                NetstatResults(results[2], host.GetNetTcpPortInUse().ToString());
            }
        }
        public void MultipleClientsNonConcurrentNetTcpClientConnection()
        {
            string   testString = new string('a', 3000);
            IWebHost host       = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    string result = channel.EchoString(testString);
                    ((IChannel)channel).Close();
                    Assert.Equal(testString, result);
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    result = channel.EchoString(testString);
                    ((IChannel)channel).Close();
                    Assert.Equal(testString, result);
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
        public void MessageContract()
        {
            IWebHost host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    var message = new ClientContract.TestMessage()
                    {
                        Header = "Header",
                        Body   = new MemoryStream(Encoding.UTF8.GetBytes("Hello world"))
                    };
                    ClientContract.TestMessage result = channel.TestMessageContract(message);
                    Assert.Equal("Header from server", result.Header);
                    Assert.Equal("Hello world from server", new StreamReader(result.Body, Encoding.UTF8).ReadToEnd());
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
        public void MultipleClientsUsingPooledSocket()
        {
            IWebHost host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    string clientIpEndpoint = channel.GetClientIpEndpoint();
                    ((IChannel)channel).Close();
                    for (int i = 0; i < 10; i++)
                    {
                        channel = factory.CreateChannel();
                        ((IChannel)channel).Open();
                        string clientIpEndpoint2 = channel.GetClientIpEndpoint();
                        Assert.Equal(clientIpEndpoint, clientIpEndpoint2);
                        ((IChannel)channel).Close();
                    }
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
        public void ConcurrentNetTcpClientConnection()
        {
            IWebHost host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    System.Threading.Tasks.Task <bool> resultTask = channel.WaitForSecondRequestAsync();
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                    channel.SecondRequest();
                    bool waitResult = resultTask.GetAwaiter().GetResult();
                    Assert.True(waitResult, $"SecondRequest wasn't executed concurrently");
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Ejemplo n.º 11
0
        public void SimpleNetTcpClientImpersonateUser()
        {
            string   sourceString = "test";
            IWebHost host         = ServiceHelper.CreateWebHostBuilder <ImpersonateCallerForAll>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport);
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    string result = channel.EchoForImpersonation(sourceString);
                    Assert.Equal(sourceString, result);
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Ejemplo n.º 12
0
        public void AuthorizationBasedonRolesTest()
        {
            string   testString = "a" + PrincipalPermissionMode.Always.ToString() + "test";
            IWebHost host       = ServiceHelper.CreateWebHostBuilder <StartupForAuthorization>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport);
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    string result = channel.EchoForAuthorizarionOneRole(testString);
                    Assert.Equal(testString, result);
                    try
                    {
                        channel.EchoForAuthorizarionNoRole(testString);
                    }catch (Exception ex)
                    {
                        Assert.Contains("Access is denied", ex.Message);
                    }
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Ejemplo n.º 13
0
        public async Task StreamedSynchronouslyCompletingTask()
        {
            string   testString = new string('a', 3000);
            IWebHost host       = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <Contract.ITaskService> factory = null;
                Contract.ITaskService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetStreamedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <Contract.ITaskService>(binding,
                                                                                             new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.StreamedRelatveAddress));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    await channel.SynchronousCompletion();

                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Ejemplo n.º 14
0
        private void assertForCommon(string sourceString, IWebHost host)
        {
            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport);
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath));
                    channel = factory.CreateChannel();

                    // ReSharper disable once SuspiciousTypeConversion.Global
                    ((IChannel)channel).Open();
                    string result = channel.EchoForPermission(sourceString);
                    Assert.Equal(sourceString, result);

                    //
                    // These were explicitly removed because the ServiceHelper already cleans these up, and
                    // in some cases not using proper disposal handling will result in:
                    // 'System.IO.IOException : Received an unexpected EOF or 0 bytes from the transport stream.'
                    // on the build server, causing false test failures.
                    //
                    // ((IChannel)channel).Close();
                    // factory.Close();
                }
                finally
                {
                    // ReSharper disable once SuspiciousTypeConversion.Global
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }