1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "motion_fuzzer.h"
17094332d3Sopenharmony_ci#include <cstddef>
18094332d3Sopenharmony_ci#include <cstdint>
19094332d3Sopenharmony_ci#include "hdf_log.h"
20094332d3Sopenharmony_ci#include "v1_1/motion_interface_stub.h"
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ciusing namespace OHOS::HDI::Motion::V1_1;
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_cinamespace OHOS {
25094332d3Sopenharmony_ciconstexpr size_t THRESHOLD = 10;
26094332d3Sopenharmony_ciconstexpr int32_t OFFSET = 4;
27094332d3Sopenharmony_ciconstexpr int32_t ARRAY0 = 0;
28094332d3Sopenharmony_ciconstexpr int32_t ARRAY1 = 1;
29094332d3Sopenharmony_ciconstexpr int32_t ARRAY2 = 2;
30094332d3Sopenharmony_ciconstexpr int32_t ARRAY3 = 3;
31094332d3Sopenharmony_ciconstexpr int32_t DIGIT8 = 8;
32094332d3Sopenharmony_ciconstexpr int32_t DIGIT16 = 16;
33094332d3Sopenharmony_ciconstexpr int32_t DIGIT24 = 24;
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_ciconst std::u16string MOTION_INTERFACE_TOKEN = u"ohos.hdi.motion.v1_1.IMotionInterface";
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ciuint32_t Convert2Uint32(const uint8_t* ptr)
38094332d3Sopenharmony_ci{
39094332d3Sopenharmony_ci    if (ptr == nullptr) {
40094332d3Sopenharmony_ci        return 0;
41094332d3Sopenharmony_ci    }
42094332d3Sopenharmony_ci    /*
43094332d3Sopenharmony_ci     * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
44094332d3Sopenharmony_ci     * and the third digit no left
45094332d3Sopenharmony_ci     */
46094332d3Sopenharmony_ci    return (ptr[ARRAY0] << DIGIT24) | (ptr[ARRAY1] << DIGIT16) | (ptr[ARRAY2] << DIGIT8) | (ptr[ARRAY3]);
47094332d3Sopenharmony_ci}
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_cibool DoSomethingInterestingWithMyAPI(const uint8_t* rawData, size_t size)
50094332d3Sopenharmony_ci{
51094332d3Sopenharmony_ci    if (rawData == nullptr) {
52094332d3Sopenharmony_ci        return false;
53094332d3Sopenharmony_ci    }
54094332d3Sopenharmony_ci    uint32_t code = Convert2Uint32(rawData);
55094332d3Sopenharmony_ci    rawData = rawData + OFFSET;
56094332d3Sopenharmony_ci    size = size - OFFSET;
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci    MessageParcel data;
59094332d3Sopenharmony_ci    data.WriteInterfaceToken(MOTION_INTERFACE_TOKEN);
60094332d3Sopenharmony_ci    data.WriteBuffer(rawData, size);
61094332d3Sopenharmony_ci    data.RewindRead(0);
62094332d3Sopenharmony_ci    MessageParcel reply;
63094332d3Sopenharmony_ci    MessageOption option;
64094332d3Sopenharmony_ci    sptr<OHOS::HDI::Motion::V1_1::IMotionInterface> g_motionInterface =
65094332d3Sopenharmony_ci        OHOS::HDI::Motion::V1_1::IMotionInterface::Get(false);
66094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
67094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:IMotionInterface::Get() failed.", __func__);
68094332d3Sopenharmony_ci        return false;
69094332d3Sopenharmony_ci    }
70094332d3Sopenharmony_ci    sptr<OHOS::HDI::Motion::V1_1::MotionInterfaceStub> motionInterface =
71094332d3Sopenharmony_ci        new OHOS::HDI::Motion::V1_1::MotionInterfaceStub(g_motionInterface);
72094332d3Sopenharmony_ci    if (motionInterface == nullptr) {
73094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:new MotionInterfaceStub failed.", __func__);
74094332d3Sopenharmony_ci        return false;
75094332d3Sopenharmony_ci    }
76094332d3Sopenharmony_ci    motionInterface->OnRemoteRequest(code, data, reply, option);
77094332d3Sopenharmony_ci
78094332d3Sopenharmony_ci    return true;
79094332d3Sopenharmony_ci}
80094332d3Sopenharmony_ci}
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci/* Fuzzer entry point */
83094332d3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84094332d3Sopenharmony_ci{
85094332d3Sopenharmony_ci    if (size < OHOS::THRESHOLD) {
86094332d3Sopenharmony_ci        return 0;
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_ci    /* Run your code on data */
90094332d3Sopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(data, size);
91094332d3Sopenharmony_ci    return 0;
92094332d3Sopenharmony_ci}
93094332d3Sopenharmony_ci
94