|
| 1 | +import { assert } from 'chai'; |
| 2 | + |
| 3 | +import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes'; |
| 4 | + |
| 5 | +import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator'; |
| 6 | + |
| 7 | +import { readFileAsString } from '../../../../helpers/readFileAsString'; |
| 8 | + |
| 9 | +import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade'; |
| 10 | + |
| 11 | +describe('ManglePropertiesTransformer', () => { |
| 12 | + describe('transformNode', () => { |
| 13 | + describe('Hexadecimal identifier names generator', () => { |
| 14 | + describe('Variant #1: base properties mangle', () => { |
| 15 | + const property1RegExp: RegExp = /'(_0x[a-f0-9]{4,6})': *0x1/; |
| 16 | + const property2RegExp: RegExp = /'(_0x[a-f0-9]{4,6})': *0x2/; |
| 17 | + const property3RegExp: RegExp = /\['(_0x[a-f0-9]{4,6})']: *0x3/; |
| 18 | + const property4RegExp: RegExp = /\[hawk]: *0x4/; |
| 19 | + |
| 20 | + |
| 21 | + let obfuscatedCode: string; |
| 22 | + |
| 23 | + before(() => { |
| 24 | + const code: string = readFileAsString(__dirname + '/fixtures/base.js'); |
| 25 | + |
| 26 | + obfuscatedCode = JavaScriptObfuscator.obfuscate( |
| 27 | + code, |
| 28 | + { |
| 29 | + ...NO_ADDITIONAL_NODES_PRESET, |
| 30 | + mangleProperties: true, |
| 31 | + identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator |
| 32 | + } |
| 33 | + ).getObfuscatedCode(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('Match #1: should mangle property', () => { |
| 37 | + assert.match(obfuscatedCode, property1RegExp); |
| 38 | + }); |
| 39 | + |
| 40 | + it('Match #2: should mangle property', () => { |
| 41 | + assert.match(obfuscatedCode, property2RegExp); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Match #3: should mangle property', () => { |
| 45 | + assert.match(obfuscatedCode, property3RegExp); |
| 46 | + }); |
| 47 | + |
| 48 | + it('Match #4: should mangle property', () => { |
| 49 | + assert.match(obfuscatedCode, property4RegExp); |
| 50 | + }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('Mangled identifier names generator', () => { |
| 55 | + describe('Variant #1: base properties mangle', () => { |
| 56 | + const property1RegExp: RegExp = /'a': *0x1/; |
| 57 | + const property2RegExp: RegExp = /'b': *0x2/; |
| 58 | + const property3RegExp: RegExp = /\['c']: *0x3/; |
| 59 | + const property4RegExp: RegExp = /\[hawk]: *0x4/; |
| 60 | + |
| 61 | + |
| 62 | + let obfuscatedCode: string; |
| 63 | + |
| 64 | + before(() => { |
| 65 | + const code: string = readFileAsString(__dirname + '/fixtures/base.js'); |
| 66 | + |
| 67 | + obfuscatedCode = JavaScriptObfuscator.obfuscate( |
| 68 | + code, |
| 69 | + { |
| 70 | + ...NO_ADDITIONAL_NODES_PRESET, |
| 71 | + mangleProperties: true, |
| 72 | + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator |
| 73 | + } |
| 74 | + ).getObfuscatedCode(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('Match #1: should mangle property', () => { |
| 78 | + assert.match(obfuscatedCode, property1RegExp); |
| 79 | + }); |
| 80 | + |
| 81 | + it('Match #2: should mangle property', () => { |
| 82 | + assert.match(obfuscatedCode, property2RegExp); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Match #3: should mangle property', () => { |
| 86 | + assert.match(obfuscatedCode, property3RegExp); |
| 87 | + }); |
| 88 | + |
| 89 | + it('Match #4: should mangle property', () => { |
| 90 | + assert.match(obfuscatedCode, property4RegExp); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('Variant #2: base properties mangle with rename globals', () => { |
| 95 | + const variable1RegExp: RegExp = /const d *= *'hawk'/; |
| 96 | + const variable2RegExp: RegExp = /const e *= *{/; |
| 97 | + const property1RegExp: RegExp = /'a': *0x1/; |
| 98 | + const property2RegExp: RegExp = /'b': *0x2/; |
| 99 | + const property3RegExp: RegExp = /\['c']: *0x3/; |
| 100 | + const property4RegExp: RegExp = /\[d]: *0x4/; |
| 101 | + |
| 102 | + |
| 103 | + let obfuscatedCode: string; |
| 104 | + |
| 105 | + before(() => { |
| 106 | + const code: string = readFileAsString(__dirname + '/fixtures/base.js'); |
| 107 | + |
| 108 | + obfuscatedCode = JavaScriptObfuscator.obfuscate( |
| 109 | + code, |
| 110 | + { |
| 111 | + ...NO_ADDITIONAL_NODES_PRESET, |
| 112 | + mangleProperties: true, |
| 113 | + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator, |
| 114 | + renameGlobals: true |
| 115 | + } |
| 116 | + ).getObfuscatedCode(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('Match #1: should rename variable name', () => { |
| 120 | + assert.match(obfuscatedCode, variable1RegExp); |
| 121 | + }); |
| 122 | + |
| 123 | + it('Match #2: should rename variable name', () => { |
| 124 | + assert.match(obfuscatedCode, variable2RegExp); |
| 125 | + }); |
| 126 | + |
| 127 | + it('Match #3: should mangle property', () => { |
| 128 | + assert.match(obfuscatedCode, property1RegExp); |
| 129 | + }); |
| 130 | + |
| 131 | + it('Match #4: should mangle property', () => { |
| 132 | + assert.match(obfuscatedCode, property2RegExp); |
| 133 | + }); |
| 134 | + |
| 135 | + it('Match #5: should mangle property', () => { |
| 136 | + assert.match(obfuscatedCode, property3RegExp); |
| 137 | + }); |
| 138 | + |
| 139 | + it('Match #6: should mangle property', () => { |
| 140 | + assert.match(obfuscatedCode, property4RegExp); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + describe('Variant #3: properties mangle of nested objects', () => { |
| 145 | + const regExp: RegExp = new RegExp('' + |
| 146 | + 'const foo *= *{' + |
| 147 | + '\'a\': *{' + |
| 148 | + '\'b\': *0x1' + |
| 149 | + '}' + |
| 150 | + '};' + |
| 151 | + 'const bar *= *foo\\[\'a\']\\[\'b\'];' + |
| 152 | + ''); |
| 153 | + |
| 154 | + let obfuscatedCode: string; |
| 155 | + |
| 156 | + before(() => { |
| 157 | + const code: string = readFileAsString(__dirname + '/fixtures/nested-objects.js'); |
| 158 | + |
| 159 | + obfuscatedCode = JavaScriptObfuscator.obfuscate( |
| 160 | + code, |
| 161 | + { |
| 162 | + ...NO_ADDITIONAL_NODES_PRESET, |
| 163 | + mangleProperties: true, |
| 164 | + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator |
| 165 | + } |
| 166 | + ).getObfuscatedCode(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('Should mangle property', () => { |
| 170 | + assert.match(obfuscatedCode, regExp); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('Variant #4: properties mangle of rest element', () => { |
| 175 | + const regExp: RegExp = new RegExp('' + |
| 176 | + 'const foo *= *{' + |
| 177 | + '\'a\': *0x1' + |
| 178 | + '};' + |
| 179 | + 'const \\{a: *bar} *= *foo;' + |
| 180 | + 'const baz *= *bar;' + |
| 181 | + ''); |
| 182 | + |
| 183 | + let obfuscatedCode: string; |
| 184 | + |
| 185 | + before(() => { |
| 186 | + const code: string = readFileAsString(__dirname + '/fixtures/rest-element.js'); |
| 187 | + |
| 188 | + obfuscatedCode = JavaScriptObfuscator.obfuscate( |
| 189 | + code, |
| 190 | + { |
| 191 | + ...NO_ADDITIONAL_NODES_PRESET, |
| 192 | + mangleProperties: true, |
| 193 | + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator |
| 194 | + } |
| 195 | + ).getObfuscatedCode(); |
| 196 | + }); |
| 197 | + |
| 198 | + it('Should mangle property', () => { |
| 199 | + assert.match(obfuscatedCode, regExp); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('Variant #5: reserved dom properties', () => { |
| 204 | + const regExp: RegExp = new RegExp('' + |
| 205 | + 'const foo *= *{' + |
| 206 | + '\'a\': *0x1,' + |
| 207 | + '\'join\': *0x2,' + |
| 208 | + '\'b\': *0x3,' + |
| 209 | + '\'c\': *0x4' + |
| 210 | + '};' + |
| 211 | + 'const baz *= *foo\\[\'a\'] *\\+ *foo\\[\'join\'] *\\+ *foo\\[\'b\'] *\\+ *foo\\[\'c\'];' + |
| 212 | + ''); |
| 213 | + |
| 214 | + let obfuscatedCode: string; |
| 215 | + |
| 216 | + before(() => { |
| 217 | + const code: string = readFileAsString(__dirname + '/fixtures/reserved-properties.js'); |
| 218 | + |
| 219 | + obfuscatedCode = JavaScriptObfuscator.obfuscate( |
| 220 | + code, |
| 221 | + { |
| 222 | + ...NO_ADDITIONAL_NODES_PRESET, |
| 223 | + mangleProperties: true, |
| 224 | + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator |
| 225 | + } |
| 226 | + ).getObfuscatedCode(); |
| 227 | + }); |
| 228 | + |
| 229 | + it('Should mangle non-reserved properties', () => { |
| 230 | + assert.match(obfuscatedCode, regExp); |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + describe('Variant #6: reserved names properties', () => { |
| 235 | + const regExp: RegExp = new RegExp('' + |
| 236 | + 'const foo *= *{' + |
| 237 | + '\'a\': *0x1,' + |
| 238 | + '\'join\': *0x2,' + |
| 239 | + '\'reserved\': *0x3,' + |
| 240 | + '\'private_\': *0x4' + |
| 241 | + '};' + |
| 242 | + 'const baz *= *foo\\[\'a\'] *\\+ *foo\\[\'join\'] *\\+ *foo\\[\'reserved\'] *\\+ *foo\\[\'private_\'];' + |
| 243 | + ''); |
| 244 | + |
| 245 | + let obfuscatedCode: string; |
| 246 | + |
| 247 | + before(() => { |
| 248 | + const code: string = readFileAsString(__dirname + '/fixtures/reserved-properties.js'); |
| 249 | + |
| 250 | + obfuscatedCode = JavaScriptObfuscator.obfuscate( |
| 251 | + code, |
| 252 | + { |
| 253 | + ...NO_ADDITIONAL_NODES_PRESET, |
| 254 | + mangleProperties: true, |
| 255 | + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator, |
| 256 | + reservedNames: ['^reserved$', '_$'] |
| 257 | + } |
| 258 | + ).getObfuscatedCode(); |
| 259 | + }); |
| 260 | + |
| 261 | + it('Should mangle non-reserved properties', () => { |
| 262 | + assert.match(obfuscatedCode, regExp); |
| 263 | + }); |
| 264 | + }); |
| 265 | + |
| 266 | + describe('Variant #7: class methods', () => { |
| 267 | + const regExp: RegExp = new RegExp('' + |
| 268 | + 'class Foo *{' + |
| 269 | + '\\[\'a\'] *\\(\\) *{}' + |
| 270 | + '}' + |
| 271 | + 'const foo *= *new Foo\\(\\);' + |
| 272 | + 'foo\\[\'a\']\\(\\);' + |
| 273 | + ''); |
| 274 | + |
| 275 | + let obfuscatedCode: string; |
| 276 | + |
| 277 | + before(() => { |
| 278 | + const code: string = readFileAsString(__dirname + '/fixtures/class-methods.js'); |
| 279 | + |
| 280 | + obfuscatedCode = JavaScriptObfuscator.obfuscate( |
| 281 | + code, |
| 282 | + { |
| 283 | + ...NO_ADDITIONAL_NODES_PRESET, |
| 284 | + mangleProperties: true, |
| 285 | + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator, |
| 286 | + reservedNames: ['^reserved$', '_$'] |
| 287 | + } |
| 288 | + ).getObfuscatedCode(); |
| 289 | + }); |
| 290 | + |
| 291 | + it('Should mangle class method name', () => { |
| 292 | + assert.match(obfuscatedCode, regExp); |
| 293 | + }); |
| 294 | + }); |
| 295 | + }); |
| 296 | + }); |
| 297 | +}); |
0 commit comments