-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpkcs7_test.go
121 lines (114 loc) · 2.49 KB
/
pkcs7_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
package pkcs7
import (
"bytes"
"strings"
"testing"
)
type testVector struct {
blockSize int
input []byte
output []byte
errorString string
}
var padTests = []testVector{
// Pads buffers.
{
16,
[]byte{
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
0xDE, 0xAD, 0xBE, 0xEF,
},
[]byte{
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
0xDE, 0xAD, 0xBE, 0xEF, 0x04, 0x04, 0x04, 0x04,
},
"",
},
// Pads empty buffers.
{
16,
[]byte{},
[]byte{
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
},
"",
},
// Pads buffers larger than the block size.
{
16,
[]byte{
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
0xDE, 0xAD, 0xBE, 0xEF,
},
[]byte{
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
0xDE, 0xAD, 0xBE, 0xEF, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
},
"",
},
// Error when block size is more than 255
{
256,
[]byte{0x01, 0x02, 0x03, 0x04},
nil,
"pkcs7: block size must be between 1 and 255 inclusive",
},
// (Pad only) Error when block size is zero
{
0,
[]byte{0x01, 0x02, 0x03, 0x04},
nil,
"pkcs7: block size must be between 1 and 255 inclusive",
},
// (Unpad only) Error when padded block has a final zero bit
{
4,
nil,
[]byte{0x01, 0x02, 0x03, 0x00},
"pkcs7: invalid padding",
},
}
func TestPad(t *testing.T) {
for i, v := range padTests {
if v.input != nil {
o, err := Pad(v.input, v.blockSize)
if err != nil {
if v.errorString == "" {
t.Errorf("Padding caused error: %v", err)
} else if !strings.Contains(err.Error(), v.errorString) {
t.Errorf("Unexpected error: we expected %s but we received %v", v.errorString, err)
return
}
}
if v.output != nil {
if !bytes.Equal(o, v.output) {
t.Errorf("Pad %d: expected %x, got %x", i, v.output, o)
}
}
}
}
}
func TestUnpad(t *testing.T) {
for i, v := range padTests {
if v.output != nil {
o, err := Unpad(v.output)
if err != nil {
if v.errorString == "" {
t.Errorf("Padding caused error: %v", err)
} else if !strings.Contains(err.Error(), v.errorString) {
t.Errorf("Unexpected error: we expected %s but we received %v", v.errorString, err)
return
}
}
if v.input != nil {
if !bytes.Equal(o, v.input) {
t.Errorf("Unpad %d: expected %x, got %x", i, v.output, o)
}
}
}
}
}