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