Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk) - unit test for testing json canonicalizer and assertion hash #201

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions sdk/src/test/java/io/opentdf/platform/sdk/ManifestTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.opentdf.platform.sdk;

import org.apache.commons.codec.binary.Hex;
import org.erdtman.jcs.JsonCanonicalizer;
import org.junit.jupiter.api.Test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand All @@ -12,6 +15,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ManifestTest {
private static final Gson gson = new Gson();

@Test
void testManifestMarshalAndUnMarshal() {
String kManifestJsonFromTDF = "{\n" +
Expand Down Expand Up @@ -90,4 +95,39 @@ void testManifestMarshalAndUnMarshal() {

assertEquals(manifest, deserializedAgain, "something changed when we deserialized -> serialized -> deserialized");
}

@Test
void testAssertionHasdh() {
var assertionConfig = new AssertionConfig();
assertionConfig.id = "424ff3a3-50ca-4f01-a2ae-ef851cd3cac0";
assertionConfig.type = AssertionConfig.Type.HandlingAssertion;
assertionConfig.scope = AssertionConfig.Scope.TrustedDataObj;
assertionConfig.appliesToState = AssertionConfig.AppliesToState.Encrypted;
assertionConfig.statement = new AssertionConfig.Statement();
assertionConfig.statement.format = "json+stanag5636";
assertionConfig.statement.schema = "urn:nato:stanag:5636:A:1:elements:json";
assertionConfig.statement.value = "{\"ocl\":{\"pol\":\"62c76c68-d73d-4628-8ccc-4c1e18118c22\",\"cls\":\"SECRET\",\"catl\":[{\"type\":\"P\",\"name\":\"Releasable To\",\"vals\":[\"usa\"]}],\"dcr\":\"2024-10-21T20:47:36Z\"},\"context\":{\"@base\":\"urn:nato:stanag:5636:A:1:elements:json\"}}";

var assertion = new Manifest.Assertion();
assertion.id = assertionConfig.id;
assertion.type = assertionConfig.type.toString();
assertion.scope = assertionConfig.scope.toString();
assertion.statement = assertionConfig.statement;
assertion.appliesToState = assertionConfig.appliesToState.toString();

final String jsonCanonicalizerString = "{\"appliesToState\":\"encrypted\",\"id\":\"424ff3a3-50ca-4f01-a2ae-ef851cd3cac0\",\"scope\":\"tdo\",\"statement\":{\"format\":\"json+stanag5636\",\"schema\":\"urn:nato:stanag:5636:A:1:elements:json\",\"value\":\"{\\\"ocl\\\":{\\\"pol\\\":\\\"62c76c68-d73d-4628-8ccc-4c1e18118c22\\\",\\\"cls\\\":\\\"SECRET\\\",\\\"catl\\\":[{\\\"type\\\":\\\"P\\\",\\\"name\\\":\\\"Releasable To\\\",\\\"vals\\\":[\\\"usa\\\"]}],\\\"dcr\\\":\\\"2024-10-21T20:47:36Z\\\"},\\\"context\\\":{\\\"@base\\\":\\\"urn:nato:stanag:5636:A:1:elements:json\\\"}}\"},\"type\":\"handling\"}";
try {
var assertionAsJson = gson.toJson(assertion);
var jc = new JsonCanonicalizer(assertionAsJson);
var assertionBytes = jc.getEncodedUTF8();
String assertionString = new String(assertionBytes, StandardCharsets.UTF_8);
assertEquals(jsonCanonicalizerString, assertionString);
//System.out.println("Encoded UTF-8: " + assertionString);

var assertionHash = assertion.hash();
assertEquals(assertionHash, "4a447a13c5a32730d20bdf7feecb9ffe16649bc731914b574d80035a3927f860");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Loading