1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { assert, expect } from 'chai'; 17import { it, describe} from 'mocha'; 18import { IOptions } from '../../../src/configs/IOptions'; 19import { FileUtils } from '../../../src/utils/FileUtils'; 20 21import ts from 'typescript'; 22import path from 'path'; 23import secharmony from '../../../src/transformers/layout/DisableConsoleTransformer'; 24 25describe('Tester cases for <DisableConsoleTransformer>', function() { 26 it('should return null if mDisableConsole is false', () => { 27 let options: IOptions | undefined = FileUtils.readFileAsJson(path.join(__dirname, "default_config.json")); 28 assert.strictEqual(options !== undefined, true); 29 const disableConsoleFactory = secharmony.transformerPlugin.createTransformerFactory(options as IOptions); 30 expect(disableConsoleFactory).to.be.equal(null); 31 }); 32 33 it('should return function if mDisableConsole is true', () => { 34 let options: IOptions | undefined = FileUtils.readFileAsJson(path.join(__dirname, "obfuscate_identifier_config.json")); 35 assert.strictEqual(options !== undefined, true); 36 const disableConsoleFactory = secharmony.transformerPlugin.createTransformerFactory(options as IOptions); 37 expect(typeof disableConsoleFactory).to.be.equal('function'); 38 }); 39 40 it('should return origin node if isSourceFile is false', () => { 41 let options: IOptions | undefined = FileUtils.readFileAsJson(path.join(__dirname, "obfuscate_identifier_config.json")); 42 assert.strictEqual(options !== undefined, true); 43 const disableConsoleFactory = secharmony.transformerPlugin.createTransformerFactory(options as IOptions); 44 const blockNode: ts.Block = ts.factory.createBlock([]); 45 let transformedResult: ts.TransformationResult<ts.Node> = ts.transform(blockNode, [disableConsoleFactory], {}); 46 expect(blockNode).to.be.equal(transformedResult.transformed[0]); 47 }); 48 49 describe('test cases for visitAst', () => { 50 let options: IOptions | undefined = FileUtils.readFileAsJson(path.join(__dirname, "obfuscate_identifier_config.json")); 51 assert.strictEqual(options !== undefined, true); 52 const disableConsoleFactory = secharmony.transformerPlugin.createTransformerFactory(options as IOptions); 53 54 it('should return factory.createModuleBlock', () => { 55 const fileContent = ` 56 module Test { 57 console.log("test whether the Block if created by factory.createModuleBlock") 58 } 59 `; 60 const sourceFile: ts.SourceFile = ts.createSourceFile('demo.ts', fileContent, ts.ScriptTarget.ES2015, true); 61 let transformedResult: ts.TransformationResult<ts.Node> = ts.transform(sourceFile, [disableConsoleFactory], {}); 62 assert.strictEqual((((transformedResult.transformed[0] as ts.SourceFile).statements[0] as ts.FunctionDeclaration).body!.statements.length), 0); 63 }); 64 65 it('should return factory.createCaseClause and factory.createDefaultClause', () => { 66 const fileContent = ` 67 function workday(day: number): void { 68 switch (day) { 69 case 6: 70 case 7: 71 console.log("Not Workday"); 72 default: 73 console.log("Workday"); 74 } 75 } 76 `; 77 const sourceFile: ts.SourceFile = ts.createSourceFile('demo.ts', fileContent, ts.ScriptTarget.ES2015, true); 78 let transformedResult: ts.TransformationResult<ts.Node> = ts.transform(sourceFile, [disableConsoleFactory], {}); 79 assert.strictEqual( 80 (((transformedResult 81 .transformed[0] as ts.SourceFile) 82 .statements[0] as ts.FunctionDeclaration) 83 .body! 84 .statements[0] as ts.SwitchStatement) 85 .caseBlock 86 .clauses[1] 87 .statements 88 .length 89 , 0); 90 assert.strictEqual( 91 (((transformedResult 92 .transformed[0] as ts.SourceFile) 93 .statements[0] as ts.FunctionDeclaration) 94 .body! 95 .statements[0] as ts.SwitchStatement) 96 .caseBlock 97 .clauses[2] 98 .statements 99 .length 100 , 0); 101 }); 102 }); 103 104 describe('test cases for isSimpleConsoleStatement', () => { 105 let options: IOptions | undefined = FileUtils.readFileAsJson(path.join(__dirname, "obfuscate_identifier_config.json")); 106 assert.strictEqual(options !== undefined, true); 107 const disableConsoleFactory = secharmony.transformerPlugin.createTransformerFactory(options as IOptions); 108 109 it('should return true beacause the content of fileContent is the support simple format', () => { 110 const fileContent = ` 111 console.log('Hello, World!'); 112 console['log']('hello, world'); 113 `; 114 const sourceFile: ts.SourceFile = ts.createSourceFile('demo.ts', fileContent, ts.ScriptTarget.ES2015, true); 115 let transformedResult: ts.TransformationResult<ts.Node> = ts.transform(sourceFile, [disableConsoleFactory], {}); 116 assert.strictEqual((transformedResult.transformed[0] as ts.SourceFile).statements.length, 0); 117 }); 118 119 it('should return false because there is no console keyword in the source file', () => { 120 const fileContent = ` 121 []; 122 `; 123 const sourceFile: ts.SourceFile = ts.createSourceFile('demo.ts', fileContent, ts.ScriptTarget.ES2015, true); 124 let transformedResult: ts.TransformationResult<ts.Node> = ts.transform(sourceFile, [disableConsoleFactory], {}); 125 assert.strictEqual( 126 (((transformedResult 127 .transformed[0] as ts.SourceFile) 128 .statements[0] as ts.ExpressionStatement) 129 .expression as ts.ArrayLiteralExpression) 130 .elements 131 .length 132 , 0); 133 }); 134 }); 135});