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 
16 #include "dmsfreeinstallcallbackstub_fuzzer.h"
17 
18 #include "dms_free_install_callback.h"
19 #include "distributed_sched_interface.h"
20 #include "mock_fuzz_util.h"
21 #include "parcel_helper.h"
22 
23 namespace OHOS {
24 namespace DistributedSchedule {
25 namespace {
26 const uint32_t ONE = 1;
27 }
OnInstallFinishedInnerFuzzTest(const uint8_t * data,size_t size)28 bool OnInstallFinishedInnerFuzzTest(const uint8_t* data, size_t size)
29 {
30     if ((data == nullptr) || (size < sizeof(int32_t))) {
31         return false;
32     }
33     FuzzUtil::MockPermission();
34     int64_t taskId = 0;
35     IDistributedSched::FreeInstallInfo info;
36     std::shared_ptr<DmsFreeInstallCallback> freeInstall_ =
37         std::make_shared<DmsFreeInstallCallback>(taskId, info);
38     uint32_t code = ONE;
39     MessageParcel dataParcel;
40     MessageParcel reply;
41     MessageOption option;
42     std::u16string descriptor = DmsFreeInstallCallbackStub::GetDescriptor();
43     freeInstall_->OnRemoteRequest(code, dataParcel, reply, option);
44 
45     dataParcel.WriteInterfaceToken(descriptor);
46     freeInstall_->OnRemoteRequest(code, dataParcel, reply, option);
47 
48     code = static_cast<uint32_t>(IDRreeInstallCallbackInterfaceCode::ON_FREE_INSTALL_DONE);
49     int32_t resultCode = *(reinterpret_cast<const int32_t*>(data));
50     int32_t requestCode = *(reinterpret_cast<const int32_t*>(data));
51     dataParcel.WriteInt32(resultCode);
52     dataParcel.WriteInt32(requestCode);
53     freeInstall_->OnRemoteRequest(code, dataParcel, reply, option);
54 
55     AAFwk::Want want;
56     dataParcel.WriteParcelable(&want);
57     freeInstall_->OnRemoteRequest(code, dataParcel, reply, option);
58     return true;
59 }
60 }
61 }
62 
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66     OHOS::DistributedSchedule::OnInstallFinishedInnerFuzzTest(data, size);
67     return 0;
68 }