• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this 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
16import {
17  ObConfigResolver,
18  SourceObConfig,
19} from '../../../src/initialization/ConfigResolver';
20
21import { HvigorErrorInfo, PropCollections, renameFileNameModule } from '../../../src/ArkObfuscator';
22
23import { describe, it } from 'mocha';
24import { expect } from 'chai';
25import path from 'path';
26import fs from 'fs';
27import exp from 'constants';
28
29function printObfLogger(errorInfo: string, errorCodeInfo: HvigorErrorInfo | string, level: string): void {
30  switch (level) {
31    case 'warn':
32      console.warn(errorInfo);
33      break;
34    case 'error':
35      console.error(errorInfo);
36      break;
37    default:
38      break;
39  }
40}
41
42describe('hsp support consumerFiles', () => {
43  describe('hsp config resolver',() => {
44    let hapConfig: SourceObConfig;
45    let localHspConfig: SourceObConfig;
46    let remoteHspConfig: SourceObConfig;
47    let remoteHarConfig: SourceObConfig;
48    let localHarConfig: SourceObConfig;
49    let localMainHspConfig: SourceObConfig;
50    let remoteMainHspConfig: SourceObConfig;
51    let libTestHapConfig: SourceObConfig;
52    const hapRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/Hap/obfuscation-rules.txt');
53    const outputDir = path.join(__dirname, '../../testData/output');
54    const localHspRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/localHsp/obfuscation-rules.txt');
55    const localHspExportRulePathFile = path.join(__dirname, '../../testData/output/localHsp_obfuscation.txt');
56    const localHspConsumerRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/localHsp/consumer-rules.txt');
57    const remoteHspRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteHsp/obfuscation-rules.txt');
58    const remoteHspConsumerRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteHsp/consumer-rules.txt');
59    const remoteHspExportRulePathFile = path.join(__dirname, '../../testData/output/remoteHsp_obfuscation.txt');
60    const remoteHspObfFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteHsp/obfuscation.txt');
61    const localHarRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/localHar/obfuscation-rules.txt');
62    const localHarConsumerRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/localHar/consumer-rules.txt');
63    const localHarExportRulePathFile = path.join(__dirname, '../../testData/output/localHar_obfuscation.txt');
64    const remoteHarRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteHar/obfuscation-rules.txt');
65    const remoteHarConsumerRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteHar/consumer-rules.txt');
66    const remoteHarExportRulePathFile = path.join(__dirname, '../../testData/output/remoteHar_obfuscation.txt');
67    const remoteHarObfFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteHar/obfuscation.txt');
68    const remoteHarDependsLocalHspObfFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteHar/obfuscationDependLocalHsp.txt');
69    const remoteHarDependsRemoteHspObfFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteHar/obfuscationDependRemoteHsp.txt');
70    const localMainHspRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/localMainHsp/obfuscation-rules.txt');
71    const localMainHspConsumerRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/localMainHsp/consumer-rules.txt');
72    const localMainHspExportRulePathFile = path.join(__dirname, '../../testData/output/localMainHsp_obfuscation.txt');
73    const remoteMainHspRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteMainHsp/obfuscation-rules.txt');
74    const remoteMainHspConsumerRulesFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteMainHsp/consumer-rules.txt');
75    const remoteMainHspExportRulePathFile = path.join(__dirname, '../../testData/output/remoteMainHsp_obfuscation.txt');
76    const remoteMainHspObfFile = path.join(__dirname, '../../testData/obfuscation/Configs/remoteMainHsp/obfuscation.txt');
77    const systemApiCacheFile = path.join(__dirname, '../../testData/output/systemApiCache.json');
78    const libTestHapRulesFile = path.join(
79      __dirname,
80      '../../testData/obfuscation/Configs/Hap/obfuscation-rules-lib-test.txt',
81    );
82    isConfigFileExist(remoteHarObfFile);
83    isConfigFileExist(hapRulesFile);
84    isConfigFileExist(localHspRulesFile);
85    isConfigFileExist(localHspConsumerRulesFile);
86    isConfigFileExist(remoteHspRulesFile);
87    isConfigFileExist(remoteHspConsumerRulesFile);
88    isConfigFileExist(localHarRulesFile);
89    isConfigFileExist(localHarConsumerRulesFile);
90    isConfigFileExist(remoteHarRulesFile);
91    isConfigFileExist(remoteHarConsumerRulesFile);
92    isConfigFileExist(localMainHspRulesFile);
93    isConfigFileExist(localMainHspConsumerRulesFile);
94    isConfigFileExist(remoteMainHspRulesFile);
95    isConfigFileExist(remoteMainHspConsumerRulesFile);
96    isConfigFileExist(remoteMainHspObfFile);
97    isConfigFileExist(remoteHarObfFile);
98    isConfigFileExist(remoteHarDependsLocalHspObfFile);
99    isConfigFileExist(remoteHarDependsRemoteHspObfFile);
100    isConfigFileExist(remoteHspObfFile);
101
102    function initModulesConfig(): void {
103      hapConfig = {
104        selfConfig: {
105          ruleOptions: {
106            enable: true,
107            rules: [hapRulesFile]
108          },
109          consumerRules: [],
110          libDir: "",
111          consumerFiles: ""
112        },
113        dependencies: {
114          libraries: [],
115          hars: [],
116          hsps: [],
117          hspLibraries: []
118        },
119        sdkApis: [],
120        obfuscationCacheDir: outputDir,
121        exportRulePath: ''
122      };
123      localHspConfig = {
124        selfConfig: {
125          ruleOptions: {
126            enable: true,
127            rules: [localHspRulesFile]
128          },
129          consumerRules: [localHspConsumerRulesFile],
130          libDir: "",
131          consumerFiles: ""
132        },
133        dependencies: { libraries: [], hars: [], hsps: [], hspLibraries: [] },
134        sdkApis: [],
135        obfuscationCacheDir: outputDir,
136        exportRulePath: localHspExportRulePathFile
137      };
138      remoteHspConfig = {
139        selfConfig: {
140          ruleOptions: {
141            enable: true,
142            rules: [remoteHspRulesFile]
143          },
144          consumerRules: [remoteHspConsumerRulesFile],
145          libDir: "",
146          consumerFiles: ""
147        },
148        dependencies: { libraries: [], hars: [], hsps: [], hspLibraries: [] },
149        sdkApis: [],
150        obfuscationCacheDir: outputDir,
151        exportRulePath: remoteHspExportRulePathFile
152      };
153      remoteHarConfig = {
154        selfConfig: {
155          ruleOptions: {
156            enable: true,
157            rules: [remoteHarRulesFile]
158          },
159          consumerRules: [remoteHarConsumerRulesFile],
160          libDir: "",
161          consumerFiles: ""
162        },
163        dependencies: { libraries: [], hars: [], hsps: [], hspLibraries: [] },
164        sdkApis: [],
165        obfuscationCacheDir: outputDir,
166        exportRulePath: remoteHarExportRulePathFile
167      };
168      localHarConfig= {
169        selfConfig: {
170          ruleOptions: {
171            enable: true,
172            rules: [localHarRulesFile]
173          },
174          consumerRules: [localHarConsumerRulesFile],
175          libDir: "",
176          consumerFiles: ""
177        },
178        dependencies: { libraries: [], hars: [], hsps: [], hspLibraries: [] },
179        sdkApis: [],
180        obfuscationCacheDir: outputDir,
181        exportRulePath: localHarExportRulePathFile
182      };
183      localMainHspConfig = {
184        selfConfig: {
185          ruleOptions: {
186            enable: true,
187            rules: [localMainHspRulesFile]
188          },
189          consumerRules: [localMainHspConsumerRulesFile],
190          libDir: "",
191          consumerFiles: ""
192        },
193        dependencies: {
194          libraries: [],
195          hars: [],
196          hsps: [],
197          hspLibraries: []
198        },
199        sdkApis: [],
200        obfuscationCacheDir: outputDir,
201        exportRulePath: localMainHspExportRulePathFile
202      };
203      remoteMainHspConfig = {
204        selfConfig: {
205          ruleOptions: {
206            enable: true,
207            rules: [remoteMainHspRulesFile]
208          },
209          consumerRules: [remoteMainHspConsumerRulesFile],
210          libDir: "",
211          consumerFiles: ""
212        },
213        dependencies: {
214          libraries: [],
215          hars: [],
216          hsps: [],
217          hspLibraries: []
218        },
219        sdkApis: [],
220        obfuscationCacheDir: outputDir,
221        exportRulePath: remoteMainHspExportRulePathFile
222      };
223      libTestHapConfig = {
224        selfConfig: {
225          ruleOptions: {
226            enable: true,
227            rules: [libTestHapRulesFile],
228          },
229          consumerRules: [],
230          libDir: '',
231          consumerFiles: '',
232        },
233        dependencies: {
234          libraries: [],
235          hars: [],
236          hsps: [],
237          hspLibraries: [],
238        },
239        sdkApis: [],
240        obfuscationCacheDir: outputDir,
241        exportRulePath: '',
242      };
243    }
244
245    function isConfigFileExist(filePath: string): void {
246      if(!fs.existsSync(filePath)){
247        throw new Error(`${filePath} does not exist`);
248      }
249    }
250
251    describe('getDependencyConfigs with hsps and hspLibrarys', ()=> {
252      describe('HSP Depends on Har Tests', function () {
253        before(async () => {
254          initModulesConfig();
255        });
256
257        it('should include only HSP consumerFiles rules in HSP obfuscation.txt', function () {
258          localHspConfig.dependencies.libraries.push(localHarConfig.selfConfig);
259          localHspConfig.dependencies.hars.push(remoteHarObfFile);
260          const hspResolver = new ObConfigResolver({ obfuscationOptions: localHspConfig, compileHar: false, compileShared: true }, printObfLogger);
261          hspResolver.resolveObfuscationConfigs();
262
263          const hspObfuscationContent = fs.readFileSync(localHspConfig.exportRulePath, 'utf-8');
264          expect(hspObfuscationContent).not.to.include('-disable-obfuscation');
265          expect(hspObfuscationContent).to.include('-enable-toplevel-obfuscation');
266          expect(hspObfuscationContent).not.to.include('-enable-property-obfuscation');
267          expect(hspObfuscationContent).not.to.include('-enable-string-property-obfuscation');
268          expect(hspObfuscationContent).not.to.include('-enable-filename-obfuscation');
269          expect(hspObfuscationContent).not.to.include('-enable-export-obfuscation');
270          expect(hspObfuscationContent).not.to.include('-remove-comments');
271          expect(hspObfuscationContent).not.to.include('-compact');
272          expect(hspObfuscationContent).not.to.include('-remove-log');
273
274          expect(hspObfuscationContent).not.to.include('-keep-dts');
275          expect(hspObfuscationContent).to.include('-keep-global-name');
276          expect(hspObfuscationContent).to.include('-keep-property-name');
277          expect(hspObfuscationContent).not.to.include('-keep-file-name');
278          expect(hspObfuscationContent).not.to.include('-keep-comments');
279
280          expect(hspObfuscationContent).to.include('localHspMethod');
281          expect(hspObfuscationContent).to.include('LocalHspClass');
282          expect(hspObfuscationContent).not.to.include('LocalHarClass');
283          expect(hspObfuscationContent).not.to.include('localHarMethod');
284          expect(hspObfuscationContent).not.to.include('RemoteHarClass');
285          expect(hspObfuscationContent).not.to.include('remoteHarMethod');
286        });
287
288        it('should merge HAR and HSP consumer rules for HAP when HAP depends on localHsp and localHsp depends on localHar', function () {
289          hapConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig);
290          const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger);
291          const finalConfig = configResolver.resolveObfuscationConfigs();
292          expect(finalConfig.options.enableToplevelObfuscation).to.be.false;
293          expect(finalConfig.options.enableExportObfuscation).to.be.true;
294          expect(finalConfig.options.enablePropertyObfuscation).to.be.false;
295          expect(finalConfig.options.disableObfuscation).to.be.false;
296          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
297          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
298          expect(finalConfig.options.printKeptNames).to.be.false;
299          expect(finalConfig.options.removeComments).to.be.false;
300          expect(finalConfig.options.compact).to.be.false;
301          expect(finalConfig.options.removeLog).to.be.false;
302
303          expect(finalConfig.reservedGlobalNames).to.include('LocalHspClass');
304          expect(finalConfig.reservedPropertyNames).to.include('localHspMethod');
305          expect(finalConfig.reservedGlobalNames).not.to.include('LocalHarClass');
306          expect(finalConfig.reservedPropertyNames).not.to.include('localHarMethod');
307          expect(finalConfig.reservedNames.length).to.equal(0);
308          expect(finalConfig.reservedFileNames.length).to.equal(0);
309          expect(finalConfig.keepComments.length).to.equal(0);
310          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
311          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
312          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
313          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
314          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
315          expect(finalConfig.excludePathSet.size).to.equal(0);
316        });
317
318        afterEach(() => {
319          if (fs.existsSync(localHspConfig.exportRulePath)) {
320            fs.unlinkSync(localHspConfig.exportRulePath);
321          }
322          if (fs.existsSync(systemApiCacheFile)) {
323            fs.unlinkSync(systemApiCacheFile);
324          }
325        });
326      });
327
328      describe('localHAR depends on localHSP Tests', function () {
329        before(async () => {
330          initModulesConfig();
331        });
332
333        it('should merge both HSP and HAR consumer rules in HAR obfuscation.txt', function () {
334          localHarConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig);
335          const localHarResolver = new ObConfigResolver({ obfuscationOptions: localHarConfig, compileHar: true, compileShared: false }, printObfLogger);
336          localHarResolver.resolveObfuscationConfigs();
337
338          const localHarObfuscationContent = fs.readFileSync(localHarConfig.exportRulePath, 'utf-8');
339
340          expect(localHarObfuscationContent).to.include('-enable-toplevel-obfuscation');
341          expect(localHarObfuscationContent).to.include('-enable-property-obfuscation');
342          expect(localHarObfuscationContent).not.to.include('-enable-string-property-obfuscation');
343          expect(localHarObfuscationContent).not.to.include('-enable-filename-obfuscation');
344          expect(localHarObfuscationContent).not.to.include('-enable-export-obfuscation');
345          expect(localHarObfuscationContent).not.to.include('-remove-comments');
346          expect(localHarObfuscationContent).not.to.include('-compact');
347          expect(localHarObfuscationContent).not.to.include('-remove-log');
348
349          expect(localHarObfuscationContent).to.include('-keep-global-name');
350          expect(localHarObfuscationContent).to.include('-keep-property-name');
351          expect(localHarObfuscationContent).not.to.include('-keep-file-name');
352          expect(localHarObfuscationContent).not.to.include('-keep-comments');
353          expect(localHarObfuscationContent).not.to.include('-keep-dts');
354
355          expect(localHarObfuscationContent).to.include('LocalHspClass');
356          expect(localHarObfuscationContent).to.include('localHspMethod');
357          expect(localHarObfuscationContent).to.include('LocalHarClass');
358          expect(localHarObfuscationContent).to.include('localHarMethod');
359        });
360
361        it('should merge HAR consumer rules for HAP when HAP depends on localHar and localHar depends on localHsp', function () {
362          hapConfig.dependencies.libraries.push(localHarConfig.selfConfig);
363          hapConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig);
364          const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger);
365          const finalConfig = configResolver.resolveObfuscationConfigs();
366
367          expect(finalConfig.options.enableToplevelObfuscation).to.be.false;
368          expect(finalConfig.options.enableExportObfuscation).to.be.true;
369          expect(finalConfig.options.enablePropertyObfuscation).to.be.false;
370          expect(finalConfig.options.disableObfuscation).to.be.false;
371          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
372          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
373          expect(finalConfig.options.printKeptNames).to.be.false;
374          expect(finalConfig.options.removeComments).to.be.false;
375          expect(finalConfig.options.compact).to.be.false;
376          expect(finalConfig.options.removeLog).to.be.false;
377
378          expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass');
379          expect(finalConfig.reservedPropertyNames).to.include('localHarMethod');
380          expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass');
381          expect(finalConfig.reservedPropertyNames).to.include('localHarMethod');
382          expect(finalConfig.reservedNames.length).to.equal(0);
383          expect(finalConfig.reservedFileNames.length).to.equal(0);
384          expect(finalConfig.keepComments.length).to.equal(0);
385          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
386          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
387          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
388          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
389          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
390          expect(finalConfig.excludePathSet.size).to.equal(0);
391        });
392
393        afterEach(() => {
394          if (fs.existsSync(localHspConfig.exportRulePath)) {
395            fs.unlinkSync(localHspConfig.exportRulePath);
396          }
397          if (fs.existsSync(localHarConfig.exportRulePath)) {
398            fs.unlinkSync(localHarConfig.exportRulePath);
399          }
400          if (fs.existsSync(systemApiCacheFile)) {
401            fs.unlinkSync(systemApiCacheFile);
402          }
403        });
404      });
405
406      describe('localHAR depends on remoteHSP Tests', function () {
407        before(async () => {
408          initModulesConfig();
409        });
410
411        it('should merge both HSP obfuscation.txt and HAR consumer rules in HAR obfuscation.txt', function () {
412          localHarConfig.dependencies.hsps?.push(remoteHspObfFile);
413          const localHarResolver = new ObConfigResolver({ obfuscationOptions: localHarConfig, compileHar: true, compileShared: false }, printObfLogger);
414          localHarResolver.resolveObfuscationConfigs();
415
416          const localHarObfuscationContent = fs.readFileSync(localHarConfig.exportRulePath, 'utf-8');
417
418          expect(localHarObfuscationContent).to.include('-remove-log');
419          expect(localHarObfuscationContent).to.include('-enable-property-obfuscation');
420          expect(localHarObfuscationContent).not.to.include('-enable-toplevel-obfuscation');
421          expect(localHarObfuscationContent).not.to.include('-enable-string-property-obfuscation');
422          expect(localHarObfuscationContent).not.to.include('-enable-filename-obfuscation');
423          expect(localHarObfuscationContent).not.to.include('-enable-export-obfuscation');
424          expect(localHarObfuscationContent).not.to.include('-remove-comments');
425          expect(localHarObfuscationContent).not.to.include('-compact');
426
427          expect(localHarObfuscationContent).to.include('-keep-global-name');
428          expect(localHarObfuscationContent).to.include('-keep-property-name');
429          expect(localHarObfuscationContent).not.to.include('-keep-file-name');
430          expect(localHarObfuscationContent).not.to.include('-keep-comments');
431          expect(localHarObfuscationContent).not.to.include('-keep-dts');
432
433          expect(localHarObfuscationContent).to.include('RemoteHspClass');
434          expect(localHarObfuscationContent).to.include('remoteHspMethod');
435          expect(localHarObfuscationContent).to.include('LocalHarClass');
436          expect(localHarObfuscationContent).to.include('localHarMethod');
437        });
438
439        it('should merge HAR consumer rules for HAP when HAP depends on localHar and localHar depends on remoteHsp', function () {
440          hapConfig.dependencies.libraries.push(localHarConfig.selfConfig);
441          hapConfig.dependencies.hsps?.push(remoteHspObfFile);
442          const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger);
443          const finalConfig = configResolver.resolveObfuscationConfigs();
444
445          expect(finalConfig.options.removeLog).to.be.false;
446          expect(finalConfig.options.enablePropertyObfuscation).to.be.false;
447          expect(finalConfig.options.enableExportObfuscation).to.be.true;
448          expect(finalConfig.options.enableToplevelObfuscation).to.be.false;
449          expect(finalConfig.options.disableObfuscation).to.be.false;
450          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
451          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
452          expect(finalConfig.options.printKeptNames).to.be.false;
453          expect(finalConfig.options.removeComments).to.be.false;
454          expect(finalConfig.options.compact).to.be.false;
455
456          expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass');
457          expect(finalConfig.reservedPropertyNames).to.include('localHarMethod');
458          expect(finalConfig.reservedGlobalNames).to.include('RemoteHspClass');
459          expect(finalConfig.reservedPropertyNames).to.include('remoteHspMethod');
460          expect(finalConfig.reservedNames.length).to.equal(0);
461          expect(finalConfig.reservedFileNames.length).to.equal(0);
462          expect(finalConfig.keepComments.length).to.equal(0);
463          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
464          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
465          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
466          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
467          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
468          expect(finalConfig.excludePathSet.size).to.equal(0);
469        });
470
471        afterEach(() => {
472          if (fs.existsSync(remoteHspConfig.exportRulePath)) {
473            fs.unlinkSync(remoteHspConfig.exportRulePath);
474          }
475          if (fs.existsSync(localHarConfig.exportRulePath)) {
476            fs.unlinkSync(localHarConfig.exportRulePath);
477          }
478          if (fs.existsSync(systemApiCacheFile)) {
479            fs.unlinkSync(systemApiCacheFile);
480          }
481        });
482      });
483
484      describe('remoteHAR depends on localHSP Tests', function () {
485        before(async () => {
486          initModulesConfig();
487        });
488
489        it('should retain HAR consumer rules for HAP when HAP depends on remoteHar and remoteHar depends on localHsp', function () {
490          hapConfig.dependencies.hars.push(remoteHarDependsLocalHspObfFile);
491          const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger);
492          const finalConfig = configResolver.resolveObfuscationConfigs();
493
494          expect(finalConfig.options.compact).to.be.false;
495          expect(finalConfig.options.enableToplevelObfuscation).to.be.false;
496          expect(finalConfig.options.enableExportObfuscation).to.be.true;
497          expect(finalConfig.options.removeLog).to.be.false;
498          expect(finalConfig.options.enablePropertyObfuscation).to.be.false;
499          expect(finalConfig.options.disableObfuscation).to.be.false;
500          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
501          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
502          expect(finalConfig.options.printKeptNames).to.be.false;
503          expect(finalConfig.options.removeComments).to.be.false;
504
505          expect(finalConfig.reservedGlobalNames).to.include('RemoteHarClass');
506          expect(finalConfig.reservedPropertyNames).to.include('remoteHarMethod');
507          expect(finalConfig.reservedGlobalNames).to.include('LocalHspClass');
508          expect(finalConfig.reservedPropertyNames).to.include('localHspMethod');
509          expect(finalConfig.reservedNames.length).to.equal(0);
510          expect(finalConfig.reservedFileNames.length).to.equal(0);
511          expect(finalConfig.keepComments.length).to.equal(0);
512          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
513          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
514          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
515          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
516          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
517          expect(finalConfig.excludePathSet.size).to.equal(0);
518        });
519
520        afterEach(() => {
521          if (fs.existsSync(localHspConfig.exportRulePath)) {
522            fs.unlinkSync(localHspConfig.exportRulePath);
523          }
524          if (fs.existsSync(remoteHarConfig.exportRulePath)) {
525            fs.unlinkSync(remoteHarConfig.exportRulePath);
526          }
527          if (fs.existsSync(systemApiCacheFile)) {
528            fs.unlinkSync(systemApiCacheFile);
529          }
530        });
531      });
532
533      describe('remoteHAR depends on remoteHSP Tests', function () {
534        before(async () => {
535          initModulesConfig();
536        });
537
538        it('should retain HAR consumer rules for HAP when HAP depends on remoteHar and remoteHar depends on remoteHsp', function () {
539          hapConfig.dependencies.hars.push(remoteHarDependsRemoteHspObfFile);
540          const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger);
541          const finalConfig = configResolver.resolveObfuscationConfigs();
542
543          expect(finalConfig.options.compact).to.be.false;
544          expect(finalConfig.options.removeLog).to.be.false;
545          expect(finalConfig.options.enableExportObfuscation).to.be.true;
546          expect(finalConfig.options.enableToplevelObfuscation).to.be.false;
547          expect(finalConfig.options.enablePropertyObfuscation).to.be.false;
548          expect(finalConfig.options.disableObfuscation).to.be.false;
549          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
550          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
551          expect(finalConfig.options.printKeptNames).to.be.false;
552          expect(finalConfig.options.removeComments).to.be.false;
553
554          expect(finalConfig.reservedGlobalNames).to.include('RemoteHarClass');
555          expect(finalConfig.reservedPropertyNames).to.include('remoteHarMethod');
556          expect(finalConfig.reservedGlobalNames).to.include('RemoteHspClass');
557          expect(finalConfig.reservedPropertyNames).to.include('remoteHspMethod');
558          expect(finalConfig.reservedNames.length).to.equal(0);
559          expect(finalConfig.reservedFileNames.length).to.equal(0);
560          expect(finalConfig.keepComments.length).to.equal(0);
561          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
562          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
563          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
564          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
565          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
566          expect(finalConfig.excludePathSet.size).to.equal(0);
567        });
568
569        after(() => {
570          if (fs.existsSync(remoteHspConfig.exportRulePath)) {
571            fs.unlinkSync(remoteHspConfig.exportRulePath);
572          }
573          if (fs.existsSync(remoteHarConfig.exportRulePath)) {
574            fs.unlinkSync(remoteHarConfig.exportRulePath);
575          }
576          if (fs.existsSync(systemApiCacheFile)) {
577            fs.unlinkSync(systemApiCacheFile);
578          }
579        });
580      });
581
582      describe('localHSP depends on localHSP and RemoteHSP Tests', function () {
583        before(async () => {
584          initModulesConfig();
585        });
586
587        it('should contain only MainHSP consumer rules in MainHSP obfuscation.txt', function () {
588          localMainHspConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig);
589          localMainHspConfig.dependencies.hsps?.push(remoteHspObfFile);
590          const localMainHspResolver = new ObConfigResolver({ obfuscationOptions: localMainHspConfig, compileHar: false, compileShared: true }, printObfLogger);
591          localMainHspResolver.resolveObfuscationConfigs();
592          const localMainHspObfuscationContent = fs.readFileSync(localMainHspConfig.exportRulePath, 'utf-8');
593
594          expect(localMainHspObfuscationContent).not.to.include('-enable-filename-obfuscation');
595          expect(localMainHspObfuscationContent).not.to.include('-remove-log');
596          expect(localMainHspObfuscationContent).not.to.include('-enable-property-obfuscation');
597          expect(localMainHspObfuscationContent).not.to.include('-enable-toplevel-obfuscation');
598          expect(localMainHspObfuscationContent).not.to.include('-enable-string-property-obfuscation');
599          expect(localMainHspObfuscationContent).not.to.include('-enable-export-obfuscation');
600          expect(localMainHspObfuscationContent).not.to.include('-remove-comments');
601          expect(localMainHspObfuscationContent).not.to.include('-compact');
602
603          expect(localMainHspObfuscationContent).to.include('-keep-global-name');
604          expect(localMainHspObfuscationContent).to.include('-keep-property-name');
605          expect(localMainHspObfuscationContent).not.to.include('-keep-file-name');
606          expect(localMainHspObfuscationContent).not.to.include('-keep-comments');
607          expect(localMainHspObfuscationContent).not.to.include('-keep-dts');
608
609          expect(localMainHspObfuscationContent).to.include('LocalMainHspClass');
610          expect(localMainHspObfuscationContent).to.include('localMainHspGreeting');
611          expect(localMainHspObfuscationContent).not.to.include('LocalHspClass');
612          expect(localMainHspObfuscationContent).not.to.include('localHspMethod');
613          expect(localMainHspObfuscationContent).not.to.include('RemoteHspClass');
614          expect(localMainHspObfuscationContent).not.to.include('remoteHspMethod');
615        });
616
617        it('should only retain MainHSP consumer rules when HAP depends on localMainHsp and localMainHsp depends on localHsp and remoteHsp', function () {
618          hapConfig.dependencies.hspLibraries?.push(localMainHspConfig.selfConfig);
619          const hapResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger);
620          const finalConfig = hapResolver.resolveObfuscationConfigs();
621
622          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
623          expect(finalConfig.options.enableExportObfuscation).to.be.true;
624          expect(finalConfig.options.compact).to.be.false;
625          expect(finalConfig.options.removeLog).to.be.false;
626          expect(finalConfig.options.enableToplevelObfuscation).to.be.false;
627          expect(finalConfig.options.enablePropertyObfuscation).to.be.false;
628          expect(finalConfig.options.disableObfuscation).to.be.false;
629          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
630          expect(finalConfig.options.printKeptNames).to.be.false;
631          expect(finalConfig.options.removeComments).to.be.false;
632
633          expect(finalConfig.reservedGlobalNames).to.include('LocalMainHspClass');
634          expect(finalConfig.reservedPropertyNames).to.include('localMainHspGreeting');
635          expect(finalConfig.reservedGlobalNames).not.to.include('LocalHspClass');
636          expect(finalConfig.reservedPropertyNames).not.to.include('localHspMethod');
637          expect(finalConfig.reservedGlobalNames).not.to.include('RemoteHspClass');
638          expect(finalConfig.reservedPropertyNames).not.to.include('remoteHspMethod');
639          expect(finalConfig.reservedNames.length).to.equal(0);
640          expect(finalConfig.reservedFileNames.length).to.equal(0);
641          expect(finalConfig.keepComments.length).to.equal(0);
642          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
643          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
644          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
645          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
646          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
647          expect(finalConfig.excludePathSet.size).to.equal(0);
648        });
649
650        after(() => {
651          if (fs.existsSync(remoteHspConfig.exportRulePath)) {
652            fs.unlinkSync(remoteHspConfig.exportRulePath);
653          }
654          if (fs.existsSync(localHspConfig.exportRulePath)) {
655            fs.unlinkSync(localHspConfig.exportRulePath);
656          }
657          if (fs.existsSync(localMainHspConfig.exportRulePath)) {
658            fs.unlinkSync(localMainHspConfig.exportRulePath);
659          }
660          if (fs.existsSync(systemApiCacheFile)) {
661            fs.unlinkSync(systemApiCacheFile);
662          }
663        });
664      });
665
666      describe('remoteHSP depends on HSP Tests', function () {
667        before(async () => {
668          initModulesConfig();
669        });
670
671        it('should only retain MainHSP consumer rules for HAP when HAP depends on RemoteMainHsp and RemoteMainHsp depends on localHsp and remoteHsp', function () {
672          hapConfig.dependencies.hsps?.push(remoteMainHspObfFile);
673          const hapResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger);
674          const finalConfig = hapResolver.resolveObfuscationConfigs();
675
676          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
677          expect(finalConfig.options.enableExportObfuscation).to.be.true;
678          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
679          expect(finalConfig.options.compact).to.be.false;
680          expect(finalConfig.options.removeLog).to.be.false;
681          expect(finalConfig.options.enableToplevelObfuscation).to.be.false;
682          expect(finalConfig.options.enablePropertyObfuscation).to.be.false;
683          expect(finalConfig.options.disableObfuscation).to.be.false;
684          expect(finalConfig.options.printKeptNames).to.be.false;
685          expect(finalConfig.options.removeComments).to.be.false;
686
687          expect(finalConfig.reservedGlobalNames).to.include('RemoteMainHspClass');
688          expect(finalConfig.reservedPropertyNames).to.include('remoteMainHspGreeting');
689          expect(finalConfig.reservedGlobalNames).not.to.include('LocalHspClass');
690          expect(finalConfig.reservedPropertyNames).not.to.include('localHspMethod');
691          expect(finalConfig.reservedGlobalNames).not.to.include('RemoteHspClass');
692          expect(finalConfig.reservedPropertyNames).not.to.include('remoteHspMethod');
693          expect(finalConfig.reservedNames.length).to.equal(0);
694          expect(finalConfig.reservedFileNames.length).to.equal(0);
695          expect(finalConfig.keepComments.length).to.equal(0);
696          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
697          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
698          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
699          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
700          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
701          expect(finalConfig.excludePathSet.size).to.equal(0);
702        });
703
704        after(() => {
705          if (fs.existsSync(remoteHspConfig.exportRulePath)) {
706            fs.unlinkSync(remoteHspConfig.exportRulePath);
707          }
708          if (fs.existsSync(localHspConfig.exportRulePath)) {
709            fs.unlinkSync(localHspConfig.exportRulePath);
710          }
711          if (fs.existsSync(remoteMainHspConfig.exportRulePath)) {
712            fs.unlinkSync(remoteMainHspConfig.exportRulePath);
713          }
714          if (fs.existsSync(systemApiCacheFile)) {
715            fs.unlinkSync(systemApiCacheFile);
716          }
717        });
718      })
719
720      describe('HAP depends on local and remote HAR and HSP Tests', function () {
721        before(async () => {
722          initModulesConfig();
723        });
724
725        it('should contain all local and remote HAR and HSP consumer rules in final HAP config', function () {
726          hapConfig.dependencies.libraries.push(localHarConfig.selfConfig);
727          hapConfig.dependencies.hars.push(remoteHarObfFile);
728          hapConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig);
729          hapConfig.dependencies.hsps?.push(remoteHspObfFile);
730          const hapResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger);
731          const finalConfig = hapResolver.resolveObfuscationConfigs();
732
733          expect(finalConfig.options.enableExportObfuscation).to.be.true;
734          expect(finalConfig.options.compact).to.be.false;
735          expect(finalConfig.options.removeLog).to.be.false;
736          expect(finalConfig.options.enableToplevelObfuscation).to.be.false;
737          expect(finalConfig.options.enablePropertyObfuscation).to.be.false;
738          expect(finalConfig.options.disableObfuscation).to.be.false;
739          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
740          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
741          expect(finalConfig.options.printKeptNames).to.be.false;
742          expect(finalConfig.options.removeComments).to.be.false;
743
744          expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass');
745          expect(finalConfig.reservedPropertyNames).to.include('localHarMethod');
746          expect(finalConfig.reservedGlobalNames).to.include('LocalHspClass');
747          expect(finalConfig.reservedPropertyNames).to.include('localHspMethod');
748          expect(finalConfig.reservedGlobalNames).to.include('RemoteHarClass');
749          expect(finalConfig.reservedPropertyNames).to.include('remoteHarMethod');
750          expect(finalConfig.reservedGlobalNames).to.include('RemoteHspClass');
751          expect(finalConfig.reservedPropertyNames).to.include('remoteHspMethod');
752          expect(finalConfig.reservedNames.length).to.equal(0);
753          expect(finalConfig.reservedFileNames.length).to.equal(0);
754          expect(finalConfig.keepComments.length).to.equal(0);
755          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
756          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
757          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
758          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
759          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
760          expect(finalConfig.excludePathSet.size).to.equal(0);
761        });
762
763        after(() => {
764          if (fs.existsSync(systemApiCacheFile)) {
765            fs.unlinkSync(systemApiCacheFile);
766          }
767        });
768      });
769
770      describe('-enable-lib-obfuscation-options test', function () {
771        before(async () => {
772          initModulesConfig();
773        });
774
775        it('should contain all local and remote HAR and HSP consumer rules in final HAP config', function () {
776          libTestHapConfig.dependencies.libraries.push(localHarConfig.selfConfig);
777          libTestHapConfig.dependencies.hars.push(remoteHarObfFile);
778          libTestHapConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig);
779          libTestHapConfig.dependencies.hsps?.push(remoteHspObfFile);
780          const hapResolver = new ObConfigResolver(
781            { obfuscationOptions: libTestHapConfig, compileHar: false, compileShared: false },
782            printObfLogger,
783          );
784          const finalConfig = hapResolver.resolveObfuscationConfigs();
785
786          expect(finalConfig.options.enableExportObfuscation).to.be.true;
787          expect(finalConfig.options.compact).to.be.true;
788          expect(finalConfig.options.removeLog).to.be.true;
789          expect(finalConfig.options.enableToplevelObfuscation).to.be.true;
790          expect(finalConfig.options.enablePropertyObfuscation).to.be.true;
791          expect(finalConfig.options.disableObfuscation).to.be.false;
792          expect(finalConfig.options.enableFileNameObfuscation).to.be.false;
793          expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false;
794          expect(finalConfig.options.printKeptNames).to.be.false;
795          expect(finalConfig.options.removeComments).to.be.false;
796
797          expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass');
798          expect(finalConfig.reservedPropertyNames).to.include('localHarMethod');
799          expect(finalConfig.reservedGlobalNames).to.include('LocalHspClass');
800          expect(finalConfig.reservedPropertyNames).to.include('localHspMethod');
801          expect(finalConfig.reservedGlobalNames).to.include('RemoteHarClass');
802          expect(finalConfig.reservedPropertyNames).to.include('remoteHarMethod');
803          expect(finalConfig.reservedGlobalNames).to.include('RemoteHspClass');
804          expect(finalConfig.reservedPropertyNames).to.include('remoteHspMethod');
805          expect(finalConfig.reservedNames.length).to.equal(0);
806          expect(finalConfig.reservedFileNames.length).to.equal(0);
807          expect(finalConfig.keepComments.length).to.equal(0);
808          expect(finalConfig.keepSourceOfPaths.length).to.equal(0);
809          expect(finalConfig.universalReservedPropertyNames.length).to.equal(0);
810          expect(finalConfig.universalReservedGlobalNames.length).to.equal(0);
811          expect(finalConfig.keepUniversalPaths.length).to.equal(0);
812          expect(finalConfig.excludeUniversalPaths.length).to.equal(0);
813          expect(finalConfig.excludePathSet.size).to.equal(0);
814        });
815
816        after(() => {
817          if (fs.existsSync(systemApiCacheFile)) {
818            fs.unlinkSync(systemApiCacheFile);
819          }
820        });
821      });
822    })
823  });
824})