This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
doc_test.go
1802 lines (1611 loc) · 58 KB
/
doc_test.go
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package did
import (
"crypto/ed25519"
"crypto/rand"
_ "embed"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"testing"
"time"
"github.com/btcsuite/btcutil/base58"
gojose "github.com/square/go-jose/v3"
"github.com/stretchr/testify/require"
"github.com/hyperledger/aries-framework-go/pkg/doc/jose/jwk"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/signer"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ed25519signature2018"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/verifier"
"github.com/hyperledger/aries-framework-go/pkg/internal/ldtestutil"
)
const pemPK = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAryQICCl6NZ5gDKrnSztO
3Hy8PEUcuyvg/ikC+VcIo2SFFSf18a3IMYldIugqqqZCs4/4uVW3sbdLs/6PfgdX
7O9D22ZiFWHPYA2k2N744MNiCD1UE+tJyllUhSblK48bn+v1oZHCM0nYQ2NqUkvS
j+hwUU3RiWl7x3D2s9wSdNt7XUtW05a/FXehsPSiJfKvHJJnGOX0BgTvkLnkAOTd
OrUZ/wK69Dzu4IvrN4vs9Nes8vbwPa/ddZEzGR0cQMt0JBkhk9kU/qwqUseP1QRJ
5I1jR4g8aYPL/ke9K35PxZWuDp3U0UPAZ3PjFAh+5T+fc7gzCs9dPzSHloruU+gl
FQIDAQAB
-----END PUBLIC KEY-----`
const (
did = "did:method:abc"
creator = did + "#key-1"
keyType = "Ed25519VerificationKey2018"
signatureType = "Ed25519Signature2018"
)
const (
missingPubKeyID = "did:example:123456789abcdefghs#key4"
wrongDataMsg = "wrongData"
)
//nolint:gochecknoglobals
var (
//go:embed testdata/valid_doc.jsonld
validDoc string
//go:embed testdata/valid_doc_resolution.jsonld
validDocResolution string
//go:embed testdata/invalid_doc.jsonld
invalidDoc string
//go:embed testdata/valid_doc_v0.11.jsonld
validDocV011 string
//go:embed testdata/valid_doc_with_base.jsonld
validDocWithBase string
)
func TestParseOfNull(t *testing.T) {
doc, err := ParseDocument([]byte("null"))
require.Error(t, err)
require.Nil(t, doc)
require.Contains(t, err.Error(), "document payload is not provided")
}
func TestValidWithDocBase(t *testing.T) {
docs := []string{validDocWithBase}
for _, d := range docs {
doc, err := ParseDocument([]byte(d))
require.NoError(t, err)
require.NotNil(t, doc)
require.Contains(t, doc.Context[0], "https://www.w3.org/ns/did/v")
// test doc id
require.Equal(t, doc.ID, "did:example:123456789abcdefghi")
hexDecodeValue, err := hex.DecodeString("02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71")
block, _ := pem.Decode([]byte(pemPK))
require.NotNil(t, block)
require.NoError(t, err)
// test authentication
eAuthentication := []Verification{
{
VerificationMethod: VerificationMethod{
ID: "did:example:123456789abcdefghi#keys-1",
Type: "Secp256k1VerificationKey2018",
Controller: "did:example:123456789abcdefghi",
Value: base58.Decode("H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"),
relativeURL: true,
},
Relationship: Authentication,
},
{
VerificationMethod: VerificationMethod{
ID: "did:example:123456789abcdefghi#key3",
Controller: "did:example:123456789abcdefghi",
Type: "RsaVerificationKey2018",
Value: hexDecodeValue,
relativeURL: true,
},
Relationship: Authentication, Embedded: true,
},
}
require.Equal(t, eAuthentication, doc.Authentication)
// test public key
ePubKey := []VerificationMethod{
{
ID: "did:example:123456789abcdefghi#keys-1",
Controller: "did:example:123456789abcdefghi",
Type: "Secp256k1VerificationKey2018",
Value: base58.Decode("H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"),
relativeURL: true,
},
{
ID: "did:example:123456789abcdefghi#key2",
Controller: "did:example:123456789abcdefghi",
Type: "RsaVerificationKey2018",
Value: block.Bytes,
relativeURL: true,
},
}
require.Equal(t, ePubKey, doc.VerificationMethod)
// test services
eServices := []Service{
{
ID: "did:example:123456789abcdefghi#inbox",
Type: "SocialWebInboxService",
relativeURL: true,
ServiceEndpoint: "https://social.example.com/83hfh37dj",
Properties: map[string]interface{}{"spamCost": map[string]interface{}{"amount": "0.50", "currency": "USD"}},
},
{
ID: "did:example:123456789abcdefghi#did-communication",
Type: "did-communication",
Priority: 0,
relativeURL: true,
RecipientKeys: []string{"did:example:123456789abcdefghi#key2"},
RoutingKeys: []string{"did:example:123456789abcdefghi#key2"},
ServiceEndpoint: "https://agent.example.com/",
Properties: map[string]interface{}{},
recipientKeysRelativeURL: map[string]bool{"did:example:123456789abcdefghi#key2": true},
routingKeysRelativeURL: map[string]bool{"did:example:123456789abcdefghi#key2": true},
},
}
require.EqualValues(t, eServices, doc.Service)
// test proof
require.EqualValues(t, "did:example:123456789abcdefghi#key-5", doc.Proof[0].Creator)
}
}
func TestDocResolution(t *testing.T) {
t.Run("test valid doc resolution", func(t *testing.T) {
d, err := ParseDocumentResolution([]byte(validDocResolution))
require.NoError(t, err)
require.Equal(t, 1, len(d.Context))
require.Equal(t, "https://w3id.org/did-resolution/v1", d.Context[0])
require.Equal(t, "did:example:21tDAKCERh95uGgKbJNHYp", d.DIDDocument.ID)
require.Equal(t, true, d.DocumentMetadata.Method.Published)
require.Equal(t, "did:ex:123333", d.DocumentMetadata.CanonicalID)
bytes, err := d.JSONBytes()
require.NoError(t, err)
d, err = ParseDocumentResolution(bytes)
require.NoError(t, err)
require.Equal(t, 1, len(d.Context))
require.Equal(t, "https://w3id.org/did-resolution/v1", d.Context[0])
require.Equal(t, "did:example:21tDAKCERh95uGgKbJNHYp", d.DIDDocument.ID)
require.Equal(t, true, d.DocumentMetadata.Method.Published)
require.Equal(t, "did:ex:123333", d.DocumentMetadata.CanonicalID)
})
t.Run("test did doc not exists", func(t *testing.T) {
_, err := ParseDocumentResolution([]byte(validDoc))
require.Error(t, err)
require.Contains(t, err.Error(), ErrDIDDocumentNotExist.Error())
})
}
func TestValid(t *testing.T) {
docs := []string{validDoc}
for _, d := range docs {
doc, err := ParseDocument([]byte(d))
require.NoError(t, err)
require.NotNil(t, doc)
require.Contains(t, doc.Context[0], "https://www.w3.org/ns/did/v")
// test doc id
require.Equal(t, doc.ID, "did:example:21tDAKCERh95uGgKbJNHYp")
hexDecodeValue, err := hex.DecodeString("02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71")
block, _ := pem.Decode([]byte(pemPK))
require.NotNil(t, block)
require.NoError(t, err)
// test authentication
eAuthentication := []Verification{
{VerificationMethod: *NewVerificationMethodFromBytes("did:example:123456789abcdefghi#keys-1",
"Secp256k1VerificationKey2018",
"did:example:123456789abcdefghi",
base58.Decode("H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV")), Relationship: Authentication},
{VerificationMethod: VerificationMethod{
ID: "did:example:123456789abcdefghs#key3",
Controller: "did:example:123456789abcdefghs",
Type: "RsaVerificationKey2018",
Value: hexDecodeValue,
}, Relationship: Authentication, Embedded: true},
}
require.Equal(t, eAuthentication, doc.Authentication)
// test public key
ePubKey := []VerificationMethod{
{
ID: "did:example:123456789abcdefghi#keys-1",
Controller: "did:example:123456789abcdefghi",
Type: "Secp256k1VerificationKey2018",
Value: base58.Decode("H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"),
},
{
ID: "did:example:123456789abcdefghw#key2",
Controller: "did:example:123456789abcdefghw",
Type: "RsaVerificationKey2018",
Value: block.Bytes,
},
}
require.Equal(t, ePubKey, doc.VerificationMethod)
// test services
eServices := []Service{
{
ID: "did:example:123456789abcdefghi#inbox",
Type: "SocialWebInboxService",
ServiceEndpoint: "https://social.example.com/83hfh37dj",
Properties: map[string]interface{}{"spamCost": map[string]interface{}{"amount": "0.50", "currency": "USD"}},
},
{
ID: "did:example:123456789abcdefghi#did-communication",
Type: "did-communication",
Priority: 0,
RecipientKeys: []string{"did:example:123456789abcdefghi#key2"},
RoutingKeys: []string{"did:example:123456789abcdefghi#key2"},
ServiceEndpoint: "https://agent.example.com/",
Properties: map[string]interface{}{},
recipientKeysRelativeURL: map[string]bool{"did:example:123456789abcdefghi#key2": false},
routingKeysRelativeURL: map[string]bool{"did:example:123456789abcdefghi#key2": false},
},
}
require.EqualValues(t, eServices, doc.Service)
}
}
func TestInvalid(t *testing.T) {
doc, err := ParseDocument([]byte(invalidDoc))
require.Error(t, err)
require.Nil(t, doc)
require.Contains(t, err.Error(), "did document not valid")
}
func TestValidWithProof(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
doc, err := ParseDocument([]byte(d))
require.NoError(t, err)
require.NotNil(t, doc)
// test proof
created, err := time.Parse(time.RFC3339, "2019-09-23T14:16:59.484733-04:00")
require.NoError(t, err)
const encProofValue = "6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA"
proofValue, err := base64.RawURLEncoding.DecodeString(encProofValue)
require.NoError(t, err)
nonce, err := base64.RawURLEncoding.DecodeString("")
require.NoError(t, err)
eProof := Proof{
Type: "Ed25519Signature2018",
Created: &created,
Creator: "did:method:abc#key-1",
ProofValue: proofValue,
Domain: "",
Nonce: nonce,
}
require.Equal(t, []Proof{eProof}, doc.Proof)
byteDoc, err := doc.JSONBytes()
require.NoError(t, err)
require.NotNil(t, byteDoc)
// test invalid created
docWithInvalid := docWithInvalidCreatedInProof
if d == validDocV011WithProof {
docWithInvalid = docV011WithInvalidCreatedInProof
}
invalidDoc, err := ParseDocument([]byte(docWithInvalid))
require.NotNil(t, err)
require.Nil(t, invalidDoc)
require.Contains(t, err.Error(), "populate proofs failed")
}
}
func TestInvalidEncodingInProof(t *testing.T) {
proofKey := []string{jsonldProofValue, jsonldSignatureValue}
for _, v := range proofKey {
c := ContextV1
if v == jsonldSignatureValue {
c = contextV011
}
// invalid encoding in nonce
rawProofs := []interface{}{map[string]interface{}{
"created": "2011-09-23T20:21:34Z",
"creator": "did:method:abc#key-1",
v: "6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA",
"nonce": "Invalid\x01",
"type": "Ed25519Signature2018",
}}
doc, err := populateProofs(c, "", "", rawProofs)
require.NotNil(t, err)
require.Nil(t, doc)
require.Contains(t, err.Error(), "illegal base64 data")
// invalid encoding in proof value
rawProofs = []interface{}{map[string]interface{}{
"created": "2011-09-23T20:21:34Z",
"creator": "did:method:abc#key-1",
v: "Invalid\x01",
"type": "Ed25519Signature2018",
}}
doc, err = populateProofs(c, "", "", rawProofs)
require.NotNil(t, err)
require.Nil(t, doc)
require.Contains(t, err.Error(), "illegal base64 data")
}
}
func TestPopulateAuthentications(t *testing.T) {
t.Run("test key not exist", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDoc), &raw))
raw.Authentication[0] = missingPubKeyID
bytes, err := json.Marshal(raw)
require.NoError(t, err)
_, err = ParseDocument(bytes)
require.Error(t, err)
expected := fmt.Sprintf("key %s does not exist in did doc verification method", missingPubKeyID)
require.Contains(t, err.Error(), expected)
})
t.Run("test key not exist v0.11", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDocV011), &raw))
m := make(map[string]string)
m[jsonldPublicKey] = missingPubKeyID
m["type"] = "key"
raw.Authentication[0] = m
bytes, err := json.Marshal(raw)
require.NoError(t, err)
_, err = ParseDocument(bytes)
require.Error(t, err)
expected := fmt.Sprintf("key %s does not exist in did doc verification method", missingPubKeyID)
require.Contains(t, err.Error(), expected)
})
}
func TestPopulateAssertionMethods(t *testing.T) {
t.Run("test key does not exist", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(docV011WithVerificationRelationships), &raw))
raw.AssertionMethod[0] = missingPubKeyID
bytes, err := json.Marshal(raw)
require.NoError(t, err)
_, err = ParseDocument(bytes)
require.Error(t, err)
expected := fmt.Sprintf("key %s does not exist in did doc verification method", missingPubKeyID)
require.Contains(t, err.Error(), expected)
})
}
func TestPopulateCapabilityDelegations(t *testing.T) {
t.Run("test key not exist", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(docV011WithVerificationRelationships), &raw))
raw.CapabilityDelegation[0] = missingPubKeyID
bytes, err := json.Marshal(raw)
require.NoError(t, err)
_, err = ParseDocument(bytes)
require.Error(t, err)
expected := fmt.Sprintf("key %s does not exist in did doc verification method", missingPubKeyID)
require.Contains(t, err.Error(), expected)
})
}
func TestPopulateCapabilityInvocations(t *testing.T) {
t.Run("test key not exist", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(docV011WithVerificationRelationships), &raw))
raw.CapabilityInvocation[0] = missingPubKeyID
bytes, err := json.Marshal(raw)
require.NoError(t, err)
_, err = ParseDocument(bytes)
require.Error(t, err)
expected := fmt.Sprintf("key %s does not exist in did doc verification method", missingPubKeyID)
require.Contains(t, err.Error(), expected)
})
}
func TestPopulateKeyAgreements(t *testing.T) {
t.Run("test key not exist", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(docV011WithVerificationRelationships), &raw))
raw.KeyAgreement[0] = missingPubKeyID
bytes, err := json.Marshal(raw)
require.NoError(t, err)
_, err = ParseDocument(bytes)
require.EqualError(t, err, fmt.Sprintf("populate key agreements failed: key %s does not exist in did doc"+
" verification method", missingPubKeyID))
})
}
func TestPublicKeys(t *testing.T) {
t.Run("test failed to decode PEM block", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
if len(raw.PublicKey) != 0 {
raw.PublicKey[1][jsonldPublicKeyPem] = wrongDataMsg
} else {
raw.VerificationMethod[1][jsonldPublicKeyPem] = wrongDataMsg
}
bytes, err := json.Marshal(raw)
require.NoError(t, err)
_, err = ParseDocument(bytes)
require.Error(t, err)
require.Contains(t, err.Error(), "failed to decode PEM block containing public key")
}
})
t.Run("test public key encoding not supported", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
if len(raw.PublicKey) != 0 {
delete(raw.PublicKey[1], jsonldPublicKeyPem)
raw.PublicKey[1]["publicKeyMultibase"] = wrongDataMsg
} else {
delete(raw.VerificationMethod[1], jsonldPublicKeyPem)
raw.VerificationMethod[1]["publicKeyMultibase"] = wrongDataMsg
}
bytes, err := json.Marshal(raw)
require.NoError(t, err)
_, err = ParseDocument(bytes)
require.Error(t, err)
require.Contains(t, err.Error(), "public key encoding not supported")
}
})
}
func TestParseDocument(t *testing.T) {
// test error from Unmarshal
_, err := ParseDocument([]byte(wrongDataMsg))
require.Error(t, err)
require.Contains(t, err.Error(), "JSON marshalling of did doc bytes bytes failed")
}
func TestValidateDidDocContext(t *testing.T) {
t.Run("test did doc with empty context", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.Context = nil
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "@context is required")
}
})
t.Run("test did doc with invalid context", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDoc), &raw))
raw.Context = []string{"https://w3id.org/did/v2", "https://w3id.org/did/v1"}
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "Does not match pattern '^https://")
})
}
func TestValidateDidDocID(t *testing.T) {
t.Run("test did doc with empty id", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.ID = ""
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "id is required")
}
})
}
func TestValidateDidDocPublicKey(t *testing.T) {
t.Run("test did doc with empty public key", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.VerificationMethod = nil
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
}
})
t.Run("test did doc public key without id", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
if len(raw.PublicKey) != 0 {
delete(raw.PublicKey[0], jsonldID)
} else {
delete(raw.VerificationMethod[0], jsonldID)
}
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "id is required")
}
})
t.Run("test did doc public key without type", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
if len(raw.PublicKey) != 0 {
delete(raw.PublicKey[0], jsonldType)
} else {
delete(raw.VerificationMethod[0], jsonldType)
}
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "type is required")
}
})
t.Run("test did doc public key without controller", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDoc), &raw))
if len(raw.PublicKey) != 0 {
delete(raw.PublicKey[0], jsonldController)
} else {
delete(raw.VerificationMethod[0], jsonldController)
}
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "controller is required")
})
t.Run("test did doc public key without controller v0.11", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDocV011), &raw))
delete(raw.PublicKey[0], jsonldOwner)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
})
}
func TestValidateDidDocAuthentication(t *testing.T) {
t.Run("test did doc with empty auth", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.Authentication = nil
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
}
})
t.Run("test did doc with invalid auth type", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.Authentication[0] = 1
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "Invalid type. Expected: object, given: integer")
}
})
t.Run("test did doc auth public key without id", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
pk, ok := raw.Authentication[1].(map[string]interface{})
require.True(t, ok)
delete(pk, jsonldID)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "id is required")
}
})
t.Run("test did doc auth public key without type", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
pk, ok := raw.Authentication[1].(map[string]interface{})
require.True(t, ok)
delete(pk, jsonldType)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "type is required")
}
})
t.Run("test did doc auth public key without controller", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDoc), &raw))
pk, ok := raw.Authentication[1].(map[string]interface{})
require.True(t, ok)
delete(pk, jsonldController)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "controller is required")
})
t.Run("test did doc auth public key without controller v0.11", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDocV011), &raw))
pk, ok := raw.Authentication[1].(map[string]interface{})
require.True(t, ok)
delete(pk, jsonldOwner)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
})
}
func TestValidateDidDocService(t *testing.T) {
t.Run("test did doc with empty service", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.Service = nil
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
}
})
t.Run("test did doc service without id", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
delete(raw.Service[0], jsonldID)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "id is required")
}
})
t.Run("test did doc service without type", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
delete(raw.Service[0], jsonldType)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "type is required")
}
})
t.Run("test did doc service without serviceEndpoint", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
delete(raw.Service[0], jsonldServicePoint)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "serviceEndpoint is required")
}
})
}
func TestValidateDidDocCreated(t *testing.T) {
t.Run("test did doc with empty created", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.Created = nil
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
}
})
t.Run("test did doc with wrong format created", func(t *testing.T) {
docs := []string{docWithInvalidCreated, docV011WithInvalidCreated}
for _, d := range docs {
doc, err := ParseDocument([]byte(d))
require.Error(t, err)
require.Nil(t, doc)
require.Contains(t, err.Error(), "cannot parse")
}
})
}
func TestValidateDidDocUpdated(t *testing.T) {
t.Run("test did doc with empty updated", func(t *testing.T) {
docs := []string{validDoc, validDocV011}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.Updated = nil
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
}
})
t.Run("test did doc with wrong format updated", func(t *testing.T) {
docs := []string{docWithInvalidUpdated, docV011WithInvalidUpdated}
for _, d := range docs {
doc, err := ParseDocument([]byte(d))
require.Error(t, err)
require.Nil(t, doc)
require.Contains(t, err.Error(), "cannot parse")
}
})
}
func TestValidateDidDocProof(t *testing.T) {
t.Run("test did doc with empty proof", func(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
raw.Proof = nil
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
}
})
t.Run("test did doc proof without type", func(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
proof, ok := raw.Proof[0].(map[string]interface{})
require.True(t, ok)
delete(proof, jsonldType)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "type is required")
}
})
t.Run("test did doc proof without created", func(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
proof, ok := raw.Proof[0].(map[string]interface{})
require.True(t, ok)
delete(proof, jsonldCreated)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "created is required")
}
})
t.Run("test did doc proof without creator", func(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
proof, ok := raw.Proof[0].(map[string]interface{})
require.True(t, ok)
delete(proof, jsonldCreator)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "creator is required")
}
})
t.Run("test did doc proof without proofValue", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDocWithProof), &raw))
proof, ok := raw.Proof[0].(map[string]interface{})
require.True(t, ok)
delete(proof, jsonldProofValue)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "proofValue is required")
})
t.Run("test did doc proof without proofValue v0.11", func(t *testing.T) {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(validDocV011WithProof), &raw))
proof, ok := raw.Proof[0].(map[string]interface{})
require.True(t, ok)
delete(proof, jsonldSignatureValue)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.Error(t, err)
require.Contains(t, err.Error(), "signatureValue is required")
})
t.Run("test did doc proof without domain", func(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
proof, ok := raw.Proof[0].(map[string]interface{})
require.True(t, ok)
delete(proof, jsonldDomain)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
}
})
t.Run("test did doc proof without nonce", func(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
raw := &rawDoc{}
require.NoError(t, json.Unmarshal([]byte(d), &raw))
proof, ok := raw.Proof[0].(map[string]interface{})
require.True(t, ok)
delete(proof, jsonldNonce)
bytes, err := json.Marshal(raw)
require.NoError(t, err)
err = validate(bytes, raw.schemaLoader())
require.NoError(t, err)
}
})
}
func TestRequiresLegacyHandling(t *testing.T) {
doc := &rawDoc{}
err := json.Unmarshal([]byte(validDocV011), doc)
require.NoError(t, err)
doc.Context = []string{ContextV1Old}
ret := requiresLegacyHandling(doc)
require.Equal(t, true, ret)
doc.Context = []string{ContextV1}
ret = requiresLegacyHandling(doc)
require.Equal(t, false, ret)
doc.Context = []string{contextV011}
ret = requiresLegacyHandling(doc)
require.Equal(t, false, ret)
}
func TestJSONConversion(t *testing.T) {
docs := []string{
validDoc, validDocV011, validDocWithProofAndJWK, docV011WithVerificationRelationships, validDocWithBase,
}
for _, d := range docs {
// setup -> create Document from json byte data
doc, err := ParseDocument([]byte(d))
require.NoError(t, err)
require.NotEmpty(t, doc)
// convert Document to json byte data
byteDoc, err := doc.JSONBytes()
require.NoError(t, err)
require.NotEmpty(t, byteDoc)
if d != validDocWithBase {
require.NotContains(t, string(byteDoc), "@base")
} else {
require.Contains(t, string(byteDoc), "@base")
}
// convert json byte data to document
doc2, err := ParseDocument(byteDoc)
require.NoError(t, err)
require.NotEmpty(t, doc2)
// verify documents created by ParseDocument and JSONBytes function matches
require.Equal(t, doc, doc2)
}
}
func TestMarshalJSON(t *testing.T) {
docs := []string{
validDoc, validDocV011, validDocWithProofAndJWK, docV011WithVerificationRelationships, validDocWithBase,
}
for _, d := range docs {
// setup -> create Document from json byte data
doc := &Doc{}
err := json.Unmarshal([]byte(d), doc)
require.NoError(t, err)