1 /*
2  * Copyright (c) 2022-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 <cstddef>
17 #include <cstdint>
18 #include <iostream>
19 #include <memory>
20 
21 #include "avsession_callback_proxy.h"
22 #include "iavcontroller_callback.h"
23 #include "iremote_proxy.h"
24 #include "avsession_log.h"
25 #include "avsession_errors.h"
26 #include "avsession_callback_proxy_fuzzer.h"
27 
28 using namespace std;
29 using namespace OHOS;
30 using namespace OHOS::AVSession;
31 
32 static const int32_t MAX_CODE_LEN = 512;
33 static const int32_t MAX_CODE_TEST = 13;
34 static const int32_t MIN_SIZE_NUM = 4;
35 
FuzzSendRequest(uint8_t * data,size_t size)36 bool AvsessionCallbackProxyFuzzer::FuzzSendRequest(uint8_t* data, size_t size)
37 {
38     if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) {
39         return false;
40     }
41 
42     uint32_t code = *(reinterpret_cast<const uint32_t*>(data));
43     if (code >= MAX_CODE_TEST) {
44         return false;
45     }
46 
47     sptr<IRemoteObject> remoteObject = nullptr;
48     std::shared_ptr<AvsessionCallbackProxyFuzzerTest> avSessionCallbackProxy =
49         std::make_shared<AvsessionCallbackProxyFuzzerTest>(remoteObject);
50     if (!avSessionCallbackProxy) {
51         SLOGI("avSessionCallbackProxy is null");
52         return false;
53     }
54     MessageParcel request;
55     CHECK_AND_PRINT_LOG(request.WriteInterfaceToken(avSessionCallbackProxy->GetDescriptor()),
56         "write interface token failed");
57     MessageParcel reply;
58     MessageOption option = { MessageOption::TF_ASYNC};
59     auto remote = avSessionCallbackProxy->GetRemote();
60     if (!remote) {
61         SLOGI("remote is null");
62         return false;
63     }
64     size -= sizeof(uint32_t);
65     request.WriteBuffer(data + sizeof(uint32_t), size);
66     request.RewindRead(0);
67     int32_t result = AVSESSION_ERROR;
68     CHECK_AND_PRINT_LOG((result = remote->SendRequest(code, request, reply, option)) == 0, "send request failed");
69     return result == AVSESSION_SUCCESS;
70 }
71 
AvsessionCallbackProxySendRequestTest(uint8_t * data,size_t size)72 bool OHOS::AVSession::AvsessionCallbackProxySendRequestTest(uint8_t* data, size_t size)
73 {
74     auto avSessionCallbackProxy = std::make_unique<AvsessionCallbackProxyFuzzer>();
75     if (avSessionCallbackProxy == nullptr) {
76         return false;
77     }
78     return avSessionCallbackProxy->FuzzSendRequest(data, size);
79 }
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     OHOS::AVSession::AvsessionCallbackProxySendRequestTest(const_cast<uint8_t*>(data), size);
86     return 0;
87 }