public void SslStream_StreamToStream_Successive_ClientWrite_Async_Success()
        {
            byte[]      recvBuf = new byte[_sampleMsg.Length];
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var serverSslStream = new SslStream(serverStream))
                        {
                            bool result = DoHandshake(clientSslStream, serverSslStream);

                            Assert.True(result, "Handshake completed.");

                            Task[] tasks = new Task[2];

                            tasks[0] = clientSslStream.WriteAsync(_sampleMsg, 0, _sampleMsg.Length);
                            tasks[1] = serverSslStream.ReadAsync(recvBuf, 0, _sampleMsg.Length);
                            bool finished = Task.WaitAll(tasks, TestConfiguration.PassingTestTimeoutMilliseconds);
                            Assert.True(finished, "Send/receive completed in the allotted time");
                            Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify first read data is as expected.");

                            tasks[0] = clientSslStream.WriteAsync(_sampleMsg, 0, _sampleMsg.Length);
                            tasks[1] = serverSslStream.ReadAsync(recvBuf, 0, _sampleMsg.Length);
                            finished = Task.WaitAll(tasks, TestConfiguration.PassingTestTimeoutMilliseconds);
                            Assert.True(finished, "Send/receive completed in the allotted time");
                            Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify second read data is as expected.");
                        }
        }
Example #2
0
        public void SslStream_StreamToStream_Successive_ClientWrite_Success()
        {
            byte[]      recvBuf = new byte[sampleMsg.Length];
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var serverSslStream = new SslStream(serverStream))
                        {
                            bool result = DoHandshake(clientSslStream, serverSslStream);

                            Assert.True(result, "Handshake completed.");

                            clientSslStream.Write(sampleMsg);

                            serverSslStream.Read(recvBuf, 0, sampleMsg.Length);

                            clientSslStream.Write(sampleMsg);

                            // TODO Test Issue #3802
                            // The condition on which read method (UpdateReadStream) in FakeNetworkStream does a network read is flawed.
                            // That works fine in single read/write but fails in multi read write as stream size can be more, but real data can be < stream size.
                            // So I am doing an explicit read here. This issue is specific to test only & irrespective of xplat.
                            serverStream.DoNetworkRead();

                            serverSslStream.Read(recvBuf, 0, sampleMsg.Length);

                            Assert.True(VerifyOutput(recvBuf, sampleMsg), "verify second read data is as expected.");
                        }
        }
        public void SslStream_StreamToStream_Successive_ClientWrite_Success()
        {
            byte[]      recvBuf = new byte[sampleMsg.Length];
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var serverSslStream = new SslStream(serverStream))
                        {
                            bool result = DoHandshake(clientSslStream, serverSslStream);

                            Assert.True(result, "Handshake completed.");

                            clientSslStream.Write(sampleMsg);

                            serverSslStream.Read(recvBuf, 0, sampleMsg.Length);

                            Assert.True(VerifyOutput(recvBuf, sampleMsg), "verify first read data is as expected.");

                            clientSslStream.Write(sampleMsg);

                            serverSslStream.Read(recvBuf, 0, sampleMsg.Length);

                            Assert.True(VerifyOutput(recvBuf, sampleMsg), "verify second read data is as expected.");
                        }
        }
        public void SslStream_StreamToStream_LargeWrites_Sync_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var serverSslStream = new SslStream(serverStream))
                        {
                            Assert.True(DoHandshake(clientSslStream, serverSslStream), "Handshake complete");

                            byte[] largeMsg         = Enumerable.Range(0, 4096 * 5).Select(i => (byte)i).ToArray();
                            byte[] receivedLargeMsg = new byte[largeMsg.Length];

                            // First do a large write and read blocks at a time
                            clientSslStream.Write(largeMsg);
                            int bytesRead = 0, totalRead = 0;
                            while (totalRead < largeMsg.Length &&
                                   (bytesRead = serverSslStream.Read(receivedLargeMsg, totalRead, receivedLargeMsg.Length - totalRead)) != 0)
                            {
                                totalRead += bytesRead;
                            }
                            Assert.Equal(receivedLargeMsg.Length, totalRead);
                            Assert.Equal(largeMsg, receivedLargeMsg);

                            // Then write again and read bytes at a time
                            clientSslStream.Write(largeMsg);
                            foreach (byte b in largeMsg)
                            {
                                Assert.Equal(b, serverSslStream.ReadByte());
                            }
                        }
        }
        public void SslStream_StreamToStream_Successive_ClientWrite_Sync_Success()
        {
            byte[] recvBuf = new byte[_sampleMsg.Length];
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
            using (var serverSslStream = new SslStream(serverStream))
            {
                bool result = DoHandshake(clientSslStream, serverSslStream);

                Assert.True(result, "Handshake completed.");

                clientSslStream.Write(_sampleMsg);

                serverSslStream.Read(recvBuf, 0, _sampleMsg.Length);

                Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify first read data is as expected.");

                clientSslStream.Write(_sampleMsg);

                serverSslStream.Read(recvBuf, 0, _sampleMsg.Length);

                Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify second read data is as expected.");
            }
        }
        public void SslStream_StreamToStream_Successive_ClientWrite_Success()
        {
            byte[] recvBuf = new byte[sampleMsg.Length];
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
            using (var serverSslStream = new SslStream(serverStream))
            {
                bool result = DoHandshake(clientSslStream, serverSslStream, TaskTimeSpan);

                Assert.True(result, "Handshake completed.");

                clientSslStream.Write(sampleMsg);

                serverSslStream.Read(recvBuf, 0, sampleMsg.Length);

                clientSslStream.Write(sampleMsg);

                // TODO Issue#3802
                // The condition on which read method (UpdateReadStream) in FakeNetworkStream does a network read is flawed.
                // That works fine in single read/write but fails in multi read write as stream size can be more, but real data can be < stream size.
                // So I am doing an explicit read here. This issue is specific to test only & irrespective of xplat.
                serverStream.DoNetworkRead();

                serverSslStream.Read(recvBuf, 0, sampleMsg.Length);

                Assert.True(VerifyOutput(recvBuf, sampleMsg), "verify second read data is as expected.");
            }
        }
Example #7
0
        public void NegotiateStream_StreamToStream_Successive_ClientWrite_Async_Success()
        {
            byte[]      recvBuf = new byte[_sampleMsg.Length];
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var client = new NegotiateStream(clientStream))
                        using (var server = new NegotiateStream(serverStream))
                        {
                            Assert.False(client.IsAuthenticated);
                            Assert.False(server.IsAuthenticated);

                            Task[] auth = new Task[2];
                            auth[0] = client.AuthenticateAsClientAsync();
                            auth[1] = server.AuthenticateAsServerAsync();

                            bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                            Assert.True(finished, "Handshake completed in the allotted time");

                            auth[0]  = client.WriteAsync(_sampleMsg, 0, _sampleMsg.Length);
                            auth[1]  = server.ReadAsync(recvBuf, 0, _sampleMsg.Length);
                            finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                            Assert.True(finished, "Send/receive completed in the allotted time");
                            Assert.True(_sampleMsg.SequenceEqual(recvBuf));

                            auth[0]  = client.WriteAsync(_sampleMsg, 0, _sampleMsg.Length);
                            auth[1]  = server.ReadAsync(recvBuf, 0, _sampleMsg.Length);
                            finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                            Assert.True(finished, "Send/receive completed in the allotted time");
                            Assert.True(_sampleMsg.SequenceEqual(recvBuf));
                        }
        }
        public void NegotiateStream_StreamToStream_Authentication_TargetName_Success()
        {
            string targetName = "testTargetName";

            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var client = new NegotiateStream(clientStream))
            using (var server = new NegotiateStream(serverStream))
            {
                Assert.False(client.IsAuthenticated);
                Assert.False(server.IsAuthenticated);

                Task[] auth = new Task[2];

                auth[0] = client.AuthenticateAsClientAsync(CredentialCache.DefaultNetworkCredentials, targetName);
                auth[1] = server.AuthenticateAsServerAsync();

                bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                Assert.True(finished, "Handshake completed in the allotted time");

                // Expected Client property values:
                Assert.True(client.IsAuthenticated);
                Assert.Equal(TokenImpersonationLevel.Identification, client.ImpersonationLevel);
                Assert.Equal(true, client.IsEncrypted);
                Assert.Equal(false, client.IsMutuallyAuthenticated);
                Assert.Equal(false, client.IsServer);
                Assert.Equal(true, client.IsSigned);
                Assert.Equal(false, client.LeaveInnerStreamOpen);

                IIdentity serverIdentity = client.RemoteIdentity;
                Assert.Equal("NTLM", serverIdentity.AuthenticationType);
                Assert.Equal(true, serverIdentity.IsAuthenticated);
                Assert.Equal(targetName, serverIdentity.Name);

                // Expected Server property values:
                Assert.True(server.IsAuthenticated);
                Assert.Equal(TokenImpersonationLevel.Identification, server.ImpersonationLevel);
                Assert.Equal(true, server.IsEncrypted);
                Assert.Equal(false, server.IsMutuallyAuthenticated);
                Assert.Equal(true, server.IsServer);
                Assert.Equal(true, server.IsSigned);
                Assert.Equal(false, server.LeaveInnerStreamOpen);

                IIdentity clientIdentity = server.RemoteIdentity;
                Assert.Equal("NTLM", clientIdentity.AuthenticationType);

                Assert.Equal(true, clientIdentity.IsAuthenticated);
                IdentityValidator.AssertIsCurrentIdentity(clientIdentity);
            }
        }
Example #9
0
        public void NegotiateStream_StreamToStream_Authentication_TargetName_Success()
        {
            string targetName = "testTargetName";

            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var client = new NegotiateStream(clientStream))
                        using (var server = new NegotiateStream(serverStream))
                        {
                            Assert.False(client.IsAuthenticated);
                            Assert.False(server.IsAuthenticated);

                            Task[] auth = new Task[2];

                            auth[0] = client.AuthenticateAsClientAsync(CredentialCache.DefaultNetworkCredentials, targetName);
                            auth[1] = server.AuthenticateAsServerAsync();

                            bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                            Assert.True(finished, "Handshake completed in the allotted time");

                            // Expected Client property values:
                            Assert.True(client.IsAuthenticated);
                            Assert.Equal(TokenImpersonationLevel.Identification, client.ImpersonationLevel);
                            Assert.Equal(true, client.IsEncrypted);
                            Assert.Equal(false, client.IsMutuallyAuthenticated);
                            Assert.Equal(false, client.IsServer);
                            Assert.Equal(true, client.IsSigned);
                            Assert.Equal(false, client.LeaveInnerStreamOpen);

                            IIdentity serverIdentity = client.RemoteIdentity;
                            Assert.Equal("NTLM", serverIdentity.AuthenticationType);
                            Assert.Equal(true, serverIdentity.IsAuthenticated);
                            Assert.Equal(targetName, serverIdentity.Name);

                            // Expected Server property values:
                            Assert.True(server.IsAuthenticated);
                            Assert.Equal(TokenImpersonationLevel.Identification, server.ImpersonationLevel);
                            Assert.Equal(true, server.IsEncrypted);
                            Assert.Equal(false, server.IsMutuallyAuthenticated);
                            Assert.Equal(true, server.IsServer);
                            Assert.Equal(true, server.IsSigned);
                            Assert.Equal(false, server.LeaveInnerStreamOpen);

                            IIdentity clientIdentity = server.RemoteIdentity;
                            Assert.Equal("NTLM", clientIdentity.AuthenticationType);

                            Assert.Equal(true, clientIdentity.IsAuthenticated);
                            IdentityValidator.AssertIsCurrentIdentity(clientIdentity);
                        }
        }
        public void SslStream_StreamToStream_Authentication_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var client = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var server = new SslStream(serverStream))
                        {
                            X509Certificate2 certificate = TestConfiguration.GetServerCertificate();
                            Task[]           auth        = new Task[2];
                            auth[0] = client.AuthenticateAsClientAsync(certificate.Subject);
                            auth[1] = server.AuthenticateAsServerAsync(certificate);

                            Task.WaitAll(auth);
                        }
        }
        public void SslStream_StreamToStream_Authentication_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var client = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var server = new SslStream(serverStream))
                        {
                            X509Certificate2 certificate = TestConfiguration.GetServerCertificate();
                            Task[]           auth        = new Task[2];
                            auth[0] = client.AuthenticateAsClientAsync(certificate.Subject);
                            auth[1] = server.AuthenticateAsServerAsync(certificate);

                            bool finished = Task.WaitAll(auth, TestTimeoutSpan);
                            Assert.True(finished, "Handshake completed in the allotted time");
                        }
        }
        public void SslStream_StreamToStream_Authentication_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var client = new SslStream(clientStream, false, AllowAnyServerCertificate))
            using (var server = new SslStream(serverStream))
            {
                X509Certificate2 certificate = TestConfiguration.GetServerCertificate();
                Task[] auth = new Task[2];
                auth[0] = client.AuthenticateAsClientAsync(certificate.Subject);
                auth[1] = server.AuthenticateAsServerAsync(certificate);

                bool finished = Task.WaitAll(auth, TimeSpan.FromSeconds(3));
                Assert.True(finished, "Handshake completed in the allotted time");
            }
        }
        public void SslStream_StreamToStream_Authentication_IncorrectServerName_Fail()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var client = new SslStream(clientStream))
                        using (var server = new SslStream(serverStream))
                        {
                            Task[] auth = new Task[2];
                            auth[0] = client.AuthenticateAsClientAsync("incorrectServer");
                            auth[1] = server.AuthenticateAsServerAsync(TestConfiguration.GetServerCertificate());

                            Assert.Throws <AuthenticationException>(() =>
                            {
                                auth[0].GetAwaiter().GetResult();
                            });

                            auth[1].GetAwaiter().GetResult();
                        }
        }
        public void SslStream_StreamToStream_Authentication_IncorrectServerName_Fail()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var client = new SslStream(clientStream))
            using (var server = new SslStream(serverStream))
            {
                Task[] auth = new Task[2];
                auth[0] = client.AuthenticateAsClientAsync("incorrectServer");
                auth[1] = server.AuthenticateAsServerAsync(TestConfiguration.GetServerCertificate());

                Assert.Throws<AuthenticationException>(() =>
                {
                    auth[0].GetAwaiter().GetResult();
                });

                auth[1].GetAwaiter().GetResult();
            }
        }
        public void SslStream_StreamToStream_Write_ReadByte_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var serverSslStream = new SslStream(serverStream))
                        {
                            bool result = DoHandshake(clientSslStream, serverSslStream);
                            Assert.True(result, "Handshake completed.");

                            for (int i = 0; i < 3; i++)
                            {
                                clientSslStream.Write(_sampleMsg);
                                foreach (byte b in _sampleMsg)
                                {
                                    Assert.Equal(b, serverSslStream.ReadByte());
                                }
                            }
                        }
        }
Example #16
0
 public FakeNetworkStream(bool isServer, MockNetwork network)
 {
     _network = network;
     _isServer = isServer;
 }
Example #17
0
 public FakeNetworkStream(bool isServer, MockNetwork network)
 {
     _network  = network;
     _isServer = isServer;
 }
        public void SslStream_StreamToStream_Write_ReadByte_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
            using (var serverSslStream = new SslStream(serverStream))
            {
                bool result = DoHandshake(clientSslStream, serverSslStream);
                Assert.True(result, "Handshake completed.");

                for (int i = 0; i < 3; i++)
                {
                    clientSslStream.Write(_sampleMsg);
                    foreach (byte b in _sampleMsg)
                    {
                        Assert.Equal(b, serverSslStream.ReadByte());
                    }
                }
            }
        }
        public void SslStream_StreamToStream_Successive_ClientWrite_Async_Success()
        {
            byte[] recvBuf = new byte[_sampleMsg.Length];
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
            using (var serverSslStream = new SslStream(serverStream))
            {
                bool result = DoHandshake(clientSslStream, serverSslStream);

                Assert.True(result, "Handshake completed.");

                Task[] tasks = new Task[2];

                tasks[0] = serverSslStream.ReadAsync(recvBuf, 0, _sampleMsg.Length);
                tasks[1] = clientSslStream.WriteAsync(_sampleMsg, 0, _sampleMsg.Length);
                bool finished = Task.WaitAll(tasks, TestConfiguration.PassingTestTimeoutMilliseconds);
                Assert.True(finished, "Send/receive completed in the allotted time");
                Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify first read data is as expected.");

                tasks[0] = serverSslStream.ReadAsync(recvBuf, 0, _sampleMsg.Length);
                tasks[1] = clientSslStream.WriteAsync(_sampleMsg, 0, _sampleMsg.Length);
                finished = Task.WaitAll(tasks, TestConfiguration.PassingTestTimeoutMilliseconds);
                Assert.True(finished, "Send/receive completed in the allotted time");
                Assert.True(VerifyOutput(recvBuf, _sampleMsg), "verify second read data is as expected.");
            }
        }
Example #20
0
        public void NegotiateStream_StreamToStream_Authentication_EmptyCredentials_Fails()
        {
            string targetName = "testTargetName";

            // Ensure there is no confusion between DefaultCredentials / DefaultNetworkCredentials and a
            // NetworkCredential object with empty user, password and domain.
            NetworkCredential emptyNetworkCredential = new NetworkCredential("", "", "");

            Assert.NotEqual(emptyNetworkCredential, CredentialCache.DefaultCredentials);
            Assert.NotEqual(emptyNetworkCredential, CredentialCache.DefaultNetworkCredentials);

            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var client = new NegotiateStream(clientStream))
                        using (var server = new NegotiateStream(serverStream))
                        {
                            Assert.False(client.IsAuthenticated);
                            Assert.False(server.IsAuthenticated);

                            Task[] auth = new Task[2];

                            auth[0] = client.AuthenticateAsClientAsync(emptyNetworkCredential, targetName);
                            auth[1] = server.AuthenticateAsServerAsync();

                            bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                            Assert.True(finished, "Handshake completed in the allotted time");

                            // Expected Client property values:
                            Assert.True(client.IsAuthenticated);
                            Assert.Equal(TokenImpersonationLevel.Identification, client.ImpersonationLevel);
                            Assert.Equal(true, client.IsEncrypted);
                            Assert.Equal(false, client.IsMutuallyAuthenticated);
                            Assert.Equal(false, client.IsServer);
                            Assert.Equal(true, client.IsSigned);
                            Assert.Equal(false, client.LeaveInnerStreamOpen);

                            IIdentity serverIdentity = client.RemoteIdentity;
                            Assert.Equal("NTLM", serverIdentity.AuthenticationType);
                            Assert.Equal(true, serverIdentity.IsAuthenticated);
                            Assert.Equal(targetName, serverIdentity.Name);

                            // Expected Server property values:
                            Assert.True(server.IsAuthenticated);
                            Assert.Equal(TokenImpersonationLevel.Identification, server.ImpersonationLevel);
                            Assert.Equal(true, server.IsEncrypted);
                            Assert.Equal(false, server.IsMutuallyAuthenticated);
                            Assert.Equal(true, server.IsServer);
                            Assert.Equal(true, server.IsSigned);
                            Assert.Equal(false, server.LeaveInnerStreamOpen);

                            IIdentity clientIdentity = server.RemoteIdentity;
                            Assert.Equal("NTLM", clientIdentity.AuthenticationType);

                            // TODO #5241: Behavior difference:
                            Assert.Equal(false, clientIdentity.IsAuthenticated);
                            // On .Net Desktop: Assert.Equal(true, clientIdentity.IsAuthenticated);

                            IdentityValidator.AssertHasName(clientIdentity, @"NT AUTHORITY\ANONYMOUS LOGON");
                        }
        }
        public void NegotiateStream_StreamToStream_Successive_ClientWrite_Async_Success()
        {
            byte[] recvBuf = new byte[_sampleMsg.Length];
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var client = new NegotiateStream(clientStream))
            using (var server = new NegotiateStream(serverStream))
            {
                Assert.False(client.IsAuthenticated);
                Assert.False(server.IsAuthenticated);

                Task[] auth = new Task[2];
                auth[0] = client.AuthenticateAsClientAsync();
                auth[1] = server.AuthenticateAsServerAsync();

                bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                Assert.True(finished, "Handshake completed in the allotted time");

                auth[0] = client.WriteAsync(_sampleMsg, 0, _sampleMsg.Length);
                auth[1] = server.ReadAsync(recvBuf, 0, _sampleMsg.Length);
                finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                Assert.True(finished, "Send/receive completed in the allotted time");
                Assert.True(_sampleMsg.SequenceEqual(recvBuf));

                auth[0] = client.WriteAsync(_sampleMsg, 0, _sampleMsg.Length);
                auth[1] = server.ReadAsync(recvBuf, 0, _sampleMsg.Length);
                finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                Assert.True(finished, "Send/receive completed in the allotted time");
                Assert.True(_sampleMsg.SequenceEqual(recvBuf));
            }
        }
        public void NegotiateStream_StreamToStream_Authentication_EmptyCredentials_Fails()
        {
            string targetName = "testTargetName";

            // Ensure there is no confusion between DefaultCredentials / DefaultNetworkCredentials and a
            // NetworkCredential object with empty user, password and domain.
            NetworkCredential emptyNetworkCredential = new NetworkCredential("", "", "");
            Assert.NotEqual(emptyNetworkCredential, CredentialCache.DefaultCredentials);
            Assert.NotEqual(emptyNetworkCredential, CredentialCache.DefaultNetworkCredentials);

            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var client = new NegotiateStream(clientStream))
            using (var server = new NegotiateStream(serverStream))
            {
                Assert.False(client.IsAuthenticated);
                Assert.False(server.IsAuthenticated);

                Task[] auth = new Task[2];

                auth[0] = client.AuthenticateAsClientAsync(emptyNetworkCredential, targetName);
                auth[1] = server.AuthenticateAsServerAsync();

                bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);

                // Expected Client property values:
                Assert.True(client.IsAuthenticated);
                Assert.Equal(TokenImpersonationLevel.Identification, client.ImpersonationLevel);
                Assert.Equal(true, client.IsEncrypted);
                Assert.Equal(false, client.IsMutuallyAuthenticated);
                Assert.Equal(false, client.IsServer);
                Assert.Equal(true, client.IsSigned);
                Assert.Equal(false, client.LeaveInnerStreamOpen);

                IIdentity serverIdentity = client.RemoteIdentity;
                Assert.Equal("NTLM", serverIdentity.AuthenticationType);
                Assert.Equal(true, serverIdentity.IsAuthenticated);
                Assert.Equal(targetName, serverIdentity.Name);

                // Expected Server property values:
                Assert.True(server.IsAuthenticated);
                Assert.Equal(TokenImpersonationLevel.Identification, server.ImpersonationLevel);
                Assert.Equal(true, server.IsEncrypted);
                Assert.Equal(false, server.IsMutuallyAuthenticated);
                Assert.Equal(true, server.IsServer);
                Assert.Equal(true, server.IsSigned);
                Assert.Equal(false, server.LeaveInnerStreamOpen);

                IIdentity clientIdentity = server.RemoteIdentity;
                Assert.Equal("NTLM", clientIdentity.AuthenticationType);

                // TODO #5241: Behavior difference:
                Assert.Equal(false, clientIdentity.IsAuthenticated);
                // On .Net Desktop: Assert.Equal(true, clientIdentity.IsAuthenticated);

                IdentityValidator.AssertHasName(clientIdentity, @"NT AUTHORITY\ANONYMOUS LOGON");
            }
        }
        public void SslStream_StreamToStream_LargeWrites_Sync_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
            using (var serverSslStream = new SslStream(serverStream))
            {
                Assert.True(DoHandshake(clientSslStream, serverSslStream), "Handshake complete");

                byte[] largeMsg = Enumerable.Range(0, 4096 * 5).Select(i => (byte)i).ToArray();
                byte[] receivedLargeMsg = new byte[largeMsg.Length];

                // First do a large write and read blocks at a time
                clientSslStream.Write(largeMsg);
                int bytesRead = 0, totalRead = 0;
                while (totalRead < largeMsg.Length &&
                    (bytesRead = serverSslStream.Read(receivedLargeMsg, totalRead, receivedLargeMsg.Length - totalRead)) != 0)
                {
                    totalRead += bytesRead;
                }
                Assert.Equal(receivedLargeMsg.Length, totalRead);
                Assert.Equal(largeMsg, receivedLargeMsg);

                // Then write again and read bytes at a time
                clientSslStream.Write(largeMsg);
                foreach (byte b in largeMsg)
                {
                    Assert.Equal(b, serverSslStream.ReadByte());
                }
            }
        }