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 "controlcamera_fuzzer.h"
17
18#include <cstddef>
19#include <cstdint>
20#define private public
21#include "addcalltoken_fuzzer.h"
22#include "surface_utils.h"
23
24using namespace OHOS::Telephony;
25namespace OHOS {
26
27int32_t ControlCamera(const uint8_t *data, size_t size)
28{
29    if (!IsServiceInited()) {
30        return TELEPHONY_ERROR;
31    }
32    int32_t callId = static_cast<int32_t>(size);
33    std::string cameraId(reinterpret_cast<const char *>(data), size);
34    auto cameraIdU16 = Str8ToStr16(cameraId);
35    MessageParcel dataParcel;
36    dataParcel.WriteInt32(callId);
37    dataParcel.WriteString16(cameraIdU16);
38    dataParcel.RewindRead(0);
39    MessageParcel reply;
40    return DelayedSingleton<CallManagerService>::GetInstance()->OnControlCamera(dataParcel, reply);
41}
42
43int32_t SetPreviewWindow(const uint8_t *data, size_t size)
44{
45    if (!IsServiceInited()) {
46        return TELEPHONY_ERROR;
47    }
48    int32_t callId = static_cast<int32_t>(size);
49    std::string surfaceId(reinterpret_cast<const char *>(data), size);
50    MessageParcel dataParcel;
51    dataParcel.WriteInt32(callId);
52    int len = static_cast<int>(surfaceId.length());
53    std::string subSurfaceId = surfaceId;
54    if (len >= 1) {
55        subSurfaceId = surfaceId.substr(0, 1);
56    }
57    if (subSurfaceId.empty() || subSurfaceId[0] < '0' || subSurfaceId[0] > '9') {
58        subSurfaceId = "";
59        dataParcel.WriteString(subSurfaceId);
60    } else {
61        dataParcel.WriteString(subSurfaceId);
62        uint64_t tmpSurfaceId = std::stoull(subSurfaceId);
63        auto surface = SurfaceUtils::GetInstance()->GetSurface(tmpSurfaceId);
64        if (surface != nullptr) {
65            sptr<IBufferProducer> producer = surface->GetProducer();
66            if (producer != nullptr) {
67                dataParcel.WriteRemoteObject(producer->AsObject());
68            }
69        }
70    }
71    MessageParcel reply;
72    return DelayedSingleton<CallManagerService>::GetInstance()->OnSetPreviewWindow(dataParcel, reply);
73}
74
75int32_t SetDisplayWindow(const uint8_t *data, size_t size)
76{
77    if (!IsServiceInited()) {
78        return TELEPHONY_ERROR;
79    }
80    int32_t callId = static_cast<int32_t>(size);
81    std::string surfaceId(reinterpret_cast<const char *>(data), size);
82    MessageParcel dataParcel;
83    dataParcel.WriteInt32(callId);
84    int len = static_cast<int>(surfaceId.length());
85    std::string subSurfaceId = surfaceId;
86    if (len >= 1) {
87        subSurfaceId = surfaceId.substr(0, 1);
88    }
89    if (subSurfaceId.empty() || subSurfaceId[0] < '0' || subSurfaceId[0] > '9') {
90        subSurfaceId = "";
91        dataParcel.WriteString(subSurfaceId);
92    } else {
93        dataParcel.WriteString(subSurfaceId);
94        uint64_t tmpSurfaceId = std::stoull(subSurfaceId);
95        auto surface = SurfaceUtils::GetInstance()->GetSurface(tmpSurfaceId);
96        if (surface != nullptr) {
97            sptr<IBufferProducer> producer = surface->GetProducer();
98            if (producer != nullptr) {
99                dataParcel.WriteRemoteObject(producer->AsObject());
100            }
101        }
102    }
103    MessageParcel reply;
104    return DelayedSingleton<CallManagerService>::GetInstance()->OnSetDisplayWindow(dataParcel, reply);
105}
106
107int32_t SetCameraZoom(const uint8_t *data, size_t size)
108{
109    if (!IsServiceInited()) {
110        return TELEPHONY_ERROR;
111    }
112    float zoomRatio = static_cast<float>(size);
113    MessageParcel dataParcel;
114    dataParcel.WriteFloat(zoomRatio);
115    dataParcel.WriteBuffer(data, size);
116    dataParcel.RewindRead(0);
117    MessageParcel reply;
118    return DelayedSingleton<CallManagerService>::GetInstance()->OnSetCameraZoom(dataParcel, reply);
119}
120
121int32_t SetPausePicture(const uint8_t *data, size_t size)
122{
123    if (!IsServiceInited()) {
124        return TELEPHONY_ERROR;
125    }
126    int32_t callId = static_cast<int32_t>(size);
127    std::string path(reinterpret_cast<const char *>(data), size);
128    auto pathU16 = Str8ToStr16(path);
129    MessageParcel dataParcel;
130    dataParcel.WriteInt32(callId);
131    dataParcel.WriteString16(pathU16);
132    dataParcel.RewindRead(0);
133    MessageParcel reply;
134    return DelayedSingleton<CallManagerService>::GetInstance()->OnSetPausePicture(dataParcel, reply);
135}
136
137int32_t SetDeviceDirection(const uint8_t *data, size_t size)
138{
139    if (!IsServiceInited()) {
140        return TELEPHONY_ERROR;
141    }
142    int32_t callId = static_cast<int32_t>(size);
143    int32_t rotation = static_cast<int32_t>(size);
144    MessageParcel dataParcel;
145    dataParcel.WriteInt32(callId);
146    dataParcel.WriteInt32(rotation);
147    MessageParcel reply;
148    return DelayedSingleton<CallManagerService>::GetInstance()->OnSetDeviceDirection(dataParcel, reply);
149}
150
151int32_t CancelCallUpgrade(const uint8_t *data, size_t size)
152{
153    if (!IsServiceInited()) {
154        return TELEPHONY_ERROR;
155    }
156    int32_t callId = static_cast<int32_t>(size);
157    MessageParcel dataParcel;
158    dataParcel.WriteInt32(callId);
159    MessageParcel reply;
160    return DelayedSingleton<CallManagerService>::GetInstance()->OnCancelCallUpgrade(dataParcel, reply);
161}
162
163int32_t RequestCameraCapabilities(const uint8_t *data, size_t size)
164{
165    if (!IsServiceInited()) {
166        return TELEPHONY_ERROR;
167    }
168    int32_t callId = static_cast<int32_t>(size);
169    MessageParcel dataParcel;
170    dataParcel.WriteInt32(callId);
171    MessageParcel reply;
172    return DelayedSingleton<CallManagerService>::GetInstance()->OnRequestCameraCapabilities(dataParcel, reply);
173}
174
175void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
176{
177    if (data == nullptr || size == 0) {
178        return;
179    }
180    ControlCamera(data, size);
181    SetPreviewWindow(data, size);
182    SetDisplayWindow(data, size);
183    SetCameraZoom(data, size);
184    SetPausePicture(data, size);
185    SetDeviceDirection(data, size);
186    CancelCallUpgrade(data, size);
187    RequestCameraCapabilities(data, size);
188}
189} // namespace OHOS
190
191/* Fuzzer entry point */
192extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
193{
194    OHOS::AddCallTokenFuzzer token;
195    /* Run your code on data */
196    OHOS::DoSomethingInterestingWithMyAPI(data, size);
197    return 0;
198}
199