Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that client use same endpoint for a given registration #1655

Open
wants to merge 4 commits into
base: clean_test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/*******************************************************************************
* Copyright (c) 2024 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.integration.tests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.leshan.integration.tests.util.LeshanProxyBuilder.givenReverseProxyFor;
import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing;
import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing;
import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.stream.Stream;

import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.endpoint.Protocol;
import org.eclipse.leshan.core.link.LinkParseException;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.response.SendResponse;
import org.eclipse.leshan.integration.tests.util.Failure;
import org.eclipse.leshan.integration.tests.util.LeshanTestClient;
import org.eclipse.leshan.integration.tests.util.LeshanTestServer;
import org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder;
import org.eclipse.leshan.integration.tests.util.ReverseProxy;
import org.eclipse.leshan.server.registration.Registration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class MultiEndpointsTest {

/*---------------------------------/
* Parameterized Tests
* -------------------------------*/
@ParameterizedTest(name = "{0} - Client using {1} - Server using {2}")
@MethodSource("transports")
@Retention(RetentionPolicy.RUNTIME)
private @interface TestAllTransportLayer {
}

static Stream<org.junit.jupiter.params.provider.Arguments> transports() {
return Stream.of(//
// ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider
arguments(Protocol.COAP, "Californium", "Californium"), //
arguments(Protocol.COAP, "Californium", "java-coap"), //
arguments(Protocol.COAP, "java-coap", "Californium"), //
arguments(Protocol.COAP, "java-coap", "java-coap"));
}

/*---------------------------------/
* Set-up and Tear-down Tests
* -------------------------------*/

LeshanTestServer server;
LeshanTestClient client;
ReverseProxy proxy;

public void setupTestFor(LeshanTestServerBuilder serverBuilder, Protocol givenProtocol,
String givenClientEndpointProvider) {
// create and start server
server = serverBuilder.build();
server.start();

// create and start proxy
proxy = givenReverseProxyFor(server, givenProtocol);
proxy.start();

/// create and start client
client = givenClientUsing(givenProtocol).with(givenClientEndpointProvider).connectingTo(server).behind(proxy)
.build();
}

public LeshanTestServerBuilder givenServerWithTwoEndpoint(Protocol givenProtocol,
String givenServerEndpointProvider) {
return givenServerUsing(givenProtocol).with(givenServerEndpointProvider, givenServerEndpointProvider);
}

@AfterEach
public void stop() throws InterruptedException {
if (client != null)
client.destroy(false);
if (proxy != null)
proxy.stop();
if (server != null)
server.destroy();
}

/*---------------------------------/
* Tests
* -------------------------------*/

@TestAllTransportLayer
public void register_then_update_on_different_endpoint(Protocol givenProtocol, String givenClientEndpointProvider,
String givenServerEndpointProvider) throws LinkParseException {

// set-up test
setupTestFor(//
givenServerWithTwoEndpoint(givenProtocol, givenServerEndpointProvider), //
givenProtocol, //
givenClientEndpointProvider);

// Start it and wait for registration
client.start();
server.waitForNewRegistrationOf(client);
client.waitForRegistrationTo(server);

// Check client is well registered
assertThat(client).isRegisteredAt(server);
Registration registration = server.getRegistrationFor(client);

// Check for update
client.triggerRegistrationUpdate();
client.waitForUpdateTo(server);
server.waitForUpdateOf(registration);
assertThat(client).isRegisteredAt(server);

// Send request from client to another server endpoint.
proxy.useNextServerAddress();

// Check update failed
client.triggerRegistrationUpdate();
Failure failure = client.waitForUpdateFailureTo(server);
assertThat(failure).failedWith(ResponseCode.BAD_REQUEST);
}

@TestAllTransportLayer
public void register_then_deregister_on_different_endpoint(Protocol givenProtocol,
String givenClientEndpointProvider, String givenServerEndpointProvider) throws LinkParseException {
// set-up test
setupTestFor(//
givenServerWithTwoEndpoint(givenProtocol, givenServerEndpointProvider), //
givenProtocol, //
givenClientEndpointProvider);

// Start it and wait for registration
client.start();
server.waitForNewRegistrationOf(client);
client.waitForRegistrationTo(server);

// Check client is well registered
assertThat(client).isRegisteredAt(server);
Registration registration = server.getRegistrationFor(client);

// Check for update
client.triggerRegistrationUpdate();
client.waitForUpdateTo(server);
server.waitForUpdateOf(registration);
assertThat(client).isRegisteredAt(server);

// Send request from client to another server endpoint.
proxy.useNextServerAddress();

// Deregister
client.stop(true);
Failure failure = client.waitForDeregistrationFailureTo(server);
assertThat(failure).failedWith(ResponseCode.BAD_REQUEST);
}

@TestAllTransportLayer
public void register_then_send_on_different_endpoint(Protocol givenProtocol, String givenClientEndpointProvider,
String givenServerEndpointProvider) throws LinkParseException, InterruptedException {

register_then_send_on_different_endpoint(//
givenServerWithTwoEndpoint(givenProtocol, givenServerEndpointProvider), //
givenProtocol, //
givenClientEndpointProvider);
}

@TestAllTransportLayer
public void register_then_send_on_different_endpoint_with_update_on_send(Protocol givenProtocol,
String givenClientEndpointProvider, String givenServerEndpointProvider)
throws LinkParseException, InterruptedException {

register_then_send_on_different_endpoint(//
givenServerWithTwoEndpoint(givenProtocol, givenServerEndpointProvider).withUpdateOnSendOperation(), //
givenProtocol, //
givenClientEndpointProvider);
}

protected void register_then_send_on_different_endpoint(LeshanTestServerBuilder givenServer, Protocol givenProtocol,
String givenClientEndpointProvider) throws LinkParseException, InterruptedException {

// set-up test
setupTestFor(//
givenServer, //
givenProtocol, //
givenClientEndpointProvider);

// Start it and wait for registration
client.start();
server.waitForNewRegistrationOf(client);
client.waitForRegistrationTo(server);

// Check client is well registered
assertThat(client).isRegisteredAt(server);
Registration registration = server.getRegistrationFor(client);

// Check for update
client.triggerRegistrationUpdate();
client.waitForUpdateTo(server);
server.waitForUpdateOf(registration);
assertThat(client).isRegisteredAt(server);

// Send request from client to another server endpoint.
proxy.useNextServerAddress();

// Send Data
Registration registrationBeforeSend = server.getRegistrationFor(client);
LwM2mServer registeredServer = client.getRegisteredServers().values().iterator().next();
SendResponse response = client.getSendService().sendData(registeredServer, ContentFormat.SENML_JSON,
Arrays.asList("/3/0/1", "/3/0/2"), 1000);
assertThat(response).hasCode(ResponseCode.BAD_REQUEST);
Registration registrationAfterSend = server.getRegistrationFor(client);
assertThat(registrationAfterSend).isEqualTo(registrationBeforeSend);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public JsonNode jSerialize(Registration r) {
o.put("qm", r.getQueueMode());
o.put("ep", r.getEndpoint());
o.put("regId", r.getId());
o.put("epUri", r.getLastEndpointUsed().toString());
o.put("epUri", r.getEndpointUri().toString());

ArrayNode links = JsonNodeFactory.instance.arrayNode();
for (Link l : r.getObjectLinks()) {
Expand Down Expand Up @@ -173,20 +173,17 @@ public byte[] bSerialize(Registration r) {
}

public Registration deserialize(JsonNode jObj) {
EndpointUri lastEndpointUsed;
EndpointUri endpointUri;
try {
lastEndpointUsed = uriHandler.createUri(jObj.get("epUri").asText());
endpointUri = uriHandler.createUri(jObj.get("epUri").asText());
} catch (IllegalStateException e1) {
throw new IllegalStateException(
String.format("Unable to deserialize last endpoint used URI %s of registration %s/%s",
jObj.get("epUri").asText(), jObj.get("regId").asText(), jObj.get("ep").asText()));
}

// TODO handle backward compatibility ?
// Registration.Builder b = new Registration.Builder(jObj.get("regId").asText(), jObj.get("ep").asText(),
// IdentitySerDes.deserialize(jObj.get("identity")), lastEndpointUsed);
Registration.Builder b = new Registration.Builder(jObj.get("regId").asText(), jObj.get("ep").asText(),
peerSerDes.deserialize(jObj.get("transportdata")), lastEndpointUsed);
peerSerDes.deserialize(jObj.get("transportdata")), endpointUri);

b.bindingMode(BindingMode.parse(jObj.get("bnd").asText()));
if (jObj.get("qm") != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public LeshanServer(LwM2mServerEndpointsProvider endpointsProvider, Registration
presenceService = createPresenceService(registrationService, awakeTimeProvider,
updateRegistrationOnNotification);
}
this.sendService = createSendHandler(registrationStore, updateRegistrationOnSend);
this.sendService = createSendHandler(registrationStore, authorizer, updateRegistrationOnSend);

// create endpoints
ServerEndpointToolbox toolbox = new ServerEndpointToolbox(decoder, encoder, linkParser,
Expand Down Expand Up @@ -202,8 +202,9 @@ protected PresenceServiceImpl createPresenceService(RegistrationService registra
return presenceService;
}

protected SendHandler createSendHandler(RegistrationStore registrationStore, boolean updateRegistrationOnSend) {
return new SendHandler(registrationStore, updateRegistrationOnSend);
protected SendHandler createSendHandler(RegistrationStore registrationStore, Authorizer authorizer,
boolean updateRegistrationOnSend) {
return new SendHandler(registrationStore, authorizer, updateRegistrationOnSend);
}

protected UplinkDeviceManagementRequestReceiver createRequestReceiver(RegistrationHandler registrationHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ public class Registration {

private final Map<String, String> applicationData;

private final EndpointUri lastEndpointUsed;
// URI of endpoint used for this registration.
private final EndpointUri endpointUri;

protected Registration(Builder builder) {

// mandatory params
id = builder.registrationId;
clientTransportData = builder.clientTransportData;
endpoint = builder.endpoint;
lastEndpointUsed = builder.lastEndpointUsed;
endpointUri = builder.endpointUri;

// object links related params
objectLinks = builder.objectLinks;
Expand Down Expand Up @@ -355,20 +356,19 @@ public Map<String, String> getApplicationData() {

/**
* @return URI of the server endpoint used by client to register.
* <p>
* This can be changed in next milestones : https://github.com/eclipse/leshan/issues/1415
*/
public EndpointUri getLastEndpointUsed() {
return lastEndpointUsed;
public EndpointUri getEndpointUri() {
return endpointUri;
}

@Override
public String toString() {
return String.format(
"Registration [registrationDate=%s, clientTransportData=%s, lifeTimeInSec=%s, smsNumber=%s, lwM2mVersion=%s, bindingMode=%s, queueMode=%s, endpoint=%s, id=%s, objectLinks=%s, additionalRegistrationAttributes=%s, rootPath=%s, supportedContentFormats=%s, supportedObjects=%s, availableInstances=%s, lastUpdate=%s, applicationData=%s]",
"Registration [registrationDate=%s, clientTransportData=%s, lifeTimeInSec=%s, smsNumber=%s, lwM2mVersion=%s, bindingMode=%s, queueMode=%s, endpoint=%s, id=%s, objectLinks=%s, additionalRegistrationAttributes=%s, rootPath=%s, supportedContentFormats=%s, supportedObjects=%s, availableInstances=%s, lastUpdate=%s, applicationData=%s, endpointUri=%s]",
registrationDate, clientTransportData, lifeTimeInSec, smsNumber, lwM2mVersion, bindingMode, queueMode,
endpoint, id, Arrays.toString(objectLinks), additionalRegistrationAttributes, rootPath,
supportedContentFormats, supportedObjects, availableInstances, lastUpdate, applicationData);
supportedContentFormats, supportedObjects, availableInstances, lastUpdate, applicationData,
endpointUri);
}

@Override
Expand All @@ -390,22 +390,22 @@ public final boolean equals(Object o) {
&& Objects.equals(supportedObjects, that.supportedObjects)
&& Objects.equals(availableInstances, that.availableInstances)
&& Objects.equals(lastUpdate, that.lastUpdate) && Objects.equals(applicationData, that.applicationData)
&& Objects.equals(lastEndpointUsed, that.lastEndpointUsed);
&& Objects.equals(endpointUri, that.endpointUri);
}

@Override
public final int hashCode() {
return Objects.hash(registrationDate, clientTransportData, lifeTimeInSec, smsNumber, lwM2mVersion, bindingMode,
queueMode, endpoint, id, Arrays.hashCode(objectLinks), additionalRegistrationAttributes, rootPath,
supportedContentFormats, supportedObjects, availableInstances, lastUpdate, applicationData,
lastEndpointUsed);
endpointUri);
}

public static class Builder {
private final String registrationId;
private final String endpoint;
private final LwM2mPeer clientTransportData;
private final EndpointUri lastEndpointUsed;
private final EndpointUri endpointUri;

private Date registrationDate;
private Date lastUpdate;
Expand All @@ -428,7 +428,7 @@ public Builder(Registration registration) {
registrationId = registration.id;
clientTransportData = registration.clientTransportData;
endpoint = registration.endpoint;
lastEndpointUsed = registration.lastEndpointUsed;
endpointUri = registration.endpointUri;

// object links related params
objectLinks = registration.objectLinks;
Expand All @@ -450,18 +450,17 @@ public Builder(Registration registration) {
applicationData = registration.applicationData;
}

public Builder(String registrationId, String endpoint, LwM2mPeer clientTransportData,
EndpointUri lastEndpointUsed) {
public Builder(String registrationId, String endpoint, LwM2mPeer clientTransportData, EndpointUri endpointUri) {

Validate.notNull(registrationId);
Validate.notEmpty(endpoint);
Validate.notNull(clientTransportData);
Validate.notNull(lastEndpointUsed);
Validate.notNull(endpointUri);

this.registrationId = registrationId;
this.endpoint = endpoint;
this.clientTransportData = clientTransportData;
this.lastEndpointUsed = lastEndpointUsed;
this.endpointUri = endpointUri;
}

public Builder registrationDate(Date registrationDate) {
Expand Down
Loading