-
-
Notifications
You must be signed in to change notification settings - Fork 519
/
testkit_test.go
177 lines (167 loc) · 4.08 KB
/
testkit_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
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.18
// +build go1.18
package age_test
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"filippo.io/age"
"filippo.io/age/armor"
)
func TestVectors(t *testing.T) {
if _, err := exec.LookPath("go"); err != nil {
t.Skipf("skipping test because 'go' command is unavailable: %v", err)
}
// Download the testkit files from CCTV using `go mod download -json` so the
// cached source of the testdata can be reused.
path := "c2sp.org/CCTV/age@v0.0.0-20221027185432-cfaa74dc42af"
cmd := exec.Command("go", "mod", "download", "-json", path)
output, err := cmd.Output()
if err != nil {
t.Fatalf("failed to run `go mod download -json %s`, output: %s", path, output)
}
var dm struct {
Dir string // absolute path to cached source root directory
}
if err := json.Unmarshal(output, &dm); err != nil {
t.Fatal(err)
}
testkitDir := filepath.Join(dm.Dir, "testdata")
tests, err := filepath.Glob(testkitDir + "/*")
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
contents, err := os.ReadFile(test)
if err != nil {
t.Fatal(err)
}
name := filepath.Base(test)
t.Run(name, func(t *testing.T) {
testVector(t, contents)
})
}
}
func testVector(t *testing.T, test []byte) {
var (
expect string
payloadHash *[32]byte
identities []age.Identity
armored bool
)
for {
line, rest, ok := bytes.Cut(test, []byte("\n"))
if !ok {
t.Fatal("invalid test file: no payload")
}
test = rest
if len(line) == 0 {
break
}
key, value, _ := strings.Cut(string(line), ": ")
switch key {
case "expect":
switch value {
case "success":
case "HMAC failure":
case "header failure":
case "armor failure":
case "payload failure":
case "no match":
default:
t.Fatal("invalid test file: unknown expect value:", value)
}
expect = value
case "payload":
h, err := hex.DecodeString(value)
if err != nil {
t.Fatal(err)
}
payloadHash = (*[32]byte)(h)
case "identity":
i, err := age.ParseX25519Identity(value)
if err != nil {
t.Fatal(err)
}
identities = append(identities, i)
case "passphrase":
i, err := age.NewScryptIdentity(value)
if err != nil {
t.Fatal(err)
}
identities = append(identities, i)
case "armored":
armored = true
case "file key":
// Ignored.
case "comment":
t.Log(value)
default:
t.Fatal("invalid test file: unknown header key:", key)
}
}
var in io.Reader = bytes.NewReader(test)
if armored {
in = armor.NewReader(in)
}
r, err := age.Decrypt(in, identities...)
if err != nil && strings.HasSuffix(err.Error(), "bad header MAC") {
if expect == "HMAC failure" {
t.Log(err)
return
}
t.Fatalf("expected %s, got HMAC error", expect)
} else if e := new(armor.Error); errors.As(err, &e) {
if expect == "armor failure" {
t.Log(err)
return
}
t.Fatalf("expected %s, got: %v", expect, err)
} else if _, ok := err.(*age.NoIdentityMatchError); ok {
if expect == "no match" {
t.Log(err)
return
}
t.Fatalf("expected %s, got: %v", expect, err)
} else if err != nil {
if expect == "header failure" {
t.Log(err)
return
}
t.Fatalf("expected %s, got: %v", expect, err)
} else if expect != "success" && expect != "payload failure" &&
expect != "armor failure" {
t.Fatalf("expected %s, got success", expect)
}
out, err := io.ReadAll(r)
if err != nil && expect == "success" {
t.Fatalf("expected %s, got: %v", expect, err)
} else if err != nil {
t.Log(err)
if expect == "armor failure" {
if e := new(armor.Error); !errors.As(err, &e) {
t.Errorf("expected armor.Error, got %T", err)
}
}
if payloadHash != nil && sha256.Sum256(out) != *payloadHash {
t.Error("partial payload hash mismatch")
}
return
} else if expect != "success" {
t.Fatalf("expected %s, got success", expect)
}
if sha256.Sum256(out) != *payloadHash {
t.Error("payload hash mismatch")
}
}