1 /*
2 * Copyright (c) 2024 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 "hstream_metadata_callback_stub_fuzzer.h"
17 #include "camera_log.h"
18 #include "hstream_capture.h"
19 #include "iservice_registry.h"
20 #include "message_parcel.h"
21 #include "accesstoken_kit.h"
22 #include "camera_metadata_info.h"
23 #include "metadata_utils.h"
24 #include "iconsumer_surface.h"
25 #include "securec.h"
26
27 namespace OHOS {
28 namespace CameraStandard {
29 static constexpr int32_t MIN_SIZE_NUM = 44;
30 static const size_t MAX_BUFFER_SIZE = 32;
31 std::shared_ptr<HStreamMetadataCallbackStubFuzz> HStreamMetadataCallbackStubFuzzer::fuzz_{nullptr};
32
33 /*
34 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
35 * tips: only support basic type
36 */
37
HStreamMetadataCallbackStubFuzzerTest(FuzzedDataProvider & fdp)38 void HStreamMetadataCallbackStubFuzzer::HStreamMetadataCallbackStubFuzzerTest(FuzzedDataProvider& fdp)
39 {
40 fuzz_ = std::make_shared<HStreamMetadataCallbackStubFuzz>();
41 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
42 MessageParcel reply;
43 MessageOption option;
44 MessageParcel dataMessageParcel;
45 dataMessageParcel.WriteInterfaceToken(HStreamMetadataCallbackStubFuzz::GetDescriptor());
46 size_t bufferSize = fdp.ConsumeIntegralInRange<size_t>(0, MAX_BUFFER_SIZE);
47 std::vector<uint8_t> buffer = fdp.ConsumeBytes<uint8_t>(bufferSize);
48 dataMessageParcel.WriteBuffer(buffer.data(), buffer.size());
49 dataMessageParcel.RewindRead(0);
50 int32_t code = fdp.ConsumeIntegral<int32_t>();
51 fuzz_->OnRemoteRequest(code, dataMessageParcel, reply, option);
52 }
53
Test(uint8_t * data,size_t size)54 void Test(uint8_t* data, size_t size)
55 {
56 auto hstreamMetadataCallbackStub = std::make_unique<HStreamMetadataCallbackStubFuzzer>();
57 CHECK_RETURN_ELOG(hstreamMetadataCallbackStub == nullptr, "hstreamMetadataCallbackStub is null");
58 FuzzedDataProvider fdp(data, size);
59 CHECK_RETURN(fdp.remaining_bytes() < MIN_SIZE_NUM);
60 hstreamMetadataCallbackStub->HStreamMetadataCallbackStubFuzzerTest(fdp);
61 }
62
63 } // namespace CameraStandard
64 } // namespace OHOS
65
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
68 {
69 OHOS::CameraStandard::Test(data, size);
70 return 0;
71 }