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 WIN_BC_OBFUSCATOR_PATH, 56 BC_OBFUSCATOR_PATH, 57 MAC_BC_OBFUSCATOR_PATH 58} from '../mock/rollup_mock/path_config'; 59import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; 60import { 61 initArkConfig, 62 initArkProjectConfig, 63 utProcessArkConfig, 64 readProjectAndLibsSource 65} from '../../../lib/fast_build/ark_compiler/common/process_ark_config'; 66import { 67 ObConfigResolver, 68 MergedConfig 69} from '../../../lib/fast_build/ark_compiler/common/ob_config_resolver'; 70import { 71 ArkObfuscator, 72 PerfMode, 73 TimeAndMemTimeTracker, 74 performanceTimeAndMemPrinter, 75 printerTimeAndMemDataConfig, 76} from 'arkguard'; 77import { UnobfuscationCollections, AtKeepCollections } from 'arkguard/lib/utils/CommonCollections'; 78 79const WINDOWS: string = 'Windows_NT'; 80const LINUX: string = 'Linux'; 81const MAC: string = 'Darwin'; 82const HARMONYOS: string = 'HarmonyOS'; 83 84mocha.describe('test process_ark_config file api', function () { 85 mocha.before(function () { 86 this.rollup = new RollUpPluginMock(); 87 }); 88 89 mocha.after(() => { 90 delete this.rollup; 91 }); 92 93 mocha.it('1-1: test initArkConfig under build debug', function () { 94 this.rollup.build(); 95 this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir; 96 this.rollup.share.projectConfig.nodeJs = true; 97 this.rollup.share.projectConfig.nodePath = NODE_JS_PATH; 98 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 99 100 expect(arkConfig.nodePath === NODE_JS_PATH).to.be.true; 101 expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true; 102 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 103 expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true; 104 expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true; 105 expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true; 106 expect(arkConfig.isDebug === true).to.be.true; 107 expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true; 108 }); 109 110 mocha.it('1-2: test initArkConfig under build release', function () { 111 this.rollup.build(RELEASE); 112 this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir; 113 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 114 115 expect(arkConfig.nodePath === NODE).to.be.true; 116 expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true; 117 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 118 expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true; 119 expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true; 120 expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true; 121 expect(arkConfig.isDebug === false).to.be.true; 122 expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true; 123 }); 124 125 mocha.it('1-3: test initArkConfig under preview debug', function () { 126 this.rollup.preview(); 127 this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir; 128 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 129 130 expect(arkConfig.nodePath === NODE).to.be.true; 131 expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true; 132 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 133 expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true; 134 expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true; 135 expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true; 136 expect(arkConfig.isDebug === true).to.be.true; 137 expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true; 138 }); 139 140 mocha.it('1-4: test initArkConfig under hot reload debug', function () { 141 this.rollup.hotReload(); 142 this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir; 143 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 144 145 expect(arkConfig.nodePath === NODE).to.be.true; 146 expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true; 147 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 148 expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true; 149 expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true; 150 expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true; 151 expect(arkConfig.isDebug === true).to.be.true; 152 expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true; 153 }); 154 155 mocha.it('2-1-1: test initArkProjectConfig under build debug: moduleJsonInfo exists', function () { 156 this.rollup.build(); 157 const arkConfig = initArkProjectConfig(this.rollup.share); 158 const buildJsonInfo = 159 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 160 const moduleJsonInfo = 161 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString()); 162 163 expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true; 164 expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true; 165 expect(arkConfig.processTs === false).to.be.true; 166 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 167 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 168 expect(arkConfig.compileMode === ESMODULE).to.be.true; 169 }); 170 171 mocha.it('2-1-2: test initArkProjectConfig under build debug: buildJsonInfo.patchConfig is true', function () { 172 this.rollup.build(); 173 this.rollup.share.projectConfig.aceBuildJson = 174 `${this.rollup.share.projectConfig.aceModuleBuild}/${LOADER_AOTMODE}`; 175 const arkConfig = initArkProjectConfig(this.rollup.share); 176 const buildJsonInfo = 177 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 178 179 expect(arkConfig.oldMapFilePath === buildJsonInfo.patchConfig.oldMapFilePath).to.be.true; 180 expect(arkConfig.pandaMode === TS2ABC).to.be.true; 181 expect(arkConfig.processTs === true).to.be.true; 182 expect(arkConfig.anBuildOutPut === buildJsonInfo.anBuildOutPut).to.be.true; 183 expect(arkConfig.anBuildMode === buildJsonInfo.anBuildMode).to.be.true; 184 expect(arkConfig.apPath === buildJsonInfo.apPath).to.be.true; 185 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 186 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 187 expect(arkConfig.compileMode === ESMODULE).to.be.true; 188 }); 189 190 mocha.it('2-2: test initArkProjectConfig under build release', function () { 191 this.rollup.build(RELEASE); 192 const arkConfig = initArkProjectConfig(this.rollup.share); 193 const buildJsonInfo = 194 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 195 const moduleJsonInfo = 196 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString()); 197 198 expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true; 199 expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true; 200 expect(arkConfig.processTs === false).to.be.true; 201 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 202 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 203 expect(arkConfig.compileMode === ESMODULE).to.be.true; 204 }); 205 206 mocha.it('2-3: test initArkProjectConfig under preview debug', function () { 207 this.rollup.preview(); 208 const arkConfig = initArkProjectConfig(this.rollup.share); 209 const buildJsonInfo = 210 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 211 const moduleJsonInfo = 212 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString()); 213 214 expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true; 215 expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true; 216 expect(arkConfig.processTs === false).to.be.true; 217 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 218 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 219 expect(arkConfig.compileMode === ESMODULE).to.be.true; 220 }); 221 222 mocha.it('2-4: test initArkProjectConfig under hot reload debug', function () { 223 this.rollup.hotReload(); 224 const arkConfig = initArkProjectConfig(this.rollup.share); 225 const buildJsonInfo = 226 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 227 const moduleJsonInfo = 228 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString()); 229 230 expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true; 231 expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true; 232 expect(arkConfig.processTs === false).to.be.true; 233 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 234 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 235 expect(arkConfig.compileMode === ESMODULE).to.be.true; 236 }); 237 238 mocha.it('2-5: test initArkProjectConfig under build debug: perf is not ADVANCED', function () { 239 performanceTimeAndMemPrinter.singleFilePrinter = new TimeAndMemTimeTracker(); 240 performanceTimeAndMemPrinter.filesPrinter = new TimeAndMemTimeTracker(); 241 this.rollup.build(RELEASE); 242 this.rollup.share.projectConfig.perf = PerfMode.NORMAL; 243 initArkProjectConfig(this.rollup.share); 244 245 expect(performanceTimeAndMemPrinter.filesPrinter).to.be.undefined; 246 expect(performanceTimeAndMemPrinter.singleFilePrinter).to.be.undefined; 247 }); 248 249 mocha.it('2-6: test initArkProjectConfig under build debug: perf is ADVANCED', function () { 250 printerTimeAndMemDataConfig.mTimeAndMemPrinter = false; 251 performanceTimeAndMemPrinter.singleFilePrinter = undefined; 252 performanceTimeAndMemPrinter.filesPrinter = undefined; 253 254 this.rollup.build(RELEASE); 255 this.rollup.share.projectConfig.perf = PerfMode.ADVANCED; 256 initArkProjectConfig(this.rollup.share); 257 258 expect(printerTimeAndMemDataConfig.mTimeAndMemPrinter).to.be.true; 259 expect(performanceTimeAndMemPrinter.filesPrinter).to.not.be.undefined; 260 expect(performanceTimeAndMemPrinter.singleFilePrinter).to.not.be.undefined; 261 }); 262 263 mocha.it('3-1: test initTerserConfig under build debug', function () { 264 this.rollup.build(); 265 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 266 const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 267 const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs(); 268 const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar; 269 const minifyOptions = 270 utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled); 271 272 expect(minifyOptions.format.beautify === true).to.be.true; 273 expect(minifyOptions.format.indent_level === 2).to.be.true; 274 expect(minifyOptions.compress.join_vars === false).to.be.true; 275 expect(minifyOptions.compress.sequences === 0).to.be.true; 276 expect(minifyOptions.compress.directives === false).to.be.true; 277 expect(minifyOptions.compress.drop_console === false).to.be.true; 278 expect(minifyOptions.mangle.toplevel === false).to.be.true; 279 }); 280 281 mocha.it('3-2: test initTerserConfig under build release', function () { 282 this.rollup.build(RELEASE); 283 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 284 const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 285 const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs(); 286 const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar; 287 const minifyOptions = 288 utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled); 289 290 expect(minifyOptions.format.beautify === true).to.be.true; 291 expect(minifyOptions.format.indent_level === 2).to.be.true; 292 expect(minifyOptions.compress.join_vars === false).to.be.true; 293 expect(minifyOptions.compress.sequences === 0).to.be.true; 294 expect(minifyOptions.compress.directives === false).to.be.true; 295 expect(minifyOptions.compress.drop_console === false).to.be.true; 296 expect(minifyOptions.mangle.toplevel === false).to.be.true; 297 }); 298 299 mocha.it('3-3: test initTerserConfig under preview debug', function () { 300 this.rollup.preview(); 301 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 302 const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 303 const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs(); 304 const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar; 305 const minifyOptions = 306 utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled); 307 308 expect(minifyOptions.format.beautify === true).to.be.true; 309 expect(minifyOptions.format.indent_level === 2).to.be.true; 310 expect(minifyOptions.compress.join_vars === false).to.be.true; 311 expect(minifyOptions.compress.sequences === 0).to.be.true; 312 expect(minifyOptions.compress.directives === false).to.be.true; 313 expect(minifyOptions.compress.drop_console === false).to.be.true; 314 expect(minifyOptions.mangle.toplevel === false).to.be.true; 315 }); 316 317 mocha.it('3-4: test initTerserConfig under hot reload debug', function () { 318 this.rollup.hotReload(); 319 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 320 const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 321 const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs(); 322 const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar; 323 const minifyOptions = 324 utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled); 325 326 expect(minifyOptions.format.beautify === true).to.be.true; 327 expect(minifyOptions.format.indent_level === 2).to.be.true; 328 expect(minifyOptions.compress.join_vars === false).to.be.true; 329 expect(minifyOptions.compress.sequences === 0).to.be.true; 330 expect(minifyOptions.compress.directives === false).to.be.true; 331 expect(minifyOptions.compress.drop_console === false).to.be.true; 332 expect(minifyOptions.mangle.toplevel === false).to.be.true; 333 }); 334 335 mocha.it('4-1: test processCompatibleVersion under build debug', function () { 336 this.rollup.build(); 337 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 338 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 339 expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true; 340 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 341 342 this.rollup.share.projectConfig.minPlatformVersion = 8; 343 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 344 expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true; 345 expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true; 346 }); 347 348 mocha.it('4-2: test processCompatibleVersion under build release', function () { 349 this.rollup.build(RELEASE); 350 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 351 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 352 expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true; 353 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 354 355 this.rollup.share.projectConfig.minPlatformVersion = 8; 356 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 357 expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true; 358 expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true; 359 }); 360 361 mocha.it('4-3: test processCompatibleVersion under preview debug', function () { 362 this.rollup.preview(); 363 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 364 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 365 expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true; 366 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 367 368 this.rollup.share.projectConfig.minPlatformVersion = 8; 369 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 370 expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true; 371 expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true; 372 }); 373 374 mocha.it('4-4: test processCompatibleVersion under hot reload debug', function () { 375 this.rollup.hotReload(); 376 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 377 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 378 expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true; 379 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 380 381 this.rollup.share.projectConfig.minPlatformVersion = 8; 382 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 383 expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true; 384 expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true; 385 }); 386 387 mocha.describe('5: test readProjectAndLibsSource api', function () { 388 let mergedObConfig: MergedConfig = {}; 389 let keepFilesAndDependencies: Set<string> = new Set(); 390 const targetFile: string = path.join(__dirname, '../../../test/ark_compiler_ut/testdata/obfuscation/third_package/oh_modules/Index.ts'); 391 const allFiles: Set<string> = new Set([targetFile]); 392 let arkguardConfig: Object = {}; 393 const languageWhiteListNum = 7900; 394 mocha.before(function () { 395 this.rollup = new RollUpPluginMock(); 396 mergedObConfig = { 397 options: { 398 disableObfuscation: false, 399 enablePropertyObfuscation: false, 400 enableStringPropertyObfuscation: false, 401 enableToplevelObfuscation: false, 402 enableFileNameObfuscation: false, 403 enableExportObfuscation: true, 404 removeComments: false, 405 compact:false, 406 removeLog: false, 407 printNameCache: '', 408 applyNameCache: '' 409 } 410 }; 411 arkguardConfig = { 412 mNameObfuscation: { 413 mEnable: true, 414 mNameGeneratorType: 1, 415 mRenameProperties: false, 416 mReservedProperties: [], 417 mTopLevel: false, 418 mReservedToplevelNames:[], 419 }, 420 mExportObfuscation: true, 421 mPerformancePrinter: [] 422 }; 423 }); 424 425 mocha.it('5-1: test readProjectAndLibsSource with export obfuscation', function () { 426 let arkObfuscator: ArkObfuscator = new ArkObfuscator(); 427 arkObfuscator.init(arkguardConfig); 428 readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies); 429 430 expect(UnobfuscationCollections.reservedExportName.has('foo1')).to.be.true; 431 expect(UnobfuscationCollections.reservedExportName.has('foo2')).to.be.true; 432 expect(UnobfuscationCollections.reservedExportName.has('ts')).to.be.true; 433 expect(UnobfuscationCollections.reservedExportName.has('foo5')).to.be.true; 434 expect(UnobfuscationCollections.reservedExportName.has('foo6')).to.be.true; 435 expect(UnobfuscationCollections.reservedExportName.size === 5).to.be.true; 436 expect(UnobfuscationCollections.reservedLangForProperty.size > languageWhiteListNum).to.be.true; 437 UnobfuscationCollections.clear(); 438 }); 439 440 mocha.it('5-2: test readProjectAndLibsSource with export & toplevel obfuscation', function () { 441 let arkObfuscator: ArkObfuscator = new ArkObfuscator(); 442 arkguardConfig.mNameObfuscation.mTopLevel = true; 443 arkObfuscator.init(arkguardConfig); 444 mergedObConfig.options.enableToplevelObfuscation = true; 445 readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies); 446 447 expect(UnobfuscationCollections.reservedExportName.has('foo1')).to.be.true; 448 expect(UnobfuscationCollections.reservedExportName.has('foo2')).to.be.true; 449 expect(UnobfuscationCollections.reservedExportName.has('ts')).to.be.true; 450 expect(UnobfuscationCollections.reservedExportName.has('foo5')).to.be.true; 451 expect(UnobfuscationCollections.reservedExportName.has('foo6')).to.be.true; 452 expect(UnobfuscationCollections.reservedExportName.size === 5).to.be.true; 453 expect(UnobfuscationCollections.reservedLangForProperty.size > languageWhiteListNum).to.be.true; 454 UnobfuscationCollections.clear(); 455 }); 456 457 mocha.it('5-3: test readProjectAndLibsSource with property obfuscation', function () { 458 arkguardConfig.mNameObfuscation.mRenameProperties = true; 459 arkguardConfig.mExportObfuscation = false; 460 arkguardConfig.mNameObfuscation.mTopLevel = false; 461 let arkObfuscator: ArkObfuscator = new ArkObfuscator(); 462 arkObfuscator.init(arkguardConfig); 463 mergedObConfig.options.enablePropertyObfuscation = true; 464 mergedObConfig.options.enableExportObfuscation = false; 465 mergedObConfig.options.enableToplevelObfuscation = false; 466 readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies); 467 468 expect(UnobfuscationCollections.reservedExportName.size === 0).to.be.true; 469 expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo1')).to.be.true; 470 expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo2')).to.be.true; 471 expect(UnobfuscationCollections.reservedExportNameAndProp.has('ts')).to.be.true; 472 expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo5')).to.be.true; 473 expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo6')).to.be.true; 474 expect(UnobfuscationCollections.reservedExportNameAndProp.has('prop21')).to.be.true; 475 expect(UnobfuscationCollections.reservedExportNameAndProp.size === 6).to.be.true; 476 UnobfuscationCollections.clear(); 477 }); 478 479 mocha.it('5-4: test readProjectAndLibsSource with export & property & toplevel obfuscation', function () { 480 arkguardConfig.mNameObfuscation.mRenameProperties = true; 481 arkguardConfig.mExportObfuscation = true; 482 arkguardConfig.mNameObfuscation.mTopLevel = true; 483 let arkObfuscator: ArkObfuscator = new ArkObfuscator(); 484 arkObfuscator.init(arkguardConfig); 485 mergedObConfig.options.enablePropertyObfuscation = true; 486 mergedObConfig.options.enableToplevelObfuscation = true; 487 mergedObConfig.options.enableExportObfuscation = true; 488 readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies); 489 490 expect(UnobfuscationCollections.reservedExportName.has('foo1')).to.be.true; 491 expect(UnobfuscationCollections.reservedExportName.has('foo2')).to.be.true; 492 expect(UnobfuscationCollections.reservedExportName.has('ts')).to.be.true; 493 expect(UnobfuscationCollections.reservedExportName.has('foo5')).to.be.true; 494 expect(UnobfuscationCollections.reservedExportName.has('foo6')).to.be.true; 495 expect(UnobfuscationCollections.reservedExportName.size === 5).to.be.true; 496 expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo1')).to.be.true; 497 expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo2')).to.be.true; 498 expect(UnobfuscationCollections.reservedExportNameAndProp.has('ts')).to.be.true; 499 expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo5')).to.be.true; 500 expect(UnobfuscationCollections.reservedExportNameAndProp.has('foo6')).to.be.true; 501 expect(UnobfuscationCollections.reservedExportNameAndProp.has('prop21')).to.be.true; 502 expect(UnobfuscationCollections.reservedExportNameAndProp.size === 6).to.be.true; 503 UnobfuscationCollections.clear(); 504 }); 505 506 mocha.it('5-5: test readProjectAndLibsSource with enableAtKeep', function () { 507 this.rollup.build(RELEASE); 508 arkguardConfig.mNameObfuscation.mEnableAtKeep = true; 509 const cachePath: string = path.join(__dirname, '../../../test/ark_compiler_ut/testdata/obfuscation/'); 510 const rulesPath: string = path.join(__dirname, '../../../test/ark_compiler_ut/testdata/obfuscation/third_package/oh_modules/obfuscation-rules.txt'); 511 const consumerRulesPath: string = path.join(__dirname, '../../../test/ark_compiler_ut/testdata/obfuscation/third_package/oh_modules/consumer-rules.txt'); 512 const atKeepExportedPath: string = path.join(__dirname, '../../../test/ark_compiler_ut/testdata/obfuscation/third_package/oh_modules/exported-rules.txt'); 513 this.rollup.share.projectConfig.obfuscationOptions = { 514 'selfConfig': { 515 'ruleOptions': { 516 'enable': true, 517 'rules': [rulesPath] 518 }, 519 'consumerRules': [consumerRulesPath], 520 }, 521 'dependencies': { 522 'libraries': [], 523 'hars': [] 524 }, 525 obfuscationCacheDir: './test/testData/cache', 526 exportRulePath: atKeepExportedPath, 527 sdkApis: [] 528 }; 529 this.rollup.share.projectConfig.compileHar = true; 530 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 531 const obConfigResolver: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 532 obConfigResolver.resolveObfuscationConfigs(); 533 let arkObfuscator: ArkObfuscator = new ArkObfuscator(); 534 arkObfuscator.init(arkguardConfig, cachePath); 535 arkObfuscator.obfConfigResolver = obConfigResolver; 536 mergedObConfig.options.enableAtKeep = true; 537 readProjectAndLibsSource(allFiles, mergedObConfig, arkObfuscator, false, keepFilesAndDependencies); 538 obConfigResolver.emitConsumerConfigFiles(); 539 expect(AtKeepCollections.keepSymbol.globalNames.has('foo2')).to.be.true; 540 expect(AtKeepCollections.keepSymbol.propertyNames.has('foo2')).to.be.true; 541 expect(AtKeepCollections.keepSymbol.propertyNames.has('prop21')).to.be.true; 542 expect(AtKeepCollections.keepAsConsumer.globalNames.has('foo3')).to.be.true; 543 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('foo3')).to.be.false; 544 const exportedContent = fs.readFileSync(atKeepExportedPath, 'utf-8'); 545 expect(exportedContent).to.include('-enable-property-obfuscation'); 546 expect(exportedContent).to.include('-keep-global-name'); 547 expect(exportedContent).to.include('foo3'); 548 expect(exportedContent).to.include('globalName00'); 549 expect(exportedContent).to.include('-keep-property-name'); 550 expect(exportedContent).to.include('propertyName00'); 551 expect(exportedContent).not.to.include('foo2'); 552 expect(exportedContent).not.to.include('prop21'); 553 }); 554 }); 555 556 mocha.it('6-1: test processPlatformInfo on windows', function () { 557 this.rollup.build(); 558 const stub = sinon.stub(os, 'type').returns(WINDOWS); 559 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 560 const expectEs2abcPath = path.join(ARKROOT_PATH, WIN_ES2ABC_PATH); 561 const expectTs2abcPath = path.join(ARKROOT_PATH, WIN_TS2ABC_PATH); 562 const expectMergeAbcPath = path.join(ARKROOT_PATH, WIN_MERGERABC_PATH); 563 const expectJs2abcPath = path.join(ARKROOT_PATH, WIN_JS2ABC_PATH); 564 const expectAotCompilerPath = path.join(ARKROOT_PATH, WIN_AOTCOMPILER_PATH); 565 const expectBcObfuscatorPath = path.join(ARKROOT_PATH, WIN_BC_OBFUSCATOR_PATH); 566 expect(arkConfig.es2abcPath === expectEs2abcPath).to.be.true; 567 expect(arkConfig.ts2abcPath === expectTs2abcPath).to.be.true; 568 expect(arkConfig.mergeAbcPath === expectMergeAbcPath).to.be.true; 569 expect(arkConfig.js2abcPath === expectJs2abcPath).to.be.true; 570 expect(arkConfig.aotCompilerPath === expectAotCompilerPath).to.be.true; 571 expect(arkConfig.bcObfuscatorPath === expectBcObfuscatorPath).to.be.true; 572 stub.restore(); 573 }); 574 575 mocha.it('6-2: test processPlatformInfo on linux', function () { 576 this.rollup.build(); 577 const stub = sinon.stub(os, 'type').returns(LINUX); 578 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 579 const expectEs2abcPath = path.join(ARKROOT_PATH, ES2ABC_PATH); 580 const expectTs2abcPath = path.join(ARKROOT_PATH, TS2ABC_PATH); 581 const expectMergeAbcPath = path.join(ARKROOT_PATH, MERGERABC_PATH); 582 const expectJs2abcPath = path.join(ARKROOT_PATH, JS2ABC_PATH); 583 const expectAotCompilerPath = path.join(ARKROOT_PATH, AOTCOMPILER_PATH); 584 const expectBcObfuscatorPath = path.join(ARKROOT_PATH, BC_OBFUSCATOR_PATH); 585 expect(arkConfig.es2abcPath === expectEs2abcPath).to.be.true; 586 expect(arkConfig.ts2abcPath === expectTs2abcPath).to.be.true; 587 expect(arkConfig.mergeAbcPath === expectMergeAbcPath).to.be.true; 588 expect(arkConfig.js2abcPath === expectJs2abcPath).to.be.true; 589 expect(arkConfig.aotCompilerPath === expectAotCompilerPath).to.be.true; 590 expect(arkConfig.bcObfuscatorPath === expectBcObfuscatorPath).to.be.true; 591 stub.restore(); 592 }); 593 594 mocha.it('6-3: test processPlatformInfo on mac', function () { 595 this.rollup.build(); 596 const stub = sinon.stub(os, 'type').returns(MAC); 597 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 598 const expectEs2abcPath = path.join(ARKROOT_PATH, MAC_ES2ABC_PATH); 599 const expectTs2abcPath = path.join(ARKROOT_PATH, MAC_TS2ABC_PATH); 600 const expectMergeAbcPath = path.join(ARKROOT_PATH, MAC_MERGERABC_PATH); 601 const expectJs2abcPath = path.join(ARKROOT_PATH, MAC_JS2ABC_PATH); 602 const expectAotCompilerPath = path.join(ARKROOT_PATH, MAC_AOTCOMPILER_PATH); 603 const expectBcObfuscatorPath = path.join(ARKROOT_PATH, MAC_BC_OBFUSCATOR_PATH); 604 expect(arkConfig.es2abcPath === expectEs2abcPath).to.be.true; 605 expect(arkConfig.ts2abcPath === expectTs2abcPath).to.be.true; 606 expect(arkConfig.mergeAbcPath === expectMergeAbcPath).to.be.true; 607 expect(arkConfig.js2abcPath === expectJs2abcPath).to.be.true; 608 expect(arkConfig.aotCompilerPath === expectAotCompilerPath).to.be.true; 609 expect(arkConfig.bcObfuscatorPath === expectBcObfuscatorPath).to.be.true; 610 stub.restore(); 611 }); 612 613 mocha.it('6-4: test processPlatformInfo on harmonyos', function () { 614 this.rollup.build(); 615 const stub = sinon.stub(os, 'type').returns(HARMONYOS); 616 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 617 const expectEs2abcPath = path.join(ARKROOT_PATH, ES2ABC_PATH); 618 expect(arkConfig.es2abcPath === expectEs2abcPath).to.be.true; 619 stub.restore(); 620 }); 621});