fix: add fuzz test

Signed-off-by: liujiandong <liujiandong1@huawei.com>
This commit is contained in:
YOUR_NAME
2022-10-29 09:30:21 +08:00
parent 6680a0fb37
commit 35e6061f6c
9 changed files with 298 additions and 1 deletions
+2 -1
View File
@@ -76,7 +76,8 @@
}
],
"test": [
"//base/telephony/cellular_data/test:unittest"
"//base/telephony/cellular_data/test:unittest",
"//base/telephony/cellular_data/test/fuzztest:fuzztest"
]
}
}
+22
View File
@@ -0,0 +1,22 @@
# Copyright (C) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/config/features.gni")
import("//build/test.gni")
group("fuzztest") {
testonly = true
deps = []
deps += [ "getcellulardatastate_fuzzer:fuzztest" ]
}
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "adddatatoken_fuzzer.h"
#include <iostream>
#include "nativetoken_kit.h"
#include "token_setproc.h"
namespace OHOS {
AddDataTokenFuzzer::AddDataTokenFuzzer()
{
const char **perms = new const char *[3];
perms[0] = "ohos.permission.GET_NETWORK_INFO";
perms[1] = "ohos.permission.GET_TELEPHONY_STATE";
perms[2] = "ohos.permission.SET_TELEPHONY_STATE";
NativeTokenInfoParams testDataInfoParams = {
.dcapsNum = 0,
.permsNum = 3,
.aclsNum = 0,
.dcaps = nullptr,
.perms = perms,
.acls = nullptr,
.processName = "data_fuzzer",
.aplStr = "system_basic",
};
currentID_ = GetAccessTokenId(&testDataInfoParams);
std::cout << "AddDataTokenFuzzer currentID_ : " << currentID_ << std::endl;
SetSelfTokenID(currentID_);
Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
}
AddDataTokenFuzzer::~AddDataTokenFuzzer()
{
std::cout << "AddDataTokenFuzzer ~AddDataTokenFuzzer" << std::endl;
}
} // namespace OHOS
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ADD_DATA_TOKEN_FUZZER
#define ADD_DATA_TOKEN_FUZZER
#include "accesstoken_kit.h"
namespace OHOS {
class AddDataTokenFuzzer {
public:
AddDataTokenFuzzer();
~AddDataTokenFuzzer();
private:
Security::AccessToken::AccessTokenID currentID_ = 0;
};
} // namespace OHOS
#endif // ADD_DATA_TOKEN_FUZZER
@@ -0,0 +1,75 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#####################hydra-fuzz###################
import("//base/telephony/core_service/telephony.gni")
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("GetCellularDataStateFuzzTest") {
module_output_path = "cellular_data/GetCellularDataStateFuzzTest"
module_out_path = module_output_path
fuzz_config_file =
"//base/telephony/cellular_data/test/fuzztest/getcellulardatastate_fuzzer"
include_dirs =
[ "//base/telephony/cellular_data/test/fuzztest/common_fuzzer" ]
configs = [ "//base/telephony/core_service/utils:telephony_log_config" ]
external_deps = [
"access_token:libaccesstoken_sdk",
"access_token:libnativetoken",
"access_token:libtoken_setproc",
"c_utils:utils",
"cellular_data:tel_cellular_data_api",
"core_service:tel_core_service_api",
"eventhandler:libeventhandler",
"init:libbegetutil",
]
defines += [
"TELEPHONY_LOG_TAG = \"CellularDataFuzzTest\"",
"LOG_DOMAIN = 0xD000F00",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [
"//base/telephony/cellular_data/test/fuzztest/common_fuzzer/adddatatoken_fuzzer.cpp",
"getcellulardatastate_fuzzer.cpp",
]
if (is_standard_system) {
external_deps += [ "hiviewdfx_hilog_native:libhilog" ]
} else {
external_deps += [ "hilog:libhilog" ]
}
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":GetCellularDataStateFuzzTest",
]
}
###############################################################################
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* corpus is necessary
*/
FUZZ
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "getcellulardatastate_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "adddatatoken_fuzzer.h"
#include "cellular_data_client.h"
#include "system_ability_definition.h"
using namespace OHOS::Telephony;
namespace OHOS {
void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
{
if (data == nullptr || size <= 0) {
return;
}
int32_t slotId = static_cast<int32_t>(size);
CellularDataClient::GetInstance().GetCellularDataState();
CellularDataClient::GetInstance().IsCellularDataEnabled();
CellularDataClient::GetInstance().IsCellularDataRoamingEnabled(slotId);
CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
CellularDataClient::GetInstance().EnableCellularDataRoaming(slotId, static_cast<int32_t>(size % 2));
CellularDataClient::GetInstance().SetDefaultCellularDataSlotId(slotId);
CellularDataClient::GetInstance().HasInternetCapability(slotId, size);
CellularDataClient::GetInstance().ClearCellularDataConnections(slotId);
CellularDataClient::GetInstance().GetCellularDataFlowType();
}
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
OHOS::AddDataTokenFuzzer token;
/* Run your code on data */
OHOS::DoSomethingInterestingWithMyAPI(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GETCELLULARDATASTATE_FUZZER_H
#define GETCELLULARDATASTATE_FUZZER_H
#define FUZZ_PROJECT_NAME "getcellulardatastate_fuzzer"
#endif // GETCELLULARDATASTATE_FUZZER_H
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2022 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>