1 /*
2 * Copyright (c) 2021-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 "backupsaappendbundlesrestoresession_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include <climits>
22 #include <vector>
23
24 #include "message_parcel.h"
25 #include "service.h"
26 #include "service_proxy.h"
27 #include "service_reverse.h"
28 #include "service_stub.h"
29 #include "securec.h"
30 #include "system_ability.h"
31
32 using namespace std;
33 using namespace OHOS::FileManagement::Backup;
34
35 namespace OHOS {
36 constexpr int32_t SERVICE_ID = 5203;
37
CmdAppendBundlesRestoreSessionFuzzTest(const uint8_t * data,size_t size)38 bool CmdAppendBundlesRestoreSessionFuzzTest(const uint8_t *data, size_t size)
39 {
40 if (data == nullptr || size == 0) {
41 return false;
42 }
43 MessageParcel datas;
44 datas.WriteInterfaceToken(ServiceStub::GetDescriptor());
45 size_t len = 0;
46 if (size >= len + sizeof(int)) {
47 int fd = *(reinterpret_cast<const int *>(data + len));
48 datas.WriteFileDescriptor(UniqueFd(fd));
49 len += sizeof(int);
50 }
51 int32_t type = 0;
52 if (size >= len + sizeof(int32_t)) {
53 type = *(reinterpret_cast<const int32_t *>(data + len));
54 datas.WriteInt32(type);
55 len += sizeof(int32_t);
56 }
57 int32_t userId = 0;
58 if (size >= len + sizeof(int32_t)) {
59 userId = *(reinterpret_cast<const int32_t *>(data + len));
60 datas.WriteInt32(userId);
61 len += sizeof(int32_t);
62 }
63 if (size > len) {
64 vector<string> bundleNames;
65 string baseStr(reinterpret_cast<const char*>(data + len), size - len);
66 bundleNames.push_back(baseStr);
67 datas.WriteStringVector(bundleNames);
68 }
69 datas.RewindRead(0);
70 MessageParcel reply;
71 MessageOption option;
72 sptr service(new Service(SERVICE_ID));
73 service->OnRemoteRequest(
74 static_cast<uint32_t>(IServiceIpcCode::COMMAND_APPEND_BUNDLES_RESTORE_SESSION_DATA),
75 datas, reply, option);
76 return true;
77 }
78 } // namespace OHOS
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82 {
83 /* Run your code on data */
84 try {
85 OHOS::CmdAppendBundlesRestoreSessionFuzzTest(data, size);
86 } catch (OHOS::FileManagement::Backup::BError &err) {
87 HILOGE("BackupSaFuzzTest error");
88 } catch (...) {
89 HILOGE("BackupSaFuzzTest exception");
90 }
91 return 0;
92 }