-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdbf_test.go
144 lines (127 loc) · 3.88 KB
/
sdbf_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package sdhash
import (
"crypto/sha1"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"math/rand"
"os"
"path"
"strings"
"testing"
)
type testCase struct {
name string
length int64
expectedErr string
fileName string
compareSelfScore int
onlyStreamMode bool
}
var testCases = []testCase{
{
name: "zero-length",
length: 0,
expectedErr: "%s is too small",
},
{
name: "small",
length: 256,
expectedErr: "%s is too small",
},
{
name: "min-size",
length: 512,
compareSelfScore: 0, // maybe too small?
},
{
name: "block-sized",
length: kB,
compareSelfScore: 100,
},
{
name: "rem-block",
length: kB*16 + 31,
compareSelfScore: 100,
},
{
name: "medium",
length: mB,
compareSelfScore: 100,
},
{
name: "chunk-sized",
length: 32 * mB,
compareSelfScore: 100,
onlyStreamMode: true,
},
}
func CreateSdbfTest(testName string, blockSize uint32, tmpDir string) func(t *testing.T) {
return func(t *testing.T) {
for _, tc := range testCases {
if tc.onlyStreamMode && testName != "stream" {
continue
}
t.Run(tc.name, func(t1 *testing.T) {
factory, err := CreateSdbfFromFilename(tc.fileName)
var sd Sdbf
if tc.expectedErr != "" && err != nil {
assert.EqualError(t1, err, fmt.Sprintf(tc.expectedErr, tc.fileName))
return
} else if err == nil {
sdDigest, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s/%s.sdbf", testName, tc.name))
require.NoError(t1, err)
sd = factory.WithBlockSize(blockSize * kB).WithName(tc.name).Compute()
// remove \r in windows builds
expected := strings.ReplaceAll(fmt.Sprintf(string(sdDigest), len(tc.name), tc.name), "\r", "")
assert.Equal(t1, expected, sd.String())
assert.Equal(t1, tc.compareSelfScore, sd.Compare(sd))
} else {
require.Fail(t1, "invalid")
return
}
sdbfParsed, err := ParseSdbfFromString(sd.String())
require.NoError(t1, err)
assert.Equal(t1, sd.String(), sdbfParsed.String())
bf := sd.GetIndex().(*bloomFilter)
tmpFile := path.Join(tmpDir, fmt.Sprintf("%s-%s.idx", testName, tc.name))
require.NoError(t1, bf.WriteToFile(tmpFile))
bfFromIndexFileInt, err := NewBloomFilterFromIndexFile(tmpFile)
require.NoError(t, err)
bfFromIndexFile := bfFromIndexFileInt.(*bloomFilter)
assert.Equal(t1, sha1.Sum(bf.buffer), sha1.Sum(bfFromIndexFile.buffer))
assert.Equal(t1, bf.hashCount, bfFromIndexFile.hashCount)
assert.Equal(t1, bf.bitMask, bfFromIndexFile.bitMask)
assert.Equal(t1, bf.compSize, bfFromIndexFile.compSize)
assert.Equal(t1, bf.name, bfFromIndexFile.name)
bfFromStringInt, err := NewBloomFilterFromString(bf.String())
assert.NoError(t1, err)
bfFromString := bfFromStringInt.(*bloomFilter)
assert.Equal(t1, sha1.Sum(bf.buffer), sha1.Sum(bfFromString.buffer))
assert.Equal(t1, bf.hashCount, bfFromString.hashCount)
assert.Equal(t1, bf.bitMask, bfFromString.bitMask)
assert.Equal(t1, bf.compSize, bfFromString.compSize)
assert.Equal(t1, bf.name, bfFromString.name)
})
}
}
}
func TestSdbfSuite(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "sdhash-test")
require.NoError(t, err)
for i, tc := range testCases {
r := rand.New(rand.NewSource(tc.length))
buf := make([]byte, tc.length)
n, err := r.Read(buf)
require.Equal(t, tc.length, int64(n))
require.NoError(t, err)
fileName := path.Join(tmpDir, tc.name)
require.NoError(t, ioutil.WriteFile(fileName, buf, 0664))
testCases[i].fileName = fileName
}
t.Run("SdbfStream", CreateSdbfTest("stream", 0, tmpDir))
t.Run("SdbfBlock1KB", CreateSdbfTest("block1kb", 1, tmpDir))
t.Run("SdbfBlock16KB", CreateSdbfTest("block16kb", 16, tmpDir))
require.NoError(t, os.RemoveAll(tmpDir))
}