add testsuites
Change-Id: Ice1193d1ae7f2e0d12a2a38a306a6399407f5037
This commit is contained in:
77
testsuites/unittest/net/netdb/BUILD.gn
Normal file
77
testsuites/unittest/net/netdb/BUILD.gn
Normal file
@@ -0,0 +1,77 @@
|
||||
# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other materials
|
||||
# provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
# to endorse or promote products derived from this software without specific prior written
|
||||
# permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import("//build/lite/config/test.gni")
|
||||
import("../../config.gni")
|
||||
|
||||
config("net_local_config") {
|
||||
if (LOSCFG_USER_TEST_NET_NETDB == true) {
|
||||
cflags = [ "-DLOSCFG_USER_TEST_NET_NETDB" ]
|
||||
cflags_cc = cflags
|
||||
}
|
||||
}
|
||||
|
||||
unittest("liteos_a_net_netdb_unittest") {
|
||||
output_extension = "bin"
|
||||
output_dir = "$root_out_dir/test/unittest/kernel"
|
||||
include_dirs = [
|
||||
"../../common/include",
|
||||
"./",
|
||||
]
|
||||
sources = [
|
||||
"../../common/osTest.cpp",
|
||||
"full/net_netdb_test_001.cpp",
|
||||
"full/net_netdb_test_002.cpp",
|
||||
"full/net_netdb_test_003.cpp",
|
||||
"full/net_netdb_test_004.cpp",
|
||||
"full/net_netdb_test_005.cpp",
|
||||
"full/net_netdb_test_006.cpp",
|
||||
"full/net_netdb_test_007.cpp",
|
||||
"full/net_netdb_test_008.cpp",
|
||||
"full/net_netdb_test_009.cpp",
|
||||
"full/net_netdb_test_010.cpp",
|
||||
"full/net_netdb_test_011.cpp",
|
||||
"full/net_netdb_test_012.cpp",
|
||||
"full/net_netdb_test_013.cpp",
|
||||
"full/net_netdb_test_014.cpp",
|
||||
"full/net_netdb_test_015.cpp",
|
||||
"full/net_netdb_test_016.cpp",
|
||||
"full/net_netdb_test_017.cpp",
|
||||
"full/net_netdb_test_018.cpp",
|
||||
"full/net_netdb_test_019.cpp",
|
||||
"full/net_netdb_test_020.cpp",
|
||||
"full/net_netdb_test_021.cpp",
|
||||
"full/net_netdb_test_022.cpp",
|
||||
"net_netdb_test.cpp",
|
||||
]
|
||||
configs = [
|
||||
"../..:public_config",
|
||||
":net_local_config",
|
||||
]
|
||||
}
|
||||
58
testsuites/unittest/net/netdb/full/net_netdb_test_001.cpp
Normal file
58
testsuites/unittest/net/netdb/full/net_netdb_test_001.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int ProtoentTest(void)
|
||||
{
|
||||
setprotoent(1);
|
||||
|
||||
// refer to the `/etc/protocols' file.
|
||||
struct protoent *prot = getprotobyname("icmp");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(prot->p_proto, 1, prot->p_proto);
|
||||
|
||||
prot = getprotobynumber(1);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(prot->p_name, "icmp"), 0, -1);
|
||||
|
||||
prot = getprotoent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1);
|
||||
|
||||
endprotoent();
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest001(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, ProtoentTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
59
testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp
Normal file
59
testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int AddrInfoTest(void)
|
||||
{
|
||||
// Prerequisite: correct DNS servers must be configured.
|
||||
struct addrinfo *addr = NULL;
|
||||
int ret = getaddrinfo("example.com", "ftp", NULL, &addr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
freeaddrinfo(addr);
|
||||
|
||||
ret = getaddrinfo("local", "ftp", NULL, &addr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, EAI_AGAIN, ret);
|
||||
|
||||
ret = getaddrinfo("localhost", "fp", NULL, &addr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, EAI_SERVICE, ret);
|
||||
|
||||
const char *p = gai_strerror(EAI_AGAIN);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(p, NULL, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest002(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, AddrInfoTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
51
testsuites/unittest/net/netdb/full/net_netdb_test_003.cpp
Normal file
51
testsuites/unittest/net/netdb/full/net_netdb_test_003.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int IfAddrsTest(void)
|
||||
{
|
||||
// Prerequisite: correct DNS servers must be configured.
|
||||
struct ifaddrs *addr = NULL;
|
||||
int ret = getifaddrs(&addr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
|
||||
|
||||
freeifaddrs(addr);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest003(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, IfAddrsTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
65
testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp
Normal file
65
testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetHostByAddrTest(void)
|
||||
{
|
||||
struct in_addr ia;
|
||||
int length = 4;
|
||||
ia.s_addr = inet_addr("127.0.0.1");
|
||||
struct hostent *addr = gethostbyaddr(&ia, sizeof ia, AF_INET);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(addr->h_name, "localhost", -1);
|
||||
ICUNIT_ASSERT_EQUAL(addr->h_length, length, -1);
|
||||
|
||||
ia.s_addr = inet_addr("100.109.180.184");
|
||||
addr = gethostbyaddr(&ia, sizeof ia, AF_INET);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(addr->h_name, "szvphisprb93341", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(addr->h_aliases[0], "szvphisprb93341", -1);
|
||||
|
||||
errno = 0;
|
||||
ia.s_addr = inet_addr("127.0.0.0");
|
||||
addr = gethostbyaddr(&ia, sizeof ia, AF_INET);
|
||||
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest004(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetHostByAddrTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
68
testsuites/unittest/net/netdb/full/net_netdb_test_005.cpp
Normal file
68
testsuites/unittest/net/netdb/full/net_netdb_test_005.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetHostByAddrRTest(void)
|
||||
{
|
||||
struct in_addr ia;
|
||||
struct hostent addr, *result = NULL;
|
||||
char buf[1024];
|
||||
char buf1[1];
|
||||
int err, ret;
|
||||
int length = 4;
|
||||
|
||||
ia.s_addr = inet_addr("127.0.0.1");
|
||||
ret = gethostbyaddr_r(&ia, sizeof ia, AF_INET, &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(result->h_addrtype, AF_INET, result->h_addrtype);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(result->h_name, "localhost", -1);
|
||||
ICUNIT_ASSERT_EQUAL(result->h_length, length, -1);
|
||||
|
||||
errno = 0;
|
||||
ia.s_addr = inet_addr("127.0.0.0");
|
||||
ret = gethostbyaddr_r(&ia, sizeof ia, AF_INET, &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
|
||||
|
||||
ret = gethostbyaddr_r(&ia, sizeof ia, AF_INET, &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
|
||||
|
||||
ret = gethostbyaddr_r(&ia, sizeof ia, AF_INET, &addr, buf1, sizeof buf1, &result, &err);
|
||||
ICUNIT_ASSERT_EQUAL(ret, ERANGE, ret);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest005(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetHostByAddrRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
58
testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp
Normal file
58
testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetHostByNameTest(void)
|
||||
{
|
||||
struct hostent *addr = gethostbyname("localhost");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "localhost"), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr->h_length, 0, addr->h_length);
|
||||
|
||||
addr = gethostbyname("127.0.0.1");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "127.0.0.1"), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr->h_length, 0, addr->h_length);
|
||||
|
||||
addr = gethostbyname("lo");
|
||||
ICUNIT_ASSERT_EQUAL(addr, NULL, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest006(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetHostByNameTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
69
testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp
Normal file
69
testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetHostByNameRTest(void)
|
||||
{
|
||||
struct hostent addr, *result = NULL;
|
||||
char buf[1024];
|
||||
char buf1[1];
|
||||
int err, ret;
|
||||
|
||||
ret = gethostbyname_r("localhost", &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(result->h_name, "localhost"), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(result->h_addrtype, AF_INET, result->h_addrtype);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result->h_length, 0, result->h_length);
|
||||
|
||||
ret = gethostbyname_r("127.0.0.1", &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(result->h_name, "127.0.0.1"), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(result->h_addrtype, AF_INET, result->h_addrtype);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result->h_length, 0, result->h_length);
|
||||
|
||||
ret = gethostbyname_r("127.0.0.1", &addr, buf1, sizeof buf1, &result, &err);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
|
||||
ret = gethostbyname_r("127.0.0.0.0", &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
|
||||
ret = gethostbyname_r("lo", &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest007(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetHostByNameRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
60
testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp
Normal file
60
testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetHostByName2Test(void)
|
||||
{
|
||||
struct hostent *addr = gethostbyname2("localhost", AF_INET);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "localhost"), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr->h_length, 0, addr->h_length);
|
||||
|
||||
addr = gethostbyname2("127.0.0.1", AF_INET);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "127.0.0.1"), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(addr->h_length, 0, addr->h_length);
|
||||
|
||||
addr = gethostbyname2("127.0.0.1", AF_INET6);
|
||||
ICUNIT_ASSERT_EQUAL(addr, NULL, -1);
|
||||
addr = gethostbyname2("localh", AF_INET);
|
||||
ICUNIT_ASSERT_EQUAL(addr, NULL, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest008(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetHostByName2Test, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
68
testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp
Normal file
68
testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetHostByName2RTest(void)
|
||||
{
|
||||
struct hostent addr, *result = NULL;
|
||||
char buf[1024];
|
||||
char buf1[1];
|
||||
int err, ret;
|
||||
|
||||
ret = gethostbyname2_r("localhost", AF_INET, &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(result->h_name, "localhost"), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(result->h_addrtype, AF_INET, result->h_addrtype);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result->h_length, 0, result->h_length);
|
||||
|
||||
ret = gethostbyname2_r("127.0.0.1", AF_INET, &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(result->h_name, "127.0.0.1"), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(result->h_addrtype, AF_INET, result->h_addrtype);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result->h_length, 0, result->h_length);
|
||||
|
||||
ret = gethostbyname2_r("127.0.0.1", AF_INET, &addr, buf1, sizeof buf1, &result, &err);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
|
||||
ret = gethostbyname2_r("127.0.0.1.1", AF_INET, &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
|
||||
ret = gethostbyname2_r("lo", AF_INET, &addr, buf, sizeof buf, &result, &err);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest009(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetHostByName2RTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
51
testsuites/unittest/net/netdb/full/net_netdb_test_010.cpp
Normal file
51
testsuites/unittest/net/netdb/full/net_netdb_test_010.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int NameInfoTest(void)
|
||||
{
|
||||
char host[256], serv[256];
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(22);
|
||||
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
int ret = getnameinfo((struct sockaddr*)&addr, sizeof addr, host, sizeof host, serv, sizeof serv, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest010(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, NameInfoTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
54
testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp
Normal file
54
testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetServByPortTest(void)
|
||||
{
|
||||
// refer to the `/etc/services' file.
|
||||
struct servent *se1 = nullptr;
|
||||
struct servent *se = getservbyport(htons(22), "tcp");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se, NULL, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se->s_name, "ssh", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se->s_proto, "tcp", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se->s_aliases[0], "ssh", -1);
|
||||
|
||||
se1 = getservbyport(htons(22), "tp");
|
||||
ICUNIT_ASSERT_EQUAL(se1, nullptr, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest011(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetServByPortTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
64
testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp
Normal file
64
testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetServByPortRTest(void)
|
||||
{
|
||||
// refer to the `/etc/services' file.
|
||||
struct servent se, *result = NULL;
|
||||
char buf[1024];
|
||||
char buf1[2];
|
||||
|
||||
int ret = getservbyport_r(htons(22), "udp", &se, buf, sizeof buf, &result);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se.s_name, "ssh", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se.s_proto, "udp", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se.s_aliases[0], "ssh", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(result->s_name, "ssh", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(result->s_proto, "udp", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(result->s_aliases[0], "ssh", -1);
|
||||
|
||||
ret = getservbyport_r(htons(22), "udp", &se, buf1, sizeof buf1, &result);
|
||||
ICUNIT_ASSERT_EQUAL(ret, ERANGE, ret);
|
||||
|
||||
ret = getservbyport_r(htons(22), "ud", &se, buf, sizeof buf, &result);
|
||||
ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest012(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetServByPortRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
48
testsuites/unittest/net/netdb/full/net_netdb_test_013.cpp
Normal file
48
testsuites/unittest/net/netdb/full/net_netdb_test_013.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int HerrorTest(void)
|
||||
{
|
||||
const char *err = hstrerror(TRY_AGAIN);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(err, NULL, -1);
|
||||
|
||||
herror(err);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest013(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, HerrorTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
48
testsuites/unittest/net/netdb/full/net_netdb_test_014.cpp
Normal file
48
testsuites/unittest/net/netdb/full/net_netdb_test_014.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
#include <net/if.h>
|
||||
|
||||
static int IfNameIndexTest(void)
|
||||
{
|
||||
struct if_nameindex *idx = if_nameindex();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(idx, NULL, -1);
|
||||
|
||||
if_freenameindex(idx);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest014(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, IfNameIndexTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
61
testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp
Normal file
61
testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
#include <net/if.h>
|
||||
|
||||
static int IfNameToIndexTest(void)
|
||||
{
|
||||
int ret;
|
||||
char if_name[20];
|
||||
char *str = nullptr;
|
||||
|
||||
ret = if_nametoindex("lo");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, -1);
|
||||
|
||||
str = if_indextoname(ret, if_name);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(str, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(if_name, "lo", -1);
|
||||
|
||||
str = if_indextoname(3, if_name);
|
||||
ICUNIT_ASSERT_EQUAL(str, nullptr, -1);
|
||||
ICUNIT_ASSERT_EQUAL(errno, ENXIO, errno);
|
||||
|
||||
ret = if_nametoindex("eth1");
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_EQUAL(errno, ENODEV, errno);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest015(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, IfNameToIndexTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
62
testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp
Normal file
62
testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetServByNameTest(void)
|
||||
{
|
||||
struct servent *se1 = nullptr;
|
||||
struct servent *se2 = nullptr;
|
||||
|
||||
se1 = getservbyname("discard", "tcp");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se1, NULL, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "discard", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_aliases[0], "discard", -1);
|
||||
|
||||
se1 = getservbyname("ssh", "tcp");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "ssh", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_aliases[0], "ssh", -1);
|
||||
|
||||
se2 = getservbyname("cho", "udp");
|
||||
ICUNIT_ASSERT_EQUAL(se2, nullptr, -1);
|
||||
|
||||
se2 = getservbyname("systat", "udp");
|
||||
ICUNIT_ASSERT_EQUAL(se2, nullptr, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest016(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetServByNameTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
67
testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp
Normal file
67
testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetServByNameRTest(void)
|
||||
{
|
||||
struct servent se;
|
||||
struct servent *result = NULL;
|
||||
char buf1[1024] = { 0 };
|
||||
char buf2[2] = { 0 };
|
||||
int ret;
|
||||
|
||||
errno = 0;
|
||||
ret = getservbyname_r("ssh", "tcp", &se, buf1, sizeof buf1, &result);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se.s_name, "ssh", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se.s_proto, "tcp", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se.s_aliases[0], "ssh", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(result->s_name, "ssh", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(result->s_proto, "tcp", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(result->s_aliases[0], "ssh", -1);
|
||||
|
||||
ret = getservbyname_r("ssh", "tcp", &se, buf2, sizeof buf2, &result);
|
||||
ICUNIT_ASSERT_EQUAL(ret, ERANGE, ret);
|
||||
|
||||
ret = getservbyname_r("ssh", "tp", &se, buf1, sizeof buf1, &result);
|
||||
ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
|
||||
|
||||
ret = getservbyname_r("sh", "tcp", &se, buf1, sizeof buf1, &result);
|
||||
ICUNIT_ASSERT_EQUAL(ret, ENOENT, ret);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest017(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetServByNameRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
83
testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp
Normal file
83
testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetServEntTest(void)
|
||||
{
|
||||
struct servent *se1 = nullptr;
|
||||
struct servent *se2 = nullptr;
|
||||
struct servent *se3 = nullptr;
|
||||
|
||||
se1 = getservent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "tcpmux", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->s_port, ntohs(1), -1);
|
||||
|
||||
endservent();
|
||||
se2 = getservent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, se2->s_name, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, se2->s_proto, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->s_port, se2->s_port, -1);
|
||||
|
||||
setservent(0);
|
||||
se3 = getservent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, se3->s_name, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, se3->s_proto, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->s_port, se3->s_port, -1);
|
||||
|
||||
se3 = getservent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "echo", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "tcp", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(7), -1);
|
||||
|
||||
se3 = getservent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "echo", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "udp", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(7), -1);
|
||||
|
||||
se3 = getservent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "discard", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "tcp", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(9), -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->s_aliases[0], "sink", -1);
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest018(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetServEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
80
testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp
Normal file
80
testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetHostEntTest(void)
|
||||
{
|
||||
struct hostent *se1 = nullptr;
|
||||
struct hostent *se2 = nullptr;
|
||||
struct hostent *se3 = nullptr;
|
||||
int type = 2;
|
||||
int length1 = 4;
|
||||
int length2 = 16;
|
||||
|
||||
se1 = gethostent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, "localhost", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->h_addrtype, type, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->h_length, length1, -1);
|
||||
|
||||
endhostent();
|
||||
se2 = gethostent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, se2->h_name, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->h_addrtype, se2->h_addrtype, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->h_length, se2->h_length, -1);
|
||||
|
||||
sethostent(0);
|
||||
se3 = gethostent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, se3->h_name, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->h_addrtype, se3->h_addrtype, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->h_length, se3->h_length, -1);
|
||||
|
||||
se3 = gethostent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se3->h_addrtype, AF_INET6, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "localhost", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se3->h_length, length2, -1);
|
||||
|
||||
se3 = gethostent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se3->h_addrtype, AF_INET, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "szvphisprb93341", -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se3->h_aliases[0], "szvphisprb93341.huawei.com", -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest019(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetHostEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
65
testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp
Normal file
65
testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetNetEntTest(void)
|
||||
{
|
||||
struct netent *se1 = nullptr;
|
||||
struct netent *se2 = nullptr;
|
||||
struct netent *se3 = nullptr;
|
||||
|
||||
se1 = getnetent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, 2, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1);
|
||||
|
||||
endnetent();
|
||||
se2 = getnetent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, se2->n_name, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, se2->n_addrtype, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_net, se2->n_net, -1);
|
||||
|
||||
setnetent(0);
|
||||
se3 = getnetent();
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, se3->n_name, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, se3->n_addrtype, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_net, se3->n_net, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest020(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetNetEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
57
testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp
Normal file
57
testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetNetBynametTest(void)
|
||||
{
|
||||
struct netent *se1 = nullptr;
|
||||
struct netent *se2 = nullptr;
|
||||
struct netent *se3 = nullptr;
|
||||
|
||||
se1 = getnetbyname("link-local");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, 2, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1);
|
||||
|
||||
se2 = getnetbyname("link");
|
||||
ICUNIT_ASSERT_EQUAL(se2, nullptr, -1);
|
||||
|
||||
se3 = getnetbyname("hs");
|
||||
ICUNIT_ASSERT_EQUAL(se3, nullptr, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest021(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetNetBynametTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
57
testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp
Normal file
57
testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
static int GetNetByAddrtTest(void)
|
||||
{
|
||||
struct netent *se1 = nullptr;
|
||||
struct netent *se2 = nullptr;
|
||||
struct netent *se3 = nullptr;
|
||||
|
||||
se1 = getnetbyaddr(inet_network("169.254.0.0"), AF_INET);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
|
||||
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, 2, -1);
|
||||
ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1);
|
||||
|
||||
se2 = getnetbyaddr(inet_network("169.254.0.1"), AF_INET);
|
||||
ICUNIT_ASSERT_EQUAL(se2, nullptr, -1);
|
||||
|
||||
se3 = getnetbyaddr(inet_network("169.254.0.1"), AF_INET6);
|
||||
ICUNIT_ASSERT_EQUAL(se3, nullptr, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetNetDbTest022(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, GetNetByAddrtTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
65
testsuites/unittest/net/netdb/lt_net_netdb.h
Normal file
65
testsuites/unittest/net/netdb/lt_net_netdb.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef NET_RESOLV_LT_NET_NETDB_H_
|
||||
#define NET_RESOLV_LT_NET_NETDB_H_
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
void NetNetDbTest001(void);
|
||||
void NetNetDbTest002(void);
|
||||
void NetNetDbTest003(void);
|
||||
void NetNetDbTest004(void);
|
||||
void NetNetDbTest005(void);
|
||||
void NetNetDbTest006(void);
|
||||
void NetNetDbTest007(void);
|
||||
void NetNetDbTest008(void);
|
||||
void NetNetDbTest009(void);
|
||||
void NetNetDbTest010(void);
|
||||
void NetNetDbTest011(void);
|
||||
void NetNetDbTest012(void);
|
||||
void NetNetDbTest013(void);
|
||||
void NetNetDbTest014(void);
|
||||
void NetNetDbTest015(void);
|
||||
void NetNetDbTest016(void);
|
||||
void NetNetDbTest017(void);
|
||||
void NetNetDbTest018(void);
|
||||
void NetNetDbTest019(void);
|
||||
void NetNetDbTest020(void);
|
||||
void NetNetDbTest021(void);
|
||||
void NetNetDbTest022(void);
|
||||
|
||||
#endif /* NET_RESOLV_LT_NET_NETDB_H_ */
|
||||
76
testsuites/unittest/net/netdb/net_netdb_test.cpp
Normal file
76
testsuites/unittest/net/netdb/net_netdb_test.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "stdio.h"
|
||||
#include <climits>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "lt_net_netdb.h"
|
||||
|
||||
using namespace testing::ext;
|
||||
namespace OHOS {
|
||||
class NetDbTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase(void)
|
||||
{
|
||||
struct sched_param param = { 0 };
|
||||
int currThreadPolicy, ret;
|
||||
ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, ¶m);
|
||||
param.sched_priority = TASK_PRIO_TEST;
|
||||
ret = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
|
||||
}
|
||||
static void TearDownTestCase(void) {}
|
||||
};
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_FULL) && defined(LOSCFG_USER_TEST_NET_NETDB)
|
||||
/* *
|
||||
* @tc.name: NetNetDbTest001
|
||||
* @tc.desc: function for NetDbTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetDbTest, NetNetDbTest001, TestSize.Level0)
|
||||
{
|
||||
NetNetDbTest001();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetNetDbTest013
|
||||
* @tc.desc: function for NetDbTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetDbTest, NetNetDbTest013, TestSize.Level0)
|
||||
{
|
||||
NetNetDbTest013();
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
63
testsuites/unittest/net/resolv/BUILD.gn
Normal file
63
testsuites/unittest/net/resolv/BUILD.gn
Normal file
@@ -0,0 +1,63 @@
|
||||
# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other materials
|
||||
# provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
# to endorse or promote products derived from this software without specific prior written
|
||||
# permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import("//build/lite/config/test.gni")
|
||||
import("../../config.gni")
|
||||
|
||||
config("net_local_config") {
|
||||
if (LOSCFG_USER_TEST_NET_RESOLV == true) {
|
||||
cflags = [ "-DLOSCFG_USER_TEST_NET_RESOLV" ]
|
||||
cflags_cc = cflags
|
||||
}
|
||||
}
|
||||
|
||||
unittest("liteos_a_net_resolv_unittest") {
|
||||
output_extension = "bin"
|
||||
output_dir = "$root_out_dir/test/unittest/kernel"
|
||||
include_dirs = [
|
||||
"../../common/include",
|
||||
"./",
|
||||
]
|
||||
sources = [
|
||||
"../../common/osTest.cpp",
|
||||
"full/net_resolv_test_001.cpp",
|
||||
"full/net_resolv_test_002.cpp",
|
||||
"full/net_resolv_test_003.cpp",
|
||||
"full/net_resolv_test_004.cpp",
|
||||
"full/net_resolv_test_005.cpp",
|
||||
"full/net_resolv_test_006.cpp",
|
||||
"full/net_resolv_test_007.cpp",
|
||||
"full/net_resolv_test_008.cpp",
|
||||
"net_resolv_test.cpp",
|
||||
]
|
||||
configs = [
|
||||
"../..:public_config",
|
||||
":net_local_config",
|
||||
]
|
||||
}
|
||||
90
testsuites/unittest/net/resolv/full/net_resolv_test_001.cpp
Normal file
90
testsuites/unittest/net/resolv/full/net_resolv_test_001.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <lt_net_resolv.h>
|
||||
#include <string.h>
|
||||
static void dump(unsigned char *s, int len) {
|
||||
if (!s) return;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
printf("%02x ", s[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static int DnCompTest(void)
|
||||
{
|
||||
unsigned char comp_dn[100] = "example";
|
||||
unsigned char *dnptrs[6] = {comp_dn, 0};
|
||||
unsigned char **lastdnptr = &dnptrs[6];
|
||||
int offset, ret;
|
||||
|
||||
offset = strlen((const char *)dnptrs[0])+1;
|
||||
ret = dn_comp("x.y.z.example.com", comp_dn+offset, sizeof(comp_dn)-offset, dnptrs, lastdnptr);
|
||||
dump(comp_dn+offset, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 19, ret);
|
||||
ICUNIT_ASSERT_EQUAL(dnptrs[1], comp_dn+offset, dnptrs[1]);
|
||||
|
||||
offset += ret+1;
|
||||
ret = dn_comp("zz.example.com", comp_dn+offset, sizeof(comp_dn)-offset, dnptrs, lastdnptr);
|
||||
dump(comp_dn+offset, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 5, ret);
|
||||
ICUNIT_ASSERT_EQUAL(dnptrs[2], comp_dn+offset, dnptrs[2]);
|
||||
|
||||
offset += ret+1;
|
||||
ret = dn_comp("a.example.com", comp_dn+offset, sizeof(comp_dn)-offset, dnptrs, lastdnptr);
|
||||
dump(comp_dn+offset, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 4, ret);
|
||||
ICUNIT_ASSERT_EQUAL(dnptrs[3], comp_dn+offset, dnptrs[3]);
|
||||
|
||||
offset += ret+1;
|
||||
ret = dn_comp("example.com.cn", comp_dn+offset, sizeof(comp_dn)-offset, dnptrs, lastdnptr);
|
||||
dump(comp_dn+offset, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 16, ret);
|
||||
ICUNIT_ASSERT_EQUAL(dnptrs[4], comp_dn+offset, dnptrs[4]);
|
||||
|
||||
offset += ret+1;
|
||||
ret = dn_comp("2example.com", comp_dn+offset, sizeof(comp_dn)-offset, dnptrs, lastdnptr);
|
||||
dump(comp_dn+offset, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 11, ret);
|
||||
ICUNIT_ASSERT_EQUAL(dnptrs[5], /*comp_dn+offset*/ NULL, dnptrs[5]); //last one is always NULL
|
||||
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
printf("%p: %s\n", dnptrs[i], dnptrs[i]);
|
||||
}
|
||||
ICUNIT_ASSERT_EQUAL(offset+ret<100, 1, ret);
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetResolvTest001(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, DnCompTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
57
testsuites/unittest/net/resolv/full/net_resolv_test_002.cpp
Normal file
57
testsuites/unittest/net/resolv/full/net_resolv_test_002.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <lt_net_resolv.h>
|
||||
|
||||
static int EtherAtonTest(void)
|
||||
{
|
||||
struct ether_addr *eaddr = ether_aton("01::EF");
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(eaddr, NULL, -1);
|
||||
|
||||
eaddr = ether_aton("2C:9D:1E:4A:41:55");
|
||||
ICUNIT_ASSERT_NOT_EQUAL(eaddr, NULL, -1);
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[0], 0x2C, eaddr->ether_addr_octet[0]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[1], 0x9D, eaddr->ether_addr_octet[1]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[2], 0x1E, eaddr->ether_addr_octet[2]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[3], 0x4A, eaddr->ether_addr_octet[3]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[4], 0x41, eaddr->ether_addr_octet[4]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[5], 0x55, eaddr->ether_addr_octet[5]);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetResolvTest002(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, EtherAtonTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
58
testsuites/unittest/net/resolv/full/net_resolv_test_003.cpp
Normal file
58
testsuites/unittest/net/resolv/full/net_resolv_test_003.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <lt_net_resolv.h>
|
||||
|
||||
static int EtherAtonrTest(void)
|
||||
{
|
||||
struct ether_addr addr;
|
||||
struct ether_addr *eaddr = ether_aton_r("::01:EF", &addr);
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(eaddr, NULL, -1);
|
||||
|
||||
eaddr = ether_aton_r("2c:9d:1e:4A:41:55", &addr);
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(eaddr, &addr, -1);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[0], 0x2c, eaddr->ether_addr_octet[0]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[1], 0x9d, eaddr->ether_addr_octet[1]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[2], 0x1e, eaddr->ether_addr_octet[2]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[3], 0x4a, eaddr->ether_addr_octet[3]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[4], 0x41, eaddr->ether_addr_octet[4]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[5], 0x55, eaddr->ether_addr_octet[5]);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetResolvTest003(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, EtherAtonrTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
54
testsuites/unittest/net/resolv/full/net_resolv_test_004.cpp
Normal file
54
testsuites/unittest/net/resolv/full/net_resolv_test_004.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <lt_net_resolv.h>
|
||||
|
||||
static int EtherHosttonTest(void)
|
||||
{
|
||||
// suppose "0:0:0:0:0:0 localhost" in `/etc/ethers' file.
|
||||
struct ether_addr addr, *eaddr = &addr;
|
||||
int ret = ether_hostton("localhost", eaddr);
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[0], 0x00, eaddr->ether_addr_octet[0]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[1], 0x00, eaddr->ether_addr_octet[1]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[2], 0x00, eaddr->ether_addr_octet[2]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[3], 0x00, eaddr->ether_addr_octet[3]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[4], 0x00, eaddr->ether_addr_octet[4]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[5], 0x00, eaddr->ether_addr_octet[5]);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetResolvTest004(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, EtherHosttonTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
61
testsuites/unittest/net/resolv/full/net_resolv_test_005.cpp
Normal file
61
testsuites/unittest/net/resolv/full/net_resolv_test_005.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <lt_net_resolv.h>
|
||||
|
||||
static int EtherLineTest(void)
|
||||
{
|
||||
struct ether_addr addr, *eaddr = &addr;
|
||||
char buf[100];
|
||||
int ret;
|
||||
|
||||
ret = ether_line("localhost 01:02:03:04:05:06", eaddr, buf);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, -1);
|
||||
|
||||
ret = ether_line("01:02:03:04:05:06 localhost", eaddr, buf);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, -1);
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(strcmp("localhost", buf), 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[0], 0x01, eaddr->ether_addr_octet[0]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[1], 0x02, eaddr->ether_addr_octet[1]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[2], 0x03, eaddr->ether_addr_octet[2]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[3], 0x04, eaddr->ether_addr_octet[3]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[4], 0x05, eaddr->ether_addr_octet[4]);
|
||||
ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[5], 0x06, eaddr->ether_addr_octet[5]);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetResolvTest005(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, EtherLineTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
49
testsuites/unittest/net/resolv/full/net_resolv_test_006.cpp
Normal file
49
testsuites/unittest/net/resolv/full/net_resolv_test_006.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <lt_net_resolv.h>
|
||||
|
||||
static int EtherNtoaTest(void)
|
||||
{
|
||||
struct ether_addr addr = {{11,12,13,14,15,16}}, *eaddr = &addr;
|
||||
char *buf = ether_ntoa(eaddr);
|
||||
|
||||
ICUNIT_ASSERT_NOT_EQUAL(buf, 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL((stricmp("b:c:d:e:f:10", buf) == 0 || stricmp("0b:0c:0d:0e:0f:10", buf) == 0), 1, printf("%s\n", buf));
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetResolvTest006(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, EtherNtoaTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
48
testsuites/unittest/net/resolv/full/net_resolv_test_007.cpp
Normal file
48
testsuites/unittest/net/resolv/full/net_resolv_test_007.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <lt_net_resolv.h>
|
||||
|
||||
static int EtherNtoarTest(void)
|
||||
{
|
||||
struct ether_addr addr = {{11,12,13,4,15,16}}, *eaddr = &addr;
|
||||
char buf[100], *p = ether_ntoa_r(eaddr, buf);
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(p, buf, (intptr_t)p);
|
||||
ICUNIT_ASSERT_EQUAL((stricmp("b:c:d:4:f:10", buf) == 0 || stricmp("0b:0c:0d:04:0f:10", buf) == 0), 1, printf("%s\n", p));
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetResolvTest007(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, EtherNtoarTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
51
testsuites/unittest/net/resolv/full/net_resolv_test_008.cpp
Normal file
51
testsuites/unittest/net/resolv/full/net_resolv_test_008.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <lt_net_resolv.h>
|
||||
|
||||
static int EtherNtohostTest(void)
|
||||
{
|
||||
// suppose "0:0:0:0:0:0 localhost" in `/etc/ethers' file.
|
||||
struct ether_addr addr = {{0,0,0,0,0,0}}, *eaddr = &addr;
|
||||
char buf[100];
|
||||
int ret = ether_ntohost(buf, eaddr);
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp("localhost", buf), 0, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetResolvTest008(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, EtherNtohostTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
51
testsuites/unittest/net/resolv/lt_net_resolv.h
Normal file
51
testsuites/unittest/net/resolv/lt_net_resolv.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef NET_RESOLV_LT_NET_RESOLV_H_
|
||||
#define NET_RESOLV_LT_NET_RESOLV_H_
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
#include <resolv.h>
|
||||
#include <netinet/ether.h>
|
||||
|
||||
int stricmp(const char *s1, const char *s2);
|
||||
|
||||
void NetResolvTest001(void);
|
||||
void NetResolvTest002(void);
|
||||
void NetResolvTest003(void);
|
||||
void NetResolvTest004(void);
|
||||
void NetResolvTest005(void);
|
||||
void NetResolvTest006(void);
|
||||
void NetResolvTest007(void);
|
||||
void NetResolvTest008(void);
|
||||
|
||||
#endif /* NET_RESOLV_LT_NET_RESOLV_H_ */
|
||||
112
testsuites/unittest/net/resolv/net_resolv_test.cpp
Normal file
112
testsuites/unittest/net/resolv/net_resolv_test.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "stdio.h"
|
||||
#include <climits>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "lt_net_resolv.h"
|
||||
|
||||
int stricmp(const char *s1, const char *s2)
|
||||
{
|
||||
for (; *s1 && *s2; s1++, s2++) {
|
||||
if (*s1 == *s2) continue;
|
||||
if ((*s1 ^ *s2) == 0x20 && (*s2 | 0x20) >= 'a' && (*s2 | 0x20) <= 'z') continue;
|
||||
break;
|
||||
}
|
||||
return *s1 - *s2;
|
||||
}
|
||||
|
||||
using namespace testing::ext;
|
||||
namespace OHOS {
|
||||
class NetResolvTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase(void) {}
|
||||
static void TearDownTestCase(void) {}
|
||||
};
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_FULL) && defined(LOSCFG_USER_TEST_NET_RESOLV)
|
||||
/* *
|
||||
* @tc.name: NetResolvTest001
|
||||
* @tc.desc: function for NetResolvTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetResolvTest, NetResolvTest001, TestSize.Level0)
|
||||
{
|
||||
NetResolvTest001();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetResolvTest002
|
||||
* @tc.desc: function for NetResolvTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetResolvTest, NetResolvTest002, TestSize.Level0)
|
||||
{
|
||||
NetResolvTest002();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetResolvTest003
|
||||
* @tc.desc: function for NetResolvTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetResolvTest, NetResolvTest003, TestSize.Level0)
|
||||
{
|
||||
NetResolvTest003();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetResolvTest006
|
||||
* @tc.desc: function for NetResolvTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetResolvTest, NetResolvTest006, TestSize.Level0)
|
||||
{
|
||||
NetResolvTest006();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetResolvTest007
|
||||
* @tc.desc: function for NetResolvTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetResolvTest, NetResolvTest007, TestSize.Level0)
|
||||
{
|
||||
NetResolvTest007();
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
69
testsuites/unittest/net/socket/BUILD.gn
Normal file
69
testsuites/unittest/net/socket/BUILD.gn
Normal file
@@ -0,0 +1,69 @@
|
||||
# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other materials
|
||||
# provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
# to endorse or promote products derived from this software without specific prior written
|
||||
# permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import("//build/lite/config/test.gni")
|
||||
import("../../config.gni")
|
||||
|
||||
config("net_local_config") {
|
||||
if (LOSCFG_USER_TEST_NET_SOCKET == true) {
|
||||
cflags = [ "-DLOSCFG_USER_TEST_NET_SOCKET" ]
|
||||
cflags_cc = cflags
|
||||
}
|
||||
}
|
||||
|
||||
unittest("liteos_a_net_socket_unittest") {
|
||||
output_extension = "bin"
|
||||
output_dir = "$root_out_dir/test/unittest/kernel"
|
||||
include_dirs = [
|
||||
"../../common/include",
|
||||
"../../net/socket",
|
||||
]
|
||||
sources = [
|
||||
"../../common/osTest.cpp",
|
||||
"net_socket_test.cpp",
|
||||
"smoke/net_socket_test_001.cpp",
|
||||
"smoke/net_socket_test_002.cpp",
|
||||
"smoke/net_socket_test_003.cpp",
|
||||
"smoke/net_socket_test_004.cpp",
|
||||
"smoke/net_socket_test_005.cpp",
|
||||
"smoke/net_socket_test_006.cpp",
|
||||
"smoke/net_socket_test_007.cpp",
|
||||
"smoke/net_socket_test_008.cpp",
|
||||
"smoke/net_socket_test_009.cpp",
|
||||
"smoke/net_socket_test_010.cpp",
|
||||
"smoke/net_socket_test_011.cpp",
|
||||
"smoke/net_socket_test_012.cpp",
|
||||
"smoke/net_socket_test_013.cpp",
|
||||
]
|
||||
deps = [ "//third_party/bounds_checking_function:libsec_shared" ]
|
||||
configs = [
|
||||
"../../:public_config",
|
||||
":net_local_config",
|
||||
]
|
||||
}
|
||||
52
testsuites/unittest/net/socket/lt_net_socket.h
Normal file
52
testsuites/unittest/net/socket/lt_net_socket.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef NET_SOCKET_LT_NET_SOCKET_H_
|
||||
#define NET_SOCKET_LT_NET_SOCKET_H_
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
void NetSocketTest001(void);
|
||||
void NetSocketTest002(void);
|
||||
void NetSocketTest003(void);
|
||||
void NetSocketTest004(void);
|
||||
void NetSocketTest005(void);
|
||||
void NetSocketTest006(void);
|
||||
void NetSocketTest007(void);
|
||||
void NetSocketTest008(void);
|
||||
void NetSocketTest009(void);
|
||||
void NetSocketTest010(void);
|
||||
void NetSocketTest011(void);
|
||||
void NetSocketTest012(void);
|
||||
void NetSocketTest013(void);
|
||||
|
||||
#endif /* NET_SOCKET_LT_NET_SOCKET_H_ */
|
||||
204
testsuites/unittest/net/socket/net_socket_test.cpp
Normal file
204
testsuites/unittest/net/socket/net_socket_test.cpp
Normal file
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "stdio.h"
|
||||
#include <climits>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "lt_net_socket.h"
|
||||
|
||||
using namespace testing::ext;
|
||||
namespace OHOS {
|
||||
class NetSocketTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase(void)
|
||||
{
|
||||
struct sched_param param = { 0 };
|
||||
int currThreadPolicy, ret;
|
||||
ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, ¶m);
|
||||
ICUNIT_ASSERT_EQUAL_VOID(ret, 0, -ret);
|
||||
param.sched_priority = TASK_PRIO_TEST;
|
||||
ret = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
|
||||
ICUNIT_ASSERT_EQUAL_VOID(ret, 0, -ret);
|
||||
}
|
||||
static void TearDownTestCase(void) {}
|
||||
};
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_SMOKE) && defined(LOSCFG_USER_TEST_NET_SOCKET)
|
||||
/* *
|
||||
* @tc.name: NetSocketTest001
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest001, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest001();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest002
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest002, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest002();
|
||||
}
|
||||
|
||||
#if TEST_ON_LINUX
|
||||
/* *
|
||||
* @tc.name: NetSocketTest003
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest003, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest003(); // getifaddrs need PF_NETLINK which was not supported by lwip currently
|
||||
}
|
||||
#endif
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest004
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest004, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest004();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest005
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest005, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest005();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest006
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest006, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest006();
|
||||
}
|
||||
|
||||
#if TEST_ON_LINUX
|
||||
/* *
|
||||
* @tc.name: NetSocketTest007
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest007, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest007();
|
||||
}
|
||||
#endif
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest008
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest008, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest008();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest009
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest009, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest009();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest010
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest010, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest010();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest011
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest011, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest011();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest012
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
HWTEST_F(NetSocketTest, NetSocketTest012, TestSize.Level0)
|
||||
{
|
||||
NetSocketTest012();
|
||||
}
|
||||
|
||||
/* *
|
||||
* @tc.name: NetSocketTest013
|
||||
* @tc.desc: function for NetSocketTest
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000EEMQ9
|
||||
*/
|
||||
/*
|
||||
HWTEST_F(NetSocketTest, NetSocketTest013, TestSize.Level0)
|
||||
{
|
||||
//NetSocketTest013(); // broadcast to self to be supported.
|
||||
}
|
||||
*/
|
||||
#endif
|
||||
}
|
||||
45
testsuites/unittest/net/socket/smoke/net_socket_test_001.cpp
Normal file
45
testsuites/unittest/net/socket/smoke/net_socket_test_001.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <osTest.h>
|
||||
|
||||
static int SocketTest(void)
|
||||
{
|
||||
int fd = socket(0, 0, 0);
|
||||
ICUNIT_ASSERT_EQUAL(fd, -1, fd);
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest001(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, SocketTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
131
testsuites/unittest/net/socket/smoke/net_socket_test_002.cpp
Normal file
131
testsuites/unittest/net/socket/smoke/net_socket_test_002.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <osTest.h>
|
||||
|
||||
#define localhost "127.0.0.1"
|
||||
#define STACK_IP localhost
|
||||
#define STACK_PORT 2277
|
||||
#define PEER_PORT STACK_PORT
|
||||
#define PEER_IP localhost
|
||||
#define MSG "Hi, I am UDP"
|
||||
#define BUF_SIZE (1024 * 8)
|
||||
|
||||
static char g_buf[BUF_SIZE + 1] = { 0 };
|
||||
|
||||
static int UdpTest(void)
|
||||
{
|
||||
int sfd;
|
||||
struct sockaddr_in srvAddr = { 0 };
|
||||
struct sockaddr_in clnAddr = { 0 };
|
||||
socklen_t clnAddrLen = sizeof(clnAddr);
|
||||
int ret = 0, i = 0;
|
||||
struct msghdr msg = { 0 };
|
||||
struct iovec iov[2] = { };
|
||||
|
||||
/* socket creation */
|
||||
sfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, sfd);
|
||||
|
||||
srvAddr.sin_family = AF_INET;
|
||||
srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
|
||||
srvAddr.sin_port = htons(STACK_PORT);
|
||||
ret = bind(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
/* send */
|
||||
clnAddr.sin_family = AF_INET;
|
||||
clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
|
||||
clnAddr.sin_port = htons(PEER_PORT);
|
||||
ret = memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ret = strcpy_s(g_buf, BUF_SIZE, MSG);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ret = sendto(sfd, g_buf, strlen(MSG), 0, (struct sockaddr*)&clnAddr,
|
||||
(socklen_t)sizeof(clnAddr));
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
|
||||
|
||||
/* recv */
|
||||
ret = memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ret = recvfrom(sfd, g_buf, sizeof(g_buf), 0, (struct sockaddr*)&clnAddr,
|
||||
&clnAddrLen);
|
||||
ICUNIT_ASSERT_EQUAL(ret, strlen(MSG), ret);
|
||||
|
||||
/* sendmsg */
|
||||
clnAddr.sin_family = AF_INET;
|
||||
clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
|
||||
clnAddr.sin_port = htons(PEER_PORT);
|
||||
ret = memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ret = strcpy_s(g_buf, BUF_SIZE, MSG);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
msg.msg_name = &clnAddr;
|
||||
msg.msg_namelen = sizeof(clnAddr);
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 2;
|
||||
iov[0].iov_base = g_buf;
|
||||
iov[0].iov_len = strlen(MSG);
|
||||
iov[1].iov_base = g_buf;
|
||||
iov[1].iov_len = strlen(MSG);
|
||||
ret = sendmsg(sfd, &msg, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(MSG), ret);
|
||||
|
||||
/* recvmsg */
|
||||
ret = memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ret = memset_s(&msg, sizeof(msg), 0, sizeof(msg));
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
msg.msg_name = &clnAddr;
|
||||
msg.msg_namelen = sizeof(clnAddr);
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 1;
|
||||
iov[0].iov_base = g_buf;
|
||||
iov[0].iov_len = sizeof(g_buf);
|
||||
ret = recvmsg(sfd, &msg, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(MSG), ret);
|
||||
|
||||
/* close socket */
|
||||
ret = close(sfd);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void NetSocketTest002(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, UdpTest, TEST_POSIX, TEST_UDP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
321
testsuites/unittest/net/socket/smoke/net_socket_test_003.cpp
Normal file
321
testsuites/unittest/net/socket/smoke/net_socket_test_003.cpp
Normal file
@@ -0,0 +1,321 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <osTest.h>
|
||||
|
||||
#define localhost "127.0.0.1"
|
||||
#define STACK_IP localhost
|
||||
#define STACK_PORT 2277
|
||||
#define PEER_PORT STACK_PORT
|
||||
#define PEER_IP localhost
|
||||
#define BUF_SIZE (1024 * 8)
|
||||
#define SRV_MSG "Hi, I am TCP server"
|
||||
#define CLI_MSG "Hi, I am TCP client"
|
||||
|
||||
static pthread_barrier_t gBarrier;
|
||||
#define Wait() pthread_barrier_wait(&gBarrier)
|
||||
|
||||
static int SampleTcpServer()
|
||||
{
|
||||
static char gBuf[BUF_SIZE + 1] = { 0 };
|
||||
int sfd = -1, lsfd = -1;
|
||||
struct sockaddr_in srvAddr = { 0 };
|
||||
struct sockaddr_in clnAddr = { 0 };
|
||||
socklen_t clnAddrLen = sizeof(clnAddr);
|
||||
struct msghdr msg = { 0 };
|
||||
struct iovec iov[2] = { };
|
||||
int ret = 0, i = 0;
|
||||
|
||||
/* tcp server */
|
||||
lsfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
LogPrintln("create listen socket inet stream: %d", lsfd);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, lsfd);
|
||||
|
||||
srvAddr.sin_family = AF_INET;
|
||||
srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
|
||||
srvAddr.sin_port = htons(STACK_PORT);
|
||||
ret = bind(lsfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
|
||||
LogPrintln("bind socket %d to %s:%d: %d", lsfd, inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, Wait() + ret);
|
||||
|
||||
ret = listen(lsfd, 0);
|
||||
LogPrintln("listen socket %d: %d", lsfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
Wait();
|
||||
|
||||
sfd = accept(lsfd, (struct sockaddr*)&clnAddr, &clnAddrLen);
|
||||
LogPrintln("accept socket %d: %d <%s:%d>", lsfd, sfd, inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port));
|
||||
ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, sfd);
|
||||
|
||||
/* send */
|
||||
ret = memset_s(gBuf, BUF_SIZE - 1, 0, BUF_SIZE - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = strcpy_s(gBuf, BUF_SIZE - 1, SRV_MSG);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = send(sfd, gBuf, strlen(SRV_MSG), 0);
|
||||
LogPrintln("send on socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), ret);
|
||||
|
||||
/* recv */
|
||||
ret = memset_s(gBuf, BUF_SIZE - 1, 0, BUF_SIZE - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = recv(sfd, gBuf, sizeof(gBuf), 0);
|
||||
LogPrintln("recv on socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, strlen(CLI_MSG), ret);
|
||||
|
||||
Wait();
|
||||
|
||||
/* sendmsg */
|
||||
clnAddr.sin_family = AF_INET;
|
||||
clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
|
||||
clnAddr.sin_port = htons(PEER_PORT);
|
||||
ret = memset_s(gBuf, BUF_SIZE - 1, 0, BUF_SIZE - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = strcpy_s(gBuf, BUF_SIZE - 1, SRV_MSG);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
msg.msg_name = &clnAddr;
|
||||
msg.msg_namelen = sizeof(clnAddr);
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 2;
|
||||
iov[0].iov_base = gBuf;
|
||||
iov[0].iov_len = strlen(SRV_MSG);
|
||||
iov[1].iov_base = gBuf;
|
||||
iov[1].iov_len = strlen(SRV_MSG);
|
||||
ret = sendmsg(sfd, &msg, 0);
|
||||
LogPrintln("sendmsg on socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(SRV_MSG), ret);
|
||||
|
||||
Wait();
|
||||
|
||||
/* recvmsg */
|
||||
ret = memset_s(gBuf, BUF_SIZE - 1, 0, BUF_SIZE - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = memset_s(&msg, sizeof(msg), 0, sizeof(msg));
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
msg.msg_name = &clnAddr;
|
||||
msg.msg_namelen = sizeof(clnAddr);
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 1;
|
||||
iov[0].iov_base = gBuf;
|
||||
iov[0].iov_len = sizeof(gBuf);
|
||||
ret = recvmsg(sfd, &msg, 0);
|
||||
LogPrintln("recvmsg on socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(CLI_MSG), ret);
|
||||
|
||||
ret = shutdown(sfd, SHUT_RDWR);
|
||||
LogPrintln("shutdown socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
close(sfd);
|
||||
close(lsfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int SampleTcpClient()
|
||||
{
|
||||
static char gBuf[BUF_SIZE + 1] = { 0 };
|
||||
int sfd = -1;
|
||||
struct sockaddr_in srvAddr = { 0 };
|
||||
struct sockaddr_in clnAddr = { 0 };
|
||||
socklen_t clnAddrLen = sizeof(clnAddr);
|
||||
int ret = 0, i = 0;
|
||||
struct msghdr msg = { 0 };
|
||||
struct iovec iov[2] = { };
|
||||
struct sockaddr addr;
|
||||
socklen_t addrLen = sizeof(addr);
|
||||
|
||||
/* tcp client connection */
|
||||
sfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
LogPrintln("create client socket inet stream: %d", sfd);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, sfd);
|
||||
|
||||
Wait();
|
||||
|
||||
srvAddr.sin_family = AF_INET;
|
||||
srvAddr.sin_addr.s_addr = inet_addr(PEER_IP);
|
||||
srvAddr.sin_port = htons(PEER_PORT);
|
||||
ret = connect(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
|
||||
LogPrintln("connect socket %d to %s:%d: %d", sfd, inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
/* test getpeername */
|
||||
ret = getpeername(sfd, &addr, &addrLen);
|
||||
LogPrintln("getpeername %d %s:%d: %d", sfd, inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr), ntohs(((struct sockaddr_in*)&addr)->sin_port), ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_EQUAL(addrLen, sizeof(struct sockaddr_in), addrLen);
|
||||
ICUNIT_ASSERT_EQUAL(((struct sockaddr_in*)&addr)->sin_addr.s_addr,
|
||||
inet_addr(PEER_IP), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
|
||||
|
||||
/* test getsockname */
|
||||
ret = getsockname(sfd, &addr, &addrLen);
|
||||
LogPrintln("getsockname %d %s:%d: %d", sfd, inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr), ntohs(((struct sockaddr_in*)&addr)->sin_port), ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_EQUAL(addrLen, sizeof(struct sockaddr_in), addrLen);
|
||||
ICUNIT_ASSERT_EQUAL(((struct sockaddr_in*)&addr)->sin_addr.s_addr,
|
||||
inet_addr(STACK_IP), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
|
||||
|
||||
/* send */
|
||||
ret = memset_s(gBuf, BUF_SIZE - 1, 0, BUF_SIZE - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = strcpy_s(gBuf, BUF_SIZE - 1, CLI_MSG);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = send(sfd, gBuf, strlen(CLI_MSG), 0);
|
||||
LogPrintln("send on socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, strlen(CLI_MSG), ret);
|
||||
|
||||
/* recv */
|
||||
ret = memset_s(gBuf, BUF_SIZE - 1, 0, BUF_SIZE - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = recv(sfd, gBuf, sizeof(gBuf), 0);
|
||||
LogPrintln("recv on socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), ret);
|
||||
|
||||
Wait();
|
||||
|
||||
/* sendmsg */
|
||||
clnAddr.sin_family = AF_INET;
|
||||
clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
|
||||
clnAddr.sin_port = htons(PEER_PORT);
|
||||
ret = memset_s(gBuf, BUF_SIZE - 1, 0, BUF_SIZE - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = strcpy_s(gBuf, BUF_SIZE - 1, CLI_MSG);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
msg.msg_name = &clnAddr;
|
||||
msg.msg_namelen = sizeof(clnAddr);
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 2;
|
||||
iov[0].iov_base = gBuf;
|
||||
iov[0].iov_len = strlen(CLI_MSG);
|
||||
iov[1].iov_base = gBuf;
|
||||
iov[1].iov_len = strlen(CLI_MSG);
|
||||
ret = sendmsg(sfd, &msg, 0);
|
||||
LogPrintln("sendmsg on socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(CLI_MSG), ret);
|
||||
|
||||
Wait();
|
||||
|
||||
/* recvmsg */
|
||||
ret = memset_s(gBuf, BUF_SIZE - 1, 0, BUF_SIZE - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gBuf[BUF_SIZE - 1] = '\0';
|
||||
ret = memset_s(&msg, sizeof(msg), 0, sizeof(msg));
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
msg.msg_name = &clnAddr;
|
||||
msg.msg_namelen = sizeof(clnAddr);
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 1;
|
||||
iov[0].iov_base = gBuf;
|
||||
iov[0].iov_len = sizeof(gBuf);
|
||||
ret = recvmsg(sfd, &msg, 0);
|
||||
LogPrintln("recvmsg on socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(SRV_MSG), ret);
|
||||
|
||||
ret = shutdown(sfd, SHUT_RDWR);
|
||||
LogPrintln("shutdown socket %d: %d", sfd, ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
close(sfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void* TcpServerRoutine(void *p)
|
||||
{
|
||||
int ret = SampleTcpServer();
|
||||
return (void*)(intptr_t)ret;
|
||||
}
|
||||
|
||||
static void* TcpClientRoutine(void *p)
|
||||
{
|
||||
int ret = SampleTcpClient();
|
||||
return (void*)(intptr_t)ret;
|
||||
}
|
||||
|
||||
static int TcpTest()
|
||||
{
|
||||
int ret;
|
||||
void *sret = NULL;
|
||||
void *cret = NULL;
|
||||
pthread_t srv, cli;
|
||||
pthread_attr_t attr;
|
||||
|
||||
ret = pthread_barrier_init(&gBarrier, 0, 2);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_attr_init(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_create(&srv, &attr, TcpServerRoutine, NULL);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_create(&cli, &attr, TcpClientRoutine, NULL);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_join(cli, &cret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
LogPrintln("client finish");
|
||||
|
||||
ret = pthread_join(srv, &sret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
LogPrintln("server finish");
|
||||
|
||||
ret = pthread_attr_destroy(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_barrier_destroy(&gBarrier);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
return (int)(intptr_t)(sret) + (int)(intptr_t)(cret);
|
||||
}
|
||||
|
||||
void NetSocketTest003(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, TcpTest, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
89
testsuites/unittest/net/socket/smoke/net_socket_test_004.cpp
Normal file
89
testsuites/unittest/net/socket/smoke/net_socket_test_004.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <osTest.h>
|
||||
|
||||
static int SockOptTest(void)
|
||||
{
|
||||
int ret, error, flag;
|
||||
struct timeval timeout;
|
||||
socklen_t len;
|
||||
|
||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd);
|
||||
|
||||
error = -1;
|
||||
len = sizeof(error);
|
||||
ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
|
||||
LogPrintln("getsockopt(%d, SOL_SOCKET, SO_ERROR, &error, &len)=%d, error=%d, len=%d, errno=%d", fd, ret, error, len, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_EQUAL(error, 0, error);
|
||||
|
||||
len = sizeof(timeout);
|
||||
ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
timeout.tv_sec = 1000;
|
||||
len = sizeof(timeout);
|
||||
ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, len);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = memset_s(&timeout, len, 0, len);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_EQUAL(timeout.tv_sec, 1000, ret);
|
||||
|
||||
error = -1;
|
||||
len = sizeof(error);
|
||||
ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
|
||||
LogPrintln("getsockopt(%d, SOL_SOCKET, SO_ERROR, &error, &len)=%d, error=%d, len=%d, errno=%d", fd, ret, error, len, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ICUNIT_ASSERT_EQUAL(error, 0, error);
|
||||
|
||||
flag=1;
|
||||
ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
|
||||
LogPrintln("setsockopt(TCP_NODELAY) ret=%d", ret);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = close(fd);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest004(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, SockOptTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
69
testsuites/unittest/net/socket/smoke/net_socket_test_005.cpp
Normal file
69
testsuites/unittest/net/socket/smoke/net_socket_test_005.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <osTest.h>
|
||||
|
||||
static int ByteOrderTest(void)
|
||||
{
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
uint32_t hl = ntohl(0x12345678);
|
||||
ICUNIT_ASSERT_EQUAL(hl, 0x78563412, hl);
|
||||
|
||||
uint32_t nl = htonl(0x12345678);
|
||||
ICUNIT_ASSERT_EQUAL(nl, 0x78563412, nl);
|
||||
|
||||
uint16_t hs = ntohs(0x1234);
|
||||
ICUNIT_ASSERT_EQUAL(hs, 0x3412, hs);
|
||||
|
||||
uint16_t ns = htons(0x1234);
|
||||
ICUNIT_ASSERT_EQUAL(ns, 0x3412, ns);
|
||||
#else
|
||||
uint32_t hl = ntohl(0x12345678);
|
||||
ICUNIT_ASSERT_EQUAL(hl, 0x12345678, hl);
|
||||
|
||||
uint32_t nl = htonl(0x12345678);
|
||||
ICUNIT_ASSERT_EQUAL(nl, 0x12345678, nl);
|
||||
|
||||
uint16_t hs = ntohs(0x1234);
|
||||
ICUNIT_ASSERT_EQUAL(hs, 0x1234, hs);
|
||||
|
||||
uint16_t ns = htons(0x1234);
|
||||
ICUNIT_ASSERT_EQUAL(ns, 0x1234, ns);
|
||||
#endif
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest005(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, ByteOrderTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
87
testsuites/unittest/net/socket/smoke/net_socket_test_006.cpp
Normal file
87
testsuites/unittest/net/socket/smoke/net_socket_test_006.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
static int InetTest(void)
|
||||
{
|
||||
struct in_addr in;
|
||||
int ret = inet_pton(AF_INET, "300.10.10.10", &in);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = inet_pton(AF_INET, "10.11.12.13", &in);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 1, ret);
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
ICUNIT_ASSERT_EQUAL(in.s_addr, 0x0d0c0b0a, in.s_addr);
|
||||
#else
|
||||
ICUNIT_ASSERT_EQUAL(in.s_addr, 0x0a0b0c0d, in.s_addr);
|
||||
#endif
|
||||
|
||||
// host order
|
||||
in_addr_t lna = inet_lnaof(in);
|
||||
ICUNIT_ASSERT_EQUAL(lna, 0x000b0c0d, lna);
|
||||
|
||||
// host order
|
||||
in_addr_t net = inet_netof(in);
|
||||
ICUNIT_ASSERT_EQUAL(net, 0x0000000a, net);
|
||||
|
||||
in = inet_makeaddr(net, lna);
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
ICUNIT_ASSERT_EQUAL(in.s_addr, 0x0d0c0b0a, in.s_addr);
|
||||
#else
|
||||
ICUNIT_ASSERT_EQUAL(in.s_addr, 0x0a0b0c0d, in.s_addr);
|
||||
#endif
|
||||
|
||||
net = inet_network("300.10.10.10");
|
||||
ICUNIT_ASSERT_EQUAL(net, -1, net);
|
||||
|
||||
// host order
|
||||
net = inet_network("10.11.12.13");
|
||||
ICUNIT_ASSERT_EQUAL(net, 0x0a0b0c0d, net);
|
||||
|
||||
const char *p = inet_ntoa(in);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(p, "10.11.12.13"), 0, -1);
|
||||
|
||||
char buf[32];
|
||||
p = inet_ntop(AF_INET, &in, buf, sizeof(buf));
|
||||
ICUNIT_ASSERT_EQUAL(p, buf, -1);
|
||||
ICUNIT_ASSERT_EQUAL(strcmp(p, "10.11.12.13"), 0, -1);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest006(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, InetTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
156
testsuites/unittest/net/socket/smoke/net_socket_test_007.cpp
Normal file
156
testsuites/unittest/net/socket/smoke/net_socket_test_007.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <osTest.h>
|
||||
|
||||
#define SRV_MSG "Hi, I am TCP server"
|
||||
#define INVALID_USER_ADDR 0X1200000
|
||||
#define INVALID_KERNEL_ADDR 0x48000000
|
||||
|
||||
static int TcpTest()
|
||||
{
|
||||
int ret;
|
||||
int lsfd = -1;
|
||||
struct msghdr msg = { 0 };
|
||||
|
||||
lsfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, lsfd);
|
||||
|
||||
ret = bind(lsfd, (const struct sockaddr *)INVALID_USER_ADDR, sizeof(struct sockaddr_in));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = bind(lsfd, (const struct sockaddr *)INVALID_KERNEL_ADDR, sizeof(struct sockaddr_in));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = connect(lsfd, (struct sockaddr*)INVALID_USER_ADDR, sizeof(struct sockaddr_in));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = connect(lsfd, (struct sockaddr*)INVALID_KERNEL_ADDR, sizeof(struct sockaddr_in));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = accept(lsfd, (struct sockaddr*)INVALID_USER_ADDR, (socklen_t *)INVALID_USER_ADDR);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = accept(lsfd, (struct sockaddr*)INVALID_KERNEL_ADDR, (socklen_t *)INVALID_KERNEL_ADDR);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = getsockname(lsfd, (struct sockaddr*)INVALID_USER_ADDR, (socklen_t *)INVALID_USER_ADDR);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = getsockname(lsfd, (struct sockaddr*)INVALID_KERNEL_ADDR, (socklen_t *)INVALID_KERNEL_ADDR);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = getpeername(lsfd, (struct sockaddr*)INVALID_USER_ADDR, (socklen_t *)INVALID_USER_ADDR);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = getpeername(lsfd, (struct sockaddr*)INVALID_KERNEL_ADDR, (socklen_t *)INVALID_KERNEL_ADDR);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = send(lsfd, (char *)INVALID_USER_ADDR, strlen(SRV_MSG), 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = send(lsfd, (char *)INVALID_KERNEL_ADDR, strlen(SRV_MSG), 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = sendto(lsfd, (char *)INVALID_USER_ADDR, strlen(SRV_MSG), 0, (struct sockaddr*)INVALID_USER_ADDR,
|
||||
(socklen_t)sizeof(struct sockaddr_in));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = sendto(lsfd, (char *)INVALID_KERNEL_ADDR, strlen(SRV_MSG), 0, (struct sockaddr*)INVALID_KERNEL_ADDR,
|
||||
(socklen_t)sizeof(struct sockaddr_in));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = recv(lsfd, (char *)INVALID_USER_ADDR, sizeof(SRV_MSG), 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = recv(lsfd, (char *)INVALID_KERNEL_ADDR, sizeof(SRV_MSG), 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = recvfrom(lsfd, (char *)INVALID_USER_ADDR, sizeof(SRV_MSG), 0, (struct sockaddr*)INVALID_USER_ADDR,
|
||||
(socklen_t *)INVALID_USER_ADDR);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = recvfrom(lsfd, (char *)INVALID_KERNEL_ADDR, sizeof(SRV_MSG), 0, (struct sockaddr*)INVALID_KERNEL_ADDR,
|
||||
(socklen_t *)INVALID_KERNEL_ADDR);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = setsockopt(lsfd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)INVALID_USER_ADDR, (socklen_t)sizeof(struct timeval));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = setsockopt(lsfd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)INVALID_KERNEL_ADDR, (socklen_t)sizeof(struct timeval));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = getsockopt(lsfd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)INVALID_USER_ADDR, (socklen_t*)sizeof(struct timeval));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = getsockopt(lsfd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)INVALID_KERNEL_ADDR, (socklen_t*)sizeof(struct timeval));
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = sendmsg(lsfd, (struct msghdr *)INVALID_USER_ADDR, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = sendmsg(lsfd, (struct msghdr *)INVALID_KERNEL_ADDR, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
msg.msg_name = (char *)INVALID_USER_ADDR;
|
||||
msg.msg_namelen = sizeof(struct sockaddr_in);
|
||||
msg.msg_iov = (struct iovec *)INVALID_KERNEL_ADDR;
|
||||
msg.msg_iovlen = 2;
|
||||
ret = recvmsg(lsfd, (struct msghdr *)&msg, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
ret = recvmsg(lsfd, (struct msghdr *)&msg, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
|
||||
#ifdef TEST_BIG_MEM
|
||||
const int bufSiz = 0x1400000; // 20M
|
||||
void *buf = malloc(bufSiz);
|
||||
if (!buf) {
|
||||
printf("malloc 20M fail\n");
|
||||
} else {
|
||||
printf("malloc 20M success\n");
|
||||
ret = memset_s(buf, bufSiz, 0, bufSiz);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
ret = send(lsfd, buf, bufSiz, 0);
|
||||
printf("send ret = %d, errno :%d\n", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
close(lsfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void NetSocketTest007(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, TcpTest, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
282
testsuites/unittest/net/socket/smoke/net_socket_test_008.cpp
Normal file
282
testsuites/unittest/net/socket/smoke/net_socket_test_008.cpp
Normal file
@@ -0,0 +1,282 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define SERVER_PORT 6666
|
||||
#define INVALID_SOCKET -1
|
||||
#define CLIENT_NUM 50
|
||||
#define BACKLOG CLIENT_NUM
|
||||
|
||||
static int gFds[FD_SETSIZE];
|
||||
static int gBye;
|
||||
|
||||
static void InitFds()
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
|
||||
static void GetReadfds(fd_set *fds, int *nfd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; i++) {
|
||||
if (gFds[i] == INVALID_SOCKET) {
|
||||
continue;
|
||||
}
|
||||
FD_SET(gFds[i], fds);
|
||||
if (*nfd < gFds[i]) {
|
||||
*nfd = gFds[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int AddFd(int fd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == INVALID_SOCKET) {
|
||||
gFds[i] = fd;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void DelFd(int fd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == fd) {
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
(void)close(fd);
|
||||
}
|
||||
|
||||
static int CloseAllFd(void)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] != INVALID_SOCKET) {
|
||||
(void)close(gFds[i]);
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int HandleRecv(int fd)
|
||||
{
|
||||
char buf[256];
|
||||
int ret = recv(fd, buf, sizeof(buf)-1, 0);
|
||||
if (ret < 0) {
|
||||
LogPrintln("[%d]Error: %s", fd, strerror(errno));
|
||||
DelFd(fd);
|
||||
} else if (ret == 0) {
|
||||
LogPrintln("[%d]Closed", fd);
|
||||
DelFd(fd);
|
||||
} else {
|
||||
buf[ret] = 0;
|
||||
LogPrintln("[%d]Received: %s", fd, buf);
|
||||
if (strstr(buf, "Bye") != NULL) {
|
||||
DelFd(fd);
|
||||
gBye++;
|
||||
}
|
||||
}
|
||||
return -(ret < 0);
|
||||
}
|
||||
|
||||
static int HandleAccept(int lsfd)
|
||||
{
|
||||
struct sockaddr_in sa;
|
||||
int saLen = sizeof(sa);
|
||||
int fd = accept(lsfd, (struct sockaddr *)&sa, (socklen_t *)&saLen);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
perror("accept");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (AddFd(fd) == -1) {
|
||||
LogPrintln("Too many clients, refuse %s:%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
LogPrintln("New client %d: %s:%d", fd, inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int HandleReadfds(fd_set *fds, int lsfd)
|
||||
{
|
||||
int ret = 0;
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == INVALID_SOCKET || !FD_ISSET(gFds[i], fds)) {
|
||||
continue;
|
||||
}
|
||||
if (gFds[i] == lsfd) {
|
||||
ret += HandleAccept(lsfd);
|
||||
} else {
|
||||
ret += HandleRecv(gFds[i]);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *ClientsThread(void *param)
|
||||
{
|
||||
int fd;
|
||||
int thrNo = (int)(intptr_t)param;
|
||||
|
||||
LogPrintln("<%d>socket client thread started", thrNo);
|
||||
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
perror("socket");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
sa.sin_port = htons(SERVER_PORT);
|
||||
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
|
||||
perror("connect");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LogPrintln("[%d]<%d>connected to %s:%d successful", fd, thrNo, inet_ntoa(sa.sin_addr), SERVER_PORT);
|
||||
|
||||
const char *msg[] = {
|
||||
"hello, ",
|
||||
"ohos, ",
|
||||
"my name is net_socket_test_008, ",
|
||||
"see u next time, ",
|
||||
"Bye!"
|
||||
};
|
||||
|
||||
for (int i = 0; i < sizeof(msg) / sizeof(msg[0]); ++i) {
|
||||
if (send(fd, msg[i], strlen(msg[i]), 0) < 0) {
|
||||
LogPrintln("[%d]<%d>send msg [%s] fail", fd, thrNo, msg[i]);
|
||||
}
|
||||
}
|
||||
|
||||
(void)shutdown(fd, SHUT_RDWR);
|
||||
(void)close(fd);
|
||||
return param;
|
||||
}
|
||||
|
||||
static int StartClients(pthread_t *cli, int cliNum)
|
||||
{
|
||||
int ret;
|
||||
pthread_attr_t attr;
|
||||
|
||||
for (int i = 0; i < cliNum; ++i) {
|
||||
ret = pthread_attr_init(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_create(&cli[i], &attr, ClientsThread, (void *)(intptr_t)i);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_attr_destroy(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int SelectTest(void)
|
||||
{
|
||||
struct sockaddr_in sa = {0};
|
||||
int lsfd;
|
||||
int ret;
|
||||
|
||||
lsfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, errno);
|
||||
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
sa.sin_port = htons(SERVER_PORT);
|
||||
ret = bind(lsfd, (struct sockaddr *)&sa, sizeof(sa));
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, errno + CloseAllFd());
|
||||
|
||||
ret = listen(lsfd, BACKLOG);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, errno + CloseAllFd());
|
||||
|
||||
InitFds();
|
||||
AddFd(lsfd);
|
||||
LogPrintln("[%d]Waiting for client to connect on port %d", lsfd, SERVER_PORT);
|
||||
|
||||
pthread_t clients[CLIENT_NUM];
|
||||
|
||||
ret = StartClients(clients, CLIENT_NUM);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret + CloseAllFd());
|
||||
|
||||
for ( ; ; ) {
|
||||
int nfd;
|
||||
fd_set readfds;
|
||||
struct timeval timeout;
|
||||
|
||||
timeout.tv_sec = 3;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
nfd = 0;
|
||||
FD_ZERO(&readfds);
|
||||
|
||||
GetReadfds(&readfds, &nfd);
|
||||
|
||||
ret = select(nfd + 1, &readfds, NULL, NULL, &timeout);
|
||||
LogPrintln("select %d", ret);
|
||||
if (ret == -1) {
|
||||
perror("select");
|
||||
break; // error occurred
|
||||
} else if (ret == 0) {
|
||||
break; // timed out
|
||||
}
|
||||
|
||||
if (HandleReadfds(&readfds, lsfd) < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < CLIENT_NUM; ++i) {
|
||||
ret = pthread_join(clients[i], NULL);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret + CloseAllFd());
|
||||
}
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(gBye, CLIENT_NUM, gBye + CloseAllFd());
|
||||
(void)CloseAllFd();
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest008(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, SelectTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
278
testsuites/unittest/net/socket/smoke/net_socket_test_009.cpp
Normal file
278
testsuites/unittest/net/socket/smoke/net_socket_test_009.cpp
Normal file
@@ -0,0 +1,278 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
#include <poll.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define SERVER_PORT 8888
|
||||
#define INVALID_SOCKET -1
|
||||
#define CLIENT_NUM 50
|
||||
#define BACKLOG CLIENT_NUM
|
||||
|
||||
static int gFds[FD_SETSIZE];
|
||||
static int gBye;
|
||||
|
||||
static void InitFds()
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
|
||||
static void GetReadfds(struct pollfd *fds, int *nfd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; i++) {
|
||||
if (gFds[i] == INVALID_SOCKET) {
|
||||
continue;
|
||||
}
|
||||
fds[*nfd].fd = gFds[i];
|
||||
fds[*nfd].events = POLLIN;
|
||||
(*nfd)++;
|
||||
}
|
||||
}
|
||||
|
||||
static int AddFd(int fd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == INVALID_SOCKET) {
|
||||
gFds[i] = fd;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void DelFd(int fd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == fd) {
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
(void)close(fd);
|
||||
}
|
||||
|
||||
static int CloseAllFd(void)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] != INVALID_SOCKET) {
|
||||
(void)close(gFds[i]);
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int HandleRecv(int fd)
|
||||
{
|
||||
char buf[256];
|
||||
int ret = recv(fd, buf, sizeof(buf)-1, 0);
|
||||
if (ret < 0) {
|
||||
LogPrintln("[%d]Error: %s", fd, strerror(errno));
|
||||
DelFd(fd);
|
||||
} else if (ret == 0) {
|
||||
LogPrintln("[%d]Closed", fd);
|
||||
DelFd(fd);
|
||||
} else {
|
||||
buf[ret] = 0;
|
||||
LogPrintln("[%d]Received: %s", fd, buf);
|
||||
if (strstr(buf, "Bye") != NULL) {
|
||||
DelFd(fd);
|
||||
gBye++;
|
||||
}
|
||||
}
|
||||
return -(ret < 0);
|
||||
}
|
||||
|
||||
static int HandleAccept(int lsfd)
|
||||
{
|
||||
struct sockaddr_in sa;
|
||||
int saLen = sizeof(sa);
|
||||
int fd = accept(lsfd, (struct sockaddr *)&sa, (socklen_t *)&saLen);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
perror("accept");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (AddFd(fd) == -1) {
|
||||
LogPrintln("Too many clients, refuse %s:%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
LogPrintln("New client %d: %s:%d", fd, inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int HandleReadfds(struct pollfd *fds, int nfds, int lsfd)
|
||||
{
|
||||
int ret = 0;
|
||||
for (int i = 0; i < nfds; ++i) {
|
||||
if (fds[i].revents == 0) {
|
||||
continue;
|
||||
}
|
||||
LogPrintln("[%d]revents: %04hx", fds[i].fd, fds[i].revents);
|
||||
if (fds[i].fd == lsfd) {
|
||||
ret += HandleAccept(lsfd);
|
||||
} else {
|
||||
ret += HandleRecv(fds[i].fd);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *ClientsThread(void *param)
|
||||
{
|
||||
int fd;
|
||||
int thrNo = (int)(intptr_t)param;
|
||||
|
||||
LogPrintln("<%d>socket client thread started", thrNo);
|
||||
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
perror("socket");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
sa.sin_port = htons(SERVER_PORT);
|
||||
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
|
||||
perror("connect");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LogPrintln("[%d]<%d>connected to %s:%d successful", fd, thrNo, inet_ntoa(sa.sin_addr), SERVER_PORT);
|
||||
|
||||
const char *msg[] = {
|
||||
"hello, ",
|
||||
"ohos, ",
|
||||
"my name is net_socket_test_009, ",
|
||||
"see u next time, ",
|
||||
"Bye!"
|
||||
};
|
||||
|
||||
for (int i = 0; i < sizeof(msg) / sizeof(msg[0]); ++i) {
|
||||
if (send(fd, msg[i], strlen(msg[i]), 0) < 0) {
|
||||
LogPrintln("[%d]<%d>send msg [%s] fail", fd, thrNo, msg[i]);
|
||||
}
|
||||
}
|
||||
|
||||
(void)shutdown(fd, SHUT_RDWR);
|
||||
(void)close(fd);
|
||||
return param;
|
||||
}
|
||||
|
||||
static int StartClients(pthread_t *cli, int cliNum)
|
||||
{
|
||||
int ret;
|
||||
pthread_attr_t attr;
|
||||
|
||||
for (int i = 0; i < cliNum; ++i) {
|
||||
ret = pthread_attr_init(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_create(&cli[i], &attr, ClientsThread, (void *)(intptr_t)i);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_attr_destroy(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int PollTest(void)
|
||||
{
|
||||
struct sockaddr_in sa = {0};
|
||||
int lsfd;
|
||||
int ret;
|
||||
|
||||
lsfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, errno);
|
||||
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
sa.sin_port = htons(SERVER_PORT);
|
||||
ret = bind(lsfd, (struct sockaddr *)&sa, sizeof(sa));
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, errno + CloseAllFd());
|
||||
|
||||
ret = listen(lsfd, BACKLOG);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, errno + CloseAllFd());
|
||||
|
||||
InitFds();
|
||||
AddFd(lsfd);
|
||||
LogPrintln("[%d]Waiting for client to connect on port %d", lsfd, SERVER_PORT);
|
||||
|
||||
pthread_t clients[CLIENT_NUM];
|
||||
|
||||
ret = StartClients(clients, CLIENT_NUM);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret + CloseAllFd());
|
||||
|
||||
for ( ; ; ) {
|
||||
int nfd;
|
||||
struct pollfd readfds[FD_SETSIZE];
|
||||
int timeoutMs;
|
||||
|
||||
timeoutMs = 3000;
|
||||
nfd = 0;
|
||||
GetReadfds(readfds, &nfd);
|
||||
|
||||
ret = poll(readfds, nfd, timeoutMs);
|
||||
LogPrintln("poll %d", ret);
|
||||
if (ret == -1) {
|
||||
perror("poll");
|
||||
break; // error occurred
|
||||
} else if (ret == 0) {
|
||||
break; // timed out
|
||||
}
|
||||
|
||||
if (HandleReadfds(readfds, nfd, lsfd) < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < CLIENT_NUM; ++i) {
|
||||
ret = pthread_join(clients[i], NULL);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret + CloseAllFd());
|
||||
}
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(gBye, CLIENT_NUM, gBye + CloseAllFd());
|
||||
(void)CloseAllFd();
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest009(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, PollTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
191
testsuites/unittest/net/socket/smoke/net_socket_test_010.cpp
Normal file
191
testsuites/unittest/net/socket/smoke/net_socket_test_010.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/if.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
static char gDefaultNetif[IFNAMSIZ] = "eth0";
|
||||
|
||||
static void InitIfreq(struct ifreq *ifr)
|
||||
{
|
||||
*ifr = (struct ifreq){{0}};
|
||||
(void)strncpy_s(ifr->ifr_name, sizeof(ifr->ifr_name) - 1, gDefaultNetif, sizeof(ifr->ifr_name) - 1);
|
||||
ifr->ifr_name[sizeof(ifr->ifr_name) - 1] = '\0';
|
||||
}
|
||||
|
||||
static char *IfIndex2Name(int fd, unsigned index, char *name)
|
||||
{
|
||||
#if SUPPORT_IF_INDEX_TO_NAME
|
||||
return if_indextoname(index, name);
|
||||
#else
|
||||
struct ifreq ifr;
|
||||
int ret;
|
||||
|
||||
ifr.ifr_ifindex = index;
|
||||
ret = ioctl(fd, SIOCGIFNAME, &ifr);
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
}
|
||||
ret = strncpy_s(name, IF_NAMESIZE - 1, ifr.ifr_name, IF_NAMESIZE - 1);
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
}
|
||||
name[IF_NAMESIZE - 1] = '\0';
|
||||
return name;
|
||||
#endif
|
||||
}
|
||||
|
||||
static unsigned IfName2Index(int fd, const char *name)
|
||||
{
|
||||
#if SUPPORT_IF_NAME_TO_INDEX
|
||||
return if_nametoindex(name);
|
||||
#else
|
||||
struct ifreq ifr;
|
||||
int ret;
|
||||
|
||||
(void)strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name) - 1, name, sizeof(ifr.ifr_name) - 1);
|
||||
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
|
||||
ret = ioctl(fd, SIOCGIFINDEX, &ifr);
|
||||
return ret < 0 ? 0 : ifr.ifr_ifindex;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int IoctlTestInternal(int sfd)
|
||||
{
|
||||
struct ifreq ifr = {{0}};
|
||||
char ifName[IFNAMSIZ] = {0}, *p = NULL;
|
||||
unsigned int loIndex = 0;
|
||||
unsigned int lanIndex = 0;
|
||||
int maxIndex = 256;
|
||||
int ret;
|
||||
|
||||
for (int i = 0; i < maxIndex; ++i) {
|
||||
p = IfIndex2Name(sfd, i, ifName);
|
||||
if (p) {
|
||||
if (strcmp(p, "lo") == 0) {
|
||||
loIndex = i;
|
||||
} else {
|
||||
lanIndex = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LogPrintln("ifindex of lo: %u, ifindex of lan: %u", loIndex, lanIndex);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(loIndex, 0, errno);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(lanIndex, 0, errno);
|
||||
|
||||
p = IfIndex2Name(sfd, loIndex, ifName);
|
||||
LogPrintln("ifindex %u: %s", loIndex, p);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(p, NULL, errno);
|
||||
|
||||
p = IfIndex2Name(sfd, lanIndex, ifName);
|
||||
LogPrintln("ifindex %u: %s", lanIndex, p);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(p, NULL, errno);
|
||||
|
||||
ret = strncpy_s(gDefaultNetif, sizeof(gDefaultNetif) -1, p, sizeof(gDefaultNetif) - 1);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
gDefaultNetif[sizeof(gDefaultNetif) - 1] = '\0';
|
||||
|
||||
ret = (int)IfName2Index(sfd, p);
|
||||
LogPrintln("index of %s: %d", p, ret);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, errno);
|
||||
|
||||
ifr.ifr_ifindex = lanIndex;
|
||||
ret = ioctl(sfd, SIOCGIFNAME, &ifr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno);
|
||||
LogPrintln("name of ifindex %u: %s", lanIndex, ifr.ifr_name);
|
||||
|
||||
InitIfreq(&ifr);
|
||||
ret = ioctl(sfd, SIOCGIFINDEX, &ifr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno);
|
||||
LogPrintln("index of ifname %s: %d", ifr.ifr_name, ifr.ifr_ifindex);
|
||||
|
||||
InitIfreq(&ifr);
|
||||
ret = ioctl(sfd, SIOCGIFHWADDR, &ifr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno);
|
||||
LogPrintln("hwaddr: %02hhX:%02hhX:%02hhX:XX:XX:XX", ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2]);
|
||||
|
||||
InitIfreq(&ifr);
|
||||
ret = ioctl(sfd, SIOCGIFFLAGS, &ifr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno);
|
||||
LogPrintln("FLAGS of ifname %s: %#x, IFF_PROMISC: %d", ifr.ifr_name, ifr.ifr_flags, !!(ifr.ifr_flags & IFF_PROMISC));
|
||||
|
||||
if (ifr.ifr_flags & IFF_PROMISC) {
|
||||
ifr.ifr_flags &= ~(IFF_PROMISC);
|
||||
} else {
|
||||
ifr.ifr_flags |= IFF_PROMISC;
|
||||
}
|
||||
LogPrintln("SIOCSIFFLAGS FLAGS: %#x", ifr.ifr_flags);
|
||||
ret = ioctl(sfd, SIOCSIFFLAGS, &ifr);
|
||||
if (ret == -1) {
|
||||
ICUNIT_ASSERT_EQUAL(errno, EPERM, errno);
|
||||
} else {
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno);
|
||||
}
|
||||
|
||||
InitIfreq(&ifr);
|
||||
ret = ioctl(sfd, SIOCGIFFLAGS, &ifr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno);
|
||||
LogPrintln("FLAGS of ifname %s: %#x, IFF_PROMISC: %d", ifr.ifr_name, ifr.ifr_flags, !!(ifr.ifr_flags & IFF_PROMISC));
|
||||
|
||||
ret = fcntl(sfd, F_GETFL, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret < 0, 0, errno);
|
||||
|
||||
ret = fcntl(sfd, F_SETFL, ret | O_NONBLOCK);
|
||||
ICUNIT_ASSERT_EQUAL(ret < 0, 0, errno);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
static int IoctlTest(void)
|
||||
{
|
||||
int sfd;
|
||||
|
||||
sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
LogPrintln("socket(PF_INET, SOCK_STREAM, IPPROTO_TCP): %d", sfd);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, errno);
|
||||
|
||||
(void)IoctlTestInternal(sfd);
|
||||
|
||||
(void)close(sfd);
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest010(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, IoctlTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
254
testsuites/unittest/net/socket/smoke/net_socket_test_011.cpp
Normal file
254
testsuites/unittest/net/socket/smoke/net_socket_test_011.cpp
Normal file
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define SERVER_PORT 7777
|
||||
#define INVALID_SOCKET -1
|
||||
#define CLIENT_NUM 50
|
||||
|
||||
static int gFds[FD_SETSIZE];
|
||||
static int gBye;
|
||||
|
||||
static void InitFds()
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
|
||||
static void GetReadfds(fd_set *fds, int *nfd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; i++) {
|
||||
if (gFds[i] == INVALID_SOCKET) {
|
||||
continue;
|
||||
}
|
||||
FD_SET(gFds[i], fds);
|
||||
if (*nfd < gFds[i]) {
|
||||
*nfd = gFds[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int AddFd(int fd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == INVALID_SOCKET) {
|
||||
gFds[i] = fd;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void DelFd(int fd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == fd) {
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
(void)close(fd);
|
||||
}
|
||||
|
||||
static int CloseAllFd(void)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] != INVALID_SOCKET) {
|
||||
(void)close(gFds[i]);
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int HandleRecv(int fd)
|
||||
{
|
||||
char buf[256];
|
||||
int ret = recv(fd, buf, sizeof(buf)-1, 0);
|
||||
if (ret < 0) {
|
||||
LogPrintln("[%d]Error: %s", fd, strerror(errno));
|
||||
DelFd(fd);
|
||||
} else if (ret == 0) {
|
||||
LogPrintln("[%d]Closed", fd);
|
||||
DelFd(fd);
|
||||
} else {
|
||||
buf[ret] = 0;
|
||||
LogPrintln("[%d]Received: %s", fd, buf);
|
||||
if (strstr(buf, "Bye") != NULL) {
|
||||
gBye++;
|
||||
}
|
||||
}
|
||||
return -(ret < 0);
|
||||
}
|
||||
|
||||
static int HandleReadfds(fd_set *fds, int lsfd)
|
||||
{
|
||||
int ret = 0;
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == INVALID_SOCKET || !FD_ISSET(gFds[i], fds)) {
|
||||
continue;
|
||||
}
|
||||
ret += HandleRecv(gFds[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *ClientsThread(void *param)
|
||||
{
|
||||
int fd;
|
||||
int thrNo = (int)(intptr_t)param;
|
||||
|
||||
LogPrintln("<%d>socket client thread started", thrNo);
|
||||
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
perror("socket");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
sa.sin_port = htons(SERVER_PORT);
|
||||
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
|
||||
perror("connect");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LogPrintln("[%d]<%d>connected to udp://%s:%d successful", fd, thrNo, inet_ntoa(sa.sin_addr), SERVER_PORT);
|
||||
|
||||
const char *msg[] = {
|
||||
"hello, ",
|
||||
"ohos, ",
|
||||
"my name is net_socket_test_011, ",
|
||||
"see u next time, ",
|
||||
"Bye!"
|
||||
};
|
||||
|
||||
for (int i = 0; i < sizeof(msg) / sizeof(msg[0]); ++i) {
|
||||
if (send(fd, msg[i], strlen(msg[i]), 0) < 0) {
|
||||
LogPrintln("[%d]<%d>send msg [%s] fail", fd, thrNo, msg[i]);
|
||||
}
|
||||
}
|
||||
|
||||
(void)shutdown(fd, SHUT_RDWR);
|
||||
(void)close(fd);
|
||||
return param;
|
||||
}
|
||||
|
||||
static int StartClients(pthread_t *cli, int cliNum)
|
||||
{
|
||||
int ret;
|
||||
pthread_attr_t attr;
|
||||
|
||||
for (int i = 0; i < cliNum; ++i) {
|
||||
ret = pthread_attr_init(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_create(&cli[i], &attr, ClientsThread, (void *)(intptr_t)i);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_attr_destroy(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int UdpSelectTest(void)
|
||||
{
|
||||
struct sockaddr_in sa = {0};
|
||||
int lsfd;
|
||||
int ret;
|
||||
|
||||
lsfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, errno);
|
||||
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
sa.sin_port = htons(SERVER_PORT);
|
||||
ret = bind(lsfd, (struct sockaddr *)&sa, sizeof(sa));
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, errno + CloseAllFd());
|
||||
|
||||
InitFds();
|
||||
AddFd(lsfd);
|
||||
LogPrintln("[%d]Waiting for client to connect on UDP port %d", lsfd, SERVER_PORT);
|
||||
|
||||
pthread_t clients[CLIENT_NUM];
|
||||
|
||||
ret = StartClients(clients, CLIENT_NUM);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret + CloseAllFd());
|
||||
|
||||
for ( ; ; ) {
|
||||
int nfd;
|
||||
fd_set readfds;
|
||||
struct timeval timeout;
|
||||
|
||||
timeout.tv_sec = 3;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
nfd = 0;
|
||||
FD_ZERO(&readfds);
|
||||
|
||||
GetReadfds(&readfds, &nfd);
|
||||
|
||||
ret = select(nfd + 1, &readfds, NULL, NULL, &timeout);
|
||||
LogPrintln("select %d", ret);
|
||||
if (ret == -1) {
|
||||
perror("select");
|
||||
break; // error occurred
|
||||
} else if (ret == 0) {
|
||||
break; // timed out
|
||||
}
|
||||
|
||||
if (HandleReadfds(&readfds, lsfd) < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < CLIENT_NUM; ++i) {
|
||||
ret = pthread_join(clients[i], NULL);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret + CloseAllFd());
|
||||
}
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(gBye, CLIENT_NUM, gBye + CloseAllFd());
|
||||
(void)CloseAllFd();
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest011(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, UdpSelectTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
451
testsuites/unittest/net/socket/smoke/net_socket_test_012.cpp
Normal file
451
testsuites/unittest/net/socket/smoke/net_socket_test_012.cpp
Normal file
@@ -0,0 +1,451 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/uio.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
|
||||
static struct iovec gIov[IOV_MAX + 1];
|
||||
|
||||
static int SocketNullTestInternal(int sfd)
|
||||
{
|
||||
int ret;
|
||||
struct sockaddr addr = {0};
|
||||
struct sockaddr *bad = (struct sockaddr *)0xbad;
|
||||
socklen_t addrlen = sizeof(addr);
|
||||
socklen_t zero = 0;
|
||||
struct msghdr message = {0};
|
||||
void *badUserAddr = (void*)0x3effffff;
|
||||
|
||||
/**
|
||||
* accept
|
||||
*/
|
||||
ret = accept(sfd, NULL, NULL);
|
||||
LogPrintln("accept: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = accept(sfd, NULL, &addrlen);
|
||||
LogPrintln("accept: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = accept(sfd, bad, &zero);
|
||||
LogPrintln("accept: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = accept(sfd, &addr, NULL);
|
||||
LogPrintln("accept: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* bind
|
||||
*/
|
||||
ret = bind(sfd, NULL, addrlen);
|
||||
LogPrintln("bind: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = bind(sfd, bad, 0);
|
||||
LogPrintln("bind: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* getpeername
|
||||
*/
|
||||
ret = getpeername(sfd, NULL, NULL);
|
||||
LogPrintln("getpeername: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = getpeername(sfd, NULL, &addrlen);
|
||||
LogPrintln("getpeername: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = getpeername(sfd, &addr, NULL);
|
||||
LogPrintln("getpeername: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
zero = 0;
|
||||
ret = getpeername(sfd, bad, &zero);
|
||||
LogPrintln("getpeername: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
ICUNIT_ASSERT_EQUAL(errno, ENOTCONN, errno);
|
||||
|
||||
/**
|
||||
* getsockname
|
||||
*/
|
||||
ret = getsockname(sfd, NULL, NULL);
|
||||
LogPrintln("getsockname: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = getsockname(sfd, NULL, &addrlen);
|
||||
LogPrintln("getsockname: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = getsockname(sfd, &addr, NULL);
|
||||
LogPrintln("getsockname: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
zero = 0;
|
||||
ret = getsockname(sfd, bad, &zero);
|
||||
LogPrintln("getsockname: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
/**
|
||||
* getsockopt
|
||||
*/
|
||||
ret = getsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, NULL, NULL);
|
||||
LogPrintln("getsockopt: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = getsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, NULL, &addrlen);
|
||||
LogPrintln("getsockopt: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = getsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, &addr, NULL);
|
||||
LogPrintln("getsockopt: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
zero = 0;
|
||||
ret = getsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, bad, &zero);
|
||||
LogPrintln("getsockopt: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
/**
|
||||
* setsockopt
|
||||
*/
|
||||
ret = setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, NULL, addrlen);
|
||||
LogPrintln("setsockopt: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, bad, 0);
|
||||
LogPrintln("setsockopt: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, bad, addrlen);
|
||||
LogPrintln("setsockopt: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* connect
|
||||
*/
|
||||
ret = connect(sfd, NULL, addrlen);
|
||||
LogPrintln("connect: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = connect(sfd, bad, 0);
|
||||
LogPrintln("connect: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = connect(sfd, bad, addrlen);
|
||||
LogPrintln("connect: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* recv
|
||||
*/
|
||||
ret = recv(sfd, NULL, 1, MSG_DONTWAIT);
|
||||
LogPrintln("recv: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* recvfrom
|
||||
*/
|
||||
ret = recvfrom(sfd, NULL, 1, MSG_DONTWAIT, NULL, NULL);
|
||||
LogPrintln("recvfrom: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = recvfrom(sfd, NULL, 1, MSG_DONTWAIT, NULL, &addrlen);
|
||||
LogPrintln("recvfrom: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = recvfrom(sfd, NULL, 1, MSG_DONTWAIT, &addr, NULL);
|
||||
LogPrintln("recvfrom: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
zero = 0;
|
||||
ret = recvfrom(sfd, NULL, 1, MSG_DONTWAIT, bad, &zero);
|
||||
LogPrintln("recvfrom: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* recvmsg
|
||||
*/
|
||||
ret = recvmsg(sfd, NULL, MSG_DONTWAIT);
|
||||
LogPrintln("recvmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = NULL;
|
||||
message.msg_iovlen = 1;
|
||||
ret = recvmsg(sfd, &message, MSG_DONTWAIT);
|
||||
LogPrintln("recvmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = gIov;
|
||||
message.msg_iovlen = 1 + IOV_MAX;
|
||||
ret = recvmsg(sfd, &message, MSG_DONTWAIT);
|
||||
LogPrintln("recvmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = (struct iovec *)((void *)0xbad);
|
||||
message.msg_iovlen = 1;
|
||||
ret = recvmsg(sfd, &message, MSG_DONTWAIT);
|
||||
LogPrintln("recvmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = (struct iovec *)((void *)0xbad);
|
||||
message.msg_iovlen = 0;
|
||||
ret = recvmsg(sfd, &message, MSG_DONTWAIT);
|
||||
LogPrintln("recvmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* send
|
||||
*/
|
||||
ret = send(sfd, NULL, 1, MSG_NOSIGNAL);
|
||||
LogPrintln("send: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = send(sfd, bad, 0, MSG_NOSIGNAL);
|
||||
LogPrintln("send: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
/**
|
||||
* sendmsg
|
||||
*/
|
||||
ret = sendmsg(sfd, NULL, MSG_NOSIGNAL);
|
||||
LogPrintln("sendmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = NULL;
|
||||
message.msg_iovlen = 1;
|
||||
ret = sendmsg(sfd, &message, MSG_NOSIGNAL);
|
||||
LogPrintln("sendmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = gIov;
|
||||
message.msg_iovlen = IOV_MAX + 1;
|
||||
ret = sendmsg(sfd, &message, MSG_NOSIGNAL);
|
||||
LogPrintln("sendmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = gIov;
|
||||
message.msg_iovlen = (~0UL / sizeof(struct iovec)) + 2; // Test overflow
|
||||
ret = sendmsg(sfd, &message, MSG_NOSIGNAL);
|
||||
LogPrintln("sendmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = (struct iovec *)0xbad;
|
||||
message.msg_iovlen = 1;
|
||||
ret = sendmsg(sfd, &message, MSG_NOSIGNAL);
|
||||
LogPrintln("sendmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
message.msg_iov = (struct iovec *)0xbad;
|
||||
message.msg_iovlen = 0;
|
||||
ret = sendmsg(sfd, &message, MSG_NOSIGNAL);
|
||||
LogPrintln("sendmsg: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* sendto
|
||||
*/
|
||||
ret = sendto(sfd, NULL, 1, MSG_NOSIGNAL, NULL, addrlen);
|
||||
LogPrintln("sendto: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = sendto(sfd, NULL, 1, MSG_NOSIGNAL, &addr, addrlen);
|
||||
LogPrintln("sendto: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = sendto(sfd, bad, 0, MSG_NOSIGNAL, &addr, addrlen);
|
||||
LogPrintln("sendto: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
ret = sendto(sfd, "NULL", 4, MSG_NOSIGNAL, NULL, addrlen);
|
||||
LogPrintln("sendto: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = sendto(sfd, "NULL", 4, MSG_NOSIGNAL, bad, 0);
|
||||
LogPrintln("sendto: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = sendto(sfd, "NULL", 4, MSG_NOSIGNAL, bad, addrlen);
|
||||
LogPrintln("sendto: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
/**
|
||||
* read/readv
|
||||
*/
|
||||
ret = read(sfd, NULL, 1);
|
||||
LogPrintln("read: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = read(sfd, NULL, 0);
|
||||
LogPrintln("read: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno);
|
||||
|
||||
ret = read(sfd, bad, 1);
|
||||
LogPrintln("read: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = read(sfd, badUserAddr, 1);
|
||||
LogPrintln("read: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = read(sfd, bad, 0);
|
||||
LogPrintln("read: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
ret = readv(sfd, (struct iovec *)bad, 0);
|
||||
LogPrintln("readv: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
ret = readv(sfd, (struct iovec *)bad, 1);
|
||||
LogPrintln("readv: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = readv(sfd, gIov, 0);
|
||||
LogPrintln("readv: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
ret = readv(sfd, gIov, IOV_MAX + 1);
|
||||
LogPrintln("readv: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
gIov[0].iov_base = bad;
|
||||
gIov[0].iov_len = 1;
|
||||
ret = readv(sfd, gIov, 1);
|
||||
LogPrintln("readv: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
gIov[0].iov_base = bad;
|
||||
gIov[0].iov_len = 0;
|
||||
ret = readv(sfd, gIov, 1);
|
||||
LogPrintln("readv: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
/**
|
||||
* write/writev
|
||||
*/
|
||||
ret = write(sfd, NULL, 1);
|
||||
LogPrintln("write: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = write(sfd, NULL, 0);
|
||||
LogPrintln("write: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
ret = write(sfd, bad, 1);
|
||||
LogPrintln("write: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = write(sfd, badUserAddr, 1);
|
||||
LogPrintln("write: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = write(sfd, bad, 0);
|
||||
LogPrintln("write: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
ret = writev(sfd, (struct iovec *)bad, 0);
|
||||
LogPrintln("writev: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
ret = writev(sfd, (struct iovec *)bad, 1);
|
||||
LogPrintln("writev: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
ret = writev(sfd, gIov, 0);
|
||||
LogPrintln("writev: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
ret = writev(sfd, gIov, IOV_MAX + 1);
|
||||
LogPrintln("writev: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
gIov[0].iov_base = bad;
|
||||
gIov[0].iov_len = 1;
|
||||
ret = writev(sfd, gIov, 1);
|
||||
LogPrintln("writev: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL(ret, -1, errno);
|
||||
|
||||
gIov[0].iov_base = bad;
|
||||
gIov[0].iov_len = 0;
|
||||
ret = writev(sfd, gIov, 1);
|
||||
LogPrintln("writev: %d, errno=%d", ret, errno);
|
||||
ICUNIT_ASSERT_EQUAL((ret == 0 || ret == -1), 1, errno);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
static int SocketSetNonBlock(int sfd)
|
||||
{
|
||||
int ret = fcntl(sfd, F_GETFL, 0);
|
||||
ICUNIT_ASSERT_EQUAL(ret < 0, 0, errno);
|
||||
|
||||
ret = fcntl(sfd, F_SETFL, ret | O_NONBLOCK);
|
||||
ICUNIT_ASSERT_EQUAL(ret < 0, 0, errno);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
static int SocketNullTest(void)
|
||||
{
|
||||
int sfd;
|
||||
|
||||
sighandler_t oldHdl = signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
sfd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, errno);
|
||||
LogPrintln("UDP socket: %d", sfd);
|
||||
(void)SocketSetNonBlock(sfd);
|
||||
(void)SocketNullTestInternal(sfd);
|
||||
(void)close(sfd);
|
||||
|
||||
sfd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, errno);
|
||||
LogPrintln("TCP socket: %d", sfd);
|
||||
(void)SocketSetNonBlock(sfd);
|
||||
(void)SocketNullTestInternal(sfd);
|
||||
(void)close(sfd);
|
||||
|
||||
(void)signal(SIGPIPE, oldHdl);
|
||||
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest012(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, SocketNullTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
|
||||
336
testsuites/unittest/net/socket/smoke/net_socket_test_013.cpp
Normal file
336
testsuites/unittest/net/socket/smoke/net_socket_test_013.cpp
Normal file
@@ -0,0 +1,336 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <osTest.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#define SERVER_PORT 9999
|
||||
#define INVALID_SOCKET -1
|
||||
#define CLIENT_NUM 1
|
||||
|
||||
static int gFds[FD_SETSIZE];
|
||||
static int gBye;
|
||||
|
||||
static void InitFds()
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
|
||||
static void GetReadfds(fd_set *fds, int *nfd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; i++) {
|
||||
if (gFds[i] == INVALID_SOCKET) {
|
||||
continue;
|
||||
}
|
||||
FD_SET(gFds[i], fds);
|
||||
if (*nfd < gFds[i]) {
|
||||
*nfd = gFds[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int AddFd(int fd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == INVALID_SOCKET) {
|
||||
gFds[i] = fd;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void DelFd(int fd)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == fd) {
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
(void)close(fd);
|
||||
}
|
||||
|
||||
static int CloseAllFd(void)
|
||||
{
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] != INVALID_SOCKET) {
|
||||
(void)close(gFds[i]);
|
||||
gFds[i] = INVALID_SOCKET;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int HandleRecv(int fd)
|
||||
{
|
||||
char buf[256];
|
||||
int ret = recv(fd, buf, sizeof(buf)-1, 0);
|
||||
if (ret < 0) {
|
||||
LogPrintln("[%d]Error: %s", fd, strerror(errno));
|
||||
DelFd(fd);
|
||||
} else if (ret == 0) {
|
||||
LogPrintln("[%d]Closed", fd);
|
||||
DelFd(fd);
|
||||
} else {
|
||||
buf[ret] = 0;
|
||||
LogPrintln("[%d]Received: %s", fd, buf);
|
||||
if (strstr(buf, "Bye") != NULL) {
|
||||
gBye++;
|
||||
}
|
||||
}
|
||||
return -(ret < 0);
|
||||
}
|
||||
|
||||
static int HandleReadfds(fd_set *fds, int lsfd)
|
||||
{
|
||||
int ret = 0;
|
||||
for (int i = 0; i < FD_SETSIZE; ++i) {
|
||||
if (gFds[i] == INVALID_SOCKET || !FD_ISSET(gFds[i], fds)) {
|
||||
continue;
|
||||
}
|
||||
ret += HandleRecv(gFds[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static unsigned int GetIp(int sfd, const char *ifname)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
unsigned int ip = 0;
|
||||
int ret = strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name) - 1, ifname, sizeof(ifr.ifr_name) - 1);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
}
|
||||
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
|
||||
ret = ioctl(sfd, SIOCGIFADDR, &ifr);
|
||||
if (ret == 0) {
|
||||
ip = ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr;
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
static unsigned int GetNetmask(int sfd, const char *ifname)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
unsigned int msk = 0;
|
||||
int ret = strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name) - 1, ifname, sizeof(ifr.ifr_name) - 1);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
}
|
||||
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
|
||||
ret = ioctl(sfd, SIOCGIFNETMASK, &ifr);
|
||||
if (ret == 0) {
|
||||
msk = ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr;
|
||||
}
|
||||
return msk;
|
||||
}
|
||||
|
||||
static char *MyInetNtoa(unsigned int ip)
|
||||
{
|
||||
struct in_addr in = {ip};
|
||||
return inet_ntoa(in);
|
||||
}
|
||||
|
||||
static void *ClientsThread(void *param)
|
||||
{
|
||||
int fd;
|
||||
int thrNo = (int)(intptr_t)param;
|
||||
int ret;
|
||||
const char *ifname[] = {"eth0", "wlan0", "et1", "wl1", "enp4s0f0"};
|
||||
unsigned int ip, msk, brdcast;
|
||||
|
||||
LogPrintln("<%d>socket client thread started", thrNo);
|
||||
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
perror("socket");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int broadcast = 1;
|
||||
ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
|
||||
if (ret != 0) {
|
||||
LogPrintln("[%d]<%d> set broadcast option fail", fd, thrNo);
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int j = 0; j < sizeof(ifname) / sizeof(ifname[0]); ++j) {
|
||||
ip = GetIp(fd, ifname[j]);
|
||||
msk = GetNetmask(fd, ifname[j]);
|
||||
if (ip != 0) {
|
||||
LogPrintln("[%d]<%d>%s: ip %s", fd, thrNo, ifname[j], MyInetNtoa(ip));
|
||||
LogPrintln("[%d]<%d>%s: netmask %s", fd, thrNo, ifname[j], MyInetNtoa(msk));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
brdcast = ip | ~msk;
|
||||
LogPrintln("[%d]<%d>broadcast address %s", fd, thrNo, MyInetNtoa(brdcast));
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = brdcast;
|
||||
sa.sin_port = htons(SERVER_PORT);
|
||||
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
|
||||
perror("connect");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LogPrintln("[%d]<%d>connected to udp://%s:%d successful", fd, thrNo, inet_ntoa(sa.sin_addr), SERVER_PORT);
|
||||
|
||||
const char *msg[] = {
|
||||
"hello, ",
|
||||
"ohos, ",
|
||||
"my name is net_socket_test_013, ",
|
||||
"see u next time, ",
|
||||
"Bye!"
|
||||
};
|
||||
|
||||
for (int i = 0; i < sizeof(msg) / sizeof(msg[0]); ++i) {
|
||||
if (send(fd, msg[i], strlen(msg[i]), 0) < 0) {
|
||||
LogPrintln("[%d]<%d>send msg [%s] fail: errno %d", fd, thrNo, msg[i], errno);
|
||||
}
|
||||
}
|
||||
|
||||
(void)shutdown(fd, SHUT_RDWR);
|
||||
(void)close(fd);
|
||||
return param;
|
||||
}
|
||||
|
||||
static int StartClients(pthread_t *cli, int cliNum)
|
||||
{
|
||||
int ret;
|
||||
pthread_attr_t attr;
|
||||
|
||||
for (int i = 0; i < cliNum; ++i) {
|
||||
ret = pthread_attr_init(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_create(&cli[i], &attr, ClientsThread, (void *)(intptr_t)i);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_attr_destroy(&attr);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int UdpBrdcastSelectTest(void)
|
||||
{
|
||||
struct sockaddr_in sa = {0};
|
||||
int lsfd;
|
||||
int ret;
|
||||
|
||||
lsfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, errno);
|
||||
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
sa.sin_port = htons(SERVER_PORT);
|
||||
ret = bind(lsfd, (struct sockaddr *)&sa, sizeof(sa));
|
||||
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, errno + CloseAllFd());
|
||||
|
||||
int broadcast = 1;
|
||||
ret = setsockopt(lsfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno + CloseAllFd());
|
||||
|
||||
int loop = 0;
|
||||
socklen_t len = sizeof(loop);
|
||||
ret = getsockopt(lsfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, &len);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno + CloseAllFd());
|
||||
LogPrintln("IP_MULTICAST_LOOP default: %d", loop);
|
||||
|
||||
loop = 0;
|
||||
ret = setsockopt(lsfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno + CloseAllFd());
|
||||
|
||||
ret = getsockopt(lsfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, &len);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, errno + CloseAllFd());
|
||||
LogPrintln("IP_MULTICAST_LOOP changed to: %d", loop);
|
||||
|
||||
InitFds();
|
||||
AddFd(lsfd);
|
||||
LogPrintln("[%d]Waiting for client to connect on UDP port %d", lsfd, SERVER_PORT);
|
||||
|
||||
pthread_t clients[CLIENT_NUM];
|
||||
|
||||
ret = StartClients(clients, CLIENT_NUM);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret + CloseAllFd());
|
||||
|
||||
for ( ; ; ) {
|
||||
int nfd;
|
||||
fd_set readfds;
|
||||
struct timeval timeout;
|
||||
|
||||
timeout.tv_sec = 3;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
nfd = 0;
|
||||
FD_ZERO(&readfds);
|
||||
|
||||
GetReadfds(&readfds, &nfd);
|
||||
|
||||
ret = select(nfd + 1, &readfds, NULL, NULL, &timeout);
|
||||
LogPrintln("select %d", ret);
|
||||
if (ret == -1) {
|
||||
perror("select");
|
||||
break; // error occurred
|
||||
} else if (ret == 0) {
|
||||
break; // timed out
|
||||
}
|
||||
|
||||
if (HandleReadfds(&readfds, lsfd) < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < CLIENT_NUM; ++i) {
|
||||
ret = pthread_join(clients[i], NULL);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret + CloseAllFd());
|
||||
}
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(gBye, CLIENT_NUM, gBye + CloseAllFd());
|
||||
(void)CloseAllFd();
|
||||
return ICUNIT_SUCCESS;
|
||||
}
|
||||
|
||||
void NetSocketTest013(void)
|
||||
{
|
||||
TEST_ADD_CASE(__FUNCTION__, UdpBrdcastSelectTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
|
||||
}
|
||||
Reference in New Issue
Block a user