提交 bf901d20 编写于 作者: C chengjinsong2

添加service_watcher moduletest

Signed-off-by: Nchengjinsong2 <chengjinsong2@huawei.com>
上级 dada4574
......@@ -320,14 +320,14 @@ int32_t GetIntParameter(const char *key, int32_t def)
{
char value[MAX_INT_LEN] = {0};
int ret = GetParameter(key, "0", value, sizeof(value));
if (ret != 0) {
if (ret < 0) {
return def;
}
long long int result = 0;
if (StringToLL(value, &result) != 0) {
return def;
}
if (result <= INT32_MIN && result >= INT32_MAX) {
if (result <= INT32_MIN || result >= INT32_MAX) {
return def;
}
return (int32_t)result;
......@@ -337,7 +337,7 @@ uint32_t GetUintParameter(const char *key, uint32_t def)
{
char value[MAX_INT_LEN] = {0};
int ret = GetParameter(key, "0", value, sizeof(value));
if (ret != 0) {
if (ret < 0) {
return def;
}
unsigned long long int result = 0;
......
......@@ -49,11 +49,18 @@ ohos_moduletest("InitModuleTest") {
sources = [
"hookmgr_moduletest.cpp",
"modulemgr_moduletest.cpp",
"service_watcher_moduleTest.cpp",
]
include_dirs = [ "//base/startup/init/interfaces/innerkits/include" ]
include_dirs = [
"//base/startup/init/interfaces/innerkits/include",
"//base/startup/init/interfaces/innerkits/include/syspara",
"//base/startup/init/interfaces/innerkits/syspara",
"//base/startup/init/services/include/param",
]
deps = [
"//base/startup/init/interfaces/innerkits:libbeget_proxy",
"//base/startup/init/interfaces/innerkits:libbegetutil",
"//third_party/googletest:gtest_main",
]
......
/*
* 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 <thread>
#include <string>
#include <gtest/gtest.h>
#include "service_control.h"
#include "service_watcher.h"
#include "test_utils.h"
using namespace testing::ext;
namespace initModuleTest {
class serviceWatcherModuleTest : public testing::Test {
public:
static void SetUpTestCase(void) {};
static void TearDownTestCase(void) {};
void SetUp(void) {};
void TearDown(void) {};
};
static void ServiceStatusChange(const char *key, ServiceStatus status)
{
std::cout<<"service Name is: "<<key<<", ServiceStatus is: "<<status<<std::endl;
}
HWTEST_F(serviceWatcherModuleTest,serviceWatcher_test_001, TestSize.Level0)
{
GTEST_LOG_(INFO) << "serviceWatcher_test_001 start";
string serviceName = "test.Service";
int ret = ServiceWatchForStatus(serviceName.c_str(), ServiceStatusChange);
EXPECT_EQ(ret, 0); // No matter if service exist or not, ServiceWatchForStatus always success.
auto status = GetServiceStatus(serviceName);
EXPECT_TRUE(status.empty());
GTEST_LOG_(INFO) << "serviceWatcher_test_001 end";
}
HWTEST_F(serviceWatcherModuleTest,serviceWatcher_test_002, TestSize.Level0)
{
GTEST_LOG_(INFO) << "serviceWatcher_test_002 start";
string serviceName = "media_service";
auto status = GetServiceStatus(serviceName);
if (status == "running") {
int ret = ServiceControl(serviceName.c_str(), STOP);
ASSERT_EQ(ret , 0);
} else if (status != "created" && status != "stopped") {
std::cout << serviceName << " in invalid status " << status << std::endl;
ASSERT_TRUE(0);
}
int ret = ServiceWatchForStatus(serviceName.c_str(), ServiceStatusChange);
EXPECT_EQ(ret, 0);
status = GetServiceStatus(serviceName);
EXPECT_TRUE(status == "stopped");
GTEST_LOG_(INFO) << "serviceWatcher_test_002 end";
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册