1 /*
2 * Copyright (c) 2022-2022 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 "perusersession_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <memory>
21 #include <string_ex.h>
22
23 #include "global.h"
24 #include "i_input_method_agent.h"
25 #include "i_input_method_core.h"
26 #include "input_client_proxy.h"
27 #include "input_method_ability.h"
28 #include "input_method_agent_proxy.h"
29 #include "input_method_agent_stub.h"
30 #include "input_method_core_proxy.h"
31 #include "input_method_info.h"
32 #include "input_method_property.h"
33 #include "iremote_broker.h"
34 #include "message_parcel.h"
35 #include "peruser_session.h"
36
37 using namespace OHOS::MiscServices;
38 namespace OHOS {
39 constexpr size_t THRESHOLD = 10;
40
ConvertToUint32(const uint8_t * ptr)41 uint32_t ConvertToUint32(const uint8_t *ptr)
42 {
43 if (ptr == nullptr) {
44 return 0;
45 }
46 uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
47 return bigVar;
48 }
49
FuzzPerUserSession(const uint8_t * rawData,size_t size)50 bool FuzzPerUserSession(const uint8_t *rawData, size_t size)
51 {
52 Property property;
53 SubProperty subProperty;
54
55 std::string str(rawData, rawData + size);
56 std::u16string packageName = Str8ToStr16(str);
57 bool isShowKeyboard = true;
58 constexpr int32_t MAIN_USER_ID = 100;
59 sptr<IInputClient> client = new (std::nothrow) InputClientStub();
60 sptr<IRemoteObject> object = client->AsObject();
61 std::shared_ptr<PerUserSession> userSessions = std::make_shared<PerUserSession>(MAIN_USER_ID);
62 sptr<IInputMethodCore> core = new InputMethodCoreProxy(object);
63 sptr<IInputMethodAgent> agent = new InputMethodAgentProxy(object);
64 InputMethodInfo *ime = new InputMethodInfo();
65
66 userSessions->OnShowKeyboardSelf();
67 userSessions->OnInputMethodSwitched(property, subProperty);
68 userSessions->GetCurrentSubProperty();
69 userSessions->SetCurrentSubProperty(subProperty);
70 userSessions->StopInputService(str);
71 userSessions->OnHideKeyboardSelf();
72 userSessions->OnStartInput(client, isShowKeyboard);
73 userSessions->OnStopInput(client);
74 userSessions->OnReleaseInput(client);
75 userSessions->OnSetCoreAndAgent(core, agent);
76
77 delete ime;
78 ime = nullptr;
79 return true;
80 }
81 }
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 if (size < OHOS::THRESHOLD) {
86 return 0;
87 }
88 /* Run your code on data */
89 OHOS::FuzzPerUserSession(data, size);
90 return 0;
91 }