• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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  clearHistoryUnobfuscatedMap,
18  historyAllUnobfuscatedNamesMap,
19  historyUnobfuscatedPropMap,
20  initObfuscationConfig
21} from '../../../src/initialization/Initializer';
22import {
23  expect,
24  assert
25} from 'chai';
26import {
27  describe,
28  it
29} from 'mocha';
30import {
31  HvigorErrorInfo,
32  MergedConfig,
33} from '../../../src/ArkObfuscator';
34
35describe('Tester Cases for <Initializer.ts>.', function () {
36  it('Tester Cases for <clearHistoryUnobfuscatedMap>.', function () {
37    historyAllUnobfuscatedNamesMap.set('key', {'prop1': 'aaa'});
38    historyUnobfuscatedPropMap.set('key', ['value']);
39    expect(historyAllUnobfuscatedNamesMap.size).to.equal(1);
40    expect(historyUnobfuscatedPropMap.size).to.equal(1);
41    clearHistoryUnobfuscatedMap();
42    expect(historyAllUnobfuscatedNamesMap.size).to.equal(0);
43    expect(historyUnobfuscatedPropMap.size).to.equal(0);
44  });
45});
46
47function printObfLogger(errorInfo: string, errorCodeInfo: HvigorErrorInfo | string, level: string): void {
48  switch (level) {
49    case 'warn':
50      console.warn(errorInfo);
51      break;
52    case 'error':
53      console.error(errorInfo);
54      break;
55    default:
56      break;
57  }
58}
59const projectConfig = {
60  'obfuscationOptions': {
61    'selfConfig': {
62      'ruleOptions': {
63        'enable': true,
64        'rules': ['./test/testData/obfuscation/Configs/bytecodeObf/bytecodeObf_enable.txt']
65      },
66      'consumerRules': [],
67    },
68    'dependencies': {
69      'libraries': [],
70      'hars': []
71    },
72    obfuscationCacheDir: ""
73  }
74};
75interface ArkProjectConfig {
76  isBytecodeObfEnabled?: boolean;
77  isArkguardEnabled?: boolean;
78  allowEtsAnnotations?: boolean;
79  obfuscationMergedObConfig?: MergedConfig;
80}
81
82describe('test for set arkguard mode property correctly for arkProjectConfig', function () {
83  describe('test for set arkguard mode property correctly for arkProjectConfig', () => {
84    it('should set isBytecodeObfEnabled when enable bytecodeObf', () => {
85      const arkProjectConfig: ArkProjectConfig = {};
86      initObfuscationConfig(projectConfig, arkProjectConfig, printObfLogger);
87      expect(arkProjectConfig.isBytecodeObfEnabled).to.be.true;
88      expect(arkProjectConfig.isArkguardEnabled).to.be.false;
89    });
90
91    it('should set isArkguardEnabled when enable arkguardObf', () => {
92      const arkProjectConfig: ArkProjectConfig = {};
93      projectConfig.obfuscationOptions.selfConfig.ruleOptions.rules = ['./test/testData/obfuscation/Configs/bytecodeObf/arkguard_enable.txt'];
94      initObfuscationConfig(projectConfig, arkProjectConfig, printObfLogger);
95      expect(arkProjectConfig.isArkguardEnabled).to.be.true;
96      expect(arkProjectConfig.isBytecodeObfEnabled).to.be.false;
97    });
98
99    it('should not set isArkguardEnabled and isBytecodeObfEnabled when obf is disabled', () => {
100      const arkProjectConfig: ArkProjectConfig = {};
101      projectConfig.obfuscationOptions.selfConfig.ruleOptions.enable = false;
102      initObfuscationConfig(projectConfig, arkProjectConfig, printObfLogger);
103      expect(arkProjectConfig.isBytecodeObfEnabled).to.be.undefined;
104      expect(arkProjectConfig.isArkguardEnabled).to.be.undefined;
105    });
106
107    it('should not set enableEtsAnnotation if allowEtsAnnotations is enabled in arkProjectConfig', () => {
108      const arkProjectConfig: ArkProjectConfig = {
109        allowEtsAnnotations: true,
110        obfuscationMergedObConfig: new MergedConfig(),
111      };
112      projectConfig.obfuscationOptions.selfConfig.ruleOptions.enable = true;
113      projectConfig.obfuscationOptions.selfConfig.ruleOptions.rules = ['./test/testData/obfuscation/Configs/bytecodeObf/arkguard_enable.txt'];
114      initObfuscationConfig(projectConfig, arkProjectConfig, printObfLogger);
115      expect(arkProjectConfig.obfuscationMergedObConfig?.options.enableEtsAnnotation).to.be.true;
116    });
117  });
118});
119