-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_test.go
More file actions
148 lines (133 loc) · 3.73 KB
/
Copy pathauto_test.go
File metadata and controls
148 lines (133 loc) · 3.73 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
package crypt_test
import (
"bytes"
"crypto/rand"
"encoding/base64"
"errors"
"strings"
"testing"
"github.com/ubgo/crypt"
)
func ringKey32(t *testing.T) []byte {
t.Helper()
k := make([]byte, 32)
if _, err := rand.Read(k); err != nil {
t.Fatalf("rand: %v", err)
}
return k
}
func TestOpenAuto_OpensAEAD(t *testing.T) {
key := ringKey32(t)
ct, err := crypt.Seal(key, []byte("hello via aead"), []byte("ctx"))
if err != nil {
t.Fatal(err)
}
pt, err := crypt.OpenAuto(key, ct, []byte("ctx"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(pt, []byte("hello via aead")) {
t.Errorf("got %q", pt)
}
}
func TestOpenAuto_OpensCBC(t *testing.T) {
key := ringKey32(t)
ct, err := crypt.EncryptCBC(key, []byte("hello via cbc"))
if err != nil {
t.Fatal(err)
}
pt, err := crypt.OpenAuto(key, ct, nil)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(pt, []byte("hello via cbc")) {
t.Errorf("got %q", pt)
}
}
func TestOpenAuto_AADIgnoredForCBC(t *testing.T) {
key := ringKey32(t)
ct, _ := crypt.EncryptCBC(key, []byte("payload"))
pt, err := crypt.OpenAuto(key, ct, []byte("ignored"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(pt, []byte("payload")) {
t.Errorf("got %q", pt)
}
}
func TestOpenAuto_RejectsGarbage(t *testing.T) {
key := ringKey32(t)
if _, err := crypt.OpenAuto(key, "this-is-not-encrypted-anything", nil); !errors.Is(err, crypt.ErrUnknownFormat) {
t.Errorf("got %v, want ErrUnknownFormat", err)
}
}
func TestOpenAuto_RejectsWrongKey_AEAD(t *testing.T) {
good := ringKey32(t)
bad := ringKey32(t)
ct, _ := crypt.Seal(good, []byte("x"), nil)
if _, err := crypt.OpenAuto(bad, ct, nil); err == nil {
t.Errorf("expected error for wrong key")
}
}
func TestOpenAuto_EmptyString(t *testing.T) {
key := bytes.Repeat([]byte{0x01}, 32)
if _, err := crypt.OpenAuto(key, "", nil); !errors.Is(err, crypt.ErrUnknownFormat) {
t.Errorf("got %v, want ErrUnknownFormat", err)
}
}
func TestOpenAuto_BadBase64NotHex(t *testing.T) {
key := bytes.Repeat([]byte{0x01}, 32)
if _, err := crypt.OpenAuto(key, "***clearly-neither***", nil); !errors.Is(err, crypt.ErrUnknownFormat) {
t.Errorf("got %v, want ErrUnknownFormat", err)
}
}
func TestOpenAuto_HexButNotCBCAligned(t *testing.T) {
key := bytes.Repeat([]byte{0x01}, 32)
bad := strings.Repeat("00", 17)
if _, err := crypt.OpenAuto(key, bad, nil); !errors.Is(err, crypt.ErrUnknownFormat) {
t.Errorf("got %v, want ErrUnknownFormat", err)
}
}
func TestOpenAuto_HexShorterThanIV(t *testing.T) {
key := bytes.Repeat([]byte{0x01}, 32)
bad := strings.Repeat("00", 8)
if _, err := crypt.OpenAuto(key, bad, nil); !errors.Is(err, crypt.ErrUnknownFormat) {
t.Errorf("got %v, want ErrUnknownFormat", err)
}
}
func TestOpenAuto_AEADCoincidentalCBCFallthrough(t *testing.T) {
key := bytes.Repeat([]byte{0x01}, 32)
too := base64.RawURLEncoding.EncodeToString([]byte{0x01, 0x02, 0x03})
if _, err := crypt.OpenAuto(key, too, nil); !errors.Is(err, crypt.ErrUnknownFormat) {
t.Errorf("got %v, want ErrUnknownFormat", err)
}
}
func TestOpenAuto_WithAADOnAEAD(t *testing.T) {
key := bytes.Repeat([]byte{0x01}, 32)
aad := []byte("ctx")
ct, _ := crypt.Seal(key, []byte("data"), aad)
pt, err := crypt.OpenAuto(key, ct, aad)
if err != nil {
t.Fatal(err)
}
if string(pt) != "data" {
t.Errorf("got %q", pt)
}
if _, err := crypt.OpenAuto(key, ct, []byte("other")); err == nil {
t.Errorf("expected error with wrong AAD")
}
}
func TestOpenAuto_AllKeySizesForCBC(t *testing.T) {
for _, n := range []int{16, 24, 32} {
key := bytes.Repeat([]byte{0x01}, n)
ct, _ := crypt.EncryptCBC(key, []byte("payload"))
pt, err := crypt.OpenAuto(key, ct, nil)
if err != nil {
t.Errorf("CBC key=%d: %v", n, err)
continue
}
if string(pt) != "payload" {
t.Errorf("CBC key=%d: got %q", n, pt)
}
}
}