• 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 "geoconvertservice_fuzzer.h"
17 
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26 #include "locationhub_ipc_interface_code.h"
27 
28 #include "location.h"
29 #include "locator_ability.h"
30 #ifdef FEATURE_GEOCODE_SUPPORT
31 #include "geo_convert_service.h"
32 #endif
33 
34 namespace OHOS {
35 using namespace OHOS::Location;
36 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
37 
ParseData(const uint8_t * data,size_t size)38 char* ParseData(const uint8_t* data, size_t size)
39 {
40     if (data == nullptr) {
41         return nullptr;
42     }
43 
44     if (size > MAX_MEM_SIZE) {
45         return nullptr;
46     }
47 
48     char* ch = (char *)malloc(size + 1);
49     if (ch == nullptr) {
50         return nullptr;
51     }
52 
53     (void)memset_s(ch, size + 1, 0x00, size + 1);
54     if (memcpy_s(ch, size, data, size) != EOK) {
55         free(ch);
56         ch = nullptr;
57         return nullptr;
58     }
59     return ch;
60 }
61 
62 #ifdef FEATURE_GEOCODE_SUPPORT
GeoConvertServiceFuzzTest(const char * data,size_t size)63 bool GeoConvertServiceFuzzTest(const char* data, size_t size)
64 {
65     MessageParcel requestParcel;
66     requestParcel.WriteInterfaceToken(u"location.IGeoConvert");
67     requestParcel.WriteBuffer(data, size);
68     requestParcel.RewindRead(0);
69 
70     MessageParcel reply;
71     MessageOption option;
72 
73     auto service1 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
74     service1->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::IS_AVAILABLE),
75         requestParcel, reply, option);
76     auto service2 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
77     service2->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::GET_FROM_COORDINATE),
78         requestParcel, reply, option);
79     auto service3 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
80     service3->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::GET_FROM_LOCATION_NAME_BY_BOUNDARY),
81         requestParcel, reply, option);
82     auto service4 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
83     service4->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::ENABLE_REVERSE_GEOCODE_MOCK),
84         requestParcel, reply, option);
85     auto service5 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
86     service5->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::DISABLE_REVERSE_GEOCODE_MOCK),
87         requestParcel, reply, option);
88     auto service6 = sptr<GeoConvertService>(new (std::nothrow) GeoConvertService());
89     service6->OnRemoteRequest(static_cast<uint32_t>(GeoConvertInterfaceCode::SET_REVERSE_GEOCODE_MOCKINFO),
90         requestParcel, reply, option);
91 
92     return true;
93 }
94 #endif
95 } // namespace OHOS
96 
97 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)98 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
99 {
100     char* ch = OHOS::ParseData(data, size);
101     if (ch != nullptr) {
102 #ifdef FEATURE_GEOCODE_SUPPORT
103         OHOS::GeoConvertServiceFuzzTest(ch, size);
104 #endif
105         free(ch);
106         ch = nullptr;
107     }
108     return 0;
109 }
110 
111