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
16 #include "directory_fuzzer.h"
17 #include "fuzz_log.h"
18 #include <fstream>
19 #include <sys/types.h>
20 #include "fuzzer/FuzzedDataProvider.h"
21 #include "directory_ex.h"
22
23 using namespace std;
24
25 namespace OHOS {
26 const uint8_t MAX_DIR_LENGTH = 15;
27 const uint8_t MAX_DIR_LEVEL = 10;
28 const uint8_t MAX_DIR_NUM = 10;
29
DirectoryTestFunc(FuzzedDataProvider * dataProvider)30 void DirectoryTestFunc(FuzzedDataProvider* dataProvider)
31 {
32 FUZZ_LOGI("DirectoryTestFunc start");
33 GetCurrentProcPath();
34
35 string testDir = "/data/test_dir";
36 ForceRemoveDirectory(testDir);
37
38 string testFile = testDir + "test_file";
39 ofstream file = ofstream(testFile, fstream::out);
40 file.close();
41
42 for (int i = 0; i < MAX_DIR_NUM; i++) {
43 string path = testDir;
44 int level = dataProvider->ConsumeIntegralInRange<int>(0, MAX_DIR_LEVEL);
45 for (int j = 0; j < level; j++) {
46 string name = dataProvider->ConsumeRandomLengthString(MAX_DIR_LENGTH);
47 path = ExcludeTrailingPathDelimiter(path) + "/" + name;
48 }
49 ForceCreateDirectory(path);
50 }
51
52 string dirName = dataProvider->ConsumeRandomLengthString(MAX_DIR_LENGTH);
53 string dirPath = testDir + dirName;
54 ForceCreateDirectory(dirPath);
55
56 string realPath1;
57 string overLongPath = testDir + dataProvider->ConsumeRandomLengthString(PATH_MAX);
58 PathToRealPath(overLongPath, realPath1);
59
60 string fileName = dataProvider->ConsumeRandomLengthString(MAX_DIR_LENGTH);
61 ExtractFileExt(fileName);
62
63 string fileFullPath = testDir + fileName;
64 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
65 ChangeModeFile(fileFullPath, mode);
66 ChangeModeDirectory(testDir, mode);
67 ChangeModeDirectory("", mode);
68
69 IsEmptyFolder(testDir);
70 ForceRemoveDirectory(testDir);
71
72 FUZZ_LOGI("DirectoryTestFunc end");
73 }
74
75 } // namespace OHOS
76
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
79 {
80 FuzzedDataProvider dataProvider(data, size);
81 OHOS::DirectoryTestFunc(&dataProvider);
82 return 0;
83 }
84