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