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 "downloadassetcbmgr_fuzzer.h"
16
17 #include <cstddef>
18 #include <cstdint>
19
20 #include "cloud_fuzzer_helper.h"
21 #include "download_asset_callback_manager.h"
22 #include "iremote_stub.h"
23
24 namespace OHOS {
25 constexpr size_t U32_AT_SIZE = 4;
26 constexpr size_t U64_AT_SIZE = 8;
27
28 using namespace std;
29 using namespace OHOS::FileManagement::CloudSync;
30
31 class IDownloadAssetCallbackTest : public IRemoteStub<IDownloadAssetCallback> {
32 public:
33 using TaskId = uint64_t;
OnFinished(const TaskId taskId,const std::string & uri,const int32_t result)34 void OnFinished(const TaskId taskId, const std::string &uri, const int32_t result) override {}
35 };
36
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)37 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
38 {
39 FuzzData fuzzData(data, size);
40 sptr<IDownloadAssetCallbackTest> callback = new (std::nothrow) IDownloadAssetCallbackTest();
41 if (callback == nullptr) {
42 return false;
43 }
44 uint64_t taskId = fuzzData.GetData<uint64_t>();
45 int32_t result = fuzzData.GetData<int32_t>();
46 size_t remainSize = fuzzData.GetRemainSize();
47 string uri = fuzzData.GetStringFromData(static_cast<int>(remainSize));
48 DownloadAssetCallbackManager::GetInstance().AddCallback(callback);
49 DownloadAssetCallbackManager::GetInstance().OnDownloadFinshed(taskId, uri, result);
50 return true;
51 }
52 } // namespace OHOS
53
54 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
56 {
57 /* Run your code on data */
58 if (data == nullptr || size <= OHOS::U32_AT_SIZE + OHOS::U64_AT_SIZE) {
59 return 0;
60 }
61
62 OHOS::DoSomethingInterestingWithMyAPI(data, size);
63 return 0;
64 }