Skip to content

Commit 29fa45b

Browse files
committed
add more tests for uac
1 parent 7e00be1 commit 29fa45b

File tree

8 files changed

+99
-65
lines changed

8 files changed

+99
-65
lines changed

Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var files = ['lib/**/*.js', 'app.js', 'test/**/*.js'];
55
grunt.initConfig({
66
mochacli: {
77
options: {
8-
timeout: 4000,
8+
timeout: 2000,
99
reporter: 'spec',
1010
bail: true
1111
},

lib/utils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var SW_SHOWNORMAL = 0x1;
1414

1515
module.exports = {
1616
elevate: function (filepath, parameters, callback) {
17-
if(parameters === null || parameters === '') {
18-
debug('Missing parameters filepath');
19-
callback('Missing parameters filepath');
17+
if(!filepath) {
18+
debug('Missing filepath');
19+
callback('Missing filepath');
2020
return;
2121
}
2222
var shellexecuteinfoval = new windef.SHELLEXECUTEINFO({

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"grunt": "^0.4.5",
1313
"grunt-mocha-cli": "^2.0.0",
1414
"load-grunt-tasks": "^3.3.0",
15+
"mockery": "^1.4.0",
1516
"ref": "^1.2.0",
1617
"ref-struct": "^1.0.2",
1718
"ref-union": "^1.0.0"

test/file_association.js

-15
This file was deleted.

test/mock/ffi.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
var advApi = require('./adv_api'),
22
assert = require('assert'),
3+
shell32 = require('./shell32'),
34
types = require('../../lib/types');
45

56
module.exports = {
67
Library: function (libFile, funcs) {
8+
var lib;
79
switch (libFile) {
810
case 'Advapi32':
911
assert(funcs.RegOpenKeyExA.constructor === Array);
1012
if(funcs.RegOpenKeyExA[1][0].indirection === types.HKEY.indirection &&
1113
funcs.RegOpenKeyExA[1][0].name === types.HKEY.name) {
1214
// this is redefition for the library only specifying
1315
// a different key type
16+
lib = advApi;
1417
break;
1518
}
1619
assert(funcs.RegQueryValueExA.constructor === Array);
@@ -19,13 +22,14 @@ module.exports = {
1922
assert(funcs.RegCloseKey.constructor === Array);
2023
assert(funcs.RegSetValueExA.constructor === Array);
2124
assert(typeof funcs === 'object');
25+
lib = advApi;
2226
break;
2327
case 'Shell32':
24-
// TODO place asserts here
28+
lib = shell32;
2529
break;
2630
default:
2731
throw 'Please add asserts for this new library file';
2832
}
29-
return advApi;
33+
return lib;
3034
}
3135
};

test/mock/shell32.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* A mock implementation of the Windows shell execute API.
3+
* This is used for our tests
4+
*/
5+
'use strict';
6+
require('ref');
7+
var debug = require('debug')('windows-registry'),
8+
assert = require('assert'),
9+
struct = require('ref-struct'),
10+
uniontype = require('ref-union'),
11+
types = require('../../lib/types');
12+
13+
var DUMMYUNIONNAME = uniontype({
14+
hIcon: types.HANDLE,
15+
hMonitor: types.HANDLE
16+
});
17+
18+
var SHELLEXECUTEINFO = struct({
19+
cbSize: types.DWORD,
20+
fMask: types.ULONG,
21+
hwnd: types.HWND,
22+
lpVerb: types.STRING,
23+
lpFile: types.STRING,
24+
lpParameters: types.STRING,
25+
lpDirectory: types.STRING,
26+
nShow: types.INT,
27+
hInstApp: types.HINSTANCE,
28+
lpIDList: types.LPVOID,
29+
lpClass: types.STRING,
30+
hkeyClass: types.HKEY,
31+
dwHotKey: types.DWORD,
32+
DUMMYUNIONNAME: DUMMYUNIONNAME,
33+
hProcess: types.HANDLE
34+
});
35+
36+
var shell32Mock = {
37+
ShellExecuteExA: function () {
38+
}
39+
};
40+
shell32Mock.ShellExecuteExA.async = function (type, cb) {
41+
debug('async');
42+
debug(type.deref().lpFile);
43+
assert.deepEqual(type.type.fields.cbSize, SHELLEXECUTEINFO.fields.cbSize);
44+
cb(null, true);
45+
};
46+
47+
module.exports = shell32Mock;

test/uac.js

-44
This file was deleted.

test/utils.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* global describe, it */
2+
'use strict';
3+
if (process.env.TEST_MOCKS_ON) {
4+
require('./test_helper');
5+
}
6+
7+
var utils = require('../lib/utils'),
8+
assert = require('assert');
9+
10+
describe('File Association Test', () => {
11+
it('Should create a file association for *.zzz files', () => {
12+
utils.associateExeForFile('myTestHandler', 'A test handler for unit tests', '', 'C:\\Program Files\\nodejs\\node.exe %1', '.zzz');
13+
assert(true);
14+
});
15+
});
16+
17+
describe('UAC elevate tests', () => {
18+
it('Should get results for elevate for a given file', (done) => {
19+
utils.elevate('C:\\Program Files\\nodejs\\node.exe', 'index.js', function (err, result) {
20+
assert.equal(err, null);
21+
assert.equal(result, true);
22+
done();
23+
});
24+
});
25+
26+
it('Empty file path, should return with error', (done) => {
27+
utils.elevate('', null, function (err, result) {
28+
assert.equal(err, 'Missing filepath');
29+
assert.equal(result, null);
30+
done();
31+
});
32+
});
33+
34+
it('Null file path, should return with error', (done) => {
35+
utils.elevate(null, null, function (err, result) {
36+
assert.equal(err, 'Missing filepath');
37+
assert.equal(result, null);
38+
done();
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)