-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdatabase_test.go
193 lines (175 loc) · 4.04 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
package gokeepasslib
import (
"bytes"
"errors"
"log"
"reflect"
"testing"
)
func TestNewDatabase(t *testing.T) {
cases := []struct {
title string
options []DatabaseOption
expectedDatabase *Database
}{
{
title: "without options",
expectedDatabase: &Database{
Options: &DBOptions{
ValidateHashes: true,
},
},
},
{
title: "with multiple options",
options: []DatabaseOption{
WithDatabaseFormattedTime(false),
func(c *Database) {
c.Options.ValidateHashes = false
},
},
expectedDatabase: &Database{
Options: &DBOptions{
ValidateHashes: false,
},
},
},
}
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
database := NewDatabase(c.options...)
if database.Options.ValidateHashes != c.expectedDatabase.Options.ValidateHashes {
t.Errorf(
"Did not receive expected database %+v, received %+v",
database.Options.ValidateHashes,
c.expectedDatabase.Options.ValidateHashes,
)
}
})
}
}
func TestDatabase_GetStreamManager(t *testing.T) {
cases := []struct {
title string
database *Database
expectedManager *StreamManager
expectedErr error
}{
{
title: "when db header is empty",
database: &Database{},
expectedManager: nil,
expectedErr: nil,
},
{
title: "when header FileHeaders is empty",
database: &Database{
Header: &DBHeader{},
},
expectedManager: nil,
expectedErr: nil,
},
{
title: "when db content is empty (kdbx4)",
database: &Database{
Header: &DBHeader{
Signature: &Signature{
MajorVersion: 4,
},
},
},
expectedManager: nil,
expectedErr: ErrInvalidDatabaseOrCredentials,
},
{
title: "when content InnerHeader is empty (kdbx4)",
database: &Database{
Header: &DBHeader{
Signature: &Signature{
MajorVersion: 4,
},
},
Content: &DBContent{},
},
expectedManager: nil,
expectedErr: ErrInvalidDatabaseOrCredentials,
},
{
title: "when content InnerHeader.InnerRandomStreamKey is empty (kdbx4)",
database: &Database{
Header: &DBHeader{
Signature: &Signature{
MajorVersion: 4,
},
},
Content: &DBContent{
InnerHeader: &InnerHeader{
InnerRandomStreamKey: nil,
},
},
},
expectedManager: nil,
expectedErr: ErrInvalidDatabaseOrCredentials,
},
{
title: "when header FileHeaders.ProtectedStreamKey is empty (kdbx3)",
database: &Database{
Header: &DBHeader{
Signature: &Signature{
MajorVersion: 3,
},
FileHeaders: &FileHeaders{
ProtectedStreamKey: nil,
},
},
},
expectedManager: nil,
expectedErr: ErrInvalidDatabaseOrCredentials,
},
}
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
manager, err := c.database.GetStreamManager()
if !reflect.DeepEqual(manager, c.expectedManager) {
t.Fatalf("Expected %v, received %v", c.expectedErr, err)
}
if !errors.Is(err, c.expectedErr) {
t.Fatalf("Expected %v, received %v", c.expectedErr, err)
}
})
}
}
func ExampleNewDatabase_kdbxv3() {
buf := bytes.NewBuffer([]byte{})
// create the new database
db := NewDatabase(
WithDatabaseKDBXVersion3(),
)
db.Content.Meta.DatabaseName = "KDBX4"
db.Credentials = NewPasswordCredentials("supersecret")
// Lock entries using stream cipher
db.LockProtectedEntries()
// and encode it into the file
keepassEncoder := NewEncoder(buf)
if err := keepassEncoder.Encode(db); err != nil {
panic(err)
}
log.Printf("Wrote kdbx file to buffer")
}
func ExampleNewDatabase_kdbxv4() {
buf := bytes.NewBuffer([]byte{})
// create the new database
db := NewDatabase(
WithDatabaseKDBXVersion4(),
)
db.Content.Meta.DatabaseName = "KDBX4"
db.Credentials = NewPasswordCredentials("supersecret")
// Lock entries using stream cipher
db.LockProtectedEntries()
// and encode it into the file
keepassEncoder := NewEncoder(buf)
if err := keepassEncoder.Encode(db); err != nil {
panic(err)
}
log.Printf("Wrote kdbx file to buffer")
}