!991 新增fuzz,提升覆盖率

Merge pull request !991 from 师皓杰/master
This commit is contained in:
openharmony_ci 2024-07-25 08:07:29 +00:00 committed by Gitee
commit 3abd0b8aa9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
12 changed files with 433 additions and 181 deletions

View File

@ -2947,7 +2947,7 @@ bool DistributedSchedService::CheckCallingUid()
void DistributedSchedService::GetContinueEventInfo(int32_t callingUid, std::vector<EventNotify> &events)
{
if (callingUid == DEFAULT_REQUEST_CODE) {
if (callingUid == DEFAULT_REQUEST_CODE && dschedContinuation_ != nullptr) {
events.emplace_back(dschedContinuation_->continueEvent_);
return;
}

View File

@ -27,6 +27,7 @@ ohos_fuzztest("AbilityConnectionWrapperStubFuzzTest") {
"${dms_path}/services/dtbschedmgr/include/collaborate/",
"${dms_path}/services/dtbschedmgr/test/unittest",
"${dms_path}/test/fuzztest/abilityconnectionwrapperstub_fuzzer",
"${dms_path}/test/fuzztest/util",
]
fuzz_config_file =
@ -44,6 +45,7 @@ ohos_fuzztest("AbilityConnectionWrapperStubFuzzTest") {
sources = [
"${dms_path}/services/dtbschedmgr/test/unittest/mock_distributed_sched.cpp",
"${dms_path}/test/fuzztest/util/mock_fuzz_util.cpp",
"abilityconnectionwrapperstub_fuzzer.cpp",
]

View File

@ -17,6 +17,7 @@
#include "ability_connection_wrapper_stub.h"
#include "mock_distributed_sched.h"
#include "mock_fuzz_util.h"
#include "parcel_helper.h"
namespace OHOS {
@ -28,6 +29,7 @@ bool OnAbilityConnectDoneFuzzTest(const uint8_t* data, size_t size)
if ((data == nullptr) || (size < sizeof(int32_t))) {
return false;
}
FuzzUtil::MockPermission();
sptr<IRemoteObject> connection(new MockDistributedSched());
std::string localDeviceId(reinterpret_cast<const char*>(data), size);
std::shared_ptr<AbilityConnectionWrapperStub> abilityConnection_ =
@ -37,7 +39,6 @@ bool OnAbilityConnectDoneFuzzTest(const uint8_t* data, size_t size)
MessageParcel dataParcel;
MessageParcel reply;
MessageOption option;
dataParcel.WriteInt32(resultCode);
abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
std::u16string descriptor = IAbilityConnection::GetDescriptor();
@ -52,7 +53,9 @@ bool OnAbilityConnectDoneFuzzTest(const uint8_t* data, size_t size)
abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
dataParcel.WriteRemoteObject(connection);
dataParcel.WriteInt32(resultCode);
abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
abilityConnection_->OnAbilityConnectDone(element, connection, resultCode);
return true;
}
@ -61,6 +64,7 @@ bool OnAbilityDisconnectDoneFuzzTest(const uint8_t* data, size_t size)
if ((data == nullptr) || (size < sizeof(int32_t))) {
return false;
}
FuzzUtil::MockPermission();
sptr<IRemoteObject> connection(new MockDistributedSched());
std::shared_ptr<AbilityConnectionWrapperStub> abilityConnection_ =
std::make_shared<AbilityConnectionWrapperStub>(connection);
@ -69,7 +73,6 @@ bool OnAbilityDisconnectDoneFuzzTest(const uint8_t* data, size_t size)
MessageParcel dataParcel;
MessageParcel reply;
MessageOption option;
dataParcel.WriteInt32(resultCode);
std::string str1(reinterpret_cast<const char*>(data), size);
std::string str2(reinterpret_cast<const char*>(data), size);
std::string str3(reinterpret_cast<const char*>(data), size);
@ -78,7 +81,9 @@ bool OnAbilityDisconnectDoneFuzzTest(const uint8_t* data, size_t size)
AppExecFwk::ElementName element(str1, str2, str3);
dataParcel.WriteParcelable(&element);
dataParcel.WriteRemoteObject(connection);
dataParcel.WriteInt32(resultCode);
abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
abilityConnection_->OnAbilityDisconnectDone(element, resultCode);
return true;
}
}

View File

@ -26,6 +26,7 @@ ohos_fuzztest("BundleManagerCallBackStubFuzzTest") {
"${dms_path}/services/dtbschedmgr/include/",
"${dms_path}/services/dtbschedmgr/include/collaborate/",
"${dms_path}/test/fuzztest/bundlemanagercallbackstub_fuzzer",
"${dms_path}/test/fuzztest/util",
]
fuzz_config_file =
@ -41,7 +42,10 @@ ohos_fuzztest("BundleManagerCallBackStubFuzzTest") {
"-Dprivate=public",
]
sources = [ "bundlemanagercallbackstub_fuzzer.cpp" ]
sources = [
"${dms_path}/test/fuzztest/util/mock_fuzz_util.cpp",
"bundlemanagercallbackstub_fuzzer.cpp",
]
deps = [ "${dms_path}/services/dtbschedmgr:distributedschedsvr" ]

View File

@ -16,6 +16,7 @@
#include "bundlemanagercallbackstub_fuzzer.h"
#include "bundle/bundle_manager_callback_stub.h"
#include "mock_fuzz_util.h"
namespace OHOS {
namespace DistributedSchedule {
@ -25,6 +26,7 @@ bool OnQueryInstallationFinishedInnerFuzzTest(const uint8_t* data, size_t size)
if ((data == nullptr) || (size < sizeof(int32_t))) {
return false;
}
FuzzUtil::MockPermission();
std::shared_ptr<DmsBundleManagerCallbackStub> bundleManager_ =
std::make_shared<DmsBundleManagerCallbackStub>();
uint32_t code = 0;
@ -45,6 +47,7 @@ bool OnQueryInstallationFinishedInnerFuzzTest(const uint8_t* data, size_t size)
dataParcel.WriteUint32(versionCode);
dataParcel.WriteInt32(missionId);
bundleManager_->OnRemoteRequest(code, dataParcel, reply, option);
bundleManager_->OnQueryInstallationFinishedInner(dataParcel, reply);
return true;
}
}

View File

@ -21,11 +21,14 @@ ohos_fuzztest("DistributedSchedStubFuzzTest") {
visibility = [ ":*" ]
include_dirs = [
"${dms_path}/test/fuzztest/distributedschedstub_fuzzer",
"${dms_path}/services/dtbschedmgr/include/",
"${dms_path}/interfaces/innerkits/common/include/",
"${dms_path}/services/dtbschedmgr/include/collaborate/",
"${dms_path}/common/include/",
"${dms_path}/interfaces/innerkits/common/include/",
"${dms_path}/services/dtbschedmgr/include/",
"${dms_path}/services/dtbschedmgr/include/collaborate/",
"${dms_path}/services/dtbschedmgr/include/dfx/",
"${dms_path}/services/dtbschedmgr/test/unittest",
"${dms_path}/test/fuzztest/distributedschedstub_fuzzer",
"${dms_path}/test/fuzztest/util",
]
fuzz_config_file = "${dms_path}/test/fuzztest/distributedschedstub_fuzzer"
@ -41,8 +44,9 @@ ohos_fuzztest("DistributedSchedStubFuzzTest") {
]
sources = [
"${dms_path}/services/dtbschedmgr/test/unittest/mock_distributed_sched.cpp",
"${dms_path}/test/fuzztest/util/mock_fuzz_util.cpp",
"distributedschedstub_fuzzer.cpp",
"fuzz_util.cpp",
]
deps = [ "${dms_path}/services/dtbschedmgr:distributedschedsvr" ]
@ -51,6 +55,9 @@ ohos_fuzztest("DistributedSchedStubFuzzTest") {
if (dmsfwk_mission_manager) {
defines += [ "SUPPORT_DISTRIBUTED_MISSION_MANAGER" ]
}
if (dmsfwk_standard_form_share) {
defines += [ "SUPPORT_DISTRIBUTED_FORM_SHARE" ]
}
external_deps = [
"ability_base:base",
@ -86,6 +93,9 @@ ohos_fuzztest("DistributedSchedStubFuzzTest") {
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
]
if (dmsfwk_standard_form_share) {
external_deps += [ "form_fwk:form_manager" ]
}
}
group("fuzztest") {

View File

@ -19,6 +19,7 @@
#include <cstdint>
#include <iostream>
#include "bool_wrapper.h"
#include "distributed_want.h"
#include "securec.h"
@ -49,6 +50,7 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_001(const char* data, size_
unsigned int flags = static_cast<unsigned int>(GetU32Data(data));
want->SetFlags(flags);
want->RemoveFlags(flags);
want->AddFlags(flags);
std::string entity(data, size);
want->AddEntity(entity);
want->HasEntity(entity);
@ -57,6 +59,8 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_001(const char* data, size_
want->SetBundle(bundleName);
std::string deviceId(data, size);
want->SetDeviceId(deviceId);
want->SetElementName(bundleName, entity);
want->SetElementName(deviceId, bundleName, entity);
return true;
}
@ -76,6 +80,15 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_002(const char* data, size_
want->GetLowerCaseScheme(uri);
want->FormatUriAndType(uri, type);
want->FormatMimeType(type);
std::string str(data, size);
want->CheckParams(str);
want->ParseUri(str);
DistributedWant wantNew;
want->WantToUri(wantNew);
want->ToUri();
want->GenerateUriString(str);
want->ToUriStringInner(str);
return true;
}
@ -131,6 +144,7 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_004(const char* data, size_
zchar charValue = U'\0';
want->SetParam(key, charValue);
want->GetCharParam(key, charValue);
want->GetParams();
return true;
}
@ -153,6 +167,9 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_005(const char* data, size_
float floatValue = 0.0;
want->SetParam(key, floatValue);
want->GetFloatParam(key, floatValue);
bool boolValue = true;
want->SetParam(key, boolValue);
want->GetBoolParam(key, boolValue);
return true;
}
@ -163,6 +180,9 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_006(const char* data, size_
std::vector<float> floatVector;
want->SetParam(key, floatVector);
want->GetFloatArrayParam(key);
long longValue = 0;
want->SetParam(key, longValue);
want->GetShortParam(key, longValue);
std::vector<long> longVector;
want->SetParam(key, longVector);
want->GetLongArrayParam(key);
@ -172,8 +192,22 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_006(const char* data, size_
std::vector<short> shortVector;
want->SetParam(key, shortVector);
want->GetShortArrayParam(key);
std::string stringValue(data, size);
want->SetParam(key, stringValue);
want->GetStringParam(key);
std::vector<std::string> stringVector;
want->SetParam(key, stringVector);
want->GetStringArrayParam(key);
want->RemoveParam(key);
bool boolValue = true;
DistributedWantParams dWantParams;
dWantParams.SetParam(key, Boolean::Box(boolValue));
want->SetParams(dWantParams);
want->ReplaceParams(dWantParams);
DistributedWant dWant;
want->ReplaceParams(dWant);
want->ClearWant(&dWant);
return true;
}
}

View File

@ -26,6 +26,7 @@ ohos_fuzztest("DmsFreeInstallCallBackStubFuzzTest") {
"${dms_path}/services/dtbschedmgr/include/",
"${dms_path}/services/dtbschedmgr/include/collaborate/",
"${dms_path}/test/fuzztest/dmsfreeinstallcallbackstub_fuzzer",
"${dms_path}/test/fuzztest/util",
]
fuzz_config_file =
@ -41,7 +42,10 @@ ohos_fuzztest("DmsFreeInstallCallBackStubFuzzTest") {
"-Dprivate=public",
]
sources = [ "dmsfreeinstallcallbackstub_fuzzer.cpp" ]
sources = [
"${dms_path}/test/fuzztest/util/mock_fuzz_util.cpp",
"dmsfreeinstallcallbackstub_fuzzer.cpp",
]
deps = [ "${dms_path}/services/dtbschedmgr:distributedschedsvr" ]

View File

@ -17,6 +17,7 @@
#include "dms_free_install_callback.h"
#include "distributed_sched_interface.h"
#include "mock_fuzz_util.h"
#include "parcel_helper.h"
namespace OHOS {
@ -29,6 +30,7 @@ bool OnInstallFinishedInnerFuzzTest(const uint8_t* data, size_t size)
if ((data == nullptr) || (size < sizeof(int32_t))) {
return false;
}
FuzzUtil::MockPermission();
int64_t taskId = 0;
IDistributedSched::FreeInstallInfo info;
std::shared_ptr<DmsFreeInstallCallback> freeInstall_ =

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "fuzz_util.h"
#include "mock_fuzz_util.h"
#include "accesstoken_kit.h"
#include "nativetoken_kit.h"
@ -28,7 +28,25 @@ constexpr const char* FOUNDATION_PROCESS_NAME = "foundation";
void FuzzUtil::MockPermission()
{
static const char *perms[] = {
"ohos.permission.DISTRIBUTED_DATASYNC"
"ohos.permission.ABILITY_BACKGROUND_COMMUNICATION",
"ohos.permission.ACCESS_SERVICE_DM",
"ohos.permission.ACCESS_SERVICE_DP",
"ohos.permission.DISTRIBUTED_DATASYNC",
"ohos.permission.INPUT_MONITORING",
"ohos.permission.GET_BUNDLE_INFO_PRIVILEGED",
"ohos.permission.GET_WIFI_INFO",
"ohos.permission.MANAGE_MISSIONS",
"ohos.permission.MANAGE_WIFI_CONNECTION",
"ohos.permission.MONITOR_DEVICE_NETWORK_STATE",
"ohos.permission.PUBLISH_SYSTEM_COMMON_EVENT",
"ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT",
"ohos.permission.RUNNING_STATE_OBSERVER",
"ohos.permission.START_INVISIBLE_ABILITY",
"ohos.permission.INPUT_MONITORING",
"ohos.permission.MANAGE_MISSIONS",
"ohos.permission.MANAGE_WIFI_CONNECTION",
"ohos.permission.MONITOR_DEVICE_NETWORK_STATE",
"ohos.permission.START_INVISIBLE_ABILITY"
};
MockProcessAndPermission(FOUNDATION_PROCESS_NAME, perms, 1);
}
@ -49,6 +67,7 @@ void FuzzUtil::MockProcessAndPermission(const char* processName, const char *per
tokenId = GetAccessTokenId(&infoInstance);
SetSelfTokenID(tokenId);
OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
setuid(0);
}
}
}

View File

@ -28,4 +28,4 @@ public:
};
}
} // namespace OHOS
#endif // SAMGR_SERVICES_SAMGR_MOCK_PERMISSION_H
#endif