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 "resmgr_fuzzer.h"
17
18 #include <string>
19 #include <securec.h>
20 #include <vector>
21 #include "resource_manager.h"
22
23 #undef private
24
25 using namespace std;
26 using namespace OHOS::Global::Resource;
27
28 namespace OHOS {
GetStringByIdFuzzTest(const char * data,size_t size,ResourceManager * rm)29 bool GetStringByIdFuzzTest(const char* data, size_t size, ResourceManager *rm)
30 {
31 bool result = false;
32 if (size > 0) {
33 uint32_t testId = reinterpret_cast<uint32_t>(data);
34 std::string extraInfo = "";
35 result = rm->GetStringById(testId, extraInfo);
36 }
37 return result;
38 }
39
GetStringArrayByIdFuzzTest(const char * data,size_t size,ResourceManager * rm)40 bool GetStringArrayByIdFuzzTest(const char* data, size_t size, ResourceManager *rm)
41 {
42 bool result = false;
43 if (size > 0) {
44 uint32_t testId = reinterpret_cast<uint32_t>(data);
45 std::vector<std::string> outValue;
46 result = rm->GetStringArrayById(testId, outValue);
47 }
48 return result;
49 }
50
GetIntegerByIdFuzzTest(const char * data,size_t size,ResourceManager * rm)51 bool GetIntegerByIdFuzzTest(const char* data, size_t size, ResourceManager *rm)
52 {
53 bool result = false;
54 if (size > 0) {
55 uint32_t testId = reinterpret_cast<uint32_t>(data);
56 int outValue;
57 result = rm->GetIntegerById(testId, outValue);
58 }
59 return result;
60 }
61
GetIntegerByNameFuzzTest(const char * data,size_t size,ResourceManager * rm)62 bool GetIntegerByNameFuzzTest(const char* data, size_t size, ResourceManager *rm)
63 {
64 bool result = false;
65 if (size > 0) {
66 std::string testName(reinterpret_cast<const char*>(data), size);
67 int outValue;
68 result = rm->GetIntegerByName(testName, outValue);
69 }
70 return result;
71 }
72
GetBooleanByIdFuzzTest(const char * data,size_t size,ResourceManager * rm)73 bool GetBooleanByIdFuzzTest(const char* data, size_t size, ResourceManager *rm)
74 {
75 bool result = false;
76 if (size > 0) {
77 uint32_t testId = reinterpret_cast<uint32_t>(data);
78 bool outValue;
79 result = rm->GetBooleanById(testId, outValue);
80 }
81 return result;
82 }
83
GetFloatByIdFuzzTest(const char * data,size_t size,ResourceManager * rm)84 bool GetFloatByIdFuzzTest(const char* data, size_t size, ResourceManager *rm)
85 {
86 bool result = false;
87 if (size > 0) {
88 uint32_t testId = reinterpret_cast<uint32_t>(data);
89 float outValue;
90 result = rm->GetFloatById(testId, outValue);
91 }
92 return result;
93 }
94
GetMediaByIdFuzzTest(const char * data,size_t size,ResourceManager * rm)95 bool GetMediaByIdFuzzTest(const char* data, size_t size, ResourceManager *rm)
96 {
97 bool result = false;
98 if (size > 0) {
99 uint32_t testId = reinterpret_cast<uint32_t>(data);
100 std::string outValue = "";
101 result = rm->GetMediaById(testId, outValue);
102 }
103 return result;
104 }
105
GetMediaByNameFuzzTest(const char * data,size_t size,ResourceManager * rm)106 bool GetMediaByNameFuzzTest(const char* data, size_t size, ResourceManager *rm)
107 {
108 bool result = false;
109 if (size > 0) {
110 std::string testName(reinterpret_cast<const char*>(data), size);
111 std::string outValue = "";
112 result = rm->GetMediaByName(testName, outValue);
113 }
114 return result;
115 }
116
GetStringFormatByIdFuzzTest(const char * data,size_t size,ResourceManager * rm)117 bool GetStringFormatByIdFuzzTest(const char* data, size_t size, ResourceManager *rm)
118 {
119 bool result = false;
120 if (size > 0) {
121 uint32_t testId = reinterpret_cast<uint32_t>(data);
122 std::string outValue = "";
123 result = rm->GetStringFormatById(outValue, testId);
124 }
125 return result;
126 }
127
GetColorByIdFuzzTest(const char * data,size_t size,ResourceManager * rm)128 bool GetColorByIdFuzzTest(const char* data, size_t size, ResourceManager *rm)
129 {
130 bool result = false;
131 if (size > 0) {
132 uint32_t testId = reinterpret_cast<uint32_t>(data);
133 uint32_t outValue = 0;
134 result = rm->GetColorById(testId, outValue);
135 }
136 return result;
137 }
138
ResourceManagerImplFuzzTest(const char * data,size_t size)139 void ResourceManagerImplFuzzTest(const char* data, size_t size)
140 {
141 ResourceManager *rm = CreateResourceManager();
142 if (rm == nullptr) {
143 return;
144 }
145 GetStringByIdFuzzTest(data, size, rm);
146 GetStringArrayByIdFuzzTest(data, size, rm);
147 GetIntegerByIdFuzzTest(data, size, rm);
148 GetIntegerByNameFuzzTest(data, size, rm);
149 GetBooleanByIdFuzzTest(data, size, rm);
150 GetFloatByIdFuzzTest(data, size, rm);
151 GetMediaByIdFuzzTest(data, size, rm);
152 GetMediaByNameFuzzTest(data, size, rm);
153 GetStringFormatByIdFuzzTest(data, size, rm);
154 GetColorByIdFuzzTest(data, size, rm);
155 delete rm;
156 return;
157 }
158 }
159
160 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)161 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
162 {
163 /* Run your code on data */
164 if (data == nullptr) {
165 return 0;
166 }
167
168 char* ch = (char *)malloc(size + 1);
169 if (ch == nullptr) {
170 return 0;
171 }
172
173 (void)memset_s(ch, size, 0x00, size);
174 if (memcpy_s(ch, size, data, size) != 0) {
175 free(ch);
176 ch = nullptr;
177 return 0;
178 }
179 OHOS::ResourceManagerImplFuzzTest(ch, size);
180 free(ch);
181 ch = nullptr;
182 return 0;
183 }
184
185