-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_test.go
374 lines (351 loc) · 14.8 KB
/
key_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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package registry
import (
"encoding/hex"
"reflect"
"strings"
"testing"
)
func TestKey_ReadSubKeyNames(t *testing.T) {
type args struct {
filename string
path string
n int
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{name: "testdata/NTUSER.DAT", args: args{filename: "testdata/NTUSER.DAT", path: "SOFTWARE", n: -1}, want: []string{"Google", "Microsoft", "Policies"}},
{name: "testdata/NTUSER.DAT", args: args{filename: "testdata/NTUSER.DAT", path: "SOFTWARE", n: 1}, want: []string{"Google"}},
{name: "testdata/NTUSER.DAT", args: args{filename: "testdata/NTUSER.DAT", path: "SOFTWARE", n: 100}, want: []string{"Google", "Microsoft", "Policies"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, err := OpenKey(tt.args.filename, tt.args.path)
if err != nil {
t.Errorf("OpenKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
defer k.Close()
got, err := k.ReadSubKeyNames(tt.args.n)
if (err != nil) != tt.wantErr {
t.Errorf("Key.ReadSubKeyNames() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Key.ReadSubKeyNames()\n[%v]\nwant\n[%v]", strings.Join(got, ", "), strings.Join(tt.want, ", "))
}
})
}
}
func TestKey_ReadValueNames(t *testing.T) {
type args struct {
filename string
path string
n int
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{name: `testdata/NTUSER.DAT SOFTWARE\Microsoft\CTF\Assemblies\0x00000816\{34745C63-B2F0-4784-8B67-5E12C8701A31}`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Microsoft\CTF\Assemblies\0x00000816\{34745C63-B2F0-4784-8B67-5E12C8701A31}`, n: -1}, want: []string{"Default", "KeyboardLayout", "Profile"}},
{name: `testdata/NTUSER.DAT SOFTWARE\Microsoft\CTF\Assemblies\0x00000816\{34745C63-B2F0-4784-8B67-5E12C8701A31}`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Microsoft\CTF\Assemblies\0x00000816\{34745C63-B2F0-4784-8B67-5E12C8701A31}`, n: 1}, want: []string{"Default"}},
{name: `testdata/NTUSER.DAT SOFTWARE\Microsoft\CTF\Assemblies\0x00000816\{34745C63-B2F0-4784-8B67-5E12C8701A31}`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Microsoft\CTF\Assemblies\0x00000816\{34745C63-B2F0-4784-8B67-5E12C8701A31}`, n: 5}, want: []string{"Default", "KeyboardLayout", "Profile"}},
{name: `testdata/NTUSER.DAT`, args: args{filename: "testdata/NTUSER.DAT", path: ``, n: 5}, want: []string{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, err := OpenKey(tt.args.filename, tt.args.path)
if err != nil {
t.Errorf("OpenKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
defer k.Close()
got, err := k.ReadValueNames(tt.args.n)
if (err != nil) != tt.wantErr {
t.Errorf("Key.ReadValueNames() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Key.ReadValueNames() = [%v], want [%v]", strings.Join(got, ", "), strings.Join(tt.want, ", "))
}
})
}
}
func TestKey_GetStringValue(t *testing.T) {
type args struct {
filename string
path string
valuename string
}
tests := []struct {
name string
args args
wantVal string
wantValtype uint32
wantErr bool
err error
}{
{name: `testdata/NTUSER.DAT SOFTWARE\Google\Chrome\NativeMessagingHosts\com.microsoft.browsercore`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Google\Chrome\NativeMessagingHosts\com.microsoft.browsercore`, valuename: "(default)"}, wantVal: `C:\Program Files\Windows Security\BrowserCore\manifest.json`, wantValtype: REG_SZ},
{name: `testdata/NTUSER.DAT Environment`, args: args{filename: "testdata/NTUSER.DAT", path: `Environment`, valuename: "Path"}, wantVal: `%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;`, wantValtype: REG_EXPAND_SZ},
{name: `testdata/NTUSER.DAT Control Panel\PowerCfg\PowerPolicies\5 Policies`, args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\PowerCfg\PowerPolicies\5`, valuename: "Policies"}, wantValtype: REG_BINARY, wantErr: true, err: ErrUnexpectedType},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, err := OpenKey(tt.args.filename, tt.args.path)
if err != nil {
t.Errorf("OpenKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
defer k.Close()
gotVal, gotValtype, err := k.GetStringValue(tt.args.valuename)
if (err != nil) != tt.wantErr || err != tt.err {
t.Errorf("Key.GetStringValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotVal != tt.wantVal {
t.Errorf("Key.GetStringValue() gotVal = %v, want %v", gotVal, tt.wantVal)
}
if gotValtype != tt.wantValtype {
t.Errorf("Key.GetStringValue() gotValtype = `%v`, want `%v`", gotValtype, tt.wantValtype)
}
})
}
}
func TestKey_GetIntegerValue(t *testing.T) {
type args struct {
filename string
path string
valuename string
}
tests := []struct {
name string
args args
wantVal uint64
wantValtype uint32
wantErr bool
err error
}{
{name: `testdata/NTUSER.DAT SOFTWARE\Microsoft\InputPersonalization`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Microsoft\InputPersonalization`, valuename: "RestrictImplicitInkCollection"}, wantVal: 0, wantValtype: REG_DWORD_LITTLE_ENDIAN},
{name: `testdata/NTUSER.DAT Classes\*\shell\UpdateEncryptionSettingsWork ImpliedSelectionModel`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Microsoft\InputPersonalization\TrainedDataStore`, valuename: "HarvestContacts"}, wantVal: 1, wantValtype: REG_DWORD_LITTLE_ENDIAN},
{name: `testdata/NTUSER.DAT Control Panel\PowerCfg\PowerPolicies\5 Policies`, args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\PowerCfg\PowerPolicies\5`, valuename: "Policies"}, wantValtype: REG_BINARY, wantErr: true, err: ErrUnexpectedType},
// TODO: REG_DWORD_BIG_ENDIAN support
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, err := OpenKey(tt.args.filename, tt.args.path)
if err != nil {
t.Errorf("OpenKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
defer k.Close()
gotVal, gotValtype, err := k.GetIntegerValue(tt.args.valuename)
if (err != nil) != tt.wantErr || err != tt.err {
t.Errorf("Key.GetIntegerValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotVal != tt.wantVal {
t.Errorf("Key.GetIntegerValue() gotVal = %v, want %v", gotVal, tt.wantVal)
}
if gotValtype != tt.wantValtype {
t.Errorf("Key.GetIntegerValue() gotValtype = %v, want %v", gotValtype, tt.wantValtype)
}
})
}
}
func TestKey_GetValue(t *testing.T) {
type args struct {
filename string
path string
valuename string
}
tests := []struct {
name string
args args
wantN int
wantVal []byte
wantValtype uint32
wantErr bool
}{
{name: `testdata/NTUSER.DAT Control Panel\Input Method\Hot Keys\00000010`, args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\Input Method\Hot Keys\00000010`, valuename: "Key Modifiers"}, wantVal: []byte{'\x02', '\xc0', '\x00', '\x00'}, wantValtype: REG_BINARY, wantN: 4},
{name: `testdata/NTUSER.DAT SOFTWARE\Google\Chrome\NativeMessagingHosts\com.microsoft.browsercore`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Google\Chrome\NativeMessagingHosts\com.microsoft.browsercore`, valuename: "(default)"}, wantVal: []byte(`C:\Program Files\Windows Security\BrowserCore\manifest.json`), wantValtype: REG_SZ, wantN: 59},
{name: `testdata/NTUSER.DAT SOFTWARE\Microsoft\InputPersonalization`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Microsoft\InputPersonalization`, valuename: "RestrictImplicitInkCollection"}, wantVal: []byte{0, 0, 0, 0}, wantValtype: REG_DWORD_LITTLE_ENDIAN, wantN: dataSizeFromType(REG_DWORD_LITTLE_ENDIAN)},
{name: `testdata/NTUSER.DAT Control Panel\International\User Profile`, args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\International\User Profile`, valuename: "Languages"}, wantVal: append([]byte(`pt-PT`), 0), wantValtype: REG_MULTI_SZ, wantN: 6},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, err := OpenKey(tt.args.filename, tt.args.path)
if err != nil {
t.Errorf("OpenKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
defer k.Close()
gotN, gotValtype, err := k.GetValue(tt.args.valuename, nil)
buf := make([]byte, gotN)
gotN, gotValtype, err = k.GetValue(tt.args.valuename, buf)
if (err != nil) != tt.wantErr {
t.Errorf("Key.GetValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotN != tt.wantN {
t.Errorf("Key.GetValue() gotN = %v, want %v", gotN, tt.wantN)
}
if gotValtype != tt.wantValtype {
t.Errorf("Key.GetValue() gotValtype = %v, want %v", gotValtype, tt.wantValtype)
}
if !reflect.DeepEqual(tt.wantVal, buf) {
t.Errorf("Key.GetValue()\nval = %v\nwant = %v\n%v%v", buf, tt.wantVal, hex.Dump(buf), hex.Dump(tt.wantVal))
}
})
}
}
func TestKey_GetValue2(t *testing.T) {
type args struct {
filename string // registry path
path string // Key path
valuename string // Key value name
buf []byte
}
tests := []struct {
name string
args args
wantN int
wantVal []byte
wantValtype uint32
wantErr bool
err error
}{
{
name: `testdata/NTUSER.DAT Control Panel\Input Method\Hot Keys\00000010`,
args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\Input Method\Hot Keys\00000010`, valuename: "Key Modifiers"},
wantValtype: REG_BINARY,
wantN: 4,
},
{
name: `testdata/NTUSER.DAT Control Panel\Input Method\Hot Keys\00000010`,
args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\Input Method\Hot Keys\00000010`, valuename: "Key Modifiers", buf: make([]byte, 4)},
wantVal: []byte{'\x02', '\xc0', '\x00', '\x00'},
wantValtype: REG_BINARY,
wantN: 4,
},
{
name: `testdata/NTUSER.DAT SOFTWARE\Google\Chrome\NativeMessagingHosts\com.microsoft.browsercore`,
args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Google\Chrome\NativeMessagingHosts\com.microsoft.browsercore`, valuename: "(default)"},
wantValtype: REG_SZ,
wantN: 59,
},
{
name: `testdata/NTUSER.DAT excess memory`,
args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Google\Chrome\NativeMessagingHosts\com.microsoft.browsercore`, valuename: "(default)", buf: make([]byte, 62)},
wantVal: append([]byte(`C:\Program Files\Windows Security\BrowserCore\manifest.json`), 0, 0, 0),
wantValtype: REG_SZ,
wantN: 59,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, err := OpenKey(tt.args.filename, tt.args.path)
if err != nil {
t.Errorf("OpenKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
defer k.Close()
gotN, gotValtype, err := k.GetValue(tt.args.valuename, tt.args.buf)
if (err != nil) != tt.wantErr || err != tt.err {
t.Errorf("Key.GetValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotN != tt.wantN {
t.Errorf("Key.GetValue() gotN = %v, want %v", gotN, tt.wantN)
}
if gotValtype != tt.wantValtype {
t.Errorf("Key.GetValue() gotValtype = %v, want %v", gotValtype, tt.wantValtype)
}
if err == nil && !reflect.DeepEqual(tt.wantVal, tt.args.buf) {
t.Errorf("Key.GetValue()\nval = %v\nwant = %v\n%v%v", tt.args.buf, tt.wantVal, hex.Dump(tt.args.buf), hex.Dump(tt.wantVal))
}
})
}
}
func TestKey_GetStringsValue(t *testing.T) {
type args struct {
filename string
path string
valuename string
}
tests := []struct {
name string
args args
wantVal []string
wantValtype uint32
wantErr bool
err error
}{
{name: `testdata/NTUSER.DAT Control Panel\International\User Profile`, args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\International\User Profile`, valuename: "Languages"}, wantVal: []string{`pt-PT`}, wantValtype: REG_MULTI_SZ},
{name: `testdata/NTUSER.DAT Control Panel\PowerCfg\PowerPolicies\5 Policies`, args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\PowerCfg\PowerPolicies\5`, valuename: "Policies"}, wantValtype: REG_BINARY, wantErr: true, err: ErrUnexpectedType}, {name: `testdata/NTUSER.DAT Control Panel\PowerCfg\PowerPolicies\5 Policies`, args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\PowerCfg\PowerPolicies\5`, valuename: "Policies"}, wantValtype: REG_BINARY, wantErr: true, err: ErrUnexpectedType},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, err := OpenKey(tt.args.filename, tt.args.path)
if err != nil {
t.Errorf("OpenKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
defer k.Close()
gotVal, gotValtype, err := k.GetStringsValue(tt.args.valuename)
if (err != nil) != tt.wantErr || err != tt.err {
t.Errorf("Key.GetStringsValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotVal, tt.wantVal) {
t.Errorf("Key.GetStringsValue() gotVal = %v, want %v", gotVal, tt.wantVal)
}
if gotValtype != tt.wantValtype {
t.Errorf("Key.GetStringsValue() gotValtype = %v, want %v", gotValtype, tt.wantValtype)
}
})
}
}
func TestKey_GetBinaryValue(t *testing.T) {
type args struct {
filename string
path string
valuename string
}
tests := []struct {
name string
args args
wantVal []byte
wantValtype uint32
wantErr bool
err error
}{
{name: `testdata/NTUSER.DAT Control Panel\Input Method\Hot Keys\00000010`, args: args{filename: "testdata/NTUSER.DAT", path: `Control Panel\Input Method\Hot Keys\00000010`, valuename: "Key Modifiers"}, wantVal: []byte{'\x02', '\xc0', '\x00', '\x00'}, wantValtype: REG_BINARY},
{name: `testdata/NTUSER.DAT Classes\*\shell\UpdateEncryptionSettingsWork ImpliedSelectionModel`, args: args{filename: "testdata/NTUSER.DAT", path: `SOFTWARE\Microsoft\InputPersonalization\TrainedDataStore`, valuename: "HarvestContacts"}, wantValtype: REG_DWORD_LITTLE_ENDIAN, wantErr: true, err: ErrUnexpectedType},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, err := OpenKey(tt.args.filename, tt.args.path)
if err != nil {
t.Errorf("OpenKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
defer k.Close()
gotVal, gotValtype, err := k.GetBinaryValue(tt.args.valuename)
if (err != nil) != tt.wantErr || err != tt.err {
t.Errorf("Key.GetBinaryValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotVal, tt.wantVal) {
t.Errorf("Key.GetBinaryValue() gotVal = %v, want %v", gotVal, tt.wantVal)
}
if gotValtype != tt.wantValtype {
t.Errorf("Key.GetBinaryValue() gotValtype = %v, want %v", gotValtype, tt.wantValtype)
}
})
}
}