1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#include "startdrag_fuzzer.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include "singleton.h"
19f857971dSopenharmony_ci
20f857971dSopenharmony_ci#define private public
21f857971dSopenharmony_ci#include "devicestatus_service.h"
22f857971dSopenharmony_ci#include "fi_log.h"
23f857971dSopenharmony_ci#include "message_parcel.h"
24f857971dSopenharmony_ci
25f857971dSopenharmony_ci#undef LOG_TAG
26f857971dSopenharmony_ci#define LOG_TAG "StartDragFuzzTest"
27f857971dSopenharmony_ci
28f857971dSopenharmony_cinamespace OHOS {
29f857971dSopenharmony_cinamespace Msdp {
30f857971dSopenharmony_cinamespace DeviceStatus {
31f857971dSopenharmony_ci
32f857971dSopenharmony_cinamespace OHOS {
33f857971dSopenharmony_ciconstexpr uint32_t DEFAULT_ICON_COLOR { 0xFF };
34f857971dSopenharmony_ciconstexpr int32_t PIXEL_MAP_WIDTH { 40 };
35f857971dSopenharmony_ciconstexpr int32_t PIXEL_MAP_HEIGHT { 40 };
36f857971dSopenharmony_ciconstexpr int32_t INT32_BYTE { 4 };
37f857971dSopenharmony_ciconstexpr int32_t MAX_PIXEL_MAP_WIDTH { 600 };
38f857971dSopenharmony_ciconstexpr int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
39f857971dSopenharmony_ci
40f857971dSopenharmony_cistd::shared_ptr<Media::PixelMap> CreatePixelMap(int32_t width, int32_t height)
41f857971dSopenharmony_ci{
42f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
43f857971dSopenharmony_ci    if (width <= 0 || width > MAX_PIXEL_MAP_WIDTH || height <= 0 || height > MAX_PIXEL_MAP_HEIGHT) {
44f857971dSopenharmony_ci        FI_HILOGE("Invalid size, height:%{public}d, width:%{public}d", height, width);
45f857971dSopenharmony_ci        return nullptr;
46f857971dSopenharmony_ci    }
47f857971dSopenharmony_ci    Media::InitializationOptions opts;
48f857971dSopenharmony_ci    opts.size.height = height;
49f857971dSopenharmony_ci    opts.size.width = width;
50f857971dSopenharmony_ci    opts.pixelFormat = Media::PixelFormat::BGRA_8888;
51f857971dSopenharmony_ci    opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
52f857971dSopenharmony_ci    opts.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
53f857971dSopenharmony_ci
54f857971dSopenharmony_ci    int32_t colorLen = width * height;
55f857971dSopenharmony_ci    uint32_t* colorPixels = new (std::nothrow) uint32_t[colorLen];
56f857971dSopenharmony_ci    if (colorPixels == nullptr) {
57f857971dSopenharmony_ci        FI_HILOGE("colorPixels is nullptr");
58f857971dSopenharmony_ci        return nullptr;
59f857971dSopenharmony_ci    }
60f857971dSopenharmony_ci    int32_t colorByteCount = colorLen * INT32_BYTE;
61f857971dSopenharmony_ci    auto ret = memset_s(colorPixels, colorByteCount, DEFAULT_ICON_COLOR, colorByteCount);
62f857971dSopenharmony_ci    if (ret != EOK) {
63f857971dSopenharmony_ci        delete[] colorPixels;
64f857971dSopenharmony_ci        FI_HILOGE("memset_s failed");
65f857971dSopenharmony_ci        return nullptr;
66f857971dSopenharmony_ci    }
67f857971dSopenharmony_ci    std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMap::Create(colorPixels, colorLen, opts);
68f857971dSopenharmony_ci    if (pixelMap == nullptr) {
69f857971dSopenharmony_ci        delete[] colorPixels;
70f857971dSopenharmony_ci        FI_HILOGE("Create pixelMap failed");
71f857971dSopenharmony_ci        return nullptr;
72f857971dSopenharmony_ci    }
73f857971dSopenharmony_ci    delete[] colorPixels;
74f857971dSopenharmony_ci    return pixelMap;
75f857971dSopenharmony_ci}
76f857971dSopenharmony_ci
77f857971dSopenharmony_cibool StartDragFuzzTest(const uint8_t* data, size_t size)
78f857971dSopenharmony_ci{
79f857971dSopenharmony_ci    const std::u16string FORMMGR_DEVICE_TOKEN { u"ohos.msdp.Idevicestatus" };
80f857971dSopenharmony_ci    MessageParcel datas;
81f857971dSopenharmony_ci    if (!datas.WriteInterfaceToken(FORMMGR_DEVICE_TOKEN)) {
82f857971dSopenharmony_ci        FI_HILOGE("Failed to write interface token");
83f857971dSopenharmony_ci        return false;
84f857971dSopenharmony_ci    }
85f857971dSopenharmony_ci    auto pixelMap = CreatePixelMap(PIXEL_MAP_WIDTH, PIXEL_MAP_HEIGHT);
86f857971dSopenharmony_ci    if (!pixelMap->Marshalling(datas)) {
87f857971dSopenharmony_ci        FI_HILOGE("Failed to marshalling pixelMap");
88f857971dSopenharmony_ci        return false;
89f857971dSopenharmony_ci    }
90f857971dSopenharmony_ci    if (!datas.WriteBuffer(data, size) || !datas.RewindRead(0)) {
91f857971dSopenharmony_ci        FI_HILOGE("Failed to write buffer");
92f857971dSopenharmony_ci        return false;
93f857971dSopenharmony_ci    }
94f857971dSopenharmony_ci    MessageOption option;
95f857971dSopenharmony_ci    MessageParcel reply;
96f857971dSopenharmony_ci    DelayedSingleton<DeviceStatusService>::GetInstance()->OnRemoteRequest(
97f857971dSopenharmony_ci        static_cast<uint32_t>(Msdp::DeviceInterfaceCode::START_DRAG), datas, reply, option);
98f857971dSopenharmony_ci    return true;
99f857971dSopenharmony_ci}
100f857971dSopenharmony_ci} // namespace OHOS
101f857971dSopenharmony_ci
102f857971dSopenharmony_ci/* Fuzzer entry point */
103f857971dSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
104f857971dSopenharmony_ci{
105f857971dSopenharmony_ci    /* Run your code on data */
106f857971dSopenharmony_ci    if (data == nullptr) {
107f857971dSopenharmony_ci        return 0;
108f857971dSopenharmony_ci    }
109f857971dSopenharmony_ci
110f857971dSopenharmony_ci    OHOS::StartDragFuzzTest(data, size);
111f857971dSopenharmony_ci    return 0;
112f857971dSopenharmony_ci}
113f857971dSopenharmony_ci} // namespace DeviceStatus
114f857971dSopenharmony_ci} // namespace Msdp
115f857971dSopenharmony_ci} // namespace OHOS
116