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 hspResolver.emitConsumerConfigFiles(); 263 264 const hspObfuscationContent = fs.readFileSync(localHspConfig.exportRulePath, 'utf-8'); 265 expect(hspObfuscationContent).not.to.include('-disable-obfuscation'); 266 expect(hspObfuscationContent).to.include('-enable-toplevel-obfuscation'); 267 expect(hspObfuscationContent).not.to.include('-enable-property-obfuscation'); 268 expect(hspObfuscationContent).not.to.include('-enable-string-property-obfuscation'); 269 expect(hspObfuscationContent).not.to.include('-enable-filename-obfuscation'); 270 expect(hspObfuscationContent).not.to.include('-enable-export-obfuscation'); 271 expect(hspObfuscationContent).not.to.include('-remove-comments'); 272 expect(hspObfuscationContent).not.to.include('-compact'); 273 expect(hspObfuscationContent).not.to.include('-remove-log'); 274 275 expect(hspObfuscationContent).not.to.include('-keep-dts'); 276 expect(hspObfuscationContent).to.include('-keep-global-name'); 277 expect(hspObfuscationContent).to.include('-keep-property-name'); 278 expect(hspObfuscationContent).not.to.include('-keep-file-name'); 279 expect(hspObfuscationContent).not.to.include('-keep-comments'); 280 281 expect(hspObfuscationContent).to.include('localHspMethod'); 282 expect(hspObfuscationContent).to.include('LocalHspClass'); 283 expect(hspObfuscationContent).not.to.include('LocalHarClass'); 284 expect(hspObfuscationContent).not.to.include('localHarMethod'); 285 expect(hspObfuscationContent).not.to.include('RemoteHarClass'); 286 expect(hspObfuscationContent).not.to.include('remoteHarMethod'); 287 }); 288 289 it('should merge HAR and HSP consumer rules for HAP when HAP depends on localHsp and localHsp depends on localHar', function () { 290 hapConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig); 291 const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger); 292 const finalConfig = configResolver.resolveObfuscationConfigs(); 293 expect(finalConfig.options.enableToplevelObfuscation).to.be.false; 294 expect(finalConfig.options.enableExportObfuscation).to.be.true; 295 expect(finalConfig.options.enablePropertyObfuscation).to.be.false; 296 expect(finalConfig.options.disableObfuscation).to.be.false; 297 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 298 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 299 expect(finalConfig.options.printKeptNames).to.be.false; 300 expect(finalConfig.options.removeComments).to.be.false; 301 expect(finalConfig.options.compact).to.be.false; 302 expect(finalConfig.options.removeLog).to.be.false; 303 304 expect(finalConfig.reservedGlobalNames).to.include('LocalHspClass'); 305 expect(finalConfig.reservedPropertyNames).to.include('localHspMethod'); 306 expect(finalConfig.reservedGlobalNames).not.to.include('LocalHarClass'); 307 expect(finalConfig.reservedPropertyNames).not.to.include('localHarMethod'); 308 expect(finalConfig.reservedNames.length).to.equal(0); 309 expect(finalConfig.reservedFileNames.length).to.equal(0); 310 expect(finalConfig.keepComments.length).to.equal(0); 311 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 312 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 313 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 314 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 315 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 316 expect(finalConfig.excludePathSet.size).to.equal(0); 317 }); 318 319 afterEach(() => { 320 if (fs.existsSync(localHspConfig.exportRulePath)) { 321 fs.unlinkSync(localHspConfig.exportRulePath); 322 } 323 if (fs.existsSync(systemApiCacheFile)) { 324 fs.unlinkSync(systemApiCacheFile); 325 } 326 }); 327 }); 328 329 describe('localHAR depends on localHSP Tests', function () { 330 before(async () => { 331 initModulesConfig(); 332 }); 333 334 it('should merge both HSP and HAR consumer rules in HAR obfuscation.txt', function () { 335 localHarConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig); 336 const localHarResolver = new ObConfigResolver({ obfuscationOptions: localHarConfig, compileHar: true, compileShared: false }, printObfLogger); 337 localHarResolver.resolveObfuscationConfigs(); 338 localHarResolver.emitConsumerConfigFiles(); 339 340 const localHarObfuscationContent = fs.readFileSync(localHarConfig.exportRulePath, 'utf-8'); 341 342 expect(localHarObfuscationContent).to.include('-enable-toplevel-obfuscation'); 343 expect(localHarObfuscationContent).to.include('-enable-property-obfuscation'); 344 expect(localHarObfuscationContent).not.to.include('-enable-string-property-obfuscation'); 345 expect(localHarObfuscationContent).not.to.include('-enable-filename-obfuscation'); 346 expect(localHarObfuscationContent).not.to.include('-enable-export-obfuscation'); 347 expect(localHarObfuscationContent).not.to.include('-remove-comments'); 348 expect(localHarObfuscationContent).not.to.include('-compact'); 349 expect(localHarObfuscationContent).not.to.include('-remove-log'); 350 351 expect(localHarObfuscationContent).to.include('-keep-global-name'); 352 expect(localHarObfuscationContent).to.include('-keep-property-name'); 353 expect(localHarObfuscationContent).not.to.include('-keep-file-name'); 354 expect(localHarObfuscationContent).not.to.include('-keep-comments'); 355 expect(localHarObfuscationContent).not.to.include('-keep-dts'); 356 357 expect(localHarObfuscationContent).to.include('LocalHspClass'); 358 expect(localHarObfuscationContent).to.include('localHspMethod'); 359 expect(localHarObfuscationContent).to.include('LocalHarClass'); 360 expect(localHarObfuscationContent).to.include('localHarMethod'); 361 }); 362 363 it('should merge HAR consumer rules for HAP when HAP depends on localHar and localHar depends on localHsp', function () { 364 hapConfig.dependencies.libraries.push(localHarConfig.selfConfig); 365 hapConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig); 366 const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger); 367 const finalConfig = configResolver.resolveObfuscationConfigs(); 368 369 expect(finalConfig.options.enableToplevelObfuscation).to.be.false; 370 expect(finalConfig.options.enableExportObfuscation).to.be.true; 371 expect(finalConfig.options.enablePropertyObfuscation).to.be.false; 372 expect(finalConfig.options.disableObfuscation).to.be.false; 373 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 374 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 375 expect(finalConfig.options.printKeptNames).to.be.false; 376 expect(finalConfig.options.removeComments).to.be.false; 377 expect(finalConfig.options.compact).to.be.false; 378 expect(finalConfig.options.removeLog).to.be.false; 379 380 expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass'); 381 expect(finalConfig.reservedPropertyNames).to.include('localHarMethod'); 382 expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass'); 383 expect(finalConfig.reservedPropertyNames).to.include('localHarMethod'); 384 expect(finalConfig.reservedNames.length).to.equal(0); 385 expect(finalConfig.reservedFileNames.length).to.equal(0); 386 expect(finalConfig.keepComments.length).to.equal(0); 387 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 388 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 389 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 390 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 391 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 392 expect(finalConfig.excludePathSet.size).to.equal(0); 393 }); 394 395 afterEach(() => { 396 if (fs.existsSync(localHspConfig.exportRulePath)) { 397 fs.unlinkSync(localHspConfig.exportRulePath); 398 } 399 if (fs.existsSync(localHarConfig.exportRulePath)) { 400 fs.unlinkSync(localHarConfig.exportRulePath); 401 } 402 if (fs.existsSync(systemApiCacheFile)) { 403 fs.unlinkSync(systemApiCacheFile); 404 } 405 }); 406 }); 407 408 describe('localHAR depends on remoteHSP Tests', function () { 409 before(async () => { 410 initModulesConfig(); 411 }); 412 413 it('should merge both HSP obfuscation.txt and HAR consumer rules in HAR obfuscation.txt', function () { 414 localHarConfig.dependencies.hsps?.push(remoteHspObfFile); 415 const localHarResolver = new ObConfigResolver({ obfuscationOptions: localHarConfig, compileHar: true, compileShared: false }, printObfLogger); 416 localHarResolver.resolveObfuscationConfigs(); 417 localHarResolver.emitConsumerConfigFiles(); 418 419 const localHarObfuscationContent = fs.readFileSync(localHarConfig.exportRulePath, 'utf-8'); 420 421 expect(localHarObfuscationContent).to.include('-remove-log'); 422 expect(localHarObfuscationContent).to.include('-enable-property-obfuscation'); 423 expect(localHarObfuscationContent).not.to.include('-enable-toplevel-obfuscation'); 424 expect(localHarObfuscationContent).not.to.include('-enable-string-property-obfuscation'); 425 expect(localHarObfuscationContent).not.to.include('-enable-filename-obfuscation'); 426 expect(localHarObfuscationContent).not.to.include('-enable-export-obfuscation'); 427 expect(localHarObfuscationContent).not.to.include('-remove-comments'); 428 expect(localHarObfuscationContent).not.to.include('-compact'); 429 430 expect(localHarObfuscationContent).to.include('-keep-global-name'); 431 expect(localHarObfuscationContent).to.include('-keep-property-name'); 432 expect(localHarObfuscationContent).not.to.include('-keep-file-name'); 433 expect(localHarObfuscationContent).not.to.include('-keep-comments'); 434 expect(localHarObfuscationContent).not.to.include('-keep-dts'); 435 436 expect(localHarObfuscationContent).to.include('RemoteHspClass'); 437 expect(localHarObfuscationContent).to.include('remoteHspMethod'); 438 expect(localHarObfuscationContent).to.include('LocalHarClass'); 439 expect(localHarObfuscationContent).to.include('localHarMethod'); 440 }); 441 442 it('should merge HAR consumer rules for HAP when HAP depends on localHar and localHar depends on remoteHsp', function () { 443 hapConfig.dependencies.libraries.push(localHarConfig.selfConfig); 444 hapConfig.dependencies.hsps?.push(remoteHspObfFile); 445 const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger); 446 const finalConfig = configResolver.resolveObfuscationConfigs(); 447 448 expect(finalConfig.options.removeLog).to.be.false; 449 expect(finalConfig.options.enablePropertyObfuscation).to.be.false; 450 expect(finalConfig.options.enableExportObfuscation).to.be.true; 451 expect(finalConfig.options.enableToplevelObfuscation).to.be.false; 452 expect(finalConfig.options.disableObfuscation).to.be.false; 453 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 454 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 455 expect(finalConfig.options.printKeptNames).to.be.false; 456 expect(finalConfig.options.removeComments).to.be.false; 457 expect(finalConfig.options.compact).to.be.false; 458 459 expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass'); 460 expect(finalConfig.reservedPropertyNames).to.include('localHarMethod'); 461 expect(finalConfig.reservedGlobalNames).to.include('RemoteHspClass'); 462 expect(finalConfig.reservedPropertyNames).to.include('remoteHspMethod'); 463 expect(finalConfig.reservedNames.length).to.equal(0); 464 expect(finalConfig.reservedFileNames.length).to.equal(0); 465 expect(finalConfig.keepComments.length).to.equal(0); 466 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 467 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 468 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 469 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 470 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 471 expect(finalConfig.excludePathSet.size).to.equal(0); 472 }); 473 474 afterEach(() => { 475 if (fs.existsSync(remoteHspConfig.exportRulePath)) { 476 fs.unlinkSync(remoteHspConfig.exportRulePath); 477 } 478 if (fs.existsSync(localHarConfig.exportRulePath)) { 479 fs.unlinkSync(localHarConfig.exportRulePath); 480 } 481 if (fs.existsSync(systemApiCacheFile)) { 482 fs.unlinkSync(systemApiCacheFile); 483 } 484 }); 485 }); 486 487 describe('remoteHAR depends on localHSP Tests', function () { 488 before(async () => { 489 initModulesConfig(); 490 }); 491 492 it('should retain HAR consumer rules for HAP when HAP depends on remoteHar and remoteHar depends on localHsp', function () { 493 hapConfig.dependencies.hars.push(remoteHarDependsLocalHspObfFile); 494 const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger); 495 const finalConfig = configResolver.resolveObfuscationConfigs(); 496 497 expect(finalConfig.options.compact).to.be.false; 498 expect(finalConfig.options.enableToplevelObfuscation).to.be.false; 499 expect(finalConfig.options.enableExportObfuscation).to.be.true; 500 expect(finalConfig.options.removeLog).to.be.false; 501 expect(finalConfig.options.enablePropertyObfuscation).to.be.false; 502 expect(finalConfig.options.disableObfuscation).to.be.false; 503 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 504 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 505 expect(finalConfig.options.printKeptNames).to.be.false; 506 expect(finalConfig.options.removeComments).to.be.false; 507 508 expect(finalConfig.reservedGlobalNames).to.include('RemoteHarClass'); 509 expect(finalConfig.reservedPropertyNames).to.include('remoteHarMethod'); 510 expect(finalConfig.reservedGlobalNames).to.include('LocalHspClass'); 511 expect(finalConfig.reservedPropertyNames).to.include('localHspMethod'); 512 expect(finalConfig.reservedNames.length).to.equal(0); 513 expect(finalConfig.reservedFileNames.length).to.equal(0); 514 expect(finalConfig.keepComments.length).to.equal(0); 515 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 516 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 517 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 518 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 519 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 520 expect(finalConfig.excludePathSet.size).to.equal(0); 521 }); 522 523 afterEach(() => { 524 if (fs.existsSync(localHspConfig.exportRulePath)) { 525 fs.unlinkSync(localHspConfig.exportRulePath); 526 } 527 if (fs.existsSync(remoteHarConfig.exportRulePath)) { 528 fs.unlinkSync(remoteHarConfig.exportRulePath); 529 } 530 if (fs.existsSync(systemApiCacheFile)) { 531 fs.unlinkSync(systemApiCacheFile); 532 } 533 }); 534 }); 535 536 describe('remoteHAR depends on remoteHSP Tests', function () { 537 before(async () => { 538 initModulesConfig(); 539 }); 540 541 it('should retain HAR consumer rules for HAP when HAP depends on remoteHar and remoteHar depends on remoteHsp', function () { 542 hapConfig.dependencies.hars.push(remoteHarDependsRemoteHspObfFile); 543 const configResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger); 544 const finalConfig = configResolver.resolveObfuscationConfigs(); 545 546 expect(finalConfig.options.compact).to.be.false; 547 expect(finalConfig.options.removeLog).to.be.false; 548 expect(finalConfig.options.enableExportObfuscation).to.be.true; 549 expect(finalConfig.options.enableToplevelObfuscation).to.be.false; 550 expect(finalConfig.options.enablePropertyObfuscation).to.be.false; 551 expect(finalConfig.options.disableObfuscation).to.be.false; 552 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 553 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 554 expect(finalConfig.options.printKeptNames).to.be.false; 555 expect(finalConfig.options.removeComments).to.be.false; 556 557 expect(finalConfig.reservedGlobalNames).to.include('RemoteHarClass'); 558 expect(finalConfig.reservedPropertyNames).to.include('remoteHarMethod'); 559 expect(finalConfig.reservedGlobalNames).to.include('RemoteHspClass'); 560 expect(finalConfig.reservedPropertyNames).to.include('remoteHspMethod'); 561 expect(finalConfig.reservedNames.length).to.equal(0); 562 expect(finalConfig.reservedFileNames.length).to.equal(0); 563 expect(finalConfig.keepComments.length).to.equal(0); 564 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 565 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 566 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 567 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 568 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 569 expect(finalConfig.excludePathSet.size).to.equal(0); 570 }); 571 572 after(() => { 573 if (fs.existsSync(remoteHspConfig.exportRulePath)) { 574 fs.unlinkSync(remoteHspConfig.exportRulePath); 575 } 576 if (fs.existsSync(remoteHarConfig.exportRulePath)) { 577 fs.unlinkSync(remoteHarConfig.exportRulePath); 578 } 579 if (fs.existsSync(systemApiCacheFile)) { 580 fs.unlinkSync(systemApiCacheFile); 581 } 582 }); 583 }); 584 585 describe('localHSP depends on localHSP and RemoteHSP Tests', function () { 586 before(async () => { 587 initModulesConfig(); 588 }); 589 590 it('should contain only MainHSP consumer rules in MainHSP obfuscation.txt', function () { 591 localMainHspConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig); 592 localMainHspConfig.dependencies.hsps?.push(remoteHspObfFile); 593 const localMainHspResolver = new ObConfigResolver({ obfuscationOptions: localMainHspConfig, compileHar: false, compileShared: true }, printObfLogger); 594 localMainHspResolver.resolveObfuscationConfigs(); 595 localMainHspResolver.emitConsumerConfigFiles(); 596 597 const localMainHspObfuscationContent = fs.readFileSync(localMainHspConfig.exportRulePath, 'utf-8'); 598 599 expect(localMainHspObfuscationContent).not.to.include('-enable-filename-obfuscation'); 600 expect(localMainHspObfuscationContent).not.to.include('-remove-log'); 601 expect(localMainHspObfuscationContent).not.to.include('-enable-property-obfuscation'); 602 expect(localMainHspObfuscationContent).not.to.include('-enable-toplevel-obfuscation'); 603 expect(localMainHspObfuscationContent).not.to.include('-enable-string-property-obfuscation'); 604 expect(localMainHspObfuscationContent).not.to.include('-enable-export-obfuscation'); 605 expect(localMainHspObfuscationContent).not.to.include('-remove-comments'); 606 expect(localMainHspObfuscationContent).not.to.include('-compact'); 607 608 expect(localMainHspObfuscationContent).to.include('-keep-global-name'); 609 expect(localMainHspObfuscationContent).to.include('-keep-property-name'); 610 expect(localMainHspObfuscationContent).not.to.include('-keep-file-name'); 611 expect(localMainHspObfuscationContent).not.to.include('-keep-comments'); 612 expect(localMainHspObfuscationContent).not.to.include('-keep-dts'); 613 614 expect(localMainHspObfuscationContent).to.include('LocalMainHspClass'); 615 expect(localMainHspObfuscationContent).to.include('localMainHspGreeting'); 616 expect(localMainHspObfuscationContent).not.to.include('LocalHspClass'); 617 expect(localMainHspObfuscationContent).not.to.include('localHspMethod'); 618 expect(localMainHspObfuscationContent).not.to.include('RemoteHspClass'); 619 expect(localMainHspObfuscationContent).not.to.include('remoteHspMethod'); 620 }); 621 622 it('should only retain MainHSP consumer rules when HAP depends on localMainHsp and localMainHsp depends on localHsp and remoteHsp', function () { 623 hapConfig.dependencies.hspLibraries?.push(localMainHspConfig.selfConfig); 624 const hapResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger); 625 const finalConfig = hapResolver.resolveObfuscationConfigs(); 626 627 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 628 expect(finalConfig.options.enableExportObfuscation).to.be.true; 629 expect(finalConfig.options.compact).to.be.false; 630 expect(finalConfig.options.removeLog).to.be.false; 631 expect(finalConfig.options.enableToplevelObfuscation).to.be.false; 632 expect(finalConfig.options.enablePropertyObfuscation).to.be.false; 633 expect(finalConfig.options.disableObfuscation).to.be.false; 634 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 635 expect(finalConfig.options.printKeptNames).to.be.false; 636 expect(finalConfig.options.removeComments).to.be.false; 637 638 expect(finalConfig.reservedGlobalNames).to.include('LocalMainHspClass'); 639 expect(finalConfig.reservedPropertyNames).to.include('localMainHspGreeting'); 640 expect(finalConfig.reservedGlobalNames).not.to.include('LocalHspClass'); 641 expect(finalConfig.reservedPropertyNames).not.to.include('localHspMethod'); 642 expect(finalConfig.reservedGlobalNames).not.to.include('RemoteHspClass'); 643 expect(finalConfig.reservedPropertyNames).not.to.include('remoteHspMethod'); 644 expect(finalConfig.reservedNames.length).to.equal(0); 645 expect(finalConfig.reservedFileNames.length).to.equal(0); 646 expect(finalConfig.keepComments.length).to.equal(0); 647 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 648 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 649 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 650 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 651 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 652 expect(finalConfig.excludePathSet.size).to.equal(0); 653 }); 654 655 after(() => { 656 if (fs.existsSync(remoteHspConfig.exportRulePath)) { 657 fs.unlinkSync(remoteHspConfig.exportRulePath); 658 } 659 if (fs.existsSync(localHspConfig.exportRulePath)) { 660 fs.unlinkSync(localHspConfig.exportRulePath); 661 } 662 if (fs.existsSync(localMainHspConfig.exportRulePath)) { 663 fs.unlinkSync(localMainHspConfig.exportRulePath); 664 } 665 if (fs.existsSync(systemApiCacheFile)) { 666 fs.unlinkSync(systemApiCacheFile); 667 } 668 }); 669 }); 670 671 describe('remoteHSP depends on HSP Tests', function () { 672 before(async () => { 673 initModulesConfig(); 674 }); 675 676 it('should only retain MainHSP consumer rules for HAP when HAP depends on RemoteMainHsp and RemoteMainHsp depends on localHsp and remoteHsp', function () { 677 hapConfig.dependencies.hsps?.push(remoteMainHspObfFile); 678 const hapResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger); 679 const finalConfig = hapResolver.resolveObfuscationConfigs(); 680 681 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 682 expect(finalConfig.options.enableExportObfuscation).to.be.true; 683 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 684 expect(finalConfig.options.compact).to.be.false; 685 expect(finalConfig.options.removeLog).to.be.false; 686 expect(finalConfig.options.enableToplevelObfuscation).to.be.false; 687 expect(finalConfig.options.enablePropertyObfuscation).to.be.false; 688 expect(finalConfig.options.disableObfuscation).to.be.false; 689 expect(finalConfig.options.printKeptNames).to.be.false; 690 expect(finalConfig.options.removeComments).to.be.false; 691 692 expect(finalConfig.reservedGlobalNames).to.include('RemoteMainHspClass'); 693 expect(finalConfig.reservedPropertyNames).to.include('remoteMainHspGreeting'); 694 expect(finalConfig.reservedGlobalNames).not.to.include('LocalHspClass'); 695 expect(finalConfig.reservedPropertyNames).not.to.include('localHspMethod'); 696 expect(finalConfig.reservedGlobalNames).not.to.include('RemoteHspClass'); 697 expect(finalConfig.reservedPropertyNames).not.to.include('remoteHspMethod'); 698 expect(finalConfig.reservedNames.length).to.equal(0); 699 expect(finalConfig.reservedFileNames.length).to.equal(0); 700 expect(finalConfig.keepComments.length).to.equal(0); 701 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 702 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 703 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 704 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 705 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 706 expect(finalConfig.excludePathSet.size).to.equal(0); 707 }); 708 709 after(() => { 710 if (fs.existsSync(remoteHspConfig.exportRulePath)) { 711 fs.unlinkSync(remoteHspConfig.exportRulePath); 712 } 713 if (fs.existsSync(localHspConfig.exportRulePath)) { 714 fs.unlinkSync(localHspConfig.exportRulePath); 715 } 716 if (fs.existsSync(remoteMainHspConfig.exportRulePath)) { 717 fs.unlinkSync(remoteMainHspConfig.exportRulePath); 718 } 719 if (fs.existsSync(systemApiCacheFile)) { 720 fs.unlinkSync(systemApiCacheFile); 721 } 722 }); 723 }) 724 725 describe('HAP depends on local and remote HAR and HSP Tests', function () { 726 before(async () => { 727 initModulesConfig(); 728 }); 729 730 it('should contain all local and remote HAR and HSP consumer rules in final HAP config', function () { 731 hapConfig.dependencies.libraries.push(localHarConfig.selfConfig); 732 hapConfig.dependencies.hars.push(remoteHarObfFile); 733 hapConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig); 734 hapConfig.dependencies.hsps?.push(remoteHspObfFile); 735 const hapResolver = new ObConfigResolver({ obfuscationOptions: hapConfig, compileHar: false, compileShared: false }, printObfLogger); 736 const finalConfig = hapResolver.resolveObfuscationConfigs(); 737 738 expect(finalConfig.options.enableExportObfuscation).to.be.true; 739 expect(finalConfig.options.compact).to.be.false; 740 expect(finalConfig.options.removeLog).to.be.false; 741 expect(finalConfig.options.enableToplevelObfuscation).to.be.false; 742 expect(finalConfig.options.enablePropertyObfuscation).to.be.false; 743 expect(finalConfig.options.disableObfuscation).to.be.false; 744 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 745 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 746 expect(finalConfig.options.printKeptNames).to.be.false; 747 expect(finalConfig.options.removeComments).to.be.false; 748 749 expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass'); 750 expect(finalConfig.reservedPropertyNames).to.include('localHarMethod'); 751 expect(finalConfig.reservedGlobalNames).to.include('LocalHspClass'); 752 expect(finalConfig.reservedPropertyNames).to.include('localHspMethod'); 753 expect(finalConfig.reservedGlobalNames).to.include('RemoteHarClass'); 754 expect(finalConfig.reservedPropertyNames).to.include('remoteHarMethod'); 755 expect(finalConfig.reservedGlobalNames).to.include('RemoteHspClass'); 756 expect(finalConfig.reservedPropertyNames).to.include('remoteHspMethod'); 757 expect(finalConfig.reservedNames.length).to.equal(0); 758 expect(finalConfig.reservedFileNames.length).to.equal(0); 759 expect(finalConfig.keepComments.length).to.equal(0); 760 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 761 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 762 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 763 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 764 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 765 expect(finalConfig.excludePathSet.size).to.equal(0); 766 }); 767 768 after(() => { 769 if (fs.existsSync(systemApiCacheFile)) { 770 fs.unlinkSync(systemApiCacheFile); 771 } 772 }); 773 }); 774 775 describe('-enable-lib-obfuscation-options test', function () { 776 before(async () => { 777 initModulesConfig(); 778 }); 779 780 it('should contain all local and remote HAR and HSP consumer rules in final HAP config', function () { 781 libTestHapConfig.dependencies.libraries.push(localHarConfig.selfConfig); 782 libTestHapConfig.dependencies.hars.push(remoteHarObfFile); 783 libTestHapConfig.dependencies.hspLibraries?.push(localHspConfig.selfConfig); 784 libTestHapConfig.dependencies.hsps?.push(remoteHspObfFile); 785 const hapResolver = new ObConfigResolver( 786 { obfuscationOptions: libTestHapConfig, compileHar: false, compileShared: false }, 787 printObfLogger, 788 ); 789 const finalConfig = hapResolver.resolveObfuscationConfigs(); 790 791 expect(finalConfig.options.enableExportObfuscation).to.be.true; 792 expect(finalConfig.options.compact).to.be.true; 793 expect(finalConfig.options.removeLog).to.be.true; 794 expect(finalConfig.options.enableToplevelObfuscation).to.be.true; 795 expect(finalConfig.options.enablePropertyObfuscation).to.be.true; 796 expect(finalConfig.options.disableObfuscation).to.be.false; 797 expect(finalConfig.options.enableFileNameObfuscation).to.be.false; 798 expect(finalConfig.options.enableStringPropertyObfuscation).to.be.false; 799 expect(finalConfig.options.printKeptNames).to.be.false; 800 expect(finalConfig.options.removeComments).to.be.false; 801 802 expect(finalConfig.reservedGlobalNames).to.include('LocalHarClass'); 803 expect(finalConfig.reservedPropertyNames).to.include('localHarMethod'); 804 expect(finalConfig.reservedGlobalNames).to.include('LocalHspClass'); 805 expect(finalConfig.reservedPropertyNames).to.include('localHspMethod'); 806 expect(finalConfig.reservedGlobalNames).to.include('RemoteHarClass'); 807 expect(finalConfig.reservedPropertyNames).to.include('remoteHarMethod'); 808 expect(finalConfig.reservedGlobalNames).to.include('RemoteHspClass'); 809 expect(finalConfig.reservedPropertyNames).to.include('remoteHspMethod'); 810 expect(finalConfig.reservedNames.length).to.equal(0); 811 expect(finalConfig.reservedFileNames.length).to.equal(0); 812 expect(finalConfig.keepComments.length).to.equal(0); 813 expect(finalConfig.keepSourceOfPaths.length).to.equal(0); 814 expect(finalConfig.universalReservedPropertyNames.length).to.equal(0); 815 expect(finalConfig.universalReservedGlobalNames.length).to.equal(0); 816 expect(finalConfig.keepUniversalPaths.length).to.equal(0); 817 expect(finalConfig.excludeUniversalPaths.length).to.equal(0); 818 expect(finalConfig.excludePathSet.size).to.equal(0); 819 }); 820 821 after(() => { 822 if (fs.existsSync(systemApiCacheFile)) { 823 fs.unlinkSync(systemApiCacheFile); 824 } 825 }); 826 }); 827 }) 828 }); 829})