新增UT测试代码

Signed-off-by: niuguoliang <niuguoliang4@huawei.com>
This commit is contained in:
niuguoliang 2024-03-28 11:44:36 +08:00
parent 56c8486abd
commit ecadc505a3
6 changed files with 753 additions and 0 deletions

View File

@ -22,8 +22,20 @@ config("module_private_config") {
ohos_unittest("sp_daemon_ut") {
module_out_path = module_output_path
sources = [
"../ByTrace.cpp",
"../Capture.cpp",
"../FPS.cpp",
"../GpuCounter.cpp",
"../sp_log.cpp",
"../sp_task.cpp",
"../sp_utils.cpp",
"../startup_delay.cpp",
"unittest/fps_test.cpp",
"unittest/gpu_counter_test.cpp",
"unittest/smartperf_main_test.cpp",
"unittest/sp_daemon_test.cpp",
"unittest/sp_task_test.cpp",
"unittest/sp_utils_test.cpp",
]
include_dirs = [
"${OHOS_PROFILER_DIR}/host/smartperf/client/client_command/include",
@ -42,6 +54,9 @@ ohos_unittest("sp_daemon_ut") {
"window_manager:libdm",
"window_manager:libwm",
]
configs = [ "//build/config/compiler:exceptions" ]
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
part_name = "${OHOS_PROFILER_PART_NAME}"
}

View File

@ -0,0 +1,77 @@
/*
* Copyright (C) 2021 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 <exception>
#include <iostream>
#include <string>
#include <thread>
#include <gtest/gtest.h>
#include <unistd.h>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <functional>
#include "FPS.h"
using namespace testing::ext;
using namespace std;
namespace OHOS {
namespace SmartPerf {
class SPdaemonTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void SetUp() {}
void TearDown() {}
};
/**
* @tc.name: FPS::SetFpsCurrentFpsTime
* @tc.desc: Test SetFpsCurrentFpsTime
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, SetFpsCurrentFpsTimeTestCase001, TestSize.Level1)
{
bool ret = false;
FpsInfo fpsInfoResult;
FPS::GetInstance().SetFpsCurrentFpsTime(fpsInfoResult);
FpsCurrentFpsTime fcf = FPS::GetInstance().GetFpsCurrentFpsTime();
if (fcf.currentFpsTime == 0) {
ret = true;
}
EXPECT_EQ(ret, true);
}
/**
* @tc.name: FPS::GetFpsCurrentFpsTime
* @tc.desc: Test GetFpsCurrentFpsTime
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, GetFpsCurrentFpsTimeTestCase001, TestSize.Level1)
{
bool ret = false;
FpsCurrentFpsTime fcf = FPS::GetInstance().GetFpsCurrentFpsTime();
if (fcf.fps == 0 && fcf.currentFpsTime == 0) {
ret = true;
}
EXPECT_EQ(ret, true);
}
}
}

View File

@ -0,0 +1,169 @@
/*
* Copyright (C) 2021 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 <exception>
#include <iostream>
#include <string>
#include <thread>
#include <gtest/gtest.h>
#include <unistd.h>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <functional>
#include "GpuCounter.h"
using namespace testing::ext;
using namespace std;
namespace OHOS {
namespace SmartPerf {
class SPdaemonTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void SetUp() {}
void TearDown() {}
};
/**
* @tc.name: GpuCounter::Init
* @tc.desc: Test Init
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, Init001, TestSize.Level1)
{
std::string recvBuf;
std::map<std::string, std::string> data;
int ret = GpuCounter::GetInstance().Init(recvBuf, data);
EXPECT_EQ(ret, -4);
}
/*
// 002屏蔽GpuCounter.cpp 第35、78-83行进行测试后续测试添加依赖文件文件齐全所以屏蔽该测试
HWTEST_F(SPdaemonTest, Init002, TestSize.Level1)
{
std::string recvBuf;
std::map<std::string, std::string> data;
int ret = GpuCounter::GetInstance().Init(recvBuf, data); // 外部依赖文件不全,返回-1
EXPECT_EQ(ret, -1);
}
// 003屏蔽GpuCounter.cpp 第35、78-83行进行测试
HWTEST_F(SPdaemonTest, Init003, TestSize.Level1)
{
std::string recvBuf = "";
std::map<std::string, std::string> data;
int ret = GpuCounter::GetInstance().Init(recvBuf, data); // recvBuf为空返回-2
EXPECT_EQ(ret, -2);
}
// 004屏蔽GpuCounter.cpp 第35、78-83行进行测试
HWTEST_F(SPdaemonTest, Init004, TestSize.Level1)
{
std::string recvBuf = "ABCDE";
std::map<std::string, std::string> data;
int ret = GpuCounter::GetInstance().Init(recvBuf, data); // 无法访问sandBoxPath路径,返回-3
EXPECT_EQ(ret, -3);
}
// 005屏蔽GpuCounter.cpp 第35、78-83行进行测试
HWTEST_F(SPdaemonTest, Init005, TestSize.Level1)
{
std::string recvBuf = "com.ohos.smartperf1";
std::map<std::string, std::string> data;
int ret = GpuCounter::GetInstance().Init(recvBuf, data);
EXPECT_EQ(ret, -1); // 此处的-1为接口中调用Start所返回的-1
}
*/
/**
* @tc.name: GpuCounter::ItemData
* @tc.desc: Test ItemData
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, ItemData001, TestSize.Level1)
{
bool ret = false;
std::string recvBuf = "catch_gpu_counter";
std::map<std::string, std::string> data;
std::map<std::string, std::string> mapInfo;
GpuCounter::GetInstance().Init(recvBuf, data);
mapInfo = GpuCounter::GetInstance().ItemData();
if (!mapInfo.empty()) {
ret = true;
}
EXPECT_EQ(ret, true);
}
/**
* @tc.name: GpuCounter::GetCounterDuration
* @tc.desc: Test GetCounterDuration
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, GetCounterDuration001, TestSize.Level1)
{
bool ret = false;
long long lg = GpuCounter::GetInstance().GetCounterDuration();
if (lg >= 0) {
ret = true;
}
EXPECT_EQ(ret, true);
}
/**
* @tc.name: GpuCounter::Start
* @tc.desc: Test Start
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, Start001, TestSize.Level1)
{
int ret = GpuCounter::GetInstance().Start();
EXPECT_EQ(ret, -1);
}
/**
* @tc.name: GpuCounter::Rest
* @tc.desc: Test Rest
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, Rest001, TestSize.Level1)
{
GpuCounter::GetInstance().Rest();
}
/**
* @tc.name: GpuCounter::Check
* @tc.desc: Test Check
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, Check001, TestSize.Level1)
{
std::string recvBuf = "catch_gpu_counter";
std::map<std::string, std::string> data;
GpuCounter::GetInstance().Init(recvBuf, data);
GpuCounter::GetInstance().Check();
}
}
}

View File

@ -0,0 +1,186 @@
/*
* Copyright (C) 2021 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 <exception>
#include <iostream>
#include <string>
#include <thread>
#include <gtest/gtest.h>
#include <unistd.h>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <functional>
#include "sp_utils.h"
#include "common.h"
using namespace testing::ext;
using namespace std;
namespace OHOS {
namespace SmartPerf {
class SPdaemonTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void SetUp() {}
void TearDown() {}
};
std::string &GetOptions(std::vector<std::string> &argv)
{
std::string str = "";
std::string strFlag;
bool isFill = false;
for (std::size_t i = 0; i < argv.size(); i++) {
if (!isFill) {
strFlag = argv[i];
if (std::string::npos != strFlag.find("SP_daemon")) {
isFill = true;
}
} else {
str += argv[i];
if (i + 1 != argv.size()) {
str += " ";
}
}
}
return str;
}
bool CheckCMDParam(std::vector<std::string> &argv, std::string &errorInfo)
{
std::string str = GetOptions(argv);
std::set<std::string> keys;
if (str.empty()) {
return true;
}
if (std::string::npos != str.find("--help") || std::string::npos != str.find("--version")) {
std::vector<std::string> out;
OHOS::SmartPerf::SPUtils::StrSplit(str, "-", out);
if (1 != out.size()) {
errorInfo = "--help and --version cannot be used together with other options";
return false;
} else {
return true;
}
}
keys.insert("editor");
keys.insert("profilerfps");
keys.insert("start");
keys.insert("stop");
keys.insert("screen");
for (auto a : OHOS::SmartPerf::commandMap) {
keys.insert(a.first.substr(1)); // No prefix required '-'
}
auto itr = keys.find("f1");
if (keys.end() != itr) {
keys.erase(itr);
}
itr = keys.find("f2");
if (keys.end() != itr) {
keys.erase(itr);
}
itr = keys.find("fl");
if (keys.end() != itr) {
keys.erase(itr);
}
itr = keys.find("ftl");
if (keys.end() != itr) {
keys.erase(itr);
}
return OHOS::SmartPerf::SPUtils::VeriyParameter(keys, str, errorInfo);
}
/**
* @tc.name: GetOptionsTestCase
* @tc.desc: Test GetOptions
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, GetOptionsTestCase001, TestSize.Level1)
{
bool ret = false;
std::vector<std::string> argv;
argv.push_back("Test");
argv.push_back("GetOptions");
argv.push_back("SP_daemon");
argv.push_back("-start");
argv.push_back("-c");
std::string str = GetOptions(argv);
if (!str.empty()) {
ret = true;
}
EXPECT_EQ(ret, true);
}
/**
* @tc.name: CheckCMDParamTestCase
* @tc.desc: Test CheckCMDParam
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, CheckCMDParamTestCase001, TestSize.Level1)
{
std::string errorInfo = "";
std::vector<std::string> argv;
argv.push_back("SP_daemon");
argv.push_back("-start");
argv.push_back("-c");
argv.push_back("-g");
argv.push_back("-t");
argv.push_back("-p");
argv.push_back("-r");
argv.push_back("-net");
argv.push_back("-snapshot");
bool ret = CheckCMDParam(argv, errorInfo);
EXPECT_EQ(ret, true);
}
HWTEST_F(SPdaemonTest, CheckCMDParamTestCase002, TestSize.Level1)
{
std::string errorInfo = "";
std::vector<std::string> argv;
argv.push_back("SP_daemon");
argv.push_back("-start");
argv.push_back("-f1");
argv.push_back("-f2");
argv.push_back("-fl");
argv.push_back("-ftl");
bool ret = CheckCMDParam(argv, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, CheckCMDParamTestCase003, TestSize.Level1)
{
std::string errorInfo = "";
std::vector<std::string> argv;
argv.push_back("SP_daemon");
argv.push_back("");
bool ret = CheckCMDParam(argv, errorInfo);
EXPECT_EQ(ret, true);
}
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (C) 2021 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 <exception>
#include <iostream>
#include <string>
#include <thread>
#include <gtest/gtest.h>
#include <unistd.h>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <functional>
#include "sp_utils.h"
#include "sp_log.h"
#include "sp_task.h"
using namespace testing::ext;
using namespace std;
namespace OHOS {
namespace SmartPerf {
class SPdaemonTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void SetUp() {}
void TearDown() {}
};
/**
* @tc.name: CheckTcpParamTestCase
* @tc.desc: Test CheckTcpParam
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, CheckTcpParamTestCase001, TestSize.Level1)
{
std::string str = "-SESSIONID 12345678 -INTERVAL 1000 -PKG ohos.samples.ecg -c -g -t -p -f -r";
std::string errorInfo = "";
SPTask &spTask = SPTask::GetInstance();
bool flag = spTask.CheckTcpParam(str, errorInfo);
EXPECT_TRUE(flag);
}
HWTEST_F(SPdaemonTest, CheckTcpParamTestCase002, TestSize.Level1)
{
std::string str = "";
std::string errorInfo = "";
SPTask &spTask = SPTask::GetInstance();
bool flag = spTask.CheckTcpParam(str, errorInfo);
EXPECT_FALSE(flag);
}
HWTEST_F(SPdaemonTest, CheckTcpParamTestCase003, TestSize.Level1)
{
std::string str = "";
std::string errorInfo = "";
SPTask &spTask = SPTask::GetInstance();
bool flag = spTask.CheckTcpParam(str, errorInfo);
EXPECT_FALSE(flag);
}
/**
* @tc.name: SPTask::DetectionAndGrab
* @tc.desc: Test DetectionAndGrab
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, DetectionAndGrabTestCase001, TestSize.Level1)
{
bool ret = false;
std::map<std::string, std::string> templateMap;
std::string recvStr = "-SESSIONID 12345678 -INTERVAL 1000 -PKG ohos.samples.ecg -c -g -t -p -f -r";
SPTask::GetInstance().InitTask(recvStr);
templateMap = SPTask::GetInstance().DetectionAndGrab();
if (templateMap.empty()) {
ret = true;
}
EXPECT_EQ(ret, true);
}
}
}

View File

@ -0,0 +1,211 @@
/*
* Copyright (C) 2021 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 <exception>
#include <iostream>
#include <string>
#include <gtest/gtest.h>
#include <unistd.h>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <functional>
#include "sp_utils.h"
using namespace testing::ext;
using namespace std;
namespace OHOS {
namespace SmartPerf {
class SPdaemonTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void SetUp() {}
void TearDown() {}
};
/**
* @tc.name: SPUtils::IntegerValueVerification
* @tc.desc: Test IntegerValueVerification
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, IntegerValueVerificationTest001, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
std::set<std::string> keys;
keys.insert("N");
keys.insert("fl");
keys.insert("ftl");
mapInfo["N"] = "";
mapInfo["fl"] = "";
mapInfo["ftl"] = "";
bool ret = SPUtils::IntegerValueVerification(keys, mapInfo, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, IntegerValueVerificationTest002, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
std::set<std::string> keys;
keys.insert("N");
keys.insert("fl");
keys.insert("ftl");
mapInfo["N"] = "A";
mapInfo["fl"] = "B";
mapInfo["ftl"] = "C";
bool ret = SPUtils::IntegerValueVerification(keys, mapInfo, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, IntegerValueVerificationTest003, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
std::set<std::string> keys;
keys.insert("N");
keys.insert("fl");
keys.insert("ftl");
mapInfo["N"] = "1";
mapInfo["fl"] = "2";
mapInfo["ftl"] = "3";
bool ret = SPUtils::IntegerValueVerification(keys, mapInfo, errorInfo);
EXPECT_EQ(ret, true);
}
/**
* @tc.name: SPUtils::VerifyValueStr
* @tc.desc: Test VerifyValueStr
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, VerifyValueStrTest001, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
mapInfo["VIEW"] = "";
bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, VerifyValueStrTest002, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
mapInfo["VIEW"] = "TestVIEW";
mapInfo["PKG"] = "";
bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, VerifyValueStrTest003, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
mapInfo["VIEW"] = "TestVIEW";
mapInfo["PKG"] = "TestPKG";
mapInfo["OUT"] = "";
bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, VerifyValueStrTest004, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
mapInfo["VIEW"] = "TestVIEW";
mapInfo["PKG"] = "TestPKG";
mapInfo["OUT"] = "Test/sp_utils_test/";
bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, VerifyValueStrTest005, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
mapInfo["VIEW"] = "TestVIEW";
mapInfo["PKG"] = "TestPKG";
mapInfo["OUT"] = "/sp_utils_test";
bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, VerifyValueStrTest006, TestSize.Level1)
{
std::string errorInfo;
std::map<std::string, std::string> mapInfo;
mapInfo["VIEW"] = "TestVIEW";
mapInfo["PKG"] = "TestPKG";
bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo);
EXPECT_EQ(ret, true);
}
/**
* @tc.name: SPUtils::VeriyKey
* @tc.desc: Test VeriyKey
* @tc.type: FUNC
*/
HWTEST_F(SPdaemonTest, VeriyKey001, TestSize.Level1)
{
std::set<std::string> keys;
std::map<std::string, std::string> mapInfo;
std::string errorInfo;
keys.insert("apple");
keys.insert("banana");
keys.insert("cherry");
keys.insert("orange");
keys.insert("pineapple");
mapInfo["A"] = "";
mapInfo["B"] = "";
mapInfo["C"] = "";
bool ret = SPUtils::VeriyKey(keys, mapInfo, errorInfo);
EXPECT_EQ(ret, false);
}
HWTEST_F(SPdaemonTest, VeriyKey002, TestSize.Level1)
{
std::set<std::string> keys;
std::map<std::string, std::string> mapInfo;
std::string errorInfo;
keys.insert("apple");
keys.insert("banana");
keys.insert("cherry");
keys.insert("orange");
keys.insert("pineapple");
mapInfo["apple"] = "";
mapInfo["cherry"] = "";
mapInfo["pineapple"] = "";
bool ret = SPUtils::VeriyKey(keys, mapInfo, errorInfo);
EXPECT_EQ(ret, true);
}
}
}