2015-09-07 21:25:05 +02:00
|
|
|
package cryptfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-04 14:36:20 +02:00
|
|
|
"testing"
|
2015-09-07 21:25:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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})
|
|
|
|
|
2015-10-04 11:03:40 +02:00
|
|
|
key := make([]byte, 16)
|
2015-09-07 21:25:05 +02:00
|
|
|
f := NewCryptFS(key, true)
|
|
|
|
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, r := range ranges {
|
2015-09-07 21:25:05 +02:00
|
|
|
parts := f.SplitRange(r.offset, r.length)
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, p := range parts {
|
2015-10-04 14:24:43 +02:00
|
|
|
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)
|
2015-09-07 21:25:05 +02:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 22:34:42 +02:00
|
|
|
|
|
|
|
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})
|
|
|
|
|
2015-10-04 11:03:40 +02:00
|
|
|
key := make([]byte, 16)
|
2015-09-08 22:34:42 +02:00
|
|
|
f := NewCryptFS(key, true)
|
|
|
|
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, r := range ranges {
|
2015-09-08 22:34:42 +02:00
|
|
|
alignedOffset, alignedLength, skipBytes := f.CiphertextRange(r.offset, r.length)
|
|
|
|
if alignedLength < r.length {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2015-10-04 14:36:20 +02:00
|
|
|
if alignedOffset%f.cipherBS != 0 {
|
2015-09-08 22:34:42 +02:00
|
|
|
t.Fail()
|
|
|
|
}
|
2015-10-04 14:36:20 +02:00
|
|
|
if r.offset%f.plainBS != 0 && skipBytes == 0 {
|
2015-09-08 22:34:42 +02:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 11:03:40 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|