From 4aa4f0f3e520fee702966ad82023d93cf948870b Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 11 Jun 2024 19:27:46 +0300 Subject: [PATCH 1/4] fix_bug: lwm2m Bootstrap ShortServerId --- .../dao/service/validator/DeviceProfileDataValidator.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java index 0a8d78fb86..0d14dbe7ed 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java @@ -330,9 +330,7 @@ public class DeviceProfileDataValidator extends AbstractHasOtaPackageValidator 65534)) { throw new DeviceCredentialsValidationException("LwM2M Server ShortServerId must not be less than 1 and more than 65534!"); } - if (serverConfig.isBootstrapServerIs() && !(serverConfig.getShortServerId() == null || serverConfig.getShortServerId() ==0)) { - throw new DeviceCredentialsValidationException("Bootstrap Server ShortServerId must be null or '0'!"); - } + String server = serverConfig.isBootstrapServerIs() ? "Bootstrap Server" : "LwM2M Server"; if (!shortServerIds.add(serverConfig.getShortServerId())) { throw new DeviceCredentialsValidationException(server + " \"Short server Id\" value = " + serverConfig.getShortServerId() + ". This value must be a unique value for all servers!"); From d5f954d3df6dca19c10793cac55f5fc315829b19 Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 12 Jun 2024 07:45:14 +0300 Subject: [PATCH 2/4] fix_bug: lwm2m Bootstrap ShortServerId must not be less than 0 and more than 65535! --- .../dao/service/validator/DeviceProfileDataValidator.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java index 0d14dbe7ed..a26b419d9c 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java @@ -331,6 +331,10 @@ public class DeviceProfileDataValidator extends AbstractHasOtaPackageValidator 65535)) { + throw new DeviceCredentialsValidationException("Bootstrap Server ShortServerId must not be less than 0 and more than 65535!"); + } + String server = serverConfig.isBootstrapServerIs() ? "Bootstrap Server" : "LwM2M Server"; if (!shortServerIds.add(serverConfig.getShortServerId())) { throw new DeviceCredentialsValidationException(server + " \"Short server Id\" value = " + serverConfig.getShortServerId() + ". This value must be a unique value for all servers!"); From 242b0019a2d41f37301987541f3bd3c98bc23238 Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 12 Jun 2024 08:41:22 +0300 Subject: [PATCH 3/4] fix_bug: lwm2m Bootstrap ShortServerId must not be less than 0 and more than 65535! -2 --- .../dao/service/validator/DeviceProfileDataValidator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java index a26b419d9c..3095de6bb9 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java @@ -331,7 +331,7 @@ public class DeviceProfileDataValidator extends AbstractHasOtaPackageValidator 65535)) { + if (serverConfig.isBootstrapServerIs() && (serverConfig.getShortServerId() < 0 || serverConfig.getShortServerId() > 65535)) { throw new DeviceCredentialsValidationException("Bootstrap Server ShortServerId must not be less than 0 and more than 65535!"); } From 8420de0cb467cc75d6d61a0a67cdd5e3e51fc26d Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 12 Jun 2024 12:53:17 +0300 Subject: [PATCH 4/4] fix_bug: lwm2m Bootstrap ShortServerId must not be less than 0 and more than 65535! add test --- .../validator/DeviceProfileDataValidator.java | 18 ++-- .../DeviceProfileDataValidatorTest.java | 88 +++++++++++++++++++ 2 files changed, 101 insertions(+), 5 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java index 3095de6bb9..2d2c7bf21c 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java @@ -327,12 +327,20 @@ public class DeviceProfileDataValidator extends AbstractHasOtaPackageValidator 65534)) { - throw new DeviceCredentialsValidationException("LwM2M Server ShortServerId must not be less than 1 and more than 65534!"); - } - if (serverConfig.isBootstrapServerIs() && (serverConfig.getShortServerId() < 0 || serverConfig.getShortServerId() > 65535)) { - throw new DeviceCredentialsValidationException("Bootstrap Server ShortServerId must not be less than 0 and more than 65535!"); + if (serverConfig.getShortServerId() != null) { + if (serverConfig.isBootstrapServerIs()){ + if(serverConfig.getShortServerId() < 0 || serverConfig.getShortServerId() > 65535){ + throw new DeviceCredentialsValidationException("Bootstrap Server ShortServerId must be in range [0 - 65535]!"); + } + } else { + if (serverConfig.getShortServerId() < 1 || serverConfig.getShortServerId() > 65534) { + throw new DeviceCredentialsValidationException("LwM2M Server ShortServerId must be in range [1 - 65534]!"); + } + } + } else { + String serverName = serverConfig.isBootstrapServerIs() ? "Bootstrap Server" : "LwM2M Server"; + throw new DeviceCredentialsValidationException(serverName + " ShortServerId must not be null!"); } String server = serverConfig.isBootstrapServerIs() ? "Bootstrap Server" : "LwM2M Server"; diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java index ac0fa2cfe1..2da8a7ff45 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java @@ -20,11 +20,18 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfileType; import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration; import org.thingsboard.server.common.data.device.profile.DeviceProfileData; +import org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration; +import org.thingsboard.server.common.data.device.profile.lwm2m.OtherConfiguration; +import org.thingsboard.server.common.data.device.profile.lwm2m.TelemetryMappingConfiguration; +import org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.AbstractLwM2MBootstrapServerCredential; +import org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.LwM2MBootstrapServerCredential; +import org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.NoSecLwM2MBootstrapServerCredential; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.dao.dashboard.DashboardService; import org.thingsboard.server.dao.device.DeviceDao; @@ -34,6 +41,8 @@ import org.thingsboard.server.dao.queue.QueueService; import org.thingsboard.server.dao.rule.RuleChainService; import org.thingsboard.server.dao.tenant.TenantService; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import static org.mockito.BDDMockito.willReturn; @@ -42,6 +51,34 @@ import static org.mockito.Mockito.verify; @SpringBootTest(classes = DeviceProfileDataValidator.class) class DeviceProfileDataValidatorTest { + private static final String OBSERVE_ATTRIBUTES_WITHOUT_PARAMS = + " {\n" + + " \"keyName\": {},\n" + + " \"observe\": [],\n" + + " \"attribute\": [],\n" + + " \"telemetry\": [],\n" + + " \"attributeLwm2m\": {}\n" + + " }"; + + public static final String CLIENT_LWM2M_SETTINGS = + " {\n" + + " \"edrxCycle\": null,\n" + + " \"powerMode\": \"DRX\",\n" + + " \"fwUpdateResource\": null,\n" + + " \"fwUpdateStrategy\": 1,\n" + + " \"psmActivityTimer\": null,\n" + + " \"swUpdateResource\": null,\n" + + " \"swUpdateStrategy\": 1,\n" + + " \"pagingTransmissionWindow\": null,\n" + + " \"clientOnlyObserveAfterConnect\": 1\n" + + " }"; + + private static final String host = "localhost"; + private static final String hostBs = "localhost"; + + private static final int port = 5685; + private static final int portBs = 5687; + @MockBean DeviceProfileDao deviceProfileDao; @MockBean @@ -79,5 +116,56 @@ class DeviceProfileDataValidatorTest { validator.validateDataImpl(tenantId, deviceProfile); verify(validator).validateString("Device profile name", deviceProfile.getName()); } + @Test + void testValidateDeviceProfile_Lwm2mBootstrap_ShortServerId_Ok() { + Integer shortServerId = 123; + Integer shortServerIdBs = 0; + Lwm2mDeviceProfileTransportConfiguration transportConfiguration = + getTransportConfiguration(OBSERVE_ATTRIBUTES_WITHOUT_PARAMS, getBootstrapServerCredentialsNoSec(shortServerId, shortServerIdBs)); + DeviceProfile deviceProfile = getDeviceProfile(transportConfiguration); + + validator.validateDataImpl(tenantId, deviceProfile); + verify(validator).validateString("Device profile name", deviceProfile.getName()); + } + + private DeviceProfile getDeviceProfile(Lwm2mDeviceProfileTransportConfiguration transportConfiguration) { + DeviceProfile deviceProfile = new DeviceProfile(); + deviceProfile.setName("default"); + deviceProfile.setType(DeviceProfileType.DEFAULT); + deviceProfile.setTransportType(DeviceTransportType.LWM2M); + DeviceProfileData data = new DeviceProfileData(); + data.setTransportConfiguration(transportConfiguration); + deviceProfile.setProfileData(data); + deviceProfile.setTenantId(tenantId); + return deviceProfile; + } + + private Lwm2mDeviceProfileTransportConfiguration getTransportConfiguration(String observeAttr, List bootstrapServerCredentials) { + Lwm2mDeviceProfileTransportConfiguration transportConfiguration = new Lwm2mDeviceProfileTransportConfiguration(); + TelemetryMappingConfiguration observeAttrConfiguration = JacksonUtil.fromString(observeAttr, TelemetryMappingConfiguration.class); + OtherConfiguration clientLwM2mSettings = JacksonUtil.fromString(CLIENT_LWM2M_SETTINGS, OtherConfiguration.class); + transportConfiguration.setBootstrapServerUpdateEnable(true); + transportConfiguration.setObserveAttr(observeAttrConfiguration); + transportConfiguration.setClientLwM2mSettings(clientLwM2mSettings); + transportConfiguration.setBootstrap(bootstrapServerCredentials); + return transportConfiguration; + } + + private List getBootstrapServerCredentialsNoSec(Integer shortServerId, Integer shortServerIdBs){ + List bootstrap = new ArrayList<>(); + bootstrap.add(getBootstrapServerCredentialNoSec(false, shortServerId, shortServerIdBs)); + bootstrap.add(getBootstrapServerCredentialNoSec(true, shortServerId, shortServerIdBs)); + return bootstrap; + } + + private AbstractLwM2MBootstrapServerCredential getBootstrapServerCredentialNoSec(boolean isBootstrap, Integer shortServerId, Integer shortServerIdBs) { + AbstractLwM2MBootstrapServerCredential bootstrapServerCredential = new NoSecLwM2MBootstrapServerCredential(); + bootstrapServerCredential.setServerPublicKey(""); + bootstrapServerCredential.setShortServerId(isBootstrap ? shortServerIdBs : shortServerId); + bootstrapServerCredential.setBootstrapServerIs(isBootstrap); + bootstrapServerCredential.setHost(isBootstrap ? hostBs : host); + bootstrapServerCredential.setPort(isBootstrap ? portBs : port); + return bootstrapServerCredential; + } }