8000 Updated test for BaseIdentifierObfuscatingReplacer and ArrayStorage · sec-js/javascript-obfuscator@e404b1d · GitHub
[go: up one dir, main page]

Skip to content

Commit e404b1d

Browse files
author
sanex3339
committed
Updated test for BaseIdentifierObfuscatingReplacer and ArrayStorage
1 parent 515e438 commit e404b1d

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

test/functional-tests/node-transformers/obfuscating-transformers/obfuscating-replacers/identifier-obfuscating-replacers/BaseIdentifierObfuscatingReplacer.spec.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { readFileAsString } from '../../../../../helpers/readFileAsString';
77
import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
88

99
describe('BaseIdentifierObfuscatingReplacer', () => {
10-
describe('Base rule', () => {
11-
describe('Variant #1: default behaviour', () => {
10+
describe('Reserved names', () => {
11+
describe('Variant #1: ignore local reserved names', () => {
1212
let obfuscatedCode: string;
1313

1414
before(() => {
15-
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
15+
const code: string = readFileAsString(__dirname + '/fixtures/local-reserved-names.js');
1616

1717
obfuscatedCode = JavaScriptObfuscator.obfuscate(
1818
code,
@@ -30,5 +30,29 @@ describe('BaseIdentifierObfuscatingReplacer', () => {
3030
);
3131
});
3232
});
33+
34+
describe('Variant #1: ignore global reserved names', () => {
35+
let obfuscatedCode: string;
36+
37+
before(() => {
38+
const code: string = readFileAsString(__dirname + '/fixtures/global-reserved-names.js');
39+
40+
obfuscatedCode = JavaScriptObfuscator.obfuscate(
41+
code,
42+
{
43+
...NO_ADDITIONAL_NODES_PRESET,
44+
renameGlobals: true,
45+
reservedNames: ['[abc|ghi]']
46+
}
47+
).getObfuscatedCode();
48+
});
49+
50+
it('Should keep reserved names without transformations when `reservedNames` option is enabled', () => {
51+
assert.match(
52+
obfuscatedCode,
53+
/var *abc *= *0x1; *var *_0x([a-f0-9]){4,6} *= *0x2; *var *ghi *= *0x3;/
54+
);
55+
});
56+
});
3357
});
3458
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var abc = 1;
2+
var def = 2;
3+
var ghi = 3;

test/unit-tests/storages/ArrayStorage.spec.ts

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ describe('ArrayStorage', () => {
7474
});
7575
});
7676

77+
describe('getStorageId', () => {
78+
const storageIdRegExp: RegExp = /^[a-zA-Z0-9]{6}$/;
79+
80+
let storageId: string;
81+
82+
before(() => {
83+
storage = getStorageInstance<string>();
84+
storage.set(storageKey, storageValue);
85+
86+
storageId = storage.getStorageId();
87+
});
88+
89+
it('should return storage id', () => {
90+
assert.match(storageId, storageIdRegExp);
91+
});
92+
});
93+
7794
describe('get', () => {
7895
describe('Variant #1: value exist', () => {
7996
const expectedValue: string = storageValue;
@@ -194,27 +211,62 @@ describe('ArrayStorage', () => {
194211
});
195212

196213
describe('mergeWith', () => {
197-
const secondStorageKey: number = 1;
198-
const secondStorageValue: string = 'bar';
214+
describe('Base merge', () => {
215+
const secondStorageKey: number = 1;
216+
const secondStorageValue: string = 'bar';
199217

200-
const expectedArray: string[] = [storageValue, secondStorageValue];
218+
const expectedArray: string[] = [storageValue, secondStorageValue];
201219

202-
let array: string[];
220+
let array: string[];
203221

204-
before(() => {
205-
storage = getStorageInstance<string>();
206-
storage.set(storageKey, storageValue);
222+
before(() => {
223+
storage = getStorageInstance<string>();
224+
storage.set(storageKey, storageValue);
207225

208-
const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
209-
secondStorage.set(secondStorageKey, secondStorageValue);
226+
const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
227+
secondStorage.set(secondStorageKey, secondStorageValue);
210228

211-
storage.mergeWith(secondStorage, false);
229+
storage.mergeWith(secondStorage, false);
212230

213-
array = storage.getStorage();
231+
array = storage.getStorage();
232+
});
233+
234+
it('should merge two storages', () => {
235+
assert.deepEqual(array, expectedArray);
236+
});
214237
});
215238

216-
it('should merge two storages', () => {
217-
assert.deepEqual(array, expectedArray);
239+
describe('Merge with storage id', () => {
240+
const secondStorageKey: number = 1;
241+
const secondStorageValue: string = 'bar';
242+
243+
const expectedArray: string[] = [storageValue, secondStorageValue];
244+
245+
let array: string[];
246+
let storageId: string;
247+
let expectedStorageId: string;
248+
249+
before(() => {
250+
storage = getStorageInstance<string>();
251+
storage.set(storageKey, storageValue);
252+
253+
const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
254+
expectedStorageId = secondStorage.getStorageId();
255+
secondStorage.set(secondStorageKey, secondStorageValue);
256+
257+
storage.mergeWith(secondStorage, true);
258+
259+
storageId = storage.getStorageId();
260+
array = storage.getStorage();
261+
});
262+
263+
it('should update storage id', () => {
264+
assert.deepEqual(storageId, expectedStorageId);
265+
});
266+
267+
it('should merge two storages', () => {
268+
assert.deepEqual(array, expectedArray);
269+
});
218270
});
219271
});
220272
});

0 commit comments

Comments
 (0)
0