1 /*
2 * Copyright (c) 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 #include "update_fuzzer.h"
16 #include <fcntl.h>
17 #include <vector>
18 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h"
19 #include "datashare_helper.h"
20 #include "file_access_extension_info.h"
21 #include "file_access_framework_errno.h"
22 #include "file_access_helper.h"
23 #include "file_filter.h"
24 #include "iservice_registry.h"
25 #include "medialibrary_errno.h"
26 #include "media_library_manager.h"
27 #include "media_log.h"
28 #include "result_set_utils.h"
29 #include "scanner_utils.h"
30
31 using namespace OHOS;
32 using namespace OHOS::FileAccessFwk;
33 namespace OHOS {
34 namespace MediaLibrary {
UpdateFuzzTest(const uint8_t * data,size_t size)35 bool UpdateFuzzTest(const uint8_t *data, size_t size)
36 {
37 if ((data == nullptr) || (size <= 0)) {
38 return false;
39 }
40
41 Uri updateFileUri(std::string(reinterpret_cast<const char*>(data), size));
42 DataShare::DataSharePredicates predicates;
43 DataShare::DataShareValuesBucket value;
44 std::string selections = std::string(reinterpret_cast<const char*>(data), size);
45 predicates.SetWhereClause(selections);
46
47 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48 if (saManager == nullptr) {
49 return false;
50 }
51 auto remoteObj = saManager->GetSystemAbility(5003);
52 if (remoteObj == nullptr) {
53 return false;
54 }
55 auto helper = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI);
56 if (helper == nullptr) {
57 return false;
58 }
59 helper->Update(updateFileUri, predicates, value);
60 return true;
61 }
62 } // namespace StorageManager
63 } // namespace OHOS
64
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
67 {
68 /* Run your code on data */
69 OHOS::MediaLibrary::UpdateFuzzTest(data, size);
70 return 0;
71 }