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 #define private public
17 
18 #include "agingrequest_fuzzer.h"
19 
20 #include "aging_request.h"
21 #include "securec.h"
22 
23 using namespace OHOS::AppExecFwk;
24 namespace OHOS {
25 
26 constexpr size_t FOO_MAX_LEN = 1024;
27 constexpr size_t U32_AT_SIZE = 4;
28 constexpr uint8_t ENABLE = 2;
DoSomethingInterestingWithMyAPI(const char * data,size_t size)29 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
30 {
31     AgingBundleInfo bundleInfo;
32     AgingRequest agingRequest;
33     agingRequest.IsReachStartAgingThreshold();
34     agingRequest.IsReachEndAgingThreshold();
35     agingRequest.SortAgingBundles();
36     agingRequest.ResetRequest();
37     agingRequest.Dump();
38     agingRequest.AddAgingBundle(bundleInfo);
39     agingRequest.GetAgingBundles();
40     int64_t dataBytes = reinterpret_cast<uintptr_t>(data);
41     agingRequest.UpdateTotalDataBytesAfterUninstalled(dataBytes);
42     agingRequest.GetTotalDataBytes();
43     agingRequest.SetTotalDataBytes(dataBytes);
44     agingRequest.SetAgingCleanType(AgingCleanType::CLEAN_CACHE);
45     agingRequest.GetAgingCleanType();
46     agingRequest.GetTotalDataBytesThreshold();
47     agingRequest.GetOneDayTimeMs();
48     agingRequest.InitAgingPolicySystemParameters();
49     agingRequest.InitAgingDatasizeThreshold();
50     agingRequest.InitAgingOneDayTimeMs();
51     return true;
52 }
53 } // namespace OHOS
54 
55 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
57 {
58     /* Run your code on data */
59     if (data == nullptr) {
60         return 0;
61     }
62 
63     /* Validate the length of size */
64     if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
65         return 0;
66     }
67 
68     char* ch = (char*)malloc(size + 1);
69     if (ch == nullptr) {
70         std::cout << "malloc failed." << std::endl;
71         return 0;
72     }
73 
74     (void)memset_s(ch, size + 1, 0x00, size + 1);
75     if (memcpy_s(ch, size + 1, data, size) != EOK) {
76         std::cout << "copy failed." << std::endl;
77         free(ch);
78         ch = nullptr;
79         return 0;
80     }
81 
82     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
83     free(ch);
84     ch = nullptr;
85     return 0;
86 }