-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsupport_test.go
168 lines (126 loc) · 4.76 KB
/
support_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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
_ "embed"
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
ldcontext "github.com/trustbloc/did-go/doc/ld/context"
lddocloader "github.com/trustbloc/did-go/doc/ld/documentloader"
jsonldsig "github.com/trustbloc/did-go/doc/ld/processor"
ldtestutil "github.com/trustbloc/did-go/doc/ld/testutil"
kmsapi "github.com/trustbloc/kms-go/spi/kms"
"github.com/trustbloc/vc-go/proof/checker"
"github.com/trustbloc/vc-go/proof/testsupport"
)
var (
//go:embed testdata/v1_valid_credential.jsonld
v1ValidCredential string //nolint:gochecknoglobals
//go:embed testdata/v1_credential_without_issuancedate.jsonld
v1CredentialWithoutIssuanceDate string //nolint:gochecknoglobals
//go:embed testdata/v2_valid_credential_multi_status.jsonld
v2ValidCredentialMultiStatus string //nolint:gochecknoglobals
)
var (
//go:embed testdata/v2_valid_credential.jsonld
v2ValidCredential string //nolint:gochecknoglobals
//go:embed testdata/v2_credential_without_issuer.jsonld
v2CredentialWithoutIssuer string //nolint:gochecknoglobals
)
func (vc *Credential) stringJSON(t *testing.T) string {
bytes, err := json.Marshal(vc)
require.NoError(t, err)
return string(bytes)
}
func jsonObjectToString(t *testing.T, vc JSONObject) string {
bytes, err := json.Marshal(vc)
require.NoError(t, err)
return string(bytes)
}
func (vc *Credential) byteJSON(t *testing.T) []byte {
bytes, err := json.Marshal(vc)
require.NoError(t, err)
return bytes
}
func (vp *Presentation) stringJSON(t *testing.T) string {
bytes, err := json.Marshal(vp)
require.NoError(t, err)
return string(bytes)
}
func createVCWithLinkedDataProof(t *testing.T) (*Credential, *checker.ProofChecker) {
t.Helper()
vc, err := ParseCredential([]byte(v1ValidCredential),
WithJSONLDDocumentLoader(createTestDocumentLoader(t)),
WithDisabledProofCheck())
require.NoError(t, err)
created := time.Now()
proofCreator, proofChecker := testsupport.NewKMSSigVerPair(t, kmsapi.ED25519Type,
"did:example:76e12ec712ebc6f1c221ebfeb1f#any")
err = vc.AddLinkedDataProof(&LinkedDataProofContext{
SignatureType: "Ed25519Signature2018",
KeyType: kmsapi.ED25519Type,
ProofCreator: proofCreator,
SignatureRepresentation: SignatureJWS,
Created: &created,
VerificationMethod: "did:example:76e12ec712ebc6f1c221ebfeb1f#any",
}, jsonldsig.WithDocumentLoader(createTestDocumentLoader(t)))
require.NoError(t, err)
return vc, proofChecker
}
func createVCWithTwoLinkedDataProofs(t *testing.T) (*Credential, *checker.ProofChecker) {
t.Helper()
vc, err := ParseCredential([]byte(v1ValidCredential),
WithJSONLDDocumentLoader(createTestDocumentLoader(t)),
WithDisabledProofCheck())
require.NoError(t, err)
created := time.Now()
proofCreators, proofChecker := testsupport.NewKMSSignersAndVerifier(t, []*testsupport.SigningKey{
{
Type: kmsapi.ED25519Type,
PublicKeyID: "did:example:76e12ec712ebc6f1c221ebfeb1f#key1",
},
{
Type: kmsapi.ED25519Type,
PublicKeyID: "did:example:76e12ec712ebc6f1c221ebfeb1f#key2",
},
})
err = vc.AddLinkedDataProof(&LinkedDataProofContext{
SignatureType: "Ed25519Signature2018",
KeyType: kmsapi.ED25519Type,
ProofCreator: proofCreators[0],
SignatureRepresentation: SignatureJWS,
Created: &created,
VerificationMethod: "did:example:76e12ec712ebc6f1c221ebfeb1f#key1",
}, jsonldsig.WithDocumentLoader(createTestDocumentLoader(t)))
require.NoError(t, err)
err = vc.AddLinkedDataProof(&LinkedDataProofContext{
SignatureType: "Ed25519Signature2018",
KeyType: kmsapi.ED25519Type,
ProofCreator: proofCreators[1],
SignatureRepresentation: SignatureJWS,
Created: &created,
VerificationMethod: "did:example:76e12ec712ebc6f1c221ebfeb1f#key2",
}, jsonldsig.WithDocumentLoader(createTestDocumentLoader(t)))
require.NoError(t, err)
return vc, proofChecker
}
func createTestDocumentLoader(t *testing.T, extraContexts ...ldcontext.Document) *lddocloader.DocumentLoader {
t.Helper()
loader, err := ldtestutil.DocumentLoader(extraContexts...)
require.NoError(t, err)
return loader
}
func parseTestCredential(t *testing.T, vcData []byte, opts ...CredentialOpt) (*Credential, error) {
t.Helper()
return ParseCredential(vcData,
append([]CredentialOpt{WithJSONLDDocumentLoader(createTestDocumentLoader(t))}, opts...)...)
}
func newTestPresentation(t *testing.T, vpData []byte, opts ...PresentationOpt) (*Presentation, error) {
t.Helper()
return ParsePresentation(vpData,
append([]PresentationOpt{WithPresJSONLDDocumentLoader(createTestDocumentLoader(t))}, opts...)...)
}