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