diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/DefaultRegistrationDataExtractor.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/DefaultRegistrationDataExtractor.java index 2687e97e97..8e19c4aa8c 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/DefaultRegistrationDataExtractor.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/DefaultRegistrationDataExtractor.java @@ -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) { diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationDataExtractor.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationDataExtractor.java index 2b11cfb67e..6361ef92c5 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationDataExtractor.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationDataExtractor.java @@ -29,6 +29,9 @@ */ public interface RegistrationDataExtractor { + /** + * @return extracted {@link RegistrationData} or null if {@link LwM2mVersion} is not supported + */ RegistrationData extractDataFromObjectLinks(Link[] objectLinks, LwM2mVersion lwM2mVersion); public class RegistrationData { diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationHandler.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationHandler.java index 74bfa1da52..d62cda7413 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationHandler.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationHandler.java @@ -77,6 +77,9 @@ public SendableResponse 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( diff --git a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationHandlerTest.java b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationHandlerTest.java index 93734bbde8..59863c4d62 100644 --- a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationHandlerTest.java +++ b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationHandlerTest.java @@ -16,6 +16,7 @@ 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; @@ -23,6 +24,8 @@ 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; @@ -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; @@ -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 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)); } @@ -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(",,".getBytes()), null); } catch (LinkParseException e) { throw new IllegalStateException(e);