Skip to content

Add more tests for utils #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var files = ['lib/**/*.js', 'app.js', 'test/**/*.js'];
grunt.initConfig({
mochacli: {
options: {
timeout: 4000,
timeout: 2000,
reporter: 'spec',
bail: true
},
Expand Down
6 changes: 3 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var SW_SHOWNORMAL = 0x1;

module.exports = {
elevate: function (filepath, parameters, callback) {
if(parameters === null || parameters === '') {
debug('Missing parameters filepath');
callback('Missing parameters filepath');
if(!filepath) {
debug('Missing filepath');
callback('Missing filepath');
return;
}
var shellexecuteinfoval = new windef.SHELLEXECUTEINFO({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"grunt": "^0.4.5",
"grunt-mocha-cli": "^2.0.0",
"load-grunt-tasks": "^3.3.0",
"mockery": "^1.4.0",
"ref": "^1.2.0",
"ref-struct": "^1.0.2",
"ref-union": "^1.0.0"
Expand Down
15 changes: 0 additions & 15 deletions test/file_association.js

This file was deleted.

8 changes: 6 additions & 2 deletions test/mock/ffi.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
var advApi = require('./adv_api'),
assert = require('assert'),
shell32 = require('./shell32'),
types = require('../../lib/types');

module.exports = {
Library: function (libFile, funcs) {
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) {
// this is redefition for the library only specifying
// a different key type
lib = advApi;
break;
}
assert(funcs.RegQueryValueExA.constructor === Array);
Expand All @@ -19,13 +22,14 @@ module.exports = {
assert(funcs.RegCloseKey.constructor === Array);
assert(funcs.RegSetValueExA.constructor === Array);
assert(typeof funcs === 'object');
lib = advApi;
break;
case 'Shell32':
// TODO place asserts here
lib = shell32;
break;
default:
throw 'Please add asserts for this new library file';
}
return advApi;
return lib;
}
};
47 changes: 47 additions & 0 deletions test/mock/shell32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* A mock implementation of the Windows shell execute API.
* This is used for our tests
*/
'use strict';
require('ref');
var debug = require('debug')('windows-registry'),
assert = require('assert'),
struct = require('ref-struct'),
uniontype = require('ref-union'),
types = require('../../lib/types');

var DUMMYUNIONNAME = uniontype({
hIcon: types.HANDLE,
hMonitor: types.HANDLE
});

var SHELLEXECUTEINFO = struct({
cbSize: types.DWORD,
fMask: types.ULONG,
hwnd: types.HWND,
lpVerb: types.STRING,
lpFile: types.STRING,
lpParameters: types.STRING,
lpDirectory: types.STRING,
nShow: types.INT,
hInstApp: types.HINSTANCE,
lpIDList: types.LPVOID,
lpClass: types.STRING,
hkeyClass: types.HKEY,
dwHotKey: types.DWORD,
DUMMYUNIONNAME: DUMMYUNIONNAME,
hProcess: types.HANDLE
});

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

module.exports = shell32Mock;
44 changes: 0 additions & 44 deletions test/uac.js

This file was deleted.

41 changes: 41 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* global describe, it */
'use strict';
if (process.env.TEST_MOCKS_ON) {
require('./test_helper');
}

var utils = require('../lib/utils'),
assert = require('assert');

describe('File Association Test', () => {
it('Should create a file association for *.zzz files', () => {
utils.associateExeForFile('myTestHandler', 'A test handler for unit tests', '', 'C:\\Program Files\\nodejs\\node.exe %1', '.zzz');
assert(true);
});
});

describe('UAC elevate tests', () => {
it('Should get results for elevate for a given file', (done) => {
utils.elevate('C:\\Program Files\\nodejs\\node.exe', 'index.js', function (err, result) {
assert.equal(err, null);
assert.equal(result, true);
done();
});
});

it('Empty file path, should return with error', (done) => {
utils.elevate('', null, function (err, result) {
assert.equal(err, 'Missing filepath');
assert.equal(result, null);
done();
});
});

it('Null file path, should return with error', (done) => {
utils.elevate(null, null, function (err, result) {
assert.equal(err, 'Missing filepath');
assert.equal(result, null);
done();
});
});
});