public void TestMalformedApplicationData()
        {
            IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 27510);

            IPEndPoint connectionEndPoint = ep;

            DtlsConnectionListener.ConnectionId connectionId = new ThreadLimitedUdpConnectionListener.ConnectionId();

            Semaphore signal = new Semaphore(0, int.MaxValue);

            using (MalformedDTLSListener listener = new MalformedDTLSListener(2, new IPEndPoint(IPAddress.Any, ep.Port), new TestLogger()))
                using (DtlsUnityConnection connection = new DtlsUnityConnection(new TestLogger(), ep))
                {
                    listener.SetCertificate(GetCertificateForServer());
                    connection.SetValidServerCertificates(GetCertificateForClient());

                    listener.NewConnection += (evt) =>
                    {
                        connectionEndPoint = evt.Connection.EndPoint;
                        connectionId       = ((ThreadLimitedUdpServerConnection)evt.Connection).ConnectionId;

                        signal.Release();
                        evt.Connection.Disconnected += (o, et) => {
                        };
                    };
                    connection.Disconnected += (o, evt) => {
                        signal.Release();
                    };

                    listener.Start();
                    connection.Connect();

                    // wait for the client to connect
                    signal.WaitOne(10);

                    ByteSpan data = new byte[5] {
                        0x01, 0x02, 0x03, 0x04, 0x05
                    };

                    Record record = new Record();
                    record.ContentType    = ContentType.ApplicationData;
                    record.Epoch          = 1;
                    record.SequenceNumber = 10;
                    record.Length         = (ushort)data.Length;

                    ByteSpan encoded = new byte[Record.Size + data.Length];
                    record.Encode(encoded);
                    data.CopyTo(encoded.Slice(Record.Size));

                    listener.InjectPacket(encoded, connectionEndPoint, connectionId);

                    // wait for the client to disconnect
                    listener.Dispose();
                    signal.WaitOne(100);
                }
        }
Example #2
0
        public void TestMalformedConnectionData()
        {
            IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 27510);

            IPEndPoint connectionEndPoint = ep;

            DtlsConnectionListener.ConnectionId connectionId = new ThreadLimitedUdpConnectionListener.ConnectionId();

            Semaphore signal = new Semaphore(0, int.MaxValue);

            using (DtlsConnectionListener listener = new DtlsConnectionListener(2, new IPEndPoint(IPAddress.Any, ep.Port), new TestLogger()))
                using (MalformedDTLSClient connection = new MalformedDTLSClient(new TestLogger(), ep))
                {
                    listener.SetCertificate(GetCertificateForServer());
                    connection.SetValidServerCertificates(GetCertificateForClient());

                    listener.NewConnection += (evt) =>
                    {
                        connectionEndPoint = evt.Connection.EndPoint;
                        connectionId       = ((ThreadLimitedUdpServerConnection)evt.Connection).ConnectionId;

                        signal.Release();
                        evt.Connection.Disconnected += (o, et) => {
                        };
                    };
                    connection.Disconnected += (o, evt) => {
                        signal.Release();
                    };

                    listener.Start();
                    connection.Connect();

                    Assert.IsTrue(listener.ReceiveThreadRunning, "Listener should be able to handle a malformed hello packet");
                    Assert.AreEqual(ConnectionState.NotConnected, connection.State);

                    Assert.AreEqual(0, listener.PeerCount);

                    // wait for the client to disconnect
                    listener.Dispose();
                    signal.WaitOne(100);
                }
        }