--- /dev/null
+package main
+
+import (
+ "bytes"
+ "testing"
+)
+
+// A TestableVolumeManagerFactory creates a volume manager with at least two TestableVolume instances.
+// The factory function, and the TestableVolume instances it returns, can use "t" to write
+// logs, fail the current test, etc.
+type TestableVolumeManagerFactory func(t *testing.T) (*RRVolumeManager, []TestableVolume)
+
+// DoHandlersWithGenericVolumeTests runs a set of handler tests with a
+// Volume Manager comprised of TestableVolume instances.
+// It calls factory to create a volume manager with TestableVolume
+// instances for each test case, to avoid leaking state between tests.
+func DoHandlersWithGenericVolumeTests(t *testing.T, factory TestableVolumeManagerFactory) {
+ testGetBlock(t, factory, TestHash, TestBlock)
+ testGetBlock(t, factory, EmptyHash, EmptyBlock)
+ testPutRawBadDataGetBlock(t, factory, TestHash, TestBlock, []byte("baddata"))
+ testPutRawBadDataGetBlock(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
+ testPutBlock(t, factory, TestHash, TestBlock)
+ testPutBlock(t, factory, EmptyHash, EmptyBlock)
+ testPutBlockCorrupt(t, factory, TestHash, TestBlock, []byte("baddata"))
+ testPutBlockCorrupt(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
+}
+
+// Setup RRVolumeManager with TestableVolumes
+func setupHandlersWithGenericVolumeTest(t *testing.T, factory TestableVolumeManagerFactory) []TestableVolume {
+ vm, testableVolumes := factory(t)
+ KeepVM = vm
+
+ for _, v := range testableVolumes {
+ defer v.Teardown()
+ }
+ defer KeepVM.Close()
+
+ return testableVolumes
+}
+
+// Put a block using PutRaw in just one volume and Get it using GetBlock
+func testGetBlock(t *testing.T, factory TestableVolumeManagerFactory, testHash string, testBlock []byte) {
+ testableVolumes := setupHandlersWithGenericVolumeTest(t, factory)
+
+ // Put testBlock in one volume
+ testableVolumes[1].PutRaw(testHash, testBlock)
+
+ // Get should pass
+ buf, err := GetBlock(testHash)
+ if err != nil {
+ t.Fatalf("Error while getting block %s", err)
+ }
+ if bytes.Compare(buf, testBlock) != 0 {
+ t.Errorf("Put succeeded but Get returned %+v, expected %+v", buf, testBlock)
+ }
+}
+
+// Put a bad block using PutRaw and get it.
+func testPutRawBadDataGetBlock(t *testing.T, factory TestableVolumeManagerFactory,
+ testHash string, testBlock []byte, badData []byte) {
+ testableVolumes := setupHandlersWithGenericVolumeTest(t, factory)
+
+ // Put bad data for testHash in both volumes
+ testableVolumes[0].PutRaw(testHash, badData)
+ testableVolumes[1].PutRaw(testHash, badData)
+
+ // Get should fail
+ _, err := GetBlock(testHash)
+ if err == nil {
+ t.Fatalf("Expected error while getting corrupt block %v", testHash)
+ }
+}
+
+// Invoke PutBlock twice to ensure CompareAndTouch path is tested.
+func testPutBlock(t *testing.T, factory TestableVolumeManagerFactory, testHash string, testBlock []byte) {
+ setupHandlersWithGenericVolumeTest(t, factory)
+
+ // PutBlock
+ if err := PutBlock(testBlock, testHash); err != nil {
+ t.Fatalf("Error during PutBlock: %s", err)
+ }
+
+ // Check that PutBlock succeeds again even after CompareAndTouch
+ if err := PutBlock(testBlock, testHash); err != nil {
+ t.Fatalf("Error during PutBlock: %s", err)
+ }
+
+ // Check that PutBlock stored the data as expected
+ buf, err := GetBlock(testHash)
+ if err != nil {
+ t.Fatalf("Error during GetBlock for %q: %s", testHash, err)
+ } else if bytes.Compare(buf, testBlock) != 0 {
+ t.Errorf("Get response incorrect. Expected %q; found %q", testBlock, buf)
+ }
+}
+
+// Put a bad block using PutRaw, overwrite it using PutBlock and get it.
+func testPutBlockCorrupt(t *testing.T, factory TestableVolumeManagerFactory,
+ testHash string, testBlock []byte, badData []byte) {
+ testableVolumes := setupHandlersWithGenericVolumeTest(t, factory)
+
+ // Put bad data for testHash in both volumes
+ testableVolumes[0].PutRaw(testHash, badData)
+ testableVolumes[1].PutRaw(testHash, badData)
+
+ // Check that PutBlock with good data succeeds
+ if err := PutBlock(testBlock, testHash); err != nil {
+ t.Fatalf("Error during PutBlock for %q: %s", testHash, err)
+ }
+
+ // Put succeeded and overwrote the badData in one volume,
+ // and Get should return the testBlock now, ignoring the bad data.
+ buf, err := GetBlock(testHash)
+ if err != nil {
+ t.Fatalf("Error during GetBlock for %q: %s", testHash, err)
+ } else if bytes.Compare(buf, testBlock) != 0 {
+ t.Errorf("Get response incorrect. Expected %q; found %q", testBlock, buf)
+ }
+}
testGet(t, factory)
testGetNoSuchBlock(t, factory)
- testCompareSameContent(t, factory)
- testCompareWithDifferentContent(t, factory)
- testCompareWithBadData(t, factory)
-
- testPutBlockWithSameContent(t, factory)
- testPutBlockWithDifferentContent(t, factory)
+ testCompareSameContent(t, factory, TestHash, TestBlock)
+ testCompareSameContent(t, factory, EmptyHash, EmptyBlock)
+ testCompareWithCollision(t, factory, TestHash, TestBlock, []byte("baddata"))
+ testCompareWithCollision(t, factory, TestHash, TestBlock, EmptyBlock)
+ testCompareWithCollision(t, factory, EmptyHash, EmptyBlock, TestBlock)
+ testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, []byte("baddata"))
+ testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, EmptyBlock)
+ testCompareWithCorruptStoredData(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
+
+ testPutBlockWithSameContent(t, factory, TestHash, TestBlock)
+ testPutBlockWithSameContent(t, factory, EmptyHash, EmptyBlock)
+ testPutBlockWithDifferentContent(t, factory, TestHash, TestBlock, TestBlock2)
+ testPutBlockWithDifferentContent(t, factory, TestHash, EmptyBlock, TestBlock)
+ testPutBlockWithDifferentContent(t, factory, TestHash, TestBlock, EmptyBlock)
+ testPutBlockWithDifferentContent(t, factory, EmptyHash, EmptyBlock, TestBlock)
testPutMultipleBlocks(t, factory)
testPutAndTouch(t, factory)
// Put a test block and compare the locator with same content
// Test should pass for both writable and read-only volumes
-func testCompareSameContent(t *testing.T, factory TestableVolumeFactory) {
+func testCompareSameContent(t *testing.T, factory TestableVolumeFactory, testHash string, testData []byte) {
v := factory(t)
defer v.Teardown()
- v.PutRaw(TestHash, TestBlock)
+ v.PutRaw(testHash, testData)
// Compare the block locator with same content
- err := v.Compare(TestHash, TestBlock)
+ err := v.Compare(testHash, testData)
if err != nil {
t.Errorf("Got err %q, expected nil", err)
}
}
-// Put a test block and compare the locator with a different content
-// Expect error due to collision
+// Test behavior of Compare() when stored data matches expected
+// checksum but differs from new data we need to store. Requires
+// testHash = md5(testDataA).
+//
// Test should pass for both writable and read-only volumes
-func testCompareWithDifferentContent(t *testing.T, factory TestableVolumeFactory) {
+func testCompareWithCollision(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
v := factory(t)
defer v.Teardown()
- v.PutRaw(TestHash, TestBlock)
+ v.PutRaw(testHash, testDataA)
// Compare the block locator with different content; collision
- err := v.Compare(TestHash, []byte("baddata"))
+ err := v.Compare(TestHash, testDataB)
if err == nil {
- t.Errorf("Expected error due to collision")
+ t.Errorf("Got err nil, expected error due to collision")
}
}
-// Put a test block with bad data (hash does not match, but Put does not verify)
-// Compare the locator with good data whose hash matches with locator
-// Expect error due to corruption.
+// Test behavior of Compare() when stored data has become
+// corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
+//
// Test should pass for both writable and read-only volumes
-func testCompareWithBadData(t *testing.T, factory TestableVolumeFactory) {
+func testCompareWithCorruptStoredData(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
v := factory(t)
defer v.Teardown()
- v.PutRaw(TestHash, []byte("baddata"))
+ v.PutRaw(TestHash, testDataB)
- err := v.Compare(TestHash, TestBlock)
- if err == nil {
- t.Errorf("Expected error due to corruption")
+ err := v.Compare(testHash, testDataA)
+ if err == nil || err == CollisionError {
+ t.Errorf("Got err %+v, expected non-collision error", err)
}
}
// Put a block and put again with same content
// Test is intended for only writable volumes
-func testPutBlockWithSameContent(t *testing.T, factory TestableVolumeFactory) {
+func testPutBlockWithSameContent(t *testing.T, factory TestableVolumeFactory, testHash string, testData []byte) {
v := factory(t)
defer v.Teardown()
return
}
- err := v.Put(TestHash, TestBlock)
+ err := v.Put(testHash, testData)
if err != nil {
t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
}
- err = v.Put(TestHash, TestBlock)
+ err = v.Put(testHash, testData)
if err != nil {
t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
}
// Put a block and put again with different content
// Test is intended for only writable volumes
-func testPutBlockWithDifferentContent(t *testing.T, factory TestableVolumeFactory) {
+func testPutBlockWithDifferentContent(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
v := factory(t)
defer v.Teardown()
return
}
- err := v.Put(TestHash, TestBlock)
+ err := v.Put(testHash, testDataA)
if err != nil {
- t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
+ t.Errorf("Got err putting block %q: %q, expected nil", testDataA, err)
}
- putErr := v.Put(TestHash, TestBlock2)
- buf, getErr := v.Get(TestHash)
+ putErr := v.Put(testHash, testDataB)
+ buf, getErr := v.Get(testHash)
if putErr == nil {
// Put must not return a nil error unless it has
// overwritten the existing data.
- if bytes.Compare(buf, TestBlock2) != 0 {
- t.Errorf("Put succeeded but Get returned %+v, expected %+v", buf, TestBlock2)
+ if bytes.Compare(buf, testDataB) != 0 {
+ t.Errorf("Put succeeded but Get returned %+v, expected %+v", buf, testDataB)
}
} else {
// It is permissible for Put to fail, but it must
// leave us with either the original data, the new
// data, or nothing at all.
- if getErr == nil && bytes.Compare(buf, TestBlock) != 0 && bytes.Compare(buf, TestBlock2) != 0 {
- t.Errorf("Put failed but Get returned %+v, which is neither %+v nor %+v", buf, TestBlock, TestBlock2)
+ if getErr == nil && bytes.Compare(buf, testDataA) != 0 && bytes.Compare(buf, testDataB) != 0 {
+ t.Errorf("Put failed but Get returned %+v, which is neither %+v nor %+v", buf, testDataA, testDataB)
}
}
if getErr == nil {