2016-12-14 23:30:29 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-12-14 23:30:29 +00:00
|
|
|
|
|
|
|
// The existence of this file means we get 0% test coverage rather than no
|
|
|
|
// test coverage at all. Remove when implementing an actual test.
|
|
|
|
|
|
|
|
package weakhash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-07-04 07:07:33 +00:00
|
|
|
"context"
|
2017-08-19 14:36:56 +00:00
|
|
|
"io"
|
2016-12-14 23:30:29 +00:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var payload = []byte("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
|
|
|
|
|
|
|
|
func TestFinder(t *testing.T) {
|
2021-11-22 07:59:47 +00:00
|
|
|
f, err := os.CreateTemp("", "")
|
2016-12-14 23:30:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := f.Write(payload); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-08-19 14:36:56 +00:00
|
|
|
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-12-14 23:30:29 +00:00
|
|
|
|
2017-01-04 21:04:13 +00:00
|
|
|
hashes := []uint32{65143183, 65798547}
|
2018-07-04 07:07:33 +00:00
|
|
|
finder, err := NewFinder(context.Background(), f, 4, hashes)
|
2016-12-14 23:30:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[uint32][]int64{
|
2017-02-05 17:49:57 +00:00
|
|
|
65143183: {1, 27, 53, 79},
|
|
|
|
65798547: {2, 28, 54, 80},
|
2016-12-14 23:30:29 +00:00
|
|
|
}
|
|
|
|
actual := make(map[uint32][]int64)
|
|
|
|
|
|
|
|
b := make([]byte, Size)
|
|
|
|
|
|
|
|
for _, hash := range hashes {
|
|
|
|
_, err := finder.Iterate(hash, b[:4], func(offset int64) bool {
|
|
|
|
if !bytes.Equal(b, payload[offset:offset+4]) {
|
|
|
|
t.Errorf("Not equal at %d: %s != %s", offset, string(b), string(payload[offset:offset+4]))
|
|
|
|
}
|
|
|
|
actual[hash] = append(actual[hash], offset)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Errorf("Not equal: %#v != %#v", actual, expected)
|
|
|
|
}
|
|
|
|
}
|