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 #include "countrycodecallbackhost_fuzzer.h"
16 
17 #include "accesstoken_kit.h"
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22 #include "nativetoken_kit.h"
23 #include "system_ability_definition.h"
24 #include "token_setproc.h"
25 
26 #include "common_utils.h"
27 #include "constant_definition.h"
28 #include "country_code_callback_napi.h"
29 #include "i_locator_callback.h"
30 #include "location.h"
31 #include "locator_ability.h"
32 
33 namespace OHOS {
34 using namespace OHOS::Location;
35 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
36 
ParseData(const uint8_t * data,size_t size)37 char* ParseData(const uint8_t* data, size_t size)
38 {
39     if (data == nullptr) {
40         return nullptr;
41     }
42 
43     if (size > MAX_MEM_SIZE) {
44         return nullptr;
45     }
46 
47     char* ch = (char *)malloc(size + 1);
48     if (ch == nullptr) {
49         return nullptr;
50     }
51 
52     (void)memset_s(ch, size + 1, 0x00, size + 1);
53     if (memcpy_s(ch, size, data, size) != EOK) {
54         free(ch);
55         ch = nullptr;
56         return nullptr;
57     }
58     return ch;
59 }
60 
CountryCodeCallbackHostFuzzTest(const char * data,size_t size)61 bool CountryCodeCallbackHostFuzzTest(const char* data, size_t size)
62 {
63     MessageParcel requestParcel;
64     requestParcel.WriteInterfaceToken(u"location.ICountryCodeCallback");
65     requestParcel.WriteBuffer(data, size);
66     requestParcel.RewindRead(0);
67 
68     MessageParcel reply;
69     MessageOption option;
70     auto callback = sptr<CountryCodeCallbackNapi>(new (std::nothrow) CountryCodeCallbackNapi());
71     callback->OnRemoteRequest(CountryCodeCallbackNapi::COUNTRY_CODE_CHANGE_EVENT, requestParcel, reply, option);
72     return true;
73 }
74 } // namespace OHOS
75 
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79     char* ch = OHOS::ParseData(data, size);
80     if (ch != nullptr) {
81         OHOS::CountryCodeCallbackHostFuzzTest(ch, size);
82         free(ch);
83         ch = nullptr;
84     }
85     return 0;
86 }
87 
88