summaryrefslogtreecommitdiff
path: root/internal/nametransform/names_test.go
blob: f996860fc9f1486123e826eba7b9e69d8460c705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package nametransform

import (
	"bytes"
	"testing"

	"github.com/rfjakob/gocryptfs/internal/cryptocore"
)

func TestEncryptPathNoIV(t *testing.T) {
	var s []string
	s = append(s, "foo")
	s = append(s, "foo12312312312312312313123123123")
	s = append(s, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890")

	key := make([]byte, cryptocore.KeyLen)
	cc := cryptocore.New(key, true, true)
	fs := New(cc, true, false)

	for _, n := range s {
		c := fs.EncryptPathNoIV(n)
		d, err := fs.DecryptPathNoIV(c)
		if err != nil {
			t.Errorf("Got error from DecryptPathNoIV: %s", err)
		}
		if d != n {
			t.Errorf("Content mismatch, n != d: n=%s c=%s d=%s", 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"))

	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")
		}
	}
}