Skip to content

Commit a03b5f7

Browse files
committed
test: add attestation verifier integration tests
1 parent e180284 commit a03b5f7

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.streamline.attestation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.nio.charset.StandardCharsets;
6+
import java.security.KeyPair;
7+
import java.security.KeyPairGenerator;
8+
import java.security.PrivateKey;
9+
import java.security.Signature;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
import static org.junit.jupiter.api.Assertions.assertFalse;
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
16+
class AttestationVerifierTest {
17+
18+
private static class StubRecord implements AttestationVerifier.AttestedRecord {
19+
final Map<String, byte[]> headers = new HashMap<>();
20+
public String topic() { return "orders"; }
21+
public int partition() { return 0; }
22+
public long offset() { return 42L; }
23+
public byte[] value() { return "{\"amount\":100}".getBytes(StandardCharsets.UTF_8); }
24+
public long timestampMs() { return 1_700_000_000_000L; }
25+
public int schemaId() { return 7; }
26+
public String keyId() { return "broker-key-1"; }
27+
public Map<String, byte[]> headers() { return headers; }
28+
}
29+
30+
@Test
31+
void verifies_valid_signature() throws Exception {
32+
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519");
33+
KeyPair kp = kpg.generateKeyPair();
34+
StubRecord r = new StubRecord();
35+
36+
Signature signer = Signature.getInstance("Ed25519");
37+
signer.initSign(kp.getPrivate());
38+
signer.update(AttestationVerifier.canonicalBytes(r));
39+
byte[] sig = signer.sign();
40+
41+
r.headers.put(AttestationVerifier.HEADER, sig);
42+
assertTrue(new AttestationVerifier(kp.getPublic()).verify(r));
43+
}
44+
45+
@Test
46+
void rejects_missing_header() throws Exception {
47+
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519");
48+
KeyPair kp = kpg.generateKeyPair();
49+
assertFalse(new AttestationVerifier(kp.getPublic()).verify(new StubRecord()));
50+
}
51+
52+
@Test
53+
void rejects_tampered_signature() throws Exception {
54+
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519");
55+
KeyPair kp = kpg.generateKeyPair();
56+
StubRecord r = new StubRecord();
57+
r.headers.put(AttestationVerifier.HEADER, new byte[]{0x00, 0x01, 0x02});
58+
assertFalse(new AttestationVerifier(kp.getPublic()).verify(r));
59+
}
60+
}

0 commit comments

Comments
 (0)