• 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 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
16import mocha from 'mocha';
17import { expect } from 'chai';
18import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock';
19import { RELEASE } from '../../../lib/fast_build/ark_compiler/common/ark_define';
20import { BytecodeObfuscator } from '../../../lib/fast_build/ark_compiler/bytecode_obfuscator';
21import { OBFUSCATE_RULE_BYTECODE_OBFUSCATION_ENABLE_PATH } from '../mock/rollup_mock/path_config';
22import { ModuleInfo } from '../../../lib/fast_build/ark_compiler/module/module_mode';
23
24mocha.describe('test bytecodeObfuscator file api', function () {
25  mocha.before(function () {
26    this.rollup = new RollUpPluginMock();
27    this.rollup.build(RELEASE);
28    this.rollup.share.projectConfig.compatibleSdkVersion=14;
29    this.rollup.share.projectConfig.obfuscationOptions = {
30      'selfConfig': {
31        'ruleOptions': {
32          'enable': true,
33          'rules': [OBFUSCATE_RULE_BYTECODE_OBFUSCATION_ENABLE_PATH]
34        },
35        'consumerRules': [],
36      },
37      'dependencies': {
38        'libraries': [],
39        'hars': []
40      },
41      obfuscationCacheDir:""
42    };
43    this.rollup.share.arkProjectConfig.obfuscationMergedObConfig={
44      options:{
45        bytecodeObf: { enable: true, enhanced: false, debugging: false },
46        disableObfuscation: false,
47        enablePropertyObfuscation: true,
48        enableStringPropertyObfuscation: false,
49        enableToplevelObfuscation: true,
50        enableFileNameObfuscation: true,
51        enableExportObfuscation: true,
52        printKeptNames: false,
53        removeComments: false,
54        compact: false,
55        removeLog: false,
56        printNameCache: '',
57        printKeptNamesPath: '',
58        applyNameCache: ''
59      },
60      reservedPropertyNames: [],
61      reservedGlobalNames: [],
62      reservedNames: [],
63      reservedFileNames: [],
64      keepComments: [],
65      keepSourceOfPaths: [],
66      universalReservedPropertyNames: [],
67      universalReservedGlobalNames: [],
68      keepUniversalPaths: [],
69      excludeUniversalPaths: [],
70      excludePathSet: new Set<string>()
71    }
72  });
73
74  mocha.after(() => {
75    delete this.rollup;
76  });
77
78  mocha.it('1-1: test initBytecodeObfuscator when obfuscation disabled', function () {
79    this.rollup.share.arkProjectConfig.isArkguardEnabled=true;
80    this.rollup.share.arkProjectConfig.isBytecodeObfEnabled=false;
81    BytecodeObfuscator.initForTest(this.rollup.share);
82    expect(BytecodeObfuscator.enable === false).to.be.true;
83    BytecodeObfuscator.cleanBcObfuscatorObject();
84  });
85
86  mocha.it('1-2: test init when obfuscation is arkguard', function () {
87    this.rollup.share.arkProjectConfig.isArkguardEnabled=true;
88    this.rollup.share.arkProjectConfig.isBytecodeObfEnabled=false;
89    BytecodeObfuscator.initForTest(this.rollup.share);
90    expect(BytecodeObfuscator.enable === false).to.be.true;
91    BytecodeObfuscator.cleanBcObfuscatorObject();
92  });
93
94  mocha.it('1-3: test init when obfuscation is bytecode obfuscation', function () {
95    this.rollup.share.arkProjectConfig.isArkguardEnabled=false;
96    this.rollup.share.arkProjectConfig.isBytecodeObfEnabled=true;
97    BytecodeObfuscator.initForTest(this.rollup.share);
98    expect(BytecodeObfuscator.enable === true).to.be.true;
99    BytecodeObfuscator.cleanBcObfuscatorObject();
100  });
101
102  mocha.it('2-1: test collectSkipModuleName add skip module pkgName correctly with keep bytecode har', function () {
103    this.rollup.share.arkProjectConfig.isArkguardEnabled=false;
104    this.rollup.share.arkProjectConfig.isBytecodeObfEnabled=true;
105    this.rollup.share.arkProjectConfig.obfuscationMergedObConfig.keepSourceOfPaths.push(
106      'xxxx/xxxx/kohtpunnbppmmzjb0tw2ry4=/oh_modules/har',
107      'xxxx/xxxx/kohtpunnbppmmzjb0tw2ry4=/oh_modules/har1'
108    );
109    this.rollup.share.projectConfig.byteCodeHarInfo={
110      'har':{
111        "compatibleSdkVersion":"14",
112        "abcPath":"adsasd"
113      },
114    }
115    BytecodeObfuscator.initForTest(this.rollup.share)
116    BytecodeObfuscator.getInstance().collectSkipModuleName();
117    expect(BytecodeObfuscator.getInstance().bytecodeObfuscateConfig.skippedRemoteHarList.has('har')).to.be.true;
118    BytecodeObfuscator.cleanBcObfuscatorObject();
119  });
120
121  mocha.it('2-2: test collectSkipModuleName add skip module pkgName correctly with bytecode har', function () {
122    this.rollup.share.arkProjectConfig.isArkguardEnabled=false;
123    this.rollup.share.arkProjectConfig.isBytecodeObfEnabled=true;
124    this.rollup.share.projectConfig.byteCodeHarInfo={
125      'Har_version_11':{
126        "compatibleSdkVersion":"11",
127      },
128      'Har_version_14':{
129        "compatibleSdkVersion":"14",
130      }
131    }
132    BytecodeObfuscator.initForTest(this.rollup.share)
133    BytecodeObfuscator.getInstance().collectSkipModuleName();
134    expect(BytecodeObfuscator.getInstance().bytecodeObfuscateConfig.skippedRemoteHarList.has('Har_version_11')).to.be.true;
135    expect(BytecodeObfuscator.getInstance().bytecodeObfuscateConfig.skippedRemoteHarList.has('Har_version_14')).to.be.true;
136    BytecodeObfuscator.cleanBcObfuscatorObject();
137  });
138
139
140  mocha.it('3-1: test generateObfCmd add config path correctly',function(){
141    this.rollup.share.arkProjectConfig.isArkguardEnabled=false;
142    this.rollup.share.arkProjectConfig.isBytecodeObfEnabled=true;
143    BytecodeObfuscator.initForTest(this.rollup.share);
144    expect(BytecodeObfuscator.enable === true).to.be.true;
145    BytecodeObfuscator.getInstance().generateObfCmd();
146    expect(BytecodeObfuscator.getInstance().cmdArgs.includes('config.json')).to.be.true;
147    BytecodeObfuscator.cleanBcObfuscatorObject();
148  });
149
150  mocha.it('3-2: test generateObfCmd add debug correctly',function(){
151    this.rollup.share.arkProjectConfig.isArkguardEnabled=false;
152    this.rollup.share.arkProjectConfig.isBytecodeObfEnabled=true;
153    this.rollup.share.arkProjectConfig.obfuscationMergedObConfig.options.bytecodeObf.debugging=true;
154    BytecodeObfuscator.initForTest(this.rollup.share);
155    expect(BytecodeObfuscator.enable === true).to.be.true;
156    BytecodeObfuscator.getInstance().generateObfCmd();
157    expect(BytecodeObfuscator.getInstance().cmdArgs.length==4).to.be.true;
158    expect(BytecodeObfuscator.getInstance().cmdArgs.includes('--debug')).to.be.true;
159    expect(BytecodeObfuscator.getInstance().cmdArgs.includes('--debug-file')).to.be.true;
160    expect(BytecodeObfuscator.getInstance().cmdArgs.includes('debug.log')).to.be.true;
161    expect(BytecodeObfuscator.getInstance().cmdArgs.includes('config.json')).to.be.true;
162    BytecodeObfuscator.cleanBcObfuscatorObject();
163  });
164
165    mocha.it("4-1: test convertAbstractPathToRecordName successfully and remove originPath",function () {
166    this.rollup.share.arkProjectConfig.isArkguardEnabled = false;
167    this.rollup.share.arkProjectConfig.isBytecodeObfEnabled = true;
168    this.rollup.share.arkProjectConfig.obfuscationMergedObConfig.options.bytecodeObf.debugging = true;
169    const originPath = 'ProblemReproduction/oh_modules/.ohpm/reflect-metadata@0.2.1/oh_modules/reflect-metadata/Reflect.js';
170    const recordName = '&reflect-meatadata|0.2.1|Reflect.js';
171    this.rollup.share.arkProjectConfig.obfuscationMergedObConfig.keepSourceOfPaths = [originPath];
172    BytecodeObfuscator.initForTest(this.rollup.share);
173    let moduleInfos: Map<String, ModuleInfo> = new Map();
174    moduleInfos.set(
175      originPath,
176      new ModuleInfo(
177        originPath,
178        'oh_modules/reflect-metadata/Reflect.js',
179        true,
180        '&reflect-meatadata|0.2.1|Reflect.js',
181        'Reflect.js',
182        'reflect-metadata',
183        'js'
184      )
185    );
186    BytecodeObfuscator.getInstance().convertAbstractPathToRecordName(moduleInfos);
187    const keepPaths =
188      BytecodeObfuscator.getInstance().bytecodeObfuscateConfig.obfuscationRules
189        .keepOptions.keepPaths;
190    expect(keepPaths.has(recordName)).to.be.true;
191    expect(keepPaths.has(originPath)).to.be.false;
192    BytecodeObfuscator.cleanBcObfuscatorObject();
193  });
194});