aboutsummaryrefslogtreecommitdiff
path: root/cryptfs/content_test.go
blob: ab48b213a47b91a0a0071fad4a3383c3b54907d3 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cryptfs

import (
	"fmt"
	"testing"
)

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

	key := make([]byte, 16)
	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.Skip >= DEFAULT_PLAINBS {
				fmt.Printf("Test fail: n=%d, length=%d, offset=%d\n", p.BlockNo, p.Length, p.Skip)
				t.Fail()
			}
		}
	}
}

func TestCiphertextRange(t *testing.T) {
	var ranges []testRange

	ranges = append(ranges, testRange{0, 70000},
		testRange{0, 10},
		testRange{234, 6511},
		testRange{65444, 54},
		testRange{6654, 8945})

	key := make([]byte, 16)
	f := NewCryptFS(key, true)

	for _, r := range ranges {
		alignedOffset, alignedLength, skipBytes := f.CiphertextRange(r.offset, r.length)
		if alignedLength < r.length {
			t.Fail()
		}
		if alignedOffset%f.cipherBS != 0 {
			t.Fail()
		}
		if r.offset%f.plainBS != 0 && skipBytes == 0 {
			t.Fail()
		}
	}
}

func TestBlockNo(t *testing.T) {
	key := make([]byte, 16)
	f := NewCryptFS(key, true)

	b := f.BlockNoCipherOff(788)
	if b != 0 {
		t.Errorf("actual: %d", b)
	}
	b = f.BlockNoCipherOff(f.CipherBS())
	if b != 1 {
		t.Errorf("actual: %d", b)
	}
	b = f.BlockNoPlainOff(788)
	if b != 0 {
		t.Errorf("actual: %d", b)
	}
	b = f.BlockNoPlainOff(f.PlainBS())
	if b != 1 {
		t.Errorf("actual: %d", b)
	}
}