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"
|
2017-08-19 14:36:56 +00:00
|
|
|
"io"
|
2016-12-14 23:30:29 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var payload = []byte("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
|
|
|
|
|
|
|
|
func TestFinder(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "")
|
|
|
|
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}
|
2017-08-19 14:36:56 +00:00
|
|
|
finder, err := NewFinder(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)
|
|
|
|
}
|
|
|
|
}
|