31 changed files with 997 additions and 184 deletions
@ -0,0 +1,42 @@ |
|||||
|
package org.thingsboard.server.service.ota; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
import org.thingsboard.server.dao.ota.TbMultipartFile; |
||||
|
|
||||
|
import javax.validation.constraints.NotNull; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.util.Optional; |
||||
|
|
||||
|
@RequiredArgsConstructor |
||||
|
public class TbMultipartFileImp implements TbMultipartFile { |
||||
|
|
||||
|
@NotNull |
||||
|
private final MultipartFile file; |
||||
|
|
||||
|
@Override |
||||
|
public Optional<InputStream> getInputStream() { |
||||
|
try { |
||||
|
return Optional.of(file.getInputStream()); |
||||
|
} catch (IOException e) { |
||||
|
return Optional.empty(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getFileName() { |
||||
|
return file.getName(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public long getFileSize() { |
||||
|
return file.getSize(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getContentType() { |
||||
|
return file.getContentType(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,100 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 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.cache.ota.files; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.io.FileUtils; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.thingsboard.server.common.data.id.OtaPackageId; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.util.Optional; |
||||
|
import java.util.UUID; |
||||
|
import java.util.concurrent.ConcurrentHashMap; |
||||
|
import java.util.concurrent.ConcurrentMap; |
||||
|
|
||||
|
@Slf4j |
||||
|
@RequiredArgsConstructor |
||||
|
@Component |
||||
|
public class BaseFileCacheService implements FileCacheService { |
||||
|
@Value("${files.temporary_files_directory}/ota/") |
||||
|
private String PATH; |
||||
|
private final static String FILE_NAME_TEMPLATE = "%s.tmp"; |
||||
|
private final TemporaryFileCleaner fileCleaner; |
||||
|
private final ConcurrentMap<OtaPackageId, Boolean> files = new ConcurrentHashMap<>(); |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public File saveDataTemporaryFile(InputStream inputStream) { |
||||
|
File path = new File(PATH); |
||||
|
try { |
||||
|
File tempFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp", path); |
||||
|
FileUtils.copyInputStreamToFile(inputStream, tempFile); |
||||
|
return tempFile; |
||||
|
} catch (IOException e) { |
||||
|
log.error("Failed to create temp file", e); |
||||
|
throw new RuntimeException("Failed to create temp file for input stream"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Optional<File> getOtaDataFile(OtaPackageId otaPackageId) { |
||||
|
String fileName = PATH + String.format(FILE_NAME_TEMPLATE,otaPackageId.getId().toString()); |
||||
|
if (exist(fileName)) { |
||||
|
fileCleaner.updateFileUsageStatus(otaPackageId); |
||||
|
return Optional.of(new File(fileName)); |
||||
|
} |
||||
|
return Optional.empty(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public File loadToFile(OtaPackageId otaPackageId, InputStream data) { |
||||
|
if (otaPackageId == null || data == null) { |
||||
|
log.error("Received null variables: {}", otaPackageId == null ? "otaPackageId" : "data"); |
||||
|
throw new RuntimeException("Input values can not be null"); |
||||
|
} |
||||
|
files.computeIfAbsent(otaPackageId, ota -> processFileSaving(ota, data)); |
||||
|
String fileName = PATH + String.format(FILE_NAME_TEMPLATE,otaPackageId.getId().toString()); |
||||
|
return new File(fileName); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private Boolean processFileSaving(OtaPackageId otaPackageId, InputStream data) { |
||||
|
String fileName = PATH + String.format(FILE_NAME_TEMPLATE,otaPackageId.getId().toString()); |
||||
|
saveAsSystemFile(fileName, data); |
||||
|
fileCleaner.updateFileUsageStatus(otaPackageId); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
private void saveAsSystemFile(String fileName, InputStream inputStream) { |
||||
|
try { |
||||
|
File file = new File(fileName); |
||||
|
FileUtils.copyInputStreamToFile(inputStream, file); |
||||
|
} catch (IOException e) { |
||||
|
log.error("Failed to copy stream to system file {}", fileName, e); |
||||
|
throw new RuntimeException("Failed to save file"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private boolean exist(String name) { |
||||
|
File file = new File(name); |
||||
|
return file.exists(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 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.cache.ota.files; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.id.OtaPackageId; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.FileNotFoundException; |
||||
|
import java.io.InputStream; |
||||
|
import java.util.Optional; |
||||
|
|
||||
|
public interface FileCacheService { |
||||
|
File loadToFile(OtaPackageId otaPackageId, InputStream data); |
||||
|
File saveDataTemporaryFile(InputStream inputStream); |
||||
|
Optional<File> getOtaDataFile(OtaPackageId otaPackageId) throws FileNotFoundException; |
||||
|
} |
||||
@ -0,0 +1,122 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 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.cache.ota.files; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.io.FileUtils; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent; |
||||
|
import org.springframework.context.event.EventListener; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.thingsboard.server.common.data.id.OtaPackageId; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.IOException; |
||||
|
import java.net.URI; |
||||
|
import java.nio.channels.FileChannel; |
||||
|
import java.nio.channels.FileLock; |
||||
|
import java.nio.file.Path; |
||||
|
import java.nio.file.StandardOpenOption; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.concurrent.ConcurrentHashMap; |
||||
|
import java.util.concurrent.ConcurrentMap; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Component |
||||
|
public class TemporaryFileCleaner { |
||||
|
@Value("${files.temporary_files_directory}/ota/") |
||||
|
private String PATH; |
||||
|
private final static String FILE_NAME_TEMPLATE = "%s%s.tmp"; |
||||
|
private final static long TEMPORARY_FILE_INACTIVITY_TIME = 900_000; |
||||
|
private final ConcurrentMap<OtaPackageId, Long> lastActivityTimes = new ConcurrentHashMap<>(); |
||||
|
|
||||
|
public void updateFileUsageStatus(OtaPackageId otaPackageId) { |
||||
|
lastActivityTimes.put(otaPackageId, System.currentTimeMillis()); |
||||
|
} |
||||
|
|
||||
|
@EventListener(ApplicationReadyEvent.class) |
||||
|
public void cleanDirectoryWithTemporaryFiles() { |
||||
|
createTempDirectoryIfNotExist(); |
||||
|
cleanDirectory(); |
||||
|
log.info("Directory {} with temporary ota files cleaned", PATH); |
||||
|
} |
||||
|
|
||||
|
private void createTempDirectoryIfNotExist() { |
||||
|
File directory = new File(PATH); |
||||
|
if (!directory.exists()) { |
||||
|
try{ |
||||
|
FileUtils.forceMkdir(directory); |
||||
|
} catch(IOException e){ |
||||
|
log.error("Failed to create directory for temporary files ", e); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void cleanDirectory() { |
||||
|
File directory = new File(PATH); |
||||
|
if (directory.isDirectory()) { |
||||
|
File[] files = directory.listFiles(); |
||||
|
if (files == null) return; |
||||
|
Arrays.stream(files).forEach( |
||||
|
file -> { |
||||
|
try { |
||||
|
FileUtils.delete(file); |
||||
|
} catch (Exception e) { |
||||
|
log.error("Failed to delete file {}", file.getName(), e); |
||||
|
} |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Scheduled(fixedDelay = 600_000) |
||||
|
private void deleteUnusedTemporaryFiles() { |
||||
|
long currentTime = System.currentTimeMillis(); |
||||
|
List<OtaPackageId> toBeDeleted = lastActivityTimes.entrySet() |
||||
|
.stream() |
||||
|
.filter(entry -> currentTime - entry.getValue() > TEMPORARY_FILE_INACTIVITY_TIME) |
||||
|
.map(Map.Entry::getKey) |
||||
|
.collect(Collectors.toList()); |
||||
|
try { |
||||
|
toBeDeleted.forEach(otaId -> { |
||||
|
deleteFile(otaId.getId().toString()); |
||||
|
lastActivityTimes.remove(otaId); |
||||
|
}); |
||||
|
} catch (Exception e) { |
||||
|
log.error("Failed to delete unused files", e); |
||||
|
} |
||||
|
log.info("Deleted {} unused temporary files", toBeDeleted.size()); |
||||
|
} |
||||
|
|
||||
|
private synchronized void deleteFile(String otaId) { |
||||
|
String fileName = String.format(FILE_NAME_TEMPLATE, PATH, otaId); |
||||
|
File file = new File(fileName); |
||||
|
try (FileChannel channel = FileChannel.open(Path.of(URI.create(file.getPath())), StandardOpenOption.APPEND)) { |
||||
|
FileLock lock = channel.lock(); |
||||
|
if (file.exists()) { |
||||
|
FileUtils.delete(file); |
||||
|
log.info("System file {} was deleted", file.getName()); |
||||
|
} |
||||
|
lock.release(); |
||||
|
} catch (IOException e) { |
||||
|
log.error("Failed to delete file {}", file.getName(), e); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,93 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 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.cache.ota.service; |
||||
|
|
||||
|
import lombok.SneakyThrows; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.thingsboard.server.cache.ota.files.BaseFileCacheService; |
||||
|
import org.thingsboard.server.cache.ota.files.TemporaryFileCleaner; |
||||
|
import org.thingsboard.server.common.data.id.OtaPackageId; |
||||
|
|
||||
|
import java.io.ByteArrayInputStream; |
||||
|
import java.io.File; |
||||
|
import java.io.FileInputStream; |
||||
|
import java.io.InputStream; |
||||
|
import java.security.MessageDigest; |
||||
|
import java.util.Objects; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
|
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
|
|
||||
|
|
||||
|
class BaseFileCacheServiceTest { |
||||
|
private final static String PATH = "/home/anastasiia/IdeaProjects/thingsboard/common/cache"; |
||||
|
private final static String FILE_FILLING = "Hello, testing environment"; |
||||
|
private static final int ONE_MEGA_BYTE = 1_000_000; |
||||
|
private final static OtaPackageId OTA_PACKAGE_ID = new OtaPackageId(UUID.randomUUID()); |
||||
|
private final static InputStream DATA = new ByteArrayInputStream(FILE_FILLING.getBytes()); |
||||
|
BaseFileCacheService baseFileCacheService = new BaseFileCacheService(new TemporaryFileCleaner()); |
||||
|
|
||||
|
|
||||
|
@Test |
||||
|
void testDataSavingWithNullInputStream() { |
||||
|
assertThrows(RuntimeException.class, () -> baseFileCacheService.loadToFile(OTA_PACKAGE_ID, null)); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testDataSavingWithNullOtaPackageId() { |
||||
|
assertThrows(RuntimeException.class, () -> baseFileCacheService.loadToFile(null, DATA)); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
@SneakyThrows |
||||
|
void testMultiSavingDataToFile() { |
||||
|
File directory = new File(PATH); |
||||
|
int beginning = Objects.requireNonNull(directory.list()).length; |
||||
|
Thread thread1 = new Thread(() -> baseFileCacheService.loadToFile(OTA_PACKAGE_ID, DATA)); |
||||
|
Thread thread2 = new Thread(() -> baseFileCacheService.loadToFile(OTA_PACKAGE_ID, DATA)); |
||||
|
thread1.start(); |
||||
|
thread2.start(); |
||||
|
thread1.join(); |
||||
|
thread2.join(); |
||||
|
File directory1 = new File(PATH); |
||||
|
int ending = Objects.requireNonNull(directory1.list()).length; |
||||
|
assertEquals(1, ending - beginning); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
@SneakyThrows |
||||
|
void testCorrectDataSavingToFile() { |
||||
|
String sha256 = calculateChecksumSHA256(new ByteArrayInputStream(FILE_FILLING.getBytes())); |
||||
|
File file = baseFileCacheService.loadToFile(OTA_PACKAGE_ID, DATA); |
||||
|
assertEquals(sha256, calculateChecksumSHA256(new FileInputStream(file))); |
||||
|
} |
||||
|
|
||||
|
@SneakyThrows |
||||
|
String calculateChecksumSHA256(InputStream stream) { |
||||
|
MessageDigest md = MessageDigest.getInstance("SHA-256"); |
||||
|
byte[] buffer = new byte[ONE_MEGA_BYTE]; |
||||
|
int count = 0; |
||||
|
while ((count = stream.read(buffer)) != -1) { |
||||
|
md.update(buffer, 0, count); |
||||
|
} |
||||
|
StringBuilder result = new StringBuilder(); |
||||
|
for (byte b : md.digest()) { |
||||
|
result.append(String.format("%02x", b)); |
||||
|
} |
||||
|
return result.toString(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
package org.thingsboard.server.dao.ota; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.util.Optional; |
||||
|
|
||||
|
public interface TbMultipartFile { |
||||
|
Optional<InputStream> getInputStream(); |
||||
|
|
||||
|
String getFileName(); |
||||
|
|
||||
|
long getFileSize(); |
||||
|
|
||||
|
String getContentType(); |
||||
|
} |
||||
@ -0,0 +1,92 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 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.ota.util; |
||||
|
|
||||
|
import com.datastax.oss.driver.shaded.guava.common.io.ByteStreams; |
||||
|
import com.google.common.hash.Funnels; |
||||
|
import com.google.common.hash.Hasher; |
||||
|
import com.google.common.hash.Hashing; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.security.MessageDigest; |
||||
|
import java.security.NoSuchAlgorithmException; |
||||
|
import java.util.Base64; |
||||
|
import java.util.zip.CRC32; |
||||
|
|
||||
|
@Slf4j |
||||
|
public class ChecksumUtil { |
||||
|
private static final int ONE_MEGA_BYTE = 1_000_000; |
||||
|
|
||||
|
public static String generateChecksum(ChecksumAlgorithm checksumAlgorithm, InputStream fileData) { |
||||
|
try { |
||||
|
switch (checksumAlgorithm) { |
||||
|
case CRC32: |
||||
|
return checksumCRC32(fileData); |
||||
|
case MURMUR3_128: |
||||
|
return checksumMurmur3_128(fileData); |
||||
|
case MURMUR3_32: |
||||
|
return checksumMurmur3_32(fileData); |
||||
|
default: |
||||
|
MessageDigest md = MessageDigest.getInstance(checksumAlgorithm.getName()); |
||||
|
return checksum(fileData, md); |
||||
|
} |
||||
|
} catch (NoSuchAlgorithmException e) { |
||||
|
log.error("No such checksum algorithm {}", checksumAlgorithm, e); |
||||
|
throw new RuntimeException(e); |
||||
|
} catch (Exception e) { |
||||
|
log.error("Failed to calculate checksum", e); |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static String checksum(InputStream inputStream, MessageDigest md) throws IOException { |
||||
|
byte[] buffer = new byte[ONE_MEGA_BYTE]; |
||||
|
int count = 0; |
||||
|
while ((count = inputStream.read(buffer)) != -1) { |
||||
|
md.update(buffer, 0, count); |
||||
|
} |
||||
|
StringBuilder result = new StringBuilder(); |
||||
|
for (byte b : md.digest()) { |
||||
|
result.append(String.format("%02x", b)); |
||||
|
} |
||||
|
return result.toString(); |
||||
|
} |
||||
|
|
||||
|
private static String checksumCRC32(InputStream inputStream) throws IOException { |
||||
|
CRC32 crc = new CRC32(); |
||||
|
byte[] buffer = new byte[ONE_MEGA_BYTE]; |
||||
|
int count = 0; |
||||
|
while ((count = inputStream.read(buffer)) != -1) { |
||||
|
crc.update(buffer, 0, count); |
||||
|
} |
||||
|
return Long.toHexString(crc.getValue()); |
||||
|
} |
||||
|
|
||||
|
private static String checksumMurmur3_32(InputStream stream) throws IOException { |
||||
|
Hasher hasher = Hashing.murmur3_32().newHasher(); |
||||
|
com.google.common.io.ByteStreams.copy(stream, Funnels.asOutputStream(hasher)); |
||||
|
return hasher.hash().toString(); |
||||
|
} |
||||
|
|
||||
|
private static String checksumMurmur3_128(InputStream stream) throws IOException { |
||||
|
Hasher hasher = Hashing.murmur3_128().newHasher(); |
||||
|
ByteStreams.copy(stream, Funnels.asOutputStream(hasher)); |
||||
|
return hasher.hash().toString(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,83 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 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.ota.util; |
||||
|
|
||||
|
import org.junit.jupiter.api.BeforeEach; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
|
||||
|
import java.io.ByteArrayInputStream; |
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
|
import static org.thingsboard.server.common.data.ota.ChecksumAlgorithm.*; |
||||
|
|
||||
|
class ChecksumUtilTest { |
||||
|
private static final int SIZE = 1_050_000; |
||||
|
ByteArrayInputStream inputStream; |
||||
|
|
||||
|
@BeforeEach |
||||
|
void setUp() { |
||||
|
char[] chars = new char[SIZE]; |
||||
|
Arrays.fill(chars, 'f'); |
||||
|
String s = new String(chars); |
||||
|
inputStream = new ByteArrayInputStream(s.getBytes()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testSha256Checksum() { |
||||
|
String generateChecksum = ChecksumUtil.generateChecksum(SHA256, inputStream); |
||||
|
assertEquals("b23c6c2faa06fe3f9a47b86914d08a77c9db9ce9b87fc8b6b2758db756159b2e", generateChecksum); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testMd5Checksum() { |
||||
|
String generateChecksum = ChecksumUtil.generateChecksum(MD5, inputStream); |
||||
|
assertEquals("352b53342cc1ef2a21c480656ef6ffe6", generateChecksum); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testSha384Checksum() { |
||||
|
String generateChecksum = ChecksumUtil.generateChecksum(SHA384, inputStream); |
||||
|
assertEquals("bd04277a8fc6ace52123d6d6214d2612ccdec802f361974198c44fd3df857b79f2343f010943340aeb7c51321a2d32e9", generateChecksum); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testSha512Checksum() { |
||||
|
String generateChecksum = ChecksumUtil.generateChecksum(SHA512, inputStream); |
||||
|
assertEquals("2efb6e7ef97eca294e8cc4bf731615622199fba59c2b5ed4f9a56c1f17be3522abac4f5d9fa0f95f39ff10a59c28597a263697b2d794e6686d260ffaa078da7e", |
||||
|
generateChecksum); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testCrc32Checksum() { |
||||
|
String generateChecksum = ChecksumUtil.generateChecksum(CRC32, inputStream); |
||||
|
assertEquals("4b8f2fda", generateChecksum); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testMurmur3_32Checksum() { |
||||
|
assertEquals("3d45a1dd", ChecksumUtil.generateChecksum(MURMUR3_32, inputStream)); |
||||
|
assertDoesNotThrow(() -> ChecksumUtil.generateChecksum(MURMUR3_32, inputStream)); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testMurmur3_128Checksum() { |
||||
|
assertEquals("9dbf0ffe5f6ecbb5edf207717e7870b7", ChecksumUtil.generateChecksum(MURMUR3_128, inputStream)); |
||||
|
assertDoesNotThrow(() -> ChecksumUtil.generateChecksum(MURMUR3_128, inputStream)); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue