• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "medialibrarycloudmediadatahandler_fuzzer.h"
17 
18 #include <cstdint>
19 #include <string>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #define private public
22 #include "cloud_media_data_handler.h"
23 #undef private
24 #include "media_log.h"
25 
26 #include "ability_context_impl.h"
27 #include "medialibrary_command.h"
28 #include "medialibrary_data_manager.h"
29 #include "medialibrary_rdbstore.h"
30 #include "medialibrary_type_const.h"
31 #include "medialibrary_unistore_manager.h"
32 #include "photo_album_column.h"
33 #include "medialibrary_kvstore_manager.h"
34 
35 namespace OHOS {
36 using namespace std;
37 using namespace OHOS::Media;
38 using namespace OHOS::Media::CloudSync;
39 const int32_t NUM_BYTES = 1;
40 std::shared_ptr<Media::MediaLibraryRdbStore> g_rdbStore;
41 FuzzedDataProvider* provider;
42 
InitCloudMediaDataHandlerFuzzer()43 static shared_ptr<CloudMediaDataHandler> InitCloudMediaDataHandlerFuzzer()
44 {
45     MEDIA_INFO_LOG("InitCloudMediaDataHandlerFuzzer enter");
46     string tableName = "PhotoAlbum";
47     int32_t cloudType = provider->ConsumeIntegral<int32_t>();
48     MEDIA_INFO_LOG("InitCloudMediaDataHandlerFuzzer: cloudType=%{public}d", cloudType);
49     int32_t userId = provider->ConsumeIntegral<int32_t>();
50     MEDIA_INFO_LOG("InitCloudMediaDataHandlerFuzzer: userId=%{public}d", userId);
51     shared_ptr<CloudMediaDataHandler> dataHandler = make_shared<CloudMediaDataHandler>(tableName, cloudType, userId);
52     return dataHandler;
53 }
54 
GetCheckRecordsFuzzer()55 static void GetCheckRecordsFuzzer()
56 {
57     shared_ptr<CloudMediaDataHandler> dataHandler = InitCloudMediaDataHandlerFuzzer();
58     if (dataHandler == nullptr) {
59         MEDIA_ERR_LOG("No data handler found!");
60         return;
61     }
62     MEDIA_INFO_LOG("GetCheckRecordsFuzzer enter");
63 
64     vector<std::string> cloudIds = { provider->ConsumeBytesAsString(NUM_BYTES) };
65     unordered_map<std::string, CloudCheckData> checkRecords;
66     dataHandler->GetCheckRecords(cloudIds, checkRecords);
67     dataHandler->dataHandler_ = nullptr;
68     dataHandler->GetCheckRecords(cloudIds, checkRecords);
69 }
70 
MediaLibraryICloudMediaDataHandlerFuzzer()71 static void MediaLibraryICloudMediaDataHandlerFuzzer()
72 {
73     shared_ptr<CloudMediaDataHandler> dataHandler = make_shared<CloudMediaDataHandler>();
74     if (dataHandler == nullptr) {
75         MEDIA_ERR_LOG("No data handler found!");
76         return;
77     }
78     string traceId = provider->ConsumeBytesAsString(NUM_BYTES);
79     dataHandler->SetTraceId(traceId);
80     dataHandler->GetTraceId();
81 
82     vector<MDKRecord> records;
83     int32_t recordSize = provider->ConsumeIntegral<int32_t>();
84     dataHandler->GetCreatedRecords(records, recordSize);
85     dataHandler->GetMetaModifiedRecords(records, recordSize);
86     dataHandler->GetFileModifiedRecords(records, recordSize);
87     dataHandler->GetDeletedRecords(records, recordSize);
88     dataHandler->GetCopyRecords(records, recordSize);
89 
90     map<std::string, MDKRecordOperResult> map;
91     int32_t failSize = 0;
92     dataHandler->OnCreateRecords(map, failSize);
93     dataHandler->OnMdirtyRecords(map, failSize);
94     dataHandler->OnFdirtyRecords(map, failSize);
95     dataHandler->OnDeleteRecords(map, failSize);
96     dataHandler->OnCopyRecords(map, failSize);
97 
98     vector<std::string> failedRecords;
99     vector<CloudMetaData> newData;
100     vector<CloudMetaData> fdirtyData;
101     vector<int32_t> stats;
102     vector<std::string> strRecords;
103     dataHandler->OnFetchRecords(records, newData, fdirtyData, failedRecords, stats);
104     dataHandler->OnDentryFileInsert(records, failedRecords);
105     dataHandler->GetRetryRecords(strRecords);
106 
107     dataHandler->OnStartSync();
108     dataHandler->OnCompleteSync();
109     dataHandler->OnCompletePull();
110     dataHandler->OnCompletePush();
111     dataHandler->OnCompleteCheck();
112 
113     string tableName = "PhotoAlbum";
114     dataHandler->SetTableName(tableName);
115     dataHandler->GetTableName();
116     dataHandler->SetTraceId(traceId);
117     dataHandler->GetTraceId();
118 }
119 
MediaLibraryCloudMediaDataHandlerFuzzer()120 static void MediaLibraryCloudMediaDataHandlerFuzzer()
121 {
122     std::shared_ptr<CloudMediaDataHandler> dataHandler = InitCloudMediaDataHandlerFuzzer();
123     if (dataHandler == nullptr) {
124         MEDIA_ERR_LOG("No data handler found!");
125         return;
126     }
127     MEDIA_INFO_LOG("MediaLibraryCloudMediaDataHandlerFuzzer enter");
128     dataHandler->SetCloudType(provider->ConsumeIntegral<int32_t>());
129     dataHandler->GetCloudType();
130 
131     string tableName = "PhotoAlbum";
132     dataHandler->SetTableName(tableName);
133     dataHandler->GetTableName();
134 
135     dataHandler->SetUserId(provider->ConsumeIntegral<int32_t>());
136     dataHandler->GetUserId();
137 
138     vector<MDKRecord> records;
139     int32_t recordSize = provider->ConsumeIntegral<int32_t>();
140     dataHandler->GetCreatedRecords(records, recordSize);
141     dataHandler->GetMetaModifiedRecords(records, recordSize);
142     dataHandler->GetFileModifiedRecords(records, recordSize);
143     dataHandler->GetDeletedRecords(records, recordSize);
144     dataHandler->GetCopyRecords(records, recordSize);
145 
146     map<std::string, MDKRecordOperResult> map;
147     int32_t failSize = 0;
148     dataHandler->OnCreateRecords(map, failSize);
149     dataHandler->OnMdirtyRecords(map, failSize);
150     dataHandler->OnFdirtyRecords(map, failSize);
151     dataHandler->OnDeleteRecords(map, failSize);
152     dataHandler->OnCopyRecords(map, failSize);
153 
154     vector<std::string> failedRecords;
155     vector<CloudMetaData> newData;
156     vector<CloudMetaData> fdirtyData;
157     vector<int32_t> stats;
158     vector<std::string> strRecords;
159     dataHandler->OnFetchRecords(records, newData, fdirtyData, failedRecords, stats);
160     dataHandler->OnDentryFileInsert(records, failedRecords);
161     dataHandler->GetRetryRecords(strRecords);
162 
163     dataHandler->OnStartSync();
164     dataHandler->OnCompleteSync();
165     dataHandler->OnCompletePull();
166     dataHandler->OnCompletePush();
167     dataHandler->OnCompleteCheck();
168 }
169 
SetTables()170 void SetTables()
171 {
172     vector<string> createTableSqlList = {
173         Media::PhotoColumn::CREATE_PHOTO_TABLE,
174         Media::PhotoAlbumColumns::CREATE_TABLE,
175     };
176     for (auto &createTableSql : createTableSqlList) {
177         int32_t ret = g_rdbStore->ExecuteSql(createTableSql);
178         if (ret != NativeRdb::E_OK) {
179             MEDIA_ERR_LOG("Execute sql %{private}s failed", createTableSql.c_str());
180             return;
181         }
182         MEDIA_DEBUG_LOG("Execute sql %{private}s success", createTableSql.c_str());
183     }
184 }
185 
Init()186 static void Init()
187 {
188     auto stageContext = std::make_shared<AbilityRuntime::ContextImpl>();
189     auto abilityContextImpl = std::make_shared<OHOS::AbilityRuntime::AbilityContextImpl>();
190     abilityContextImpl->SetStageContext(stageContext);
191     int32_t sceneCode = 0;
192     auto ret = Media::MediaLibraryDataManager::GetInstance()->InitMediaLibraryMgr(abilityContextImpl,
193         abilityContextImpl, sceneCode);
194     CHECK_AND_RETURN_LOG(ret == NativeRdb::E_OK, "InitMediaLibraryMgr failed, ret: %{public}d", ret);
195 
196     auto rdbStore = Media::MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
197     if (rdbStore == nullptr) {
198         MEDIA_ERR_LOG("rdbStore is nullptr");
199         return;
200     }
201     g_rdbStore = rdbStore;
202     SetTables();
203 }
204 
ClearKvStore()205 static inline void ClearKvStore()
206 {
207     Media::MediaLibraryKvStoreManager::GetInstance().CloseAllKvStore();
208 }
209 } // namespace OHOS
210 
LLVMFuzzerInitialize(int * argc,char *** argv)211 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
212 {
213     OHOS::Init();
214     return 0;
215 }
216 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)217 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
218 {
219     FuzzedDataProvider fdp(data, size);
220     if (data == nullptr) {
221         return 0;
222     }
223     OHOS::provider = &fdp;
224     OHOS::MediaLibraryICloudMediaDataHandlerFuzzer();
225     OHOS::InitCloudMediaDataHandlerFuzzer();
226     OHOS::GetCheckRecordsFuzzer();
227     OHOS::MediaLibraryCloudMediaDataHandlerFuzzer();
228     OHOS::ClearKvStore();
229     return 0;
230 }