-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_test.go
208 lines (174 loc) · 6.35 KB
/
database_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"encoding/json"
"os"
"reflect"
"testing"
)
func TestSetupFileHashDatabase(t *testing.T) {
//// Test normal directory containing files and recursive directories with files
// Setup
directoryOne, _ := os.MkdirTemp("", "jab_database_test_one")
fileOne, _ := os.CreateTemp(directoryOne, "fileone")
fileOne.WriteString("testcontent1")
fileTwo, _ := os.CreateTemp(directoryOne, "filetwo")
fileTwo.WriteString("testcontent2")
directoryTwo, _ := os.MkdirTemp(directoryOne, "jab_database_test_two")
fileThree, _ := os.CreateTemp(directoryTwo, "filethree")
directoryThree, _ := os.MkdirTemp(directoryTwo, "jab_database_test_three")
fileFour, _ := os.CreateTemp(directoryThree, "filefour")
fileFour.WriteString("testcontent4")
directoryFour, _ := os.MkdirTemp("", "jab_database_test_four")
fileFive, _ := os.CreateTemp(directoryFour, "filefive")
fileFive.WriteString("testcontent5")
fileSix, _ := os.CreateTemp("", "filesix")
fileSix.WriteString("testcontent6")
fileSeven, _ := os.CreateTemp("", "fileseven")
fileSeven.WriteString("testcontent7")
correctDB := FileHashDatabase{}
correctDB[FilePath(fileOne.Name())] = "19e1c0dda4fbdd75d6f0d31e5e28b2a4103b370049909bb485ebacf297c8952f"
correctDB[FilePath(fileTwo.Name())] = "f25b7c4a9e88d251b5942c7d7a2c6480eef3e07195f9b7063ca332702edeaff9"
correctDB[FilePath(fileThree.Name())] = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
correctDB[FilePath(fileFour.Name())] = "d12ebbd2f807bf53517dd8e8eed737a919e4d2f2b3952d62a52b23fcd53a3109"
correctDB[FilePath(fileFive.Name())] = "2b4a26d3dceacce9b09c4faddd5594d6113235d0fb03ff1bd70bf27a2b7f7dc7"
correctDB[FilePath(fileSix.Name())] = "c2d098c3d20d3d6e321689f26377b72424e72704edfa331d5c30e08d0ef97b32"
correctDB[FilePath(fileSeven.Name())] = "f004d515b6bd74a6f052bbf5f24b4c08b0d990e8c5811e3721a4642584b9a754"
// Execute
db, err := SetupFileHashDatabase([]string{directoryOne, directoryFour, fileSix.Name(), fileSeven.Name()})
if err != nil {
t.Error("could not create file hash database")
}
// Validate
/*
// Debug
t.Log("generated db")
for path, hash := range db {
t.Logf("path: %s, hash: %s", path, hash)
}
t.Log("correct db")
for path, hash := range correctDB {
t.Logf("path: %s, hash: %s", path, hash)
}
*/
result := reflect.DeepEqual(correctDB, db)
if !result {
t.Error("databases not matching")
}
// Teardown
os.RemoveAll(directoryOne)
os.RemoveAll(directoryFour)
os.Remove(fileSix.Name())
os.Remove(fileSeven.Name())
//// Test empty directory
// Setup
directoryOne, _ = os.MkdirTemp("", "jab_database_test_one")
correctDB = FileHashDatabase{}
// Execute
db, err = SetupFileHashDatabase([]string{directoryOne})
if err != nil {
t.Error("could not create file hash database")
}
// Validate
result = reflect.DeepEqual(correctDB, db)
if !result {
t.Error("databases not matching")
}
// Teardown
os.RemoveAll(directoryOne)
}
func TestLoadFileHashDatabase(t *testing.T) {
//// Test normal database
// Setup DB
correctDB := FileHashDatabase{}
correctDB[FilePath("fileone")] = "19e1c0dda4fbdd75d6f0d31e5e28b2a4103b370049909bb485ebacf297c8952f"
correctDB[FilePath("filetwo")] = "f25b7c4a9e88d251b5942c7d7a2c6480eef3e07195f9b7063ca332702edeaff9"
correctDB[FilePath("filethree")] = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
correctDB[FilePath("filefour")] = "d12ebbd2f807bf53517dd8e8eed737a919e4d2f2b3952d62a52b23fcd53a3109"
correctDB[FilePath("filefive")] = "2b4a26d3dceacce9b09c4faddd5594d6113235d0fb03ff1bd70bf27a2b7f7dc7"
correctDB[FilePath("filesix")] = "c2d098c3d20d3d6e321689f26377b72424e72704edfa331d5c30e08d0ef97b32"
correctDB[FilePath("fileseven")] = "f004d515b6bd74a6f052bbf5f24b4c08b0d990e8c5811e3721a4642584b9a754"
// Manually save DB
jsonBytes, _ := json.Marshal(correctDB)
fileDB, _ := os.CreateTemp("", "hashes.db")
fileDB.Write(jsonBytes)
// Load DB with function
db, err := LoadFileHashDatabase(fileDB.Name())
if err != nil {
t.Error("could not load db")
}
// Validate
result := reflect.DeepEqual(correctDB, db)
if !result {
t.Error("databases not matching")
}
// Teardown
os.Remove(fileDB.Name())
//// Test empty database
// Setup DB
correctDB = FileHashDatabase{}
// Manually save DB
jsonBytes, _ = json.Marshal(correctDB)
fileDB, _ = os.CreateTemp("", "hashes.db")
fileDB.Write(jsonBytes)
// Load DB with function
db, err = LoadFileHashDatabase(fileDB.Name())
if err != nil {
t.Error("could not load db")
}
// Validate
result = reflect.DeepEqual(correctDB, db)
if !result {
t.Error("databases not matching")
}
// Teardown
os.Remove(fileDB.Name())
}
func TestSaveFileHashDatabase(t *testing.T) {
//// Test normal database
// Setup DB
correctDB := FileHashDatabase{}
correctDB[FilePath("fileone")] = "19e1c0dda4fbdd75d6f0d31e5e28b2a4103b370049909bb485ebacf297c8952f"
correctDB[FilePath("filetwo")] = "f25b7c4a9e88d251b5942c7d7a2c6480eef3e07195f9b7063ca332702edeaff9"
correctDB[FilePath("filethree")] = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
correctDB[FilePath("filefour")] = "d12ebbd2f807bf53517dd8e8eed737a919e4d2f2b3952d62a52b23fcd53a3109"
correctDB[FilePath("filefive")] = "2b4a26d3dceacce9b09c4faddd5594d6113235d0fb03ff1bd70bf27a2b7f7dc7"
correctDB[FilePath("filesix")] = "c2d098c3d20d3d6e321689f26377b72424e72704edfa331d5c30e08d0ef97b32"
correctDB[FilePath("fileseven")] = "f004d515b6bd74a6f052bbf5f24b4c08b0d990e8c5811e3721a4642584b9a754"
// Save DB with function
fileDB, _ := os.CreateTemp("", "hashes.db")
err := SaveFileHashDatabase(correctDB, fileDB.Name())
if err != nil {
t.Error("could not save db")
}
// Manually load DB
dbBytes, _ := os.ReadFile(fileDB.Name())
var db FileHashDatabase
json.Unmarshal([]byte(dbBytes), &db)
// Validate
result := reflect.DeepEqual(correctDB, db)
if !result {
t.Error("databases not matching")
}
// Teardown
os.Remove(fileDB.Name())
//// Test empty database
// Setup DB
correctDB = FileHashDatabase{}
// Save DB with function
fileDB, _ = os.CreateTemp("", "hashes.db")
err = SaveFileHashDatabase(correctDB, fileDB.Name())
if err != nil {
t.Error("could not save db")
}
// Manually load DB
dbBytes, _ = os.ReadFile(fileDB.Name())
var dbEmpty FileHashDatabase
json.Unmarshal([]byte(dbBytes), &dbEmpty)
// Validate
result = reflect.DeepEqual(correctDB, dbEmpty)
if !result {
t.Error("databases not matching")
}
// Teardown
os.Remove(fileDB.Name())
}