2015-09-07 21:25:05 +02:00
|
|
|
package cryptfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-10-04 14:36:20 +02:00
|
|
|
"testing"
|
2015-09-07 21:25:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTranslatePath(t *testing.T) {
|
|
|
|
var s []string
|
|
|
|
s = append(s, "foo")
|
|
|
|
s = append(s, "foo12312312312312312313123123123")
|
|
|
|
s = append(s, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
|
|
|
|
2015-10-07 22:08:30 +02:00
|
|
|
key := make([]byte, KEY_LEN)
|
2015-11-03 18:19:19 +01:00
|
|
|
fs := NewCryptFS(key, true, false)
|
2015-09-07 21:25:05 +02:00
|
|
|
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, n := range s {
|
2015-11-27 00:03:10 +01:00
|
|
|
c, err := fs.TranslatePathZeroIV(n, OpEncrypt)
|
|
|
|
d, err := fs.TranslatePathZeroIV(c, OpDecrypt)
|
2015-09-07 21:25:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error from DecryptName: %s", err)
|
|
|
|
}
|
|
|
|
if d != n {
|
|
|
|
t.Errorf("Content mismatch, n=\"%s\" d=\"%s\"", n, d)
|
|
|
|
}
|
|
|
|
//fmt.Printf("n=%s c=%s d=%s\n", n, c, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07 22:08:30 +02:00
|
|
|
key := make([]byte, KEY_LEN)
|
2015-11-03 18:19:19 +01:00
|
|
|
fs := NewCryptFS(key, true, false)
|
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]
|
|
|
|
padded := fs.pad16(orig)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
unpadded, err := fs.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))
|
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|