2016-02-06 19:20:54 +01:00
|
|
|
package contentenc
|
2015-09-07 21:25:05 +02:00
|
|
|
|
|
|
|
import (
|
2015-10-04 14:36:20 +02:00
|
|
|
"testing"
|
2016-02-06 20:22:45 +01:00
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/cryptocore"
|
2015-09-07 21:25:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type testRange struct {
|
|
|
|
offset uint64
|
|
|
|
length uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitRange(t *testing.T) {
|
2023-01-24 21:35:32 +01:00
|
|
|
ranges := []testRange{
|
|
|
|
{0, 70000},
|
|
|
|
{0, 10},
|
|
|
|
{234, 6511},
|
|
|
|
{65444, 54},
|
|
|
|
{0, 1024 * 1024},
|
|
|
|
{0, 65536},
|
|
|
|
{6654, 8945},
|
|
|
|
}
|
2015-09-07 21:25:05 +02:00
|
|
|
|
2016-02-06 20:22:45 +01:00
|
|
|
key := make([]byte, cryptocore.KeyLen)
|
2021-09-10 12:14:19 +02:00
|
|
|
cc := cryptocore.New(key, cryptocore.BackendGoGCM, DefaultIVBits, true)
|
|
|
|
f := New(cc, DefaultBS)
|
2015-09-07 21:25:05 +02:00
|
|
|
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, r := range ranges {
|
2015-11-01 12:11:36 +01:00
|
|
|
parts := f.ExplodePlainRange(r.offset, r.length)
|
|
|
|
var lastBlockNo uint64 = 1 << 63
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, p := range parts {
|
2015-11-01 01:32:33 +01:00
|
|
|
if p.BlockNo == lastBlockNo {
|
|
|
|
t.Errorf("Duplicate block number %d", p.BlockNo)
|
|
|
|
}
|
|
|
|
lastBlockNo = p.BlockNo
|
2016-02-06 20:22:45 +01:00
|
|
|
if p.Length > DefaultBS || p.Skip >= DefaultBS {
|
2015-11-01 01:32:33 +01:00
|
|
|
t.Errorf("Test fail: n=%d, length=%d, offset=%d\n", p.BlockNo, p.Length, p.Skip)
|
2015-09-07 21:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 22:34:42 +02:00
|
|
|
|
|
|
|
func TestCiphertextRange(t *testing.T) {
|
2023-01-24 21:35:32 +01:00
|
|
|
ranges := []testRange{
|
|
|
|
{0, 70000},
|
|
|
|
{0, 10},
|
|
|
|
{234, 6511},
|
|
|
|
{65444, 54},
|
|
|
|
{6654, 8945},
|
|
|
|
}
|
2015-09-08 22:34:42 +02:00
|
|
|
|
2016-02-06 20:22:45 +01:00
|
|
|
key := make([]byte, cryptocore.KeyLen)
|
2021-09-10 12:14:19 +02:00
|
|
|
cc := cryptocore.New(key, cryptocore.BackendGoGCM, DefaultIVBits, true)
|
|
|
|
f := New(cc, DefaultBS)
|
2015-09-08 22:34:42 +02:00
|
|
|
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, r := range ranges {
|
2015-11-01 12:11:36 +01:00
|
|
|
|
|
|
|
blocks := f.ExplodePlainRange(r.offset, r.length)
|
|
|
|
alignedOffset, alignedLength := blocks[0].JointCiphertextRange(blocks)
|
|
|
|
skipBytes := blocks[0].Skip
|
|
|
|
|
2015-09-08 22:34:42 +02:00
|
|
|
if alignedLength < r.length {
|
2015-11-14 17:16:17 +01:00
|
|
|
t.Errorf("alignedLength=%d is smaller than length=%d", alignedLength, r.length)
|
2015-09-08 22:34:42 +02:00
|
|
|
}
|
2016-10-02 06:14:18 +02:00
|
|
|
if (alignedOffset-HeaderLen)%f.cipherBS != 0 {
|
2015-11-01 01:32:33 +01:00
|
|
|
t.Errorf("alignedOffset=%d is not aligned", alignedOffset)
|
2015-09-08 22:34:42 +02:00
|
|
|
}
|
2015-10-04 14:36:20 +02:00
|
|
|
if r.offset%f.plainBS != 0 && skipBytes == 0 {
|
2015-11-01 01:32:33 +01:00
|
|
|
t.Errorf("skipBytes=0")
|
2015-09-08 22:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 11:03:40 +02:00
|
|
|
|
|
|
|
func TestBlockNo(t *testing.T) {
|
2016-02-06 20:22:45 +01:00
|
|
|
key := make([]byte, cryptocore.KeyLen)
|
2021-09-10 12:14:19 +02:00
|
|
|
cc := cryptocore.New(key, cryptocore.BackendGoGCM, DefaultIVBits, true)
|
|
|
|
f := New(cc, DefaultBS)
|
2015-10-04 11:03:40 +02:00
|
|
|
|
2015-11-01 12:11:36 +01:00
|
|
|
b := f.CipherOffToBlockNo(788)
|
2015-10-04 11:03:40 +02:00
|
|
|
if b != 0 {
|
|
|
|
t.Errorf("actual: %d", b)
|
|
|
|
}
|
2016-10-02 06:14:18 +02:00
|
|
|
b = f.CipherOffToBlockNo(HeaderLen + f.cipherBS)
|
2015-10-04 11:03:40 +02:00
|
|
|
if b != 1 {
|
|
|
|
t.Errorf("actual: %d", b)
|
|
|
|
}
|
2015-11-01 12:11:36 +01:00
|
|
|
b = f.PlainOffToBlockNo(788)
|
2015-10-04 11:03:40 +02:00
|
|
|
if b != 0 {
|
|
|
|
t.Errorf("actual: %d", b)
|
|
|
|
}
|
2015-11-01 12:11:36 +01:00
|
|
|
b = f.PlainOffToBlockNo(f.plainBS)
|
2015-10-04 11:03:40 +02:00
|
|
|
if b != 1 {
|
|
|
|
t.Errorf("actual: %d", b)
|
|
|
|
}
|
|
|
|
}
|