• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 {FileUtils} from '../../../src/utils/FileUtils';
18import {assert, expect} from 'chai';
19import * as fs from 'fs';
20import path from 'path';
21
22const renameFileNameModule = require('../../../src/transformers/rename/RenameFileNameTransformer');
23
24describe('Tester Cases for <FileUtils>.', function () {
25  /** test for readFile */
26  it('Tester: <file not found> case for FileUtils#readFile', function () {
27    let path = '/user/local/tester';
28    assert.strictEqual(FileUtils.readFile(path), undefined);
29  });
30
31  it('Tester: <read file content.> case for FileUtils#readFile', function () {
32    let path = 'test/ut/utils/demo.txt';
33    assert.strictEqual(FileUtils.readFile(path), 'hello world!');
34  });
35
36  /** test for readFileAsJson */
37  it('Tester: <read file as json.> case for FileUtils#readFileAsJson', function () {
38    let path = 'test/ut/utils/demo.json';
39    let obj = FileUtils.readFileAsJson(path);
40    assert.strictEqual(obj?.mCompact, true);
41  });
42
43  it('Tester: <file not found.> case for FileUtils#readFileAsJson', function () {
44    let path = 'test/utils/demo_not_found.json';
45    let obj = FileUtils.readFileAsJson(path);
46    assert.strictEqual(obj, undefined);
47  });
48
49  it('Tester: <error json format.> case for FileUtils#readFileAsJson', function () {
50    let path = 'test/utils/error_json.txt';
51    let obj = FileUtils.readFileAsJson(path);
52    assert.strictEqual(obj, undefined);
53  });
54
55  /** test for getFileName */
56  it('Tester: <get file name with undefined input.> case for FileUtils#getFileName', function () {
57    let path = null;
58    assert.strictEqual(FileUtils.getFileName(path), undefined);
59
60    path = undefined;
61    assert.strictEqual(FileUtils.getFileName(path), undefined);
62  });
63
64  it('Tester: <get relative file fullname.> case for FileUtils#getFileName', function () {
65    let path = 'resources/configs/user_profile.json';
66    assert.strictEqual(FileUtils.getFileName(path), 'user_profile.json');
67  });
68
69  it('Tester: <get windows file fullname.> case for FileUtils#getFileName', function () {
70    let path = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5\\user_profile.json';
71    assert.strictEqual(FileUtils.getFileName(path), 'user_profile.json');
72  });
73
74  it('Tester: <get single file fullname.> case for FileUtils#getFileName', function () {
75    let path = 'user_profile.json';
76    assert.strictEqual(FileUtils.getFileName(path), 'user_profile.json');
77  });
78
79  /** test for getFileExtension */
80  it('Tester: <get file extension with undefined input.> case for FileUtils#getFileExtension', function () {
81    let path = null;
82    assert.strictEqual(FileUtils.getFileExtension(path), undefined);
83
84    path = undefined;
85    assert.strictEqual(FileUtils.getFileExtension(path), undefined);
86  });
87
88  it('Tester: <get file extension with input not contain point.> case for FileUtils#getFileExtension', function () {
89    let path = 'resources/configs/user_profile';
90    assert.strictEqual(FileUtils.getFileExtension(path), undefined);
91  });
92
93  it('Tester: <get file extension with dir contain point.> case for FileUtils#getFileExtension', function () {
94    let path = 'resources/configs.dir/user_profile.conf';
95    assert.strictEqual(FileUtils.getFileExtension(path), 'conf');
96  });
97
98  it('Tester: <get file extension.> case for FileUtils#getFileExtension', function () {
99    let path = 'resources/configs/user_profile.json';
100    assert.strictEqual(FileUtils.getFileExtension(path), 'json');
101  });
102
103  it('Tester: <get file extension with point end.> case for FileUtils#getFileExtension', function () {
104    let path = 'resources/configs/user_profile.';
105    assert.strictEqual(FileUtils.getFileExtension(path), '');
106  });
107
108  /** test for writeFile */
109  it('Tester: <write file test.> case for FileUtils#writeFile', function () {
110    let path = 'test/ut/utils/write_demo.txt';
111    let content = 'hello';
112    FileUtils.writeFile(path, content);
113
114    const fileContent = FileUtils.readFile(path);
115    assert.strictEqual(fileContent, content);
116  });
117
118  /** test for getPrefix */
119  it('Tester: <get prefix test.> case for FileUtils#getPrefix', function () {
120    let path = 'test/utils/write_demo.txt';
121    let prefix = 'test/utils/';
122
123    assert.strictEqual(FileUtils.getPrefix(path), prefix);
124  });
125
126  it('Tester: <get windows prefix test.> case for FileUtils#getPrefix', function () {
127    let path = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5\\us';
128    let prefix = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5\\';
129
130    assert.strictEqual(FileUtils.getPrefix(path), prefix);
131  });
132
133  it('Tester: <get no prefix test.> case for FileUtils#getPrefix', function () {
134    let path = 'D:';
135    let prefix = undefined;
136
137    assert.strictEqual(FileUtils.getPrefix(path), prefix);
138  });
139
140  /** test for getPathWithoutPrefix */
141  it('Tester: <get path without prefix no prefix test.> case for FileUtils#getPathWithoutPrefix', function () {
142    let path = 'D:';
143    let prefix = 'D:\\HuaweiApp';
144
145    assert.strictEqual(FileUtils.getPathWithoutPrefix(path, prefix), path);
146  });
147
148  it('Tester: <get path without prefix contain prefix test.> case for FileUtils#getPathWithoutPrefix', function () {
149    let path = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5';
150    let prefix = 'D:\\HuaweiApp';
151
152    assert.strictEqual(FileUtils.getPathWithoutPrefix(path, prefix), '\\ohsdk\\ets\\3.2.7.5');
153  });
154
155  it('Tester: <get path without prefix path and prefix equal test.> case for FileUtils#getPathWithoutPrefix', function () {
156    let path = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5';
157    let prefix = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5';
158
159    assert.strictEqual(FileUtils.getPathWithoutPrefix(path, prefix), '');
160  });
161
162  it('Tester: <determine whether oh_modules or not.> case for renameFileNameModule#isInOhModules', function () {
163    let projectInfo = {
164      projectRootPath: '/test/Obfuscation/arkguard',
165      packageDir: 'oh_modules'
166    };
167    let originalPath = '/test/Obfuscation/rkguard/entry/src/main/ets/pages/Index.ets';
168
169    assert.strictEqual(renameFileNameModule.isInOhModules(projectInfo, originalPath), false);
170  });
171
172  it('Tester: <determine whether oh_modules or not.> case for renameFileNameModule#isInOhModules', function () {
173    let projectInfo = {
174      projectRootPath: '/test/Obfuscation/arkguard',
175      packageDir: 'oh_modules'
176    };
177    let originalPath = '/test/Obfuscation/arkguard/oh_modules/.ohpm/json5@2.2.3/oh_modules/json5/dist/index.mjs';
178
179    assert.strictEqual(renameFileNameModule.isInOhModules(projectInfo, originalPath), true);
180  });
181
182  it('Tester: test API collectPathReservedString', function () {
183    let filePath1 = 'D:\\workplace\\Obfuscation\\TestForFilename\\entry';
184    let reservedNames = [];
185    FileUtils.collectPathReservedString(filePath1, reservedNames);
186    let filePath2 = '/OpenHarmony/arkcompiler/ets_frontend/arkguard/test/grammar/test.ts';
187    FileUtils.collectPathReservedString(filePath2, reservedNames);
188    let filePath3 = '../../test.ts.ts';
189    FileUtils.collectPathReservedString(filePath3, reservedNames);
190    assert.strictEqual(reservedNames[0], 'D:');
191    assert.strictEqual(reservedNames[1], 'workplace');
192    assert.strictEqual(reservedNames[2], 'Obfuscation');
193    assert.strictEqual(reservedNames[3], 'TestForFilename');
194    assert.strictEqual(reservedNames[4], 'entry');
195    assert.strictEqual(reservedNames[5], '');
196    assert.strictEqual(reservedNames[6], 'OpenHarmony');
197    assert.strictEqual(reservedNames[7], 'arkcompiler');
198    assert.strictEqual(reservedNames[8], 'ets_frontend');
199    assert.strictEqual(reservedNames[9], 'arkguard');
200    assert.strictEqual(reservedNames[10], 'test');
201    assert.strictEqual(reservedNames[11], 'grammar');
202    assert.strictEqual(reservedNames[12], 'test.ts');
203    assert.strictEqual(reservedNames[13], '..');
204    assert.strictEqual(reservedNames[14], '..');
205    assert.strictEqual(reservedNames[15], 'test.ts.ts');
206  });
207
208  it('Tester: test API deleteFolderRecursive', function () {
209    FileUtils.createDirectory('test/ut/utils/testData');
210    FileUtils.createDirectory('test/ut/utils/testData/test1');
211    let testPath: string =path.join(__dirname,'testData/test1/deleteFolderRecursive');
212    FileUtils.writeFile(testPath,'test');
213    expect(fs.existsSync(testPath)).to.be.true;
214    FileUtils.deleteFolderRecursive('test/ut/utils/testData');
215    expect(fs.existsSync(testPath)).to.be.false;
216  });
217});
218