From 64506cd64ef2b5b0396bdff85756b0b32df9213d Mon Sep 17 00:00:00 2001 From: rubn Date: Fri, 6 Dec 2024 02:28:52 +0100 Subject: [PATCH] :construction: #85 #70 --- .../ComputeDigestAlgorithmConfiguration.java | 17 ++++++++ .../hashservice/ComputeSha256Service.java | 5 ++- src/main/resources/application.yml | 12 +++--- .../hashservice/ComputeSha256ServiceTest.java | 43 ++++++++++++++++++- 4 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/esp/espflow/configuration/ComputeDigestAlgorithmConfiguration.java diff --git a/src/main/java/com/esp/espflow/configuration/ComputeDigestAlgorithmConfiguration.java b/src/main/java/com/esp/espflow/configuration/ComputeDigestAlgorithmConfiguration.java new file mode 100644 index 0000000..6cde74b --- /dev/null +++ b/src/main/java/com/esp/espflow/configuration/ComputeDigestAlgorithmConfiguration.java @@ -0,0 +1,17 @@ +package com.esp.espflow.configuration; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * @author rubn + */ +@Data +@Configuration +@ConfigurationProperties(prefix = "compute") +public class ComputeDigestAlgorithmConfiguration { + + private String digestAlgorithm; + +} diff --git a/src/main/java/com/esp/espflow/service/hashservice/ComputeSha256Service.java b/src/main/java/com/esp/espflow/service/hashservice/ComputeSha256Service.java index b9b6f0b..c959321 100644 --- a/src/main/java/com/esp/espflow/service/hashservice/ComputeSha256Service.java +++ b/src/main/java/com/esp/espflow/service/hashservice/ComputeSha256Service.java @@ -1,5 +1,6 @@ package com.esp.espflow.service.hashservice; +import com.esp.espflow.configuration.ComputeDigestAlgorithmConfiguration; import com.esp.espflow.entity.dto.EsptoolSha256Dto; import com.esp.espflow.exceptions.CanNotComputeSha256Exception; import com.esp.espflow.service.respository.impl.EsptoolSha256ServiceImpl; @@ -29,7 +30,7 @@ @RequiredArgsConstructor public class ComputeSha256Service { - private static final String SHA_256 = "SHA-256"; + private final ComputeDigestAlgorithmConfiguration computeDigestAlgorithmConfiguration; private final EsptoolSha256ServiceImpl esptool256Service; /** @@ -56,7 +57,7 @@ private Mono startComputeSha256(final String fileName) { final Path path = Path.of(fileName); try (var bis = new BufferedInputStream(Files.newInputStream(path))) { final byte[] buffer = new byte[FileCopyUtils.BUFFER_SIZE]; - final MessageDigest messageDigest = MessageDigest.getInstance(SHA_256); + final MessageDigest messageDigest = MessageDigest.getInstance(this.computeDigestAlgorithmConfiguration.getDigestAlgorithm()); int dataRead = 0; while ((dataRead = bis.read(buffer)) != -1) { messageDigest.update(buffer, 0, dataRead); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f474001..e1cc6b7 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -18,15 +18,10 @@ spring: sql: init: mode: always - #h2: - # console: - # enabled: true - # path: /h2-console -# vaadin: # To improve the performance during development. # For more information https://vaadin.com/docs/flow/spring/tutorial-spring-configuration.html#special-configuration-parameters - allowed-packages: com.vaadin,org.vaadin,dev.hilla,com.esp.espflow, com.flowingcode, com.infraleap + allowed-packages: com.vaadin,org.vaadin,dev.hilla,com.esp, com.flowingcode, com.infraleap # Launch the default browser when starting the application in development mode launch-browser: true frontend: @@ -36,4 +31,7 @@ vaadin: login: access-name: ${ADMIN_ESPFLOW_USER} - access-password: ${ESPFLOW_PASSWORD} \ No newline at end of file + access-password: ${ESPFLOW_PASSWORD} + +compute: + digest-algorithm: SHA-256 \ No newline at end of file diff --git a/src/test/java/com/esp/espflow/service/hashservice/ComputeSha256ServiceTest.java b/src/test/java/com/esp/espflow/service/hashservice/ComputeSha256ServiceTest.java index 11485d7..b7199a3 100644 --- a/src/test/java/com/esp/espflow/service/hashservice/ComputeSha256ServiceTest.java +++ b/src/test/java/com/esp/espflow/service/hashservice/ComputeSha256ServiceTest.java @@ -1,5 +1,6 @@ package com.esp.espflow.service.hashservice; +import com.esp.espflow.configuration.ComputeDigestAlgorithmConfiguration; import com.esp.espflow.entity.dto.EsptoolSha256Dto; import com.esp.espflow.service.respository.impl.EsptoolSha256ServiceImpl; import org.junit.jupiter.api.DisplayName; @@ -28,6 +29,9 @@ class ComputeSha256ServiceTest { @Mock private EsptoolSha256ServiceImpl esptoolSha256Service; + @Mock + private ComputeDigestAlgorithmConfiguration computeDigestAlgorithmConfiguration; + @Test @DisplayName("Input file is empty") void computeSha256FailureEmpty() { @@ -47,7 +51,7 @@ void computeSha256FailureNull() { } @Test - @DisplayName("Input esptool file with sha256") + @DisplayName("Input esptool file with SHA-256") void computeSha256Success() { String property = System.getProperty("os.arch"); System.setProperty("os.arch", "amd64"); @@ -58,6 +62,7 @@ void computeSha256Success() { .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") .build(); + when(computeDigestAlgorithmConfiguration.getDigestAlgorithm()).thenReturn("SHA-256"); when(esptoolSha256Service.findBySha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")).thenReturn(Optional.of(actualEsptoolSha256Dto)); EsptoolSha256Dto expectedEsptoolSha256Dto = EsptoolSha256Dto.builder() @@ -75,4 +80,40 @@ void computeSha256Success() { } + @Test + @DisplayName("Input esptool file with SHA-256 but arch does not match!") + void computeSha256Failure_emptyMono() { + String property = System.getProperty("os.arch"); + System.setProperty("os.arch", "amd64"); + + EsptoolSha256Dto actualEsptoolSha256Dto = EsptoolSha256Dto.builder() + .osArch("amd63") + .esptoolVersion("v4.7.0") + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + when(computeDigestAlgorithmConfiguration.getDigestAlgorithm()).thenReturn("SHA-256"); + when(esptoolSha256Service.findBySha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")).thenReturn(Optional.of(actualEsptoolSha256Dto)); + + StepVerifier.create(computeSha256Service.computeSha256("src/test/resources/esptool/esptool-linux-amd64/esptool")) + .expectErrorMatches(error -> error.getMessage().contains("Can not compute sha256")) + .verify(); + + System.clearProperty("os.arch"); + System.setProperty("os.arch", property); + + } + + @Test + @DisplayName("Input esptool, java.security.NoSuchAlgorithmException: shaWtF MessageDigest not available") + void computeSha256Failure_NoSuchAlgorithmException() { + + when(computeDigestAlgorithmConfiguration.getDigestAlgorithm()).thenReturn("shaWtF"); + + StepVerifier.create(computeSha256Service.computeSha256("src/test/resources/esptool/esptool-linux-amd64/esptool")) + .expectErrorMatches(error -> error.getMessage().contains("Can not compute sha256")) + .verify(); + + } + } \ No newline at end of file