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 #define private public
16 #define protected public
17 #include "ime_system_channel.h"
18 #include "input_method_system_ability_proxy.h"
19 #undef private
20 
21 #include "imesystemchannel_fuzzer.h"
22 #include "system_cmd_channel_stub.h"
23 #include <cstddef>
24 #include <cstdint>
25 
26 #include "input_method_agent_stub.h"
27 #include "message_parcel.h"
28 
29 using namespace OHOS::MiscServices;
30 namespace OHOS {
31 constexpr size_t THRESHOLD = 10;
32 constexpr int32_t PRIVATEDATAVALUE = 100;
FuzzGetSmartMenuCfg()33 void FuzzGetSmartMenuCfg()
34 {
35     ImeSystemCmdChannel::GetInstance()->GetSmartMenuCfg();
36 }
37 
FuzzConnectSystemCmd()38 void FuzzConnectSystemCmd()
39 {
40     sptr<OnSystemCmdListener> listener = new (std::nothrow) OnSystemCmdListener();
41     if (listener == nullptr) {
42         return;
43     }
44 
45     ImeSystemCmdChannel::GetInstance()->SetSystemCmdListener(listener);
46     ImeSystemCmdChannel::GetInstance()->GetSystemCmdListener();
47     ImeSystemCmdChannel::GetInstance()->ConnectSystemCmd(listener);
48     ImeSystemCmdChannel::GetInstance()->RunConnectSystemCmd();
49 }
50 
FuzzSystemCmdAgent()51 void FuzzSystemCmdAgent()
52 {
53     ImeSystemCmdChannel::GetInstance()->GetSystemCmdAgent();
54     ImeSystemCmdChannel::GetInstance()->ClearSystemCmdAgent();
55 }
56 
FuzzOnSystemCmdAgent()57 void FuzzOnSystemCmdAgent()
58 {
59     sptr<SystemCmdChannelStub> stub = new SystemCmdChannelStub();
60 
61     MessageParcel data;
62     data.WriteRemoteObject(stub->AsObject());
63     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
64     ImeSystemCmdChannel::GetInstance()->OnConnectCmdReady(remoteObject);
65     ImeSystemCmdChannel::GetInstance()->OnSystemCmdAgentDied(remoteObject);
66 }
67 
FuzzPrivateCommand(const uint8_t * data,size_t size)68 void FuzzPrivateCommand(const uint8_t *data, size_t size)
69 {
70     bool fuzzedBool = static_cast<bool>(data[0] % 2);
71 
72     std::unordered_map<std::string, PrivateDataValue> privateCommand;
73     PrivateDataValue privateDataValue1 = std::string("stringValue");
74     PrivateDataValue privateDataValue2 = fuzzedBool;
75     PrivateDataValue privateDataValue3 = PRIVATEDATAVALUE;
76     privateCommand.emplace("value1", privateDataValue1);
77     privateCommand.emplace("value2", privateDataValue2);
78     privateCommand.emplace("value3", privateDataValue3);
79 
80     ImeSystemCmdChannel::GetInstance()->SendPrivateCommand(privateCommand);
81     ImeSystemCmdChannel::GetInstance()->ReceivePrivateCommand(privateCommand);
82 }
83 
FuzzNotifyPanelStatus(const uint8_t * data,size_t size)84 void FuzzNotifyPanelStatus(const uint8_t *data, size_t size)
85 {
86     bool fuzzedBool = static_cast<bool>(data[0] % 2);
87     auto fuzzedUint32 = static_cast<uint32_t>(size);
88 
89     SysPanelStatus sysPanelStatus = { fuzzedBool, 0, fuzzedUint32, fuzzedUint32 };
90     ImeSystemCmdChannel::GetInstance()->NotifyPanelStatus(sysPanelStatus);
91 }
92 } // namespace OHOS
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
96 {
97     if (size < OHOS::THRESHOLD) {
98         return 0;
99     }
100     /* Run your code on data */
101 
102     OHOS::FuzzGetSmartMenuCfg();
103     OHOS::FuzzConnectSystemCmd();
104     OHOS::FuzzSystemCmdAgent();
105     OHOS::FuzzOnSystemCmdAgent();
106     OHOS::FuzzPrivateCommand(data, size);
107     OHOS::FuzzNotifyPanelStatus(data, size);
108     return 0;
109 }