-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadding_test.go
91 lines (83 loc) · 2.09 KB
/
padding_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
package criptus
import (
"bytes"
"testing"
)
func TestPKCS7PaddingAndUnpadding(t *testing.T) {
testCases := []struct {
name string
input []byte
blockSize int
wantPadded []byte
}{
{
name: "Exact Block Size",
input: []byte("1234567890123456"),
blockSize: 16,
wantPadded: append([]byte("1234567890123456"), bytes.Repeat([]byte{byte(16)}, 16)...),
},
{
name: "Needs Padding",
input: []byte("12345"),
blockSize: 8,
wantPadded: append([]byte("12345"), bytes.Repeat([]byte{byte(3)}, 3)...),
},
{
name: "Empty Input",
input: []byte(""),
blockSize: 8,
wantPadded: bytes.Repeat([]byte{byte(8)}, 8),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
padded := pkcs7Padding(tc.input, tc.blockSize)
if !bytes.Equal(padded, tc.wantPadded) {
t.Errorf("pkcs7Padding() = %v, want %v", padded, tc.wantPadded)
}
unpadded := pkcs7UnPadding(padded)
if !bytes.Equal(unpadded, tc.input) {
t.Errorf("pkcs7UnPadding() = %v, want %v", unpadded, tc.input)
}
})
}
}
func TestPKCS5PaddingAndUnpadding(t *testing.T) {
testCases := []struct {
name string
input []byte
blockSize int
wantPadded []byte
}{
{
name: "Exact Block Size",
input: []byte("12345678"),
blockSize: 8,
wantPadded: append([]byte("12345678"), bytes.Repeat([]byte{byte(8)}, 8)...),
},
{
name: "Needs Padding",
input: []byte("12345"),
blockSize: 8,
wantPadded: append([]byte("12345"), bytes.Repeat([]byte{byte(3)}, 3)...),
},
{
name: "Empty Input",
input: []byte(""),
blockSize: 8,
wantPadded: bytes.Repeat([]byte{byte(8)}, 8),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
padded := pkcs5Padding(tc.input, tc.blockSize)
if !bytes.Equal(padded, tc.wantPadded) {
t.Errorf("pkcs5Padding() = %v, want %v", padded, tc.wantPadded)
}
unpadded := pkcs5UnPadding(padded)
if !bytes.Equal(unpadded, tc.input) {
t.Errorf("pkcs5UnPadding() = %v, want %v", unpadded, tc.input)
}
})
}
}