Skip to content

Commit

Permalink
Set up -W API method mocks
Browse files Browse the repository at this point in the history
Now store saved registry strings as copies of the buffer passed,
not as strings.
  • Loading branch information
RupW committed Jun 21, 2018
1 parent 5f88a5d commit 1d5a9a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
29 changes: 18 additions & 11 deletions test/mock/adv_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ var advApi = {
_Inout_opt_ LPDWORD lpcbData
);
*/
RegQueryValueExA: function (hKey, valueName, shouldBeNull, lpType, lpData, lpcbData) {
debug('RegQueryValueExA');
RegQueryValueExW: function (hKey, valueName, shouldBeNull, lpType, lpData, lpcbData) {
debug('RegQueryValueExW');
if (lpData === null) {
debug(keys[hKey.address()].values.test_value_name);
lpType.writeUInt32LE(windef.REG_VALUE_TYPE.REG_SZ, 0);
lpcbData.writeUInt32LE(keys[hKey.address()].values[valueName].length, 0);
return 0;
}

lpData.write(keys[hKey.address()].values[valueName].value, 'utf8');
keys[hKey.address()].values[valueName].valueBuffer.copy(lpData);
lpType.writeUInt16LE(windef.REG_VALUE_TYPE.REG_SZ);
return 0;
},
Expand All @@ -67,9 +67,9 @@ var advApi = {
_Out_ PHKEY phkResult
);
*/
RegOpenKeyExA: function (hKey, subKeyName, shouldBeZero, accessLevel, pHkey) {
RegOpenKeyExW: function (hKey, subKeyName, shouldBeZero, accessLevel, pHkey) {
var accessLevelFound = findValueInHash(accessLevel, windef.KEY_ACCESS);
debug('Mock: RegOpenKeyExA subkey: ' + subKeyName);
debug('Mock: RegOpenKeyExW subkey: ' + subKeyName);
if (hKey.address) {
debug('Mock: hKey address:' + hKey.address());
}
Expand Down Expand Up @@ -123,8 +123,8 @@ var advApi = {
_In_ DWORD cbData
);
*/
RegSetValueExA: function (hKey, valueName, shouldBeNull, valueType, valueBuffer, bufferLength) {
debug('Mock: RegSetValueExA');
RegSetValueExW: function (hKey, valueName, shouldBeNull, valueType, valueBuffer, bufferLength) {
debug('Mock: RegSetValueExW');
// predefined key
if (typeof hKey === 'number') {
assert(findValueInHash(hKey, windef.HKEY), 'Mock: Invalid predefined key specified');
Expand All @@ -136,9 +136,16 @@ var advApi = {
assert(valueBuffer.constructor === Buffer);
assert(typeof bufferLength === 'number');

// Use the passed length, not the length of valueBuffer
var value = ref.reinterpret(valueBuffer, bufferLength, 0);

// Copy the value to a new buffer and store that
var valueCopy = new Buffer(bufferLength);
value.copy(valueCopy, 0, 0, bufferLength);

keys[hKey.address()].values[valueName] = {
valueType: valueType,
value: ref.readCString(valueBuffer),
valueBuffer: valueCopy,
length: bufferLength
};
return 0;
Expand All @@ -156,8 +163,8 @@ var advApi = {
_Out_opt_ LPDWORD lpdwDisposition
);
*/
RegCreateKeyExA: function (hKey, subKeyName, shouldBeNull, shouldBeNull2, securityAttributes, accessLevel, shouldBeNull3, pHkey, shouldBeNull4) {
debug('Mock: RegCreateKeyExA');
RegCreateKeyExW: function (hKey, subKeyName, shouldBeNull, shouldBeNull2, securityAttributes, accessLevel, shouldBeNull3, pHkey, shouldBeNull4) {
debug('Mock: RegCreateKeyExW');
assert(hKey.constructor === Buffer);
assert(typeof subKeyName === 'string');
assert(shouldBeNull === null);
Expand Down Expand Up @@ -192,7 +199,7 @@ var advApi = {
_In_opt_ LPCTSTR lpSubKey
);
*/
RegDeleteTreeA: function (hKey, subKeyName) {
RegDeleteTreeW: function (hKey, subKeyName) {
if (typeof hKey === 'number') {
assert(findValueInHash(hKey, windef.HKEY), 'Mock: Invalid predefined key specified');
} else {
Expand Down
14 changes: 7 additions & 7 deletions test/mock/ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module.exports = {
var lib;
switch (libFile) {
case 'Advapi32':
assert(funcs.RegOpenKeyExA.constructor === Array);
if(funcs.RegOpenKeyExA[1][0].indirection === types.HKEY.indirection &&
funcs.RegOpenKeyExA[1][0].name === types.HKEY.name) {
assert(funcs.RegOpenKeyExW.constructor === Array);
if(funcs.RegOpenKeyExW[1][0].indirection === types.HKEY.indirection &&
funcs.RegOpenKeyExW[1][0].name === types.HKEY.name) {
// this is redefition for the library only specifying
// a different key type
lib = advApi;
break;
}
assert(funcs.RegQueryValueExA.constructor === Array);
assert(funcs.RegCreateKeyExA.constructor === Array);
assert(funcs.RegDeleteTreeA.constructor === Array);
assert(funcs.RegQueryValueExW.constructor === Array);
assert(funcs.RegCreateKeyExW.constructor === Array);
assert(funcs.RegDeleteTreeW.constructor === Array);
assert(funcs.RegCloseKey.constructor === Array);
assert(funcs.RegSetValueExA.constructor === Array);
assert(funcs.RegSetValueExW.constructor === Array);
assert(typeof funcs === 'object');
lib = advApi;
break;
Expand Down
4 changes: 2 additions & 2 deletions test/mock/shell32.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ var SHELLEXECUTEINFO = struct({
});

var shell32Mock = {
ShellExecuteExA: function () {
ShellExecuteExW: function () {
}
};
shell32Mock.ShellExecuteExA.async = function (type, cb) {
shell32Mock.ShellExecuteExW.async = function (type, cb) {
debug('async');
debug(type.deref().lpFile);
assert.deepEqual(type.type.fields.cbSize, SHELLEXECUTEINFO.fields.cbSize);
Expand Down

0 comments on commit 1d5a9a1

Please sign in to comment.