1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "napicommonwant_fuzzer.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include <cstddef> 19eace7efcSopenharmony_ci#include <cstdint> 20eace7efcSopenharmony_ci 21eace7efcSopenharmony_ci#define private public 22eace7efcSopenharmony_ci#define protected public 23eace7efcSopenharmony_ci#include "napi_common_want.h" 24eace7efcSopenharmony_ci#undef protected 25eace7efcSopenharmony_ci#undef private 26eace7efcSopenharmony_ci 27eace7efcSopenharmony_ci#include "ability_record.h" 28eace7efcSopenharmony_ci#include "array_wrapper.h" 29eace7efcSopenharmony_ci#include "bool_wrapper.h" 30eace7efcSopenharmony_ci#include "byte_wrapper.h" 31eace7efcSopenharmony_ci#include "double_wrapper.h" 32eace7efcSopenharmony_ci#include "float_wrapper.h" 33eace7efcSopenharmony_ci#include "int_wrapper.h" 34eace7efcSopenharmony_ci#include "long_wrapper.h" 35eace7efcSopenharmony_ci#include "short_wrapper.h" 36eace7efcSopenharmony_ci#include "string_wrapper.h" 37eace7efcSopenharmony_ci#include "zchar_wrapper.h" 38eace7efcSopenharmony_ci#include "remote_object_wrapper.h" 39eace7efcSopenharmony_ci#include "js_runtime_lite.h" 40eace7efcSopenharmony_ci#include "js_environment.h" 41eace7efcSopenharmony_ci 42eace7efcSopenharmony_ciusing namespace OHOS::AAFwk; 43eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk; 44eace7efcSopenharmony_ciusing namespace OHOS::AbilityRuntime; 45eace7efcSopenharmony_ci 46eace7efcSopenharmony_cinamespace OHOS { 47eace7efcSopenharmony_cinamespace { 48eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0; 49eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1; 50eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3; 51eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024; 52eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 53eace7efcSopenharmony_ciconstexpr uint8_t ENABLE = 2; 54eace7efcSopenharmony_ciconstexpr size_t OFFSET_ZERO = 24; 55eace7efcSopenharmony_ciconstexpr size_t OFFSET_ONE = 16; 56eace7efcSopenharmony_ciconstexpr size_t OFFSET_TWO = 8; 57eace7efcSopenharmony_ci} 58eace7efcSopenharmony_ci 59eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr) 60eace7efcSopenharmony_ci{ 61eace7efcSopenharmony_ci // convert fuzz input data to an integer 62eace7efcSopenharmony_ci return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) | 63eace7efcSopenharmony_ci ptr[INPUT_THREE]; 64eace7efcSopenharmony_ci} 65eace7efcSopenharmony_ci 66eace7efcSopenharmony_cisptr<Token> GetFuzzAbilityToken() 67eace7efcSopenharmony_ci{ 68eace7efcSopenharmony_ci sptr<Token> token = nullptr; 69eace7efcSopenharmony_ci AbilityRequest abilityRequest; 70eace7efcSopenharmony_ci abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; 71eace7efcSopenharmony_ci abilityRequest.abilityInfo.name = "MainAbility"; 72eace7efcSopenharmony_ci abilityRequest.abilityInfo.type = AbilityType::DATA; 73eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); 74eace7efcSopenharmony_ci if (abilityRecord) { 75eace7efcSopenharmony_ci token = abilityRecord->GetToken(); 76eace7efcSopenharmony_ci } 77eace7efcSopenharmony_ci return token; 78eace7efcSopenharmony_ci} 79eace7efcSopenharmony_ci 80eace7efcSopenharmony_civoid NapiCommonWantFuzztest1(bool boolParam, std::string &stringParam, int32_t int32Param) 81eace7efcSopenharmony_ci{ 82eace7efcSopenharmony_ci napi_env env = nullptr; 83eace7efcSopenharmony_ci ElementName elementName; 84eace7efcSopenharmony_ci elementName.SetDeviceID(stringParam); 85eace7efcSopenharmony_ci elementName.SetBundleName(stringParam); 86eace7efcSopenharmony_ci elementName.SetAbilityName(stringParam); 87eace7efcSopenharmony_ci elementName.SetModuleName(stringParam); 88eace7efcSopenharmony_ci WrapElementName(env, elementName); // branch failed 89eace7efcSopenharmony_ci napi_value param = nullptr; 90eace7efcSopenharmony_ci UnwrapElementName(env, param, elementName); // branch failed 91eace7efcSopenharmony_ci AAFwk::WantParams wantParams1; 92eace7efcSopenharmony_ci WrapWantParams(env, wantParams1); // branch failed 93eace7efcSopenharmony_ci wantParams1.SetParam("intf1", String::Box(stringParam)); 94eace7efcSopenharmony_ci wantParams1.SetParam("intf2", Long::Box(int32Param)); 95eace7efcSopenharmony_ci wantParams1.SetParam("intf3", Boolean::Box(boolParam)); 96eace7efcSopenharmony_ci wantParams1.SetParam("intf4", Integer::Box(int32Param)); 97eace7efcSopenharmony_ci wantParams1.SetParam("intf5", Float::Box(int32Param)); 98eace7efcSopenharmony_ci wantParams1.SetParam("intf5", RemoteObjectWrap::Box(nullptr)); 99eace7efcSopenharmony_ci wantParams1.SetParam("intf6", Char::Box(int32Param)); 100eace7efcSopenharmony_ci wantParams1.SetParam("intf7", Double::Box(int32Param)); 101eace7efcSopenharmony_ci wantParams1.SetParam("intf8", Byte::Box(int32Param)); 102eace7efcSopenharmony_ci std::size_t size = 3; // 3 means arraysize. 103eace7efcSopenharmony_ci sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IBoolean); 104eace7efcSopenharmony_ci if (ao != nullptr) { 105eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 106eace7efcSopenharmony_ci ao->Set(i, Boolean::Box(boolParam)); 107eace7efcSopenharmony_ci } 108eace7efcSopenharmony_ci wantParams1.SetParam("intf8", ao); 109eace7efcSopenharmony_ci } 110eace7efcSopenharmony_ci WrapWantParams(env, wantParams1); // branch failed 111eace7efcSopenharmony_ci UnwrapWantParams(env, param, wantParams1); // branch failed 112eace7efcSopenharmony_ci BlackListFilter(Want::PARAM_RESV_WINDOW_MODE); // branch 113eace7efcSopenharmony_ci BlackListFilter(Want::PARAM_RESV_DISPLAY_ID); // branch 114eace7efcSopenharmony_ci BlackListFilter(stringParam); // branch 115eace7efcSopenharmony_ci Want want; 116eace7efcSopenharmony_ci WrapWant(env, want); // branch 117eace7efcSopenharmony_ci UnwrapWant(env, param, want); // branch 118eace7efcSopenharmony_ci int resultCode = 0; 119eace7efcSopenharmony_ci WrapAbilityResult(env, resultCode, want); // branch 120eace7efcSopenharmony_ci UnWrapAbilityResult(env, param, resultCode, want); // branch 121eace7efcSopenharmony_ci napi_value jsProValue = nullptr; 122eace7efcSopenharmony_ci HandleNapiObject(env, param, jsProValue, stringParam, wantParams1); // branch 123eace7efcSopenharmony_ci IsSpecialObject(env, param, stringParam, stringParam, static_cast<napi_valuetype>(int32Param)); // branch 124eace7efcSopenharmony_ci HandleFdObject(env, param, stringParam, wantParams1); // branch 125eace7efcSopenharmony_ci HandleRemoteObject(env, param, stringParam, wantParams1); // branch 126eace7efcSopenharmony_ci CreateJsWant(env, want); // branch 127eace7efcSopenharmony_ci CreateJsWantParams(env, wantParams1); // branch 128eace7efcSopenharmony_ci} 129eace7efcSopenharmony_ci 130eace7efcSopenharmony_civoid NapiCommonWantFuzztest2(bool boolParam, std::string &stringParam, int32_t int32Param) 131eace7efcSopenharmony_ci{ 132eace7efcSopenharmony_ci napi_env env = nullptr; 133eace7efcSopenharmony_ci AAFwk::WantParams wantParams1; 134eace7efcSopenharmony_ci napi_value object = nullptr; 135eace7efcSopenharmony_ci InnerWrapJsWantParamsWantParams(env, object, stringParam, wantParams1); // failed 136eace7efcSopenharmony_ci std::size_t size = 3; // 3 means arraysize. 137eace7efcSopenharmony_ci sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IBoolean); 138eace7efcSopenharmony_ci if (ao != nullptr) { 139eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 140eace7efcSopenharmony_ci ao->Set(i, Boolean::Box(boolParam)); 141eace7efcSopenharmony_ci } 142eace7efcSopenharmony_ci } 143eace7efcSopenharmony_ci WrapJsWantParamsArray(env, object, stringParam, ao); // branch 144eace7efcSopenharmony_ci} 145eace7efcSopenharmony_ci 146eace7efcSopenharmony_civoid NapiCommonWantFuzztest3(bool boolParam, std::string &stringParam, int32_t int32Param) 147eace7efcSopenharmony_ci{ 148eace7efcSopenharmony_ci std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr; 149eace7efcSopenharmony_ci AbilityRuntime::JsRuntime::Options options; 150eace7efcSopenharmony_ci auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv); 151eace7efcSopenharmony_ci napi_env env = reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()); 152eace7efcSopenharmony_ci ElementName elementName1; 153eace7efcSopenharmony_ci elementName1.SetDeviceID(stringParam); 154eace7efcSopenharmony_ci elementName1.SetBundleName(stringParam); 155eace7efcSopenharmony_ci elementName1.SetAbilityName(stringParam); 156eace7efcSopenharmony_ci elementName1.SetModuleName(stringParam); 157eace7efcSopenharmony_ci napi_value jsObject = WrapElementName(env, elementName1); // branch 158eace7efcSopenharmony_ci 159eace7efcSopenharmony_ci napi_value param = nullptr; 160eace7efcSopenharmony_ci ElementName elementName2; 161eace7efcSopenharmony_ci UnwrapElementName(env, param, elementName2); // branch null param 162eace7efcSopenharmony_ci ElementName elementName3; 163eace7efcSopenharmony_ci UnwrapElementName(env, jsObject, elementName3); // branch not null param 164eace7efcSopenharmony_ci 165eace7efcSopenharmony_ci AAFwk::WantParams wantParams1; 166eace7efcSopenharmony_ci WrapWantParams(env, wantParams1); 167eace7efcSopenharmony_ci wantParams1.SetParam("intf1", String::Box(stringParam)); 168eace7efcSopenharmony_ci wantParams1.SetParam("intf2", Long::Box(int32Param)); 169eace7efcSopenharmony_ci wantParams1.SetParam("intf3", Boolean::Box(boolParam)); 170eace7efcSopenharmony_ci wantParams1.SetParam("intf4", Integer::Box(int32Param)); 171eace7efcSopenharmony_ci wantParams1.SetParam("intf5", Float::Box(int32Param)); 172eace7efcSopenharmony_ci wantParams1.SetParam("intf5", RemoteObjectWrap::Box(nullptr)); 173eace7efcSopenharmony_ci wantParams1.SetParam("intf6", Char::Box(int32Param)); 174eace7efcSopenharmony_ci wantParams1.SetParam("intf7", Double::Box(int32Param)); 175eace7efcSopenharmony_ci wantParams1.SetParam("intf8", Byte::Box(int32Param)); 176eace7efcSopenharmony_ci std::size_t size = 3; // 3 means arraysize. 177eace7efcSopenharmony_ci sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IBoolean); 178eace7efcSopenharmony_ci if (ao != nullptr) { 179eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 180eace7efcSopenharmony_ci ao->Set(i, Boolean::Box(boolParam)); 181eace7efcSopenharmony_ci } 182eace7efcSopenharmony_ci wantParams1.SetParam("intf8", ao); 183eace7efcSopenharmony_ci } 184eace7efcSopenharmony_ci WrapWantParams(env, wantParams1); // branch null param 185eace7efcSopenharmony_ci UnwrapWantParams(env, param, wantParams1); // branch null param 186eace7efcSopenharmony_ci UnwrapWantParams(env, jsObject, wantParams1); // branch not null param 187eace7efcSopenharmony_ci} 188eace7efcSopenharmony_ci 189eace7efcSopenharmony_civoid NapiCommonWantFuzztest4(bool boolParam, std::string &stringParam, int32_t int32Param) 190eace7efcSopenharmony_ci{ 191eace7efcSopenharmony_ci napi_value param = nullptr; 192eace7efcSopenharmony_ci std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr; 193eace7efcSopenharmony_ci AbilityRuntime::JsRuntime::Options options; 194eace7efcSopenharmony_ci auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv); 195eace7efcSopenharmony_ci napi_env env = reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()); 196eace7efcSopenharmony_ci Want want; 197eace7efcSopenharmony_ci want.SetElementName(stringParam, stringParam, stringParam, stringParam); 198eace7efcSopenharmony_ci WrapWant(env, want); // wrap 199eace7efcSopenharmony_ci 200eace7efcSopenharmony_ci UnwrapWant(env, param, want); // branch null param 201eace7efcSopenharmony_ci ElementName elementName1; 202eace7efcSopenharmony_ci elementName1.SetDeviceID(stringParam); 203eace7efcSopenharmony_ci elementName1.SetBundleName(stringParam); 204eace7efcSopenharmony_ci elementName1.SetAbilityName(stringParam); 205eace7efcSopenharmony_ci elementName1.SetModuleName(stringParam); 206eace7efcSopenharmony_ci napi_value jsObject = WrapElementName(env, elementName1); // branch 207eace7efcSopenharmony_ci UnwrapWant(env, jsObject, want); // branch not null param 208eace7efcSopenharmony_ci 209eace7efcSopenharmony_ci int resultCode = 0; 210eace7efcSopenharmony_ci napi_value jsonObject1 = WrapAbilityResult(env, resultCode, want); // env not null 211eace7efcSopenharmony_ci UnWrapAbilityResult(env, param, resultCode, want); // null param 212eace7efcSopenharmony_ci UnWrapAbilityResult(env, jsonObject1, resultCode, want); // null param 213eace7efcSopenharmony_ci 214eace7efcSopenharmony_ci napi_value jsProValue = nullptr; 215eace7efcSopenharmony_ci AAFwk::WantParams wantParams1; 216eace7efcSopenharmony_ci HandleNapiObject(env, param, jsProValue, stringParam, wantParams1); // param null 217eace7efcSopenharmony_ci HandleNapiObject(env, jsObject, jsProValue, stringParam, wantParams1); // param not null jsProValue null. 218eace7efcSopenharmony_ci 219eace7efcSopenharmony_ci IsSpecialObject(env, param, stringParam, stringParam, static_cast<napi_valuetype>(int32Param)); // param null 220eace7efcSopenharmony_ci IsSpecialObject(env, jsObject, stringParam, stringParam, static_cast<napi_valuetype>(int32Param)); // param not null 221eace7efcSopenharmony_ci 222eace7efcSopenharmony_ci HandleFdObject(env, param, stringParam, wantParams1); // branch null param 223eace7efcSopenharmony_ci HandleRemoteObject(env, param, stringParam, wantParams1); // branch null param 224eace7efcSopenharmony_ci CreateJsWant(env, want); // branch 225eace7efcSopenharmony_ci CreateJsWantParams(env, wantParams1); // branch 226eace7efcSopenharmony_ci napi_value object = nullptr; 227eace7efcSopenharmony_ci InnerWrapJsWantParamsWantParams(env, object, stringParam, wantParams1); // branch null object 228eace7efcSopenharmony_ci napi_value jsObject2 = nullptr; 229eace7efcSopenharmony_ci napi_create_object(env, &jsObject2); 230eace7efcSopenharmony_ci InnerWrapJsWantParamsWantParams(env, jsObject2, stringParam, wantParams1); // branch object, key not exist. 231eace7efcSopenharmony_ci AAFwk::WantParams wantParams2; 232eace7efcSopenharmony_ci wantParams2.SetParam("intf1", String::Box(stringParam)); 233eace7efcSopenharmony_ci InnerWrapJsWantParamsWantParams(env, jsObject2, "intf1", wantParams2); // branch object, key exist. 234eace7efcSopenharmony_ci} 235eace7efcSopenharmony_ci 236eace7efcSopenharmony_civoid NapiCommonWantFuzztest5(bool boolParam, std::string &stringParam, int32_t int32Param) 237eace7efcSopenharmony_ci{ 238eace7efcSopenharmony_ci napi_value param = nullptr; 239eace7efcSopenharmony_ci std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr; 240eace7efcSopenharmony_ci AbilityRuntime::JsRuntime::Options options; 241eace7efcSopenharmony_ci auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv); 242eace7efcSopenharmony_ci napi_env env = reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()); 243eace7efcSopenharmony_ci napi_value nullObject = nullptr; 244eace7efcSopenharmony_ci std::size_t size = 3; // 3 means arraysize. 245eace7efcSopenharmony_ci sptr<IArray> ao1 = new (std::nothrow) Array(size, g_IID_IBoolean); 246eace7efcSopenharmony_ci if (ao1 != nullptr) { 247eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 248eace7efcSopenharmony_ci ao1->Set(i, Boolean::Box(boolParam)); 249eace7efcSopenharmony_ci } 250eace7efcSopenharmony_ci } 251eace7efcSopenharmony_ci WrapJsWantParamsArray(env, nullObject, stringParam, ao1); // null object. 252eace7efcSopenharmony_ci napi_value jsObject1 = nullptr; 253eace7efcSopenharmony_ci napi_create_object(env, &jsObject1); 254eace7efcSopenharmony_ci WrapJsWantParamsArray(env, jsObject1, stringParam, ao1); // not null object. 255eace7efcSopenharmony_ci 256eace7efcSopenharmony_ci sptr<IArray> ao2 = new (std::nothrow) Array(size, g_IID_IChar); 257eace7efcSopenharmony_ci if (ao2 != nullptr) { 258eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 259eace7efcSopenharmony_ci ao2->Set(i, Char::Box(int32Param)); 260eace7efcSopenharmony_ci } 261eace7efcSopenharmony_ci } 262eace7efcSopenharmony_ci WrapJsWantParamsArray(env, nullObject, stringParam, ao2); // null object. 263eace7efcSopenharmony_ci napi_value jsObject2 = nullptr; 264eace7efcSopenharmony_ci napi_create_object(env, &jsObject2); 265eace7efcSopenharmony_ci WrapJsWantParamsArray(env, jsObject2, stringParam, ao2); // not null object. 266eace7efcSopenharmony_ci 267eace7efcSopenharmony_ci sptr<IArray> ao3 = new (std::nothrow) Array(size, g_IID_IByte); 268eace7efcSopenharmony_ci if (ao3 != nullptr) { 269eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 270eace7efcSopenharmony_ci ao3->Set(i, Byte::Box(int32Param)); 271eace7efcSopenharmony_ci } 272eace7efcSopenharmony_ci } 273eace7efcSopenharmony_ci WrapJsWantParamsArray(env, nullObject, stringParam, ao3); // null object. 274eace7efcSopenharmony_ci napi_value jsObject3 = nullptr; 275eace7efcSopenharmony_ci napi_create_object(env, &jsObject3); 276eace7efcSopenharmony_ci WrapJsWantParamsArray(env, jsObject3, stringParam, ao3); // not null object. 277eace7efcSopenharmony_ci 278eace7efcSopenharmony_ci sptr<IArray> ao4 = new (std::nothrow) Array(size, g_IID_IShort); 279eace7efcSopenharmony_ci if (ao4 != nullptr) { 280eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 281eace7efcSopenharmony_ci ao4->Set(i, Short::Box(int32Param)); 282eace7efcSopenharmony_ci } 283eace7efcSopenharmony_ci } 284eace7efcSopenharmony_ci WrapJsWantParamsArray(env, nullObject, stringParam, ao4); // null object. 285eace7efcSopenharmony_ci napi_value jsObject4 = nullptr; 286eace7efcSopenharmony_ci napi_create_object(env, &jsObject4); 287eace7efcSopenharmony_ci WrapJsWantParamsArray(env, jsObject4, stringParam, ao4); // not null object. 288eace7efcSopenharmony_ci} 289eace7efcSopenharmony_ci 290eace7efcSopenharmony_civoid NapiCommonWantFuzztest6(bool boolParam, std::string &stringParam, int32_t int32Param) 291eace7efcSopenharmony_ci{ 292eace7efcSopenharmony_ci napi_value param = nullptr; 293eace7efcSopenharmony_ci std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr; 294eace7efcSopenharmony_ci AbilityRuntime::JsRuntime::Options options; 295eace7efcSopenharmony_ci auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv); 296eace7efcSopenharmony_ci napi_env env = reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()); 297eace7efcSopenharmony_ci napi_value nullObject = nullptr; 298eace7efcSopenharmony_ci std::size_t size = 3; // 3 means arraysize. 299eace7efcSopenharmony_ci sptr<IArray> ao1 = new (std::nothrow) Array(size, g_IID_ILong); 300eace7efcSopenharmony_ci if (ao1 != nullptr) { 301eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 302eace7efcSopenharmony_ci ao1->Set(i, Long::Box(int32Param)); 303eace7efcSopenharmony_ci } 304eace7efcSopenharmony_ci } 305eace7efcSopenharmony_ci WrapJsWantParamsArray(env, nullObject, stringParam, ao1); // null object. 306eace7efcSopenharmony_ci napi_value jsObject1 = nullptr; 307eace7efcSopenharmony_ci napi_create_object(env, &jsObject1); 308eace7efcSopenharmony_ci WrapJsWantParamsArray(env, jsObject1, stringParam, ao1); // not null object. 309eace7efcSopenharmony_ci 310eace7efcSopenharmony_ci sptr<IArray> ao2 = new (std::nothrow) Array(size, g_IID_IFloat); 311eace7efcSopenharmony_ci if (ao2 != nullptr) { 312eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 313eace7efcSopenharmony_ci ao2->Set(i, Float::Box(int32Param)); 314eace7efcSopenharmony_ci } 315eace7efcSopenharmony_ci } 316eace7efcSopenharmony_ci WrapJsWantParamsArray(env, nullObject, stringParam, ao2); // null object. 317eace7efcSopenharmony_ci napi_value jsObject2 = nullptr; 318eace7efcSopenharmony_ci napi_create_object(env, &jsObject2); 319eace7efcSopenharmony_ci WrapJsWantParamsArray(env, jsObject2, stringParam, ao2); // not null object. 320eace7efcSopenharmony_ci 321eace7efcSopenharmony_ci sptr<IArray> ao3 = new (std::nothrow) Array(size, g_IID_IDouble); 322eace7efcSopenharmony_ci if (ao3 != nullptr) { 323eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 324eace7efcSopenharmony_ci ao3->Set(i, Double::Box(int32Param)); 325eace7efcSopenharmony_ci } 326eace7efcSopenharmony_ci } 327eace7efcSopenharmony_ci WrapJsWantParamsArray(env, nullObject, stringParam, ao3); // null object. 328eace7efcSopenharmony_ci napi_value jsObject3 = nullptr; 329eace7efcSopenharmony_ci napi_create_object(env, &jsObject3); 330eace7efcSopenharmony_ci WrapJsWantParamsArray(env, jsObject3, stringParam, ao3); // not null object. 331eace7efcSopenharmony_ci 332eace7efcSopenharmony_ci sptr<IArray> ao4 = new (std::nothrow) Array(size, g_IID_IString); 333eace7efcSopenharmony_ci if (ao4 != nullptr) { 334eace7efcSopenharmony_ci for (std::size_t i = 0; i < size; i++) { 335eace7efcSopenharmony_ci ao4->Set(i, String::Box(stringParam)); 336eace7efcSopenharmony_ci } 337eace7efcSopenharmony_ci } 338eace7efcSopenharmony_ci WrapJsWantParamsArray(env, nullObject, stringParam, ao4); // null object. 339eace7efcSopenharmony_ci napi_value jsObject4 = nullptr; 340eace7efcSopenharmony_ci napi_create_object(env, &jsObject4); 341eace7efcSopenharmony_ci WrapJsWantParamsArray(env, jsObject4, stringParam, ao4); // not null object. 342eace7efcSopenharmony_ci} 343eace7efcSopenharmony_ci 344eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 345eace7efcSopenharmony_ci{ 346eace7efcSopenharmony_ci bool boolParam = *data % ENABLE; 347eace7efcSopenharmony_ci std::string stringParam(data, size); 348eace7efcSopenharmony_ci int32_t int32Param = static_cast<int32_t>(GetU32Data(data)); 349eace7efcSopenharmony_ci NapiCommonWantFuzztest1(boolParam, stringParam, int32Param); 350eace7efcSopenharmony_ci NapiCommonWantFuzztest2(boolParam, stringParam, int32Param); 351eace7efcSopenharmony_ci NapiCommonWantFuzztest3(boolParam, stringParam, int32Param); 352eace7efcSopenharmony_ci NapiCommonWantFuzztest4(boolParam, stringParam, int32Param); 353eace7efcSopenharmony_ci NapiCommonWantFuzztest5(boolParam, stringParam, int32Param); 354eace7efcSopenharmony_ci NapiCommonWantFuzztest6(boolParam, stringParam, int32Param); 355eace7efcSopenharmony_ci return true; 356eace7efcSopenharmony_ci} 357eace7efcSopenharmony_ci} 358eace7efcSopenharmony_ci 359eace7efcSopenharmony_ci/* Fuzzer entry point */ 360eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 361eace7efcSopenharmony_ci{ 362eace7efcSopenharmony_ci /* Run your code on data */ 363eace7efcSopenharmony_ci if (data == nullptr) { 364eace7efcSopenharmony_ci return 0; 365eace7efcSopenharmony_ci } 366eace7efcSopenharmony_ci 367eace7efcSopenharmony_ci /* Validate the length of size */ 368eace7efcSopenharmony_ci if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { 369eace7efcSopenharmony_ci return 0; 370eace7efcSopenharmony_ci } 371eace7efcSopenharmony_ci 372eace7efcSopenharmony_ci char* ch = (char*)malloc(size + 1); 373eace7efcSopenharmony_ci if (ch == nullptr) { 374eace7efcSopenharmony_ci std::cout << "malloc failed." << std::endl; 375eace7efcSopenharmony_ci return 0; 376eace7efcSopenharmony_ci } 377eace7efcSopenharmony_ci 378eace7efcSopenharmony_ci (void)memset_s(ch, size + 1, 0x00, size + 1); 379eace7efcSopenharmony_ci if (memcpy_s(ch, size, data, size) != EOK) { 380eace7efcSopenharmony_ci std::cout << "copy failed." << std::endl; 381eace7efcSopenharmony_ci free(ch); 382eace7efcSopenharmony_ci ch = nullptr; 383eace7efcSopenharmony_ci return 0; 384eace7efcSopenharmony_ci } 385eace7efcSopenharmony_ci 386eace7efcSopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(ch, size); 387eace7efcSopenharmony_ci free(ch); 388eace7efcSopenharmony_ci ch = nullptr; 389eace7efcSopenharmony_ci return 0; 390eace7efcSopenharmony_ci} 391eace7efcSopenharmony_ci 392