]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Add unit test illustrating a connection failure due to resent handshake records
authorMatthew Endsley <mendsley@gmail.com>
Fri, 2 Apr 2021 00:31:10 +0000 (17:31 -0700)
committerMatthew Endsley <mendsley@gmail.com>
Fri, 2 Apr 2021 00:31:10 +0000 (17:31 -0700)
Hazel.UnitTests/Dtls/ConnectionTests.cs

index 3279ce2430802d72036b306a845675928e016657..0b9073e88f1d068e80adf60c62e6cf609e399534 100644 (file)
@@ -226,5 +226,78 @@ IsdbLCwHYD3GVgk/D7NVxyU=
                 signal.WaitOne(100);
             }
         }
+
+        [TestMethod]
+        public void TestResentHandshakeConnects()
+        {
+            IPEndPoint captureEndPoint = new IPEndPoint(IPAddress.Loopback, 27511);
+            IPEndPoint listenerEndPoint = new IPEndPoint(IPAddress.Loopback, 27510);
+
+            bool serverConnected = false;
+            bool serverDisconnected = false;
+            bool clientDisconnected = false;
+
+            Semaphore signal = new Semaphore(0, int.MaxValue);
+
+            using (SocketCapture capture = new SocketCapture(captureEndPoint, listenerEndPoint))
+            using (DtlsConnectionListener listener = new DtlsConnectionListener(2, new IPEndPoint(IPAddress.Any, listenerEndPoint.Port), new TestLogger()))
+            using (DtlsUnityConnection connection = new DtlsUnityConnection(new TestLogger(), captureEndPoint))
+            {
+                Semaphore listenerToConnectionThrottle = new Semaphore(0, int.MaxValue);
+                capture.SendToLocalSemaphore = listenerToConnectionThrottle;
+                Thread throttleThread = new Thread(() => {
+                    // HelloVerifyRequest
+                    listenerToConnectionThrottle.Release(1);
+                    // ServerHello, Server Certificate
+                    listenerToConnectionThrottle.Release(1);
+
+                    // Trigger a resend of ServerHello, ServerCertificate
+                    Thread.Sleep(1000);
+                    listenerToConnectionThrottle.Release(1);
+
+
+                    // ServerKeyExchange, ServerHelloDone
+                    listenerToConnectionThrottle.Release(1);
+
+                    // Trigger a resend of ServerKeyExchange, ServerHelloDone
+                    Thread.Sleep(1000);
+                    listenerToConnectionThrottle.Release(1);
+
+                    capture.SendToLocalSemaphore = null;
+                });
+                throttleThread.Start();
+
+                listener.SetCertificate(GetCertificateForServer());
+                connection.SetValidServerCertificates(GetCertificateForClient());
+
+                listener.NewConnection += (evt) =>
+                {
+                    serverConnected = true;
+                    signal.Release();
+                    evt.Connection.Disconnected += (o, et) => {
+                        serverDisconnected = true;
+                    };
+                };
+                connection.Disconnected += (o, evt) => {
+                    clientDisconnected = true;
+                    signal.Release();
+                };
+
+                listener.Start();
+                connection.Connect();
+
+                // wait for the client to connect
+                signal.WaitOne(10);
+
+                listener.Dispose();
+
+                // wait for the client to disconnect
+                signal.WaitOne(100);
+
+                Assert.IsTrue(serverConnected);
+                Assert.IsTrue(clientDisconnected);
+                Assert.IsFalse(serverDisconnected);
+            }
+        }
     }
 }