use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class WriteTest method can_write_updating_object_instance.
@Test
public void can_write_updating_object_instance() throws InterruptedException {
// setup server object
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(1, 0, 3, 60));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// write server object
LwM2mResource lifetime = LwM2mSingleResource.newIntegerResource(1, 120);
LwM2mResource defaultMinPeriod = LwM2mSingleResource.newIntegerResource(2, 10);
response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.UPDATE, 1, 0, lifetime, defaultMinPeriod));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read the values to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(1, 0));
LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
assertEquals(lifetime, instance.getResource(1));
assertEquals(defaultMinPeriod, instance.getResource(2));
// no resources are removed when updating
assertNotNull(instance.getResource(3));
assertNotNull(instance.getResource(6));
assertNotNull(instance.getResource(7));
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class SecurityTest method dont_sent_request_if_identity_change.
@Test
public void dont_sent_request_if_identity_change() throws NonUniqueSecurityInfoException, InterruptedException, IOException {
// Create PSK server & start it
// default server support PSK
helper.createServer();
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 client is not registered
helper.assertClientNotRegisterered();
// Start it and wait for registration
helper.client.start();
helper.waitForRegistration(1);
// Check client is well registered
helper.assertClientRegisterered();
// Ensure we can send a read request
helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1));
// Pause the client
// helper.client.stop(false);
// Add new credential to the server
helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(GOOD_ENDPOINT, "anotherPSK", GOOD_PSK_KEY));
// Get connector
Endpoint endpoint = helper.client.getCoapServer().getEndpoint(helper.client.getSecuredAddress());
DTLSConnector connector = (DTLSConnector) ((CoapEndpoint) endpoint).getConnector();
// Clear DTLS session to force new handshake
connector.clearConnectionState();
// Change PSK idea
helper.setNewPsk(helper.client, "anotherPSK");
// restart connector
connector.start();
// send and empty message to force a new handshake with new credentials
SimpleMessageCallback callback = new SimpleMessageCallback();
connector.send(RawData.outbound(new byte[0], new AddressEndpointContext(helper.server.getSecuredAddress()), callback, false));
// Wait until new handshake DTLS is done
EndpointContext endpointContext = callback.getEndpointContext(1000);
assertEquals(endpointContext.getPeerIdentity().getName(), "anotherPSK");
// Try to send a read request this should failed with an SendFailedException.
try {
helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1), 1000);
fail("send must failed");
} catch (SendFailedException e) {
assertTrue("must be caused by an EndpointMismatchException", e.getCause() instanceof EndpointMismatchException);
} finally {
connector.stop();
helper.client.destroy(false);
helper.client = null;
}
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class WriteTest method write_string_resource.
private void write_string_resource(ContentFormat format) throws InterruptedException {
// write resource
String expectedvalue = "stringvalue";
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, STRING_RESOURCE_ID, expectedvalue));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read resource to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, STRING_RESOURCE_ID));
LwM2mResource resource = (LwM2mResource) readResponse.getContent();
assertEquals(expectedvalue, resource.getValue());
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class CoapRequestBuilderTest method build_read_request_with_root_path.
@Test
public void build_read_request_with_root_path() throws Exception {
Registration reg = newRegistration("/");
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
ReadRequest request = new ReadRequest(3);
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals("coap://127.0.0.1:12354/3", coapRequest.getURI());
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class CoapRequestBuilderTest method build_read_request_with_non_default_object_path.
@Test
public void build_read_request_with_non_default_object_path() throws Exception {
Registration reg = newRegistration("/lwm2m");
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
ReadRequest request = new ReadRequest(3, 0, 1);
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals("coap://127.0.0.1:12354/lwm2m/3/0/1", coapRequest.getURI());
}
Aggregations