From 28cdff5889927fcf8d720f13fcfe139720906988 Mon Sep 17 00:00:00 2001
From: Jakob Unterwurzacher
Date: Tue, 8 Sep 2015 22:34:42 +0200
Subject: tests: add TestCiphertextRange

---
 cryptfs/content_test.go    | 26 ++++++++++++++++++++++++++
 cryptfs/cryptfs_content.go |  9 +++++----
 pathfs_frontend/file.go    |  2 +-
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/cryptfs/content_test.go b/cryptfs/content_test.go
index 1900818..b53eeeb 100644
--- a/cryptfs/content_test.go
+++ b/cryptfs/content_test.go
@@ -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()
+		}
+	}
+}
diff --git a/cryptfs/cryptfs_content.go b/cryptfs/cryptfs_content.go
index e23d9b5..b9dd61f 100644
--- a/cryptfs/cryptfs_content.go
+++ b/cryptfs/cryptfs_content.go
@@ -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
diff --git a/pathfs_frontend/file.go b/pathfs_frontend/file.go
index 6e7b4a1..47c2ffc 100644
--- a/pathfs_frontend/file.go
+++ b/pathfs_frontend/file.go
@@ -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()
-- 
cgit v1.2.3