tests: Add append test

This commit is contained in:
Jakob Unterwurzacher 2015-09-30 23:42:18 +02:00
parent b835f83fd5
commit 3fef613591
2 changed files with 35 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
@ -145,6 +146,38 @@ func TestTruncate(t *testing.T) {
}
}
func TestAppend(t *testing.T) {
fn := plainDir + "append"
file, err := os.Create(fn)
if err != nil {
t.FailNow()
}
data := []byte("testdata123456789") // length 17
var buf bytes.Buffer
var hashWant string
for i := 0; i <= 500; i++ {
file.Write(data)
buf.Write(data)
bin := md5.Sum(buf.Bytes())
hashWant = hex.EncodeToString(bin[:])
hashActual := md5fn(fn)
if hashWant != hashActual {
t.FailNow()
}
}
// Overwrite with the same data
// Hash must stay the same
file.Seek(0, 0)
for i := 0; i <= 500; i++ {
file.Write(data)
hashActual := md5fn(fn)
if hashWant != hashActual {
t.FailNow()
}
}
}
func BenchmarkStreamWrite(t *testing.B) {
buf := make([]byte, 1024*1024)
t.SetBytes(int64(len(buf)))

View File

@ -70,11 +70,12 @@ func (f *file) doRead(off uint64, length uint64) ([]byte, fuse.Status) {
cryptfs.Warn.Printf("read: ReadAt: %s\n", err.Error())
return nil, fuse.ToStatus(err)
}
cryptfs.Debug.Printf("ReadAt length=%d offset=%d -> n=%d len=%d\n", alignedLength, alignedOffset, n, len(ciphertext))
cryptfs.Debug.Printf("ReadAt offset=%d length=%d -> n=%d len=%d\n", alignedLength, alignedOffset, n, len(ciphertext))
// Decrypt it
plaintext, err := f.cfs.DecryptBlocks(ciphertext)
if err != nil {
cryptfs.Warn.Printf("Corrupt block at offset=%d\n", off + uint64(len(plaintext)))
cryptfs.Warn.Printf("doRead: returning IO error\n")
return nil, fuse.EIO
}