-
Notifications
You must be signed in to change notification settings - Fork 0
/
pad_test.go
51 lines (45 loc) · 1.43 KB
/
pad_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
package crypka_test
import (
"bytes"
"fmt"
"testing"
"github.com/teawithsand/crypka"
)
func TestCanPadMessages(t *testing.T) {
assert := func(in, out []byte, sz int) {
res := crypka.PaddingIEC78164().Pad(in, sz)
if !bytes.Equal(res, out) {
t.Error(fmt.Sprintf(
"Invalid test case found:\nGot: %x\nExpected: %x\nSize: %d", res, out, sz,
))
}
}
assert([]byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee}, []byte{0xaa, 0xbb, 0xcc, 0x80, 0x00}, 3)
assert([]byte{0xaa, 0xbb, 0xcc, 0xdd}, []byte{0xaa, 0xbb, 0xcc, 0x80}, 3)
assert([]byte{0xaa, 0xbb, 0xcc, 0xdd}, []byte{0x80, 0x00, 0x00, 0x00}, 0)
assert([]byte{0xaa, 0xbb, 0xcc, 0xdd}, []byte{0xaa, 0xbb, 0xcc, 0xdd}, 4)
assert([]byte{0xaa, 0xbb, 0xcc, 0xdd}, []byte{0xaa, 0xbb, 0xcc, 0xdd}, 10)
}
func TestCanUnpadMessages(t *testing.T) {
assert := func(in []byte, outIdx int) {
idx := crypka.PaddingIEC78164().Unpad(in)
if idx < 0 && outIdx >= 0 {
t.Error(fmt.Sprintf(
"Expected `%x` to be invalid\n", in,
))
} else if idx != outIdx {
t.Error(fmt.Sprintf(
"Expected `%x` to yield %d\nYielded: %d\n", in, outIdx, idx,
))
}
}
assert([]byte{0xaa, 0xbb, 0xcc, 0x80, 0x00}, 3)
assert([]byte{0xaa, 0xbb, 0xcc, 0x80}, 3)
assert([]byte{0xaa, 0xbb, 0xcc, 0x80, 0x00, 0x00}, 3)
assert([]byte{0x80, 0x00}, 0)
assert([]byte{0x80, 0x00, 0x00}, 0)
assert([]byte{0x80, 0x00, 0x00, 0x00}, 0)
assert([]byte{}, -1)
assert([]byte{0xaa}, -1)
assert([]byte{0xaa, 0xaa, 0xbb}, -1)
}