From 90e248615f02e7a6a913d30f71561b5e4c86ee53 Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Thu, 25 Jun 2020 14:47:35 +0200 Subject: [PATCH] lib/scanner: Test weak hash consistency (ref #5556) (#6794) Relevant much earlier changes: 9b1c592fb7e7ca86fec25545be742c13bdae25b9 bd1c29ee323573bf66d01b9d2887e1e8cb70ab68 Make sure vanilla and rolling adler are consistent. And that they match with scanner.Validate. --- lib/model/model.go | 2 +- lib/scanner/blocks_test.go | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/model/model.go b/lib/model/model.go index 3e95dd937..b6ac2d689 100644 --- a/lib/model/model.go +++ b/lib/model/model.go @@ -1525,7 +1525,7 @@ func (m *model) Request(deviceID protocol.DeviceID, folder, name string, size in if !scanner.Validate(res.data, hash, weakHash) { m.recheckFile(deviceID, folder, name, offset, hash) - l.Debugf("%v REQ(in) failed validating data (%v): %s: %q / %q o=%d s=%d", m, err, deviceID, folder, name, offset, size) + l.Debugf("%v REQ(in) failed validating data: %s: %q / %q o=%d s=%d", m, deviceID, folder, name, offset, size) return nil, protocol.ErrNoSuchFile } diff --git a/lib/scanner/blocks_test.go b/lib/scanner/blocks_test.go index 011a4f97e..a93ea2e2e 100644 --- a/lib/scanner/blocks_test.go +++ b/lib/scanner/blocks_test.go @@ -123,7 +123,9 @@ func TestAdler32Variants(t *testing.T) { hf1.Reset() hf2.Reset() - return sum1 == sum2 + // Make sure whatever we use in Validate matches too resp. this + // tests gets adjusted if we ever switch the weak hash algo. + return sum1 == sum2 && Validate(data, nil, sum1) } // protocol block sized data @@ -141,8 +143,7 @@ func TestAdler32Variants(t *testing.T) { } // rolling should have the same result as the individual blocks - // themselves. Which is not the same as the original non-rollind adler32 - // blocks. + // themselves. windowSize := 128 @@ -152,10 +153,14 @@ func TestAdler32Variants(t *testing.T) { for i := windowSize; i < len(data); i++ { if i%windowSize == 0 { // let the reference function catch up + window := data[i-windowSize : i] + hf1.Reset() + hf1.Write(window) hf2.Reset() - hf2.Write(data[i-windowSize : i]) + hf2.Write(window) // verify that they are in sync with the rolling function + sum1 := hf1.Sum32() sum2 := hf2.Sum32() sum3 := hf3.Sum32() t.Logf("At i=%d, sum2=%08x, sum3=%08x", i, sum2, sum3) @@ -163,6 +168,13 @@ func TestAdler32Variants(t *testing.T) { t.Errorf("Mismatch after roll; i=%d, sum2=%08x, sum3=%08x", i, sum2, sum3) break } + if sum1 != sum3 { + t.Errorf("Mismatch after roll; i=%d, sum1=%08x, sum3=%08x", i, sum1, sum3) + break + } + if !Validate(window, nil, sum1) { + t.Errorf("Validation failure after roll; i=%d", i) + } } hf3.Roll(data[i]) }