github.com/msales/pkg/v3@v3.24.0/cryptox/padding_test.go (about) 1 package cryptox_test 2 3 import ( 4 "testing" 5 6 "github.com/msales/pkg/v3/cryptox" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestPKCS7Pad(t *testing.T) { 11 tests := []struct { 12 name string 13 blocksize int 14 in []byte 15 want []byte 16 wantErr bool 17 }{ 18 { 19 name: "input fully divisible by the blocksize", 20 blocksize: 4, 21 in: []byte{'t', 'e', 's', 't'}, 22 want: []byte{'t', 'e', 's', 't', 0x4, 0x4, 0x4, 0x4}, 23 }, 24 { 25 name: "input less than the blocksize", 26 blocksize: 4, 27 in: []byte{'t', 'e'}, 28 want: []byte{'t', 'e', 0x2, 0x2}, 29 }, 30 { 31 name: "input more than the blocksize", 32 blocksize: 4, 33 in: []byte{'t', 'e', 's', 't', 's'}, 34 want: []byte{'t', 'e', 's', 't', 's', 0x3, 0x3, 0x3}, 35 }, 36 { 37 name: "0 blocksize", 38 blocksize: 0, 39 in: []byte{'t', 'e', 's', 't'}, 40 want: nil, 41 wantErr: true, 42 }, 43 { 44 name: "negative blocksize", 45 blocksize: -1, 46 in: []byte{'t', 'e', 's', 't'}, 47 want: nil, 48 wantErr: true, 49 }, 50 { 51 name: "empty input", 52 blocksize: 4, 53 in: []byte{}, 54 want: nil, 55 wantErr: true, 56 }, 57 { 58 name: "nil input", 59 blocksize: 4, 60 in: nil, 61 want: nil, 62 wantErr: true, 63 }, 64 } 65 66 for _, tt := range tests { 67 t.Run(tt.name, func(t *testing.T) { 68 out, err := cryptox.PKCS7Pad(tt.in, tt.blocksize) 69 70 assert.Equal(t, tt.want, out) 71 if tt.wantErr { 72 assert.Error(t, err) 73 } else { 74 assert.NoError(t, err) 75 } 76 }) 77 } 78 } 79 80 func TestPKCS7Unpad(t *testing.T) { 81 tests := []struct { 82 name string 83 blocksize int 84 in []byte 85 want []byte 86 wantErr bool 87 }{ 88 { 89 name: "full block to unpad", 90 blocksize: 4, 91 in: []byte{'t', 'e', 's', 't', 0x4, 0x4, 0x4, 0x4}, 92 want: []byte{'t', 'e', 's', 't'}, 93 }, 94 { 95 name: "part of block to unpad", 96 blocksize: 4, 97 in: []byte{'t', 'e', 0x2, 0x2}, 98 want: []byte{'t', 'e'}, 99 }, 100 { 101 name: "0 blocksize", 102 blocksize: 0, 103 in: []byte{'t', 'e', 's', 't'}, 104 want: nil, 105 wantErr: true, 106 }, 107 { 108 name: "negative blocksize", 109 blocksize: -1, 110 in: []byte{'t', 'e', 's', 't'}, 111 want: nil, 112 wantErr: true, 113 }, 114 { 115 name: "empty input", 116 blocksize: 4, 117 in: []byte{}, 118 want: nil, 119 wantErr: true, 120 }, 121 { 122 name: "empty input", 123 blocksize: 4, 124 in: nil, 125 want: nil, 126 wantErr: true, 127 }, 128 { 129 name: "input not fully divisible by the blocksize", 130 blocksize: 4, 131 in: []byte{'t', 'e', 's', 't', 's'}, 132 want: nil, 133 wantErr: true, 134 }, 135 { 136 name: "zero padding byte", 137 blocksize: 5, 138 in: []byte{'t', 'e', 's', 't', 0x0}, 139 want: nil, 140 wantErr: true, 141 }, 142 { 143 name: "zero padding byte greater than input length", 144 blocksize: 5, 145 in: []byte{'t', 'e', 's', 't', 0x6}, 146 want: nil, 147 wantErr: true, 148 }, 149 { 150 name: "no padding byte at the end", 151 blocksize: 4, 152 in: []byte{'t', 'e', 's', 't'}, 153 want: nil, 154 wantErr: true, 155 }, 156 { 157 name: "non-identical padding bytes", 158 blocksize: 4, 159 in: []byte{'t', 'e', 0x3, 0x4}, 160 want: nil, 161 wantErr: true, 162 }, 163 } 164 165 for _, tt := range tests { 166 t.Run(tt.name, func(t *testing.T) { 167 out, err := cryptox.PKCS7Unpad(tt.in, tt.blocksize) 168 169 assert.Equal(t, tt.want, out) 170 if tt.wantErr { 171 assert.Error(t, err) 172 } else { 173 assert.NoError(t, err) 174 } 175 }) 176 } 177 } 178 179 func BenchmarkPKCS7Pad(b *testing.B) { 180 var data = []byte("12345678") 181 182 b.ReportAllocs() 183 184 for i := 0; i < b.N; i++ { 185 _, _ = cryptox.PKCS7Pad(data, 4) 186 } 187 } 188 189 func BenchmarkPKCS7Unpad(b *testing.B) { 190 var data = []byte{'1', '2', '3', '4', '5', '6', '7', '8', 0x4, 0x4, 0x4, 0x4} 191 192 b.ReportAllocs() 193 194 for i := 0; i < b.N; i++ { 195 _, _ = cryptox.PKCS7Unpad(data, 4) 196 } 197 }