1/*
2 * Copyright (c) 2023 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 "startdrag_fuzzer.h"
17
18#include "singleton.h"
19
20#define private public
21#include "devicestatus_service.h"
22#include "fi_log.h"
23#include "message_parcel.h"
24
25#undef LOG_TAG
26#define LOG_TAG "StartDragFuzzTest"
27
28namespace OHOS {
29namespace Msdp {
30namespace DeviceStatus {
31
32namespace OHOS {
33constexpr uint32_t DEFAULT_ICON_COLOR { 0xFF };
34constexpr int32_t PIXEL_MAP_WIDTH { 40 };
35constexpr int32_t PIXEL_MAP_HEIGHT { 40 };
36constexpr int32_t INT32_BYTE { 4 };
37constexpr int32_t MAX_PIXEL_MAP_WIDTH { 600 };
38constexpr int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
39
40std::shared_ptr<Media::PixelMap> CreatePixelMap(int32_t width, int32_t height)
41{
42    CALL_DEBUG_ENTER;
43    if (width <= 0 || width > MAX_PIXEL_MAP_WIDTH || height <= 0 || height > MAX_PIXEL_MAP_HEIGHT) {
44        FI_HILOGE("Invalid size, height:%{public}d, width:%{public}d", height, width);
45        return nullptr;
46    }
47    Media::InitializationOptions opts;
48    opts.size.height = height;
49    opts.size.width = width;
50    opts.pixelFormat = Media::PixelFormat::BGRA_8888;
51    opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
52    opts.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
53
54    int32_t colorLen = width * height;
55    uint32_t* colorPixels = new (std::nothrow) uint32_t[colorLen];
56    if (colorPixels == nullptr) {
57        FI_HILOGE("colorPixels is nullptr");
58        return nullptr;
59    }
60    int32_t colorByteCount = colorLen * INT32_BYTE;
61    auto ret = memset_s(colorPixels, colorByteCount, DEFAULT_ICON_COLOR, colorByteCount);
62    if (ret != EOK) {
63        delete[] colorPixels;
64        FI_HILOGE("memset_s failed");
65        return nullptr;
66    }
67    std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMap::Create(colorPixels, colorLen, opts);
68    if (pixelMap == nullptr) {
69        delete[] colorPixels;
70        FI_HILOGE("Create pixelMap failed");
71        return nullptr;
72    }
73    delete[] colorPixels;
74    return pixelMap;
75}
76
77bool StartDragFuzzTest(const uint8_t* data, size_t size)
78{
79    const std::u16string FORMMGR_DEVICE_TOKEN { u"ohos.msdp.Idevicestatus" };
80    MessageParcel datas;
81    if (!datas.WriteInterfaceToken(FORMMGR_DEVICE_TOKEN)) {
82        FI_HILOGE("Failed to write interface token");
83        return false;
84    }
85    auto pixelMap = CreatePixelMap(PIXEL_MAP_WIDTH, PIXEL_MAP_HEIGHT);
86    if (!pixelMap->Marshalling(datas)) {
87        FI_HILOGE("Failed to marshalling pixelMap");
88        return false;
89    }
90    if (!datas.WriteBuffer(data, size) || !datas.RewindRead(0)) {
91        FI_HILOGE("Failed to write buffer");
92        return false;
93    }
94    MessageOption option;
95    MessageParcel reply;
96    DelayedSingleton<DeviceStatusService>::GetInstance()->OnRemoteRequest(
97        static_cast<uint32_t>(Msdp::DeviceInterfaceCode::START_DRAG), datas, reply, option);
98    return true;
99}
100} // namespace OHOS
101
102/* Fuzzer entry point */
103extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
104{
105    /* Run your code on data */
106    if (data == nullptr) {
107        return 0;
108    }
109
110    OHOS::StartDragFuzzTest(data, size);
111    return 0;
112}
113} // namespace DeviceStatus
114} // namespace Msdp
115} // namespace OHOS
116