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 <csignal> 231e934351Sopenharmony_ci#include <cstring> 241e934351Sopenharmony_ci#include <functional> 251e934351Sopenharmony_ci#include <iostream> 261e934351Sopenharmony_ci 271e934351Sopenharmony_ci#include "net_websocket.h" 281e934351Sopenharmony_ci#include "netstack_log.h" 291e934351Sopenharmony_ci#include "secure_char.h" 301e934351Sopenharmony_ci 311e934351Sopenharmony_cinamespace OHOS { 321e934351Sopenharmony_cinamespace NetStack { 331e934351Sopenharmony_cinamespace WebSocketClient { 341e934351Sopenharmony_ciconst uint8_t *g_baseFuzzData = nullptr; 351e934351Sopenharmony_cisize_t g_baseFuzzSize = 0; 361e934351Sopenharmony_cisize_t g_baseFuzzPos = 0; 371e934351Sopenharmony_ciconstexpr size_t STR_LEN = 255; 381e934351Sopenharmony_ciconst int32_t LWS_CLOSE_STATUS_NORMAL = 1000; 391e934351Sopenharmony_citemplate <class T> T GetData() 401e934351Sopenharmony_ci{ 411e934351Sopenharmony_ci T object{}; 421e934351Sopenharmony_ci size_t objectSize = sizeof(object); 431e934351Sopenharmony_ci if (g_baseFuzzData == nullptr || g_baseFuzzSize <= g_baseFuzzPos || objectSize > g_baseFuzzSize - g_baseFuzzPos) { 441e934351Sopenharmony_ci return object; 451e934351Sopenharmony_ci } 461e934351Sopenharmony_ci errno_t ret = memcpy_s(&object, objectSize, g_baseFuzzData + g_baseFuzzPos, objectSize); 471e934351Sopenharmony_ci if (ret != EOK) { 481e934351Sopenharmony_ci return object; 491e934351Sopenharmony_ci } 501e934351Sopenharmony_ci g_baseFuzzPos += objectSize; 511e934351Sopenharmony_ci return object; 521e934351Sopenharmony_ci} 531e934351Sopenharmony_ci 541e934351Sopenharmony_civoid SetGlobalFuzzData(const uint8_t *data, size_t size) 551e934351Sopenharmony_ci{ 561e934351Sopenharmony_ci g_baseFuzzData = data; 571e934351Sopenharmony_ci g_baseFuzzSize = size; 581e934351Sopenharmony_ci g_baseFuzzPos = 0; 591e934351Sopenharmony_ci} 601e934351Sopenharmony_ci 611e934351Sopenharmony_cistd::string GetStringFromData(int strlen) 621e934351Sopenharmony_ci{ 631e934351Sopenharmony_ci if (strlen < 1) { 641e934351Sopenharmony_ci return ""; 651e934351Sopenharmony_ci } 661e934351Sopenharmony_ci 671e934351Sopenharmony_ci char cstr[strlen]; 681e934351Sopenharmony_ci cstr[strlen - 1] = '\0'; 691e934351Sopenharmony_ci for (int i = 0; i < strlen - 1; i++) { 701e934351Sopenharmony_ci cstr[i] = GetData<char>(); 711e934351Sopenharmony_ci } 721e934351Sopenharmony_ci std::string str(cstr); 731e934351Sopenharmony_ci return str; 741e934351Sopenharmony_ci} 751e934351Sopenharmony_ci 761e934351Sopenharmony_civoid SetAddHeaderTest(const uint8_t *data, size_t size) 771e934351Sopenharmony_ci{ 781e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 791e934351Sopenharmony_ci return; 801e934351Sopenharmony_ci } 811e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 821e934351Sopenharmony_ci std::string str = GetStringFromData(STR_LEN); 831e934351Sopenharmony_ci struct WebSocket *client = nullptr; 841e934351Sopenharmony_ci struct WebSocket_Header header1; 851e934351Sopenharmony_ci header1.fieldName = str.c_str(); 861e934351Sopenharmony_ci header1.fieldValue = str.c_str(); 871e934351Sopenharmony_ci header1.next = nullptr; 881e934351Sopenharmony_ci OH_WebSocketClient_AddHeader(client, header1); 891e934351Sopenharmony_ci} 901e934351Sopenharmony_ci 911e934351Sopenharmony_civoid SetRequestOptionsTest(const uint8_t *data, size_t size) 921e934351Sopenharmony_ci{ 931e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 941e934351Sopenharmony_ci return; 951e934351Sopenharmony_ci } 961e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 971e934351Sopenharmony_ci struct WebSocket *client = new WebSocket(); 981e934351Sopenharmony_ci const char *url1 = "www.baidu.com"; 991e934351Sopenharmony_ci 1001e934351Sopenharmony_ci OH_WebSocketClient_Connect(client, url1, client->requestOptions); 1011e934351Sopenharmony_ci} 1021e934351Sopenharmony_ci 1031e934351Sopenharmony_civoid SetConnectUrlTest(const uint8_t *data, size_t size) 1041e934351Sopenharmony_ci{ 1051e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 1061e934351Sopenharmony_ci return; 1071e934351Sopenharmony_ci } 1081e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 1091e934351Sopenharmony_ci std::string str = GetStringFromData(STR_LEN); 1101e934351Sopenharmony_ci struct WebSocket *client = new WebSocket(); 1111e934351Sopenharmony_ci const char *url1 = str.c_str(); 1121e934351Sopenharmony_ci OH_WebSocketClient_Connect(client, url1, client->requestOptions); 1131e934351Sopenharmony_ci} 1141e934351Sopenharmony_ci 1151e934351Sopenharmony_civoid SetSendDataTest(const uint8_t *data, size_t size) 1161e934351Sopenharmony_ci{ 1171e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 1181e934351Sopenharmony_ci return; 1191e934351Sopenharmony_ci } 1201e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 1211e934351Sopenharmony_ci struct WebSocket *client = new WebSocket(); 1221e934351Sopenharmony_ci const char *senddata = "Hello, world,websocket!"; 1231e934351Sopenharmony_ci int32_t sendLength = std::strlen(senddata); 1241e934351Sopenharmony_ci OH_WebSocketClient_Send(client, const_cast<char *>(senddata), sendLength); 1251e934351Sopenharmony_ci} 1261e934351Sopenharmony_ci 1271e934351Sopenharmony_civoid SetSendDataLengthTest(const uint8_t *data, size_t size) 1281e934351Sopenharmony_ci{ 1291e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 1301e934351Sopenharmony_ci return; 1311e934351Sopenharmony_ci } 1321e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 1331e934351Sopenharmony_ci struct WebSocket *client = new WebSocket(); 1341e934351Sopenharmony_ci const char *senddata = "Hello, world,websocket!"; 1351e934351Sopenharmony_ci int32_t sendLength = size; 1361e934351Sopenharmony_ci OH_WebSocketClient_Send(client, const_cast<char *>(senddata), sendLength); 1371e934351Sopenharmony_ci} 1381e934351Sopenharmony_ci 1391e934351Sopenharmony_civoid SetCloseOptionTest(const uint8_t *data, size_t size) 1401e934351Sopenharmony_ci{ 1411e934351Sopenharmony_ci if ((data == nullptr) || (size < 1)) { 1421e934351Sopenharmony_ci return; 1431e934351Sopenharmony_ci } 1441e934351Sopenharmony_ci SetGlobalFuzzData(data, size); 1451e934351Sopenharmony_ci struct WebSocket *client = new WebSocket(); 1461e934351Sopenharmony_ci 1471e934351Sopenharmony_ci struct WebSocket_CloseOption CloseOption; 1481e934351Sopenharmony_ci CloseOption.code = LWS_CLOSE_STATUS_NORMAL; 1491e934351Sopenharmony_ci CloseOption.reason = " "; 1501e934351Sopenharmony_ci OH_WebSocketClient_Close(client, CloseOption); 1511e934351Sopenharmony_ci} 1521e934351Sopenharmony_ci 1531e934351Sopenharmony_ci} // namespace WebSocketClient 1541e934351Sopenharmony_ci} // namespace NetStack 1551e934351Sopenharmony_ci} // namespace OHOS 1561e934351Sopenharmony_ci 1571e934351Sopenharmony_ci/* Fuzzer entry point */ 1581e934351Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 1591e934351Sopenharmony_ci{ 1601e934351Sopenharmony_ci /* Run your code on data */ 1611e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetAddHeaderTest(data, size); 1621e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetRequestOptionsTest(data, size); 1631e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetConnectUrlTest(data, size); 1641e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetSendDataTest(data, size); 1651e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetSendDataLengthTest(data, size); 1661e934351Sopenharmony_ci OHOS::NetStack::WebSocketClient::SetCloseOptionTest(data, size); 1671e934351Sopenharmony_ci return 0; 1681e934351Sopenharmony_ci}