コード例 #1
0
        public async Task Read_CorrectlyUnlocksAfterFailure()
        {
            (Stream stream1, Stream stream2) = TestHelper.GetConnectedStreams();
            var clientStream = new ThrowingDelegatingStream(stream1);

            using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
                using (var serverSslStream = new SslStream(stream2))
                {
                    await DoHandshake(clientSslStream, serverSslStream);

                    // Throw an exception from the wrapped stream's read operation
                    clientStream.ExceptionToThrow = new FormatException();
                    IOException thrown = await Assert.ThrowsAsync <IOException>(() => ReadAsync(clientSslStream, new byte[1], 0, 1));

                    Assert.Same(clientStream.ExceptionToThrow, thrown.InnerException);
                    clientStream.ExceptionToThrow = null;

                    // Validate that the SslStream continues to be usable
                    for (byte b = 42; b < 52; b++) // arbitrary test values
                    {
                        await WriteAsync(serverSslStream, new byte[1] {
                            b
                        }, 0, 1);

                        byte[] buffer = new byte[1];
                        Assert.Equal(1, await ReadAsync(clientSslStream, buffer, 0, 1));
                        Assert.Equal(b, buffer[0]);
                    }
                }
        }
コード例 #2
0
        public async Task Write_CorrectlyUnlocksAfterFailure()
        {
            (Stream stream1, Stream stream2) = TestHelper.GetConnectedStreams();
            var clientStream = new ThrowingDelegatingStream(stream1);

            using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
                using (var serverSslStream = new SslStream(stream2))
                {
                    await DoHandshake(clientSslStream, serverSslStream);

                    // Throw an exception from the wrapped stream's write operation
                    clientStream.ExceptionToThrow = new FormatException();
                    IOException thrown = await Assert.ThrowsAsync <IOException>(() => WriteAsync(clientSslStream, new byte[1], 0, 1));

                    Assert.Same(clientStream.ExceptionToThrow, thrown.InnerException);
                    clientStream.ExceptionToThrow = null;

                    // Validate that the SslStream continues to be writable. However, the stream is still largely
                    // unusable: because the previously encrypted data won't have been written to the underlying
                    // stream and thus not received by the reader, if we tried to validate this data being received
                    // by the reader, it would likely fail with a decryption error.
                    await WriteAsync(clientSslStream, new byte[1] {
                        42
                    }, 0, 1);
                }
        }
コード例 #3
0
        public async Task Write_CorrectlyUnlocksAfterFailure()
        {
            var network      = new VirtualNetwork();
            var clientStream = new ThrowingDelegatingStream(new VirtualNetworkStream(network, isServer: false));

            using (var clientSslStream = new SslStream(clientStream, false, AllowAnyServerCertificate))
                using (var serverSslStream = new SslStream(new VirtualNetworkStream(network, isServer: true)))
                {
                    await DoHandshake(clientSslStream, serverSslStream);

                    // Throw an exception from the wrapped stream's write operation
                    clientStream.ExceptionToThrow = new FormatException();
                    IOException thrown = await Assert.ThrowsAsync <IOException>(() => WriteAsync(clientSslStream, new byte[1], 0, 1));

                    Assert.Same(clientStream.ExceptionToThrow, thrown.InnerException);
                    clientStream.ExceptionToThrow = null;

                    // Validate that the SslStream continues to be usable
                    for (byte b = 42; b < 52; b++)
                    {
                        await WriteAsync(clientSslStream, new byte[1] {
                            b
                        }, 0, 1);

                        byte[] buffer = new byte[1];
                        Assert.Equal(1, await ReadAsync(serverSslStream, buffer, 0, 1));
                        Assert.Equal(b, buffer[0]);
                    }
                }
        }