diff --git a/arkguard/package.json b/arkguard/package.json index 7838645676c6e8b5f42bd5b986f8e3012074f98f..3a1347a942d54395899dfc4843ba5004ecbf6982 100644 --- a/arkguard/package.json +++ b/arkguard/package.json @@ -12,7 +12,7 @@ "build": "npm run clean && node node_modules/typescript/lib/tsc.js", "test": "npm run test:ut && npm run test:grammar", "test:ut": "node ./node_modules/mocha/bin/mocha --require ts-node/register ./test/ut/**/*.ts", - "test:grammar": "rm -rf test/local && node --loader=ts-node/esm ./src/cli/SecHarmony.ts ./test/grammar --config-path ./scripts/grammarTestConfig.json && node ./scripts/grammarTestScript.js" + "test:grammar": "rm -rf test/local && node ./scripts/testScript.js && node ./scripts/grammarTestScript.js" }, "repository": { "type": "git", @@ -52,4 +52,4 @@ "tsconfig.json", "README.md" ] -} +} \ No newline at end of file diff --git a/arkguard/scripts/grammarTestConfig.json b/arkguard/scripts/grammarTestConfig.json index ea8122d928f67c85e6b57a2198fb892b7b176629..3ce9b7e20e8acce4c5e01eba0b53e4514d1db916 100644 --- a/arkguard/scripts/grammarTestConfig.json +++ b/arkguard/scripts/grammarTestConfig.json @@ -3,18 +3,28 @@ "mRemoveComments": false, "mOutputDir": "../test/local", "mDisableHilog": false, - "mDisableConsole":false, + "mDisableConsole": false, "mSimplify": false, "mNameObfuscation": { "mEnable": true, "mNameGeneratorType": 1, "mDictionaryList": [], - "mReservedNames": ["moduleName01", "stringLiteralObj02", "Interface11", "class11", "enum11", "Object11"], + "mReservedNames": [ + "moduleName01", + "stringLiteralObj02", + "Interface11", + "class11", + "enum11", + "Object11" + ], "mRenameProperties": true, - "mReservedProperties": ["strictEqual", "readFileSync"], + "mReservedProperties": [ + "strictEqual", + "readFileSync" + ], "mKeepStringProperty": true }, "mEnableSourceMap": false, "mEnableNameCache": false, - "mTopLevel":true + "mTopLevel": true } \ No newline at end of file diff --git a/arkguard/scripts/grammarTestScript.js b/arkguard/scripts/grammarTestScript.js index e173f279ef0297911e091ff2fce927eba19a0765..de1e34825e52a9f5502bb8d103cb487cd55189ec 100644 --- a/arkguard/scripts/grammarTestScript.js +++ b/arkguard/scripts/grammarTestScript.js @@ -1,14 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); const testDirectory = path.resolve('./test/local'); +function compareWithExpected(filePath) { + const expectedFilePath = filePath.replace(/\.ts$/, '-expected.txt'); + + if (!fs.existsSync(expectedFilePath)) { + return true; + } + + const actualContent = fs.readFileSync(filePath, 'utf-8').trim(); + const expectedContent = fs.readFileSync(expectedFilePath, 'utf-8').trim(); + + return actualContent === expectedContent; +} + function runTest(filePath) { try { const command = `node ./node_modules/ts-node/dist/bin.js ${filePath}`; execSync(command); - return true; + if (compareWithExpected(filePath)) { + return true; + } else { + console.error(`Test case ${filePath} failed: Content does not match`); + return false; + } } catch (error) { console.error(`Test case ${filePath} failed:`, error); return false; @@ -36,7 +69,7 @@ function runTestsInDirectory(directoryPath) { failedFiles.push(filePath); } } - } else if (path.extname(filePath) === '.ts') { + } else if (path.extname(filePath) === '.ts' || path.extname(filePath) === '.js') { const isSuccess = runTest(filePath); if (isSuccess) { successCount++; diff --git a/arkguard/scripts/testScript.js b/arkguard/scripts/testScript.js new file mode 100644 index 0000000000000000000000000000000000000000..a5dbb7116c8eb01b4ca98d24645972baf2926ff4 --- /dev/null +++ b/arkguard/scripts/testScript.js @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const fs = require('fs'); +const path = require('path'); +const { exec } = require('child_process'); + +function obfuscateDirs(obfConfig, obfDir) { + const command = `node --loader=ts-node/esm src/cli/SecHarmony.ts ${obfDir} --config-path ${obfConfig}` + exec(command, (error, stdout, stderr) => { + if (error) { + console.error(`Error executing command: ${error.message}`); + return; + } + }); +} + + +function traverseDirs(rootDirPath, configPath) { + const currentEntries = fs.readdirSync(rootDirPath); + let configFile = "obfConfig.json"; + + if (currentEntries.includes(configFile)) { + configPath = rootDirPath; + } + + const hasJsOrTsFiles = currentEntries.some(entry => entry.endsWith('.js') || entry.endsWith('.ts')); + + if (hasJsOrTsFiles) { + obfuscateDirs(path.join(configPath, configFile), rootDirPath); + return; + } + + for (const currentEntry of currentEntries) { + const currentPath = path.join(rootDirPath, currentEntry); + if (fs.statSync(currentPath).isDirectory()) { + traverseDirs(currentPath, configPath); + } + } +} + +function run() { + const testCasesRootDir = path.join(__dirname, '../test/grammar'); + traverseDirs(testCasesRootDir, testCasesRootDir) +} + +function main() { + run(); +} + +main() \ No newline at end of file diff --git a/arkguard/src/transformers/layout/SimplifyTransformer.ts b/arkguard/src/transformers/layout/SimplifyTransformer.ts index c76d0d21ee9a834cc6adce97569c3257bc548220..5e5d3b3498bf084e7ab1b3b631d3c4641680b793 100644 --- a/arkguard/src/transformers/layout/SimplifyTransformer.ts +++ b/arkguard/src/transformers/layout/SimplifyTransformer.ts @@ -42,9 +42,9 @@ import type { Expression } from 'typescript'; -import type {IOptions} from '../../configs/IOptions'; -import type {TransformPlugin} from '../TransformPlugin'; -import {isCommentedNode, isSuperCallStatement} from '../../utils/TransformUtil'; +import type { IOptions } from '../../configs/IOptions'; +import type { TransformPlugin } from '../TransformPlugin'; +import { isCommentedNode, isSuperCallStatement } from '../../utils/TransformUtil'; namespace secharmony { const TRANSFORMER_ORDER: number = 5; diff --git a/arkguard/src/utils/OhsUtil.ts b/arkguard/src/utils/OhsUtil.ts index a97703b9d72cccb2f5accf74f5cf97535743d33d..031d0a56b41d94df2b1f3f8d510e61dad3b992b4 100644 --- a/arkguard/src/utils/OhsUtil.ts +++ b/arkguard/src/utils/OhsUtil.ts @@ -53,7 +53,7 @@ import type { TypeAliasDeclaration, } from 'typescript'; -import {OhPackType} from './TransformUtil'; +import { OhPackType } from './TransformUtil'; export const stringPropsSet: Set = new Set(); /** diff --git a/arkguard/test/grammar/class_validation/obfConfig.json b/arkguard/test/grammar/class_validation/obfConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..1a198a96adc0bf9d8311c7db389d4b6be4c4395d --- /dev/null +++ b/arkguard/test/grammar/class_validation/obfConfig.json @@ -0,0 +1,22 @@ +{ + "mCompact": false, + "mRemoveComments": false, + "mOutputDir": "../../local", + "mDisableHilog": false, + "mDisableConsole": false, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mReservedNames": [], + "mRenameProperties": true, + "mReservedProperties": [ + "strictEqual" + ], + "mKeepStringProperty": false + }, + "mEnableSourceMap": false, + "mEnableNameCache": false, + "mTopLevel": true +} \ No newline at end of file diff --git a/arkguard/test/grammar/compact/decoratorAndModifier-expected.txt b/arkguard/test/grammar/compact/decoratorAndModifier-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..91837d05e586e1b967b5db52f51e0e33beab1aa9 --- /dev/null +++ b/arkguard/test/grammar/compact/decoratorAndModifier-expected.txt @@ -0,0 +1 @@ +function a(f: Function) { console.log(`Class name: ${f.name}`); } function b(f: any, g: string) { console.log(`Property name: ${g}`); } function c(f: any, g: string) { console.log(`Property name: ${g}`); } @a class e { @b @c private g: string; @c @b public h: number; @b async i(): Promise { } } function d() { let f = 1; let g = 2; let h = 3; let i = 4; let j = 5; } \ No newline at end of file diff --git a/arkguard/test/grammar/compact/decoratorAndModifier.ts b/arkguard/test/grammar/compact/decoratorAndModifier.ts new file mode 100644 index 0000000000000000000000000000000000000000..ca071153b4647e4fcb57592b394f95cae20a8a5b --- /dev/null +++ b/arkguard/test/grammar/compact/decoratorAndModifier.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function logClassName(constructor: Function) { + console.log(`Class name: ${constructor.name}`); +} + +function logProperty1(target: any, key: string) { + console.log(`Property name: ${key}`); +} +function logProperty2(target: any, key: string) { + console.log(`Property name: ${key}`); +} + +@logClassName +class ExampleClass { + @logProperty1 @logProperty2 + private someProperty: string; // commentssssss + @logProperty2 + @logProperty1 + public secondProperty: number; + + @logProperty1 + async foo(): Promise { } +} +function foo() { + let a1 = 1 + let a2 = 2; + let a3 = 3 + let a4 = 4 + let a5 = 5 +} diff --git a/arkguard/test/grammar/compact/numericLiteralsWithTrailingDecimalPoints-expected.txt b/arkguard/test/grammar/compact/numericLiteralsWithTrailingDecimalPoints-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..917324371e451a1b987fe4feefed839c51f3f901 --- /dev/null +++ b/arkguard/test/grammar/compact/numericLiteralsWithTrailingDecimalPoints-expected.txt @@ -0,0 +1 @@ +1..toString(); 1.0.toString(); 1. + 2.0 + 3.; var a: number = 1; var b = a.toString(); var c = 3..toString(); var d = 3..toString(); var e = 3..toString(); var f = 3.['toString'](); var g = 3 .toString(); var h = new Number(4).toString(); var i = 3. + 3.; var j = 0 .toString(); var k = 3. .toString(); var l = 3 .toString(); var m = 3 .toString(); var n = 3 .toString(); var o = 3. .toString(); var p = 3 .toString(); var q = 3. .toString(); var r = 3 .toString(); var s = 3. .toString(); \ No newline at end of file diff --git a/arkguard/test/grammar/compact/numericLiteralsWithTrailingDecimalPoints.ts b/arkguard/test/grammar/compact/numericLiteralsWithTrailingDecimalPoints.ts new file mode 100644 index 0000000000000000000000000000000000000000..e2ced618af4fe5e3d8e265b9cb25cadeb2604d4e --- /dev/null +++ b/arkguard/test/grammar/compact/numericLiteralsWithTrailingDecimalPoints.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +1..toString(); +1.0.toString(); +1. + 2.0 + 3.; + +var i: number = 1; +var case1 = i.toString(); +var case3 = 3 .toString(); +var case4 = 3 .toString(); +var case5 = 3 .toString(); +var case6 = 3.['toString'](); +var case7 = 3 + .toString(); +var case8 = new Number(4).toString(); +var case9 = 3. + 3.; +var case10 = 0 /* comment */.toString(); +var case11 = 3. /* comment */.toString(); +var case12 = 3 + /* comment */ .toString(); +var case122 = 3 +/* comment */.toString(); +var case1222 = 3 + + .toString(); +var case13 = 3. + /* comment */.toString(); +var case14 = 3 + // comment + .toString(); +var case15 = 3. + // comment + .toString(); +var case16 = 3 // comment time + .toString(); +var case17 = 3. // comment time again + .toString(); + + + + diff --git a/arkguard/test/grammar/compact/obfConfig.json b/arkguard/test/grammar/compact/obfConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..32cc81fb26c3051b0a92efc88c11ff13b2084812 --- /dev/null +++ b/arkguard/test/grammar/compact/obfConfig.json @@ -0,0 +1,18 @@ +{ + "mCompact": true, + "mRemoveComments": false, + "mOutputDir": "../../local", + "mDisableHilog": false, + "mDisableConsole": false, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mRenameProperties": true, + "mKeepStringProperty": false + }, + "mEnableSourceMap": false, + "mEnableNameCache": false, + "mTopLevel": true +} \ No newline at end of file diff --git a/arkguard/test/grammar/compact/tsconfig.json b/arkguard/test/grammar/compact/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..7c2f781e008b6a058d6a08b767f0285068aef70f --- /dev/null +++ b/arkguard/test/grammar/compact/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "resolveJsonModule": true, + "declaration": true, + "allowJs": true, + "checkJs": true, + "experimentalDecorators": true + }, +} \ No newline at end of file diff --git a/arkguard/test/grammar/enum/obfConfig.json b/arkguard/test/grammar/enum/obfConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..1a198a96adc0bf9d8311c7db389d4b6be4c4395d --- /dev/null +++ b/arkguard/test/grammar/enum/obfConfig.json @@ -0,0 +1,22 @@ +{ + "mCompact": false, + "mRemoveComments": false, + "mOutputDir": "../../local", + "mDisableHilog": false, + "mDisableConsole": false, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mReservedNames": [], + "mRenameProperties": true, + "mReservedProperties": [ + "strictEqual" + ], + "mKeepStringProperty": false + }, + "mEnableSourceMap": false, + "mEnableNameCache": false, + "mTopLevel": true +} \ No newline at end of file diff --git a/arkguard/test/grammar/export/obfConfig.json b/arkguard/test/grammar/export/obfConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..1a198a96adc0bf9d8311c7db389d4b6be4c4395d --- /dev/null +++ b/arkguard/test/grammar/export/obfConfig.json @@ -0,0 +1,22 @@ +{ + "mCompact": false, + "mRemoveComments": false, + "mOutputDir": "../../local", + "mDisableHilog": false, + "mDisableConsole": false, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mReservedNames": [], + "mRenameProperties": true, + "mReservedProperties": [ + "strictEqual" + ], + "mKeepStringProperty": false + }, + "mEnableSourceMap": false, + "mEnableNameCache": false, + "mTopLevel": true +} \ No newline at end of file diff --git a/arkguard/test/grammar/jsfile/script1.js b/arkguard/test/grammar/jsfile/script1.js new file mode 100644 index 0000000000000000000000000000000000000000..e8d02c5db709df3c86013b9f65e8d0c536af104e --- /dev/null +++ b/arkguard/test/grammar/jsfile/script1.js @@ -0,0 +1,66 @@ +function addNumbers(a, b) { + return a + b; +} + +const person1 = { + firstName: 'John', + lastName: 'Doe', + age: 30, + greet: function () { + console.log(`Hello, my name is ${this.firstName} ${this.lastName}.`); + } +}; + +function findMax(arr) { + let max = arr[0]; + for (let i = 1; i < arr.length; i++) { + if (arr[i] > max) { + max = arr[i]; + } + } + return max; +} + +function isPalindrome(str) { + const cleanStr = str.toLowerCase().replace(/[^a-zA-Z0-9]/g, ''); + const reversedStr = cleanStr.split('').reverse().join(''); + return cleanStr === reversedStr; +} + +const person2 = { + firstName: 'John', + lastName: 'Doe', + age: 30, + greet: function () { + return `Hello, my name is ${this.firstName} ${this.lastName}.`; + } +}; + +function multiplyTable(n) { + for (let i = 1; i <= 10; i++) { + console.log(`${n} x ${i} = ${n * i}`); + } +} + +const result = addNumbers(3, 4); + +person1.greet(); + +const numbers = [1, 2, 3, 4, 5]; + +const sum = numbers.reduce((acc, curr) => acc + curr, 0); + +const maxNumber = findMax(numbers); +let i = 1, factorial = 1; +while (i <= 5) { + factorial *= i; + i++; +} + +const testString = "A man, a plan, a canal: Panama"; +const isPalindromic = isPalindrome(testString); + +const greeting = person1.greet(); + +const number = 7; +multiplyTable(number); \ No newline at end of file diff --git a/arkguard/test/grammar/obfConfig.json b/arkguard/test/grammar/obfConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..c83806d2a907beffa78411a3af7b9251f4133068 --- /dev/null +++ b/arkguard/test/grammar/obfConfig.json @@ -0,0 +1,18 @@ +{ + "mCompact": false, + "mRemoveComments": false, + "mOutputDir": "../local", + "mDisableHilog": false, + "mDisableConsole": false, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mRenameProperties": true, + "mKeepStringProperty": false + }, + "mEnableSourceMap": false, + "mEnableNameCache": false, + "mTopLevel": true +} \ No newline at end of file diff --git a/arkguard/test/grammar/obfuscation_validation/obfConfig.json b/arkguard/test/grammar/obfuscation_validation/obfConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..5f6ae29ea624d458e9e17df3d7b77796def45a38 --- /dev/null +++ b/arkguard/test/grammar/obfuscation_validation/obfConfig.json @@ -0,0 +1,30 @@ +{ + "mCompact": false, + "mRemoveComments": false, + "mOutputDir": "../../local", + "mDisableHilog": false, + "mDisableConsole": false, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mReservedNames": [ + "moduleName01", + "stringLiteralObj02", + "Interface11", + "class11", + "enum11", + "Object11" + ], + "mRenameProperties": true, + "mReservedProperties": [ + "strictEqual", + "readFileSync" + ], + "mKeepStringProperty": true + }, + "mEnableSourceMap": false, + "mEnableNameCache": false, + "mTopLevel": true +} \ No newline at end of file diff --git a/arkguard/test/grammar/obj/obfConfig.json b/arkguard/test/grammar/obj/obfConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..1a198a96adc0bf9d8311c7db389d4b6be4c4395d --- /dev/null +++ b/arkguard/test/grammar/obj/obfConfig.json @@ -0,0 +1,22 @@ +{ + "mCompact": false, + "mRemoveComments": false, + "mOutputDir": "../../local", + "mDisableHilog": false, + "mDisableConsole": false, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mReservedNames": [], + "mRenameProperties": true, + "mReservedProperties": [ + "strictEqual" + ], + "mKeepStringProperty": false + }, + "mEnableSourceMap": false, + "mEnableNameCache": false, + "mTopLevel": true +} \ No newline at end of file