1 /*
2  * Copyright (c) 2024 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 <cstring>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include <ui/rs_surface_node.h>
21 #include <unordered_map>
22 
23 #define private public
24 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/application_context.h"
25 #include "nweb.h"
26 #include "nweb_adapter_helper.h"
27 #include "nweb_c_api.h"
28 #include "nweb_create_window.h"
29 #include "nweb_helper.h"
30 #include "nweb_init_params.h"
31 #include "nweb_log.h"
32 
33 using namespace testing;
34 using namespace testing::ext;
35 using namespace OHOS;
36 using namespace OHOS::Rosen;
37 using namespace OHOS::AbilityRuntime;
38 
39 namespace OHOS {
40 namespace {
41 sptr<Surface> g_surface = nullptr;
42 const int DEFAULT_WIDTH = 2560;
43 const int DEFAULT_HEIGHT = 1396;
44 const int32_t NWEB_MAX_WIDTH = 7681;
45 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
46 std::shared_ptr<AbilityRuntime::ApplicationContext> g_applicationContext = nullptr;
47 } // namespace
48 
49 namespace AbilityRuntime {
GetApplicationContext()50 std::shared_ptr<ApplicationContext> Context::GetApplicationContext()
51 {
52     return g_applicationContext;
53 }
54 } // namespace AbilityRuntime
55 
56 namespace NWeb {
57 
58 class NwebHelperTest : public testing::Test {
59 public:
60     static void SetUpTestCase(void);
61     static void TearDownTestCase(void);
62     void SetUp();
63     void TearDown();
64 };
65 
66 class ApplicationContextMock : public ApplicationContext {
67 public:
68     MOCK_CONST_METHOD0(GetBaseDir, std::string());
69 };
70 
71 class MockNWebEngine : public OHOS::NWeb::NWebEngine {
72 public:
CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info)73     std::shared_ptr<NWeb> CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info)
74     {
75         return nullptr;
76     }
77 
GetNWeb(int32_t nweb_id)78     std::shared_ptr<NWeb> GetNWeb(int32_t nweb_id)
79     {
80         return nullptr;
81     }
82 
GetDataBase()83     std::shared_ptr<NWebDataBase> GetDataBase()
84     {
85         return nullptr;
86     }
87 
GetWebStorage()88     std::shared_ptr<NWebWebStorage> GetWebStorage()
89     {
90         return nullptr;
91     }
92 
GetCookieManager()93     std::shared_ptr<NWebCookieManager> GetCookieManager()
94     {
95         return nullptr;
96     }
97 
GetDownloadManager()98     std::shared_ptr<NWebDownloadManager> GetDownloadManager()
99     {
100         return nullptr;
101     }
102 
SetWebTag(int32_t nweb_id, const char* web_tag)103     void SetWebTag(int32_t nweb_id, const char* web_tag) {}
104 
InitializeWebEngine(std::shared_ptr<NWebEngineInitArgs> init_args)105     void InitializeWebEngine(std::shared_ptr<NWebEngineInitArgs> init_args) {}
106 
PrepareForPageLoad(const std::string& url, bool preconnectable, int32_t num_sockets)107     void PrepareForPageLoad(const std::string& url, bool preconnectable, int32_t num_sockets) {}
108 
SetWebDebuggingAccess(bool isEnableDebug)109     void SetWebDebuggingAccess(bool isEnableDebug) {}
110 
AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts)111     void AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts) {}
112 
RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts)113     void RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts) {}
ClearIntelligentTrackingPreventionBypassingList()114     void ClearIntelligentTrackingPreventionBypassingList() {}
115 
GetDefaultUserAgent()116     std::string GetDefaultUserAgent()
117     {
118         return "";
119     }
120 
PauseAllTimers()121     void PauseAllTimers() {}
122 
ResumeAllTimers()123     void ResumeAllTimers() {}
124 
PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs>& pre_args, const std::map<std::string, std::string>& additional_http_headers, const std::string& cache_key, const uint32_t& cache_valid_time)125     void PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs>& pre_args,
126         const std::map<std::string, std::string>& additional_http_headers, const std::string& cache_key,
127         const uint32_t& cache_valid_time)
128     {}
129 
SetRenderProcessMode(RenderProcessMode mode)130     void SetRenderProcessMode(RenderProcessMode mode) {}
131 
GetRenderProcessMode()132     RenderProcessMode GetRenderProcessMode()
133     {
134         return RenderProcessMode::SINGLE_MODE;
135     }
136 
ClearPrefetchedResource(const std::vector<std::string>& cache_key_list)137     void ClearPrefetchedResource(const std::vector<std::string>& cache_key_list) {}
138 
WarmupServiceWorker(const std::string& url)139     void WarmupServiceWorker(const std::string& url) {}
140 
SetHostIP(const std::string& hostName, const std::string& address, int32_t aliveTime)141     void SetHostIP(const std::string& hostName, const std::string& address, int32_t aliveTime) {}
142 
ClearHostIP(const std::string& hostName)143     void ClearHostIP(const std::string& hostName) {}
144 
GetAdsBlockManager()145     std::shared_ptr<NWebAdsBlockManager> GetAdsBlockManager()
146     {
147         return nullptr;
148     }
149 
EnableBackForwardCache(bool nativeEmbed, bool mediaTakeOver)150     void EnableBackForwardCache(bool nativeEmbed, bool mediaTakeOver) {}
151 };
152 
SetUpTestCase(void)153 void NwebHelperTest::SetUpTestCase(void)
154 {
155     RSSurfaceNodeConfig config;
156     config.SurfaceNodeName = "webTestSurfaceName";
157     auto surfaceNode = RSSurfaceNode::Create(config, false);
158     g_surface = surfaceNode->GetSurface();
159 }
160 
TearDownTestCase(void)161 void NwebHelperTest::TearDownTestCase(void) {}
162 
SetUp(void)163 void NwebHelperTest::SetUp(void) {}
164 
TearDown(void)165 void NwebHelperTest::TearDown(void) {}
166 
167 std::unordered_map<std::string, std::string> g_argsMap;
168 
NWebHelperFuzzTest(const uint8_t* data, size_t size)169 bool NWebHelperFuzzTest(const uint8_t* data, size_t size)
170 {
171     int32_t nweb_id = 1;
172     bool result = NWebHelper::Instance().LoadNWebSDK();
173     (void)result;
174     NWebHelper::Instance().SetBundlePath(MOCK_INSTALLATION_DIR);
175     result = NWebAdapterHelper::Instance().Init(false);
176     (void)result;
177     std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
178     std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
179     (void)nweb;
180     std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
181     NWebHelper::Instance().SetHttpDns(config);
182     auto nwebHelper = NWebHelper::Instance().GetNWeb(nweb_id);
183     (void)nwebHelper;
184     NWebHelper::Instance().PrepareForPageLoad("web_test", true, 0);
185     NWebHelper::Instance().WarmupServiceWorker("web_test");
186     NWebHelper::Instance().PrefetchResource(nullptr, {}, "web_test", 0);
187     NWebHelper::Instance().ClearPrefetchedResource({ "web_test" });
188     NWebAdapterHelper::Instance().ReadConfigIfNeeded();
189     result = NWebHelper::Instance().InitAndRun(false);
190     (void)result;
191     ApplicationContextMock* contextMock = new ApplicationContextMock();
192     g_applicationContext.reset(contextMock);
193     result = NWebHelper::Instance().InitAndRun(false);
194     (void)result;
195     NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
196     result = NWebHelper::Instance().InitAndRun(false);
197     (void)result;
198     NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
199     result = NWebHelper::Instance().LoadNWebSDK();
200     (void)result;
201     result = NWebHelper::Instance().LoadNWebSDK();
202     (void)result;
203     WebDownloadManager_PutDownloadCallback(nullptr);
204     g_applicationContext.reset();
205     return true;
206 }
207 
NWebHelperFuzzTest_002(const uint8_t* data, size_t size)208 bool NWebHelperFuzzTest_002(const uint8_t* data, size_t size)
209 {
210     auto web_storage = NWebHelper::Instance().GetWebStorage();
211     bool result = false;
212     if (web_storage != nullptr) {
213         result = true;
214     }
215     (void)result;
216     std::string config = NWebAdapterHelper::Instance().ParsePerfConfig("web", "test");
217     (void)config;
218     NWebConfigHelper::Instance().perfConfig_.emplace("web/test", "web_test");
219     config = NWebAdapterHelper::Instance().ParsePerfConfig("web", "test");
220     (void)config;
221     return true;
222 }
223 
NWebHelperFuzzTest_003(const uint8_t* data, size_t size)224 bool NWebHelperFuzzTest_003(const uint8_t* data, size_t size)
225 {
226     auto dataBase = NWebHelper::Instance().GetDataBase();
227     bool result = false;
228     if (dataBase != nullptr) {
229         result = true;
230     }
231     (void)result;
232 
233     std::shared_ptr<NWebCookieManager> cook = NWebHelper::Instance().GetCookieManager();
234     (void)cook;
235 
236     void* enhanceSurfaceInfo = nullptr;
237     int32_t temp = 1;
238     std::shared_ptr<NWeb> nweb =
239         NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
240     (void)nweb;
241     enhanceSurfaceInfo = static_cast<void*>(&temp);
242     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
243     (void)nweb;
244     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(), DEFAULT_WIDTH, NWEB_MAX_WIDTH);
245     (void)nweb;
246     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(), NWEB_MAX_WIDTH, DEFAULT_HEIGHT);
247     (void)nweb;
248     std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
249     nweb = NWebHelper::Instance().CreateNWeb(create_info);
250     (void)nweb;
251     return true;
252 }
253 
254 } // namespace NWeb
255 } // namespace OHOS
256 
257 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)258 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
259 {
260     /* Run your code on data */
261     OHOS::NWeb::NWebHelperFuzzTest(data, size);
262     OHOS::NWeb::NWebHelperFuzzTest_002(data, size);
263     OHOS::NWeb::NWebHelperFuzzTest_003(data, size);
264     return 0;
265 }
266