added message digest module
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m26s

This commit is contained in:
Jorge Bornhausen 2024-10-18 23:00:46 +02:00
parent d6f92b22f9
commit fd6e7790a6
6 changed files with 231 additions and 0 deletions

View file

@ -0,0 +1,81 @@
package ch.phoenixtechnologies.quarkus.commons.digest;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
@QuarkusTest
class MessageDigestServiceImplTest {
public static final String INPUT = "test";
public static final byte[] EXPECTED_SHA256_DIGEST = {-97, -122, -48, -127, -120, 76, 125, 101, -102, 47, -22, -96, -59, 90, -48, 21, -93, -65, 79, 27, 43, 11, -126, 44, -47, 93, 108, 21, -80, -16, 10, 8};
@Inject
MessageDigestServiceImpl service;
@Test
void getInstance() {
assertThat(service.getInstance())
.isInstanceOf(MessageDigest.class);
}
@ParameterizedTest
@ValueSource(strings = {"SHA-256", "SHA-512", "MD5"})
void getInstanceWithAlgorithm(String algorithm) {
assertThat(service.getInstance(algorithm))
.isInstanceOf(MessageDigest.class);
}
@ParameterizedTest
@ValueSource(strings = {"ADBC", "acvb", "null"})
void getInstanceWhenUnknownAlgorithm(String algorithm) {
assertThatThrownBy(() -> service.getInstance(algorithm))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Unknown algorithm: " + algorithm);
}
@Test
void getInstanceWhenAlgorithmIsNull() {
assertThatThrownBy(() -> service.getInstance(null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("null algorithm name");
}
@Test
void digest() {
assertThat(service.digest(INPUT))
.as("Digest should match expected value")
.isEqualTo(EXPECTED_SHA256_DIGEST);
}
@Test
void digestWithCharset() {
assertThat(service.digest(INPUT, StandardCharsets.UTF_8))
.as("Digest should match expected value")
.isEqualTo(EXPECTED_SHA256_DIGEST);
}
@Test
void digestWithCharsetAndAlgorithm() {
assertThat(service.digest(INPUT, StandardCharsets.UTF_8, "SHA-256"))
.as("Digest should match expected value")
.isEqualTo(EXPECTED_SHA256_DIGEST);
}
@Test
void digestWithCharsetAndMessageDigestInstance() {
var messageDigest = service.getInstance();
assertThat(service.digest(INPUT, StandardCharsets.UTF_8, messageDigest))
.as("Digest should match expected value")
.isEqualTo(EXPECTED_SHA256_DIGEST);
}
}