forked from glaslos/ssdeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssdeep_test.go
169 lines (141 loc) · 3.71 KB
/
ssdeep_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ssdeep
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
)
func assertNoError(t *testing.T, err error) {
if err != nil {
t.Fatalf("Received unexpected error %+v", err)
}
}
func assertError(t *testing.T, err error) {
if err == nil {
t.Fatal("An error is expected but got nil.")
}
}
func assertHashEqual(t *testing.T, expected, actual string) {
if expected != actual {
t.Fatalf("Hash mismatch: %s (expected)\n"+
" != %s (actual)", expected, actual)
}
}
func TestIntegrity(t *testing.T) {
rand.Seed(1)
resultsFile, err := ioutil.ReadFile("ssdeep_results.json")
assertNoError(t, err)
originalResults := make(map[string]string)
err = json.Unmarshal(resultsFile, &originalResults)
assertNoError(t, err)
for i := 4097; i < 10*1024*1024; i += 4096 * 10 {
t.Run(fmt.Sprintf("Bytes in size of %d", i), func(t *testing.T) {
size := i
if size == 4097 {
i--
}
blob := make([]byte, size, size)
rand.Read(blob)
assertNoError(t, err)
result, err := FuzzyBytes(blob)
assertNoError(t, err)
assertHashEqual(t, originalResults[fmt.Sprint(size)], result)
})
}
}
func concatCopyPreAllocate(slices [][]byte) []byte {
var totalLen int
for _, s := range slices {
totalLen += len(s)
}
tmp := make([]byte, totalLen)
var i int
for _, s := range slices {
i += copy(tmp[i:], s)
}
return tmp
}
func TestRollingHash(t *testing.T) {
s := ssdeepState{
rollingState: rollingState{
window: make([]byte, rollingWindow),
},
}
s.rollHash(byte('A'))
rh := s.rollingState.rollSum()
if rh != 585 {
t.Fatal("Rolling hash not matching")
}
}
func TestFuzzyBytesOutputsTheRightResult(t *testing.T) {
b, err := ioutil.ReadFile("LICENSE")
assertNoError(t, err)
b = concatCopyPreAllocate([][]byte{b, b})
hashResult, err := FuzzyBytes(b)
assertNoError(t, err)
expectedResult := "96:PuNQHTo6pYrYJWrYJ6N3w53hpYTdhuNQHTo6pYrYJWrYJ6N3w53hpYTP:+QHTrpYrsWrs6N3g3LaGQHTrpYrsWrsa"
assertHashEqual(t, expectedResult, hashResult)
}
func TestFuzzyFileOutputsTheRightResult(t *testing.T) {
f, err := os.Open("ssdeep_results.json")
assertNoError(t, err)
defer f.Close()
hashResult, err := FuzzyFile(f)
assertNoError(t, err)
expectedResult := "1536:74peLhFipssVfuInITTTZzMoW0379xy3u:VVFosEfudTj579k3u"
assertHashEqual(t, expectedResult, hashResult)
}
func TestFuzzyFileOutputsAnErrorForSmallFiles(t *testing.T) {
f, err := os.Open("LICENSE")
assertNoError(t, err)
defer f.Close()
_, err = FuzzyFile(f)
assertError(t, err)
}
func TestFuzzyFilenameOutputsTheRightResult(t *testing.T) {
hashResult, err := FuzzyFilename("ssdeep_results.json")
assertNoError(t, err)
expectedResult := "1536:74peLhFipssVfuInITTTZzMoW0379xy3u:VVFosEfudTj579k3u"
assertHashEqual(t, expectedResult, hashResult)
}
func TestFuzzyFilenameOutputsErrorWhenFileNotExists(t *testing.T) {
_, err := FuzzyFilename("foo.bar")
assertError(t, err)
}
func TestFuzzyBytesWithLenLessThanMinimumOutputsAnError(t *testing.T) {
_, err := FuzzyBytes([]byte{})
assertError(t, err)
}
func TestFuzzyBytesWithOutputsAnError(t *testing.T) {
_, err := FuzzyBytes(make([]byte, 4096, 4096))
assertError(t, err)
}
func BenchmarkRollingHash(b *testing.B) {
s := newSsdeepState()
for i := 0; i < b.N; i++ {
s.rollHash(byte(i))
}
}
func BenchmarkSumHash(b *testing.B) {
testHash := hashInit
data := []byte("Hereyougojustsomedatatomakeyouhappy")
for i := 0; i < b.N; i++ {
testHash = sumHash(data[rand.Intn(len(data))], testHash)
}
}
func BenchmarkBlockSize(b *testing.B) {
s := newSsdeepState()
for i := 0; i < b.N; i++ {
s.getBlockSize(207160)
}
}
func BenchmarkProcessByte(b *testing.B) {
s := newSsdeepState()
s.blockSize = 42
s.newRollingState()
for i := 0; i < b.N; i++ {
s.processByte(byte(i))
}
}