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 
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstdio>
22 
23 #include "random_access_file.h"
24 #include "random_access_file_input.h"
25 #include "random_access_file_output.h"
26 
27 namespace OHOS {
28 namespace SignatureTools {
29 
30 const char* UNSIGNED_HAP_FILE_PATH = "./zip/unsigned.hap";
RandomAccessFileReadFileFunc(const uint8_t* data, size_t size)31 void RandomAccessFileReadFileFunc(const uint8_t* data, size_t size)
32 {
33     auto outputHap = std::make_shared<RandomAccessFile>();
34     if (!outputHap->Init(UNSIGNED_HAP_FILE_PATH)) {
35         return;
36     }
37     std::string buf(1, 0);
38     outputHap->ReadFileFullyFromOffset(buf.data(), 0, 1);
39 }
40 
RandomAccessFileInputConstructor(const uint8_t* data, size_t size)41 void RandomAccessFileInputConstructor(const uint8_t* data, size_t size)
42 {
43     RandomAccessFile file;
44     if (!file.Init(UNSIGNED_HAP_FILE_PATH)) {
45         return;
46     }
47     int64_t fileLength = file.GetLength();
48     RandomAccessFileInput fileInput(file, 0, fileLength);
49     fileInput.Size();
50 }
51 
RandomAccessFileOutputConstructor(const uint8_t* data, size_t size)52 void RandomAccessFileOutputConstructor(const uint8_t* data, size_t size)
53 {
54     RandomAccessFile file;
55     if (!file.Init(UNSIGNED_HAP_FILE_PATH)) {
56         return;
57     }
58     RandomAccessFileOutput fileOutput(&file);
59 }
60 
DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)61 void DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
62 {
63     if (data == nullptr || size == 0) {
64         return;
65     }
66 
67     RandomAccessFileReadFileFunc(data, size);
68     RandomAccessFileInputConstructor(data, size);
69     RandomAccessFileOutputConstructor(data, size);
70 }
71 } // namespace SignatureTools
72 } // namespace OHOS
73 
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77     /* Run your code on data */
78     (void)rename("./zip/unsigned.txt", OHOS::SignatureTools::UNSIGNED_HAP_FILE_PATH);
79     sync();
80     OHOS::SignatureTools::DoSomethingInterestingWithMyAPI(data, size);
81     return 0;
82 }