2016-02-06 22:54:14 +01:00
|
|
|
package nametransform
|
|
|
|
|
|
|
|
import (
|
2021-10-21 09:37:04 +02:00
|
|
|
"strings"
|
2016-02-06 22:54:14 +01:00
|
|
|
"testing"
|
2021-10-21 09:37:04 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/contentenc"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/cryptocore"
|
2016-02-06 22:54:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsLongName(t *testing.T) {
|
|
|
|
n := "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=.name"
|
2016-04-10 19:32:10 +02:00
|
|
|
if NameType(n) != LongNameFilename {
|
2016-02-06 22:54:14 +01:00
|
|
|
t.Errorf("False negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
n = "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU="
|
2016-04-10 19:32:10 +02:00
|
|
|
if NameType(n) != LongNameContent {
|
2016-02-06 22:54:14 +01:00
|
|
|
t.Errorf("False negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
n = "LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU="
|
2016-04-10 19:32:10 +02:00
|
|
|
if NameType(n) != LongNameNone {
|
2016-02-06 22:54:14 +01:00
|
|
|
t.Errorf("False positive")
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
|
|
|
|
func TestRemoveLongNameSuffix(t *testing.T) {
|
|
|
|
filename := "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU=.name"
|
|
|
|
content := "gocryptfs.longname.LkwUdALvV_ANnzQN6ZZMYnxxfARD3IeZWCKnxGJjYmU="
|
|
|
|
if RemoveLongNameSuffix(filename) != content {
|
|
|
|
t.Error(".name suffix not removed")
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 09:37:04 +02:00
|
|
|
|
|
|
|
func newLognamesTestInstance(longNameMax uint8) *NameTransform {
|
|
|
|
key := make([]byte, cryptocore.KeyLen)
|
|
|
|
cCore := cryptocore.New(key, cryptocore.BackendGoGCM, contentenc.DefaultIVBits, true)
|
|
|
|
return New(cCore.EMECipher, true, longNameMax, true, nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLongNameMax(t *testing.T) {
|
|
|
|
iv := make([]byte, 16)
|
|
|
|
for max := 0; max <= NameMax; max++ {
|
|
|
|
n := newLognamesTestInstance(uint8(max))
|
|
|
|
if max == 0 {
|
|
|
|
// effective value is 255
|
|
|
|
max = NameMax
|
|
|
|
}
|
|
|
|
for l := 0; l <= NameMax+10; l++ {
|
|
|
|
name := strings.Repeat("x", l)
|
|
|
|
out, err := n.EncryptAndHashName(name, iv)
|
|
|
|
if l == 0 || l > NameMax {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("should have rejected a name of length %d, but did not", l)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cName, _ := n.EncryptName(name, iv)
|
|
|
|
rawLen := len(cName)
|
|
|
|
want := LongNameNone
|
|
|
|
if rawLen > max {
|
|
|
|
want = LongNameContent
|
|
|
|
}
|
|
|
|
have := NameType(out)
|
|
|
|
if have != want {
|
|
|
|
t.Errorf("l=%d max=%d: wanted %v, got %v\nname=%q\nout=%q", l, max, want, have, name, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|