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 "cachedlocationcallbackhost_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
27#ifdef FEATURE_GNSS_SUPPORT
28#include "cached_locations_callback_napi.h"
29#endif
30#include "common_utils.h"
31#include "constant_definition.h"
32#include "country_code_callback_napi.h"
33#include "i_locator_callback.h"
34#include "location.h"
35#include "locator.h"
36#include "location_switch_callback_napi.h"
37#include "locator.h"
38#include "locator_ability.h"
39#include "locator_callback_napi.h"
40#include "location_log.h"
41
42namespace OHOS {
43using namespace OHOS::Location;
44const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
45
46char* ParseData(const uint8_t* data, size_t size)
47{
48    if (data == nullptr) {
49        return nullptr;
50    }
51
52    if (size > MAX_MEM_SIZE) {
53        return nullptr;
54    }
55
56    char* ch = (char *)malloc(size + 1);
57    if (ch == nullptr) {
58        return nullptr;
59    }
60
61    (void)memset_s(ch, size + 1, 0x00, size + 1);
62    if (memcpy_s(ch, size, data, size) != EOK) {
63        free(ch);
64        ch = nullptr;
65        return nullptr;
66    }
67    return ch;
68}
69
70bool CachedLocationsCallbackHostFuzzTest(const char* data, size_t size)
71{
72    MessageParcel requestParcel;
73    requestParcel.WriteInterfaceToken(u"location.ICachedLocationsCallback");
74    requestParcel.WriteBuffer(data, size);
75    requestParcel.RewindRead(0);
76
77    MessageParcel reply;
78    MessageOption option;
79#ifdef FEATURE_GNSS_SUPPORT
80    auto callback = sptr<CachedLocationsCallbackNapi>(new (std::nothrow) CachedLocationsCallbackNapi());
81    callback->OnRemoteRequest(CachedLocationsCallbackNapi::RECEIVE_CACHED_LOCATIONS_EVENT,
82        requestParcel, reply, option);
83#endif
84    return true;
85}
86} // namespace OHOS
87
88/* Fuzzer entry point */
89extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90{
91    char* ch = OHOS::ParseData(data, size);
92    if (ch != nullptr) {
93        OHOS::CachedLocationsCallbackHostFuzzTest(ch, size);
94        free(ch);
95        ch = nullptr;
96    }
97    return 0;
98}
99
100