-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbinary_test.go
246 lines (212 loc) · 7.35 KB
/
binary_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package gokeepasslib
import (
"bytes"
"crypto/rand"
"strconv"
"testing"
)
// Tests that binaries can set and get content correctly compressed or uncompressed
func TestBinaryKDBXv31(t *testing.T) {
db := NewDatabase()
binary := db.AddBinary([]byte("test"))
binary.ID = 4
binary2 := db.AddBinary([]byte("replace me"))
binary2.SetContent([]byte("Hello world!"))
if binary2.ID != 5 {
t.Fatalf("Binary2 assigned wrong id by binaries.Add, should be 5, was %d", binary2.ID)
}
if db.FindBinary(2) != nil {
t.Fatalf("Binaries.find for id 2 should be nil, wasn't")
}
references := []BinaryReference{}
references = append(references, binary.CreateReference("example.txt"))
if references[0].Value.ID != 4 {
t.Fatalf("Binary Reference ID is incorrect. Should be 4, was %d", references[0].Value.ID)
}
if data, _ := db.FindBinary(references[0].Value.ID).GetContentBytes(); string(data) != "test" {
t.Fatalf(
"Binary Reference GetContentBytes is incorrect. Should be `test`, was '%s'",
string(data),
)
}
if str, _ := db.FindBinary(references[0].Value.ID).GetContentString(); str != "test" {
t.Fatalf("Binary Reference GetContentString is incorrect. Should be `test`, was '%s'", str)
}
found := db.FindBinary(binary2.ID)
if data, _ := found.GetContentBytes(); string(data) != "Hello world!" {
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `Hello world!`, was '%s'",
string(data),
)
}
if str, _ := found.GetContentString(); str != "Hello world!" {
t.Fatalf("Binary content from FindBinary is incorrect. Should be `Hello world!`, was '%s'", str)
}
}
func TestBinaryKDBXv4(t *testing.T) {
db := NewDatabase(WithDatabaseKDBXVersion4())
randomData := make([]byte, 1024*1024)
rand.Read(randomData)
binary := db.AddBinary(randomData)
entry := NewEntry(WithEntryFormattedTime(!db.Header.IsKdbx4()))
entry.Binaries = append(entry.Binaries, binary.CreateReference("test"))
db.Content.Root.Groups[0].Entries = append(db.Content.Root.Groups[0].Entries, entry)
db.LockProtectedEntries()
var buffer bytes.Buffer
encoder := NewEncoder(&buffer)
encoder.Encode(db)
db = NewDatabase(WithDatabaseKDBXVersion4())
decoder := NewDecoder(bytes.NewReader(buffer.Bytes()))
decoder.Decode(db)
db.UnlockProtectedEntries()
found := db.FindBinary(binary.ID)
if data, _ := found.GetContentBytes(); string(data) != string(randomData) {
t.Log("Received:", len(data))
t.Log("Expexted:", len(randomData))
t.Fatalf("Binary content from Find is incorrect")
}
}
func TestBinaryKDBXv31CleanBinaries(t *testing.T) {
db := NewDatabase()
expected := []*Binary{}
expectedContent := []string{}
count := 5
for i := 0; i < count; i++ {
str := "test " + strconv.Itoa(i)
expectedContent = append(expectedContent, str)
expected = append(expected, db.AddBinary([]byte(str)))
}
if len(db.Content.Meta.Binaries) != count {
t.Fatalf("Expected %d binary elements, found %d", count, len(db.Content.Meta.Binaries))
}
entry := NewEntry(WithEntryFormattedTime(!db.Header.IsKdbx4()))
for i := 0; i < count; i++ {
entry.Binaries = append(entry.Binaries, expected[i].CreateReference("test"))
}
db.Content.Root.Groups[0].Entries = []Entry{}
db.Content.Root.Groups[0].Entries = append(db.Content.Root.Groups[0].Entries, entry)
binaries := db.Content.Root.Groups[0].Entries[0].Binaries
for i := 0; i < count; i++ {
found := db.FindBinary(binaries[i].Value.ID)
if data, _ := found.GetContentString(); data != expectedContent[i] {
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `%s`, was '%s'",
expectedContent[i],
data,
)
}
}
toRemove := map[int]bool{0: true, 2: true, 4: true}
newBinaries := []BinaryReference{}
newExpected := []*Binary{}
newExpectedContent := []string{}
for i := range binaries {
if _, remove := toRemove[i]; !remove {
newBinaries = append(newBinaries, binaries[i])
newExpected = append(newExpected, expected[i])
newExpectedContent = append(newExpectedContent, expectedContent[i])
}
}
db.Content.Root.Groups[0].Entries[0].Binaries = newBinaries
expected = newExpected
expectedContent = newExpectedContent
db.LockProtectedEntries()
var buffer bytes.Buffer
encoder := NewEncoder(&buffer)
encoder.Encode(db)
db = NewDatabase()
decoder := NewDecoder(bytes.NewReader(buffer.Bytes()))
decoder.Decode(db)
db.UnlockProtectedEntries()
if len(db.Content.Meta.Binaries) != len(expected) {
t.Fatalf("Expected %d binary elements, found %d", len(expected), len(db.Content.Meta.Binaries))
}
for i := 0; i < len(expected); i++ {
found := db.FindBinary(i)
if found == nil {
t.Fatalf("Binary (ID=%d) not found", i)
}
if data, _ := found.GetContentBytes(); string(data) != expectedContent[i] {
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `%s`, was '%s'",
expectedContent[i],
string(data),
)
}
ref := db.Content.Root.Groups[0].Entries[0].Binaries[i]
if ref.Value.ID != i {
t.Fatalf("Binary reference is incorrect. Should be `%d`, was '%d'", i, ref.Value.ID)
}
}
}
func TestBinaryKDBXv4CleanBinaries(t *testing.T) {
db := NewDatabase(WithDatabaseKDBXVersion4())
expected := []*Binary{}
count := 5
for i := 0; i < count; i++ {
expected = append(expected, db.AddBinary([]byte("test "+strconv.Itoa(i))))
}
if len(db.Content.InnerHeader.Binaries) != count {
t.Fatalf("Expected %d binary elements, found %d", count, len(db.Content.InnerHeader.Binaries))
}
entry := NewEntry(WithEntryFormattedTime(!db.Header.IsKdbx4()))
for i := 0; i < count; i++ {
entry.Binaries = append(entry.Binaries, expected[i].CreateReference("test"))
}
db.Content.Root.Groups[0].Entries = []Entry{}
db.Content.Root.Groups[0].Entries = append(db.Content.Root.Groups[0].Entries, entry)
binaries := db.Content.Root.Groups[0].Entries[0].Binaries
for i := 0; i < count; i++ {
found := db.FindBinary(binaries[i].Value.ID)
if data, _ := found.GetContentBytes(); string(data) != string(expected[i].Content) {
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `%s`, was '%s'",
string(expected[i].Content),
string(data),
)
}
}
toRemove := map[int]bool{0: true, 2: true, 4: true}
newBinaries := []BinaryReference{}
newExpected := []*Binary{}
for i := range binaries {
if _, remove := toRemove[i]; !remove {
newBinaries = append(newBinaries, binaries[i])
newExpected = append(newExpected, expected[i])
}
}
db.Content.Root.Groups[0].Entries[0].Binaries = newBinaries
expected = newExpected
db.LockProtectedEntries()
var buffer bytes.Buffer
encoder := NewEncoder(&buffer)
encoder.Encode(db)
db = NewDatabase(WithDatabaseKDBXVersion4())
decoder := NewDecoder(bytes.NewReader(buffer.Bytes()))
decoder.Decode(db)
db.UnlockProtectedEntries()
if len(db.Content.InnerHeader.Binaries) != len(expected) {
t.Fatalf(
"Expected %d binary elements, found %d",
len(expected),
len(db.Content.InnerHeader.Binaries),
)
}
for i := 0; i < len(expected); i++ {
found := db.FindBinary(i)
if found == nil {
t.Fatalf("Binary (ID=%d) not found", i)
}
if data, _ := found.GetContentBytes(); string(data) != string(expected[i].Content) {
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `%s`, was '%s'",
string(expected[i].Content),
string(data),
)
}
ref := db.Content.Root.Groups[0].Entries[0].Binaries[i]
if ref.Value.ID != i {
t.Fatalf("Binary reference is incorrect. Should be `%d`, was '%d'", i, ref.Value.ID)
}
}
}