-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathissue_test.go
More file actions
298 lines (277 loc) · 7.78 KB
/
Copy pathissue_test.go
File metadata and controls
298 lines (277 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package peac
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/peacprotocol/peac/sdks/go/jws"
)
func testSigningKey(t *testing.T) *jws.SigningKey {
t.Helper()
key, err := jws.GenerateSigningKey("test-key-1")
if err != nil {
t.Fatal(err)
}
return key
}
func TestIssue_Valid(t *testing.T) {
key := testSigningKey(t)
result, err := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
Kid: "test-key-1",
Clock: FixedClock{Time: time.Unix(1700000000, 0)},
})
if err != nil {
t.Fatalf("Issue() failed: %v", err)
}
if result.JWS == "" {
t.Fatal("expected non-empty JWS")
}
if result.ReceiptID == "" {
t.Fatal("expected non-empty ReceiptID")
}
if result.IssuedAt != 1700000000 {
t.Errorf("IssuedAt = %d, want 1700000000", result.IssuedAt)
}
parts := strings.Split(result.JWS, ".")
if len(parts) != 3 {
t.Fatalf("JWS has %d parts, want 3", len(parts))
}
headerBytes, _ := jws.Decode(parts[0])
var header jws.Header
_ = json.Unmarshal(headerBytes, &header)
if header.Type != InteractionRecordTyp {
t.Errorf("header typ = %s, want %s", header.Type, InteractionRecordTyp)
}
if header.Algorithm != "EdDSA" {
t.Errorf("header alg = %s, want EdDSA", header.Algorithm)
}
if header.KeyID != "test-key-1" {
t.Errorf("header kid = %s, want test-key-1", header.KeyID)
}
payloadBytes, _ := jws.Decode(parts[1])
var claims InteractionRecordClaims
_ = json.Unmarshal(payloadBytes, &claims)
if claims.Iss != "https://example.com" {
t.Errorf("iss = %s, want https://example.com", claims.Iss)
}
if claims.Kind != KindEvidence {
t.Errorf("kind = %s, want evidence", claims.Kind)
}
if claims.Type != "org.peacprotocol/test" {
t.Errorf("type = %s, want org.peacprotocol/test", claims.Type)
}
if claims.PeacVersion != PeacVersion {
t.Errorf("peac_version = %s, want %s", claims.PeacVersion, PeacVersion)
}
if claims.Rid == "" {
t.Error("rid should not be empty")
}
}
func TestIssue_DIDIssuer(t *testing.T) {
key := testSigningKey(t)
result, err := Issue(IssueOptions{
Iss: "did:key:z6Mk1234",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
})
if err != nil {
t.Fatalf("Issue() with did: iss failed: %v", err)
}
if result.JWS == "" {
t.Fatal("expected non-empty JWS")
}
}
func TestIssue_ChallengeKind(t *testing.T) {
key := testSigningKey(t)
result, err := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindChallenge,
Type: "org.peacprotocol/rate-limited",
SigningKey: key,
})
if err != nil {
t.Fatalf("Issue() with challenge kind failed: %v", err)
}
parts := strings.Split(result.JWS, ".")
payloadBytes, _ := jws.Decode(parts[1])
var claims InteractionRecordClaims
_ = json.Unmarshal(payloadBytes, &claims)
if claims.Kind != KindChallenge {
t.Errorf("kind = %s, want challenge", claims.Kind)
}
}
func TestIssue_RejectsHTTPIssuer(t *testing.T) {
key := testSigningKey(t)
_, err := Issue(IssueOptions{
Iss: "http://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
})
if err == nil {
t.Fatal("expected error for http:// issuer")
}
ie, ok := err.(*IssueError)
if !ok {
t.Fatalf("expected *IssueError, got %T", err)
}
if ie.Code != ErrCodeInvalidIss {
t.Errorf("code = %s, want %s", ie.Code, ErrCodeInvalidIss)
}
}
func TestIssue_RejectsMissingIss(t *testing.T) {
key := testSigningKey(t)
_, err := Issue(IssueOptions{Kind: KindEvidence, Type: "org.peacprotocol/test", SigningKey: key})
if err == nil {
t.Fatal("expected error for missing iss")
}
}
func TestIssue_RejectsMissingKind(t *testing.T) {
key := testSigningKey(t)
_, err := Issue(IssueOptions{Iss: "https://example.com", Type: "org.peacprotocol/test", SigningKey: key})
if err == nil {
t.Fatal("expected error for missing kind")
}
}
func TestIssue_RejectsInvalidKind(t *testing.T) {
key := testSigningKey(t)
_, err := Issue(IssueOptions{Iss: "https://example.com", Kind: "unknown", Type: "org.peacprotocol/test", SigningKey: key})
if err == nil {
t.Fatal("expected error for invalid kind")
}
if ie := err.(*IssueError); ie.Code != ErrCodeInvalidKind {
t.Errorf("code = %s, want %s", ie.Code, ErrCodeInvalidKind)
}
}
func TestIssue_RejectsMissingType(t *testing.T) {
key := testSigningKey(t)
_, err := Issue(IssueOptions{Iss: "https://example.com", Kind: KindEvidence, SigningKey: key})
if err == nil {
t.Fatal("expected error for missing type")
}
}
func TestIssue_RejectsMissingKey(t *testing.T) {
_, err := Issue(IssueOptions{Iss: "https://example.com", Kind: KindEvidence, Type: "org.peacprotocol/test"})
if err == nil {
t.Fatal("expected error for missing signing key")
}
}
func TestIssue_WithExtensions(t *testing.T) {
key := testSigningKey(t)
result, err := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/commerce",
SigningKey: key,
Extensions: map[string]any{
"org.peacprotocol/commerce": map[string]any{
"amount_minor": "1000",
"currency": "USD",
},
},
})
if err != nil {
t.Fatalf("Issue() with extensions failed: %v", err)
}
parts := strings.Split(result.JWS, ".")
payloadBytes, _ := jws.Decode(parts[1])
var claims InteractionRecordClaims
_ = json.Unmarshal(payloadBytes, &claims)
if claims.Ext == nil {
t.Fatal("expected extensions in claims")
}
if _, ok := claims.Ext["org.peacprotocol/commerce"]; !ok {
t.Fatal("expected commerce extension")
}
}
func TestIssue_WithPolicyBlock(t *testing.T) {
key := testSigningKey(t)
result, err := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
Policy: &PolicyBlock{Digest: "sha256:abc123"},
})
if err != nil {
t.Fatalf("Issue() with policy failed: %v", err)
}
parts := strings.Split(result.JWS, ".")
payloadBytes, _ := jws.Decode(parts[1])
var claims InteractionRecordClaims
_ = json.Unmarshal(payloadBytes, &claims)
if claims.Peac == nil {
t.Fatal("expected policy block")
}
if claims.Peac.Digest != "sha256:abc123" {
t.Errorf("policy digest = %s, want sha256:abc123", claims.Peac.Digest)
}
}
func TestIssue_WithPillars(t *testing.T) {
key := testSigningKey(t)
_, err := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
Pillars: []string{"commerce", "access"},
})
if err != nil {
t.Fatalf("Issue() with pillars failed: %v", err)
}
}
func TestIssue_RejectsInvalidPillar(t *testing.T) {
key := testSigningKey(t)
_, err := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
Pillars: []string{"invalid-pillar"},
})
if err == nil {
t.Fatal("expected error for invalid pillar")
}
}
func TestIssue_RidIsUUIDv7(t *testing.T) {
key := testSigningKey(t)
result, err := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
})
if err != nil {
t.Fatal(err)
}
parts := strings.Split(result.JWS, ".")
payloadBytes, _ := jws.Decode(parts[1])
var claims InteractionRecordClaims
_ = json.Unmarshal(payloadBytes, &claims)
if len(claims.Rid) != 36 {
t.Errorf("rid length = %d, want 36", len(claims.Rid))
}
if claims.Rid[14] != '7' {
t.Errorf("rid version char = %c, want 7", claims.Rid[14])
}
}
func TestIssueJWS_Convenience(t *testing.T) {
key := testSigningKey(t)
jwsStr, err := IssueJWS(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
})
if err != nil {
t.Fatal(err)
}
if jwsStr == "" {
t.Fatal("expected non-empty JWS")
}
}