-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsuite_test.go
153 lines (135 loc) · 4.03 KB
/
testsuite_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
package trustdidweb
import (
"crypto/ed25519"
"encoding/json"
"fmt"
"os"
"slices"
"testing"
"time"
"github.com/lestrrat-go/jwx/jwk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testEntry struct {
Id string `json:"id"`
Type []string `json:"type"`
Purpose string `json:"purpose"`
Input string `json:"input,omitempty"`
Expect string `json:"expect"`
SigningKey map[string]interface{} `json:"signingKey,omitempty"`
Params LogParams `json:"params,omitempty"`
DIDDocument DIDDocument `json:"didDocument"`
Options testEntryOptions `json:"options,omitempty"`
}
type testEntryOptions struct {
SigningTime time.Time `json:"signingTime,format:RFC3339"`
}
func (e testEntry) key(t *testing.T) ed25519.PrivateKey {
t.Helper()
jwkKey, _ := json.Marshal(e.SigningKey)
key, err := jwk.ParseKey(jwkKey)
if err != nil {
t.Fatalf("failed to get private key: %s", err)
}
privKey := ed25519.PrivateKey{}
if err := key.Raw(&privKey); err != nil {
t.Fatalf("failed to get private key: %s", err)
}
return privKey
}
func TestSuite(t *testing.T) {
const CreationTest = "CreationTest"
const VerificationTest = "VerificationTest"
const UpdateTest = "UpdateTest"
const PositiveEvaluationTest = "PositiveEvaluationTest"
const NegativeEvaluationTest = "NegativeEvaluationTest"
t.Run("TestSuite", func(t *testing.T) {
manifestFile := "testdata/manifest.json"
// Load the manifest
manifest, err := os.ReadFile(manifestFile)
if err != nil {
t.Fatalf("failed to read manifest file: %s", err)
}
// Unmarshal the manifest
var entries []testEntry
if err := json.Unmarshal(manifest, &entries); err != nil {
t.Fatalf("failed to unmarshal manifest file: %s", err)
}
// Run the TestSuite
for _, entry := range entries {
name := fmt.Sprintf("%s-%s", entry.Id, entry.Purpose)
t.Run(name, func(t *testing.T) {
if !entry.Options.SigningTime.IsZero() {
oldTimeFunc := timeFunc
defer func() {
timeFunc = oldTimeFunc
}()
timeFunc = func() time.Time {
return entry.Options.SigningTime
}
}
var resLog DIDLog
switch entry.Type[0] {
case CreationTest:
privKey := entry.key(t)
resLog, err = Create(entry.DIDDocument, privKey)
if err != nil {
require.NoError(t, err)
}
case UpdateTest:
privKey := entry.key(t)
inputFile, err := os.ReadFile(entry.Input)
if err != nil {
t.Fatalf("failed to read input file: %s", err)
}
inputLog, err := ParseLog(inputFile)
if err != nil {
t.Fatalf("failed to parse input file: %s", err)
}
resLog, err = inputLog.Update(entry.Params, entry.DIDDocument, privKey)
if err != nil {
t.Fatalf("failed to update DIDDocument: %s", err)
}
// check if the documents match
if len(entry.DIDDocument) > 0 {
actualDoc, err := resLog.Document()
if err != nil {
t.Fatalf("failed to get DIDDocument: %s", err)
}
assert.Equal(t, entry.DIDDocument, actualDoc)
}
case VerificationTest:
inputFile, err := os.ReadFile(entry.Input)
if err != nil {
t.Fatalf("failed to read input file: %s", err)
}
inputLog, err := ParseLog(inputFile)
if err != nil {
t.Fatalf("failed to parse input file: %s", err)
}
err = inputLog.Verify()
if slices.Contains(entry.Type, PositiveEvaluationTest) {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
default:
t.Skipf("unsupported test type: %s", entry.Type[0])
}
// only check if there is an expected result defined
if entry.Expect != "" {
actual, err := resLog.MarshalText()
if err != nil {
t.Fatalf("failed to marshal DIDDocument")
}
expected, err := os.ReadFile(entry.Expect)
if err != nil {
t.Fatalf("failed to read expected file: %s", err)
}
assert.Equal(t, string(expected), string(actual))
}
})
}
})
}