1 /*
2 * Copyright (c) 2024 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 "clouddaemonstub_fuzzer.h"
16
17 #include <cstddef>
18 #include <cstdint>
19 #include <map>
20 #include <iostream>
21
22 #include "cloud_daemon_stub.h"
23 #include "securec.h"
24 #include "message_parcel.h"
25 #include "cloud_file_daemon_interface_code.h"
26 #include "dfs_error.h"
27
28 namespace OHOS {
29
30 constexpr size_t U32_AT_SIZE = 4;
31
32 using namespace OHOS::FileManagement::CloudFile;
33 using namespace std;
34 using namespace OHOS::FileManagement;
35
36 const std::u16string CLOUD_DEAMON_TOKEN = u"ohos.filemanagement.distributedfile.clouddaemon";
37
38 namespace FileManagement {
39 namespace CloudFile {
40 class CloudDaemonStubImpl : public CloudDaemonStub {
41 public:
42 CloudDaemonStubImpl() = default;
~CloudDaemonStubImpl()43 ~CloudDaemonStubImpl() override {}
StartFuse(int32_t userId,int32_t deviceFd,const std::string & path)44 int32_t StartFuse(int32_t userId, int32_t deviceFd, const std::string &path) override
45 {
46 return E_OK;
47 }
48 };
49 }
50 }
51
HandleStartFuseInnerFuzzTest(std::shared_ptr<CloudDaemonStub> cloudDaemonStubStr,std::unique_ptr<char[]> data,size_t size)52 void HandleStartFuseInnerFuzzTest(
53 std::shared_ptr<CloudDaemonStub> cloudDaemonStubStr,
54 std::unique_ptr<char[]> data, size_t size)
55 {
56 // CLOUD_DAEMON_CMD_START_FUSE
57 uint32_t code = static_cast<uint32_t>(CloudFileDaemonInterfaceCode::CLOUD_DAEMON_CMD_START_FUSE);
58 MessageParcel datas;
59 datas.WriteInterfaceToken(CLOUD_DEAMON_TOKEN);
60 datas.WriteBuffer(data.get(), size);
61 datas.RewindRead(0);
62 MessageParcel reply;
63 MessageOption option;
64
65 cloudDaemonStubStr->OnRemoteRequest(code, datas, reply, option);
66 }
67
68 } // namespace OHOS
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72 {
73 /* Run your code on data */
74 if (data == nullptr) {
75 return 0;
76 }
77
78 /* Validate the length of size */
79 if (size < OHOS::U32_AT_SIZE) {
80 return 0;
81 }
82
83 auto str = std::make_unique<char[]>(size + 1);
84 (void)memset_s(str.get(), size + 1, 0x00, size + 1);
85 if (memcpy_s(str.get(), size, data, size) != EOK) {
86 return 0;
87 }
88
89 auto cloudDaemonStubStr = std::make_shared<OHOS::CloudDaemonStubImpl>();
90
91 OHOS::HandleStartFuseInnerFuzzTest(cloudDaemonStubStr, move(str), size);
92 return 0;
93 }
94