Skip to content

Commit

Permalink
test: add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnderScorer committed Jan 26, 2024
1 parent a1afa5c commit ba7374d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 5 deletions.
22 changes: 20 additions & 2 deletions src/main/java/com/fingerprint/Sealed.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public List<UnsealException> getUnsealExceptions() {
}
}

public static class InvalidSealedDataException extends IllegalArgumentException {
public InvalidSealedDataException() {
super("Invalid sealed data");
}
}

public static class InvalidSealedDataHeaderException extends IllegalArgumentException {
public InvalidSealedDataHeaderException() {
super("Invalid sealed data header");
}
}

public static class UnsealException extends Exception {
public final DecryptionKey decryptionKey;

Expand All @@ -63,7 +75,7 @@ public UnsealException(String message, DecryptionKey decryptionKey, Exception ex

public static byte[] unseal(byte[] sealed, DecryptionKey[] keys) throws IllegalArgumentException, UnsealAggregateException {
if (!Arrays.equals(Arrays.copyOf(sealed, SEAL_HEADER.length), SEAL_HEADER)) {
throw new IllegalArgumentException("Invalid sealed data header");
throw new InvalidSealedDataHeaderException();
}

UnsealAggregateException aggregateException = new UnsealAggregateException();
Expand Down Expand Up @@ -105,7 +117,13 @@ public static EventResponse unsealEventResponse(byte[] sealed, DecryptionKey[] k

ObjectMapper mapper = ObjectMapperUtil.getObjectMapper();

return mapper.readValue(unsealed, EventResponse.class);
EventResponse value = mapper.readValue(unsealed, EventResponse.class);

if (value.getProducts() == null) {
throw new InvalidSealedDataException();
}

return value;
}

private static byte[] decryptAes256Gcm(byte[] sealedData, byte[] decryptionKey) throws Exception {
Expand Down
88 changes: 85 additions & 3 deletions src/test/java/com/fingerprint/SealedTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fingerprint;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fingerprint.model.EventResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
Expand Down Expand Up @@ -35,12 +36,95 @@ public void unsealEventResponseTest() throws Exception {
assert eventResponse.equals(expectedResponse);
}

@Test
public void unsealEventResponseWithInvalidSealedResultTest() throws Exception {
// "{\"invalid\":true}"
byte[] sealedResult = Base64.getDecoder().decode("noXc7VOpBstjjcavDKSKr4HTavt4mdq8h6NC32T0hUtw9S0jXT8lPjZiWL8SyHxmrF3uTGqO+g==");
byte[] key = Base64.getDecoder().decode("p2PA7MGy5tx56cnyJaFZMr96BCFwZeHjZV2EqMvTq53=");

assertThrows(Sealed.InvalidSealedDataException.class, () -> Sealed.unsealEventResponse(
sealedResult,
new Sealed.DecryptionKey[]{
new Sealed.DecryptionKey(
//Invalid key
Base64.getDecoder().decode("p2PA7MGy5tx56cnyJaFZMr96BCFwZeHjZV2EqMvTq54="),
Sealed.DecryptionAlgorithm.AES_256_GCM
),
new Sealed.DecryptionKey(
//Invalid key
Base64.getDecoder().decode("aW52YWxpZA=="),
Sealed.DecryptionAlgorithm.AES_256_GCM
),
new Sealed.DecryptionKey(
key,
Sealed.DecryptionAlgorithm.AES_256_GCM
)
}
));
}

@Test
public void unsealEventResponseWithInvalidJsonSealedResultTest() throws Exception {
byte[] sealedResult = Base64.getDecoder().decode("noXc7XdbEp5JpFNJaMxCB5leuFeW9Fs0tqvwnbU3ND2yShYn+dgeUWvdk32YrXam4yuvhmpO8gww//Qmsu2sbyvyMRuXmlKoriV9EVPYVCB2xszskg34ngrAh4sreRZV3c8d0DcXZulbMiiXrli931fEABWRHM0NtcoPuubqb+TysNSoFIYVZxpRVDR8jDiTXuQyPzvqBJD4+xeQTOOAOjPlqRTQSSBrlWjeZLNA70wWX7VRDXA1SoR+1k7bkBFK4OwRnh5rVGeGvGeHisOe/SyOL6GlQyBk3sRdSCQiI/g0ywdqLsOk4xDdCgg5vMI07APvL9FSaQrglMvD8NRmQOr5glZoV6S3DoBgaYQVvEygTZy2gfJ0z6hLY6Q8WSW0hpb3t9m4MP9WC5Vc2r0fmfqX7gjYZpwyfJxsyyk4iksminhm2T8N8DTYuZuz82jjaGNDqAPn1PZKqiEh8H9TpcgewAP8mlVrB5CUPJMHH+p7dM5zibfKM9+1MPxvZNp0PBkljBwrfGjiKlmYhn7bb5UW5TeEMtiP27KoA26PX+NV130Vi9Y/LUgMivLwaIc+jnlFyaoqg6Kg6H8G3WhT0r/pc4KP0mwyHJzfXjep8kQZGKxbMd0Sc3h4kpoWR1hdYM4QZRvKQzh7BqBPtPiVgHYoEJf9qFVxYhel9UFONz65q5bA2Y25oFKpzfsiXQqFEo/LRANnW7iUdfesGtGjjP4N6rd8ssNpYf57FmPBpWC4RwjG45MHRUSajCVLKiwUgFQbOo7/t5hgQIQOui3jmCBDjCjpjGZK8vd2nFputUTqI/MmZK7THaDPFsn8h9M1boF3VMCzDXygJFhd5lwdVErXGtQcc1lApEvdOr24QB5Io4SjfjJCfEQ7g4ulBXuqsh6I4VkcuMh5zgBIdmGm");
byte[] key = Base64.getDecoder().decode("p2PA7MGy5tx56cnyJaFZMr96BCFwZeHjZV2EqMvTq53=");

assertThrows(JsonMappingException.class, () -> Sealed.unsealEventResponse(
sealedResult,
new Sealed.DecryptionKey[]{
new Sealed.DecryptionKey(
//Invalid key
Base64.getDecoder().decode("p2PA7MGy5tx56cnyJaFZMr96BCFwZeHjZV2EqMvTq54="),
Sealed.DecryptionAlgorithm.AES_256_GCM
),
new Sealed.DecryptionKey(
//Invalid key
Base64.getDecoder().decode("aW52YWxpZA=="),
Sealed.DecryptionAlgorithm.AES_256_GCM
),
new Sealed.DecryptionKey(
key,
Sealed.DecryptionAlgorithm.AES_256_GCM
)
}
));
}

@Test
public void unsealEventResponseWithNotCompressedSealedResultTest() throws Exception {
byte[] sealedResult = Base64.getDecoder().decode("noXc7dtuk0smGE+ZbaoXzrp6Rq8ySxLepejTsu7+jUXlPhV1w+WuHx9gbPhaENJnOQo8BcGmsaRhL5k2NVj+DRNzYO9cQD7wHxmXKCyTbl/dvSYOMoHziUZ2VbQ7tmaorFny26v8jROr/UBGfvPE0dLKC36IN9ZlJ3X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/25DkM/Pm7RBzwtjgmnRKjT+mROeC/7VQLoz3amv09O8Mvbt+h/lX5+51Q834F7NgIGagbB20WtWcMtrmKrvCEZlaoiZrmYVSbi1RfknRK7CTPJkopw9IjO7Ut2EhKZ+jL4rwk6TlVm6EC6Kuj7KNqp6wB/UNe9eM2Eym/aiHAcja8XN4YQhSIuJD2Wxb0n3LkKnAjK1/GY65c8K6rZsVYQ0MQL1j4lMl0UZPjG/vzKyetIsVDyXc4J9ZhOEMYnt/LaxEeSt4EMJGBA9wpTmz33X4h3ij0Y3DY/rH7lrEScUknw20swTZRm5T6q1bnimj7M1OiOkebdI09MZ0nyaTWRHdB7B52C/moh89Q7qa2Fulp5h8Us1FYRkWBLt37a5rGI1IfVeP38KaPbagND+XzWpNqX4HVrAVPLQVK5EwUvGamED3ooJ0FMieTc0IH0N+IeUYG7Q8XmrRVBcw32W8pEfYLO9L71An/J0jQZCIP8DuQnUG0mOvunOuloBGvP/9LvkBlkamh68F0a5f5ny1jloyIFJhRh5dt2SBlbsXS9AKqUwARYSSsA9Ao4WJWOZMyjp8A+qIBAfW65MdhhUDKYMBgIAbMCc3uiptzElQQopE5TT5xIhwfYxa503jVzQbz1Q==");
byte[] key = Base64.getDecoder().decode("p2PA7MGy5tx56cnyJaFZMr96BCFwZeHjZV2EqMvTq53=");

Sealed.UnsealAggregateException exception = assertThrows(Sealed.UnsealAggregateException.class, () -> Sealed.unsealEventResponse(
sealedResult,
new Sealed.DecryptionKey[]{
new Sealed.DecryptionKey(
//Invalid key
Base64.getDecoder().decode("p2PA7MGy5tx56cnyJaFZMr96BCFwZeHjZV2EqMvTq54="),
Sealed.DecryptionAlgorithm.AES_256_GCM
),
new Sealed.DecryptionKey(
//Invalid key
Base64.getDecoder().decode("aW52YWxpZA=="),
Sealed.DecryptionAlgorithm.AES_256_GCM
),
new Sealed.DecryptionKey(
key,
Sealed.DecryptionAlgorithm.AES_256_GCM
)
}
));

Sealed.UnsealException unsealException = exception.getUnsealExceptions().get(2);

assertEquals(unsealException.exception.getMessage(), "invalid distance too far back");
}

@Test
public void unsealEventResponseWithInvalidHeaderTest() throws Exception {
byte[] sealedResult = Base64.getDecoder().decode("noXc7xXO+mqeAGrvBMgObi/S0fXTpP3zupk8qFqsO/1zdtWCD169iLA3VkkZh9ICHpZ0oWRzqG0M9/TnCeKFohgBLqDp6O0zEfXOv6i5q++aucItznQdLwrKLP+O0blfb4dWVI8/aSbd4ELAZuJJxj9bCoVZ1vk+ShbUXCRZTD30OIEAr3eiG9aw00y1UZIqMgX6CkFlU9L9OnKLsNsyomPIaRHTmgVTI5kNhrnVNyNsnzt9rY7fUD52DQxJILVPrUJ1Q+qW7VyNslzGYBPG0DyYlKbRAomKJDQIkdj/Uwa6bhSTq4XYNVvbk5AJ/dGwvsVdOnkMT2Ipd67KwbKfw5bqQj/cw6bj8Cp2FD4Dy4Ud4daBpPRsCyxBM2jOjVz1B/lAyrOp8BweXOXYugwdPyEn38MBZ5oL4D38jIwR/QiVnMHpERh93jtgwh9Abza6i4/zZaDAbPhtZLXSM5ztdctv8bAb63CppLU541Kf4OaLO3QLvfLRXK2n8bwEwzVAqQ22dyzt6/vPiRbZ5akh8JB6QFXG0QJF9DejsIspKF3JvOKjG2edmC9o+GfL3hwDBiihYXCGY9lElZICAdt+7rZm5UxMx7STrVKy81xcvfaIp1BwGh/HyMsJnkE8IczzRFpLlHGYuNDxdLoBjiifrmHvOCUDcV8UvhSV+UAZtAVejdNGo5G/bz0NF21HUO4pVRPu6RqZIs/aX4hlm6iO/0Ru00ct8pfadUIgRcephTuFC2fHyZxNBC6NApRtLSNLfzYTTo/uSjgcu6rLWiNo5G7yfrM45RXjalFEFzk75Z/fu9lCJJa5uLFgDNxlU+IaFjArfXJCll3apbZp4/LNKiU35ZlB7ZmjDTrji1wLep8iRVVEGht/DW00MTok7Zn7Fv+MlxgWmbZB3BuezwTmXb/fNw==");
byte[] key = Base64.getDecoder().decode("p2PA7MGy5tx56cnyJaFZMr96BCFwZeHjZV2EqMvTq53=");

Exception thrown = assertThrows(Exception.class, () -> Sealed.unsealEventResponse(
assertThrows(Sealed.InvalidSealedDataHeaderException.class, () -> Sealed.unsealEventResponse(
sealedResult,
new Sealed.DecryptionKey[]{
new Sealed.DecryptionKey(
Expand All @@ -59,8 +143,6 @@ public void unsealEventResponseWithInvalidHeaderTest() throws Exception {
)
}
));

assertEquals("Invalid sealed data header", thrown.getMessage());
}

@Test
Expand Down

0 comments on commit ba7374d

Please sign in to comment.