Пример #1
0
    public void ShouldThrowIOExceptionNotInvalidOperationExceptionTest()
    {
        var stream     = new UnreadableStream();
        var serializer = new BinarySerializer();

        Assert.ThrowsException <IOException>(() => serializer.Deserialize <int>(stream));
    }
Пример #2
0
 public void ConstructorErrors()
 {
     Assert.Throws <ArgumentNullException>(() => _readBuffer = new TestHarness.BufferedReadStream(null));
     using (var urs = new UnreadableStream(_baseStream))
     {
         Assert.Throws <ArgumentException>(() => new TestHarness.BufferedReadStream(urs));
     }
 }
 public void ConstructorErrors()
 {
     Assert.Throws<ArgumentNullException>(() => _readBuffer = new TestHarness.BufferedReadStream(null));
     using (var urs = new UnreadableStream(_baseStream))
     {
         Assert.Throws<ArgumentException>(() => new TestHarness.BufferedReadStream(urs));
     }
 }
Пример #4
0
        /// <summary>
        /// Reads the TLS Client Hello from the tcp stream, parsing out the server name provided by the Server Name extension. Returns a stream that behaves as if nothing had been read.
        /// </summary>
        /// <param name="tcpStream">The stream that is expected to begin a TLS 1.0, 1.1, or 1.2 handshake.</param>
        /// <param name="serverName">The server name from the TLS Server Name extension.  Or null.</param>
        /// <returns></returns>
        /// <exception cref="Exception">Throws if there is a problem parsing the TLS Client Hello.</exception>
        public static Stream Read(Stream tcpStream, out string serverName)
        {
            UnreadableStream unread = new UnreadableStream(tcpStream);

            FragmentStream readerOfClientHello = new FragmentStream(() =>
            {
                TLSPlaintext fragment = new TLSPlaintext(tcpStream);
                unread.Unread(fragment.data_header);
                unread.Unread(fragment.data_fragment);

                if (fragment.type != ContentType.handshake)
                {
                    throw new Exception("TLS protocol error: Fragment began with byte " + (byte)fragment.type + ". Expected " + (byte)ContentType.handshake);
                }
                if (!fragment.version.IsSupported())
                {
                    throw new Exception("Unsupported TLS protocol version: " + fragment.version);
                }

                return(fragment);
            });

            HandshakeMessage firstHandshakeMessage = new HandshakeMessage(new BasicDataStream(readerOfClientHello));

            if (firstHandshakeMessage.msg_type == HandshakeType.client_hello)
            {
                ClientHello clientHello = firstHandshakeMessage.body as ClientHello;
                serverName = clientHello.serverName;
            }
            else
            {
                serverName = null;
            }

            return(unread);
        }