Пример #1
0
func TestPrependedBlocks(t *testing.T) {
	const BLOCKSIZE = 100
	const BLOCK_COUNT = 20
	checksum := NewFileChecksumGenerator(BLOCKSIZE)

	file1 := io.LimitReader(
		readers.NewNonRepeatingSequence(0),
		BLOCKSIZE*BLOCK_COUNT,
	)

	file2 := io.LimitReader(
		io.MultiReader(
			readers.OneReader(BLOCKSIZE), // Off by one block
			readers.NewNonRepeatingSequence(0),
		),
		BLOCKSIZE*BLOCK_COUNT,
	)

	output1 := bytes.NewBuffer(nil)
	chksum1, _ := checksum.GenerateChecksums(file1, output1)

	output2 := bytes.NewBuffer(nil)
	chksum2, _ := checksum.GenerateChecksums(file2, output2)

	if bytes.Compare(chksum1, chksum2) == 0 {
		t.Fatal("Checksums should be different")
	}

	weaksize, strongSize := checksum.GetChecksumSizes()
	sums1, _ := chunks.LoadChecksumsFromReader(output1, weaksize, strongSize)
	sums2, _ := chunks.LoadChecksumsFromReader(output2, weaksize, strongSize)

	if len(sums1) != len(sums2) {
		t.Fatalf("Checksum lengths differ %v vs %v", len(sums1), len(sums2))
	}

	if sums1[0].Match(sums2[0]) {
		t.Error("Chunk sums1[0] should differ from sums2[0]")
	}

	for i, _ := range sums2 {
		if i == 0 {
			continue
		}

		if !sums1[i-1].Match(sums2[i]) {
			t.Errorf("Chunk sums1[%v] equal sums2[%v]", i-1, i)
		}

	}
}
Пример #2
0
// Each of the data blocks is the same, so the checksums for the blocks should be the same
func TestChecksumBlocksTheSame(t *testing.T) {
	const BLOCKSIZE = 100
	const BLOCK_COUNT = 20

	checksum := NewFileChecksumGenerator(BLOCKSIZE)
	output := bytes.NewBuffer(nil)

	_, err := checksum.GenerateChecksums(
		readers.OneReader(BLOCKSIZE*BLOCK_COUNT),
		output,
	)

	if err != nil {
		t.Fatal(err)
	}

	weakSize, strongSize := checksum.GetChecksumSizes()

	if output.Len() != BLOCK_COUNT*(strongSize+weakSize) {
		t.Errorf(
			"Unexpected output length: %v, expected %v",
			output.Len(),
			BLOCK_COUNT*(strongSize+weakSize),
		)
	}

	results, err := chunks.LoadChecksumsFromReader(output, weakSize, strongSize)

	if err != nil {
		t.Fatal(err)
	}

	if len(results) != BLOCK_COUNT {
		t.Fatalf("Results too short! %v", len(results))
	}

	first := results[0]

	for i, chk := range results {
		if chk.ChunkOffset != uint(i) {
			t.Errorf("Unexpected offset %v on chunk %v", chk.ChunkOffset, i)
		}
		if !first.Match(chk) {
			t.Fatalf("Chunks have different checksums on %v", i)
		}
	}
}
Пример #3
0
func BenchmarkStrongComparison(b *testing.B) {
	b.ReportAllocs()
	b.SetBytes(1)

	const BLOCK_SIZE = 8
	generator := filechecksum.NewFileChecksumGenerator(BLOCK_SIZE)

	b.StartTimer()

	results := (&Comparer{}).StartFindMatchingBlocks(
		readers.OneReader(b.N+BLOCK_SIZE),
		0,
		generator,
		&NegativeStrongIndex{},
	)

	for _, ok := <-results; ok; {
	}

	b.StopTimer()
}