1 /*
2 * Copyright (c) 2025 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 "backupsagetfilehandle_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include <climits>
22 #include <fuzzer/FuzzedDataProvider.h>
23 #include <vector>
24
25 #include "message_parcel.h"
26 #include "sandbox_helper.h"
27 #include "service.h"
28 #include "service_proxy.h"
29 #include "service_reverse.h"
30 #include "service_stub.h"
31 #include "securec.h"
32 #include "system_ability.h"
33
34 using namespace std;
35 using namespace OHOS::FileManagement::Backup;
36
37 namespace OHOS {
38 constexpr int32_t SERVICE_ID = 5203;
39
CmdGetFileHandleFuzzTest(const uint8_t * data,size_t size)40 bool CmdGetFileHandleFuzzTest(const uint8_t *data, size_t size)
41 {
42 MessageParcel datas;
43 datas.WriteInterfaceToken(ServiceStub::GetDescriptor());
44 int len = size >> 1;
45 std::string bundleName(string(reinterpret_cast<const char*>(data), len));
46 datas.WriteString(bundleName);
47 std::string filePath(string(reinterpret_cast<const char*>(data + len), len));
48 datas.WriteString(filePath);
49 datas.RewindRead(0);
50 MessageParcel reply;
51 MessageOption option;
52
53 sptr service(new Service(SERVICE_ID));
54 service->OnRemoteRequest(static_cast<uint32_t>(IServiceIpcCode::COMMAND_GET_FILE_HANDLE),
55 datas, reply, option);
56 return true;
57 }
58 } // namespace OHOS
59
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62 {
63 try {
64 OHOS::CmdGetFileHandleFuzzTest(data, size);
65 } catch (OHOS::FileManagement::Backup::BError &err) {
66 HILOGE("BackupSaFuzzTest error");
67 } catch (...) {
68 HILOGE("BackupSaFuzzTest exception");
69 }
70 return 0;
71 }