• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "vcard_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include <fstream>
24 #include <sstream>
25 
26 #include "addcoreservicetoken_fuzzer.h"
27 #include "datashare_helper.h"
28 #include "datashare_predicates.h"
29 #include "event_runner.h"
30 #include "iservice_registry.h"
31 #include "system_ability_definition.h"
32 #include "telephony_errors.h"
33 #include "telephony_log_wrapper.h"
34 #include "vcard_constructor.h"
35 #include "vcard_manager.h"
36 #include "vcard_utils.h"
37 
38 using namespace OHOS::Telephony;
39 namespace OHOS {
40 constexpr const char *FILE_NAME = "example.vcf";
41 
WriteTestData(const std::string & testStr)42 void WriteTestData(const std::string &testStr)
43 {
44     std::ofstream file;
45     file.open(FILE_NAME, std::ios::out);
46     if (file.is_open()) {
47         std::stringstream ss(testStr);
48         std::string line;
49 
50         while (std::getline(ss, line)) {
51             file << line << std::endl;
52         }
53     }
54     file.close();
55 }
56 
DecodeVcard(const uint8_t * data,size_t size)57 void DecodeVcard(const uint8_t *data, size_t size)
58 {
59     std::string inputString = R"(
60 BEGIN:VCARD
61 VERSION:2.0
62 N;CHARSET=UTF-8:刘;小;;;
63 EMAIL;TYPE=WORK:test@example.com
64 EMAIL;TYPE=HOME:home@example.com
65 EMAIL;TYPE=INTERNET:email@example.com
66 EMAIL;TYPE=PREF:preferred@example.com
67 EMAIL;TYPE=X-CUSTOM:custom@example.com
68 EMAIL;INTERNET:"llll"
69  <test@example.com>
70 END:VCARD
71 )";
72     WriteTestData(inputString);
73     int32_t errorCode;
74     VCardManager::GetInstance().Decode(FILE_NAME, errorCode);
75 }
76 
DecodeVcardData(const uint8_t * data,size_t size)77 void DecodeVcardData(const uint8_t *data, size_t size)
78 {
79     std::string fuzzdata(reinterpret_cast<const char *>(data), size);
80     std::string inputString = R"(
81 BEGIN:VCARD
82 VERSION:2.0
83 N;CHARSET=UTF-8:刘;小;;;
84 EMAIL;TYPE=WORK:test@example.com
85 EMAIL;TYPE=HOME:home@example.com
86 EMAIL;TYPE=INTERNET:email@example.com
87 EMAIL;TYPE=PREF:preferred@example.com
88 EMAIL;TYPE=X-CUSTOM:custom@example.com
89 EMAIL;INTERNET:"llll"
90  <test@example.com>
91 END:VCARD
92 )" + fuzzdata;
93     WriteTestData(inputString);
94     int32_t errorCode;
95     VCardManager::GetInstance().Decode(FILE_NAME, errorCode);
96 }
97 
DecodeVcardRelation(const uint8_t * data,size_t size)98 void DecodeVcardRelation(const uint8_t *data, size_t size)
99 {
100     std::string inputString =
101         "BEGIN:VCARD\r\nVERSION:2.1\r\nX_OHOS_CUSTOM;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:relation;="
102         "E6=B5=8B=E8=AF=95;=E6=B5=8B=E8=AF=95=69=64;=E6=B5=8B=E8=AF=95=6E=61=6D=65\r\nX_OHOS_CUSTOM:"
103         "relation;realationName;labelId;labelName\r\nEND:VCARD\r\n";
104     WriteTestData(inputString);
105     int32_t errorCode;
106     VCardManager::GetInstance().Decode(FILE_NAME, errorCode);
107 }
108 
DecodeVcardRelationData(const uint8_t * data,size_t size)109 void DecodeVcardRelationData(const uint8_t *data, size_t size)
110 {
111     std::string fuzzdata(reinterpret_cast<const char *>(data), size);
112     std::string inputString =
113         "BEGIN:VCARD\r\nVERSION:2.1\r\nX_OHOS_CUSTOM;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:relation;="
114         "E6=B5=8B=E8=AF=95;=E6=B5=8B=E8=AF=95=69=64;=E6=B5=8B=E8=AF=95=6E=61=6D=65\r\nX_OHOS_CUSTOM:"
115         "relation;realationName;labelId;labelName\r\nEND:VCARD\r\n" +
116         fuzzdata;
117     WriteTestData(inputString);
118     int32_t errorCode;
119     VCardManager::GetInstance().Decode(FILE_NAME, errorCode);
120 }
121 
ContructName(const uint8_t * data,size_t size)122 void ContructName(const uint8_t *data, size_t size)
123 {
124     auto nameData = std::make_shared<VCardNameData>();
125     nameData->displayName_ = "test";
126     nameData->family_ = "测试F";
127     nameData->given_ = "wowowo";
128     nameData->middle_ = "测试M";
129     nameData->suffix_ = "wowowoSu";
130     nameData->prefix_ = "测试P";
131     nameData->phoneticFamily_ = "测试FP";
132     nameData->phoneticGiven_ = "测试GV";
133     nameData->phoneticMiddle_ = "wowowowMI";
134     auto contact = std::make_shared<VCardContact>();
135     contact->names_.push_back(nameData);
136     auto constructor = std::make_shared<VCardConstructor>();
137     auto value = constructor->ContactVCard(contact);
138 }
139 
ContructNameData(const uint8_t * data,size_t size)140 void ContructNameData(const uint8_t *data, size_t size)
141 {
142     if (data == nullptr || size == 0) {
143         return;
144     }
145     auto nameData = std::make_shared<VCardNameData>();
146     std::string displayName(reinterpret_cast<const char *>(data), size);
147     nameData->displayName_ = displayName;
148     std::string family(reinterpret_cast<const char *>(data), size);
149     nameData->family_ = family;
150     std::string given(reinterpret_cast<const char *>(data), size);
151     nameData->given_ = given;
152     std::string middle(reinterpret_cast<const char *>(data), size);
153     nameData->middle_ = middle;
154     std::string suffix(reinterpret_cast<const char *>(data), size);
155     nameData->suffix_ = suffix;
156     std::string prefix(reinterpret_cast<const char *>(data), size);
157     nameData->prefix_ = prefix;
158     std::string phoneticFamily(reinterpret_cast<const char *>(data), size);
159     nameData->phoneticFamily_ = phoneticFamily;
160     std::string phoneticGiven(reinterpret_cast<const char *>(data), size);
161     nameData->phoneticGiven_ = phoneticGiven;
162     std::string phoneticMiddle(reinterpret_cast<const char *>(data), size);
163     nameData->phoneticMiddle_ = phoneticMiddle;
164     auto contact = std::make_shared<VCardContact>();
165     contact->names_.push_back(nameData);
166     auto constructor = std::make_shared<VCardConstructor>();
167     auto value = constructor->ContactVCard(contact);
168 }
169 
ContructRelation(const uint8_t * data,size_t size)170 void ContructRelation(const uint8_t *data, size_t size)
171 {
172     auto data1 = std::make_shared<VCardRelationData>();
173     data1->relationName_ = "测试";
174     data1->labelId_ = "测试id";
175     data1->labelName_ = "测试name";
176     auto data2 = std::make_shared<VCardRelationData>();
177     data2->relationName_ = "realationName";
178     data2->labelId_ = "labelId";
179     data2->labelName_ = "labelName";
180     auto contact = std::make_shared<VCardContact>();
181     contact->relations_.push_back(data1);
182     contact->relations_.push_back(data2);
183     auto constructor = std::make_shared<VCardConstructor>();
184     auto value = constructor->ContactVCard(contact);
185 }
186 
ContructRelationData(const uint8_t * data,size_t size)187 void ContructRelationData(const uint8_t *data, size_t size)
188 {
189     auto data1 = std::make_shared<VCardRelationData>();
190     std::string test(reinterpret_cast<const char *>(data), size);
191     data1->relationName_ = test;
192     std::string testId(reinterpret_cast<const char *>(data), size);
193     data1->labelId_ = testId;
194     std::string testName(reinterpret_cast<const char *>(data), size);
195     data1->labelName_ = testName;
196     auto data2 = std::make_shared<VCardRelationData>();
197     std::string realationName(reinterpret_cast<const char *>(data), size);
198     data2->relationName_ = realationName;
199     std::string labelId(reinterpret_cast<const char *>(data), size);
200     data2->labelId_ = labelId;
201     std::string labelName(reinterpret_cast<const char *>(data), size);
202     data2->labelName_ = labelName;
203     auto contact = std::make_shared<VCardContact>();
204     contact->relations_.push_back(data1);
205     contact->relations_.push_back(data2);
206     auto constructor = std::make_shared<VCardConstructor>();
207     auto value = constructor->ContactVCard(contact);
208 }
209 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)210 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
211 {
212     DecodeVcard(data, size);
213     DecodeVcardData(data, size);
214     DecodeVcardRelation(data, size);
215     DecodeVcardRelationData(data, size);
216     ContructName(data, size);
217     ContructNameData(data, size);
218     ContructRelation(data, size);
219     ContructRelationData(data, size);
220     return;
221 }
222 } // namespace OHOS
223 
224 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)225 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
226 {
227     /* Run your code on data */
228     OHOS::DoSomethingInterestingWithMyAPI(data, size);
229     return 0;
230 }
231