-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrity_test.go
198 lines (180 loc) · 6.68 KB
/
integrity_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
package main
import (
"os"
"sort"
"testing"
)
func TestCheckIntegrity(t *testing.T) {
//// Test a normal scenario
// 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")
correctAddedChanges := []IntegrityChangesAdded{
{
FilePath: FilePath(fileOne.Name()),
FileHash: "19e1c0dda4fbdd75d6f0d31e5e28b2a4103b370049909bb485ebacf297c8952f",
},
{
FilePath: FilePath(fileTwo.Name()),
FileHash: "f25b7c4a9e88d251b5942c7d7a2c6480eef3e07195f9b7063ca332702edeaff9",
},
{
FilePath: FilePath(fileThree.Name()),
FileHash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
{
FilePath: FilePath(fileFour.Name()),
FileHash: "d12ebbd2f807bf53517dd8e8eed737a919e4d2f2b3952d62a52b23fcd53a3109",
},
{
FilePath: FilePath(fileFive.Name()),
FileHash: "2b4a26d3dceacce9b09c4faddd5594d6113235d0fb03ff1bd70bf27a2b7f7dc7",
},
{
FilePath: FilePath(fileSix.Name()),
FileHash: "c2d098c3d20d3d6e321689f26377b72424e72704edfa331d5c30e08d0ef97b32",
},
{
FilePath: FilePath(fileSeven.Name()),
FileHash: "f004d515b6bd74a6f052bbf5f24b4c08b0d990e8c5811e3721a4642584b9a754",
},
}
// Execute
fileDB, _ := os.CreateTemp("", "hashes.db")
integrityChanges, err := CheckIntegrity(fileDB.Name(), []string{
directoryOne,
directoryFour,
fileSix.Name(),
fileSeven.Name(),
})
if err != nil {
t.Error("could not check integrity")
}
if len(integrityChanges.Added) != 7 || len(integrityChanges.Modified) != 0 || len(integrityChanges.Removed) != 0 {
t.Errorf("bad integrity change; added %v; modified %v; removed %v", len(integrityChanges.Added), len(integrityChanges.Modified), len(integrityChanges.Removed))
}
// Validate
sort.Slice(correctAddedChanges, func(i, j int) bool {
return correctAddedChanges[i].FilePath < correctAddedChanges[j].FilePath
})
sort.Slice(integrityChanges.Added, func(i, j int) bool {
return integrityChanges.Added[i].FilePath < integrityChanges.Added[j].FilePath
})
for index, value := range integrityChanges.Added {
if value.FilePath != correctAddedChanges[index].FilePath || value.FileHash != correctAddedChanges[index].FileHash {
t.Error("integrity not preserved")
}
}
//// Test a changed scenario with files added, removed and modified
// Setup
fileEight, _ := os.CreateTemp(directoryTwo, "fileeigth")
fileEight.WriteString("testcontent8")
fileTwo.WriteString("testcontent2")
fileThree.WriteString("testcontent3")
fileSix.WriteString("testcontent6")
os.Remove(fileFour.Name())
os.Remove(fileSeven.Name())
correctAddedChanges = []IntegrityChangesAdded{
{
FilePath: FilePath(fileEight.Name()),
FileHash: "87302530342d37cddef11a7d6f06808c7c1625907c60083ca0f71e48d1114537",
},
}
correctModifiedChanges := []IntegrityChangesModified{
{
FilePath: FilePath(fileTwo.Name()),
OldFileHash: "f25b7c4a9e88d251b5942c7d7a2c6480eef3e07195f9b7063ca332702edeaff9",
NewFileHash: "aeb3d6b8fb1c94ea4e1a08b8d26eb43dc53d911c149a9039e8869e51b5ff7daa",
},
{
FilePath: FilePath(fileThree.Name()),
OldFileHash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
NewFileHash: "e10a2b08a5b491fa24d934edff560bd8e0154b31165b8ef1214c59ab02ccc838",
},
{
FilePath: FilePath(fileSix.Name()),
OldFileHash: "c2d098c3d20d3d6e321689f26377b72424e72704edfa331d5c30e08d0ef97b32",
NewFileHash: "09ab606d95f819e8371f12bda5dd0af4d18507b648946a5f72a3f282317c8bd2",
},
}
correctRemovedChanges := []IntegrityChangesRemoved{
{
FilePath: FilePath(fileFour.Name()),
FileHash: "d12ebbd2f807bf53517dd8e8eed737a919e4d2f2b3952d62a52b23fcd53a3109",
},
{
FilePath: FilePath(fileSeven.Name()),
FileHash: "f004d515b6bd74a6f052bbf5f24b4c08b0d990e8c5811e3721a4642584b9a754",
},
}
// Execute
integrityChanges, err = CheckIntegrity(fileDB.Name(), []string{
directoryOne,
directoryFour,
fileSix.Name(),
fileSeven.Name(),
})
if err != nil {
t.Error("could not check integrity")
}
if len(integrityChanges.Added) != 1 || len(integrityChanges.Modified) != 3 || len(integrityChanges.Removed) != 2 {
t.Errorf("bad integrity change; added %v; modified %v; removed %v", len(integrityChanges.Added), len(integrityChanges.Modified), len(integrityChanges.Removed))
}
// Validate
sort.Slice(correctAddedChanges, func(i, j int) bool {
return correctAddedChanges[i].FilePath < correctAddedChanges[j].FilePath
})
sort.Slice(integrityChanges.Added, func(i, j int) bool {
return integrityChanges.Added[i].FilePath < integrityChanges.Added[j].FilePath
})
for index, value := range integrityChanges.Added {
if value.FilePath != correctAddedChanges[index].FilePath || value.FileHash != correctAddedChanges[index].FileHash {
t.Error("integrity not preserved")
}
}
sort.Slice(correctModifiedChanges, func(i, j int) bool {
return correctModifiedChanges[i].FilePath < correctModifiedChanges[j].FilePath
})
sort.Slice(integrityChanges.Modified, func(i, j int) bool {
return integrityChanges.Modified[i].FilePath < integrityChanges.Modified[j].FilePath
})
for index, value := range integrityChanges.Modified {
if value.FilePath != correctModifiedChanges[index].FilePath ||
value.OldFileHash != correctModifiedChanges[index].OldFileHash ||
value.NewFileHash != correctModifiedChanges[index].NewFileHash {
t.Error("integrity not preserved")
}
}
sort.Slice(correctRemovedChanges, func(i, j int) bool {
return correctRemovedChanges[i].FilePath < correctRemovedChanges[j].FilePath
})
sort.Slice(integrityChanges.Removed, func(i, j int) bool {
return integrityChanges.Removed[i].FilePath < integrityChanges.Removed[j].FilePath
})
for index, value := range integrityChanges.Removed {
if value.FilePath != correctRemovedChanges[index].FilePath || value.FileHash != correctRemovedChanges[index].FileHash {
t.Error("integrity not preserved")
}
}
//// Teardown
os.Remove(fileDB.Name())
os.RemoveAll(directoryOne)
os.RemoveAll(directoryFour)
os.Remove(fileSix.Name())
os.Remove(fileSeven.Name())
}