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 { 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.fileReservedInfo.enumProperties.size).to.be.equal(0); 117 expect(fileWhiteLists.fileReservedInfo.propertyParams.size).to.be.equal(0); 118 }); 119 120 describe('test for collect FileWhiteLists', function () { 121 it('should update fileWhiteList if already exists', () => { 122 let projectWhiteListManagerForTest = new ProjectWhiteListManager(cachePath, false, true); 123 projectWhiteListManagerForTest.setCurrentCollector('testPath1'); 124 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.structProperties.add('test1'); 125 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.enumProperties.add('test2'); 126 const fileWhilteList: FileWhiteList | undefined = projectWhiteListManagerForTest.getFileWhiteListMap().get('testPath1'); 127 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test1')).to.be.true; 128 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test2')).to.be.true; 129 expect(projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.structProperties.size).to.be.equal(1); 130 expect(projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.enumProperties.size).to.be.equal(1); 131 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.structProperties.add('test3'); 132 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.enumProperties.add('test4'); 133 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test1')).to.be.true; 134 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test2')).to.be.true; 135 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test3')).to.be.true; 136 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test4')).to.be.true; 137 }); 138 it('should create new fileWhiteList if not exists', () => { 139 let projectWhiteListManagerForTest = new ProjectWhiteListManager(cachePath, false, true); 140 projectWhiteListManagerForTest.setCurrentCollector('testPath2'); 141 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.structProperties.add("test3"); 142 projectWhiteListManagerForTest.fileWhiteListInfo.fileKeepInfo.enumProperties.add("test4"); 143 const fileWhilteList: FileWhiteList | undefined = projectWhiteListManagerForTest.getFileWhiteListMap().get('testPath2'); 144 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test1')).to.be.false; 145 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test2')).to.be.false; 146 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test3')).to.be.true; 147 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test4')).to.be.true; 148 }); 149 it('should collect atKeep if is enabled', () => { 150 let projectWhiteListManagerForTest = new ProjectWhiteListManager(cachePath, false, true); 151 projectWhiteListManagerForTest.setCurrentCollector('testPath3'); 152 let fileWhilteListTemp: FileWhiteList = projectWhiteListManagerForTest.fileWhiteListInfo; 153 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 154 fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.add('test2'); 155 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.add('test3'); 156 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.add('test4'); 157 fileWhilteListTemp.fileKeepInfo.structProperties.add('test5'); 158 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test6'); 159 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test7'); 160 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test8'); 161 fileWhilteListTemp.fileKeepInfo.stringProperties.add('test9'); 162 fileWhilteListTemp.fileReservedInfo.enumProperties.add('test10'); 163 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test11'); 164 const fileWhilteList: FileWhiteList | undefined = projectWhiteListManagerForTest.getFileWhiteListMap().get('testPath3'); 165 expect(fileWhilteList?.fileKeepInfo.keepSymbol?.globalNames.has('test1')).to.be.true; 166 expect(fileWhilteList?.fileKeepInfo.keepSymbol?.propertyNames.has('test2')).to.be.true; 167 expect(fileWhilteList?.fileKeepInfo.keepAsConsumer?.globalNames.has('test3')).to.be.true; 168 expect(fileWhilteList?.fileKeepInfo.keepAsConsumer?.propertyNames.has('test4')).to.be.true; 169 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test5')).to.be.true; 170 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test6')).to.be.true; 171 expect(fileWhilteList?.fileKeepInfo.exported.globalNames.has('test7')).to.be.true; 172 expect(fileWhilteList?.fileKeepInfo.exported.propertyNames.has('test8')).to.be.true; 173 expect(fileWhilteList?.fileKeepInfo.stringProperties.has('test9')).to.be.true; 174 expect(fileWhilteList?.fileReservedInfo.enumProperties.has('test10')).to.be.true; 175 expect(fileWhilteList?.fileReservedInfo.propertyParams.has('test11')).to.be.true; 176 expect(fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.size).to.be.equal(1); 177 expect(fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.size).to.be.equal(1); 178 expect(fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.size).to.be.equal(1); 179 expect(fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.size).to.be.equal(1); 180 expect(fileWhilteListTemp.fileKeepInfo.structProperties.size).to.be.equal(1); 181 expect(fileWhilteListTemp.fileKeepInfo.enumProperties.size).to.be.equal(1); 182 expect(fileWhilteListTemp.fileKeepInfo.exported.globalNames.size).to.be.equal(1); 183 expect(fileWhilteListTemp.fileKeepInfo.exported.propertyNames.size).to.be.equal(1); 184 expect(fileWhilteListTemp.fileKeepInfo.stringProperties.size).to.be.equal(1); 185 expect(fileWhilteListTemp.fileReservedInfo.enumProperties.size).to.be.equal(1); 186 expect(fileWhilteListTemp.fileReservedInfo.propertyParams.size).to.be.equal(1); 187 }); 188 it('should not collect atKeep if not enabled', () => { 189 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, false, false); 190 projectWhiteListManager.setCurrentCollector('testPath4') 191 let fileWhilteListTemp: FileWhiteList = projectWhiteListManager.fileWhiteListInfo; 192 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 193 fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.add('test2'); 194 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.add('test3'); 195 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.add('test4'); 196 fileWhilteListTemp.fileKeepInfo.structProperties.add('test5'); 197 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test6'); 198 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test7'); 199 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test8'); 200 fileWhilteListTemp.fileKeepInfo.stringProperties.add('test9'); 201 fileWhilteListTemp.fileReservedInfo.enumProperties.add('test10'); 202 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test11'); 203 const fileWhilteList: FileWhiteList | undefined = projectWhiteListManager.getFileWhiteListMap().get('testPath4'); 204 expect(fileWhilteList?.fileKeepInfo.keepSymbol?.globalNames).to.be.undefined; 205 expect(fileWhilteList?.fileKeepInfo.keepSymbol?.propertyNames).to.be.undefined; 206 expect(fileWhilteList?.fileKeepInfo.keepAsConsumer?.globalNames).to.be.undefined; 207 expect(fileWhilteList?.fileKeepInfo.keepAsConsumer?.propertyNames).to.be.undefined; 208 expect(fileWhilteList?.fileKeepInfo.structProperties.has('test5')).to.be.true; 209 expect(fileWhilteList?.fileKeepInfo.enumProperties.has('test6')).to.be.true; 210 expect(fileWhilteList?.fileKeepInfo.exported.globalNames.has('test7')).to.be.true; 211 expect(fileWhilteList?.fileKeepInfo.exported.propertyNames.has('test8')).to.be.true; 212 expect(fileWhilteList?.fileKeepInfo.stringProperties.has('test9')).to.be.true; 213 expect(fileWhilteList?.fileReservedInfo.enumProperties.has('test10')).to.be.true; 214 expect(fileWhilteList?.fileReservedInfo.propertyParams.has('test11')).to.be.true; 215 }); 216 }); 217 218 describe('test for createOrUpdateWhiteListCaches', function () { 219 beforeEach(() => { 220 UnobfuscationCollections.clear(); 221 ApiExtractor.mConstructorPropertySet.clear(); 222 ApiExtractor.mEnumMemberSet.clear(); 223 }) 224 it('should write fileWhiteLists and projectWhiteList if is not incremental', () => { 225 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, false, true); 226 projectWhiteListManager.setCurrentCollector('testPath1'); 227 let fileWhilteListTemp: FileWhiteList = projectWhiteListManager.fileWhiteListInfo; 228 fileWhilteListTemp.fileKeepInfo.structProperties.add('test01'); 229 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test02'); 230 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 231 fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.add('test2'); 232 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.add('test3'); 233 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.add('test4'); 234 fileWhilteListTemp.fileKeepInfo.structProperties.add('test5'); 235 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test6'); 236 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test7'); 237 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test8'); 238 fileWhilteListTemp.fileKeepInfo.stringProperties.add('test9'); 239 fileWhilteListTemp.fileReservedInfo.enumProperties.add('test10'); 240 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test11'); 241 projectWhiteListManager.setCurrentCollector('testPath2'); 242 fileWhilteListTemp = projectWhiteListManager.fileWhiteListInfo; 243 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test12'); 244 projectWhiteListManager.setCurrentCollector('testPath3'); 245 fileWhilteListTemp = projectWhiteListManager.fileWhiteListInfo; 246 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test13'); 247 projectWhiteListManager.createOrUpdateWhiteListCaches(); 248 let fileWhiteLists = path.join(__dirname,'obfuscation/fileWhiteLists.json'); 249 let fileWhiteLists_expected = path.join(__dirname,'obfuscation/fileWhiteLists_expected01.json'); 250 let projectWhiteLists = path.join(__dirname,'obfuscation/projectWhiteList.json'); 251 let projectWhiteLists_expected = path.join(__dirname,'obfuscation/projectWhiteList_expected01.json'); 252 expect(compareFiles(fileWhiteLists, fileWhiteLists_expected)).to.be.true; 253 expect(compareFiles(projectWhiteLists, projectWhiteLists_expected)).to.be.true; 254 expect(AtKeepCollections.keepSymbol.globalNames.size==0).to.be.true; 255 expect(AtKeepCollections.keepSymbol.propertyNames.size==0).to.be.true; 256 expect(AtKeepCollections.keepAsConsumer.globalNames.size==0).to.be.true; 257 expect(AtKeepCollections.keepAsConsumer.propertyNames.size==0).to.be.true; 258 expect(UnobfuscationCollections.reservedStruct.size==0).to.be.true; 259 expect(UnobfuscationCollections.reservedEnum.size==0).to.be.true; 260 expect(UnobfuscationCollections.reservedExportName.size==0).to.be.true; 261 expect(UnobfuscationCollections.reservedExportNameAndProp.size==0).to.be.true; 262 expect(UnobfuscationCollections.reservedStrProp.size==0).to.be.true; 263 expect(ApiExtractor.mConstructorPropertySet.size==0).to.be.true; 264 expect(ApiExtractor.mEnumMemberSet.size==0).to.be.true; 265 }); 266 it('should update fileWhiteLists and projectWhiteList if is incremental(project white list changed)', () => { 267 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, true, true); 268 projectWhiteListManager.setCurrentCollector('testPath4'); 269 let fileWhilteListTemp: FileWhiteList = projectWhiteListManager.fileWhiteListInfo; 270 fileWhilteListTemp.fileKeepInfo.structProperties.add('test01'); 271 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test02'); 272 projectWhiteListManager.setCurrentCollector('testPath4'); 273 fileWhilteListTemp = projectWhiteListManager.fileWhiteListInfo; 274 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 275 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test32'); 276 fileWhilteListTemp.fileKeepInfo.keepSymbol?.propertyNames.add('test2'); 277 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.globalNames.add('test3'); 278 fileWhilteListTemp.fileKeepInfo.keepAsConsumer?.propertyNames.add('test4'); 279 fileWhilteListTemp.fileKeepInfo.structProperties.add('test5'); 280 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test6'); 281 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test7'); 282 fileWhilteListTemp.fileKeepInfo.exported.propertyNames.add('test8'); 283 fileWhilteListTemp.fileKeepInfo.stringProperties.add('test9'); 284 fileWhilteListTemp.fileReservedInfo.enumProperties.add('test10'); 285 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test11'); 286 fileWhilteListTemp.fileReservedInfo.propertyParams.add('test31'); 287 projectWhiteListManager.setCurrentCollector('testPath3'); 288 fileWhilteListTemp = projectWhiteListManager.fileWhiteListInfo; 289 fileWhilteListTemp.fileKeepInfo.exported.globalNames.add('test33'); 290 const deletedFilesSet: Set<string> = new Set(['testPath2', 'testPathNotExists']); 291 projectWhiteListManager.createOrUpdateWhiteListCaches(deletedFilesSet); 292 let fileWhiteLists = path.join(__dirname,'obfuscation/fileWhiteLists.json'); 293 let fileWhiteLists_expected = path.join(__dirname,'obfuscation/fileWhiteLists_expected02.json'); 294 let projectWhiteLists = path.join(__dirname,'obfuscation/projectWhiteList.json'); 295 let projectWhiteLists_expected = path.join(__dirname,'obfuscation/projectWhiteList_expected02.json'); 296 expect(compareFiles(fileWhiteLists, fileWhiteLists_expected)).to.be.true; 297 expect(compareFiles(projectWhiteLists, projectWhiteLists_expected)).to.be.true; 298 expect(projectWhiteListManager.getShouldReObfuscate()).to.be.true; 299 expect(AtKeepCollections.keepSymbol.globalNames.has('test1')).to.be.true; 300 expect(AtKeepCollections.keepSymbol.propertyNames.has('test2')).to.be.true; 301 expect(AtKeepCollections.keepAsConsumer.globalNames.has('test3')).to.be.true; 302 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('test4')).to.be.true; 303 expect(AtKeepCollections.keepSymbol.globalNames.has('test32')).to.be.true; 304 expect(UnobfuscationCollections.reservedStruct.has('test01')).to.be.true; 305 expect(UnobfuscationCollections.reservedEnum.has('test02')).to.be.true; 306 expect(UnobfuscationCollections.reservedStruct.has('test5')).to.be.true; 307 expect(UnobfuscationCollections.reservedEnum.has('test6')).to.be.true; 308 expect(UnobfuscationCollections.reservedExportName.has('test7')).to.be.true; 309 expect(UnobfuscationCollections.reservedExportNameAndProp.has('test8')).to.be.true; 310 expect(UnobfuscationCollections.reservedStrProp.has('test9')).to.be.true; 311 expect(ApiExtractor.mEnumMemberSet.has('test10')).to.be.true; 312 expect(ApiExtractor.mConstructorPropertySet.has('test11')).to.be.true; 313 expect(ApiExtractor.mConstructorPropertySet.has('test31')).to.be.true; 314 expect(UnobfuscationCollections.reservedExportName.has('test33')).to.be.true; 315 expect(UnobfuscationCollections.reservedExportNameAndProp.has('test12')).to.be.false; 316 }); 317 it('should update fileWhiteLists and projectWhiteList if is incremental(project white list not changed)', () => { 318 let projectWhiteListManager = new ProjectWhiteListManager(cachePath, true, true); 319 projectWhiteListManager.setCurrentCollector('testPath5'); 320 let fileWhilteListTemp: FileWhiteList | undefined = projectWhiteListManager.fileWhiteListInfo; 321 fileWhilteListTemp.fileKeepInfo.structProperties.add('test01'); 322 fileWhilteListTemp.fileKeepInfo.enumProperties.add('test02'); 323 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test1'); 324 fileWhilteListTemp.fileKeepInfo.keepSymbol?.globalNames.add('test32'); 325 projectWhiteListManager.createOrUpdateWhiteListCaches(); 326 let fileWhiteLists = path.join(__dirname,'obfuscation/fileWhiteLists.json'); 327 let fileWhiteLists_expected = path.join(__dirname,'obfuscation/fileWhiteLists_expected03.json'); 328 let projectWhiteLists = path.join(__dirname,'obfuscation/projectWhiteList.json'); 329 let projectWhiteLists_expected = path.join(__dirname,'obfuscation/projectWhiteList_expected02.json'); 330 expect(compareFiles(fileWhiteLists, fileWhiteLists_expected)).to.be.true; 331 expect(compareFiles(projectWhiteLists, projectWhiteLists_expected)).to.be.true; 332 expect(projectWhiteListManager.getShouldReObfuscate()).to.be.false; 333 }); 334 }); 335 }) 336 337 describe('test for FilePathManager', function () { 338 let cachePath = 'test/ut/utils/obfuscation'; 339 describe('test for constructor', function () { 340 it('should be initialized correctly', () => { 341 const filePathManager = new FilePathManager(cachePath); 342 expect(filePathManager.getFilePathsCache()).to.be.equal('test/ut/utils/obfuscation/sourceFilePaths.cache'); 343 expect(filePathManager.getSourceFilePaths().size).to.be.equal(0); 344 expect(filePathManager.getDeletedSourceFilePaths().size).to.be.equal(0); 345 expect(filePathManager.getAddedSourceFilePaths().size).to.be.equal(0); 346 }); 347 }); 348 349 describe('test for createOrUpdateSourceFilePaths', function () { 350 let testFilePath1: string; 351 let testFilePath2: string; 352 let testFilePath3: string; 353 let testFilePath4: string; 354 let testFilePath5: string; 355 356 before(()=>{ 357 FileUtils.deleteFolderRecursive('test/ut/utils/testData'); 358 testFilePath1 = 'test/ut/utils/testData/filePath1.ts'; 359 testFilePath2 = 'test/ut/utils/testData/filePath2.ts'; 360 testFilePath3 = 'test/ut/utils/testData/filePath3.ts'; 361 testFilePath4 = 'test/ut/utils/testData/filePath4.ts'; 362 testFilePath5 = 'test/ut/utils/testData/filePath5.json'; 363 FileUtils.createDirectory('test/ut/utils/testData'); 364 fs.writeFileSync(testFilePath1, 'test'); 365 fs.writeFileSync(testFilePath2, 'test'); 366 fs.writeFileSync(testFilePath3, 'test'); 367 fs.writeFileSync(testFilePath4, 'test'); 368 369 FileUtils.deleteFile(path.join(__dirname,'obfuscation/sourceFilePaths.cache')); 370 }) 371 372 it('should writeSourceFilePaths when is not incremental', () => { 373 const filePathManager = new FilePathManager(cachePath); 374 const filePathsSet: Set<string> = new Set(); 375 filePathsSet.add(testFilePath1); 376 filePathsSet.add(testFilePath2); 377 filePathsSet.add(testFilePath3); 378 filePathsSet.add(testFilePath5); 379 filePathManager.createOrUpdateSourceFilePaths(filePathsSet); 380 let sourceFilePaths = path.join(__dirname,'obfuscation/sourceFilePaths.cache'); 381 let sourceFilePaths_expected = path.join(__dirname,'obfuscation/sourceFilePaths_expected01.txt'); 382 expect(compareFiles(sourceFilePaths, sourceFilePaths_expected)).to.be.true; 383 }); 384 it('should update SourceFilePaths correctly when is incremental', () => { 385 const filePathManager = new FilePathManager(cachePath); 386 const filePathsSet: Set<string> = new Set(); 387 filePathsSet.add(testFilePath1); 388 filePathsSet.add(testFilePath3); 389 filePathsSet.add(testFilePath4); 390 filePathManager.createOrUpdateSourceFilePaths(filePathsSet); 391 let sourceFilePaths = path.join(__dirname,'obfuscation/sourceFilePaths.cache'); 392 let sourceFilePaths_expected = path.join(__dirname,'obfuscation/sourceFilePaths_expected02.txt'); 393 expect(compareFiles(sourceFilePaths, sourceFilePaths_expected)).to.be.true; 394 expect(filePathManager.getAddedSourceFilePaths().has(testFilePath4)).to.be.true; 395 expect(filePathManager.getDeletedSourceFilePaths().has(testFilePath2)).to.be.true; 396 }); 397 }); 398 }); 399 400 describe('test for FileContentManager', function () { 401 let cachePath = 'test/ut/utils/obfuscation'; 402 describe('test for constructor', function () { 403 it('should be initialized correctly', () => { 404 FileUtils.deleteFolderRecursive('test/ut/utils/obfuscation/transformed'); 405 const fileContentManager = new FileContentManager(cachePath, false); 406 const transformedPath = 'test/ut/utils/obfuscation/transformed'; 407 const fileNamesMapPath = 'test/ut/utils/obfuscation/transformed/transformedFileNamesMap.json'; 408 expect(fileContentManager.getTransformedFilesDir()).to.be.equal(transformedPath); 409 expect(fileContentManager.getFileNamesMapPath()).to.be.equal(fileNamesMapPath); 410 expect(fileContentManager.fileNamesMap.size).to.be.equal(0); 411 expect(fileContentManager.getIsIncremental()).to.be.equal(false); 412 expect(fs.existsSync(transformedPath)).to.be.equal(true); 413 }); 414 }); 415 describe('test for updateFileContent', function () { 416 const fileContent01: FileContent = { 417 moduleInfo: { 418 content: "let a = 1;\n", 419 buildFilePath: "D:\\WORK\\demo\\libraryHar\\build\\default\\cache\\default\\default@HarCompileArkTS\\esmodule\\release\\libraryHar\\src\\main\\ets\\components\\test.ts", 420 relativeSourceFilePath: "libraryHar/src/main/ets/components/test.ets", 421 originSourceFilePath: "D:/WORK/demo/libraryHar/src/main/ets/components/test.ets", 422 rollupModuleId: "D:\\WORK\\demo\\libraryHar\\src\\main\\ets\\components\\test.ets" 423 }, 424 previousStageSourceMap: { 425 version: 3, 426 file: "test.ets", 427 sourceRoot: "", 428 sources: [ 429 "libraryHar/src/main/ets/components/test.ets" 430 ], 431 names: [], 432 mappings: "AAAA,IAAI,CAAC,GAAG,CAAC,CAAC" 433 } 434 }; 435 const fileContent01_updated: FileContent = { 436 moduleInfo: { 437 content: "let a1 = \"1\";\n", 438 buildFilePath: "D:\\WORK\\demo\\libraryHar\\build\\default\\cache\\default\\default@HarCompileArkTS\\esmodule\\release\\libraryHar\\src\\main\\ets\\components\\test.ts", 439 relativeSourceFilePath: "libraryHar/src/main/ets/components/test.ets", 440 originSourceFilePath: "D:/WORK/demo/libraryHar/src/main/ets/components/test.ets", 441 rollupModuleId: "D:\\WORK\\demo\\libraryHar\\src\\main\\ets\\components\\test.ets" 442 }, 443 previousStageSourceMap: { 444 version: 3, 445 file: "test.ets", 446 sourceRoot: "", 447 sources: [ 448 "libraryHar/src/main/ets/components/test.ets" 449 ], 450 names: [], 451 mappings: "AAAA,IAAI,CAAC,GAAG,CAAC,CAAC" 452 } 453 }; 454 const fileContent02: FileContent = { 455 moduleInfo: { 456 content: "let a = 2;\n", 457 buildFilePath: "D:\\WORK\\demo\\libraryHar\\build\\default\\cache\\default\\default@HarCompileArkTS\\esmodule\\release\\libraryHar\\src\\main\\ets\\components\\test2.ts", 458 relativeSourceFilePath: "libraryHar/src/main/ets/components/test2.ets", 459 originSourceFilePath: "D:/WORK/demo/libraryHar/src/main/ets/components/test2.ets", 460 rollupModuleId: "D:\\WORK\\demo\\libraryHar\\src\\main\\ets\\components\\test2.ets" 461 }, 462 previousStageSourceMap: { 463 version: 3, 464 file: "test2.ets", 465 sourceRoot: "", 466 sources: [ 467 "libraryHar/src/main/ets/components/test2.ets" 468 ], 469 names: [], 470 mappings: "AAAA,IAAI,CAAC,GAAG,CAAC,CAAC" 471 } 472 }; 473 const fileContent03: FileContent = { 474 moduleInfo: { 475 content: "let a = 3;\n", 476 buildFilePath: "D:\\WORK\\demo\\libraryHar\\build\\default\\cache\\default\\default@HarCompileArkTS\\esmodule\\release\\libraryHar\\src\\main\\ets\\components\\test3.ts", 477 relativeSourceFilePath: "libraryHar/src/main/ets/components/test3.ets", 478 originSourceFilePath: "D:/WORK/demo/libraryHar/src/main/ets/components/test3.ets", 479 rollupModuleId: "D:\\WORK\\demo\\libraryHar\\src\\main\\ets\\components\\test3.ets" 480 }, 481 previousStageSourceMap: { 482 version: 3, 483 file: "test3.ets", 484 sourceRoot: "", 485 sources: [ 486 "libraryHar/src/main/ets/components/test3.ets" 487 ], 488 names: [], 489 mappings: "AAAA,IAAI,CAAC,GAAG,CAAC,CAAC" 490 } 491 }; 492 it('should writeFileContent when full compilation', () => { 493 const fileContentManager = new FileContentManager(cachePath, false); 494 fileContentManager.updateFileContent(fileContent01); 495 fileContentManager.updateFileContent(fileContent02); 496 const transformedFilePath01: string | undefined= fileContentManager.fileNamesMap.get(fileContent01.moduleInfo.originSourceFilePath!); 497 const transformedFilePath02: string | undefined= fileContentManager.fileNamesMap.get(fileContent02.moduleInfo.originSourceFilePath!); 498 const reservedFileContent01 = fileContentManager.readFileContent(transformedFilePath01!); 499 const reservedFileContent02 = fileContentManager.readFileContent(transformedFilePath02!); 500 fileContentManager.writeFileNamesMap(); 501 expect(reservedFileContent01).to.be.deep.equal(fileContent01); 502 expect(reservedFileContent02).to.be.deep.equal(fileContent02); 503 }); 504 it('should updateFileContent when incremental compilation', () => { 505 const fileContentManager = new FileContentManager(cachePath, true); 506 // before update 507 fileContentManager.readFileNamesMap(); 508 const transformedFilePath01_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent01.moduleInfo.originSourceFilePath!); 509 const transformedFilePath02_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent02.moduleInfo.originSourceFilePath!); 510 const transformedFilePath03_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent03.moduleInfo.originSourceFilePath!); 511 const reservedFileContent01_before = fileContentManager.readFileContent(transformedFilePath01_before!); 512 const reservedFileContent02_before = fileContentManager.readFileContent(transformedFilePath02_before!); 513 expect(transformedFilePath03_before).to.be.undefined; 514 expect(reservedFileContent01_before).to.be.deep.equal(fileContent01); 515 expect(reservedFileContent02_before).to.be.deep.equal(fileContent02); 516 517 // after update 518 const deletedFilePath: Set<string> = new Set(); 519 deletedFilePath.add(fileContent02.moduleInfo.originSourceFilePath!); 520 deletedFilePath.add('pathNotExists'); 521 fileContentManager.deleteFileContent(deletedFilePath); 522 fileContentManager.updateFileContent(fileContent01_updated); 523 fileContentManager.updateFileContent(fileContent03); 524 const transformedFilePath01: string | undefined= fileContentManager.fileNamesMap.get(fileContent01.moduleInfo.originSourceFilePath!); 525 const transformedFilePath02: string | undefined= fileContentManager.fileNamesMap.get(fileContent02.moduleInfo.originSourceFilePath!); 526 const transformedFilePath03: string | undefined= fileContentManager.fileNamesMap.get(fileContent03.moduleInfo.originSourceFilePath!); 527 expect(transformedFilePath02).to.be.undefined; 528 const reservedFileContent01 = fileContentManager.readFileContent(transformedFilePath01!); 529 const reservedFileContent03 = fileContentManager.readFileContent(transformedFilePath03!); 530 expect(reservedFileContent01).to.be.deep.equal(fileContent01_updated); 531 expect(reservedFileContent03).to.be.deep.equal(fileContent03); 532 }); 533 it('should sort fileNamesMap correctly', () => { 534 const fileContentManager = new FileContentManager(cachePath, false); 535 const fileNamesMap = fileContentManager.fileNamesMap; 536 fileNamesMap.set('file4','test'); 537 fileNamesMap.set('file3','test'); 538 fileNamesMap.set('file5','test'); 539 const sortedNames: string[] = fileContentManager.getSortedFiles(); 540 expect(sortedNames[0]).to.be.equal('file3'); 541 expect(sortedNames[1]).to.be.equal('file4'); 542 expect(sortedNames[2]).to.be.equal('file5'); 543 }); 544 }); 545 }); 546})