-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathpadding_test.go
More file actions
207 lines (195 loc) · 4.53 KB
/
padding_test.go
File metadata and controls
207 lines (195 loc) · 4.53 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package openssl
import (
"testing"
"github.com/stretchr/testify/assert"
)
const TestBlockSize = 16
func runTestCases(t *testing.T, testCases []struct {
name string
args struct{ src []byte }
want []byte
wantErr error
}, testFunc func([]byte) ([]byte, error)) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
result, err := testFunc(tt.args.src)
assert.Equal(t, tt.wantErr, err)
assert.Equal(t, tt.want, result)
})
}
}
func TestPKCS7UnPadding(t *testing.T) {
type args struct {
src []byte
}
testCases := []struct {
name string
args args
want []byte
wantErr error
}{
{
name: "len(src)==0",
args: args{src: []byte{}},
want: []byte{},
wantErr: ErrUnPadding,
},
{
name: `src=="121"`,
args: args{src: []byte{1, 2, 1}},
want: []byte{1, 2},
wantErr: nil,
},
{
name: `src=="12111"`,
args: args{src: []byte{1, 2, 1, 1, 9}},
want: []byte{1, 2, 1, 1, 9},
wantErr: ErrUnPadding,
},
}
// Convert testCases to match the expected type of runTestCases
convertedTestCases := make([]struct {
name string
args struct{ src []byte }
want []byte
wantErr error
}, len(testCases))
for i, tc := range testCases {
convertedTestCases[i] = struct {
name string
args struct{ src []byte }
want []byte
wantErr error
}{
name: tc.name,
args: struct{ src []byte }{src: tc.args.src},
want: tc.want,
wantErr: tc.wantErr,
}
}
runTestCases(t, convertedTestCases, PKCS7UnPadding)
}
func TestPadding(t *testing.T) {
tests := []struct {
name string
padding string
src []byte
wantLen int
}{
{name: "PKCS5", padding: PKCS5_PADDING, src: []byte("test"), wantLen: TestBlockSize},
{name: "PKCS7", padding: PKCS7_PADDING, src: []byte("test"), wantLen: TestBlockSize},
{name: "Zeros", padding: ZEROS_PADDING, src: []byte("test"), wantLen: TestBlockSize},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Padding(tt.padding, tt.src, TestBlockSize)
assert.Equal(t, tt.wantLen, len(got))
})
}
}
func TestUnPadding(t *testing.T) {
padding := PKCS7_PADDING
tests := []struct {
name string
src []byte
want []byte
wantErr bool
}{
{name: "valid padding", src: PKCS7Padding([]byte("test"), TestBlockSize), want: []byte("test"), wantErr: false},
{name: "empty src", src: []byte{}, want: []byte{}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UnPadding(padding, tt.src)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}
func TestPKCS5Padding(t *testing.T) {
tests := []struct {
name string
src []byte
wantLen int
}{
{name: "test PKCS5", src: []byte("test"), wantLen: TestBlockSize},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := PKCS5Padding(tt.src, TestBlockSize)
assert.Equal(t, tt.wantLen, len(got))
})
}
}
func TestPKCS5Unpadding(t *testing.T) {
padded := PKCS5Padding([]byte("test"), TestBlockSize)
tests := []struct {
name string
src []byte
want []byte
wantErr bool
}{
{name: "valid padding", src: padded, want: []byte("test"), wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := PKCS5Unpadding(tt.src)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}
func TestPKCS7Padding(t *testing.T) {
tests := []struct {
name string
src []byte
wantLen int
}{
{name: "test PKCS7", src: []byte("test"), wantLen: TestBlockSize},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := PKCS7Padding(tt.src, TestBlockSize)
assert.Equal(t, tt.wantLen, len(got))
})
}
}
func TestZerosPadding(t *testing.T) {
tests := []struct {
name string
src []byte
wantLen int
}{
{name: "test Zeros", src: []byte("test"), wantLen: TestBlockSize},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ZerosPadding(tt.src, TestBlockSize)
assert.Equal(t, tt.wantLen, len(got))
})
}
}
func TestZerosUnPadding(t *testing.T) {
tests := []struct {
name string
src []byte
want []byte
}{
{name: "test ZerosUnPadding", src: ZerosPadding([]byte("test"), TestBlockSize), want: []byte("test")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ZerosUnPadding(tt.src)
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}