1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use rollupObject file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17import { expect } from 'chai';
18import mocha from 'mocha';
19import fs from 'fs';
20import path from 'path';
21import sinon from 'sinon';
22import os from "os";
23
24import {
25  OBFUSCATION_TOOL,
26  ESMODULE,
27  RELEASE,
28  TS2ABC
29} from '../../../lib/fast_build/ark_compiler/common/ark_define';
30import {
31  NODE,
32  BUNDLE_NAME_DEFAULT,
33  ENTRY_MODULE_NAME_DEFAULT,
34  NODE_JS_PATH,
35  LOADER_AOTMODE
36} from '../mock/rollup_mock/common';
37import {
38  ES2ABC_PATH,
39  TS2ABC_PATH,
40  MERGERABC_PATH,
41  JS2ABC_PATH,
42  AOTCOMPILER_PATH,
43  WIN_ES2ABC_PATH,
44  WIN_TS2ABC_PATH,
45  WIN_MERGERABC_PATH,
46  WIN_JS2ABC_PATH,
47  WIN_AOTCOMPILER_PATH,
48  MAC_ES2ABC_PATH,
49  MAC_TS2ABC_PATH,
50  MAC_MERGERABC_PATH,
51  MAC_JS2ABC_PATH,
52  MAC_AOTCOMPILER_PATH,
53  ARKROOT_PATH,
54  ARKCONFIG_TS2ABC_PATH
55} from '../mock/rollup_mock/path_config';
56import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock';
57import {
58  initArkConfig,
59  initArkProjectConfig,
60  utProcessArkConfig,
61  readProjectAndLibsSource
62} from '../../../lib/fast_build/ark_compiler/common/process_ark_config';
63import {
64  ObConfigResolver,
65  MergedConfig
66} from '../../../lib/fast_build/ark_compiler/common/ob_config_resolver';
67import { ArkObfuscator } from 'arkguard';
68import { UnobfuscationCollections } from 'arkguard/lib/utils/CommonCollections';
69
70const WINDOWS: string = 'Windows_NT';
71const LINUX: string = 'Linux';
72const MAC: string = 'Darwin';
73const HARMONYOS: string = 'HarmonyOS';
74
75mocha.describe('test process_ark_config file api', function () {
76  mocha.before(function () {
77    this.rollup = new RollUpPluginMock();
78  });
79
80  mocha.after(() => {
81    delete this.rollup;
82  });
83
84  mocha.it('1-1: test initArkConfig under build debug', function () {
85    this.rollup.build();
86    this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir;
87    this.rollup.share.projectConfig.nodeJs = true;
88    this.rollup.share.projectConfig.nodePath = NODE_JS_PATH;
89    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
90
91    expect(arkConfig.nodePath === NODE_JS_PATH).to.be.true;
92    expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true;
93    expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true;
94    expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true;
95    expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true;
96    expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true;
97    expect(arkConfig.isDebug === true).to.be.true;
98    expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true;
99  });
100
101  mocha.it('1-2: test initArkConfig under build release', function () {
102    this.rollup.build(RELEASE);
103    this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir;
104    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
105
106    expect(arkConfig.nodePath === NODE).to.be.true;
107    expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true;
108    expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true;
109    expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true;
110    expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true;
111    expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true;
112    expect(arkConfig.isDebug === false).to.be.true;
113    expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true;
114  });
115
116  mocha.it('1-3: test initArkConfig under preview debug', function () {
117    this.rollup.preview();
118    this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir;
119    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
120
121    expect(arkConfig.nodePath === NODE).to.be.true;
122    expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true;
123    expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true;
124    expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true;
125    expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true;
126    expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true;
127    expect(arkConfig.isDebug === true).to.be.true;
128    expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true;
129  });
130
131  mocha.it('1-4: test initArkConfig under hot reload debug', function () {
132    this.rollup.hotReload();
133    this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir;
134    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
135
136    expect(arkConfig.nodePath === NODE).to.be.true;
137    expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true;
138    expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true;
139    expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true;
140    expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true;
141    expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true;
142    expect(arkConfig.isDebug === true).to.be.true;
143    expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true;
144  });
145
146  mocha.it('2-1-1: test initArkProjectConfig under build debug: moduleJsonInfo exists', function () {
147    this.rollup.build();
148    const arkConfig = initArkProjectConfig(this.rollup.share);
149    const buildJsonInfo =
150      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString());
151    const moduleJsonInfo =
152      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString());
153
154    expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true;
155    expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true;
156    expect(arkConfig.processTs === false).to.be.true;
157    expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true;
158    expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true;
159    expect(arkConfig.compileMode === ESMODULE).to.be.true;
160  });
161
162  mocha.it('2-1-2: test initArkProjectConfig under build debug: buildJsonInfo.patchConfig is true', function () {
163    this.rollup.build();
164    this.rollup.share.projectConfig.aceBuildJson =
165      `${this.rollup.share.projectConfig.aceModuleBuild}/${LOADER_AOTMODE}`;
166    const arkConfig = initArkProjectConfig(this.rollup.share);
167    const buildJsonInfo =
168      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString());
169
170    expect(arkConfig.oldMapFilePath === buildJsonInfo.patchConfig.oldMapFilePath).to.be.true;
171    expect(arkConfig.pandaMode === TS2ABC).to.be.true;
172    expect(arkConfig.processTs === true).to.be.true;
173    expect(arkConfig.anBuildOutPut === buildJsonInfo.anBuildOutPut).to.be.true;
174    expect(arkConfig.anBuildMode === buildJsonInfo.anBuildMode).to.be.true;
175    expect(arkConfig.apPath === buildJsonInfo.apPath).to.be.true;
176    expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true;
177    expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true;
178    expect(arkConfig.compileMode === ESMODULE).to.be.true;
179  });
180
181  mocha.it('2-2: test initArkProjectConfig under build release', function () {
182    this.rollup.build(RELEASE);
183    const arkConfig = initArkProjectConfig(this.rollup.share);
184    const buildJsonInfo =
185      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString());
186    const moduleJsonInfo =
187      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString());
188
189    expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true;
190    expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true;
191    expect(arkConfig.processTs === false).to.be.true;
192    expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true;
193    expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true;
194    expect(arkConfig.compileMode === ESMODULE).to.be.true;
195  });
196
197  mocha.it('2-3: test initArkProjectConfig under preview debug', function () {
198    this.rollup.preview();
199    const arkConfig = initArkProjectConfig(this.rollup.share);
200    const buildJsonInfo =
201      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString());
202    const moduleJsonInfo =
203      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString());
204
205    expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true;
206    expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true;
207    expect(arkConfig.processTs === false).to.be.true;
208    expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true;
209    expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true;
210    expect(arkConfig.compileMode === ESMODULE).to.be.true;
211  });
212
213  mocha.it('2-4: test initArkProjectConfig under hot reload debug', function () {
214    this.rollup.hotReload();
215    const arkConfig = initArkProjectConfig(this.rollup.share);
216    const buildJsonInfo =
217      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString());
218    const moduleJsonInfo =
219      JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString());
220
221    expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true;
222    expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true;
223    expect(arkConfig.processTs === false).to.be.true;
224    expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true;
225    expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true;
226    expect(arkConfig.compileMode === ESMODULE).to.be.true;
227  });
228
229  mocha.it('3-1: test initTerserConfig under build debug', function () {
230    this.rollup.build();
231    const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL);
232    const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true);
233    const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs();
234    const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar;
235    const minifyOptions =
236      utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled);
237
238    expect(minifyOptions.format.beautify === true).to.be.true;
239    expect(minifyOptions.format.indent_level === 2).to.be.true;
240    expect(minifyOptions.compress.join_vars === false).to.be.true;
241    expect(minifyOptions.compress.sequences === 0).to.be.true;
242    expect(minifyOptions.compress.directives === false).to.be.true;
243    expect(minifyOptions.compress.drop_console === false).to.be.true;
244    expect(minifyOptions.mangle.toplevel === false).to.be.true;
245  });
246
247  mocha.it('3-2: test initTerserConfig under build release', function () {
248    this.rollup.build(RELEASE);
249    const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL);
250    const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true);
251    const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs();
252    const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar;
253    const minifyOptions =
254      utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled);
255
256    expect(minifyOptions.format.beautify === true).to.be.true;
257    expect(minifyOptions.format.indent_level === 2).to.be.true;
258    expect(minifyOptions.compress.join_vars === false).to.be.true;
259    expect(minifyOptions.compress.sequences === 0).to.be.true;
260    expect(minifyOptions.compress.directives === false).to.be.true;
261    expect(minifyOptions.compress.drop_console === false).to.be.true;
262    expect(minifyOptions.mangle.toplevel === false).to.be.true;
263  });
264
265  mocha.it('3-3: test initTerserConfig under preview debug', function () {
266    this.rollup.preview();
267    const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL);
268    const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true);
269    const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs();
270    const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar;
271    const minifyOptions =
272      utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled);
273
274    expect(minifyOptions.format.beautify === true).to.be.true;
275    expect(minifyOptions.format.indent_level === 2).to.be.true;
276    expect(minifyOptions.compress.join_vars === false).to.be.true;
277    expect(minifyOptions.compress.sequences === 0).to.be.true;
278    expect(minifyOptions.compress.directives === false).to.be.true;
279    expect(minifyOptions.compress.drop_console === false).to.be.true;
280    expect(minifyOptions.mangle.toplevel === false).to.be.true;
281  });
282
283  mocha.it('3-4: test initTerserConfig under hot reload debug', function () {
284    this.rollup.hotReload();
285    const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL);
286    const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true);
287    const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs();
288    const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar;
289    const minifyOptions =
290      utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled);
291
292    expect(minifyOptions.format.beautify === true).to.be.true;
293    expect(minifyOptions.format.indent_level === 2).to.be.true;
294    expect(minifyOptions.compress.join_vars === false).to.be.true;
295    expect(minifyOptions.compress.sequences === 0).to.be.true;
296    expect(minifyOptions.compress.directives === false).to.be.true;
297    expect(minifyOptions.compress.drop_console === false).to.be.true;
298    expect(minifyOptions.mangle.toplevel === false).to.be.true;
299  });
300
301  mocha.it('4-1: test processCompatibleVersion under build debug', function () {
302    this.rollup.build();
303    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
304    utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig);
305    expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true;
306    expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true;
307
308    this.rollup.share.projectConfig.minPlatformVersion = 8;
309    utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig);
310    expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true;
311    expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true;
312  });
313
314  mocha.it('4-2: test processCompatibleVersion under build release', function () {
315    this.rollup.build(RELEASE);
316    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
317    utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig);
318    expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true;
319    expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true;
320
321    this.rollup.share.projectConfig.minPlatformVersion = 8;
322    utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig);
323    expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true;
324    expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true;
325  });
326
327  mocha.it('4-3: test processCompatibleVersion under preview debug', function () {
328    this.rollup.preview();
329    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
330    utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig);
331    expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true;
332    expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true;
333
334    this.rollup.share.projectConfig.minPlatformVersion = 8;
335    utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig);
336    expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true;
337    expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true;
338  });
339
340  mocha.it('4-4: test processCompatibleVersion under hot reload debug', function () {
341    this.rollup.hotReload();
342    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
343    utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig);
344    expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true;
345    expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true;
346
347    this.rollup.share.projectConfig.minPlatformVersion = 8;
348    utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig);
349    expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true;
350    expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true;
351  });
352
353  mocha.describe('5: test readProjectAndLibsSource api', function () {
354    let mergedObConfig: MergedConfig = {};
355    let keepFilesAndDependencies: Set<string> = new Set();
356    const targetFile: string = path.join(__dirname, '../../../test/ark_compiler_ut/testdata/obfuscation/third_package/oh_modules/Index.ts');
357    const allFiles: Set<string> = new Set([targetFile]);
358    let arkguardConfig: Object = {};
359    const languageWhiteListNum = 7900;
360    mocha.before(function () {
361      this.rollup = new RollUpPluginMock();
362      mergedObConfig = {
363        options: {
364          disableObfuscation: false,
365          enablePropertyObfuscation: false,
366          enableStringPropertyObfuscation: false,
367          enableToplevelObfuscation: false,
368          enableFileNameObfuscation: false,
369          enableExportObfuscation: true,
370          removeComments: false,
371          compact:false,
372          removeLog:  false,
373          printNameCache: '',
374          applyNameCache: ''
375        }
376      };
377      arkguardConfig = {
378        mNameObfuscation: {
379          mEnable: true,
380          mNameGeneratorType: 1,
381          mRenameProperties: false,
382          mReservedProperties: [],
383          mTopLevel: false,
384          mReservedToplevelNames:[],
385        },
386        mExportObfuscation: true,
387        mPerformancePrinter: []
388      };
389    });
390
391    mocha.it('5-1: test readProjectAndLibsSource with export obfuscation', function () {
392      let arkObfuscator: ArkObfuscator = new ArkObfuscator();
393      arkObfuscator.init(arkguardConfig);
394      readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies);
395
396      expect(UnobfuscationCollections.reservedExportName.has('foo1')).to.be.true;
397      expect(UnobfuscationCollections.reservedExportName.has('foo2')).to.be.true;
398      expect(UnobfuscationCollections.reservedExportName.has('ts')).to.be.true;
399      expect(UnobfuscationCollections.reservedExportName.has('foo5')).to.be.true;
400      expect(UnobfuscationCollections.reservedExportName.has('foo6')).to.be.true;
401      expect(UnobfuscationCollections.reservedExportName.size === 5).to.be.true;
402      expect(UnobfuscationCollections.reservedLangForProperty.size > languageWhiteListNum).to.be.true;
403      UnobfuscationCollections.clear();
404    });
405
406    mocha.it('5-2: test readProjectAndLibsSource with export & toplevel obfuscation', function () {
407      let arkObfuscator: ArkObfuscator = new ArkObfuscator();
408      arkguardConfig.mNameObfuscation.mTopLevel = true;
409      arkObfuscator.init(arkguardConfig);
410      mergedObConfig.options.enableToplevelObfuscation = true;
411      readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies);
412
413      expect(UnobfuscationCollections.reservedExportName.has('foo1')).to.be.true;
414      expect(UnobfuscationCollections.reservedExportName.has('foo2')).to.be.true;
415      expect(UnobfuscationCollections.reservedExportName.has('ts')).to.be.true;
416      expect(UnobfuscationCollections.reservedExportName.has('foo5')).to.be.true;
417      expect(UnobfuscationCollections.reservedExportName.has('foo6')).to.be.true;
418      expect(UnobfuscationCollections.reservedExportName.size === 5).to.be.true;
419      expect(UnobfuscationCollections.reservedLangForProperty.size > languageWhiteListNum).to.be.true;
420      UnobfuscationCollections.clear();
421    });
422
423    mocha.it('5-3: test readProjectAndLibsSource with property obfuscation', function () {
424      arkguardConfig.mNameObfuscation.mRenameProperties = true;
425      arkguardConfig.mExportObfuscation = false;
426      arkguardConfig.mNameObfuscation.mTopLevel = false;
427      let arkObfuscator: ArkObfuscator = new ArkObfuscator();
428      arkObfuscator.init(arkguardConfig);
429      mergedObConfig.options.enablePropertyObfuscation = true;
430      mergedObConfig.options.enableExportObfuscation = false;
431      mergedObConfig.options.enableToplevelObfuscation = false;
432      readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies);
433
434      expect(UnobfuscationCollections.reservedExportName.size === 0).to.be.true;
435      expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo1')).to.be.true;
436      expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo2')).to.be.true;
437      expect(UnobfuscationCollections.reservedExportNameAndProp.has('ts')).to.be.true;
438      expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo5')).to.be.true;
439      expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo6')).to.be.true;
440      expect(UnobfuscationCollections.reservedExportNameAndProp.has('prop21')).to.be.true;
441      expect(UnobfuscationCollections.reservedExportNameAndProp.size === 6).to.be.true;
442      UnobfuscationCollections.clear();
443    });
444
445    mocha.it('5-4: test readProjectAndLibsSource with export & property & toplevel obfuscation', function () {
446      arkguardConfig.mNameObfuscation.mRenameProperties = true;
447      arkguardConfig.mExportObfuscation = true;
448      arkguardConfig.mNameObfuscation.mTopLevel = true;
449      let arkObfuscator: ArkObfuscator = new ArkObfuscator();
450      arkObfuscator.init(arkguardConfig);
451      mergedObConfig.options.enablePropertyObfuscation = true;
452      mergedObConfig.options.enableToplevelObfuscation = true;
453      mergedObConfig.options.enableExportObfuscation = true;
454      readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies);
455
456      expect(UnobfuscationCollections.reservedExportName.has('foo1')).to.be.true;
457      expect(UnobfuscationCollections.reservedExportName.has('foo2')).to.be.true;
458      expect(UnobfuscationCollections.reservedExportName.has('ts')).to.be.true;
459      expect(UnobfuscationCollections.reservedExportName.has('foo5')).to.be.true;
460      expect(UnobfuscationCollections.reservedExportName.has('foo6')).to.be.true;
461      expect(UnobfuscationCollections.reservedExportName.size === 5).to.be.true;
462      expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo1')).to.be.true;
463      expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo2')).to.be.true;
464      expect(UnobfuscationCollections.reservedExportNameAndProp.has('ts')).to.be.true;
465      expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo5')).to.be.true;
466      expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo6')).to.be.true;
467      expect(UnobfuscationCollections.reservedExportNameAndProp.has('prop21')).to.be.true;
468      expect(UnobfuscationCollections.reservedExportNameAndProp.size === 6).to.be.true;
469      UnobfuscationCollections.clear();
470    });
471  });
472
473  mocha.it('6-1: test processPlatformInfo on windows', function () {
474    this.rollup.build();
475    const stub = sinon.stub(os, 'type').returns(WINDOWS);
476    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
477    const expectEs2abcPath = path.join(ARKROOT_PATH, WIN_ES2ABC_PATH);
478    const expectTs2abcPath = path.join(ARKROOT_PATH, WIN_TS2ABC_PATH);
479    const expectMergeAbcPath = path.join(ARKROOT_PATH, WIN_MERGERABC_PATH);
480    const expectJs2abcPath = path.join(ARKROOT_PATH, WIN_JS2ABC_PATH);
481    const expectAotCompilerPath = path.join(ARKROOT_PATH, WIN_AOTCOMPILER_PATH);
482    expect(arkConfig.es2abcPath === expectEs2abcPath).to.be.true;
483    expect(arkConfig.ts2abcPath === expectTs2abcPath).to.be.true;
484    expect(arkConfig.mergeAbcPath === expectMergeAbcPath).to.be.true;
485    expect(arkConfig.js2abcPath === expectJs2abcPath).to.be.true;
486    expect(arkConfig.aotCompilerPath === expectAotCompilerPath).to.be.true;
487    stub.restore();
488  });
489
490  mocha.it('6-2: test processPlatformInfo on linux', function () {
491    this.rollup.build();
492    const stub = sinon.stub(os, 'type').returns(LINUX);
493    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
494    const expectEs2abcPath = path.join(ARKROOT_PATH, ES2ABC_PATH);
495    const expectTs2abcPath = path.join(ARKROOT_PATH, TS2ABC_PATH);
496    const expectMergeAbcPath = path.join(ARKROOT_PATH, MERGERABC_PATH);
497    const expectJs2abcPath = path.join(ARKROOT_PATH, JS2ABC_PATH);
498    const expectAotCompilerPath = path.join(ARKROOT_PATH, AOTCOMPILER_PATH);
499    expect(arkConfig.es2abcPath === expectEs2abcPath).to.be.true;
500    expect(arkConfig.ts2abcPath === expectTs2abcPath).to.be.true;
501    expect(arkConfig.mergeAbcPath === expectMergeAbcPath).to.be.true;
502    expect(arkConfig.js2abcPath === expectJs2abcPath).to.be.true;
503    expect(arkConfig.aotCompilerPath === expectAotCompilerPath).to.be.true;
504    stub.restore();
505  });
506
507  mocha.it('6-3: test processPlatformInfo on mac', function () {
508    this.rollup.build();
509    const stub = sinon.stub(os, 'type').returns(MAC);
510    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
511    const expectEs2abcPath = path.join(ARKROOT_PATH, MAC_ES2ABC_PATH);
512    const expectTs2abcPath = path.join(ARKROOT_PATH, MAC_TS2ABC_PATH);
513    const expectMergeAbcPath = path.join(ARKROOT_PATH, MAC_MERGERABC_PATH);
514    const expectJs2abcPath = path.join(ARKROOT_PATH, MAC_JS2ABC_PATH);
515    const expectAotCompilerPath = path.join(ARKROOT_PATH, MAC_AOTCOMPILER_PATH);
516    expect(arkConfig.es2abcPath === expectEs2abcPath).to.be.true;
517    expect(arkConfig.ts2abcPath === expectTs2abcPath).to.be.true;
518    expect(arkConfig.mergeAbcPath === expectMergeAbcPath).to.be.true;
519    expect(arkConfig.js2abcPath === expectJs2abcPath).to.be.true;
520    expect(arkConfig.aotCompilerPath === expectAotCompilerPath).to.be.true;
521    stub.restore();
522  });
523
524  mocha.it('6-4: test processPlatformInfo on harmonyos', function () {
525    this.rollup.build();
526    const stub = sinon.stub(os, 'type').returns(HARMONYOS);
527    const arkConfig = initArkConfig(this.rollup.share.projectConfig);
528    const expectEs2abcPath = path.join(ARKROOT_PATH, ES2ABC_PATH);
529    expect(arkConfig.es2abcPath === expectEs2abcPath).to.be.true;
530    stub.restore();
531  });
532});