-
Notifications
You must be signed in to change notification settings - Fork 30
/
group_test.go
330 lines (295 loc) · 7.89 KB
/
group_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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package gokeepasslib
import (
"bytes"
"encoding/xml"
"errors"
"testing"
"time"
w "github.com/tobischo/gokeepasslib/v3/wrappers"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
)
func TestNewGroup(t *testing.T) {
cases := []struct {
title string
options []GroupOption
expectedGroup Group
}{
{
title: "without options",
expectedGroup: Group{
Name: "",
},
},
{
title: "with multiple options",
options: []GroupOption{
WithGroupFormattedTime(false),
func(rd *Group) {
rd.Name = "other name"
},
},
expectedGroup: Group{
Name: "other name",
},
},
{
title: "with custom icon",
options: []GroupOption{
WithGroupFormattedTime(false),
func(rd *Group) {
rd.CustomIconUUID = UUID{
0xde, 0xad, 0xbe, 0xef,
0xc0, 0xff, 0xee, 0xde,
0xed, 0x01, 0x23, 0x45,
0x67, 0x89, 0xab, 0xcd,
}
},
},
expectedGroup: Group{
CustomIconUUID: UUID{
0xde, 0xad, 0xbe, 0xef,
0xc0, 0xff, 0xee, 0xde,
0xed, 0x01, 0x23, 0x45,
0x67, 0x89, 0xab, 0xcd,
},
},
},
}
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
group := NewGroup(c.options...)
if group.Name != c.expectedGroup.Name {
t.Errorf(
"Did not receive expected name %+v, received %+v",
group.Name,
c.expectedGroup.Name,
)
}
})
}
}
func compareGroup(a, b Group) bool {
if !cmp.Equal(
a,
b,
cmp.AllowUnexported(Group{}),
cmpopts.IgnoreFields(Group{}, "Entries", "Groups"),
) {
return false
}
if !slices.EqualFunc(
a.Groups,
b.Groups,
compareGroup,
) {
return false
}
if !slices.EqualFunc(
a.Entries,
b.Entries,
compareEntry,
) {
return false
}
return true
}
func TestGroup_Clone(t *testing.T) {
cases := []struct {
title string
}{
{
title: "success",
},
}
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
group := NewGroup()
clone := group.Clone()
if &clone == &group {
t.Errorf("clone struct has the same pointer address")
}
if clone.UUID == group.UUID {
t.Errorf("clone did not receive a new UUID")
}
clone.UUID = group.UUID
if !compareGroup(clone, group) {
t.Errorf(
"Did not receive expected Group %+v, received %+v",
clone,
group,
)
}
})
}
}
func TestGroupSetKdbxFormatVersion(t *testing.T) {
cases := []struct {
title string
formattedInitValue bool
version formatVersion
expectedFormattedValue bool
}{
{
title: "initialized as v3, changed to v4",
formattedInitValue: true,
version: 4,
expectedFormattedValue: false,
},
{
title: "initialized as v4, changed to v3",
formattedInitValue: false,
version: 3,
expectedFormattedValue: true,
},
{
title: "initialized as v3, not changed",
formattedInitValue: true,
version: 3,
expectedFormattedValue: true,
},
{
title: "initialized as v4, not changed",
formattedInitValue: false,
version: 4,
expectedFormattedValue: false,
},
}
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
group := NewGroup(
WithGroupFormattedTime(c.formattedInitValue),
)
entry := NewEntry(
WithEntryFormattedTime(c.formattedInitValue),
)
group.Entries = append(group.Entries, entry)
subGroup := NewGroup(
WithGroupFormattedTime(c.formattedInitValue),
)
group.Groups = append(group.Groups, subGroup)
(&group).setKdbxFormatVersion(c.version)
// Takes a single time value as an example, as TimeData is independently tested.
if group.Times.CreationTime.Formatted != c.expectedFormattedValue {
t.Errorf("Failed to set root CreationTime formatted value accordingly")
}
if group.Groups[0].Times.CreationTime.Formatted != c.expectedFormattedValue {
t.Errorf("Failed to set sub group CreationTime formatted value accordingly")
}
if group.Entries[0].Times.CreationTime.Formatted != c.expectedFormattedValue {
t.Errorf("Failed to set entry CreationTime formatted value accordingly")
}
})
}
}
func prepareTimeWrapper(time time.Time) (*w.TimeWrapper, string) {
wrapper := &w.TimeWrapper{Time: time}
data, _ := wrapper.MarshalText()
text := string(data)
wrapper.UnmarshalText(data)
return wrapper, text
}
func TestGroupUnmarshalXML(t *testing.T) {
now := time.Now()
creationTime := now
lastModificationTime := now.AddDate(0, 0, 1)
lastAccessTime := now.AddDate(0, 0, 2)
expiryTime := now.AddDate(0, 0, 3)
locationChanged := now.AddDate(0, 0, 4)
creationTimeWrapper, creationTimeText := prepareTimeWrapper(creationTime)
lastModificationTimeWrapper, lastModificationTimeText := prepareTimeWrapper(lastModificationTime)
lastAccessTimeWrapper, lastAccessTimeText := prepareTimeWrapper(lastAccessTime)
expiryTimeWrapper, expiryTimeText := prepareTimeWrapper(expiryTime)
locationChangedWrapper, locationChangedText := prepareTimeWrapper(locationChanged)
cases := []struct {
title string
xmlData string
expectedGroup Group
expectedErr error
}{
{
title: "empty group",
xmlData: "<Group></Group>",
},
{
title: "child group first",
xmlData: "<Group><Group></Group><Entry></Entry></Group>",
expectedGroup: Group{
Entries: []Entry{{}},
Groups: []Group{{}},
groupChildOrder: groupChildOrderGroupFirst,
},
},
{
title: "child entry first",
xmlData: "<Group><Entry></Entry><Group></Group></Group>",
expectedGroup: Group{
Entries: []Entry{{}},
Groups: []Group{{}},
groupChildOrder: groupChildOrderEntryFirst,
},
},
{
title: "with the other fields",
xmlData: `
<Group>
<UUID>uJSFMJ8KrUSO0Qiivnk2Eg==</UUID>
<Name>kdbx4key</Name>
<Notes>notes</Notes>
<IconID>49</IconID>
<Times>
<CreationTime>` + creationTimeText + `</CreationTime>
<LastModificationTime>` + lastModificationTimeText + `</LastModificationTime>
<LastAccessTime>` + lastAccessTimeText + `</LastAccessTime>
<ExpiryTime>` + expiryTimeText + `</ExpiryTime>
<Expires>False</Expires>
<UsageCount>1</UsageCount>
<LocationChanged>` + locationChangedText + `</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence>abc</DefaultAutoTypeSequence>
<EnableAutoType>True</EnableAutoType>
<EnableSearching>False</EnableSearching>
<LastTopVisibleEntry>SnB29sd3a06jo6GR1BkGBQ==</LastTopVisibleEntry>
</Group>`,
expectedGroup: Group{
UUID: UUID{
0xb8, 0x94, 0x85, 0x30,
0x9f, 0xa, 0xad, 0x44,
0x8e, 0xd1, 0x8, 0xa2,
0xbe, 0x79, 0x36, 0x12,
},
Name: "kdbx4key",
Notes: "notes",
IconID: 49,
Times: TimeData{
CreationTime: creationTimeWrapper,
LastModificationTime: lastModificationTimeWrapper,
LastAccessTime: lastAccessTimeWrapper,
ExpiryTime: expiryTimeWrapper,
Expires: w.NewBoolWrapper(false),
UsageCount: 1,
LocationChanged: locationChangedWrapper,
},
IsExpanded: w.NewBoolWrapper(true),
DefaultAutoTypeSequence: "abc",
EnableAutoType: w.NewNullableBoolWrapper(true),
EnableSearching: w.NewNullableBoolWrapper(false),
LastTopVisibleEntry: "SnB29sd3a06jo6GR1BkGBQ==",
},
},
}
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
decoder := xml.NewDecoder(bytes.NewBufferString(c.xmlData))
var group Group
err := decoder.Decode(&group)
if !errors.Is(c.expectedErr, err) {
t.Errorf("Expected %#v, received %#v", c.expectedErr, err)
}
assert.Equal(t, c.expectedGroup, group, "The groups should be identical")
})
}
}