• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::string name(reinterpret_cast<const char*>(data), size);
45     if (!file.Init(UNSIGNED_HAP_FILE_PATH + name)) {
46         return;
47     }
48     int64_t fileLength = file.GetLength();
49     RandomAccessFileInput fileInput(file, 0, fileLength);
50     fileInput.Size();
51 }
52 
RandomAccessFileOutputConstructor(const uint8_t * data,size_t size)53 void RandomAccessFileOutputConstructor(const uint8_t* data, size_t size)
54 {
55     RandomAccessFile file;
56     std::string name(reinterpret_cast<const char*>(data), size);
57     if (!file.Init(UNSIGNED_HAP_FILE_PATH + name)) {
58         return;
59     }
60     RandomAccessFileOutput fileOutput(&file);
61 }
62 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)63 void DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
64 {
65     if (data == nullptr || size == 0) {
66         return;
67     }
68 
69     RandomAccessFileReadFileFunc(data, size);
70     RandomAccessFileInputConstructor(data, size);
71     RandomAccessFileOutputConstructor(data, size);
72 }
73 } // namespace SignatureTools
74 } // namespace OHOS
75 
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79     /* Run your code on data */
80     (void)rename("./zip/unsigned.txt", OHOS::SignatureTools::UNSIGNED_HAP_FILE_PATH);
81     sync();
82     OHOS::SignatureTools::DoSomethingInterestingWithMyAPI(data, size);
83     return 0;
84 }