Skip to content

Commit

Permalink
GH-1279 : check if lwm2m version is supported at registration time
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jan 20, 2025
1 parent 8cdd24b commit 388a4d0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class DefaultRegistrationDataExtractor implements RegistrationDataExtract

@Override
public RegistrationData extractDataFromObjectLinks(Link[] objectLinks, LwM2mVersion lwM2mVersion) {
if (!lwM2mVersion.isSupported()) {
return null;
}

RegistrationData data = new RegistrationData();
if (objectLinks != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
*/
public interface RegistrationDataExtractor {

/**
* @return extracted {@link RegistrationData} or <code>null</code> if {@link LwM2mVersion} is not supported
*/
RegistrationData extractDataFromObjectLinks(Link[] objectLinks, LwM2mVersion lwM2mVersion);

public class RegistrationData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public SendableResponse<RegisterResponse> register(LwM2mPeer sender, RegisterReq
LwM2mVersion lwM2mVersion = LwM2mVersion.get(registerRequest.getLwVersion());
RegistrationData objLinksData = dataExtractor.extractDataFromObjectLinks(registerRequest.getObjectLinks(),
lwM2mVersion);
if (objLinksData == null) {
return new SendableResponse<>(RegisterResponse.preconditionFailed("unsupported lwm2m version"));
}

// Create Registration from RegisterRequest
Registration.Builder builder = new Registration.Builder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
package org.eclipse.leshan.server.registration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.endpoint.EndpointUri;
import org.eclipse.leshan.core.link.DefaultLinkParser;
import org.eclipse.leshan.core.link.LinkParseException;
Expand All @@ -32,6 +35,8 @@
import org.eclipse.leshan.core.request.RegisterRequest;
import org.eclipse.leshan.core.request.UpdateRequest;
import org.eclipse.leshan.core.request.UplinkRequest;
import org.eclipse.leshan.core.response.RegisterResponse;
import org.eclipse.leshan.core.response.SendableResponse;
import org.eclipse.leshan.server.security.Authorizer;
import org.eclipse.leshan.servers.DefaultServerEndpointNameProvider;
import org.eclipse.leshan.servers.security.Authorization;
Expand Down Expand Up @@ -114,6 +119,18 @@ public void test_update_without_application_data_from_authorizer() {
assertEquals(appData, registration.getApplicationData());
}

@Test
public void test_unsupported_lwm2m_version() {
// handle REGISTER request
SendableResponse<RegisterResponse> response = registrationHandler.register(givenIdentity(),
givenRegisterRequestWithEndpoint("myEndpoint", LwM2mVersion.get("1.2")), givenServerEndpointUri());
assertEquals(response.getResponse().getCode(), ResponseCode.PRECONDITION_FAILED);

// check result
Registration registration = registrationStore.getRegistrationByEndpoint("myEndpoint");
assertNull(registration);
}

private IpPeer givenIdentity() {
return new IpPeer(new InetSocketAddress(0));
}
Expand All @@ -123,8 +140,12 @@ private EndpointUri givenServerEndpointUri() {
}

private RegisterRequest givenRegisterRequestWithEndpoint(String endpoint) {
return givenRegisterRequestWithEndpoint(endpoint, LwM2mVersion.V1_1);
}

private RegisterRequest givenRegisterRequestWithEndpoint(String endpoint, LwM2mVersion version) {
try {
return new RegisterRequest(endpoint, 3600l, "1.1", EnumSet.of(BindingMode.U), false, null,
return new RegisterRequest(endpoint, 3600l, version.toString(), EnumSet.of(BindingMode.U), false, null,
new DefaultLinkParser().parseCoreLinkFormat("</1/0/1>,</2/1>,</3>".getBytes()), null);
} catch (LinkParseException e) {
throw new IllegalStateException(e);
Expand Down

0 comments on commit 388a4d0

Please sign in to comment.