add storage daemon netlink test code

Signed-off-by: linjun9528 <linjun27@huawei.com>
This commit is contained in:
linjun9528 2022-03-24 14:39:41 +08:00
parent 06edacbf29
commit 07b1863aea
9 changed files with 639 additions and 14 deletions

View File

@ -62,6 +62,7 @@
"test": [
"//foundation/filemanagement/storage_service/services/storage_daemon/crypto/test:crypto_test",
"//foundation/filemanagement/storage_service/services/storage_daemon/ipc/test:storage_daemon_ipc_test",
"//foundation/filemanagement/storage_service/services/storage_daemon/netlink/test:storage_daemon_netlink_test",
"//foundation/filemanagement/storage_service/services/storage_daemon/user/test:storage_daemon_user_test",
"//foundation/filemanagement/storage_service/services/storage_daemon/utils/test:storage_daemon_utils_test",
"//foundation/filemanagement/storage_service/services/storage_daemon/volume/test:storage_daemon_volume_test",

View File

@ -110,7 +110,7 @@ int32_t NetlinkListener::ReadMsg(int32_t fd_count, struct pollfd ufds[2])
}
if (msg == 0) {
LOGI("Stop listener");
break;
return E_ERR;
}
} else if (ufds[i].fd == socketFd_) {
if ((static_cast<uint32_t>(ufds[i].revents) & POLLIN)) {

View File

@ -1,4 +1,4 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# 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
@ -13,20 +13,23 @@
import("//build/test.gni")
ROOT_DIR = "//foundation/filemanagement/storage_service/services/storage_daemon"
ROOT_DIR = "//foundation/filemanagement/storage_service/services"
ohos_unittest("netlink_manager_test") {
ohos_unittest("netlink_data_test") {
module_out_path = "filemanagement/storage_service/storage_daemon"
defines = [ "STORAGE_LOG_TAG = \"StorageDaemon\"" ]
include_dirs = [
"$ROOT_DIR/include",
"$ROOT_DIR/common/include",
"$ROOT_DIR/storage_daemon/include",
"//foundation/filemanagement/storage_service/utils/include",
"//foundation/filemanagement/storage_service/storage_manager/include",
]
sources = [
"$ROOT_DIR/netlink/src/netlink_manager.cpp",
"$ROOT_DIR/netlink/test/netlink_manager_test.cpp",
"$ROOT_DIR/storage_daemon/netlink/src/netlink_data.cpp",
"$ROOT_DIR/storage_daemon/netlink/test/netlink_data_test.cpp",
]
deps = [
@ -40,7 +43,136 @@ ohos_unittest("netlink_manager_test") {
]
}
ohos_unittest("netlink_handler_test") {
module_out_path = "filemanagement/storage_service/storage_daemon"
defines = [ "STORAGE_LOG_TAG = \"StorageDaemon\"" ]
include_dirs = [
"$ROOT_DIR/common/include",
"$ROOT_DIR/storage_daemon/include",
"$ROOT_DIR/storage_manager/include",
"//foundation/filemanagement/storage_service/utils/include",
"//foundation/filemanagement/storage_service/interfaces/innerkits/storage_manager/native",
"//utils/native/base/include",
]
sources = [
"$ROOT_DIR/storage_daemon/disk/src/disk_config.cpp",
"$ROOT_DIR/storage_daemon/disk/src/disk_info.cpp",
"$ROOT_DIR/storage_daemon/disk/src/disk_manager.cpp",
"$ROOT_DIR/storage_daemon/ipc/src/storage_manager_client.cpp",
"$ROOT_DIR/storage_daemon/netlink/src/netlink_data.cpp",
"$ROOT_DIR/storage_daemon/netlink/src/netlink_handler.cpp",
"$ROOT_DIR/storage_daemon/netlink/src/netlink_listener.cpp",
"$ROOT_DIR/storage_daemon/netlink/test/netlink_handler_test.cpp",
"$ROOT_DIR/storage_daemon/utils/disk_utils.cpp",
"$ROOT_DIR/storage_daemon/utils/file_utils.cpp",
"$ROOT_DIR/storage_daemon/utils/string_utils.cpp",
"$ROOT_DIR/storage_daemon/volume/src/external_volume_info.cpp",
"$ROOT_DIR/storage_daemon/volume/src/process.cpp",
"$ROOT_DIR/storage_daemon/volume/src/volume_info.cpp",
"$ROOT_DIR/storage_daemon/volume/src/volume_manager.cpp",
"$ROOT_DIR/storage_manager/innerkits_impl/src/disk.cpp",
"$ROOT_DIR/storage_manager/innerkits_impl/src/volume_core.cpp",
]
deps = [
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"safwk:system_ability_fwk",
"samgr_standard:samgr_proxy",
]
}
ohos_unittest("netlink_listener_test") {
module_out_path = "filemanagement/storage_service/storage_daemon"
defines = [ "STORAGE_LOG_TAG = \"StorageDaemon\"" ]
include_dirs = [
"$ROOT_DIR/storage_daemon/include",
"$ROOT_DIR/common/include",
"//foundation/filemanagement/storage_service/utils/include",
]
sources = [
"$ROOT_DIR/storage_daemon/netlink/src/netlink_listener.cpp",
"$ROOT_DIR/storage_daemon/netlink/test/netlink_listener_test.cpp",
]
deps = [
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
]
}
ohos_unittest("netlink_manager_test") {
module_out_path = "filemanagement/storage_service/storage_daemon"
defines = [ "STORAGE_LOG_TAG = \"StorageDaemon\"" ]
include_dirs = [
"$ROOT_DIR/common/include",
"$ROOT_DIR/storage_daemon/include",
"$ROOT_DIR/storage_manager/include",
"//foundation/filemanagement/storage_service/utils/include",
"//foundation/filemanagement/storage_service/interfaces/innerkits/storage_manager/native",
"//utils/native/base/include",
]
sources = [
"$ROOT_DIR/storage_daemon/disk/src/disk_config.cpp",
"$ROOT_DIR/storage_daemon/disk/src/disk_info.cpp",
"$ROOT_DIR/storage_daemon/disk/src/disk_manager.cpp",
"$ROOT_DIR/storage_daemon/ipc/src/storage_manager_client.cpp",
"$ROOT_DIR/storage_daemon/netlink/src/netlink_data.cpp",
"$ROOT_DIR/storage_daemon/netlink/src/netlink_handler.cpp",
"$ROOT_DIR/storage_daemon/netlink/src/netlink_listener.cpp",
"$ROOT_DIR/storage_daemon/netlink/src/netlink_manager.cpp",
"$ROOT_DIR/storage_daemon/netlink/test/netlink_manager_test.cpp",
"$ROOT_DIR/storage_daemon/utils/disk_utils.cpp",
"$ROOT_DIR/storage_daemon/utils/file_utils.cpp",
"$ROOT_DIR/storage_daemon/utils/string_utils.cpp",
"$ROOT_DIR/storage_daemon/volume/src/external_volume_info.cpp",
"$ROOT_DIR/storage_daemon/volume/src/process.cpp",
"$ROOT_DIR/storage_daemon/volume/src/volume_info.cpp",
"$ROOT_DIR/storage_daemon/volume/src/volume_manager.cpp",
"$ROOT_DIR/storage_manager/innerkits_impl/src/disk.cpp",
"$ROOT_DIR/storage_manager/innerkits_impl/src/volume_core.cpp",
]
deps = [
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"safwk:system_ability_fwk",
"samgr_standard:samgr_proxy",
]
}
group("storage_daemon_netlink_test") {
testonly = true
deps = [ ":netlink_manager_test" ]
deps = [
":netlink_data_test",
":netlink_handler_test",
":netlink_listener_test",
":netlink_manager_test",
]
}

View File

@ -0,0 +1,157 @@
/*
* 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 <gtest/gtest.h>
#include "netlink/netlink_data.h"
#include "storage_service_log.h"
namespace OHOS {
namespace StorageDaemon {
namespace {
const char* ERROR_MESSAGE = "AAAAAABBBBBBCCCCCC\0param=add\0";
const char* DEVPATH_TEST = "DEVPATH=/dev/test\0ACTION=add\0";
const char* SUBSYSTEM_TEST = "SUBSYSTEM=ABCABC\0ACTION=add\0";
const char* PARAM_TEST = "ParamName=test\0ACTION=add\0";
}
using namespace testing::ext;
class NetlinkDataTest : public testing::Test {
public:
static void SetUpTestCase(void) {};
static void TearDownTestCase(void) {};
void SetUp() {};
void TearDown() {};
};
/**
* @tc.name: NetLinkDataTest_Decode_001
* @tc.desc: Verify the Decode function when msg is incorrect.
* @tc.type: FUNC
*/
HWTEST_F(NetlinkDataTest, NetlinkDataTest_Decode_001, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkDataTest_Decode_001 start";
NetlinkData netlinkData;
netlinkData.Decode(ERROR_MESSAGE);
EXPECT_TRUE(netlinkData.GetSyspath().compare("") == 0) << "check the Syspath";
EXPECT_TRUE(netlinkData.GetDevpath().compare("") == 0) << "check the Devpath";
EXPECT_TRUE(netlinkData.GetSubsystem().compare("") == 0) << "check the Subsystem";
EXPECT_TRUE(netlinkData.GetAction() == NetlinkData::Actions::UNKNOWN) << "check the Action";
GTEST_LOG_(INFO) << "NetlinkDataTest_Decode_001 end";
}
/**
* @tc.name: NetlinkDataTest_Decode_002
* @tc.desc: Verify the Decode function of ACTION.
* @tc.type: FUNC
*/
HWTEST_F(NetlinkDataTest, NetlinkDataTest_Decode_002, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkDataTest_Decode_002 start";
std::map<std::string, NetlinkData::Actions> actions = {
{"ACTION=add\0DEVPATH=/dev/test\0", NetlinkData::Actions::ADD},
{"ACTION=remove\0DEVPATH=/dev/test\0", NetlinkData::Actions::REMOVE},
{"ACTION=move\0DEVPATH=/dev/test\0", NetlinkData::Actions::MOVE},
{"ACTION=change\0DEVPATH=/dev/test\0", NetlinkData::Actions::CHANGE},
{"ACTION=online\0DEVPATH=/dev/test\0", NetlinkData::Actions::ONLINE},
{"ACTION=offline\0DEVPATH=/dev/test\0", NetlinkData::Actions::OFFLINE},
{"ACTION=bind\0DEVPATH=/dev/test\0", NetlinkData::Actions::BIND},
{"ACTION=unbind\0DEVPATH=/dev/test\0", NetlinkData::Actions::UNBIND}
};
NetlinkData netlinkData;
EXPECT_TRUE(actions.size() == netlinkData.actionMaps.size())
<< "confirm test action count as the same of the actual";
for (auto action : actions) {
netlinkData.Decode(action.first.c_str());
EXPECT_TRUE(netlinkData.GetAction() == action.second) << "check the Action";
}
GTEST_LOG_(INFO) << "NetlinkDataTest_Decode_002 end";
}
/**
* @tc.name: NetLinkDataTest_Decode_003
* @tc.desc: Verify the Decode function of Devpath and Syspath.
* @tc.type: FUNC
*/
HWTEST_F(NetlinkDataTest, NetlinkDataTest_Decode_003, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkDataTest_Decode_003 start";
NetlinkData netlinkData;
netlinkData.Decode(DEVPATH_TEST);
EXPECT_TRUE(netlinkData.GetDevpath().compare("/dev/test") == 0) << "check the Devpath";
EXPECT_TRUE(netlinkData.GetSyspath().compare("/sys/dev/test") == 0) << "check the Syspath";
GTEST_LOG_(INFO) << "NetlinkDataTest_Decode_003 end";
}
/**
* @tc.name: NetLinkDataTest_Decode_004
* @tc.desc: Verify the Decode function of Subsystem.
* @tc.type: FUNC
*/
HWTEST_F(NetlinkDataTest, NetlinkDataTest_Decode_004, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkDataTest_Decode_004 start";
NetlinkData netlinkData;
netlinkData.Decode(SUBSYSTEM_TEST);
EXPECT_TRUE(netlinkData.GetSubsystem().compare("ABCABC") == 0) << "check the Subsystem";
GTEST_LOG_(INFO) << "NetlinkDataTest_Decode_004 end";
}
/**
* @tc.name: NetlinkDataTest_GetParam_001
* @tc.desc: Verify the GetParam function when para name is incorrect.
* @tc.type: FUNC
*/
HWTEST_F(NetlinkDataTest, NetlinkDataTest_GetParam_001, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetLinkDataTest_GetParam_001 start";
NetlinkData netlinkData;
netlinkData.Decode(PARAM_TEST);
std::string param = netlinkData.GetParam("IncorrectParamName");
EXPECT_TRUE(param.compare("") == 0) << "get \"\" when para name is incorrect";
GTEST_LOG_(INFO) << "NetlinkDataTest_GetParam_001 end";
}
/**
* @tc.name: NetLinkDataTest_GetParam_002
* @tc.desc: Verify the GetParam function when para name is correct.
* @tc.type: FUNC
*/
HWTEST_F(NetlinkDataTest, NetlinkDataTest_GetParam_002, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkDataTest_GetParam_002 start";
NetlinkData netlinkData;
netlinkData.Decode(PARAM_TEST);
std::string param = netlinkData.GetParam("ParamName");
EXPECT_TRUE(param.compare("test") == 0) << "check GetParam when para name is correct";
GTEST_LOG_(INFO) << "NetlinkDataTest_GetParam_002 end";
}
} // STORAGE_DAEMON
} // OHOS

View File

@ -0,0 +1,128 @@
/*
* 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 <fcntl.h>
#include <memory>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include "gtest/gtest.h"
#include "netlink/netlink_handler.h"
#include "securec.h"
#include "storage_service_errno.h"
#include "storage_service_log.h"
namespace OHOS {
namespace StorageDaemon {
using namespace testing::ext;
int32_t StartSocket(int32_t& socketFd);
class NetlinkHandlerTest : public testing::Test {
public:
static void SetUpTestCase(void) {};
static void TearDownTestCase(void) {};
void SetUp() {};
void TearDown() {};
};
/**
* @tc.name: NetlinkHandlerTest_Start_001
* @tc.desc: Verify the Start function when socket not start.
* @tc.type: FUNC
* @tc.require: SR000GGUOT
*/
HWTEST_F(NetlinkHandlerTest, NetlinkHandlerTest_Start_001, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkHandlerTest_Start_001 start";
int32_t socket = -1;
std::shared_ptr<NetlinkHandler> handler = std::make_shared<NetlinkHandler>(socket);
auto execFun = [](std::shared_ptr<NetlinkHandler> netlinkHandler) {
int32_t ret = netlinkHandler->Start();
EXPECT_TRUE(ret == E_ERR);
};
std::thread execThread(execFun, handler);
sleep(1);
execThread.join();
GTEST_LOG_(INFO) << "NetlinkHandlerTest_Start_001 end";
}
/**
* @tc.name: NetlinkHandlerTest_Start_Stop_001
* @tc.desc: Verify the Start/Stop function.
* @tc.type: FUNC
* @tc.require: SR000GGUOT
*/
HWTEST_F(NetlinkHandlerTest, NetlinkHandlerTest_Start_Stop_001, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkHandlerTest_Start_Stop_001 start";
int32_t socket = 0;
auto ret = StartSocket(socket);
ASSERT_TRUE(ret == 0 && socket >= 0);
std::shared_ptr<NetlinkHandler> netlinkHandler = std::make_shared<NetlinkHandler>(socket);
auto execFun = [](std::shared_ptr<NetlinkHandler> handler) {
auto ret = handler->Start();
EXPECT_TRUE(ret == E_OK);
};
std::thread execThread(execFun, netlinkHandler);
sleep(1);
ret = netlinkHandler->Stop();
EXPECT_TRUE(ret == E_OK);
execThread.join();
(void)close(socket);
GTEST_LOG_(INFO) << "NetlinkHandlerTest_Start_Stop_001 end";
}
int32_t StartSocket(int32_t& socketFd)
{
struct sockaddr_nl addr;
int32_t sz = 256 * 1024;
int32_t on = 1;
(void)memset_s(&addr, sizeof(addr), 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
addr.nl_groups = 0xffffffff;
socketFd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
if (socketFd < 0) {
LOGE("Create netlink socket failed, errno %{public}d", errno);
return -1;
}
if (setsockopt(socketFd, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) != 0) {
LOGE("Set SO_RCVBUFFORCE failed, errno %{public}d", errno);
(void)close(socketFd);
return -1;
}
if (setsockopt(socketFd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) != 0) {
LOGE("Set SO_PASSCRED failed, errno %{public}d", errno);
(void)close(socketFd);
return -1;
}
if (bind(socketFd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
LOGE("Socket bind failed, errno %{public}d", errno);
(void)close(socketFd);
return -1;
}
return 0;
}
} // STORAGE_DAEMON
} // OHOS

View File

@ -0,0 +1,32 @@
/*
* 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 NETLINK_LISTENER_MOCK_H
#define NETLINK_LISTENER_MOCK_H
#include "gmock/gmock.h"
#include "netlink/netlink_listener.h"
namespace OHOS {
namespace StorageDaemon {
class NetlinkListenerMock : public NetlinkListener {
public:
explicit NetlinkListenerMock(int32_t socket) : NetlinkListener(socket) {}
virtual ~NetlinkListenerMock() {}
MOCK_METHOD1(OnEvent, void(char *));
};
} // namespace StorageDaemon
} // namespace OHOS
#endif // NETLINK_LISTENER_MOCK_H

View File

@ -0,0 +1,126 @@
/*
* 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 <fcntl.h>
#include <memory>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include "gtest/gtest.h"
#include "netlink_listener_mock.h"
#include "securec.h"
#include "storage_service_errno.h"
#include "storage_service_log.h"
namespace OHOS {
namespace StorageDaemon {
using namespace testing::ext;
int32_t StartSocket(int32_t& socketFd);
class NetlinkListenerTest : public testing::Test {
public:
static void SetUpTestCase(void) {};
static void TearDownTestCase(void) {};
void SetUp() {};
void TearDown() {};
};
/**
* @tc.name: NetlinkListenerTest_StartListener_001
* @tc.desc: Verify the StartListener function when socket not start.
* @tc.type: FUNC
* @tc.require: SR000GGUOT
*/
HWTEST_F(NetlinkListenerTest, NetlinkListenerTest_StartListener_001, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkListenerTest_StartListener_001 start";
int32_t socket = -1;
std::shared_ptr<NetlinkListenerMock> mock = std::make_shared<NetlinkListenerMock>(socket);
auto execFun = [](std::shared_ptr<NetlinkListenerMock> netlinkListerner) {
int32_t ret = netlinkListerner->StartListener();
EXPECT_TRUE(ret == E_ERR);
};
std::thread execThread(execFun, mock);
sleep(1);
execThread.join();
GTEST_LOG_(INFO) << "NetlinkListenerTest_StartListener_001 end";
}
/**
* @tc.name: NetlinkListenerTest_StartListener_StopListener_001
* @tc.desc: Verify the StartListener/StopListener function.
* @tc.type: FUNC
* @tc.require: SR000GGUOT
*/
HWTEST_F(NetlinkListenerTest, NetlinkListenerTest_StartListener_StopListener_001, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NetlinkListenerTest_StartListener_StopListener_001 start";
int32_t socket = 0;
int32_t ret = StartSocket(socket);
ASSERT_TRUE(ret == 0 && socket >= 0);
std::shared_ptr<NetlinkListenerMock> mock = std::make_shared<NetlinkListenerMock>(socket);
auto execFun = [](std::shared_ptr<NetlinkListenerMock> netlinkListerner) {
int32_t ret = netlinkListerner->StartListener();
EXPECT_TRUE(ret == E_OK);
};
std::thread execThread(execFun, mock);
sleep(1);
ret = mock->StopListener();
EXPECT_TRUE(ret == E_OK);
execThread.join();
(void)close(socket);
GTEST_LOG_(INFO) << "NetlinkListenerTest_StartListener_StopListener_001 end";
}
int32_t StartSocket(int32_t& socketFd)
{
struct sockaddr_nl addr;
int32_t sz = 256 * 1024;
int32_t on = 1;
(void)memset_s(&addr, sizeof(addr), 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
addr.nl_groups = 0xffffffff;
socketFd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
if (socketFd < 0) {
LOGE("Create netlink socket failed, errno %{public}d", errno);
return -1;
}
if (setsockopt(socketFd, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) != 0) {
LOGE("Set SO_RCVBUFFORCE failed, errno %{public}d", errno);
(void)close(socketFd);
return -1;
}
if (setsockopt(socketFd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) != 0) {
LOGE("Set SO_PASSCRED failed, errno %{public}d", errno);
(void)close(socketFd);
return -1;
}
if (bind(socketFd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
LOGE("Socket bind failed, errno %{public}d", errno);
(void)close(socketFd);
return -1;
}
return 0;
}
} // STORAGE_DAEMON
} // OHOS

View File

@ -15,6 +15,8 @@
#include <gtest/gtest.h>
#include <memory>
#include "storage_service_errno.h"
#include "storage_service_log.h"
#include "netlink/netlink_manager.h"
@ -46,5 +48,52 @@ HWTEST_F(NetlinkManagerTest, Storage_Service_NetlinkManagerTest_Instance_001, Te
GTEST_LOG_(INFO) << "Storage_Service_NetlinkManagerTest_Instance_001 end";
}
/**
* @tc.name: Storage_Service_NetlinkManagerTest_Instance_002
* @tc.desc: Verify the Instance function.
* @tc.type: FUNC
* @tc.require: SR000GGUOT
*/
HWTEST_F(NetlinkManagerTest, Storage_Service_NetlinkManagerTest_Instance_002, TestSize.Level1)
{
GTEST_LOG_(INFO) << "Storage_Service_NetlinkManagerTest_Instance_002 start";
NetlinkManager *netlinkManager1 = NetlinkManager::Instance();
ASSERT_TRUE(netlinkManager1 != nullptr);
NetlinkManager *netlinkManager2 = NetlinkManager::Instance();
ASSERT_TRUE(netlinkManager2 != nullptr);
ASSERT_TRUE(netlinkManager1 == netlinkManager2);
GTEST_LOG_(INFO) << "Storage_Service_NetlinkManagerTest_Instance_002 end";
}
/**
* @tc.name: Storage_Service_NetlinkManagerTest_Start_Stop_001
* @tc.desc: Verify the Start/Stop function.
* @tc.type: FUNC
* @tc.require: SR000GGUOT
*/
HWTEST_F(NetlinkManagerTest, Storage_Service_NetlinkManagerTest_Start_Stop_001, TestSize.Level1)
{
GTEST_LOG_(INFO) << "Storage_Service_NetlinkManagerTest_Start_Stop_001 start";
NetlinkManager *netlinkManager = NetlinkManager::Instance();
ASSERT_TRUE(netlinkManager != nullptr);
auto execFun = [](NetlinkManager *manager) {
auto ret = manager->Start();
EXPECT_TRUE(ret == E_OK);
};
std::thread execThread(execFun, netlinkManager);
sleep(1);
auto ret = netlinkManager->Stop();
EXPECT_TRUE(ret == E_OK);
execThread.join();
GTEST_LOG_(INFO) << "Storage_Service_NetlinkManagerTest_Start_Stop_001 end";
}
} // STORAGE_DAEMON
} // OHOS

View File

@ -22,8 +22,8 @@ ohos_unittest("external_volume_info_test") {
include_dirs = [
"$ROOT_DIR/storage_daemon/include",
"//foundation/filemanagement/storage_service/services/common/include",
"//foundation/filemanagement/storage_service/services/storage_manager/include",
"$ROOT_DIR/common/include",
"$ROOT_DIR/storage_manager/include",
]
sources = [
@ -55,8 +55,8 @@ ohos_unittest("volume_info_test") {
include_dirs = [
"$ROOT_DIR/storage_daemon/include",
"$ROOT_DIR/storage_daemon/utils/test/common",
"//foundation/filemanagement/storage_service/services/common/include",
"//foundation/filemanagement/storage_service/services/storage_manager/include",
"$ROOT_DIR/common/include",
"$ROOT_DIR/storage_manager/include",
]
sources = [
@ -86,8 +86,8 @@ ohos_unittest("volume_manager_test") {
include_dirs = [
"$ROOT_DIR/storage_daemon/include",
"$ROOT_DIR/storage_daemon/include/volume",
"//foundation/filemanagement/storage_service/services/common/include",
"//foundation/filemanagement/storage_service/services/storage_manager/include",
"$ROOT_DIR/common/include",
"$ROOT_DIR/storage_manager/include",
"//foundation/filemanagement/storage_service/interfaces/innerkits/storage_manager/native",
"//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include",
]