-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathoptions.test.ts
303 lines (258 loc) · 6.91 KB
/
options.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import JsConfuser from "../src/index";
import { ObfuscateOptions } from "../src/options";
test("Variant #1: Accept percentages", async () => {
var { code: output } = await JsConfuser.obfuscate(`var TEST_VARIABLE;`, {
target: "node",
renameGlobals: true,
renameVariables: true,
stringConcealing: 0.5,
});
expect(output).not.toContain("TEST_VARIABLE");
});
test("Variant #2: Accept probability arrays", async () => {
var { code: output } = await JsConfuser.obfuscate(`var TEST_VARIABLE;`, {
target: "node",
renameVariables: true,
renameGlobals: true,
identifierGenerator: [
"hexadecimal",
"mangled",
] as ObfuscateOptions["identifierGenerator"], // half hexadecimal, half randomized
});
expect(output).not.toContain("TEST_VARIABLE");
});
test("Variant #3: Accept probability maps", async () => {
var { code: output } = await JsConfuser.obfuscate(`var TEST_VARIABLE;`, {
target: "node",
renameVariables: true,
renameGlobals: true,
identifierGenerator: {
// 25% each
hexadecimal: 0.25,
randomized: 0.25,
mangled: 0.25,
number: 0.25,
},
});
expect(output).not.toContain("TEST_VARIABLE");
});
test("Variant #4: Work with compact set to false", async () => {
var { code: output } = await JsConfuser.obfuscate(
`
var a;
var b;
var c;
`,
{
target: "node",
compact: false,
}
);
expect(output).toContain("\n");
expect(output).toContain("var a;\nvar b;\nvar c;");
});
test("Variant #5: Work with compact set to true", async () => {
var { code: output } = await JsConfuser.obfuscate(
`
var a;
var b;
var c;
`,
{
target: "node",
compact: true,
}
);
expect(output).not.toContain("\n");
expect(output).not.toContain("\t");
expect(output).toContain("var a;var b;var c;");
});
test("Variant #6: Verbose option", async () => {
var { code } = await JsConfuser.obfuscate(
`
var object = { key: 1 };
TEST_OUTPUT = object.key;
`,
{
target: "node",
compact: false,
verbose: true,
objectExtraction: true,
}
);
var TEST_OUTPUT;
eval(code);
expect(TEST_OUTPUT).toStrictEqual(1);
});
test("Variant #7: Error on invalid lock option", async () => {
expect(
JsConfuser.obfuscate(`var TEST_VARIABLE;`, {
target: "node",
lock: "invalid",
} as any)
).rejects.toThrow();
expect(
JsConfuser.obfuscate(`var TEST_VARIABLE;`, {
target: "node",
lock: {
invalidProperty: true,
},
} as any)
).rejects.toThrow();
});
test("Variant #8: Error on invalid target values", async () => {
var invalid: any = {
target: "__invalid__target__",
};
await expect(async () => {
return await JsConfuser.obfuscate("5+5", invalid);
}).rejects.toThrow();
});
test("Variant #9: Error when target property missing", async () => {
var invalid: any = {
objectExtraction: true,
};
await expect(async () => {
return await JsConfuser.obfuscate("5+5", invalid);
}).rejects.toThrow();
});
test("Variant #10: Error when invalid options are passed in", async () => {
var invalid: any = {
target: "browser",
__invalid__prop__: true,
};
await expect(async () => {
return await JsConfuser.obfuscate("5+5", invalid);
}).rejects.toThrow();
});
test("Variant #11: Error when invalid startDate is passed in", async () => {
var invalid: any = {
target: "browser",
lock: {
startDate: "__invalid__date__object__",
},
};
await expect(async () => {
return await JsConfuser.obfuscate("5+5", invalid);
}).rejects.toThrow();
});
test("Variant #12: Error when invalid endDate is passed in", async () => {
var invalid: any = {
target: "browser",
lock: {
endDate: "__invalid__date__object__",
},
};
await expect(async () => {
return await JsConfuser.obfuscate("5+5", invalid);
}).rejects.toThrow();
});
test("Variant #13: Error when source code is not a string", async () => {
await expect(async () => {
return await JsConfuser.obfuscate({} as any, {
target: "node",
preset: "low",
});
}).rejects.toThrow();
});
test("Variant #14: Rename Globals should accept a callback function", async () => {
var globalsCollected: string[] = [];
var { code } = await JsConfuser.obfuscate(
`
var renameMe = true;
var doNotRenameMe = false;
TEST_OUTPUT = [renameMe, doNotRenameMe]
`,
{
target: "node",
renameVariables: true,
renameGlobals: (globalName) => {
globalsCollected.push(globalName);
if (globalName === "doNotRenameMe") {
return false;
}
return true;
},
}
);
// Ensure renameGlobals callback was called
expect(globalsCollected).toContain("renameMe");
expect(globalsCollected).toContain("doNotRenameMe");
// Ensure code was changed correctly
expect(code).not.toContain("renameMe");
expect(code).toContain("doNotRenameMe");
// Ensure code still works
var TEST_OUTPUT;
eval(code);
expect(TEST_OUTPUT).toStrictEqual([true, false]);
});
test("Variant #15: Fine-tune options using the limit property", async () => {
var { code } = await JsConfuser.obfuscate(
`
var renameMyVar1 = 1;
var renameMyVar2 = 2;
var keepMyVar3 = 3;
var keepMyVar4 = 4;
var keepMyVar5 = 5;
`,
{
target: "node",
renameVariables: {
value: true,
limit: 2,
},
identifierGenerator: "mangled",
}
);
// Ensure the first two variables were renamed
expect(code).not.toContain("renameMyVar1");
expect(code).not.toContain("renameMyVar2");
expect(code).toContain("var a");
expect(code).toContain("var b");
// Ensure the remaining variables were not renamed
expect(code).toContain("keepMyVar3");
expect(code).toContain("keepMyVar4");
expect(code).toContain("keepMyVar5");
});
test("Variant #16: Limit of not should rename any variables", async () => {
var { code } = await JsConfuser.obfuscate(
`
var renameMyVar1 = 1;
var renameMyVar2 = 2;
`,
{
target: "node",
renameVariables: {
value: true,
limit: 0,
},
identifierGenerator: "mangled",
}
);
// Ensure the variable names were preserved
expect(code).toContain("renameMyVar1");
expect(code).toContain("renameMyVar2");
expect(code).not.toContain("var a");
expect(code).not.toContain("var b");
});
test("Variant #17: Limit of -1 should rename all variables", async () => {
var { code } = await JsConfuser.obfuscate(
`
var renameMyVar1 = 1;
var renameMyVar2 = 2;
`,
{
target: "node",
renameVariables: {
value: true,
limit: -1,
},
identifierGenerator: "mangled",
}
);
// Ensure both variables were renamed
expect(code).not.toContain("renameMyVar1");
expect(code).not.toContain("renameMyVar2");
expect(code).toContain("var a");
expect(code).toContain("var b");
});