diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java index adb97196fd..ccddcb16bf 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java @@ -85,6 +85,17 @@ public class AssetServiceTest extends AbstractServiceTest { }); } + @Test + public void testSaveDeviceWithNameContains0x00_thenDataValidationException() { + Asset asset = new Asset(); + asset.setTenantId(tenantId); + asset.setType("default"); + asset.setName("F0929906\000\000\000\000\000\000\000\000\000"); + Assertions.assertThrows(DataValidationException.class, () -> { + assetService.saveAsset(asset); + }); + } + @Test public void testSaveAssetWithEmptyTenant() { Asset asset = new Asset(); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetDataValidatorTest.java new file mode 100644 index 0000000000..c26f02ee1f --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetDataValidatorTest.java @@ -0,0 +1,89 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.asset.AssetDao; +import org.thingsboard.server.dao.customer.CustomerDao; +import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.ThrowableAssert.catchThrowableOfType; +import static org.mockito.BDDMockito.willReturn; + +@SpringBootTest(classes = AssetDataValidator.class) +@Slf4j +class AssetDataValidatorTest { + + @MockBean + AssetDao assetDao; + @MockBean + TenantService tenantService; + @MockBean + CustomerDao customerDao; + @Autowired + AssetDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @ParameterizedTest + @ValueSource(strings = { + "coffee", "1", "big box", "世界", "!", "--", "~!@#$%^&*()_+=-/|\\[]{};:'`\"?<>,.", "\uD83D\uDC0C", "\041", + "Gdy Pomorze nie pomoże, to pomoże może morze, a gdy morze nie pomoże, to pomoże może Gdańsk", + }) + void testAssetName_thenOK(final String name) { + Asset asset = new Asset(); + asset.setTenantId(tenantId); + asset.setName(name); + validator.validateDataImpl(tenantId, asset); + } + + @ParameterizedTest + @ValueSource(strings = { + "", " ", " ", "\n", "\r\n", "\t", "\000", "\000\000", "\001", "\002", "\040", "\u0000", "\u0000\u0000", + "F0929906\000\000\000\000\000\000\000\000\000", "\000\000\000F0929906", + "\u0000F0929906", "F092\u00009906", "F0929906\u0000" + }) + void testAssetName_thenDataValidationException(final String name) { + Asset asset = new Asset(); + asset.setTenantId(tenantId); + asset.setName(name); + + DataValidationException exception = catchThrowableOfType(() -> + validator.validateDataImpl(tenantId, asset), DataValidationException.class); + log.warn("Exception message: {}", exception == null ? null : exception.getMessage()); + + assertThatThrownBy(() -> validator.validateDataImpl(tenantId, asset)) + .isInstanceOf(DataValidationException.class) + .hasMessageMatching(".*Asset.*"); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java index 4e023ad6ac..b1a83b034e 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java @@ -84,7 +84,7 @@ class DeviceDataValidatorTest { assertThatThrownBy(() -> validator.validateDataImpl(tenantId, device)) .isInstanceOf(DataValidationException.class) - .hasMessageMatching(".*\\S.*"); + .hasMessageMatching(".*Device.*"); } } diff --git a/dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java b/dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java index ef68465256..1ffb740553 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java @@ -44,6 +44,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -94,6 +95,12 @@ public class JpaAssetDaoTest extends AbstractJpaDaoTest { savedAssetProfiles.clear(); } + @Test + public void testSaveDeviceName0x00_thenSomeDatabaseException() { + assertThatThrownBy(() -> assets.add( + saveAsset(UUID.randomUUID(), tenantId2, customerId2, "F0929906\000\000\000\000\000\000\000\000\000"))); + } + @Test public void testFindAssetsByTenantId() { PageLink pageLink = new PageLink(20, 0, "ASSET_");