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 "wantfifth_fuzzer.h"
17
18#include <cstddef>
19#include <cstdint>
20#include <iostream>
21
22#define private public
23#include "want.h"
24#undef private
25#include "securec.h"
26
27using namespace OHOS::AAFwk;
28
29namespace OHOS {
30namespace {
31constexpr size_t FOO_MAX_LEN = 1024;
32constexpr size_t U32_AT_SIZE = 4;
33}
34uint32_t GetU32Data(const char* ptr)
35{
36    // convert fuzz input data to an integer
37    return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
38}
39bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
40{
41    std::shared_ptr<Want> want = std::make_shared<Want>();
42    std::string key(data, size);
43    std::vector<zchar> charVector;
44    want->SetParam(key, charVector);
45    want->GetCharArrayParam(key);
46    std::vector<int> intVector;
47    want->SetParam(key, intVector);
48    want->GetIntArrayParam(key);
49    double doubleValue = 0.0;
50    want->SetParam(key, doubleValue);
51    want->GetDoubleParam(key, doubleValue);
52    std::vector<double> doubleVector;
53    want->SetParam(key, doubleVector);
54    want->GetDoubleArrayParam(key);
55    float floatValue = 0.0;
56    want->SetParam(key, floatValue);
57    want->GetFloatParam(key, floatValue);
58    return true;
59}
60}
61
62/* Fuzzer entry point */
63extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
64{
65    /* Run your code on data */
66    if (data == nullptr) {
67        std::cout << "invalid data" << std::endl;
68        return 0;
69    }
70
71    /* Validate the length of size */
72    if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
73        return 0;
74    }
75
76    char* ch = reinterpret_cast<char *>(malloc(size + 1));
77    if (ch == nullptr) {
78        std::cout << "malloc failed." << std::endl;
79        return 0;
80    }
81
82    (void)memset_s(ch, size + 1, 0x00, size + 1);
83    if (memcpy_s(ch, size, data, size) != EOK) {
84        std::cout << "copy failed." << std::endl;
85        free(ch);
86        ch = nullptr;
87        return 0;
88    }
89
90    OHOS::DoSomethingInterestingWithMyAPI(ch, size);
91    free(ch);
92    ch = nullptr;
93    return 0;
94}
95