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