Skip to content

Commit

Permalink
Add some tests about Leshan timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 11, 2020
1 parent 623eecf commit dd51dac
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package org.eclipse.leshan.integration.tests;

import static org.junit.Assert.assertEquals;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -132,7 +134,7 @@ public void async_send_without_acknowleged() throws Exception {
// Request should timedout in ~1s we don't send ACK
callback.waitForResponse(1500);
Assert.assertTrue("we should timeout", callback.getException() instanceof TimeoutException);

assertEquals(TimeoutException.Type.COAP_TIMEOUT, ((TimeoutException) callback.getException()).getType());
}

@Test
Expand All @@ -157,5 +159,6 @@ public void async_send_with_acknowleged_request_without_response() throws Except
Assert.assertTrue("we should still wait for response", callback.getException() == null);
callback.waitForResponse(2000);
Assert.assertTrue("we should timeout", callback.getException() instanceof TimeoutException);
assertEquals(TimeoutException.Type.RESPONSE_TIMEOUT, ((TimeoutException) callback.getException()).getType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ protected LeshanServerBuilder createServerBuilder() {
LeshanServerBuilder builder = super.createServerBuilder();
securityStore = new InMemorySecurityStore();
builder.setSecurityStore(securityStore);
Builder dtlsConfig = new DtlsConnectorConfig.Builder();
dtlsConfig.setMaxRetransmissions(1);
dtlsConfig.setRetransmissionTimeout(300);
builder.setDtlsConfig(dtlsConfig);
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.californium.scandium.dtls.DTLSSession;
import org.eclipse.leshan.core.request.ReadRequest;
import org.eclipse.leshan.core.request.exception.SendFailedException;
import org.eclipse.leshan.core.request.exception.TimeoutException;
import org.eclipse.leshan.core.response.ReadResponse;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.security.EditableSecurityStore;
Expand Down Expand Up @@ -268,6 +269,46 @@ public void server_initiates_dtls_handshake() throws NonUniqueSecurityInfoExcept
assertNotNull(session);
}

@Test
public void server_initiates_dtls_handshake_timeout() throws NonUniqueSecurityInfoException, InterruptedException {
// Create PSK server & start it
helper.createServer(); // default server support PSK
helper.server.start();

// Create PSK Client
helper.createPSKClient();

// Add client credentials to the server
helper.getSecurityStore()
.add(SecurityInfo.newPreSharedKeyInfo(helper.getCurrentEndpoint(), GOOD_PSK_ID, GOOD_PSK_KEY));

// Check for registration
helper.assertClientNotRegisterered();
helper.client.start();
helper.waitForRegistrationAtServerSide(1);
Registration registration = helper.getCurrentRegistration();
helper.assertClientRegisterered();

// Remove DTLS connection at server side.
((DTLSConnector) helper.server.coap().getSecuredEndpoint().getConnector()).clearConnectionState();

// stop client
helper.client.stop(false);

// try to send request synchronously
ReadResponse readResponse = helper.server.send(registration, new ReadRequest(3), 1000);
assertNull(readResponse);

// try to send request asynchronously
Callback<ReadResponse> callback = new Callback<>();
helper.server.send(registration, new ReadRequest(3), 1000, callback, callback);
callback.waitForResponse(1100);
assertTrue(callback.getException() instanceof TimeoutException);
assertEquals(TimeoutException.Type.DTLS_HANDSHAKE_TIMEOUT,
((TimeoutException) callback.getException()).getType());

}

@Test
public void server_does_not_initiate_dtls_handshake_with_queue_mode()
throws NonUniqueSecurityInfoException, InterruptedException {
Expand Down

0 comments on commit dd51dac

Please sign in to comment.