• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "registeventcb_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_window_adapter.h"
28 #include "nweb_surface_adapter.h"
29 #include "window.h"
30 
31 using namespace OHOS::NWeb;
32 using namespace OHOS::Rosen;
33 
34 namespace OHOS {
35 namespace {
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 const int DEFAULT_WIDTH = 2560;
48 const int DEFAULT_HEIGHT = 1396;
49 }
50 
HasArg(const std::string & arg)51 bool HasArg(const std::string &arg)
52 {
53     return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
54 }
55 
GetArgValue(const std::string & arg)56 std::string GetArgValue(const std::string &arg)
57 {
58     if (!HasArg(arg)) {
59         return "";
60     }
61     return g_argsMap.at(arg);
62 }
63 
GetNumFromArgs(const std::string & arg)64 uint32_t GetNumFromArgs(const std::string &arg)
65 {
66     if (!HasArg(arg)) {
67         return 0;
68     }
69     return std::stoi(GetArgValue(arg));
70 }
71 
GetWebEngineArgs(const std::string & arg)72 std::list<std::string> GetWebEngineArgs(const std::string &arg)
73 {
74     std::string webEngineArgValue = GetArgValue(arg);
75     std::list<std::string> webEngineArgList;
76     if (webEngineArgValue.empty()) {
77         return webEngineArgList;
78     }
79     uint32_t start = 0;
80     uint32_t pos = 0;
81     while (pos < webEngineArgValue.size()) {
82         if (webEngineArgValue[pos] == ',') {
83             webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
84             pos++;
85             start = pos;
86         } else {
87             pos++;
88         }
89     }
90     webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
91     webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
92     return webEngineArgList;
93 }
94 
GetInitArgs()95 OHOS::NWeb::NWebInitArgs GetInitArgs()
96 {
97     OHOS::NWeb::NWebInitArgs initArgs = {
98         .dump_path = GetArgValue(ARG_DUMP),
99         .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false,
100         .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG),
101         .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG),
102         .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false,
103     };
104     return initArgs;
105 }
106 
CreateWindow()107 sptr<Rosen::Window> CreateWindow()
108 {
109     sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
110     if (option == nullptr) {
111         return nullptr;
112     }
113     int width = HasArg(ARG_WIDTH) ? GetNumFromArgs(ARG_WIDTH) : DEFAULT_WIDTH;
114     int height = HasArg(ARG_HEIGHT) ? GetNumFromArgs(ARG_HEIGHT) : DEFAULT_HEIGHT;
115     option->SetWindowRect({0, 0, width, height});
116     auto window = Rosen::Window::Create("nweb_test_window", option);
117     return window;
118 }
119 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)120 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
121 {
122     if ((data == nullptr) || (size == 0)) {
123         return false;
124     }
125     sptr<Rosen::Window> window = CreateWindow();
126     if (window == nullptr) {
127         return false;
128     }
129     auto adapter = NWebWindowAdapter::Instance();
130     adapter.RegistEventCb(window.GetRefPtr(), nullptr);
131     return true;
132 }
133 }
134 
135 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)136 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
137 {
138     /* Run your code on data */
139     OHOS::DoSomethingInterestingWithMyAPI(data, size);
140     return 0;
141 }
142