-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_test.go
More file actions
141 lines (128 loc) · 4.68 KB
/
Copy pathdecode_test.go
File metadata and controls
141 lines (128 loc) · 4.68 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
package cose
import (
"io"
"testing"
"github.com/stretchr/testify/require"
)
func TestDecodeMalformed(t *testing.T) {
cases := []struct {
name string
hex string
want error
}{
{"empty input", "", ErrMalformed},
{"truncated array", "d86084", ErrMalformed},
{"truncated mid-item", "d8608443a101", ErrMalformed},
{"not a tag (bare array)", "8440a0f6818340a041aa", ErrNotEncrypt},
{"bare integer, not a tag", "01", ErrNotEncrypt},
{"unsupported tag (55799)", "d9d9f78440a0f6818340a041aa", ErrNotEncrypt},
{"tag 16 with 4 elems (Encrypt0 shape violated)", "d08440a0f6818340a041aa", ErrMalformed},
{"tag 96 array too short (3 elems)", "d8608340a0f6", ErrMalformed},
{"array too long (5 elems)", "d8608540a0f6818340a041aa40", ErrMalformed},
{"protected not a byte string", "d86084a0a0f6818340a041aa", ErrMalformed},
{"protected content not a map", "d860844103a0f6818340a041aa", ErrMalformed},
{"unprotected not a map", "d860844040f6818340a041aa", ErrMalformed},
{"body ciphertext not null", "d8608440a041ff818340a041aa", ErrDetachedPayload},
{"recipients not an array", "d8608440a0f640", ErrMalformed},
{"recipients empty", "d8608440a0f680", ErrNoRecipients},
{"recipient not an array", "d8608440a0f68140", ErrMalformed},
{"recipient array too short (2)", "d8608440a0f6818240a0", ErrMalformed},
{"recipient nested (4 elems)", "d8608440a0f6818440a041aa80", ErrMalformed},
{"recipient ciphertext wrong type", "d8608440a0f6818340a001", ErrMalformed},
{"duplicate protected label", "d8608445a201030104a0f6818340a041aa", ErrMalformed},
{"duplicate unprotected label", "d8608440a201030104f6818340a041aa", ErrMalformed},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Tolerate spaces in the hex literals above for readability.
raw := hexDec(t, removeSpaces(tc.hex))
env, rest, err := Decode(raw)
require.ErrorIs(t, err, tc.want)
require.Nil(t, env)
require.Nil(t, rest)
})
}
}
// TestDecodeTruncatedIsUnexpectedEOF pins that a decode failing only because the
// input stopped mid-item is distinguishable from one whose bytes are complete and
// wrong. A caller reading a prefix of a larger object (see fee's envelope-header
// probe) grows its read only for the former; without this a complete-but-invalid
// item would be re-read at ever larger sizes to no purpose.
func TestDecodeTruncatedIsUnexpectedEOF(t *testing.T) {
truncated := map[string]string{
"empty input": "",
"truncated array": "d86084",
"truncated mid-item": "d8608443a101",
}
for name, h := range truncated {
t.Run(name, func(t *testing.T) {
_, _, err := Decode(hexDec(t, h))
require.ErrorIs(t, err, io.ErrUnexpectedEOF)
})
}
// Complete items that are simply not what we accept must not look truncated,
// however short they are.
complete := map[string]string{
"bare integer, not a tag": "01",
"tag 96 array too short": "d8608340a0f6",
"duplicate protected label": "d8608445a201030104a0f6818340a041aa",
"body ciphertext not null": "d8608440a041ff818340a041aa",
}
for name, h := range complete {
t.Run(name, func(t *testing.T) {
_, _, err := Decode(hexDec(t, h))
require.NotErrorIs(t, err, io.ErrUnexpectedEOF)
})
}
}
func removeSpaces(s string) string {
out := make([]byte, 0, len(s))
for i := 0; i < len(s); i++ {
if s[i] != ' ' {
out = append(out, s[i])
}
}
return string(out)
}
func TestDecodeExpectedType(t *testing.T) {
withType := func(typ any) []byte {
t.Helper()
env := &Envelope{
Headers: Headers{Protected: Header{}.Set(HeaderLabelType, typ)},
Recipients: []*Recipient{{Ciphertext: []byte{0xAA}}},
}
b, err := env.Encode()
require.NoError(t, err)
return b
}
withoutType := func() []byte {
t.Helper()
env := &Envelope{
Headers: Headers{Protected: Header{}.Set(HeaderLabelAlg, AlgA256GCM)},
Recipients: []*Recipient{{Ciphertext: []byte{0xAA}}},
}
b, err := env.Encode()
require.NoError(t, err)
return b
}
t.Run("matching type", func(t *testing.T) {
_, _, err := Decode(withType(exampleType), WithExpectedType(exampleType))
require.NoError(t, err)
})
t.Run("wrong type", func(t *testing.T) {
_, _, err := Decode(withType("application/other"), WithExpectedType(exampleType))
require.ErrorIs(t, err, ErrUnexpectedType)
})
t.Run("missing type", func(t *testing.T) {
_, _, err := Decode(withoutType(), WithExpectedType(exampleType))
require.ErrorIs(t, err, ErrUnexpectedType)
})
t.Run("type present but not a string", func(t *testing.T) {
_, _, err := Decode(withType(int64(7)), WithExpectedType(exampleType))
require.ErrorIs(t, err, ErrUnexpectedType)
})
t.Run("no check when option omitted", func(t *testing.T) {
_, _, err := Decode(withoutType())
require.NoError(t, err)
})
}