lib/scanner: Test weak hash consistency (ref #5556) (#6794)

Relevant much earlier changes:
  9b1c592fb7
  bd1c29ee32

Make sure vanilla and rolling adler are consistent. And that they match
with scanner.Validate.
This commit is contained in:
Simon Frei 2020-06-25 14:47:35 +02:00 committed by GitHub
parent 57f47bcf83
commit 90e248615f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -1525,7 +1525,7 @@ func (m *model) Request(deviceID protocol.DeviceID, folder, name string, size in
if !scanner.Validate(res.data, hash, weakHash) { if !scanner.Validate(res.data, hash, weakHash) {
m.recheckFile(deviceID, folder, name, offset, hash) 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 return nil, protocol.ErrNoSuchFile
} }

View File

@ -123,7 +123,9 @@ func TestAdler32Variants(t *testing.T) {
hf1.Reset() hf1.Reset()
hf2.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 // protocol block sized data
@ -141,8 +143,7 @@ func TestAdler32Variants(t *testing.T) {
} }
// rolling should have the same result as the individual blocks // rolling should have the same result as the individual blocks
// themselves. Which is not the same as the original non-rollind adler32 // themselves.
// blocks.
windowSize := 128 windowSize := 128
@ -152,10 +153,14 @@ func TestAdler32Variants(t *testing.T) {
for i := windowSize; i < len(data); i++ { for i := windowSize; i < len(data); i++ {
if i%windowSize == 0 { if i%windowSize == 0 {
// let the reference function catch up // let the reference function catch up
window := data[i-windowSize : i]
hf1.Reset()
hf1.Write(window)
hf2.Reset() hf2.Reset()
hf2.Write(data[i-windowSize : i]) hf2.Write(window)
// verify that they are in sync with the rolling function // verify that they are in sync with the rolling function
sum1 := hf1.Sum32()
sum2 := hf2.Sum32() sum2 := hf2.Sum32()
sum3 := hf3.Sum32() sum3 := hf3.Sum32()
t.Logf("At i=%d, sum2=%08x, sum3=%08x", i, sum2, sum3) 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) t.Errorf("Mismatch after roll; i=%d, sum2=%08x, sum3=%08x", i, sum2, sum3)
break 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]) hf3.Roll(data[i])
} }