|
92 | 92 | import org.eclipse.californium.scandium.dtls.HandshakeMessage; |
93 | 93 | import org.eclipse.californium.scandium.dtls.HandshakeType; |
94 | 94 | import org.eclipse.californium.scandium.dtls.Handshaker; |
| 95 | +import org.eclipse.californium.scandium.dtls.HelloRequest; |
95 | 96 | import org.eclipse.californium.scandium.dtls.HelloVerifyRequest; |
96 | 97 | import org.eclipse.californium.scandium.dtls.InMemoryConnectionStore; |
97 | 98 | import org.eclipse.californium.scandium.dtls.InMemorySessionCache; |
@@ -1676,6 +1677,26 @@ public void testDestroyClearsConnectionStore() throws Exception { |
1676 | 1677 | assertThat(clientConnectionStore.get(serverEndpoint), is(nullValue())); |
1677 | 1678 | } |
1678 | 1679 |
|
| 1680 | + @Test |
| 1681 | + public void testNoRenegotiationOnHelloRequest() throws Exception { |
| 1682 | + givenAnEstablishedSession(false); |
| 1683 | + |
| 1684 | + // Catch alert receive by the server |
| 1685 | + SingleAlertCatcher alertCatcher = new SingleAlertCatcher(); |
| 1686 | + server.setAlertHandler(alertCatcher); |
| 1687 | + |
| 1688 | + // send a HELLO_REQUEST message to the client |
| 1689 | + server.sendRecord(new Record(ContentType.HANDSHAKE, establishedServerSession.getWriteEpoch(), |
| 1690 | + establishedServerSession.getSequenceNumber(), new HelloRequest(clientEndpoint), |
| 1691 | + establishedServerSession)); |
| 1692 | + |
| 1693 | + // ensure client answer with a NO_RENOGIATION alert |
| 1694 | + AlertMessage alert = alertCatcher.waitForFirstAlert(MAX_TIME_TO_WAIT_SECS, TimeUnit.SECONDS); |
| 1695 | + assertNotNull("Server does not receive alert as answer of HELLO_REQUEST", alert); |
| 1696 | + assertEquals("Client must answer to HELLO_REQUEST with a NO_RENEGOTIATION alert", AlertDescription.NO_RENEGOTIATION, alert.getDescription()); |
| 1697 | + assertEquals("NO_RENEGOTIATION alert MUST be a warning", AlertLevel.WARNING, alert.getLevel()); |
| 1698 | + } |
| 1699 | + |
1679 | 1700 | private ClientHello createClientHello() { |
1680 | 1701 | return createClientHello(null); |
1681 | 1702 | } |
@@ -2060,4 +2081,30 @@ public void sendRecord(InetSocketAddress peerAddress, byte[] record) throws IOEx |
2060 | 2081 | } |
2061 | 2082 | } |
2062 | 2083 | } |
| 2084 | + |
| 2085 | + private class SingleAlertCatcher implements AlertHandler { |
| 2086 | + |
| 2087 | + private CountDownLatch latch = new CountDownLatch(1); |
| 2088 | + private AlertMessage alert; |
| 2089 | + |
| 2090 | + @Override |
| 2091 | + public void onAlert(InetSocketAddress peer, AlertMessage alert) { |
| 2092 | + if (latch.getCount() != 0) { |
| 2093 | + this.alert = alert; |
| 2094 | + latch.countDown(); |
| 2095 | + } |
| 2096 | + } |
| 2097 | + |
| 2098 | + /** |
| 2099 | + * @return {@code AlertMessage} if the count reached zero and {@code n} |
| 2100 | + * if the waiting time elapsed before the count reached zero |
| 2101 | + */ |
| 2102 | + public AlertMessage waitForFirstAlert(long timeout, TimeUnit unit) throws InterruptedException { |
| 2103 | + if (latch.await(timeout, unit)) { |
| 2104 | + return alert; |
| 2105 | + } else { |
| 2106 | + return null; |
| 2107 | + } |
| 2108 | + } |
| 2109 | + } |
2063 | 2110 | } |
0 commit comments