diff --git a/arkguard/src/ArkObfuscator.ts b/arkguard/src/ArkObfuscator.ts index 2e4795e2cafe4109f447561417b9024d4da300e5..941847acd0b2a8d1777b5a4ee9eb609429c9d745 100644 --- a/arkguard/src/ArkObfuscator.ts +++ b/arkguard/src/ArkObfuscator.ts @@ -419,9 +419,9 @@ export class ArkObfuscator { private initIncrementalCache(cachePath: string, enableAtKeep: boolean): void { this.filePathManager = new FilePathManager(cachePath); - this.isIncremental = this.filePathManager.isIncremental(); + this.fileContentManager = new FileContentManager(cachePath); - this.fileContentManager = new FileContentManager(cachePath, this.isIncremental); + this.isIncremental = this.filePathManager.getIsIncremental() && this.fileContentManager.getIsIncremental(); initProjectWhiteListManager(cachePath, this.isIncremental, enableAtKeep); } diff --git a/arkguard/src/utils/ProjectCollections.ts b/arkguard/src/utils/ProjectCollections.ts index e218b8b2479665158c740d695dc091893e79276e..304e407c0adcfcd89bbadc1a0c26f6d6db9fb6cf 100644 --- a/arkguard/src/utils/ProjectCollections.ts +++ b/arkguard/src/utils/ProjectCollections.ts @@ -25,6 +25,7 @@ import * as crypto from 'crypto'; import * as ts from 'typescript'; import fs from 'fs'; import path from 'path'; +import { readJsonSync } from 'fs-extra'; export function addToSet(targetSet: Set, sourceSet: Set): void { sourceSet.forEach((element) => targetSet.add(element)); @@ -636,6 +637,9 @@ export class FilePathManager { // Cache path of sourceFilePaths.cache file private filePathsCache: string; + // If is incremental build + private isIncremental: boolean; + public getSourceFilePaths(): Set { return this.sourceFilePaths; } @@ -652,8 +656,13 @@ export class FilePathManager { return this.filePathsCache; } + public getIsIncremental(): boolean { + return this.isIncremental; + } + constructor(cachePath: string) { this.filePathsCache = path.join(cachePath, SOURCE_FILE_PATHS); + this.isIncremental = fs.existsSync(this.filePathsCache); this.sourceFilePaths = new Set(); this.deletedSourceFilePaths = new Set(); this.addedSourceFilePaths = new Set(); @@ -696,16 +705,12 @@ export class FilePathManager { // Create or update sourceFilePaths.cache public createOrUpdateSourceFilePaths(sourceFilePaths: Set): void { this.setSourceFilePaths(sourceFilePaths); - if (this.isIncremental()) { + if (this.isIncremental) { this.updateSourceFilePaths(); } else { this.writeSourceFilePaths(); } } - - public isIncremental(): boolean { - return fs.existsSync(this.filePathsCache); - } } /** @@ -737,11 +742,11 @@ export class FileContentManager { return this.isIncremental; } - constructor(cachePath: string, isIncremental: boolean) { + constructor(cachePath: string) { this.transformedFilesDir = path.join(cachePath, TRANSFORMED_PATH); this.fileNamesMapPath = path.join(this.transformedFilesDir, FILE_NAMES_MAP); this.fileNamesMap = new Map(); - this.isIncremental = isIncremental; + this.isIncremental = this.readFileNamesMap(); FileUtils.createDirectory(this.transformedFilesDir); } @@ -776,9 +781,23 @@ export class FileContentManager { } } - public readFileNamesMap(): void { - const jsonObject = FileUtils.readFileAsJson(this.fileNamesMapPath); - this.fileNamesMap = new Map(Object.entries(jsonObject)); + // Returns false if is not incremental; + // Returns true if is incremental, and read content of fileNamesMap + private readFileNamesMap(): boolean { + if (!fs.existsSync(this.fileNamesMapPath)) { + return false; + } + try { + const jsonObject = readJsonSync(this.fileNamesMapPath); + if (!jsonObject) { + return false; + } + this.fileNamesMap = new Map(Object.entries(jsonObject)); + return true; + } catch (e) { + console.error('json file read error: ' + this.fileNamesMapPath); + return false; + } } public writeFileNamesMap(): void { diff --git a/arkguard/test/ut/arkobfuscator/ArkObfuscator2.spec.ts b/arkguard/test/ut/arkobfuscator/ArkObfuscator2.spec.ts index a345d5feb3e538a83a7bc568c0db1253cd83f8e3..8d464c1b73be487b232d40892fcb7e35d715a5b3 100644 --- a/arkguard/test/ut/arkobfuscator/ArkObfuscator2.spec.ts +++ b/arkguard/test/ut/arkobfuscator/ArkObfuscator2.spec.ts @@ -63,7 +63,12 @@ import { historyFileNameMangledTable, } from '../../../src/transformers/rename/RenameFileNameTransformer'; import { LocalVariableCollections } from '../../../src/utils/CommonCollections'; -import { SOURCE_FILE_PATHS, projectWhiteListManager } from '../../../src/utils/ProjectCollections'; +import { + FILE_NAMES_MAP, + SOURCE_FILE_PATHS, + TRANSFORMED_PATH, + projectWhiteListManager +} from '../../../src/utils/ProjectCollections'; import { FilePathObj } from '../../../src/common/type'; import { historyAllUnobfuscatedNamesMap } from '../../../src/initialization/Initializer'; import path from 'path'; @@ -215,11 +220,34 @@ export declare function findElement(arr: T[], callback: (item: T) => boolean) }; const cachePath = 'test/ut/utils/obfuscation'; const filePathsCache = path.join(cachePath, SOURCE_FILE_PATHS); - let content = 'hello'; - FileUtils.writeFile(filePathsCache, content); + const transformedFilePath = path.join(cachePath, TRANSFORMED_PATH); + const fileNamesMapCache = path.join(transformedFilePath, FILE_NAMES_MAP); + FileUtils.deleteFile(filePathsCache); + FileUtils.deleteFile(fileNamesMapCache); + let content1 = 'hello'; + let content2 = "{\"a\":\"1\"}"; + let content3 = ""; + // should return true if filePathsCache and fileNamesMapCache exists + FileUtils.writeFile(filePathsCache, content1); + FileUtils.writeFile(fileNamesMapCache, content2); obfuscator.init(config, cachePath); expect(obfuscator.isIncremental).to.be.true; + // should return false if filePathsCache not exists + FileUtils.deleteFile(filePathsCache); + obfuscator.init(config, cachePath); + expect(obfuscator.isIncremental).to.be.false; + // should return false if fileNamesMapCache not exists + FileUtils.writeFile(filePathsCache, content1); + FileUtils.deleteFile(fileNamesMapCache); + obfuscator.init(config, cachePath); + expect(obfuscator.isIncremental).to.be.false; + // should return false if fileNamesMap is undefined + FileUtils.writeFile(fileNamesMapCache, content3); + obfuscator.init(config, cachePath); + expect(obfuscator.isIncremental).to.be.false; + // clear FileUtils.deleteFile(filePathsCache); + FileUtils.deleteFile(fileNamesMapCache); }); }); diff --git a/arkguard/test/ut/utils/ProjectCollections.spec.ts b/arkguard/test/ut/utils/ProjectCollections.spec.ts index 1695ac1e82cb370310c4b07b3b65c05e355a7d16..a3015cc23b6c081a2c1570e37751d605412e7057 100644 --- a/arkguard/test/ut/utils/ProjectCollections.spec.ts +++ b/arkguard/test/ut/utils/ProjectCollections.spec.ts @@ -442,7 +442,7 @@ describe('test for CommonCollections', function () { describe('test for constructor', function () { it('should be initialized correctly', () => { FileUtils.deleteFolderRecursive('test/ut/utils/obfuscation/transformed'); - const fileContentManager = new FileContentManager(cachePath, false); + const fileContentManager = new FileContentManager(cachePath); const transformedPath = 'test/ut/utils/obfuscation/transformed'; const fileNamesMapPath = 'test/ut/utils/obfuscation/transformed/transformedFileNamesMap.json'; expect(fileContentManager.getTransformedFilesDir()).to.be.equal(transformedPath); @@ -530,7 +530,7 @@ describe('test for CommonCollections', function () { } }; it('should writeFileContent when full compilation', () => { - const fileContentManager = new FileContentManager(cachePath, false); + const fileContentManager = new FileContentManager(cachePath); fileContentManager.updateFileContent(fileContent01); fileContentManager.updateFileContent(fileContent02); const transformedFilePath01: string | undefined= fileContentManager.fileNamesMap.get(fileContent01.moduleInfo.originSourceFilePath!); @@ -542,9 +542,8 @@ describe('test for CommonCollections', function () { expect(reservedFileContent02).to.be.deep.equal(fileContent02); }); it('should updateFileContent when incremental compilation', () => { - const fileContentManager = new FileContentManager(cachePath, true); + const fileContentManager = new FileContentManager(cachePath); // before update - fileContentManager.readFileNamesMap(); const transformedFilePath01_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent01.moduleInfo.originSourceFilePath!); const transformedFilePath02_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent02.moduleInfo.originSourceFilePath!); const transformedFilePath03_before: string | undefined= fileContentManager.fileNamesMap.get(fileContent03.moduleInfo.originSourceFilePath!); @@ -571,8 +570,9 @@ describe('test for CommonCollections', function () { expect(reservedFileContent03).to.be.deep.equal(fileContent03); }); it('should sort fileNamesMap correctly', () => { - const fileContentManager = new FileContentManager(cachePath, false); + const fileContentManager = new FileContentManager(cachePath); const fileNamesMap = fileContentManager.fileNamesMap; + fileNamesMap.clear(); fileNamesMap.set('file4','test'); fileNamesMap.set('file3','test'); fileNamesMap.set('file5','test');