aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-09-07 21:25:05 +0200
committerJakob Unterwurzacher2015-09-07 21:25:05 +0200
commitb65882985dd7aa3ea7ae76c49d592ff30b353a8b (patch)
treeb66ef37e2b7ef015dfa6f0e96be7e10633d596e9
parentaf0ae03db23f845dbcf8a6738057222ce7986bd8 (diff)
Add tests for name encryption and byte range splitting
-rw-r--r--cryptfs/content_test.go34
-rw-r--r--cryptfs/names_test.go57
2 files changed, 91 insertions, 0 deletions
diff --git a/cryptfs/content_test.go b/cryptfs/content_test.go
new file mode 100644
index 0000000..1900818
--- /dev/null
+++ b/cryptfs/content_test.go
@@ -0,0 +1,34 @@
+package cryptfs
+
+import (
+ "testing"
+ "fmt"
+)
+
+type testRange struct {
+ offset uint64
+ length uint64
+}
+
+func TestSplitRange(t *testing.T) {
+ var ranges []testRange
+
+ ranges = append(ranges, testRange{0, 70000},
+ testRange{0, 10},
+ testRange{234, 6511},
+ testRange{65444, 54},
+ testRange{6654, 8945})
+
+ var key [16]byte
+ f := NewCryptFS(key, true)
+
+ for _, r := range(ranges) {
+ parts := f.SplitRange(r.offset, r.length)
+ for _, p := range(parts) {
+ if p.Length > DEFAULT_PLAINBS || p.Offset >= DEFAULT_PLAINBS {
+ fmt.Printf("Test fail: n=%d, length=%d, offset=%d\n", p.BlockNo, p.Length, p.Offset)
+ t.Fail()
+ }
+ }
+ }
+}
diff --git a/cryptfs/names_test.go b/cryptfs/names_test.go
new file mode 100644
index 0000000..30ecfb9
--- /dev/null
+++ b/cryptfs/names_test.go
@@ -0,0 +1,57 @@
+package cryptfs
+
+import (
+ "testing"
+ "bytes"
+)
+
+func TestTranslatePath(t *testing.T) {
+ var s []string
+ s = append(s, "foo")
+ s = append(s, "foo12312312312312312313123123123")
+ s = append(s, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890")
+
+ var key [16]byte
+ fs := NewCryptFS(key, true)
+
+ for _, n := range(s) {
+ c := fs.EncryptPath(n)
+ d, err := fs.DecryptPath(c)
+ 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"))
+ var key [16]byte
+ fs := NewCryptFS(key, true)
+ for i := range(s) {
+ orig := s[i]
+ padded := fs.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 := 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))
+ }
+ if ! bytes.Equal(orig, unpadded) {
+ t.Error("Content mismatch orig vs unpadded")
+ }
+ }
+}