libgocryptfs/internal/nametransform/names_test.go
Jakob Unterwurzacher 04858ddd22 nametransform: check name validity on encryption
xfstests generic/523 discovered that we allowed to set
xattrs with "/" in the name, but did not allow to read
them later.

With this change we do not allow to set them in the first
place.
2021-06-02 14:29:48 +02:00

78 lines
1.6 KiB
Go

package nametransform
import (
"bytes"
"strings"
"testing"
)
func TestPad16(t *testing.T) {
s := [][]byte{
[]byte("foo"),
[]byte("12345678901234567"),
[]byte("12345678901234567abcdefg"),
}
for i := range s {
orig := s[i]
padded := pad16(orig)
if len(padded) <= len(orig) {
t.Errorf("Padded length not bigger than orig: %d", len(padded))
}
if len(padded)%16 != 0 {
t.Errorf("Length is not aligend: %d", len(padded))
}
unpadded, err := unPad16(padded)
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))
}
if !bytes.Equal(orig, unpadded) {
t.Error("Content mismatch orig vs unpadded")
}
}
}
// TestUnpad16Garbage - unPad16 should never crash on corrupt or malicious inputs
func TestUnpad16Garbage(t *testing.T) {
testCases := [][]byte{
make([]byte, 0),
make([]byte, 16),
make([]byte, 1),
make([]byte, 17),
bytes.Repeat([]byte{16}, 16),
bytes.Repeat([]byte{17}, 16),
}
for _, v := range testCases {
_, err := unPad16([]byte(v))
if err == nil {
t.Fail()
}
}
}
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)
}
}
}