2016-02-06 19:20:54 +01:00
|
|
|
package nametransform
|
2015-09-07 21:25:05 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-10-04 14:36:20 +02:00
|
|
|
"testing"
|
2015-09-07 21:25:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPad16(t *testing.T) {
|
|
|
|
var s [][]byte
|
|
|
|
s = append(s, []byte("foo"))
|
|
|
|
s = append(s, []byte("12345678901234567"))
|
|
|
|
s = append(s, []byte("12345678901234567abcdefg"))
|
2015-10-04 11:03:40 +02:00
|
|
|
|
2015-10-04 14:36:20 +02:00
|
|
|
for i := range s {
|
2015-09-07 21:25:05 +02:00
|
|
|
orig := s[i]
|
2016-02-06 20:22:45 +01:00
|
|
|
padded := pad16(orig)
|
2015-09-07 21:25:05 +02:00
|
|
|
if len(padded) <= len(orig) {
|
|
|
|
t.Errorf("Padded length not bigger than orig: %d", len(padded))
|
|
|
|
}
|
2015-10-04 14:36:20 +02:00
|
|
|
if len(padded)%16 != 0 {
|
2015-09-07 21:25:05 +02:00
|
|
|
t.Errorf("Length is not aligend: %d", len(padded))
|
|
|
|
}
|
2016-02-06 20:22:45 +01:00
|
|
|
unpadded, err := unPad16(padded)
|
2015-09-07 21:25:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("unPad16 returned error:", err)
|
|
|
|
}
|
|
|
|
if len(unpadded) != len(orig) {
|
|
|
|
t.Errorf("Size mismatch: orig=%d unpadded=%d", len(s[i]), len(unpadded))
|
|
|
|
}
|
2015-10-04 14:36:20 +02:00
|
|
|
if !bytes.Equal(orig, unpadded) {
|
2015-09-07 21:25:05 +02:00
|
|
|
t.Error("Content mismatch orig vs unpadded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 20:46:24 +02:00
|
|
|
|
|
|
|
// TestUnpad16Garbage - unPad16 should never crash on corrupt or malicious inputs
|
|
|
|
func TestUnpad16Garbage(t *testing.T) {
|
|
|
|
var testCases [][]byte
|
|
|
|
testCases = append(testCases, make([]byte, 0))
|
|
|
|
testCases = append(testCases, make([]byte, 16))
|
|
|
|
testCases = append(testCases, make([]byte, 1))
|
|
|
|
testCases = append(testCases, make([]byte, 17))
|
|
|
|
testCases = append(testCases, bytes.Repeat([]byte{16}, 16))
|
|
|
|
testCases = append(testCases, bytes.Repeat([]byte{17}, 16))
|
|
|
|
for _, v := range testCases {
|
|
|
|
_, err := unPad16([]byte(v))
|
|
|
|
if err == nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|