1/* 2 * Copyright (c) 2022 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 "readfileindir_fuzzer.h" 17 18#include <fstream> 19#include <iostream> 20#include <string> 21#include <vector> 22#include "init.h" 23#include "init_utils.h" 24 25static int FakeParser(const char *testFile, void *context) 26{ 27 UNUSED(context); 28 std::ifstream fileStream; 29 fileStream.open(testFile); 30 std::vector<std::string> fileText; 31 std::string textLine; 32 while (getline(fileStream, textLine)) { 33 fileText.push_back(textLine); 34 } 35 fileStream.close(); 36 std::vector<std::string>().swap(fileText); 37 return 0; 38} 39 40namespace OHOS { 41 bool FuzzReadFileInDir(const uint8_t* data, size_t size) 42 { 43 bool result = false; 44 FILE *pFile = nullptr; 45 pFile = fopen("ReadFileInDir.test", "w+"); 46 if (pFile == nullptr) { 47 std::cout << "[fuzz] open file ReadFileInDir.test failed"; 48 return false; 49 } 50 if (fwrite(data, 1, size, pFile) != size) { 51 std::cout << "[fuzz] write data to ReadFileInDir.test failed"; 52 (void)fclose(pFile); 53 return false; 54 } 55 (void)fclose(pFile); 56 if (!ReadFileInDir("/data", ".test", FakeParser, NULL)) { 57 result = true; 58 } 59 return result; 60 } 61} 62 63/* Fuzzer entry point */ 64extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 65{ 66 /* Run your code on data */ 67 OHOS::FuzzReadFileInDir(data, size); 68 return 0; 69} 70