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 "backupsapublishfile_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
CmdPublishFileFuzzTest(const uint8_t * data,size_t size)38 bool CmdPublishFileFuzzTest(const uint8_t *data, size_t size)
39 {
40 if (data == nullptr || size < sizeof(uint32_t)) {
41 return false;
42 }
43 MessageParcel datas;
44 datas.WriteInterfaceToken(ServiceStub::GetDescriptor());
45 uint32_t sn = *(reinterpret_cast<const uint32_t *>(data));
46 int len = (size - sizeof(uint32_t)) >> 1;
47 std::string fileName(reinterpret_cast<const char *>(data + sizeof(uint32_t)), len);
48 std::string bundleName(reinterpret_cast<const char *>(data + sizeof(uint32_t) + len), len);
49 BFileInfo fileInfo(fileName, bundleName, sn);
50 datas.WriteParcelable(&fileInfo);
51 datas.RewindRead(0);
52 MessageParcel reply;
53 MessageOption option;
54
55 sptr service(new Service(SERVICE_ID));
56 service->OnRemoteRequest(static_cast<uint32_t>(IServiceIpcCode::COMMAND_PUBLISH_FILE),
57 datas, reply, option);
58 return true;
59 }
60 } // namespace OHOS
61
62 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64 {
65 /* Run your code on data */
66 try {
67 OHOS::CmdPublishFileFuzzTest(data, size);
68 } catch (OHOS::FileManagement::Backup::BError &err) {
69 HILOGE("BackupSaFuzzTest error");
70 } catch (...) {
71 HILOGE("BackupSaFuzzTest exception");
72 }
73 return 0;
74 }