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 #ifdef FEATURE_GEOCODE_SUPPORT
17 #include "geoconvertproxy_fuzzer.h"
18
19 #include "geo_convert_proxy.h"
20 #include "geo_convert_service.h"
21 #include "geocoding_mock_info.h"
22 #include "message_parcel.h"
23 #include "geocode_convert_location_request.h"
24 #include "geocode_convert_address_request.h"
25
26 namespace OHOS {
27 using namespace OHOS::Location;
28 const int MIN_DATA_LEN = 3;
GeoConvertProxyFuzzerTest1(const uint8_t * data,size_t size)29 bool GeoConvertProxyFuzzerTest1(const uint8_t* data, size_t size)
30 {
31 if (size < MIN_DATA_LEN) {
32 return true;
33 }
34 int index = 0;
35 sptr<GeoConvertService> service =
36 new (std::nothrow) GeoConvertService();
37 std::shared_ptr<GeoConvertProxy> geoConvertProxy =
38 std::make_shared<GeoConvertProxy>(service);
39 MessageParcel request;
40 MessageParcel reply;
41 geoConvertProxy->IsGeoConvertAvailable(request);
42 geoConvertProxy->GetAddressByCoordinate(request, reply);
43 geoConvertProxy->GetAddressByLocationName(request, reply);
44 geoConvertProxy->EnableReverseGeocodingMock();
45 geoConvertProxy->DisableReverseGeocodingMock();
46 std::vector<std::shared_ptr<GeocodingMockInfo>> geocodingMockInfos;
47 geoConvertProxy->SetReverseGeocodingMockInfo(geocodingMockInfos);
48 geoConvertProxy->SendSimpleMsgAndParseResult(data[index++]);
49 geoConvertProxy->SendSimpleMsg(data[index++], request);
50 geoConvertProxy->SendMsgWithDataReply(data[index++], request, reply);
51 auto geoConvertRequest = std::make_unique<GeocodeConvertAddressRequest>();
52 MessageParcel convertRequest;
53 geoConvertRequest->SetLocale("zh");
54 geoConvertRequest->SetMaxItems(1);
55 geoConvertRequest->SetDescription("zh");
56 geoConvertRequest->SetMaxLatitude(0.0);
57 geoConvertRequest->SetMaxLongitude(0.0);
58 geoConvertRequest->SetMinLatitude(0.0);
59 geoConvertRequest->SetMinLongitude(0.0);
60 geoConvertRequest->SetTransId("zh");
61 geoConvertRequest->SetCountry("zh");
62 geoConvertRequest->UnmarshallingMessageParcel(convertRequest);
63 geoConvertRequest->GetLocale();
64 geoConvertRequest->GetMaxItems();
65 geoConvertRequest->GetDescription();
66 geoConvertRequest->GetMaxLatitude();
67 geoConvertRequest->GetMaxLongitude();
68 geoConvertRequest->GetMinLatitude();
69 geoConvertRequest->GetMinLongitude();
70 geoConvertRequest->GetTransId();
71 geoConvertRequest->GetCountry();
72 return true;
73 }
74
GeoConvertProxyFuzzerTest2(const uint8_t * data,size_t size)75 bool GeoConvertProxyFuzzerTest2(const uint8_t* data, size_t size)
76 {
77 if (size < MIN_DATA_LEN) {
78 return true;
79 }
80 int index = 0;
81 sptr<GeoConvertService> service =
82 new (std::nothrow) GeoConvertService();
83 std::shared_ptr<GeoConvertProxy> geoConvertProxy =
84 std::make_shared<GeoConvertProxy>(service);
85 MessageParcel request;
86 MessageParcel reply;
87 geoConvertProxy->IsGeoConvertAvailable(request);
88 geoConvertProxy->GetAddressByCoordinate(request, reply);
89 geoConvertProxy->GetAddressByLocationName(request, reply);
90 geoConvertProxy->EnableReverseGeocodingMock();
91 geoConvertProxy->DisableReverseGeocodingMock();
92 std::vector<std::shared_ptr<GeocodingMockInfo>> geocodingMockInfos;
93 geoConvertProxy->SetReverseGeocodingMockInfo(geocodingMockInfos);
94 geoConvertProxy->SendSimpleMsgAndParseResult(data[index++]);
95 geoConvertProxy->SendSimpleMsg(data[index++], request);
96 geoConvertProxy->SendMsgWithDataReply(data[index++], request, reply);
97 auto geoConvertRequest = std::make_unique<GeocodeConvertLocationRequest>();
98 MessageParcel convertRequest;
99 geoConvertRequest->SetLocale("zh");
100 geoConvertRequest->SetMaxItems(1);
101 geoConvertRequest->SetTransId("zh");
102 geoConvertRequest->SetCountry("zh");
103 geoConvertRequest->UnmarshallingMessageParcel(convertRequest);
104 geoConvertRequest->GetLocale();
105 geoConvertRequest->GetMaxItems();
106 geoConvertRequest->GetTransId();
107 geoConvertRequest->GetCountry();
108 return true;
109 }
110 }
111
112 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)113 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
114 {
115 /* Run your code on data */
116 OHOS::GeoConvertProxyFuzzerTest1(data, size);
117 OHOS::GeoConvertProxyFuzzerTest2(data, size);
118 return 0;
119 }
120 #endif // FEATURE_GEOCODE_SUPPORT