Skip to content

Commit

Permalink
🚧 #85 #70
Browse files Browse the repository at this point in the history
  • Loading branch information
rucko24 committed Dec 6, 2024
1 parent a8afba8 commit 64506cd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,7 +30,7 @@
@RequiredArgsConstructor
public class ComputeSha256Service {

private static final String SHA_256 = "SHA-256";
private final ComputeDigestAlgorithmConfiguration computeDigestAlgorithmConfiguration;
private final EsptoolSha256ServiceImpl esptool256Service;

/**
Expand All @@ -56,7 +57,7 @@ private Mono<EsptoolSha256Dto> 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);
Expand Down
12 changes: 5 additions & 7 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -36,4 +31,7 @@ vaadin:

login:
access-name: ${ADMIN_ESPFLOW_USER}
access-password: ${ESPFLOW_PASSWORD}
access-password: ${ESPFLOW_PASSWORD}

compute:
digest-algorithm: SHA-256
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,6 +29,9 @@ class ComputeSha256ServiceTest {
@Mock
private EsptoolSha256ServiceImpl esptoolSha256Service;

@Mock
private ComputeDigestAlgorithmConfiguration computeDigestAlgorithmConfiguration;

@Test
@DisplayName("Input file is empty")
void computeSha256FailureEmpty() {
Expand All @@ -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");
Expand All @@ -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()
Expand All @@ -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();

}

}

0 comments on commit 64506cd

Please sign in to comment.