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 "formiteminfo_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #define protected public
23 #include "form_item_info.h"
24 #undef private
25 #undef protected
26 #include "securec.h"
27
28 using namespace OHOS::AppExecFwk;
29
30 namespace OHOS {
31 constexpr size_t FOO_MAX_LEN = 1024;
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)34 uint32_t GetU32Data(const char* ptr)
35 {
36 // convert fuzz input data to an integer
37 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
38 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)39 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
40 {
41 FormItemInfo formItemInfo;
42 std::string dir(data, size);
43 std::vector<std::string> dirs;
44 dirs.emplace_back(dir);
45 formItemInfo.SetHapSourceDirs(dirs);
46 formItemInfo.GetHapSourceDirs(dirs);
47 formItemInfo.IsTemporaryForm();
48 std::string moduleName(data, size);
49 formItemInfo.GetHapSourceByModuleName(moduleName);
50 formItemInfo.IsValidItem();
51 FormRecord record;
52 formItemInfo.IsMatch(record);
53 formItemInfo.IsSameFormConfig(record);
54 formItemInfo.IsFormVisibleNotify();
55 std::string left(data, size);
56 std::string right(data, size);
57 formItemInfo.IsEqual(left, right);
58 int64_t formId = static_cast<int64_t>(GetU32Data(data));
59 formItemInfo.SetFormId(formId);
60 std::string packageName(data, size);
61 formItemInfo.SetPackageName(packageName);
62 std::string providerBundleName(data, size);
63 formItemInfo.SetProviderBundleName(providerBundleName);
64 std::string hostBundleName(data, size);
65 formItemInfo.SetHostBundleName(hostBundleName);
66 formItemInfo.SetModuleName(moduleName);
67 std::string abilityName(data, size);
68 formItemInfo.SetAbilityName(abilityName);
69 std::string formName(data, size);
70 formItemInfo.SetFormName(formName);
71 std::string jsComponentName(data, size);
72 formItemInfo.SetJsComponentName(jsComponentName);
73 std::string abilityModuleName(data, size);
74 formItemInfo.SetAbilityModuleName(abilityModuleName);
75 int specificationId = static_cast<int>(GetU32Data(data));
76 formItemInfo.SetSpecificationId(specificationId);
77 bool isEnableUpdateFlag = *data % ENABLE;
78 formItemInfo.SetEnableUpdateFlag(isEnableUpdateFlag);
79 int updateDuration = static_cast<int>(GetU32Data(data));
80 formItemInfo.SetUpdateDuration(updateDuration);
81 std::string scheduledUpdateTime(data, size);
82 formItemInfo.SetScheduledUpdateTime(scheduledUpdateTime);
83 std::string hapSourceDir(data, size);
84 formItemInfo.AddHapSourceDirs(hapSourceDir);
85 formItemInfo.GetFormId();
86 formItemInfo.GetPackageName();
87 formItemInfo.GetProviderBundleName();
88 formItemInfo.GetHostBundleName();
89 formItemInfo.GetModuleName();
90 formItemInfo.GetAbilityName();
91 formItemInfo.GetFormName();
92 formItemInfo.GetJsComponentName();
93 formItemInfo.GetAbilityModuleName();
94 formItemInfo.GetSpecificationId();
95 formItemInfo.GetUpdateDuration();
96 formItemInfo.GetScheduledUpdateTime();
97 bool temporaryFlag = *data % ENABLE;
98 formItemInfo.SetTemporaryFlag(temporaryFlag);
99 std::string moduleSourceDir(data, size);
100 formItemInfo.AddModuleInfo(moduleName, moduleSourceDir);
101 return formItemInfo.IsEnableUpdateFlag();
102 }
103 }
104
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
107 {
108 /* Run your code on data */
109 if (data == nullptr) {
110 return 0;
111 }
112
113 if (size < OHOS::U32_AT_SIZE) {
114 return 0;
115 }
116
117 /* Validate the length of size */
118 if (size == 0 || size > OHOS::FOO_MAX_LEN) {
119 return 0;
120 }
121
122 char* ch = (char *)malloc(size + 1);
123 if (ch == nullptr) {
124 return 0;
125 }
126
127 (void)memset_s(ch, size + 1, 0x00, size + 1);
128 if (memcpy_s(ch, size, data, size) != EOK) {
129 free(ch);
130 ch = nullptr;
131 return 0;
132 }
133
134 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
135 free(ch);
136 ch = nullptr;
137 return 0;
138 }
139
140