1 /*
2 * Copyright (c) 2022 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 <cstdio>
17 #include <string>
18 #include <cstdint>
19
20 #include "album_asset_fuzzer.h"
21 #include "album_asset.h"
22
23 namespace OHOS {
24 namespace MediaLibrary {
25
26 using namespace Media;
27
ModifyAlbumAssetFuzzTest(const uint8_t * data,size_t size)28 bool ModifyAlbumAssetFuzzTest(const uint8_t *data, size_t size)
29 {
30 if ((data == nullptr) || size <= 0) {
31 return false;
32 }
33
34 std::string albumUri(reinterpret_cast<const char*>(data), size);
35 AlbumAsset albumAsset;
36 bool errCode = albumAsset.ModifyAlbumAsset(albumUri);
37 if (!errCode) {
38 return true;
39 }
40
41 return false;
42 }
43
DeleteAlbumAssetFuzzTest(const uint8_t * data,size_t size)44 bool DeleteAlbumAssetFuzzTest(const uint8_t *data, size_t size)
45 {
46 if ((data == nullptr) || size <= 0) {
47 return false;
48 }
49
50 std::string albumUri = std::string(reinterpret_cast<const char*>(data), size);
51 if (albumUri.find("../") != std::string::npos) {
52 return false;
53 }
54
55 std::string uri = std::string("/data/test/fuzzTest/") + std::string(albumUri);
56 AlbumAsset albumAsset;
57 bool errCode = albumAsset.DeleteAlbumAsset(uri);
58 if (!errCode) {
59 return true;
60 }
61
62 return false;
63 }
64
65 } // namespace MediaLibrary
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 OHOS::MediaLibrary::ModifyAlbumAssetFuzzTest(data, size);
73 OHOS::MediaLibrary::DeleteAlbumAssetFuzzTest(data, size);
74 return 0;
75 }