|
| 1 | +/*jshint mocha:true */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +var assert = require('assert'); |
| 5 | +var fs = require('fs'); |
| 6 | +var path = require('path'); |
| 7 | +var vm = require('vm'); |
| 8 | + |
| 9 | +// Only urlencode_unpacker.js exports via module.exports; P_A_C_K_E_R and |
| 10 | +// JavascriptObfuscator are browser-only globals (plain `var X = {...}` |
| 11 | +// declarations meant to be loaded via <script> tags), so they're run as |
| 12 | +// source into the sandbox below instead of required. |
| 13 | +var Urlencoded = require('../../src/unpackers/urlencode_unpacker'); |
| 14 | + |
| 15 | +function runFileInContext(sandbox, filePath) { |
| 16 | + var source = fs.readFileSync(filePath, 'utf8'); |
| 17 | + vm.runInContext(source, sandbox, { filename: path.basename(filePath) }); |
| 18 | +} |
| 19 | + |
| 20 | +// web/common-function.js is a browser script (no module.exports) that relies |
| 21 | +// on globals like window/requirejs/jQuery/the unpacker libs loaded via <script> |
| 22 | +// tags. Load it into a sandboxed vm context with just the globals its |
| 23 | +// pure/logic functions need, then pull those functions off the sandbox. |
| 24 | +function loadCommonFunctions() { |
| 25 | + var sandbox = { |
| 26 | + window: { location: { href: '' } }, |
| 27 | + requirejs: function() {}, |
| 28 | + Urlencoded: Urlencoded |
| 29 | + }; |
| 30 | + sandbox.requirejs.config = function() {}; |
| 31 | + |
| 32 | + vm.createContext(sandbox); |
| 33 | + runFileInContext(sandbox, path.join(__dirname, '../../src/unpackers/p_a_c_k_e_r_unpacker.js')); |
| 34 | + runFileInContext(sandbox, path.join(__dirname, '../../src/unpackers/javascriptobfuscator_unpacker.js')); |
| 35 | + runFileInContext(sandbox, path.join(__dirname, '../../../web/common-function.js')); |
| 36 | + return sandbox; |
| 37 | +} |
| 38 | + |
| 39 | +describe('web/common-function.js', function() { |
| 40 | + var common = loadCommonFunctions(); |
| 41 | + |
| 42 | + describe('any', function() { |
| 43 | + it('returns the first argument when truthy', function() { |
| 44 | + assert.equal(common.any(1, 2), 1); |
| 45 | + }); |
| 46 | + it('falls back to the second argument when the first is falsy', function() { |
| 47 | + assert.equal(common.any(0, 2), 2); |
| 48 | + assert.equal(common.any(null, 'x'), 'x'); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('mergeObjects', function() { |
| 53 | + it('overrides base options with additional options', function() { |
| 54 | + assert.deepEqual( |
| 55 | + common.mergeObjects({ a: 1, b: 2 }, { b: 3, c: 4 }), { a: 1, b: 3, c: 4 }); |
| 56 | + }); |
| 57 | + it('returns a copy of the base options when no additional options are given', function() { |
| 58 | + assert.deepEqual(common.mergeObjects({ a: 1 }, {}), { a: 1 }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('unpacker_filter', function() { |
| 63 | + it('passes through source with no leading comments', function() { |
| 64 | + assert.equal(common.unpacker_filter('var a = 1;'), '\nvar a = 1;'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('preserves a single leading block comment', function() { |
| 68 | + assert.equal( |
| 69 | + common.unpacker_filter('/* comment */var a = 1;'), |
| 70 | + '/* comment */\nvar a = 1;'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('preserves a single leading line comment', function() { |
| 74 | + assert.equal( |
| 75 | + common.unpacker_filter('// comment\nvar a = 1;'), |
| 76 | + '// comment\nvar a = 1;'); |
| 77 | + }); |
| 78 | + |
| 79 | + // Regression test for #1546: a second leading comment used to lose its |
| 80 | + // formatting because whitespace was stripped after every comment instead |
| 81 | + // of once after all leading comments were collected. |
| 82 | + it('preserves multiple leading block comments in order', function() { |
| 83 | + var input = '/* first */\n/* second */\nvar a = 1;'; |
| 84 | + assert.equal(common.unpacker_filter(input), input); |
| 85 | + }); |
| 86 | + |
| 87 | + it('preserves a mix of leading line and block comments', function() { |
| 88 | + var input = '// line one\n/* block two */\nvar a = 1;'; |
| 89 | + assert.equal(common.unpacker_filter(input), input); |
| 90 | + }); |
| 91 | + |
| 92 | + it('unpacks urlencoded source after stripping leading comments', function() { |
| 93 | + assert.equal( |
| 94 | + common.unpacker_filter('/* encoded */javascript:var%20a=1'), |
| 95 | + '/* encoded */\n\njavascript:var a=1'); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments