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 "updateshadowpic_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 "UpdateShadowPicFuzzTest"
27
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31
32 namespace OHOS {
33 constexpr int32_t MAX_PIXEL_MAP_WIDTH { 600 };
34 constexpr int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
35 constexpr int32_t PIXEL_MAP_WIDTH { 40 };
36 constexpr int32_t PIXEL_MAP_HEIGHT { 40 };
37 constexpr int32_t INT32_BYTE { 4 };
38 constexpr uint32_t DEFAULT_ICON_COLOR { 0xFF };
39
CreatePixelMap(int32_t width,int32_t height)40 std::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, width:%{public}d, height:%{public}d", width, height);
45 return nullptr;
46 }
47 Media::InitializationOptions opts;
48 opts.size.width = width;
49 opts.size.height = height;
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* colors = new (std::nothrow) uint32_t[colorLen];
56 if (colors == nullptr) {
57 FI_HILOGE("colorPixels is nullptr");
58 return nullptr;
59 }
60 int32_t colorByteCount = colorLen * INT32_BYTE;
61 auto ret = memset_s(colors, colorByteCount, DEFAULT_ICON_COLOR, colorByteCount);
62 if (ret != EOK) {
63 FI_HILOGE("memset_s failed");
64 delete[] colors;
65 return nullptr;
66 }
67 std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMap::Create(colors, colorLen, opts);
68 if (pixelMap == nullptr) {
69 FI_HILOGE("Create pixelMap failed");
70 delete[] colors;
71 return nullptr;
72 }
73 delete[] colors;
74 return pixelMap;
75 }
76
UpdateShadowPicFuzzTest(const uint8_t * data,size_t size)77 bool UpdateShadowPicFuzzTest(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("Write failed");
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("Write failed");
92 return false;
93 }
94 MessageParcel reply;
95 MessageOption option;
96 DelayedSingleton<DeviceStatusService>::GetInstance()->OnRemoteRequest(
97 static_cast<uint32_t>(Msdp::DeviceInterfaceCode::UPDATE_SHADOW_PIC), datas, reply, option);
98 return true;
99 }
100 } // namespace OHOS
101
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)102 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
103 {
104 /* Run your code on data */
105 if (data == nullptr) {
106 return 0;
107 }
108
109 OHOS::UpdateShadowPicFuzzTest(data, size);
110 return 0;
111 }
112 } // namespace DeviceStatus
113 } // namespace Msdp
114 } // namespace OHOS
115