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 
SetWholeWebDrawing()145     void SetWholeWebDrawing() {}
146 
GetAdsBlockManager()147     std::shared_ptr<NWebAdsBlockManager> GetAdsBlockManager()
148     {
149         return nullptr;
150     }
151 
EnableBackForwardCache(bool nativeEmbed,bool mediaTakeOver)152     void EnableBackForwardCache(bool nativeEmbed, bool mediaTakeOver) {}
153 };
154 
SetUpTestCase(void)155 void NwebHelperTest::SetUpTestCase(void)
156 {
157     RSSurfaceNodeConfig config;
158     config.SurfaceNodeName = "webTestSurfaceName";
159     auto surfaceNode = RSSurfaceNode::Create(config, false);
160     g_surface = surfaceNode->GetSurface();
161 }
162 
TearDownTestCase(void)163 void NwebHelperTest::TearDownTestCase(void) {}
164 
SetUp(void)165 void NwebHelperTest::SetUp(void) {}
166 
TearDown(void)167 void NwebHelperTest::TearDown(void) {}
168 
169 std::unordered_map<std::string, std::string> g_argsMap;
170 
NWebHelperFuzzTest(const uint8_t * data,size_t size)171 bool NWebHelperFuzzTest(const uint8_t* data, size_t size)
172 {
173     int32_t nweb_id = 1;
174     bool result = NWebHelper::Instance().LoadNWebSDK();
175     (void)result;
176     NWebHelper::Instance().SetBundlePath(MOCK_INSTALLATION_DIR);
177     result = NWebAdapterHelper::Instance().Init(false);
178     (void)result;
179     std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
180     std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
181     (void)nweb;
182     std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
183     NWebHelper::Instance().SetHttpDns(config);
184     auto nwebHelper = NWebHelper::Instance().GetNWeb(nweb_id);
185     (void)nwebHelper;
186     NWebHelper::Instance().PrepareForPageLoad("web_test", true, 0);
187     NWebHelper::Instance().WarmupServiceWorker("web_test");
188     NWebHelper::Instance().PrefetchResource(nullptr, {}, "web_test", 0);
189     NWebHelper::Instance().ClearPrefetchedResource({ "web_test" });
190     NWebAdapterHelper::Instance().ReadConfigIfNeeded();
191     result = NWebHelper::Instance().InitAndRun(false);
192     (void)result;
193     ApplicationContextMock* contextMock = new ApplicationContextMock();
194     g_applicationContext.reset(contextMock);
195     result = NWebHelper::Instance().InitAndRun(false);
196     (void)result;
197     NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
198     result = NWebHelper::Instance().InitAndRun(false);
199     (void)result;
200     NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
201     result = NWebHelper::Instance().LoadNWebSDK();
202     (void)result;
203     result = NWebHelper::Instance().LoadNWebSDK();
204     (void)result;
205     WebDownloadManager_PutDownloadCallback(nullptr);
206     g_applicationContext.reset();
207     return true;
208 }
209 
NWebHelperFuzzTest_002(const uint8_t * data,size_t size)210 bool NWebHelperFuzzTest_002(const uint8_t* data, size_t size)
211 {
212     auto web_storage = NWebHelper::Instance().GetWebStorage();
213     bool result = false;
214     if (web_storage != nullptr) {
215         result = true;
216     }
217     (void)result;
218     std::string config = NWebAdapterHelper::Instance().ParsePerfConfig("web", "test");
219     (void)config;
220     NWebConfigHelper::Instance().perfConfig_.emplace("web/test", "web_test");
221     config = NWebAdapterHelper::Instance().ParsePerfConfig("web", "test");
222     (void)config;
223     return true;
224 }
225 
NWebHelperFuzzTest_003(const uint8_t * data,size_t size)226 bool NWebHelperFuzzTest_003(const uint8_t* data, size_t size)
227 {
228     auto dataBase = NWebHelper::Instance().GetDataBase();
229     bool result = false;
230     if (dataBase != nullptr) {
231         result = true;
232     }
233     (void)result;
234 
235     std::shared_ptr<NWebCookieManager> cook = NWebHelper::Instance().GetCookieManager();
236     (void)cook;
237 
238     void* enhanceSurfaceInfo = nullptr;
239     int32_t temp = 1;
240     std::shared_ptr<NWeb> nweb =
241         NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
242     (void)nweb;
243     enhanceSurfaceInfo = static_cast<void*>(&temp);
244     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
245     (void)nweb;
246     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(), DEFAULT_WIDTH, NWEB_MAX_WIDTH);
247     (void)nweb;
248     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(), NWEB_MAX_WIDTH, DEFAULT_HEIGHT);
249     (void)nweb;
250     std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
251     nweb = NWebHelper::Instance().CreateNWeb(create_info);
252     (void)nweb;
253     return true;
254 }
255 
256 } // namespace NWeb
257 } // namespace OHOS
258 
259 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)260 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
261 {
262     /* Run your code on data */
263     OHOS::NWeb::NWebHelperFuzzTest(data, size);
264     OHOS::NWeb::NWebHelperFuzzTest_002(data, size);
265     OHOS::NWeb::NWebHelperFuzzTest_003(data, size);
266     return 0;
267 }
268