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