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 { describe, it } from 'mocha'; 17import { expect } from 'chai'; 18import { 19 addToSet, 20 arrayToSet, 21 setToArray, 22 areSetsEqual, 23 FilePathManager, 24 FileContentManager, 25 ProjectWhiteListManager, 26 FileWhiteList, 27 FileContent 28} from '../../../src/utils/ProjectCollections'; 29import { AtIntentCollections, AtKeepCollections, UnobfuscationCollections } from '../../../src/utils/CommonCollections'; 30import { ApiExtractor } from '../../../src/common/ApiExtractor' 31import { FileUtils } from '../../../src/utils/FileUtils'; 32import * as fs from 'fs'; 33import path from 'path'; 34 35function compareFiles(filePath1: string, filePath2: string): boolean { 36 const content1 = fs.readFileSync(filePath1, 'utf8'); 37 const content2 = fs.readFileSync(filePath2, 'utf8'); 38 return content1 === content2; 39} 40 41describe('test for CommonCollections', function () { 42 describe('test for addToSet', function () { 43 it('should add all elements from sourceSet to targetSet', ()=> { 44 const targetSet = new Set<number>([1, 2]); 45 const sourceSet = new Set<number>([2, 3, 4]); 46 addToSet(targetSet, sourceSet); 47 expect(targetSet.size).to.be.equal(4); 48 expect(targetSet.has(1)).to.be.true; 49 expect(targetSet.has(2)).to.be.true; 50 expect(targetSet.has(3)).to.be.true; 51 expect(targetSet.has(4)).to.be.true; 52 }); 53 }); 54 describe('test for arrayToSet', function () { 55 it('should convert an array to a set', ()=> { 56 const array = [1, 2, 3]; 57 const set = arrayToSet(array); 58 expect(set.size).to.be.equal(3); 59 expect(set.has(1)).to.be.true; 60 expect(set.has(2)).to.be.true; 61 expect(set.has(3)).to.be.true; 62 }); 63 }); 64 describe('test for setToArray', function () { 65 it('should convert a set to an array', () => { 66 const set = new Set<number>([1, 2, 3]); 67 const array = setToArray(set); 68 expect(array).to.deep.equal([1, 2, 3]) 69 }); 70 }); 71 describe('test for areSetsEqual', function () { 72 it('should return true for equal sets', () => { 73 const set1 = new Set<number>([1, 2, 3]); 74 const set2 = new Set<number>([1, 2, 3]); 75 expect(areSetsEqual(set1, set2)).to.be.true; 76 }); 77 78 it('should return false for unequal sets', () => { 79 const set1 = new Set<number>([1, 2, 3]); 80 const set2 = new Set<number>([1, 2, 4]); 81 expect(areSetsEqual(set1, set2)).to.be.false; 82 }); 83 84 it('should return false for sets of different sizes', () => { 85 const set1 = new Set<number>([1, 2]); 86 const set2 = new Set<number>([1, 2, 3]); 87 expect(areSetsEqual(set1, set2)).to.be.false; 88 }); 89 }); 90 91 describe('test for ProjectWhiteListManager', function () { 92 let cachePath = 'test/ut/utils/obfuscation'; 93 describe('test for constructor', function () { 94 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, false, true); 95 const fileWhiteLists = projectWhiteListManager.fileWhiteListInfo; 96 expect(fileWhiteLists).to.be.undefined; 97 expect(projectWhiteListManager.getEnableAtKeep()).to.be.true; 98 expect(projectWhiteListManager.getFileWhiteListsCachePath()).to.be.equal('test/ut/utils/obfuscation/fileWhiteLists.json'); 99 expect(projectWhiteListManager.getProjectWhiteListCachePath()).to.be.equal('test/ut/utils/obfuscation/projectWhiteList.json'); 100 expect(projectWhiteListManager.getIsIncremental()).to.be.false; 101 expect(projectWhiteListManager.getFileWhiteListMap().size).to.be.equal(0); 102 }); 103 describe('test for createFileWhiteList', function () { 104 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, false, false); 105 const fileWhiteLists = projectWhiteListManager.createFileWhiteList(); 106 expect(fileWhiteLists).to.not.be.undefined; 107 expect(fileWhiteLists.fileKeepInfo.keepSymbol?.globalNames).to.be.undefined; 108 expect(fileWhiteLists.fileKeepInfo.keepSymbol?.propertyNames).to.be.undefined; 109 expect(fileWhiteLists.fileKeepInfo.keepAsConsumer?.globalNames).to.be.undefined; 110 expect(fileWhiteLists.fileKeepInfo.keepAsConsumer?.propertyNames).to.be.undefined; 111 expect(fileWhiteLists.fileKeepInfo.structProperties.size).to.be.equal(0); 112 expect(fileWhiteLists.fileKeepInfo.exported?.propertyNames.size).to.be.equal(0); 113 expect(fileWhiteLists.fileKeepInfo.exported?.globalNames.size).to.be.equal(0); 114 expect(fileWhiteLists.fileKeepInfo.enumProperties.size).to.be.equal(0); 115 expect(fileWhiteLists.fileKeepInfo.stringProperties.size).to.be.equal(0); 116 expect(fileWhiteLists.fileKeepInfo.arkUIKeepInfo?.propertyNames.size).to.be.equal(0); 117 expect(fileWhiteLists.fileKeepInfo.arkUIKeepInfo?.globalNames.size).to.be.equal(0); 118 expect(fileWhiteLists.fileReservedInfo.enumProperties.size).to.be.equal(0); 119 expect(fileWhiteLists.fileReservedInfo.propertyParams.size).to.be.equal(0); 120 }); 121 122 describe('test for collect FileWhiteLists', function () { 123 it('should update fileWhiteList if already exists', () => { 124 let projectWhiteListManagerForTest = new ProjectWhiteListManager(cachePath, false, true); 125 projectWhiteListManagerForTest.setCurrentCollector('testPath1'); 126 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.structProperties.add('test1'); 127 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.enumProperties.add('test2'); 128 const fileWhilteList: FileWhiteList | undefined = projectWhiteListManagerForTest.getFileWhiteListMap().get('testPath1'); 129 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test1')).to.be.true; 130 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test2')).to.be.true; 131 expect(projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.structProperties.size).to.be.equal(1); 132 expect(projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.enumProperties.size).to.be.equal(1); 133 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.structProperties.add('test3'); 134 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.enumProperties.add('test4'); 135 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test1')).to.be.true; 136 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test2')).to.be.true; 137 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test3')).to.be.true; 138 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test4')).to.be.true; 139 }); 140 it('should create new fileWhiteList if not exists', () => { 141 let projectWhiteListManagerForTest = new ProjectWhiteListManager(cachePath, false, true); 142 projectWhiteListManagerForTest.setCurrentCollector('testPath2'); 143 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.structProperties.add("test3"); 144 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.enumProperties.add("test4"); 145 const fileWhilteList: FileWhiteList | undefined = projectWhiteListManagerForTest.getFileWhiteListMap().get('testPath2'); 146 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test1')).to.be.false; 147 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test2')).to.be.false; 148 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test3')).to.be.true; 149 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test4')).to.be.true; 150 }); 151 it('should collect atKeep if is enabled', () => { 152 let projectWhiteListManagerForTest = new ProjectWhiteListManager(cachePath, false, true); 153 projectWhiteListManagerForTest.setCurrentCollector('testPath3'); 154 let fileWhilteListTemp: FileWhiteList = projectWhiteListManagerForTest.fileWhiteListInfo; 155 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 156 fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.add('test2'); 157 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.add('test3'); 158 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.add('test4'); 159 fileWhilteListTemp.fileKeepInfo.structProperties.add('test5'); 160 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test6'); 161 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test7'); 162 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test8'); 163 fileWhilteListTemp.fileKeepInfo.stringProperties.add('test9'); 164 fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo.globalNames.add('test111'); 165 fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo.propertyNames.add('test112'); 166 fileWhilteListTemp.fileReservedInfo.enumProperties.add('test10'); 167 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test11'); 168 const fileWhilteList: FileWhiteList | undefined = projectWhiteListManagerForTest.getFileWhiteListMap().get('testPath3'); 169 expect(fileWhilteList?.fileKeepInfo.keepSymbol?.globalNames.has('test1')).to.be.true; 170 expect(fileWhilteList?.fileKeepInfo.keepSymbol?.propertyNames.has('test2')).to.be.true; 171 expect(fileWhilteList?.fileKeepInfo.keepAsConsumer?.globalNames.has('test3')).to.be.true; 172 expect(fileWhilteList?.fileKeepInfo.keepAsConsumer?.propertyNames.has('test4')).to.be.true; 173 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test5')).to.be.true; 174 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test6')).to.be.true; 175 expect(fileWhilteList?.fileKeepInfo.exported.globalNames.has('test7')).to.be.true; 176 expect(fileWhilteList?.fileKeepInfo.exported.propertyNames.has('test8')).to.be.true; 177 expect(fileWhilteList?.fileKeepInfo.stringProperties.has('test9')).to.be.true; 178 expect(fileWhilteList?.fileKeepInfo.arkUIKeepInfo.globalNames.has('test111')).to.be.true; 179 expect(fileWhilteList?.fileKeepInfo.arkUIKeepInfo.propertyNames.has('test112')).to.be.true; 180 expect(fileWhilteList?.fileReservedInfo.enumProperties.has('test10')).to.be.true; 181 expect(fileWhilteList?.fileReservedInfo.propertyParams.has('test11')).to.be.true; 182 expect(fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.size).to.be.equal(1); 183 expect(fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.size).to.be.equal(1); 184 expect(fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.size).to.be.equal(1); 185 expect(fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.size).to.be.equal(1); 186 expect(fileWhilteListTemp.fileKeepInfo.structProperties.size).to.be.equal(1); 187 expect(fileWhilteListTemp.fileKeepInfo.enumProperties.size).to.be.equal(1); 188 expect(fileWhilteListTemp.fileKeepInfo.exported.globalNames.size).to.be.equal(1); 189 expect(fileWhilteListTemp.fileKeepInfo.exported.propertyNames.size).to.be.equal(1); 190 expect(fileWhilteListTemp.fileKeepInfo.stringProperties.size).to.be.equal(1); 191 expect(fileWhilteListTemp.fileReservedInfo.enumProperties.size).to.be.equal(1); 192 expect(fileWhilteListTemp.fileReservedInfo.propertyParams.size).to.be.equal(1); 193 expect(fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo.globalNames.size).to.be.equal(1); 194 expect(fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo.propertyNames.size).to.be.equal(1); 195 }); 196 it('should not collect atKeep if not enabled', () => { 197 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, false, false); 198 projectWhiteListManager.setCurrentCollector('testPath4') 199 let fileWhilteListTemp: FileWhiteList = projectWhiteListManager.fileWhiteListInfo; 200 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 201 fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.add('test2'); 202 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.add('test3'); 203 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.add('test4'); 204 fileWhilteListTemp.fileKeepInfo.structProperties.add('test5'); 205 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test6'); 206 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test7'); 207 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test8'); 208 fileWhilteListTemp.fileKeepInfo.stringProperties.add('test9'); 209 fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo.globalNames.add('test111'); 210 fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo.propertyNames.add('test112'); 211 fileWhilteListTemp.fileReservedInfo.enumProperties.add('test10'); 212 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test11'); 213 const fileWhilteList: FileWhiteList | undefined = projectWhiteListManager.getFileWhiteListMap().get('testPath4'); 214 expect(fileWhilteList?.fileKeepInfo.keepSymbol?.globalNames).to.be.undefined; 215 expect(fileWhilteList?.fileKeepInfo.keepSymbol?.propertyNames).to.be.undefined; 216 expect(fileWhilteList?.fileKeepInfo.keepAsConsumer?.globalNames).to.be.undefined; 217 expect(fileWhilteList?.fileKeepInfo.keepAsConsumer?.propertyNames).to.be.undefined; 218 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test5')).to.be.true; 219 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test6')).to.be.true; 220 expect(fileWhilteList?.fileKeepInfo.exported.globalNames.has('test7')).to.be.true; 221 expect(fileWhilteList?.fileKeepInfo.exported.propertyNames.has('test8')).to.be.true; 222 expect(fileWhilteList?.fileKeepInfo.stringProperties.has('test9')).to.be.true; 223 expect(fileWhilteList?.fileKeepInfo.arkUIKeepInfo.globalNames.has('test111')).to.be.true; 224 expect(fileWhilteList?.fileKeepInfo.arkUIKeepInfo.propertyNames.has('test112')).to.be.true; 225 expect(fileWhilteList?.fileReservedInfo.enumProperties.has('test10')).to.be.true; 226 expect(fileWhilteList?.fileReservedInfo.propertyParams.has('test11')).to.be.true; 227 }); 228 }); 229 230 describe('test for createOrUpdateWhiteListCaches', function () { 231 beforeEach(() => { 232 UnobfuscationCollections.clear(); 233 ApiExtractor.mConstructorPropertySet.clear(); 234 ApiExtractor.mEnumMemberSet.clear(); 235 }) 236 it('should write fileWhiteLists and projectWhiteList if is not incremental', () => { 237 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, false, true); 238 projectWhiteListManager.setCurrentCollector('testPath1'); 239 let fileWhilteListTemp: FileWhiteList = projectWhiteListManager.fileWhiteListInfo; 240 fileWhilteListTemp.fileKeepInfo.structProperties.add('test01'); 241 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test02'); 242 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 243 fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.add('test2'); 244 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.add('test3'); 245 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.add('test4'); 246 fileWhilteListTemp.fileKeepInfo.structProperties.add('test5'); 247 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test6'); 248 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test7'); 249 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test8'); 250 fileWhilteListTemp.fileKeepInfo.stringProperties.add('test9'); 251 fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo?.globalNames.add('test111'); 252 fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo?.propertyNames.add('test112'); 253 fileWhilteListTemp.fileReservedInfo.enumProperties.add('test10'); 254 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test11'); 255 projectWhiteListManager.setCurrentCollector('testPath2'); 256 fileWhilteListTemp = projectWhiteListManager.fileWhiteListInfo; 257 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test12'); 258 projectWhiteListManager.setCurrentCollector('testPath3'); 259 fileWhilteListTemp = projectWhiteListManager.fileWhiteListInfo; 260 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test13'); 261 projectWhiteListManager.createOrUpdateWhiteListCaches(); 262 let fileWhiteLists = path.join(__dirname,'obfuscation/fileWhiteLists.json'); 263 let fileWhiteLists_expected = path.join(__dirname,'obfuscation/fileWhiteLists_expected01.json'); 264 let projectWhiteLists = path.join(__dirname,'obfuscation/projectWhiteList.json'); 265 let projectWhiteLists_expected = path.join(__dirname,'obfuscation/projectWhiteList_expected01.json'); 266 expect(compareFiles(fileWhiteLists, fileWhiteLists_expected)).to.be.true; 267 expect(compareFiles(projectWhiteLists, projectWhiteLists_expected)).to.be.true; 268 expect(AtKeepCollections.keepSymbol.globalNames.size==0).to.be.true; 269 expect(AtKeepCollections.keepSymbol.propertyNames.size==0).to.be.true; 270 expect(AtKeepCollections.keepAsConsumer.globalNames.size==0).to.be.true; 271 expect(AtKeepCollections.keepAsConsumer.propertyNames.size==0).to.be.true; 272 expect(UnobfuscationCollections.reservedStruct.size==0).to.be.true; 273 expect(UnobfuscationCollections.reservedEnum.size==0).to.be.true; 274 expect(UnobfuscationCollections.reservedExportName.size==0).to.be.true; 275 expect(UnobfuscationCollections.reservedExportNameAndProp.size==0).to.be.true; 276 expect(UnobfuscationCollections.reservedStrProp.size==0).to.be.true; 277 expect(ApiExtractor.mConstructorPropertySet.size==0).to.be.true; 278 expect(ApiExtractor.mEnumMemberSet.size==0).to.be.true; 279 expect(AtIntentCollections.globalNames.size==1).to.be.true; 280 expect(AtIntentCollections.propertyNames.size==1).to.be.true; 281 expect(AtIntentCollections.globalNames.has('test111')).to.be.true; 282 expect(AtIntentCollections.propertyNames.has('test112')).to.be.true; 283 }); 284 it('should update fileWhiteLists and projectWhiteList if is incremental(project white list changed)', () => { 285 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, true, true); 286 projectWhiteListManager.setCurrentCollector('testPath4'); 287 let fileWhilteListTemp: FileWhiteList = projectWhiteListManager.fileWhiteListInfo; 288 fileWhilteListTemp.fileKeepInfo.structProperties.add('test01'); 289 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test02'); 290 projectWhiteListManager.setCurrentCollector('testPath4'); 291 fileWhilteListTemp = projectWhiteListManager.fileWhiteListInfo; 292 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 293 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test32'); 294 fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.add('test2'); 295 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.add('test3'); 296 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.add('test4'); 297 fileWhilteListTemp.fileKeepInfo.structProperties.add('test5'); 298 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test6'); 299 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test7'); 300 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test8'); 301 fileWhilteListTemp.fileKeepInfo.stringProperties.add('test9'); 302 fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo.globalNames.add('test122'); 303 fileWhilteListTemp.fileKeepInfo.arkUIKeepInfo.propertyNames.add('test123'); 304 fileWhilteListTemp.fileReservedInfo.enumProperties.add('test10'); 305 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test11'); 306 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test31'); 307 projectWhiteListManager.setCurrentCollector('testPath3'); 308 fileWhilteListTemp = projectWhiteListManager.fileWhiteListInfo; 309 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test33'); 310 const deletedFilesSet: Set<string> = new Set(['testPath2', 'testPathNotExists']); 311 projectWhiteListManager.createOrUpdateWhiteListCaches(deletedFilesSet); 312 let fileWhiteLists = path.join(__dirname,'obfuscation/fileWhiteLists.json'); 313 let fileWhiteLists_expected = path.join(__dirname,'obfuscation/fileWhiteLists_expected02.json'); 314 let projectWhiteLists = path.join(__dirname,'obfuscation/projectWhiteList.json'); 315 let projectWhiteLists_expected = path.join(__dirname,'obfuscation/projectWhiteList_expected02.json'); 316 expect(compareFiles(fileWhiteLists, fileWhiteLists_expected)).to.be.true; 317 expect(compareFiles(projectWhiteLists, projectWhiteLists_expected)).to.be.true; 318 expect(projectWhiteListManager.getShouldReObfuscate()).to.be.true; 319 expect(AtKeepCollections.keepSymbol.globalNames.has('test1')).to.be.true; 320 expect(AtKeepCollections.keepSymbol.propertyNames.has('test2')).to.be.true; 321 expect(AtKeepCollections.keepAsConsumer.globalNames.has('test3')).to.be.true; 322 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('test4')).to.be.true; 323 expect(AtKeepCollections.keepSymbol.globalNames.has('test32')).to.be.true; 324 expect(UnobfuscationCollections.reservedStruct.has('test01')).to.be.true; 325 expect(UnobfuscationCollections.reservedEnum.has('test02')).to.be.true; 326 expect(UnobfuscationCollections.reservedStruct.has('test5')).to.be.true; 327 expect(UnobfuscationCollections.reservedEnum.has('test6')).to.be.true; 328 expect(UnobfuscationCollections.reservedExportName.has('test7')).to.be.true; 329 expect(UnobfuscationCollections.reservedExportNameAndProp.has('test8')).to.be.true; 330 expect(UnobfuscationCollections.reservedStrProp.has('test9')).to.be.true; 331 expect(ApiExtractor.mEnumMemberSet.has('test10')).to.be.true; 332 expect(ApiExtractor.mConstructorPropertySet.has('test11')).to.be.true; 333 expect(ApiExtractor.mConstructorPropertySet.has('test31')).to.be.true; 334 expect(UnobfuscationCollections.reservedExportName.has('test33')).to.be.true; 335 expect(UnobfuscationCollections.reservedExportNameAndProp.has('test12')).to.be.false; 336 expect(AtIntentCollections.globalNames.has('test122')).to.be.true; 337 expect(AtIntentCollections.propertyNames.has('test123')).to.be.true; 338 }); 339 it('should update fileWhiteLists and projectWhiteList if is incremental(project white list not changed)', () => { 340 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, true, true); 341 projectWhiteListManager.setCurrentCollector('testPath5'); 342 let fileWhilteListTemp: FileWhiteList | undefined = projectWhiteListManager.fileWhiteListInfo; 343 fileWhilteListTemp.fileKeepInfo.structProperties.add('test01'); 344 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test02'); 345 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 346 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test32'); 347 projectWhiteListManager.createOrUpdateWhiteListCaches(); 348 let fileWhiteLists = path.join(__dirname,'obfuscation/fileWhiteLists.json'); 349 let fileWhiteLists_expected = path.join(__dirname,'obfuscation/fileWhiteLists_expected03.json'); 350 let projectWhiteLists = path.join(__dirname,'obfuscation/projectWhiteList.json'); 351 let projectWhiteLists_expected = path.join(__dirname,'obfuscation/projectWhiteList_expected02.json'); 352 expect(compareFiles(fileWhiteLists, fileWhiteLists_expected)).to.be.true; 353 expect(compareFiles(projectWhiteLists, projectWhiteLists_expected)).to.be.true; 354 expect(projectWhiteListManager.getShouldReObfuscate()).to.be.false; 355 }); 356 }); 357 358 describe('test for createProjectWhiteList when bytecodeObfuscate enable', function () { 359 it('should add property decorated to projectWhiteList when bytecodeObfuscate enable)', () => { 360 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, false, false); 361 projectWhiteListManager.setCurrentCollector("testPath5") 362 const fileWhiteLists = projectWhiteListManager.createFileWhiteList(); 363 fileWhiteLists.bytecodeObfuscateKeepInfo = { 364 "decoratorMap": { 365 "Track": ["prop1"] 366 } 367 }; 368 projectWhiteListManager.getFileWhiteListMap().set("testPath5", fileWhiteLists); 369 projectWhiteListManager.createOrUpdateWhiteListCaches(); 370 const cacheContent = fs.readFileSync(projectWhiteListManager.getProjectWhiteListCachePath(), 'utf-8'); 371 const parsedCache = JSON.parse(cacheContent); 372 expect(parsedCache.projectKeepInfo.globalNames).to.include("prop1"); 373 }); 374 }) 375 }) 376 377 describe('test for FilePathManager', function () { 378 let cachePath = 'test/ut/utils/obfuscation'; 379 describe('test for constructor', function () { 380 it('should be initialized correctly', () => { 381 const filePathManager = new FilePathManager(cachePath); 382 expect(filePathManager.getFilePathsCache()).to.be.equal('test/ut/utils/obfuscation/sourceFilePaths.cache'); 383 expect(filePathManager.getSourceFilePaths().size).to.be.equal(0); 384 expect(filePathManager.getDeletedSourceFilePaths().size).to.be.equal(0); 385 expect(filePathManager.getAddedSourceFilePaths().size).to.be.equal(0); 386 }); 387 }); 388 389 describe('test for createOrUpdateSourceFilePaths', function () { 390 let testFilePath1: string; 391 let testFilePath2: string; 392 let testFilePath3: string; 393 let testFilePath4: string; 394 let testFilePath5: string; 395 396 before(()=>{ 397 FileUtils.deleteFolderRecursive('test/ut/utils/testData'); 398 testFilePath1 = 'test/ut/utils/testData/filePath1.ts'; 399 testFilePath2 = 'test/ut/utils/testData/filePath2.ts'; 400 testFilePath3 = 'test/ut/utils/testData/filePath3.ts'; 401 testFilePath4 = 'test/ut/utils/testData/filePath4.ts'; 402 testFilePath5 = 'test/ut/utils/testData/filePath5.json'; 403 FileUtils.createDirectory('test/ut/utils/testData'); 404 fs.writeFileSync(testFilePath1, 'test'); 405 fs.writeFileSync(testFilePath2, 'test'); 406 fs.writeFileSync(testFilePath3, 'test'); 407 fs.writeFileSync(testFilePath4, 'test'); 408 409 FileUtils.deleteFile(path.join(__dirname,'obfuscation/sourceFilePaths.cache')); 410 }) 411 412 it('should writeSourceFilePaths when is not incremental', () => { 413 const filePathManager = new FilePathManager(cachePath); 414 const filePathsSet: Set<string> = new Set(); 415 filePathsSet.add(testFilePath1); 416 filePathsSet.add(testFilePath2); 417 filePathsSet.add(testFilePath3); 418 filePathsSet.add(testFilePath5); 419 filePathManager.createOrUpdateSourceFilePaths(filePathsSet); 420 let sourceFilePaths = path.join(__dirname,'obfuscation/sourceFilePaths.cache'); 421 let sourceFilePaths_expected = path.join(__dirname,'obfuscation/sourceFilePaths_expected01.txt'); 422 expect(compareFiles(sourceFilePaths, sourceFilePaths_expected)).to.be.true; 423 }); 424 it('should update SourceFilePaths correctly when is incremental', () => { 425 const filePathManager = new FilePathManager(cachePath); 426 const filePathsSet: Set<string> = new Set(); 427 filePathsSet.add(testFilePath1); 428 filePathsSet.add(testFilePath3); 429 filePathsSet.add(testFilePath4); 430 filePathManager.createOrUpdateSourceFilePaths(filePathsSet); 431 let sourceFilePaths = path.join(__dirname,'obfuscation/sourceFilePaths.cache'); 432 let sourceFilePaths_expected = path.join(__dirname,'obfuscation/sourceFilePaths_expected02.txt'); 433 expect(compareFiles(sourceFilePaths, sourceFilePaths_expected)).to.be.true; 434 expect(filePathManager.getAddedSourceFilePaths().has(testFilePath4)).to.be.true; 435 expect(filePathManager.getDeletedSourceFilePaths().has(testFilePath2)).to.be.true; 436 }); 437 }); 438 }); 439 440 describe('test for FileContentManager', function () { 441 let cachePath = 'test/ut/utils/obfuscation'; 442 describe('test for constructor', function () { 443 it('should be initialized correctly', () => { 444 FileUtils.deleteFolderRecursive('test/ut/utils/obfuscation/transformed'); 445 const fileContentManager = new FileContentManager(cachePath, false); 446 const transformedPath = 'test/ut/utils/obfuscation/transformed'; 447 const fileNamesMapPath = 'test/ut/utils/obfuscation/transformed/transformedFileNamesMap.json'; 448 expect(fileContentManager.getTransformedFilesDir()).to.be.equal(transformedPath); 449 expect(fileContentManager.getFileNamesMapPath()).to.be.equal(fileNamesMapPath); 450 expect(fileContentManager.fileNamesMap.size).to.be.equal(0); 451 expect(fileContentManager.getIsIncremental()).to.be.equal(false); 452 expect(fs.existsSync(transformedPath)).to.be.equal(true); 453 }); 454 }); 455 describe('test for updateFileContent', function () { 456 const fileContent01: FileContent = { 457 moduleInfo: { 458 content: "let a = 1;\n", 459 buildFilePath: "D:\\WORK\\demo\\libraryHar\\build\\default\\cache\\default\\default@HarCompileArkTS\\esmodule\\release\\libraryHar\\src\\main\\ets\\components\\test.ts", 460 relativeSourceFilePath: "libraryHar/src/main/ets/components/test.ets", 461 originSourceFilePath: "D:/WORK/demo/libraryHar/src/main/ets/components/test.ets", 462 rollupModuleId: "D:\\WORK\\demo\\libraryHar\\src\\main\\ets\\components\\test.ets" 463 }, 464 previousStageSourceMap: { 465 version: 3, 466 file: "test.ets", 467 sourceRoot: "", 468 sources: [ 469 "libraryHar/src/main/ets/components/test.ets" 470 ], 471 names: [], 472 mappings: "AAAA,IAAI,CAAC,GAAG,CAAC,CAAC" 473 } 474 }; 475 const fileContent01_updated: FileContent = { 476 moduleInfo: { 477 content: "let a1 = \"1\";\n", 478 buildFilePath: "D:\\WORK\\demo\\libraryHar\\build\\default\\cache\\default\\default@HarCompileArkTS\\esmodule\\release\\libraryHar\\src\\main\\ets\\components\\test.ts", 479 relativeSourceFilePath: "libraryHar/src/main/ets/components/test.ets", 480 originSourceFilePath: "D:/WORK/demo/libraryHar/src/main/ets/components/test.ets", 481 rollupModuleId: "D:\\WORK\\demo\\libraryHar\\src\\main\\ets\\components\\test.ets" 482 }, 483 previousStageSourceMap: { 484 version: 3, 485 file: "test.ets", 486 sourceRoot: "", 487 sources: [ 488 "libraryHar/src/main/ets/components/test.ets" 489 ], 490 names: [], 491 mappings: "AAAA,IAAI,CAAC,GAAG,CAAC,CAAC" 492 } 493 }; 494 const fileContent02: FileContent = { 495 moduleInfo: { 496 content: "let a = 2;\n", 497 buildFilePath: "D:\\WORK\\demo\\libraryHar\\build\\default\\cache\\default\\default@HarCompileArkTS\\esmodule\\release\\libraryHar\\src\\main\\ets\\components\\test2.ts", 498 relativeSourceFilePath: "libraryHar/src/main/ets/components/test2.ets", 499 originSourceFilePath: "D:/WORK/demo/libraryHar/src/main/ets/components/test2.ets", 500 rollupModuleId: "D:\\WORK\\demo\\libraryHar\\src\\main\\ets\\components\\test2.ets" 501 }, 502 previousStageSourceMap: { 503 version: 3, 504 file: "test2.ets", 505 sourceRoot: "", 506 sources: [ 507 "libraryHar/src/main/ets/components/test2.ets" 508 ], 509 names: [], 510 mappings: "AAAA,IAAI,CAAC,GAAG,CAAC,CAAC" 511 } 512 }; 513 const fileContent03: FileContent = { 514 moduleInfo: { 515 content: "let a = 3;\n", 516 buildFilePath: "D:\\WORK\\demo\\libraryHar\\build\\default\\cache\\default\\default@HarCompileArkTS\\esmodule\\release\\libraryHar\\src\\main\\ets\\components\\test3.ts", 517 relativeSourceFilePath: "libraryHar/src/main/ets/components/test3.ets", 518 originSourceFilePath: "D:/WORK/demo/libraryHar/src/main/ets/components/test3.ets", 519 rollupModuleId: "D:\\WORK\\demo\\libraryHar\\src\\main\\ets\\components\\test3.ets" 520 }, 521 previousStageSourceMap: { 522 version: 3, 523 file: "test3.ets", 524 sourceRoot: "", 525 sources: [ 526 "libraryHar/src/main/ets/components/test3.ets" 527 ], 528 names: [], 529 mappings: "AAAA,IAAI,CAAC,GAAG,CAAC,CAAC" 530 } 531 }; 532 it('should writeFileContent when full compilation', () => { 533 const fileContentManager = new FileContentManager(cachePath, false); 534 fileContentManager.updateFileContent(fileContent01); 535 fileContentManager.updateFileContent(fileContent02); 536 const transformedFilePath01: string | undefined= fileContentManager.fileNamesMap.get(fileContent01.moduleInfo.originSourceFilePath!); 537 const transformedFilePath02: string | undefined= fileContentManager.fileNamesMap.get(fileContent02.moduleInfo.originSourceFilePath!); 538 const reservedFileContent01 = fileContentManager.readFileContent(transformedFilePath01!); 539 const reservedFileContent02 = fileContentManager.readFileContent(transformedFilePath02!); 540 fileContentManager.writeFileNamesMap(); 541 expect(reservedFileContent01).to.be.deep.equal(fileContent01); 542 expect(reservedFileContent02).to.be.deep.equal(fileContent02); 543 }); 544 it('should updateFileContent when incremental compilation', () => { 545 const fileContentManager = new FileContentManager(cachePath, true); 546 // before update 547 fileContentManager.readFileNamesMap(); 548 const transformedFilePath01_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent01.moduleInfo.originSourceFilePath!); 549 const transformedFilePath02_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent02.moduleInfo.originSourceFilePath!); 550 const transformedFilePath03_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent03.moduleInfo.originSourceFilePath!); 551 const reservedFileContent01_before = fileContentManager.readFileContent(transformedFilePath01_before!); 552 const reservedFileContent02_before = fileContentManager.readFileContent(transformedFilePath02_before!); 553 expect(transformedFilePath03_before).to.be.undefined; 554 expect(reservedFileContent01_before).to.be.deep.equal(fileContent01); 555 expect(reservedFileContent02_before).to.be.deep.equal(fileContent02); 556 557 // after update 558 const deletedFilePath: Set<string> = new Set(); 559 deletedFilePath.add(fileContent02.moduleInfo.originSourceFilePath!); 560 deletedFilePath.add('pathNotExists'); 561 fileContentManager.deleteFileContent(deletedFilePath); 562 fileContentManager.updateFileContent(fileContent01_updated); 563 fileContentManager.updateFileContent(fileContent03); 564 const transformedFilePath01: string | undefined= fileContentManager.fileNamesMap.get(fileContent01.moduleInfo.originSourceFilePath!); 565 const transformedFilePath02: string | undefined= fileContentManager.fileNamesMap.get(fileContent02.moduleInfo.originSourceFilePath!); 566 const transformedFilePath03: string | undefined= fileContentManager.fileNamesMap.get(fileContent03.moduleInfo.originSourceFilePath!); 567 expect(transformedFilePath02).to.be.undefined; 568 const reservedFileContent01 = fileContentManager.readFileContent(transformedFilePath01!); 569 const reservedFileContent03 = fileContentManager.readFileContent(transformedFilePath03!); 570 expect(reservedFileContent01).to.be.deep.equal(fileContent01_updated); 571 expect(reservedFileContent03).to.be.deep.equal(fileContent03); 572 }); 573 it('should sort fileNamesMap correctly', () => { 574 const fileContentManager = new FileContentManager(cachePath, false); 575 const fileNamesMap = fileContentManager.fileNamesMap; 576 fileNamesMap.set('file4','test'); 577 fileNamesMap.set('file3','test'); 578 fileNamesMap.set('file5','test'); 579 const sortedNames: string[] = fileContentManager.getSortedFiles(); 580 expect(sortedNames[0]).to.be.equal('file3'); 581 expect(sortedNames[1]).to.be.equal('file4'); 582 expect(sortedNames[2]).to.be.equal('file5'); 583 }); 584 }); 585 }); 586})