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 "sendkeyevent_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
25 using namespace OHOS::Rosen;
26
27 namespace OHOS {
28 std::shared_ptr<OHOS::NWeb::NWeb> g_nweb = nullptr;
29 sptr<Surface> g_surface = nullptr;
30 std::unordered_map<std::string, std::string> g_argsMap;
31 const std::string ARG_URL = "--url";
32 const std::string ARG_DUMP = "--dump-path";
33 const std::string ARG_FRAME_INFO = "--frame-info";
34 const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
35 const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
36 const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
37 const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
38 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
39 const std::string ARG_WIDTH = "--width";
40 const std::string ARG_HEIGHT = "--height";
41
HasArg(const std::string & arg)42 bool HasArg(const std::string &arg)
43 {
44 return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
45 }
46
GetArgValue(const std::string & arg)47 std::string GetArgValue(const std::string &arg)
48 {
49 if (!HasArg(arg)) {
50 return "";
51 }
52 return g_argsMap.at(arg);
53 }
54
GetWebEngineArgs(const std::string & arg)55 std::list<std::string> GetWebEngineArgs(const std::string &arg)
56 {
57 std::string webEngineArgValue = GetArgValue(arg);
58 std::list<std::string> webEngineArgList;
59 if (webEngineArgValue.empty()) {
60 return webEngineArgList;
61 }
62 uint32_t start = 0;
63 uint32_t pos = 0;
64 while (pos < webEngineArgValue.size()) {
65 if (webEngineArgValue[pos] == ',') {
66 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
67 pos++;
68 start = pos;
69 } else {
70 pos++;
71 }
72 }
73 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
74 webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
75 return webEngineArgList;
76 }
77
GetInitArgs()78 OHOS::NWeb::NWebInitArgs GetInitArgs()
79 {
80 OHOS::NWeb::NWebInitArgs initArgs = {
81 .dump_path = GetArgValue(ARG_DUMP),
82 .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false,
83 .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG),
84 .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG),
85 .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false,
86 };
87 return initArgs;
88 }
89
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)90 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
91 {
92 if ((data == nullptr) || (size < sizeof(int32_t))) {
93 return true;
94 }
95 if (!g_surface) {
96 RSSurfaceNodeConfig config;
97 config.SurfaceNodeName = "webTestSurfaceName";
98 auto surfaceNode = RSSurfaceNode::Create(config, false);
99 if (surfaceNode == nullptr) {
100 return false;
101 }
102 g_surface = surfaceNode->GetSurface();
103 if (g_surface== nullptr) {
104 return false;
105 }
106 }
107 g_nweb = NWeb::NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs());
108 if (g_nweb == nullptr) {
109 return true;
110 }
111 int32_t keyCode;
112 int32_t keyAction;
113 if (memcpy_s(&keyCode, sizeof(int32_t), data, sizeof(int32_t)) != 0) {
114 return true;
115 }
116 if (memcpy_s(&keyAction, sizeof(int32_t), data, sizeof(int32_t)) != 0) {
117 return true;
118 }
119 g_nweb->SendKeyEvent(keyCode, keyAction);
120 return true;
121 }
122 }
123
124 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)125 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
126 {
127 /* Run your code on data */
128 OHOS::DoSomethingInterestingWithMyAPI(data, size);
129 return 0;
130 }
131