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 "storagedaemondeletesharefile_fuzzer.h"
16
17 #include <cstddef>
18 #include <cstdint>
19 #include <cstring>
20
21 #include "message_parcel.h"
22 #include "storage_daemon_ipc_interface_code.h"
23 #include "storage_daemon_stub.h"
24 #include "storage_daemon.h"
25 #include "securec.h"
26
27 using namespace OHOS::StorageDaemon;
28
29 namespace OHOS {
30 constexpr size_t FOO_MAX_LEN = 1024;
31 constexpr size_t U32_AT_SIZE = 4;
32
33 std::shared_ptr<StorageDaemon::StorageDaemon> storageDaemon =
34 std::make_shared<StorageDaemon::StorageDaemon>();
35
GetU32Data(const char * ptr)36 uint32_t GetU32Data(const char* ptr)
37 {
38 // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移
39 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
40 }
41
StorageDaemonDeleteShareFileFuzzTest(std::unique_ptr<char[]> data,size_t size)42 bool StorageDaemonDeleteShareFileFuzzTest(std::unique_ptr<char[]> data, size_t size)
43 {
44 uint32_t code = static_cast<int32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE);
45 MessageParcel datas;
46 datas.WriteInterfaceToken(StorageDaemonStub::GetDescriptor());
47 datas.WriteBuffer(data.get(), size);
48 datas.RewindRead(0);
49 MessageParcel reply;
50 MessageOption option;
51 storageDaemon->OnRemoteRequest(code, datas, reply, option);
52
53 return true;
54 }
55 } // namespace OHOS
56
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
59 {
60 /* Run your code on data */
61 if (data == nullptr) {
62 return 0;
63 }
64
65 /* Validate the length of size */
66 if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
67 return 0;
68 }
69
70 auto str = std::make_unique<char[]>(size + 1);
71 (void)memset_s(str.get(), size + 1, 0x00, size + 1);
72 if (memcpy_s(str.get(), size, data, size) != EOK) {
73 return 0;
74 }
75 OHOS::StorageDaemonDeleteShareFileFuzzTest(move(str), size);
76 return 0;
77 }
78