2016-02-06 19:20:54 +01:00
|
|
|
package nametransform
|
2015-09-07 21:25:05 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-02 14:21:30 +02:00
|
|
|
"strings"
|
2015-10-04 14:36:20 +02:00
|
|
|
"testing"
|
2015-09-07 21:25:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPad16(t *testing.T) {
|
2020-04-18 16:14:48 +02:00
|
|
|
s := [][]byte{
|
|
|
|
[]byte("foo"),
|
|
|
|
[]byte("12345678901234567"),
|
|
|
|
[]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) {
|
2020-04-18 16:14:48 +02:00
|
|
|
testCases := [][]byte{
|
|
|
|
make([]byte, 0),
|
|
|
|
make([]byte, 16),
|
|
|
|
make([]byte, 1),
|
|
|
|
make([]byte, 17),
|
|
|
|
bytes.Repeat([]byte{16}, 16),
|
|
|
|
bytes.Repeat([]byte{17}, 16),
|
|
|
|
}
|
2017-05-23 20:46:24 +02:00
|
|
|
for _, v := range testCases {
|
|
|
|
_, err := unPad16([]byte(v))
|
|
|
|
if err == nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-02 14:21:30 +02:00
|
|
|
|
|
|
|
func TestIsValidName(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
in string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"", false},
|
|
|
|
{".", false},
|
|
|
|
{"..", false},
|
|
|
|
{"...", true},
|
|
|
|
{"asdasd/asdasd", false},
|
|
|
|
{"asdasd\000asdasd", false},
|
|
|
|
{"hello", true},
|
|
|
|
{strings.Repeat("x", 255), true},
|
|
|
|
{strings.Repeat("x", 256), false},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
have := IsValidName(c.in)
|
|
|
|
if (have == nil) != c.want {
|
|
|
|
t.Errorf("IsValidName(%q): want %v have %v", c.in, c.want, have)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|