add net interface hats test

Signed-off-by: wenfei6316 <wen_fei@runkaihong.com.cn>
Change-Id: Ibd55aa84b1b9e1ead77ed98896210af358106b88
This commit is contained in:
wenfei6316 2024-09-04 13:40:13 +08:00
parent e44b44b153
commit c4aade6d73
7 changed files with 296 additions and 2 deletions

View File

@ -54,7 +54,8 @@ Note:If the text contains special characters, please escape them according to th
<configuration>
<oatconfig>
<policylist>
<policy name="projectPolicy" desc=""><policyitem type="copyright" name="Huawei Technologies Co., Ltd." path=".*" rule="may" group="defaultGroup" filefilter="copyrightPolicyFilter" desc="Developed by Huawei Technologies"/>
<policy name="projectPolicy" desc="">
<policyitem type="copyright" name="Huawei Technologies Co., Ltd." path=".*" rule="may" group="defaultGroup" filefilter="copyrightPolicyFilter" desc="Developed by Huawei Technologies"/>
<policyitem type="copyright" name="Huawei Device Co., Ltd." path=".*" rule="may" group="defaultGroup" filefilter="copyrightPolicyFilter" desc="Developed by Huawei Device"/>
<policyitem type="copyright" name="HiHope Open Source Organization." path=".*" rule="may" group="defaultGroup" filefilter="copyrightPolicyFilter" desc="Developed By Hihope"/>
</policy>

View File

@ -12,7 +12,7 @@
# limitations under the License.
import("//build/ohos_var.gni")
import("../build.gni")
import("//test/xts/hats/build.gni")
import(
"open_posix_testsuite/conformance/interfaces_gn/posix_interface_config.gni")
@ -33,6 +33,7 @@ group("kernel") {
"prctl:HatsPrctlTest",
"purgeableMem:HatsPurgeable",
"rtginterface:HatsRtgInterfaceTest",
"syscalls:HatsSyscallTest",
]
} else {
deps = [
@ -48,6 +49,7 @@ group("kernel") {
"prctl:HatsPrctlTest",
"purgeableMem:HatsPurgeable",
"rtginterface:HatsRtgInterfaceTest",
"syscalls:HatsSyscallTest",
]
}
}

20
kernel/syscalls/BUILD.gn Normal file
View File

@ -0,0 +1,20 @@
# Copyright (C) 2024 HiHope Open Source Organization.
# 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/ohos_var.gni")
import("//test/xts/hats/build.gni")
group("HatsSyscallTest") {
testonly = true
deps = [ "net:HatsNetTest" ]
}

View File

@ -0,0 +1,20 @@
# Copyright (C) 2024 HiHope Open Source Organization.
# 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/ohos_var.gni")
import("//test/xts/hats/build.gni")
group("HatsNetTest") {
testonly = true
deps = [ "accept4:HatsAccept4Test" ]
}

View File

@ -0,0 +1,207 @@
/*
* Copyright (C) 2024 HiHope Open Source Organization.
* 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 <cerrno>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#include <arpa/inet.h>
#include <gtest/gtest.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "securec.h"
using namespace testing::ext;
class HatsAccept4Test : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
private:
};
void HatsAccept4Test::SetUp()
{
}
void HatsAccept4Test::TearDown()
{
}
void HatsAccept4Test::SetUpTestCase()
{
}
void HatsAccept4Test::TearDownTestCase()
{
}
static const int BAD_SOCKET_FD = -1;
static const int TEST_PORT = 8888;
static const char *TEST_LOCAL_IP = "127.0.0.1";
static const char *TEST_CLIENT_IP = "0.0.0.0";
enum AcceptType {
GET_NONE = 0,
GET_CLIENT_SOCKET_ADDR_TEST,
GET_CLIENT_SOCKET_ADDR_LEN_TEST,
};
static void SocketServiceStart(int *fd)
{
int ret;
int socketFd = -1;
int32_t backLog = 2;
int32_t optVal = 1;
struct sockaddr_in serAddr = {
.sin_family = AF_INET,
.sin_port = htons(TEST_PORT),
.sin_addr = {
.s_addr = inet_addr(TEST_LOCAL_IP),
}
};
socketFd = socket(AF_INET, SOCK_STREAM, 0);
ASSERT_TRUE(socketFd > 0);
ret = setsockopt(socketFd, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(optVal));
ASSERT_TRUE(ret == 0);
ret = bind(socketFd, reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(serAddr));
ASSERT_TRUE(ret == 0);
ret = listen(socketFd, backLog);
ASSERT_EQ(ret, 0);
*fd = socketFd;
}
static void ClientConnect(void)
{
int ret;
int socketFd = -1;
struct sockaddr_in serAddr = {
.sin_family = AF_INET,
.sin_port = htons(TEST_PORT),
.sin_addr = {
.s_addr = inet_addr(TEST_LOCAL_IP),
}
};
socketFd = socket(AF_INET, SOCK_STREAM, 0);
EXPECT_TRUE(socketFd > 0);
ret = connect(socketFd, reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(struct sockaddr_in));
EXPECT_EQ(ret, 0);
close(socketFd);
}
/*
* @tc.number SUB_KERNEL_SYSCALL_ACCEPT4_0100
* @tc.name Accept4ValidSockfdSuccess_0001
* @tc.desc accept4 valid sockfd success.
*/
HWTEST_F(HatsAccept4Test, Accept4ValidSockfdSuccess_0001, Function | MediumTest | Level1)
{
int pid = -1;
int socketFd = -1;
int acceptFd = -1;
int status = 0;
struct sockaddr_in cliAddr;
socklen_t addrlen;
SocketServiceStart(&socketFd);
ASSERT_TRUE(socketFd > 0);
if ((pid = fork()) == 0) {
ClientConnect();
exit(0);
}
acceptFd = accept4(socketFd, reinterpret_cast<struct sockaddr *>(&cliAddr), &addrlen, 0);
EXPECT_TRUE(acceptFd > 0);
close(acceptFd);
close(socketFd);
waitpid(pid, &status, 0);
EXPECT_TRUE(status == 0);
}
/*
* @tc.number SUB_KERNEL_SYSCALL_ACCEPT4_0200
* @tc.name Accept4GetClientAddrSuccess_0002
* @tc.desc accept4 get client addr success.
*/
HWTEST_F(HatsAccept4Test, Accept4GetClientAddrSuccess_0002, Function | MediumTest | Level1)
{
int pid = -1;
int acceptFd = -1;
int socketFd = -1;
int status = 0;
struct sockaddr_in cliAddr = { 0 };
socklen_t addrlen = 0;
SocketServiceStart(&socketFd);
ASSERT_TRUE(socketFd > 0);
if ((pid = fork()) == 0) {
ClientConnect();
exit(0);
}
acceptFd = accept4(socketFd, reinterpret_cast<struct sockaddr *>(&cliAddr), &addrlen, 0);
EXPECT_STREQ(inet_ntoa(cliAddr.sin_addr), TEST_CLIENT_IP);
close(acceptFd);
close(socketFd);
waitpid(pid, &status, 0);
EXPECT_TRUE(status == 0);
}
/*
* @tc.number SUB_KERNEL_SYSCALL_ACCEPT4_0300
* @tc.name Accept4GetClientAddrSuccess_0003
* @tc.desc accept4 get client addr len success.
*/
HWTEST_F(HatsAccept4Test, Accept4GetClientAddrSuccess_0003, Function | MediumTest | Level1)
{
int pid = -1;
int acceptFd = -1;
int socketFd = -1;
int status = 0;
struct sockaddr_in cliAddr = { 0 };
socklen_t addrlen;
SocketServiceStart(&socketFd);
ASSERT_TRUE(socketFd > 0);
if ((pid = fork()) == 0) {
ClientConnect();
exit(0);
}
acceptFd = accept4(socketFd, reinterpret_cast<struct sockaddr *>(&cliAddr), &addrlen, 0);
EXPECT_EQ(addrlen, sizeof(struct sockaddr));
close(acceptFd);
close(socketFd);
waitpid(pid, &status, 0);
EXPECT_TRUE(status == 0);
}
/*
* @tc.number SUB_KERNEL_SYSCALL_ACCEPT4_0400
* @tc.name Accept4InvalidFd_0004
* @tc.desc accept4 use invalid socket fd, return -1, and set errno.
*/
HWTEST_F(HatsAccept4Test, Accept4InvalidFd_0004, Function | MediumTest | Level2)
{
int ret = accept4(BAD_SOCKET_FD, nullptr, nullptr, 0);
EXPECT_EQ(ret, -1);
EXPECT_EQ(errno, EBADF);
ret = accept4(STDIN_FILENO, nullptr, nullptr, 0);
EXPECT_EQ(ret, -1);
EXPECT_EQ(errno, ENOTSOCK);
}

View File

@ -0,0 +1,26 @@
# Copyright (C) 2024 HiHope Open Source Organization.
# 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("//test/xts/tools/build/suite.gni")
module_output_path = "hats/syscalls"
ohos_moduletest_suite("HatsAccept4Test") {
module_out_path = module_output_path
sources = [ "./Accept4ApiTest.cpp" ]
deps = [ "//third_party/googletest:gtest_main" ]
include_dirs = [ "include" ]
cflags = [ "-Wno-error" ]
external_deps = [ "c_utils:utils" ]
subsystem_name = "kernel"
part_name = "hats"
}

View File

@ -0,0 +1,18 @@
{
"description": "Configuration for HatsAccept4Test Tests",
"kits": [
{
"push": [
"HatsAccept4Test->/data/local/tmp/HatsAccept4Test"
],
"type": "PushKit"
}
],
"driver": {
"native-test-timeout": "120000",
"type": "CppTest",
"module-name": "HatsAccept4Test",
"runtime-hint": "1s",
"native-test-device-path": "/data/local/tmp"
}
}