-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.js
1229 lines (946 loc) · 36.5 KB
/
test.js
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const {Registry} = require('./index.js')
const {assert} = require('chai')
// I don't know why chai doesn't have this
assert.isError = (maybeErr, message = 'expected to be error') => {
assert.instanceOf(maybeErr, Error, message)
}
assert.isNotError = (maybeErr, message = 'expected to not be error') => {
if (maybeErr !== undefined)
assert.isFalse(maybeErr instanceof Error, message)
}
assert.willThrow = async promise => {
assert.isError(await promise.catch(err => err), 'expected to throw')
}
assert.willNotThrow = async promise => {
assert.isNotError(await promise.catch(err => err), 'expected to not throw')
}
var OW_PATH = 'HKLM\\SOFTWARE\\Overwatch'
var BW_PATH = 'HKLM\\SOFTWARE\\Overwatch\\Blackwatch'
const PATH = 'HKLM\\SOFTWARE\\MikeKovarik'
const PATH_NONEXISTENT = 'HKCR\\Some\\made\\up\\path\\that\\doesnt\\exist'
const PATH_BLOCK = 'HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Blocked'
function noop() {}
var resolveError = err => err
describe('Registry static', () => {
describe('.getKeys', () => {
it('is function', async () => {
assert.isFunction(Registry.getKeys)
})
it('returns undefined if the key path does not exist', async () => {
var keys = await Registry.getKeys(PATH_NONEXISTENT)
assert.equal(keys, undefined)
})
it('returns an array', async () => {
var keys = await Registry.getKeys('HKCR\\Directory\\Background')
assert.isArray(keys)
})
it('accepts full hive names', async () => {
var keys = await Registry.getKeys('HKEY_CLASSES_ROOT\\Directory\\Background')
assert.isArray(keys)
})
it('returns empty array if no subkeys', async () => {
// note: .txt pobably always has ShellNew subkey. It's best bet for writing test without
// having to employ other methods like setKey()
var keys = await Registry.getKeys('HKCR\\.txt\\ShellNew')
assert.isEmpty(keys)
})
it('returned array should contain subkeys', async () => {
var keys = await Registry.getKeys('HKCR\\Directory\\Background')
for (var item of keys) {
assert.isFalse(item.toUpperCase().startsWith('HKEY_CLASSES_ROOT'))
}
assert.include(keys, 'shell')
assert.include(keys, 'shellex')
})
it('returns an array of lowercase subkeys by default', async () => {
var keys = await Registry.getKeys('HKCR\\Directory\\Background\\shellex')
assert.isNotEmpty(keys)
assert.include(keys, 'contextmenuhandlers')
})
it('returns an array of case sensitive subkeys if requested', async () => {
var keys = await Registry.getKeys('HKCR\\Directory\\Background\\shellex', {lowercase: false})
assert.isNotEmpty(keys)
assert.include(keys, 'ContextMenuHandlers')
})
describe('complex form', () => {
it('returns an array of strings', async () => {
var keys = await Registry.getKeys('HKCR\\Directory\\Background', {format: 'complex'})
assert.isArray(keys)
assert.isNotEmpty(keys)
var item = keys[0]
assert.isString(item)
})
it('subkeys start with long hive names', async () => {
var keys = await Registry.getKeys('HKCR\\Directory\\Background', {format: 'complex'})
for (var item of keys) {
assert.isTrue(item.toUpperCase().startsWith('HKEY_CLASSES_ROOT'))
}
})
it('includes correct subkeys', async () => {
var keys = await Registry.getKeys('HKCR\\Directory\\Background', {format: 'complex'})
keys = keys.map(key => key.split('\\').pop())
assert.include(keys, 'shell')
assert.include(keys, 'shellex')
})
it('includes correct subkeys 2', async () => {
var keys = await Registry.getKeys('HKCR\\Directory\\Background\\shellex', {format: 'complex'})
keys = keys.map(path => path.toLowerCase())
assert.include(keys, 'hkey_classes_root\\directory\\background\\shellex\\contextmenuhandlers')
})
})
})
describe('.getValues', () => {
it('is function', async () => {
assert.isFunction(Registry.getValues)
})
it('returns undefined if the key path does not exist', async () => {
var values = await Registry.getValues(PATH_NONEXISTENT)
assert.equal(values, undefined)
})
it('returns an object', async () => {
var values = await Registry.getValues('HKCR\\*')
assert.isObject(values)
})
it('returns empty object if no entries', async () => {
var values = await Registry.getValues('HKCR\\*\\shell')
assert.isEmpty(values)
})
it('undefined values should be properly recognized as undefined', async () => {
var values = await Registry.getValues('HKCR\\*')
assert.equal(values[''], undefined)
})
it('empty string values should be properly recognized as empty string', async () => {
var values = await Registry.getValues('HKCR\\*')
assert.equal(values['alwaysshowext'], '')
})
// NOTE: paths and value names are key insensitive and whenever its queries
// reg command will always respond. But when
it('value name is case insensitive by default', async () => {
var result = await Registry.getValues('HKCR\\*')
assert.exists(result['alwaysshowext'])
result = await Registry.getValues('HKCR\\*')
assert.notExists(result['AlwaysShowExt'])
})
it('can be switched to case sensitive', async () => {
var result = await Registry.getValues('HKCR\\*', {lowercase: false})
assert.exists(result['AlwaysShowExt'])
result = await Registry.getValues('HKCR\\*', {lowercase: false})
assert.notExists(result['alwaysshowext'])
})
describe('complex form', () => {
it('returns an array of objects', async () => {
var values = await Registry.getValues('HKCR\\*', {format: 'complex'})
assert.isArray(values)
var entry = values[0]
assert.isObject(entry)
})
it('entries are represented by objects', async () => {
var values = await Registry.getValues('HKCR\\*', {format: 'complex'})
var entry = values[0]
assert.isString(entry.name)
assert.isTrue('data' in entry)
assert.isString(entry.type)
})
})
})
describe('.getValue', () => {
it('is function', async () => {
assert.isFunction(Registry.getValue)
})
it('returns undefined if the key path does not exist', async () => {
var result = await Registry.getValue(PATH_NONEXISTENT)
assert.equal(result, undefined)
})
it('returns value of default entry', async () => {
var result = await Registry.getValue('HKCR\\Directory', '')
assert.equal(result, 'File Folder')
})
it('omitted second argument (name) falls back to default name (empty string)', async () => {
var result = await Registry.getValue('HKCR\\Directory')
assert.equal(result, 'File Folder')
})
it('returns empty string if the value entry has empty string data', async () => {
var result = await Registry.getValue('HKCR\\*', 'alwaysshowext')
assert.equal(result, '')
})
it('returns string if the value entry has string data', async () => {
var result = await Registry.getValue('HKCR\\*', 'AlwaysShowExt')
assert.isString(result)
})
// NOTE: reg command is insensitive
it('value name is case insensitive by default', async () => {
var result = await Registry.getValue('HKCR\\*', 'AlwaysShowExt')
assert.exists(result)
result = await Registry.getValue('HKCR\\*', 'alwaysshowext')
assert.exists(result)
})
//it('can be switched to case sensitive', async () => {
// var result = await Registry.getValue('HKCR\\*', 'AlwaysShowExt', true)
// assert.exists(result)
// result = await Registry.getValue('HKCR\\*', 'alwaysshowext', true)
// assert.notExists(result)
//})
it('returns undefined if the value entry exists, but does not value any data', async () => {
var result = await Registry.getValue('HKCR\\*', '')
assert.equal(result, undefined)
})
describe('output type casting', () => {
const PATH = 'HKLM\\SOFTWARE\\MikeKovarik'
it('reads REG_SZ as string', async () => {
var NAME = 'casting-1'
await Registry.setValue(PATH, NAME, 'some string', 'REG_SZ')
assert.isString(await Registry.getValue(PATH, NAME))
})
it('reads REG_DWORD as number', async () => {
var NAME = 'casting-2'
await Registry.setValue(PATH, NAME, 1234, 'REG_DWORD')
assert.isNumber(await Registry.getValue(PATH, NAME))
})
it('reads REG_QWORD as string in 0x format', async () => {
var NAME = 'casting-3'
await Registry.setValue(PATH, NAME, 1234, 'REG_QWORD')
var data = await Registry.getValue(PATH, NAME)
assert.isString(data)
assert.isTrue(data.startsWith('0x'))
})
it('reads REG_BINARY as buffer', async () => {
var NAME = 'casting-4'
await Registry.setValue(PATH, NAME, Buffer.alloc(0), 'REG_BINARY')
var data = await Registry.getValue(PATH, NAME)
assert.equal(data.constructor.name, 'Buffer')
})
it('reads REG_MULTI_SZ as array', async () => {
var NAME = 'casting-4'
await Registry.setValue(PATH, NAME, [], 'REG_MULTI_SZ')
assert.isArray(await Registry.getValue(PATH, NAME))
})
it('string to REG_BINARY', async () => {
var NAME = 'binary-value'
var buffer = Buffer.from('Experience tranquility')
await Registry.deleteValue(PATH, NAME)
await Registry.setValue(PATH, NAME, buffer.toString(), 'REG_BINARY')
var {data, type} = await Registry.getValue(PATH, NAME, {format: 'complex'})
assert.equal(type, 'REG_BINARY')
assert.equal(data.toString(), buffer.toString())
})
})
describe('complex form', () => {
it('returns an object', async () => {
var entry = await Registry.getValue('HKCR\\*', 'AlwaysShowExt', {format: 'complex'})
assert.isObject(entry)
})
it(`has 'name', 'type' and 'data' fields`, async () => {
var entry = await Registry.getValue('HKCR\\*', 'AlwaysShowExt', {format: 'complex'})
assert.isString(entry.name)
assert.isTrue('data' in entry)
assert.isString(entry.type)
})
})
})
describe('.getKey', () => {
it('is function', async () => {
assert.equal(typeof Registry.getKey, 'function')
})
it('returns undefined if the key path does not exist', async () => {
var result = await Registry.getKey(PATH_NONEXISTENT)
assert.equal(typeof result, 'undefined')
})
it('returns lowercase keys', async () => {
var result = await Registry.getKey('HKCR\\Directory\\shellex', {lowercase: true})
assert.isObject(result['contextmenuhandlers'])
assert.notExists(result['ContextMenuHandlers'])
})
it('can return case sensitive keys', async () => {
var result = await Registry.getKey('HKCR\\Directory\\shellex', {lowercase: false})
assert.isObject(result['ContextMenuHandlers'])
assert.notExists(result['contextmenuhandlers'])
})
describe('simple format', () => {
it('returns simple format', async () => {
var result = await Registry.getKey('HKCR\\Directory')
assert.isObject(result)
assert.isObject(result.$values)
})
it('has keys', async () => {
var result = await Registry.getKey('HKCR\\Directory')
assert.exists(result['background'])
assert.exists(result['shell'])
assert.exists(result['shellex'])
})
it('has values', async () => {
var result = await Registry.getKey('HKCR\\Directory')
assert.equal(result.$values[''], 'File Folder')
})
it('empty default value does show up in $values', async () => {
await Registry.delete(OW_PATH)
await Registry.set(OW_PATH)
var result = await Registry.getKey(OW_PATH)
assert.isNotEmpty(Object.keys(result.$values))
assert.isTrue(Object.keys(result.$values).includes(''))
})
it('undefined default value does not show up in $values', async () => {
await Registry.delete(OW_PATH)
await Registry.set(OW_PATH)
await Registry.delete(OW_PATH, '')
var result = await Registry.getKey(OW_PATH)
assert.isEmpty(Object.keys(result.$values))
})
it('can work recursively', async () => {
var result = await Registry.getKey('HKCR\\Directory\\Background', true)
assert.isObject(result['shellex'])
assert.isObject(result['shellex']['contextmenuhandlers'])
})
})
describe('complex format', () => {
it('returns complex format', async () => {
var result = await Registry.getKey('HKCR\\Directory', {format: 'complex'})
assert.isObject(result)
assert.isObject(result.keys) // TODO
assert.isArray(result.values)
})
it('has keys', async () => {
var result = await Registry.getKey('HKCR\\Directory', {format: 'complex'})
assert.exists(result.keys['background'])
assert.exists(result.keys['shell'])
assert.exists(result.keys['shellex'])
})
it('has values', async () => {
var result = await Registry.getKey('HKCR\\Directory', {format: 'complex'})
var defaultEntry = result.values.filter(entryObject => entryObject.name === '')[0]
assert.isObject(defaultEntry)
assert.equal(defaultEntry.name, '')
assert.equal(defaultEntry.data, 'File Folder')
assert.equal(defaultEntry.type, 'REG_SZ')
})
it('empty default value does show up in $values', async () => {
await Registry.delete(OW_PATH)
await Registry.set(OW_PATH)
var result = await Registry.getKey(OW_PATH, {format: 'complex'})
assert.isNotEmpty(result.values)
assert.isTrue(result.values[0].name === '')
})
it('undefined default value does not show up in $values', async () => {
await Registry.delete(OW_PATH)
await Registry.set(OW_PATH)
await Registry.delete(OW_PATH, '')
var result = await Registry.getKey(OW_PATH, {format: 'complex'})
assert.isEmpty(result.values)
})
it('can work recursively', async () => {
var result = await Registry.getKey('HKCR\\Directory\\Background', true, {format: 'complex'})
assert.isObject(result.keys['shellex'])
assert.isObject(result.keys['shellex'].keys)
assert.isObject(result.keys['shellex'].keys['contextmenuhandlers'])
})
})
})
describe('.get', () => {
it('is function', async () => {
assert.isFunction(Registry.get)
})
it('returns undefined if the key path does not exist', async () => {
var result = await Registry.get(PATH_NONEXISTENT)
assert.equal(result, undefined)
})
it('only using one parameter serves as alias for .getKey', async () => {
var result = await Registry.get('HKCR\\*')
assert.isObject(result)
assert.isString(result.$values['alwaysshowext'])
})
it('using two parameters serves as alias for .getValue', async () => {
var result = await Registry.get('HKCR\\*', 'alwaysshowext')
assert.isString(result)
})
})
describe('.hasKey', () => {
it('is function', async () => {
assert.isFunction(Registry.hasKey)
})
it('returns true if key at given path exists', async () => {
assert.isTrue(await Registry.hasKey('HKLM\\SOFTWARE'))
})
it(`returns false if key at given path doesn't exist`, async () => {
assert.isFalse(await Registry.hasKey('HKLM\\SOFTWARE\\MikeKovarik\\non\\existent'))
})
})
describe('.hasValue', () => {
it('is function', async () => {
assert.isFunction(Registry.hasValue)
})
it('returns true if value entry exists at given path', async () => {
assert.isTrue(await Registry.hasValue('HKCR\\*', 'AlwaysShowExt'))
})
it(`returns false if value entry doesn't exist at given path`, async () => {
assert.isFalse(await Registry.hasValue('HKCR\\*', 'foo-bar non existent'))
})
})
describe('.has', () => {
it('is function', async () => {
assert.isFunction(Registry.has)
})
it('returns true if key at given path exists', async () => {
assert.isTrue(await Registry.has('HKLM\\SOFTWARE'))
})
it(`returns false if key at given path doesn't exist`, async () => {
assert.isFalse(await Registry.has('HKLM\\SOFTWARE\\MikeKovarik\\non\\existent'))
})
it('returns true if value entry exists at given path', async () => {
assert.isTrue(await Registry.has('HKCR\\*', 'AlwaysShowExt'))
})
it(`returns false if value entry doesn't exist at given path`, async () => {
assert.isFalse(await Registry.has('HKCR\\*', 'foo-bar non existent'))
})
})
describe('.deleteKey', () => {
it('is function', async () => {
assert.isFunction(Registry.deleteKey)
})
it('deletes the key at given path', async () => {
await Registry.setKey(PATH)
await Registry.deleteKey(PATH)
assert.isFalse(await Registry.hasKey(PATH))
})
it('deletion of an existing key does not throw', async () => {
await Registry.setKey(PATH)
await assert.willNotThrow(Registry.deleteKey(PATH))
})
it('deletion of nonexisting key does not throw', async () => {
await Registry.deleteKey('HKLM\\SOFTWARE\\MikeKovarik\\some\\random\\key')
})
})
describe('.deleteValue', () => {
it('is function', async () => {
assert.isFunction(Registry.deleteValue)
})
it('deletes the value at given path', async () => {
const NAME = 'deleteme'
await Registry.set(PATH, NAME, 'this is the value entry to be deleted')
assert.isTrue(await Registry.hasValue(PATH, NAME))
await Registry.deleteValue(PATH, NAME)
assert.isFalse(await Registry.hasValue(PATH, NAME))
})
it('deletion of an existing value entry does not throw', async () => {
const NAME = 'deleteme'
await Registry.set(PATH, NAME, 'this is the value entry to be deleted')
await assert.willNotThrow(Registry.deleteValue(PATH, NAME))
})
//it('deletion of nonexisting value entry throws', async () => {
it('deletion of nonexisting value does not throw', async () => {
const NAME = 'deleteme'
await Registry.deleteValue(PATH, NAME).catch(noop)
await Registry.deleteValue(PATH, NAME)
})
})
describe('.delete', () => {
it('is function', async () => {
assert.isFunction(Registry.delete)
})
it('only using one parameter serves as alias for .deleteKey', async () => {
await Registry.setKey(PATH)
assert.isTrue(await Registry.hasKey(PATH))
await Registry.delete(PATH)
assert.isFalse(await Registry.hasKey(PATH))
})
it('using two parameters serves as alias for .deleteValue', async () => {
var NAME = 'deleteme'
await Registry.set(PATH, NAME, 'Embrace the Iris')
assert.isTrue(await Registry.hasKey(PATH))
assert.isTrue(await Registry.hasValue(PATH, NAME))
await Registry.delete(PATH, NAME)
assert.isTrue(await Registry.hasKey(PATH))
assert.isFalse(await Registry.hasValue(PATH, NAME))
})
})
describe('.setKey', () => {
it('is function', async () => {
assert.isFunction(Registry.setKey)
})
it('creates new key', async () => {
const PATH = 'HKLM\\SOFTWARE\\MikeKovarik\\nonexistent'
await Registry.deleteKey(PATH).catch(noop)
await Registry.setKey(PATH)
assert.isTrue(await Registry.hasKey(PATH))
})
it('overwrites already existing key', async () => {
const PATH = 'HKLM\\SOFTWARE\\MikeKovarik\\existing'
await Registry.setKey(PATH).catch(noop)
await Registry.setKey(PATH)
assert.isTrue(await Registry.hasKey(PATH))
})
})
describe('.setValue', () => {
async function getEntryType(NAME) {
// get complex object
var result = await Registry.getValue(PATH, NAME, {format: 'complex'})
return result.type
}
it('is function', async () => {
assert.isFunction(Registry.setValue)
})
it('creates new value entry', async () => {
var NAME = 'value-entry-1'
var DATA = 'this is the newly created value data'
await Registry.deleteValue(PATH, NAME).catch(noop)
assert.isFalse(await Registry.hasValue(PATH, NAME))
await Registry.setValue(PATH, NAME, DATA)
assert.equal(await Registry.getValue(PATH, NAME), DATA)
})
it('overwrites existing value entry', async () => {
var NAME = 'value-entry-2'
var INITIAL_DATA = 'initial data'
var FINAL_DATA = 'expected final data'
await Registry.setValue(PATH, NAME, INITIAL_DATA)
assert.isTrue(await Registry.hasValue(PATH, NAME))
await Registry.setValue(PATH, NAME, FINAL_DATA)
assert.equal(await Registry.getValue(PATH, NAME), FINAL_DATA)
})
it(`creates empty REG_SZ value if the data argument isn't specified`, async () => {
var NAME = 'LegacyDisable'
await Registry.delete(OW_PATH, NAME)
await Registry.setValue(OW_PATH, NAME)
var value = await Registry.getValue(OW_PATH, NAME, {format: 'complex'})
assert.equal(value.name, 'legacydisable')
assert.equal(value.type, 'REG_SZ')
assert.equal(value.data, '')
})
it('also creates key if the path doesnt exist', async () => {
var NAME = 'nested-entry-1'
var DATA = 'this is the newly created value data'
await Registry.deleteKey(PATH + '\\deep', NAME).catch(noop)
assert.isFalse(await Registry.hasKey(PATH + '\\deep'))
await Registry.setValue(PATH + '\\deep\\sub\\key', NAME, DATA)
assert.isTrue(await Registry.hasKey(PATH + '\\deep'))
})
describe('input data type inferring', () => {
it('string as REG_SZ', async () => {
var NAME = 'type-infer-1'
await Registry.setValue(PATH, NAME, 'some string')
assert.equal(await getEntryType(NAME), 'REG_SZ')
})
it('number as REG_DWORD', async () => {
var NAME = 'type-infer-2'
await Registry.setValue(PATH, NAME, 123)
assert.equal(await getEntryType(NAME), 'REG_DWORD')
})
it('array as REG_MULTI_SZ', async () => {
var NAME = 'type-infer-3'
await Registry.setValue(PATH, NAME, ['one', 'two', 'three'])
assert.equal(await getEntryType(NAME), 'REG_MULTI_SZ')
})
it('buffer as REG_BINARY', async () => {
var NAME = 'type-infer-4'
await Registry.setValue(PATH, NAME, Buffer.from('Zenyatta'))
assert.equal(await getEntryType(NAME), 'REG_BINARY')
})
})
describe('storing and retrieving the same value', () => {
it('REG_SZ string', async () => {
var NAME = 'value-1'
var DATA = 'some data'
await Registry.setValue(PATH, NAME, DATA, 'REG_SZ')
assert.equal(await Registry.getValue(PATH, NAME), DATA)
})
it('REG_DWORD number', async () => {
var NAME = 'value-2'
var DATA = 123
await Registry.setValue(PATH, NAME, DATA, 'REG_DWORD')
assert.equal(await Registry.getValue(PATH, NAME), DATA)
})
it('REG_DWORD string', async () => {
var NAME = 'value-3'
var DATA = '123'
await Registry.setValue(PATH, NAME, DATA, 'REG_DWORD')
assert.equal(await Registry.getValue(PATH, NAME), DATA)
})
it('REG_QWORD number', async () => {
var NAME = 'value-4'
var DATA = 123
await Registry.setValue(PATH, NAME, DATA, 'REG_QWORD')
assert.equal(await Registry.getValue(PATH, NAME), '0x' + DATA.toString(16))
})
it('REG_QWORD number', async () => {
var NAME = 'value-5'
var DATA = 123
await Registry.setValue(PATH, NAME, DATA + '', 'REG_QWORD')
assert.equal(await Registry.getValue(PATH, NAME), '0x' + DATA.toString(16))
})
it('REG_MULTI_SZ array', async () => {
var NAME = 'value-6'
var DATA = ['one', 'two', 'three']
await Registry.setValue(PATH, NAME, DATA, 'REG_MULTI_SZ')
var array = await Registry.getValue(PATH, NAME)
assert.equal(array.length, DATA.length)
assert.equal(array[0], DATA[0])
assert.equal(array[1], DATA[1])
assert.equal(array[2], DATA[2])
})
it('REG_MULTI_SZ string', async () => {
var NAME = 'value-7'
var DATA = 'one\0two\0three'
await Registry.setValue(PATH, NAME, DATA, 'REG_MULTI_SZ')
var array = await Registry.getValue(PATH, NAME)
assert.equal(array.length, 3)
assert.equal(array[0], 'one')
assert.equal(array[1], 'two')
assert.equal(array[2], 'three')
})
it('REG_BINARY', async () => {
var NAME = 'value-8'
var DATA = Buffer.from('Experience tranquility')
await Registry.setValue(PATH, NAME, DATA, 'REG_BINARY')
var buffer = await Registry.getValue(PATH, NAME)
assert.equal(buffer.toString(), DATA.toString())
})
})
describe('iput data type override', () => {
it('explicitly stores string as REG_DWORD', async () => {
var NAME = 'type-override-1'
await Registry.setValue(PATH, NAME, '123', 'REG_DWORD')
assert.equal(await getEntryType(NAME), 'REG_DWORD')
})
it('explicitly stores string as REG_QWORD', async () => {
var NAME = 'type-override-2'
await Registry.setValue(PATH, NAME, '123', 'REG_QWORD')
assert.equal(await getEntryType(NAME), 'REG_QWORD')
})
it('explicitly stores buffer as REG_SZ', async () => {
var NAME = 'type-override-3'
await Registry.setValue(PATH, NAME, Buffer.from('Zenyatta'), 'REG_SZ')
assert.equal(await getEntryType(NAME), 'REG_SZ')
})
it('explicitly stores string as REG_BINARY', async () => {
var NAME = 'type-override-4'
var DATA = 'Experience tranquility'
await Registry.setValue(PATH, NAME, 'Zenyatta', 'REG_BINARY')
assert.equal(await getEntryType(NAME), 'REG_BINARY')
})
})
describe('type name alias', () => {
it('sz as REG_SZ', async () => {
var NAME = 'value-entry-9'
var DATA = '123'
await Registry.setValue(PATH, NAME, DATA, 'sz')
assert.equal(await getEntryType(NAME), 'REG_SZ')
})
it('dword as REG_DWORD', async () => {
var NAME = 'value-entry-10'
var DATA = 123
await Registry.setValue(PATH, NAME, DATA, 'dword')
assert.equal(await getEntryType(NAME), 'REG_DWORD')
})
})
})
describe('.set', () => {
// todo
it('is function', async () => {
assert.isFunction(Registry.set)
})
// TODO in future
it(`complex`, async () => {
await Registry.delete(OW_PATH)
await Registry.set(OW_PATH, {
'': `Soldiers. Scientists. Adventurers. Oddities.`,
leader: 'Jack Morrison',
scientists: ['Angela Ziegler', 'Winston', 'Mei-Ling Zhou']
})
assert.isTrue(await Registry.has(OW_PATH))
assert.isTrue(await Registry.has(OW_PATH, 'leader'))
assert.equal((await Registry.get(OW_PATH, 'scientists')).length, 3)
})
it(`nested complex`, async () => {
await Registry.delete(OW_PATH)
await Registry.set(OW_PATH, {
'': `Soldiers. Scientists. Adventurers. Oddities.`,
leader: 'Jack Morrison',
scientists: ['Angela Ziegler', 'Winston', 'Mei-Ling Zhou'],
Blackwatch: {
leader: 'Gabriel Reyes'
}
})
assert.isTrue(await Registry.has(OW_PATH))
assert.isTrue(await Registry.has(BW_PATH))
assert.equal(await Registry.get(OW_PATH, 'leader'), 'Jack Morrison')
assert.equal(await Registry.get(BW_PATH, 'leader'), 'Gabriel Reyes')
})
})
describe('.clearValues', () => {
it('is function', async () => {
assert.isFunction(Registry.clearValues)
})
it('deletes all values at given path, except for the default one', async () => {
await Registry.set(PATH, '', 'Experience tranquility')
await Registry.set(PATH, 'val1', 'Embrace the Iris')
//assert.isTrue(await Registry.hasKey(PATH))
//assert.isTrue(await Registry.hasValue(PATH, ''))
//assert.isTrue(await Registry.hasValue(PATH, 'val1'))
await Registry.clearValues(PATH)
assert.isTrue(await Registry.hasKey(PATH))
assert.isTrue(await Registry.hasValue(PATH, ''))
assert.isFalse(await Registry.hasValue(PATH, 'val1'))
assert.equal(Object.keys(await Registry.getValues(PATH)).length, 0)
})
it('retains subkeys', async () => {
await Registry.setKey(PATH + '\\subkey')
await Registry.clearValues(PATH)
assert.notEqual((await Registry.getKeys(PATH)).length, 0)
})
})
describe('.clearKeys', () => {
it('is function', async () => {
assert.isFunction(Registry.clearKeys)
})
it('deletes all subkeys', async () => {
await Registry.set(PATH + '\\subpath', 'val1', 'Embrace the Iris')
await Registry.clearKeys(PATH)
assert.equal(Object.keys(await Registry.getKeys(PATH)).length, 0)
})
})
describe('.clear', () => {
it('is function', async () => {
assert.isFunction(Registry.clear)
})
it('deletes all values and subkeys', async () => {
await Registry.set(PATH, 'val1', 'Embrace the Iris')
await Registry.set(PATH + '\\subpath', 'val1', 'Embrace the Iris')
await Registry.clear(PATH)
var result = await Registry.get(PATH, {format: 'complex'})
assert.equal(Object.keys(result.keys).length, 0)
assert.equal(result.values.length, 0)
})
})
describe(`default value`, () => {
var PATH2 = PATH + '\\default-value-test'
function createBeforeAndAfter() {
before(async () => {
await Registry.deleteKey(PATH2).catch(noop)
await Registry.setKey(PATH2)
})
after(() => Registry.deleteKey(PATH2))
}
describe(`new key creation also creates default value with empty string data`, () => {
createBeforeAndAfter()
it('.getValue returns undefined', async () => {
assert.equal(await Registry.getValue(PATH2, ''), '')
})
it('.hasValue does not throw', async () => {
assert.isTrue(await Registry.hasValue(PATH2, ''))
})
it('.getValues includes it', async () => {
assert.notEqual((await Registry.getValues(PATH2, false)).length, 0)
})
})
describe(`no data`, () => {
createBeforeAndAfter()
it('.getValue returns undefined', async () => {
await Registry.deleteValue(PATH2, '')
assert.equal(await Registry.getValue(PATH2, ''), undefined)
})
it('.hasValue does not throw', async () => {
assert.isTrue(await Registry.hasValue(PATH2, ''))
})
it('.getValues does not include it', async () => {
assert.equal((await Registry.getValues(PATH2, {format: 'complex'})).length, 0)
})
})
describe(`setting data to empty string`, () => {
createBeforeAndAfter()
it('.getValue returns empty string', async () => {
await Registry.setValue(PATH2, '', '')
assert.equal(await Registry.getValue(PATH2, ''), '')
})
it('.hasValue does not throw', async () => {
assert.isTrue(await Registry.hasValue(PATH2, ''))
})
it('.getValues includes it', async () => {
assert.notEqual((await Registry.getValues(PATH2, {format: 'complex'})).length, 0)
})
})
describe(`setting data to string`, () => {
createBeforeAndAfter()
it('.getValue returns the string', async () => {
await Registry.setValue(PATH2, '', 'harmony')
assert.equal(await Registry.getValue(PATH2, ''), 'harmony')
})
it('.hasValue does not throw', async () => {
assert.isTrue(await Registry.hasValue(PATH2, ''))
})
it('.getValues includes it', async () => {
assert.notEqual((await Registry.getValues(PATH2, {format: 'complex'})).length, 0)
})
})
})
describe('global options', () => {
after(() => Registry.format = 'simple')