1 /*
2 * Copyright (c) 2025 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 "medialibrary_ptpspecialhandles_fuzzer.h"
16
17 #include <cstdint>
18 #include <string>
19 #include <fstream>
20 #include <vector>
21 #include <fuzzer/FuzzedDataProvider.h>
22
23 #include "system_ability_definition.h"
24 #include "iservice_registry.h"
25 #include "userfilemgr_uri.h"
26 #include "payload_data.h"
27 #include "close_session_data.h"
28 #include "media_log.h"
29
30 #define private public
31 #include "ptp_special_handles.h"
32 #undef private
33
34 namespace OHOS {
35 using namespace std;
36 using namespace Media;
37
38 FuzzedDataProvider *provider = nullptr;
39
PtpSpecialHandlesTest(const uint8_t * data,size_t size)40 static void PtpSpecialHandlesTest(const uint8_t* data, size_t size)
41 {
42 auto specialInstance = PtpSpecialHandles::GetInstance();
43 uint32_t deleteHandle = provider->ConsumeIntegral<uint32_t>();
44 uint32_t realHandle = provider->ConsumeIntegral<uint32_t>();
45 specialInstance->AddHandleToMap(deleteHandle, realHandle);
46 deleteHandle = provider->ConsumeIntegral<uint32_t>();
47 realHandle = provider->ConsumeIntegral<uint32_t>();
48 specialInstance->AddHandleToMap(deleteHandle, realHandle);
49 uint32_t key = provider->ConsumeIntegral<uint32_t>();
50
51 specialInstance->HandleConvertToAdded(key);
52 specialInstance->FindRealHandle(key);
53 specialInstance->HandleConvertToDeleted(key);
54 specialInstance->FindDeletedHandle(key);
55 specialInstance->ClearDeletedHandles();
56 }
57
58 } // namespace OHOS
59
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62 {
63 /* Run your code on data */
64 if (data == nullptr) {
65 return 0;
66 }
67
68 FuzzedDataProvider fdp(data, size);
69 OHOS::provider = &fdp;
70
71 OHOS::PtpSpecialHandlesTest(data, size);
72 return 0;
73 }
74