1 /*
2 * Copyright (c) 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 "oninputkeyevent_fuzzer.h"
17
18 #include <cstring>
19 #include <securec.h>
20 #include <ui/rs_surface_node.h>
21
22 #include "nweb.h"
23 #include "nweb_adapter_helper.h"
24 #include "nweb_handler_impl_test.h"
25 #include "nweb_input_event_consumer.h"
26 #include "window.h"
27
28 const int DEFAULT_WIDTH = 2560;
29 const int DEFAULT_HEIGHT = 1396;
30
31 using namespace OHOS::NWeb;
32
33 namespace OHOS {
34 std::unordered_map<std::string, std::string> g_argsMap;
35 const std::string ARG_URL = "--url";
36 const std::string ARG_DUMP = "--dump-path";
37 const std::string ARG_FRAME_INFO = "--frame-info";
38 const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
39 const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
40 const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
41 const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
42 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
43 const std::string ARG_WIDTH = "--width";
44 const std::string ARG_HEIGHT = "--height";
45
HasArg(const std::string & arg)46 bool HasArg(const std::string &arg)
47 {
48 return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
49 }
50
GetArgValue(const std::string & arg)51 std::string GetArgValue(const std::string &arg)
52 {
53 if (!HasArg(arg)) {
54 return "";
55 }
56 return g_argsMap.at(arg);
57 }
58
GetNumFromArgs(const std::string & arg)59 uint32_t GetNumFromArgs(const std::string &arg)
60 {
61 if (!HasArg(arg)) {
62 return 0;
63 }
64 return std::stoi(GetArgValue(arg));
65 }
66
GetWebEngineArgs(const std::string & arg)67 std::list<std::string> GetWebEngineArgs(const std::string &arg)
68 {
69 std::string webEngineArgValue = GetArgValue(arg);
70 std::list<std::string> webEngineArgList;
71 if (webEngineArgValue.empty()) {
72 return webEngineArgList;
73 }
74 uint32_t start = 0;
75 uint32_t pos = 0;
76 while (pos < webEngineArgValue.size()) {
77 if (webEngineArgValue[pos] == ',') {
78 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
79 pos++;
80 start = pos;
81 } else {
82 pos++;
83 }
84 }
85 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
86 webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
87 return webEngineArgList;
88 }
89
GetInitArgs()90 OHOS::NWeb::NWebInitArgs GetInitArgs()
91 {
92 OHOS::NWeb::NWebInitArgs initArgs = {
93 .dump_path = GetArgValue(ARG_DUMP),
94 .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false,
95 .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG),
96 .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG),
97 .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false,
98 };
99 return initArgs;
100 }
101
CreateWindow()102 sptr<Rosen::Window> CreateWindow()
103 {
104 sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
105 if (option == nullptr) {
106 return nullptr;
107 }
108 int width = HasArg(ARG_WIDTH) ? GetNumFromArgs(ARG_WIDTH) : DEFAULT_WIDTH;
109 int height = HasArg(ARG_HEIGHT) ? GetNumFromArgs(ARG_HEIGHT) : DEFAULT_HEIGHT;
110 option->SetWindowRect({0, 0, width, height});
111 auto window = Rosen::Window::Create("nweb_test_window", option);
112 return window;
113 }
114
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)115 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
116 {
117 if ((data == nullptr) || (size == 0)) {
118 return false;
119 }
120 auto window = CreateWindow();
121 if (window == nullptr) {
122 return false;
123 }
124 auto nweb = NWeb::NWebAdapterHelper::Instance().CreateNWeb(
125 window->GetSurfaceNode()->GetSurface(), GetInitArgs());
126 if (nweb == nullptr) {
127 return false;
128 }
129 NWebInputEventConsumer inputEvent(nweb);
130 auto keyEvent = MMI::KeyEvent::Create();
131 if (keyEvent == nullptr) {
132 return false;
133 }
134 inputEvent.OnInputEvent(keyEvent);
135 return true;
136 }
137 }
138
139 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)140 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
141 {
142 /* Run your code on data */
143 OHOS::DoSomethingInterestingWithMyAPI(data, size);
144 return 0;
145 }
146