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 #include "downloadassetcallbackstub_fuzzer.h"
16 
17 #include <cstddef>
18 #include <cstdint>
19 
20 #include "cloud_fuzzer_helper.h"
21 #include "download_asset_callback_client.h"
22 #include "download_asset_callback_stub.h"
23 
24 #include "message_parcel.h"
25 #include "utils_log.h"
26 
27 namespace OHOS {
28 constexpr size_t U32_AT_SIZE = 4;
29 constexpr size_t U64_AT_SIZE = 8;
30 
31 using namespace OHOS::FileManagement::CloudSync;
32 using namespace std;
33 
WriteInterfaceToken(MessageParcel & datas)34 bool WriteInterfaceToken(MessageParcel &datas)
35 {
36     if (!datas.WriteInterfaceToken(DownloadAssetCallbackStub::GetDescriptor())) {
37         LOGE("Write token failed.");
38         return false;
39     }
40     return true;
41 }
42 
HandleOnFinishedFuzzTest(std::shared_ptr<DownloadAssetCallbackStub> downloadAssetCallbackStubStr,const uint8_t * data,size_t size)43 void HandleOnFinishedFuzzTest(std::shared_ptr<DownloadAssetCallbackStub> downloadAssetCallbackStubStr,
44                               const uint8_t *data,
45                               size_t size)
46 {
47     FuzzData fuzzData(data, size);
48     MessageParcel datas;
49     if (!WriteInterfaceToken(datas)) {
50         return;
51     }
52     int32_t taskId = fuzzData.GetData<uint64_t>();
53     datas.WriteUint64(taskId);
54     int32_t result = fuzzData.GetData<int32_t>();
55     string uri = fuzzData.GetStringFromData(static_cast<int>(size - U32_AT_SIZE - U64_AT_SIZE));
56     datas.WriteString(uri);
57     datas.WriteInt32(result);
58     datas.RewindRead(0);
59     // SERVICE_CMD_ON_DOWNLOAD_FINSHED
60     uint32_t code = 0;
61     MessageParcel reply;
62     MessageOption option;
63 
64     downloadAssetCallbackStubStr->OnRemoteRequest(code, datas, reply, option);
65 }
66 } // namespace OHOS
67 
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
70 {
71     /* Run your code on data */
72     if (data == nullptr || size < (OHOS::U32_AT_SIZE + OHOS::U64_AT_SIZE)) {
73         return 0;
74     }
75 
76     auto downloadAssetCallbackStubStr =
77         std::make_shared<OHOS::FileManagement::CloudSync::DownloadAssetCallbackClient>();
78     OHOS::HandleOnFinishedFuzzTest(downloadAssetCallbackStubStr, data, size);
79     return 0;
80 }
81