1f857971dSopenharmony_ci/* 2f857971dSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f857971dSopenharmony_ci * you may not use this file except in compliance with the License. 5f857971dSopenharmony_ci * You may obtain a copy of the License at 6f857971dSopenharmony_ci * 7f857971dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f857971dSopenharmony_ci * 9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f857971dSopenharmony_ci * See the License for the specific language governing permissions and 13f857971dSopenharmony_ci * limitations under the License. 14f857971dSopenharmony_ci */ 15f857971dSopenharmony_ci 16f857971dSopenharmony_ci#include "dsoftbusadapter_fuzzer.h" 17f857971dSopenharmony_ci 18f857971dSopenharmony_ci#include "singleton.h" 19f857971dSopenharmony_ci 20f857971dSopenharmony_ci#include "devicestatus_define.h" 21f857971dSopenharmony_ci#include "dsoftbus_adapter_impl.h" 22f857971dSopenharmony_ci#include "socket_session_manager.h" 23f857971dSopenharmony_ci 24f857971dSopenharmony_ci#include "message_parcel.h" 25f857971dSopenharmony_ci 26f857971dSopenharmony_ci#undef LOG_TAG 27f857971dSopenharmony_ci#define LOG_TAG "DsoftbusAdapterFuzzTest" 28f857971dSopenharmony_cinamespace OHOS { 29f857971dSopenharmony_cinamespace Msdp { 30f857971dSopenharmony_cinamespace DeviceStatus { 31f857971dSopenharmony_ci#define SERVER_SESSION_NAME "ohos.msdp.device_status.intention.serversession" 32f857971dSopenharmony_ciconst uint8_t *g_baseFuzzData = nullptr; 33f857971dSopenharmony_cisize_t g_baseFuzzSize = 0; 34f857971dSopenharmony_cisize_t g_baseFuzzPos = 0; 35f857971dSopenharmony_ciconstexpr size_t STR_LEN = 255; 36f857971dSopenharmony_ciconstexpr size_t PKG_NAME_SIZE_MAX { 65 }; 37f857971dSopenharmony_ciconstexpr size_t DEVICE_NAME_SIZE_MAX { 256 }; 38f857971dSopenharmony_ci 39f857971dSopenharmony_citemplate <class T> T GetData() 40f857971dSopenharmony_ci{ 41f857971dSopenharmony_ci T objetct{}; 42f857971dSopenharmony_ci size_t objetctSize = sizeof(objetct); 43f857971dSopenharmony_ci if (g_baseFuzzData == nullptr || objetctSize > g_baseFuzzSize - g_baseFuzzPos) { 44f857971dSopenharmony_ci return objetct; 45f857971dSopenharmony_ci } 46f857971dSopenharmony_ci errno_t ret = memcpy_s(&objetct, objetctSize, g_baseFuzzData + g_baseFuzzPos, objetctSize); 47f857971dSopenharmony_ci if (ret != EOK) { 48f857971dSopenharmony_ci return {}; 49f857971dSopenharmony_ci } 50f857971dSopenharmony_ci g_baseFuzzPos += objetctSize; 51f857971dSopenharmony_ci return objetct; 52f857971dSopenharmony_ci} 53f857971dSopenharmony_ci 54f857971dSopenharmony_civoid SetGlobalFuzzData(const uint8_t *data, size_t size) 55f857971dSopenharmony_ci{ 56f857971dSopenharmony_ci g_baseFuzzData = data; 57f857971dSopenharmony_ci g_baseFuzzSize = size; 58f857971dSopenharmony_ci g_baseFuzzPos = 0; 59f857971dSopenharmony_ci} 60f857971dSopenharmony_ci 61f857971dSopenharmony_cistd::string GetStringFromData(int strlen) 62f857971dSopenharmony_ci{ 63f857971dSopenharmony_ci if (strlen < 1) { 64f857971dSopenharmony_ci return ""; 65f857971dSopenharmony_ci } 66f857971dSopenharmony_ci 67f857971dSopenharmony_ci char cstr[strlen]; 68f857971dSopenharmony_ci cstr[strlen - 1] = '\0'; 69f857971dSopenharmony_ci for (int i = 0; i < strlen - 1; i++) { 70f857971dSopenharmony_ci cstr[i] = GetData<char>(); 71f857971dSopenharmony_ci } 72f857971dSopenharmony_ci std::string str(cstr); 73f857971dSopenharmony_ci return str; 74f857971dSopenharmony_ci} 75f857971dSopenharmony_ci 76f857971dSopenharmony_ciclass DSoftbusObserver final : public IDSoftbusObserver { 77f857971dSopenharmony_cipublic: 78f857971dSopenharmony_ci DSoftbusObserver() = default; 79f857971dSopenharmony_ci ~DSoftbusObserver() = default; 80f857971dSopenharmony_ci 81f857971dSopenharmony_ci void OnBind(const std::string &networkId) {} 82f857971dSopenharmony_ci void OnShutdown(const std::string &networkId) {} 83f857971dSopenharmony_ci void OnConnected(const std::string &networkId) {} 84f857971dSopenharmony_ci bool OnPacket(const std::string &networkId, NetPacket &packet) 85f857971dSopenharmony_ci { 86f857971dSopenharmony_ci return true; 87f857971dSopenharmony_ci } 88f857971dSopenharmony_ci bool OnRawData(const std::string &networkId, const void *data, uint32_t dataLen) 89f857971dSopenharmony_ci { 90f857971dSopenharmony_ci return true; 91f857971dSopenharmony_ci } 92f857971dSopenharmony_ci}; 93f857971dSopenharmony_ci 94f857971dSopenharmony_cibool EnableFuzzTest(const uint8_t* data, size_t size) 95f857971dSopenharmony_ci{ 96f857971dSopenharmony_ci if ((data == nullptr) || (size < 1)) { 97f857971dSopenharmony_ci return false; 98f857971dSopenharmony_ci } 99f857971dSopenharmony_ci SetGlobalFuzzData(data, size); 100f857971dSopenharmony_ci 101f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->Enable(); 102f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->SetupServer(); 103f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->ShutdownServer(); 104f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->CloseAllSessions(); 105f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->CloseAllSessionsLocked(); 106f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->Disable(); 107f857971dSopenharmony_ci return true; 108f857971dSopenharmony_ci} 109f857971dSopenharmony_ci 110f857971dSopenharmony_cibool AddObserverFuzzTest(const uint8_t* data, size_t size) 111f857971dSopenharmony_ci{ 112f857971dSopenharmony_ci if ((data == nullptr) || (size < 1)) { 113f857971dSopenharmony_ci return false; 114f857971dSopenharmony_ci } 115f857971dSopenharmony_ci SetGlobalFuzzData(data, size); 116f857971dSopenharmony_ci 117f857971dSopenharmony_ci std::shared_ptr<IDSoftbusObserver> observer = std::make_shared<DSoftbusObserver>(); 118f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->AddObserver(observer); 119f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->RemoveObserver(observer); 120f857971dSopenharmony_ci return true; 121f857971dSopenharmony_ci} 122f857971dSopenharmony_ci 123f857971dSopenharmony_cibool CheckDeviceOnlineFuzzTest(const uint8_t* data, size_t size) 124f857971dSopenharmony_ci{ 125f857971dSopenharmony_ci if ((data == nullptr) || (size < 1)) { 126f857971dSopenharmony_ci return false; 127f857971dSopenharmony_ci } 128f857971dSopenharmony_ci SetGlobalFuzzData(data, size); 129f857971dSopenharmony_ci 130f857971dSopenharmony_ci std::string networkId = GetStringFromData(STR_LEN); 131f857971dSopenharmony_ci CircleStreamBuffer circleBuffer; 132f857971dSopenharmony_ci 133f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->CheckDeviceOnline(networkId); 134f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->CloseSession(networkId); 135f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->HandleSessionData(networkId, circleBuffer); 136f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->OpenSessionLocked(networkId); 137f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->OnConnectedLocked(networkId); 138f857971dSopenharmony_ci return true; 139f857971dSopenharmony_ci} 140f857971dSopenharmony_ci 141f857971dSopenharmony_cibool OpenSessionFuzzTest(const uint8_t* data, size_t size) 142f857971dSopenharmony_ci{ 143f857971dSopenharmony_ci if ((data == nullptr) || (size < 1)) { 144f857971dSopenharmony_ci return false; 145f857971dSopenharmony_ci } 146f857971dSopenharmony_ci SetGlobalFuzzData(data, size); 147f857971dSopenharmony_ci 148f857971dSopenharmony_ci std::string networkId = GetStringFromData(STR_LEN); 149f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->OpenSession(networkId); 150f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->FindConnection(networkId); 151f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->CloseSession(networkId); 152f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->CloseAllSessions(); 153f857971dSopenharmony_ci return true; 154f857971dSopenharmony_ci} 155f857971dSopenharmony_ci 156f857971dSopenharmony_ci 157f857971dSopenharmony_cibool SendPacketFuzzTest(const uint8_t* data, size_t size) 158f857971dSopenharmony_ci{ 159f857971dSopenharmony_ci if ((data == nullptr) || (size < 1)) { 160f857971dSopenharmony_ci return false; 161f857971dSopenharmony_ci } 162f857971dSopenharmony_ci SetGlobalFuzzData(data, size); 163f857971dSopenharmony_ci 164f857971dSopenharmony_ci Parcel parcel; 165f857971dSopenharmony_ci NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE); 166f857971dSopenharmony_ci std::string networkId = GetStringFromData(STR_LEN); 167f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->SendPacket(networkId, packet); 168f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->SendParcel(networkId, parcel); 169f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->BroadcastPacket(packet); 170f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->HandlePacket(networkId, packet); 171f857971dSopenharmony_ci return true; 172f857971dSopenharmony_ci} 173f857971dSopenharmony_ci 174f857971dSopenharmony_cibool InitSocketFuzzTest(const uint8_t* data, size_t size) 175f857971dSopenharmony_ci{ 176f857971dSopenharmony_ci if ((data == nullptr) || (size < 1)) { 177f857971dSopenharmony_ci return false; 178f857971dSopenharmony_ci } 179f857971dSopenharmony_ci SetGlobalFuzzData(data, size); 180f857971dSopenharmony_ci 181f857971dSopenharmony_ci int32_t socket = GetData<int32_t>(); 182f857971dSopenharmony_ci uint32_t dataLen = GetData<uint32_t>(); 183f857971dSopenharmony_ci std::string networkId = GetStringFromData(STR_LEN); 184f857971dSopenharmony_ci int32_t *g_data = new int32_t(socket); 185f857971dSopenharmony_ci 186f857971dSopenharmony_ci char name[DEVICE_NAME_SIZE_MAX] { SERVER_SESSION_NAME }; 187f857971dSopenharmony_ci char pkgName[PKG_NAME_SIZE_MAX] { FI_PKG_NAME }; 188f857971dSopenharmony_ci SocketInfo info { 189f857971dSopenharmony_ci .name = name, 190f857971dSopenharmony_ci .pkgName = pkgName, 191f857971dSopenharmony_ci .dataType = DATA_TYPE_BYTES 192f857971dSopenharmony_ci }; 193f857971dSopenharmony_ci 194f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->InitSocket(info, socket, socket); 195f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->ConfigTcpAlive(socket); 196f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->OnShutdown(socket, SHUTDOWN_REASON_UNKNOWN); 197f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->OnBytes(socket, g_data, dataLen); 198f857971dSopenharmony_ci DSoftbusAdapterImpl::GetInstance()->HandleRawData(networkId, g_data, dataLen); 199f857971dSopenharmony_ci return true; 200f857971dSopenharmony_ci} 201f857971dSopenharmony_ci 202f857971dSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 203f857971dSopenharmony_ci{ 204f857971dSopenharmony_ci /* Run your code on data */ 205f857971dSopenharmony_ci if (data == nullptr) { 206f857971dSopenharmony_ci return 0; 207f857971dSopenharmony_ci } 208f857971dSopenharmony_ci 209f857971dSopenharmony_ci OHOS::Msdp::DeviceStatus::EnableFuzzTest(data, size); 210f857971dSopenharmony_ci OHOS::Msdp::DeviceStatus::AddObserverFuzzTest(data, size); 211f857971dSopenharmony_ci OHOS::Msdp::DeviceStatus::CheckDeviceOnlineFuzzTest(data, size); 212f857971dSopenharmony_ci OHOS::Msdp::DeviceStatus::OpenSessionFuzzTest(data, size); 213f857971dSopenharmony_ci OHOS::Msdp::DeviceStatus::SendPacketFuzzTest(data, size); 214f857971dSopenharmony_ci OHOS::Msdp::DeviceStatus::InitSocketFuzzTest(data, size); 215f857971dSopenharmony_ci return 0; 216f857971dSopenharmony_ci} 217f857971dSopenharmony_ci} // namespace DeviceStatus 218f857971dSopenharmony_ci} // namespace Msdp 219f857971dSopenharmony_ci} // namespace OHOS