11e934351Sopenharmony_ci/* 21e934351Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 31e934351Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 41e934351Sopenharmony_ci * you may not use this file except in compliance with the License. 51e934351Sopenharmony_ci * You may obtain a copy of the License at 61e934351Sopenharmony_ci * 71e934351Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 81e934351Sopenharmony_ci * 91e934351Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 101e934351Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 111e934351Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121e934351Sopenharmony_ci * See the License for the specific language governing permissions and 131e934351Sopenharmony_ci * limitations under the License. 141e934351Sopenharmony_ci */ 151e934351Sopenharmony_ci 161e934351Sopenharmony_ci#include <cstring> 171e934351Sopenharmony_ci#include <map> 181e934351Sopenharmony_ci#include <securec.h> 191e934351Sopenharmony_ci#include <string> 201e934351Sopenharmony_ci#include <vector> 211e934351Sopenharmony_ci 221e934351Sopenharmony_ci#include "netstack_log.h" 231e934351Sopenharmony_ci#include "secure_char.h" 241e934351Sopenharmony_ci#include "websocket_client_innerapi.h" 251e934351Sopenharmony_ci 261e934351Sopenharmony_cinamespace OHOS { 271e934351Sopenharmony_cinamespace NetStack { 281e934351Sopenharmony_cinamespace WebSocketClient { 291e934351Sopenharmony_cinamespace { 301e934351Sopenharmony_ciOpenOptions openOptions; 311e934351Sopenharmony_cistd::map<std::string, std::string> headers = { 321e934351Sopenharmony_ci {"Content-Type", "application/json"}, 331e934351Sopenharmony_ci {"Authorization", "Bearer your_token_here"}, 341e934351Sopenharmony_ci}; 351e934351Sopenharmony_ciconst uint8_t *g_baseFuzzData = nullptr; 361e934351Sopenharmony_cisize_t g_baseFuzzSize = 0; 371e934351Sopenharmony_cisize_t g_baseFuzzPos = 0; 381e934351Sopenharmony_ci[[maybe_unused]] constexpr size_t STR_LEN = 255; 391e934351Sopenharmony_ci} // namespace 401e934351Sopenharmony_citemplate <class T> T GetData() 411e934351Sopenharmony_ci{ 421e934351Sopenharmony_ci T object{}; 431e934351Sopenharmony_ci size_t objectSize = sizeof(object); 441e934351Sopenharmony_ci if (g_baseFuzzData == nullptr || g_baseFuzzSize <= g_baseFuzzPos || objectSize > g_baseFuzzSize - g_baseFuzzPos) { 451e934351Sopenharmony_ci return object; 461e934351Sopenharmony_ci } 471e934351Sopenharmony_ci errno_t ret = memcpy_s(&object, objectSize, g_baseFuzzData + g_baseFuzzPos, objectSize); 481e934351Sopenharmony_ci if (ret != EOK) { 491e934351Sopenharmony_ci return object; 501e934351Sopenharmony_ci } 511e934351Sopenharmony_ci g_baseFuzzPos += objectSize; 521e934351Sopenharmony_ci return object; 531e934351Sopenharmony_ci} 541e934351Sopenharmony_ci 551e934351Sopenharmony_civoid SetGlobalFuzzData(const uint8_t *data, size_t size) 561e934351Sopenharmony_ci{ 571e934351Sopenharmony_ci g_baseFuzzData = data; 581e934351Sopenharmony_ci g_baseFuzzSize = size; 591e934351Sopenharmony_ci g_baseFuzzPos = 0; 601e934351Sopenharmony_ci} 611e934351Sopenharmony_ci 621e934351Sopenharmony_cistd::string GetStringFromData(int strlen) 631e934351Sopenharmony_ci{ 641e934351Sopenharmony_ci if (strlen < 1) { 651e934351Sopenharmony_ci return ""; 661e934351Sopenharmony_ci } 671e934351Sopenharmony_ci 681e934351Sopenharmony_ci char cstr[strlen]; 691e934351Sopenharmony_ci cstr[strlen - 1] = '\0'; 701e934351Sopenharmony_ci for (int i = 0; i < strlen - 1; i++) { 711e934351Sopenharmony_ci cstr[i] = GetData<char>(); 721e934351Sopenharmony_ci } 731e934351Sopenharmony_ci std::string str(cstr); 741e934351Sopenharmony_ci return str; 751e934351Sopenharmony_ci} 761e934351Sopenharmony_ci 771e934351Sopenharmony_cistatic void OnMessage(WebSocketClient *client, const std::string &data, size_t length) {} 781e934351Sopenharmony_ci 791e934351Sopenharmony_cistatic void OnOpen(WebSocketClient *client, OpenResult openResult) {} 801e934351Sopenharmony_ci 811e934351Sopenharmony_cistatic void OnError(WebSocketClient *client, ErrorResult error) {} 821e934351Sopenharmony_ci 831e934351Sopenharmony_cistatic void OnClose(WebSocketClient *client, CloseResult closeResult) {} 841e934351Sopenharmony_ci 851e934351Sopenharmony_civoid SetRequestOptionsTest(const uint8_t *data, size_t size) 861e934351Sopenharmony_ci{ 871e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 881e934351Sopenharmony_ci return; 891e934351Sopenharmony_ci } 901e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 911e934351Sopenharmony_ci std::string str = GetStringFromData(STR_LEN); 921e934351Sopenharmony_ci openOptions.headers["Content-Type"] = str; 931e934351Sopenharmony_ci openOptions.headers["Authorization"] = str; 941e934351Sopenharmony_ci WebSocketClient *client = new WebSocketClient(); 951e934351Sopenharmony_ci client->Registcallback(OnOpen, OnMessage, OnError, OnClose); 961e934351Sopenharmony_ci client->Connect("www.baidu.com", openOptions); 971e934351Sopenharmony_ci} 981e934351Sopenharmony_ci 991e934351Sopenharmony_civoid SetConnectUrlTest(const uint8_t *data, size_t size) 1001e934351Sopenharmony_ci{ 1011e934351Sopenharmony_ci if (size < 1 || data == nullptr) { 1021e934351Sopenharmony_ci return; 1031e934351Sopenharmony_ci } 1041e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 1051e934351Sopenharmony_ci std::string str = GetStringFromData(STR_LEN); 1061e934351Sopenharmony_ci openOptions.headers["Authorization"] = "Bearer your_token_here"; 1071e934351Sopenharmony_ci openOptions.headers["Content-Type"] = "application/json"; 1081e934351Sopenharmony_ci WebSocketClient *client = new WebSocketClient(); 1091e934351Sopenharmony_ci client->Registcallback(OnOpen, OnMessage, OnError, OnClose); 1101e934351Sopenharmony_ci client->Connect(str, openOptions); 1111e934351Sopenharmony_ci} 1121e934351Sopenharmony_ci 1131e934351Sopenharmony_civoid SetSendDataTest(const uint8_t *data, size_t size) 1141e934351Sopenharmony_ci{ 1151e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 1161e934351Sopenharmony_ci return; 1171e934351Sopenharmony_ci } 1181e934351Sopenharmony_ci openOptions.headers["Authorization"] = "Bearer your_token_here"; 1191e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 1201e934351Sopenharmony_ci std::string str = GetStringFromData(STR_LEN); 1211e934351Sopenharmony_ci openOptions.headers["Content-Type"] = "application/json"; 1221e934351Sopenharmony_ci WebSocketClient *client = new WebSocketClient(); 1231e934351Sopenharmony_ci client->Registcallback(OnOpen, OnMessage, OnError, OnClose); 1241e934351Sopenharmony_ci client->Connect("www.baidu.com", openOptions); 1251e934351Sopenharmony_ci const char *data1 = str.c_str(); 1261e934351Sopenharmony_ci int32_t sendLength = std::strlen(data1); 1271e934351Sopenharmony_ci client->Send(const_cast<char *>(data1), sendLength); 1281e934351Sopenharmony_ci} 1291e934351Sopenharmony_ci 1301e934351Sopenharmony_civoid SetSendDataLengthTest(const uint8_t *data, size_t size) 1311e934351Sopenharmony_ci{ 1321e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 1331e934351Sopenharmony_ci return; 1341e934351Sopenharmony_ci } 1351e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 1361e934351Sopenharmony_ci openOptions.headers["Content-Type"] = "application/json"; 1371e934351Sopenharmony_ci openOptions.headers["Authorization"] = "Bearer your_token_here"; 1381e934351Sopenharmony_ci WebSocketClient *client = new WebSocketClient(); 1391e934351Sopenharmony_ci client->Registcallback(OnOpen, OnMessage, OnError, OnClose); 1401e934351Sopenharmony_ci client->Connect("www.baidu.com", openOptions); 1411e934351Sopenharmony_ci const char *data1 = "Hello,world!"; 1421e934351Sopenharmony_ci client->Send(const_cast<char *>(data1), size); 1431e934351Sopenharmony_ci} 1441e934351Sopenharmony_ci 1451e934351Sopenharmony_civoid SetCloseOptionTest(const uint8_t *data, size_t size) 1461e934351Sopenharmony_ci{ 1471e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 1481e934351Sopenharmony_ci return; 1491e934351Sopenharmony_ci } 1501e934351Sopenharmony_ci openOptions.headers["Content-Type"] = "application/json"; 1511e934351Sopenharmony_ci openOptions.headers["Authorization"] = "Bearer your_token_here"; 1521e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 1531e934351Sopenharmony_ci std::string str = GetStringFromData(STR_LEN); 1541e934351Sopenharmony_ci 1551e934351Sopenharmony_ci WebSocketClient *client = new WebSocketClient(); 1561e934351Sopenharmony_ci client->Registcallback(OnOpen, OnMessage, OnError, OnClose); 1571e934351Sopenharmony_ci client->Connect("www.baidu.com", openOptions); 1581e934351Sopenharmony_ci CloseOption CloseOptions; 1591e934351Sopenharmony_ci CloseOptions.code = size; 1601e934351Sopenharmony_ci CloseOptions.reason = str.c_str(); 1611e934351Sopenharmony_ci client->Close(CloseOptions); 1621e934351Sopenharmony_ci} 1631e934351Sopenharmony_ci 1641e934351Sopenharmony_ci} // namespace WebSocketClient 1651e934351Sopenharmony_ci} // namespace NetStack 1661e934351Sopenharmony_ci} // namespace OHOS 1671e934351Sopenharmony_ci 1681e934351Sopenharmony_ci/* Fuzzer entry point */ 1691e934351Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 1701e934351Sopenharmony_ci{ 1711e934351Sopenharmony_ci /* Run your code on data */ 1721e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetRequestOptionsTest(data, size); 1731e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetConnectUrlTest(data, size); 1741e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetSendDataTest(data, size); 1751e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetSendDataLengthTest(data, size); 1761e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetCloseOptionTest(data, size); 1771e934351Sopenharmony_ci return 0; 1781e934351Sopenharmony_ci}