Skip to content

Commit 538a990

Browse files
committed
fix lint ts file.
add unit tests. fix SetValue function manage only reg_sz & reg_dword
1 parent 2518562 commit 538a990

File tree

3 files changed

+105
-21
lines changed

3 files changed

+105
-21
lines changed

lib/registry.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ export function setValue(
170170
// this code is a no-op when the module is missing
171171
return false
172172
}
173+
174+
if (
175+
valueType != RegistryValueType.REG_SZ &&
176+
valueType != RegistryValueType.REG_DWORD
177+
) {
178+
// not implemented yet
179+
return false
180+
}
173181
const hkey = mapToLong(key)
174182

175183
const result: boolean = nativeModule.setValue(

src/main.cc

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ NAN_METHOD(EnumKeys) {
380380
auto first = reinterpret_cast<HKEY>(Nan::To<int64_t>(info[0]).FromJust());
381381

382382
HKEY hCurrentKey = first;
383-
if (argCount == 5 && !info[1]->IsNullOrUndefined() && !info[2]->IsNullOrUndefined())
383+
if (argCount == 5 && !info[1]->IsNullOrUndefined() && !info[2]->IsNullOrUndefined() && !info[3]->IsNullOrUndefined())
384384
{
385385
Nan::Utf8String subkeyArg(Nan::To<v8::String>(info[1]).ToLocalChecked());
386386
auto subkey = utf8ToWideChar(std::string(*subkeyArg));
@@ -411,65 +411,79 @@ NAN_METHOD(EnumKeys) {
411411
first,
412412
subkey,
413413
0,
414-
KEY_READ | KEY_WOW64_64KEY,
414+
KEY_WRITE | KEY_WOW64_64KEY,
415415
&hOpenKey);
416416

417417
if (openKey == ERROR_FILE_NOT_FOUND)
418418
{
419-
// the key does not exist,
420-
info.GetReturnValue().Set(New<v8::Boolean>(false));
419+
Nan::ThrowTypeError("RegOpenKeyEx : cannot find the registrykey, error_code : ERROR_FILE_NOT_FOUND");
421420
return;
422421
}
423422
else if (openKey == ERROR_SUCCESS)
424423
{
425-
long dwordType;
426424
long setValue = ERROR_INVALID_HANDLE;
427425

428-
if (valueType == L"REG_SZ")
426+
if (wcscmp(valueType, L"REG_SZ") == 0)
429427
{
430-
Nan::Utf8String typeArg(Nan::To<v8::String>(info[3]).ToLocalChecked());
428+
Nan::Utf8String typeArg(Nan::To<v8::String>(info[4]).ToLocalChecked());
431429
auto valueData = utf8ToWideChar(std::string(*typeArg));
432-
dwordType = REG_SZ;
430+
if (valueData == nullptr)
431+
{
432+
Nan::ThrowTypeError("A string was expected for the fifth argument, but could not be parsed.");
433+
return;
434+
}
435+
int datalength = static_cast<int>(wcslen(valueData) * sizeof(valueData[0]));
433436
setValue = RegSetValueEx(
434437
hOpenKey,
435438
valueName,
436439
0,
437-
dwordType,
440+
REG_SZ,
438441
(const BYTE *)valueData,
439-
sizeof(valueData));
442+
datalength);
440443
}
441-
else if (valueType == L"REG_DWORD")
444+
else if (wcscmp(valueType, L"REG_DWORD") == 0)
442445
{
443-
auto valueData = (const DWORD *)(Nan::To<int64_t>(info[3]).FromJust());
444-
dwordType = REG_DWORD;
446+
int dwordData = Nan::To<int>(info[4]).FromJust();
447+
DWORD valueData = static_cast<DWORD>(dwordData);
448+
445449
setValue = RegSetValueEx(
446450
hOpenKey,
447451
valueName,
448452
0,
449-
dwordType,
450-
(const BYTE *)valueData,
453+
REG_DWORD,
454+
(const BYTE *)&valueData,
451455
sizeof(valueData));
452456
}
457+
else
458+
{
459+
char errorMessage[255];
460+
sprintf_s(errorMessage, "RegSetValueEx Unmanaged type : '%ls'", valueType);
461+
Nan::ThrowTypeError(errorMessage);
462+
return;
463+
}
453464

454465
if (setValue != ERROR_SUCCESS)
455466
{
456467
// FIXME: the key does not exist, just return an empty array for now
457468
info.GetReturnValue().Set(New<v8::Boolean>(false));
458469
return;
459470
}
471+
info.GetReturnValue().Set(New<v8::Boolean>(true));
460472
RegCloseKey(hOpenKey);
461473
}
462474
else
463475
{
464476
char errorMessage[46]; // 35 for message + 10 for int + 1 for nul
465477
sprintf_s(errorMessage, "RegOpenKeyEx failed - exit code: '%d'", openKey);
466-
Nan::ThrowError(errorMessage);
478+
Nan::ThrowTypeError(errorMessage);
479+
return;
467480
}
468481
}
469-
470-
info.GetReturnValue().Set(New<v8::Boolean>(true));
471-
if (hCurrentKey != first)
472-
RegCloseKey(hCurrentKey);
482+
else
483+
{
484+
Nan::ThrowTypeError("An argument has invalid format");
485+
return;
486+
}
473487
}
474488
}
475489

test/registry-test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { enumerateValues, enumerateKeys, HKEY } from '../lib/index'
1+
import {
2+
enumerateValues,
3+
enumerateKeys,
4+
setValue,
5+
createKey,
6+
HKEY,
7+
RegistryValueType,
8+
} from '../lib/index'
29

310
if (process.platform === 'win32') {
411
describe('enumerateValue', () => {
@@ -60,4 +67,59 @@ if (process.platform === 'win32') {
6067
expect(values.indexOf('Microsoft')).toBeGreaterThanOrEqual(0)
6168
})
6269
})
70+
71+
describe('setValue', () => {
72+
it("can't set not implemented value for a registry key", () => {
73+
const result = setValue(
74+
HKEY.HKEY_CURRENT_USER,
75+
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
76+
'ValueTest',
77+
RegistryValueType.REG_EXPAND_SZ,
78+
'Value'
79+
)
80+
expect(result).toBeFalsy()
81+
})
82+
83+
it('can set DWORD value for a registry key', () => {
84+
let result = false
85+
try {
86+
result = setValue(
87+
HKEY.HKEY_CURRENT_USER,
88+
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
89+
'ValueTestDword',
90+
RegistryValueType.REG_DWORD,
91+
1
92+
)
93+
} catch (e) {
94+
console.log(e)
95+
}
96+
expect(result).toBeTruthy()
97+
})
98+
99+
it('can set REG_SZ value for a registry key', () => {
100+
let result = false
101+
try {
102+
result = setValue(
103+
HKEY.HKEY_CURRENT_USER,
104+
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
105+
'ValueTestSz',
106+
RegistryValueType.REG_SZ,
107+
'Value 123 !'
108+
)
109+
} catch (e) {
110+
console.log(e)
111+
}
112+
expect(result).toBeTruthy()
113+
})
114+
})
115+
116+
describe('createKey', () => {
117+
it('can create a registry key', () => {
118+
const result = createKey(
119+
HKEY.HKEY_CURRENT_USER,
120+
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\UnitTests'
121+
)
122+
expect(result).toBeTruthy()
123+
})
124+
})
63125
}

0 commit comments

Comments
 (0)