tests: add TestCiphertextRange
This commit is contained in:
parent
b13cc9686c
commit
28cdff5889
@ -32,3 +32,29 @@ func TestSplitRange(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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})
|
||||
|
||||
var key [16]byte
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -115,7 +115,7 @@ func (be *CryptFS) minu64(x uint64, y uint64) uint64 {
|
||||
|
||||
// CiphertextRange - Get byte range in backing ciphertext corresponding
|
||||
// to plaintext range. Returns a range aligned to ciphertext blocks.
|
||||
func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (uint64, uint64, int) {
|
||||
func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (alignedOffset uint64, alignedLength uint64, skipBytes int) {
|
||||
// Decrypting the ciphertext will yield too many plaintext bytes. Skip this number
|
||||
// of bytes from the front.
|
||||
skip := offset % be.plainBS
|
||||
@ -123,10 +123,11 @@ func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (uint64, uint64
|
||||
firstBlockNo := offset / be.plainBS
|
||||
lastBlockNo := ( offset + length - 1 ) / be.plainBS
|
||||
|
||||
alignedOffset := firstBlockNo * be.cipherBS
|
||||
alignedLength := (lastBlockNo - firstBlockNo + 1) * be.cipherBS
|
||||
alignedOffset = firstBlockNo * be.cipherBS
|
||||
alignedLength = (lastBlockNo - firstBlockNo + 1) * be.cipherBS
|
||||
|
||||
return alignedOffset, alignedLength, int(skip)
|
||||
skipBytes = int(skip)
|
||||
return alignedOffset, alignedLength, skipBytes
|
||||
}
|
||||
|
||||
// Get the byte range in the ciphertext corresponding to blocks
|
||||
|
@ -129,7 +129,7 @@ func (f *file) Write(data []byte, off int64) (uint32, fuse.Status) {
|
||||
}
|
||||
// Modify
|
||||
blockData = f.cfs.MergeBlocks(oldData, blockData, int(b.Offset))
|
||||
cryptfs.Debug.Printf("oldData=%d blockData=%d\n", len(oldData), len(blockData))
|
||||
cryptfs.Debug.Printf("len(oldData)=%d len(blockData)=%d\n", len(oldData), len(blockData))
|
||||
}
|
||||
// Write
|
||||
blockOffset, _ := b.CiphertextRange()
|
||||
|
Loading…
Reference in New Issue
Block a user