diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/config.gni b/kernel_liteos_a-master/testsuites/unittest/net/netdb/config.gni new file mode 100644 index 0000000..2c1702a --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/config.gni @@ -0,0 +1,60 @@ +# Copyright (c) 2022-2022 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("//kernel/liteos_a/testsuites/unittest/config.gni") + +netdb_include_dirs = [ "$TEST_UNITTEST_DIR/net/netdb" ] + +netdb_sources_entry = [ "$TEST_UNITTEST_DIR/net/netdb/net_netdb_test.cpp" ] + +netdb_sources_smoke = [ + "$TEST_UNITTEST_DIR/net/netdb/smoke/net_netdb_test_001.cpp", + "$TEST_UNITTEST_DIR/net/netdb/smoke/net_netdb_test_013.cpp", +] + +netdb_sources_full = [ + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_002.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_003.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_004.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_005.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_006.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_007.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_008.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_009.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_010.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_011.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_012.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_015.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_016.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_017.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_018.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_019.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_020.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_021.cpp", + "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_022.cpp", +] diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp new file mode 100644 index 0000000..2e29594 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp @@ -0,0 +1,71 @@ +/* + * 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. + char host_file[] = "127.0.0.2 example.com\n", serv_file[] = "ftp 21/tcp\n"; + char *pathList[] = {"/etc/hosts", "/etc/services"}; + char *streamList[] = {host_file, serv_file}; + int streamLen[] = {sizeof(host_file), sizeof(serv_file)}; + const int file_number = 2; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + + 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); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest002(void) +{ + TEST_ADD_CASE(__FUNCTION__, AddrInfoTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_003.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_003.cpp new file mode 100644 index 0000000..229c3ad --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_003.cpp @@ -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); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp new file mode 100644 index 0000000..c05a6fb --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp @@ -0,0 +1,75 @@ +/* + * 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) +{ + char host_file[] = "127.0.0.1 localhost\n100.0.0.0 example.com example\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {host_file}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + + struct in_addr ia; + int length = 4; // the address length is 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.0.0.0"); + 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, "example.com", -1); + + errno = 0; + ia.s_addr = inet_addr("127.0.0.0"); + addr = gethostbyaddr(&ia, sizeof ia, AF_INET); + ICUNIT_ASSERT_EQUAL(errno, ENOENT, errno); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest004(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetHostByAddrTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_005.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_005.cpp new file mode 100644 index 0000000..7fe49c7 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_005.cpp @@ -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); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp new file mode 100644 index 0000000..9cc7c82 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp @@ -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 GetHostByNameTest(void) +{ + char host_file[] = "127.0.0.1 localhost\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {host_file}; + int streamLen[] = {sizeof(host_file)}; + int flag = PrepareFileEnv(pathList, streamList, streamLen, 1); + if (flag != 0) { + RecoveryFileEnv(pathList, 1); + return -1; + } + + 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); + + RecoveryFileEnv(pathList, 1); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest006(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetHostByNameTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp new file mode 100644 index 0000000..42ef5c5 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp @@ -0,0 +1,81 @@ +/* + * 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) +{ + char host_file[] = "127.0.0.1 localhost\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {static_cast(host_file)}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + + 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); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest007(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetHostByNameRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp new file mode 100644 index 0000000..7819b3b --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp @@ -0,0 +1,72 @@ +/* + * 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) +{ + char host_file[] = "127.0.0.1 localhost\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {static_cast(host_file)}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + + 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); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest008(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetHostByName2Test, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp new file mode 100644 index 0000000..7a9ab53 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp @@ -0,0 +1,80 @@ +/* + * 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) +{ + char host_file[] = "127.0.0.1 localhost\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {host_file}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + + 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); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest009(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetHostByName2RTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_010.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_010.cpp new file mode 100644 index 0000000..efd6b88 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_010.cpp @@ -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); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp new file mode 100644 index 0000000..32f7d43 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp @@ -0,0 +1,67 @@ +/* + * 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. + char serv_file[] = "ssh 22/tcp\n"; + char *pathList[] = {"/etc/services"}; + char *streamList[] = {static_cast(serv_file)}; + int streamLen[] = {sizeof(serv_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + + const int test_port_no = 22; // ssh port number is 22 + struct servent *se1 = nullptr; + struct servent *se = getservbyport(htons(test_port_no), "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(test_port_no), "tp"); + ICUNIT_ASSERT_EQUAL(se1, nullptr, -1); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest011(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetServByPortTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/it_pthread_test.h b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/it_pthread_test.h new file mode 100644 index 0000000..afcef81 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/it_pthread_test.h @@ -0,0 +1,92 @@ +/* + * 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 _IT_PTHREAD_TEST_H +#define _IT_PTHREAD_TEST_H + +#include "osTest.h" +#include +#include +#include + +#define SLEEP_AND_YIELD(tick) usleep((tick)*10 * 1000) + +#include "sys/syscall.h" + +static inline int Syscall(int nbr, int parm1, int parm2, int parm3, int parm4) +{ + register int reg7 __asm__("r7") = nbr; + register int reg3 __asm__("r3") = parm4; + register int reg2 __asm__("r2") = parm3; + register int reg1 __asm__("r1") = parm2; + register int reg0 __asm__("r0") = parm1; + + __asm__ __volatile__("svc 0" : "=r"(reg0) : "r"(reg7), "r"(reg0), "r"(reg1), "r"(reg2), "r"(reg3) : "memory"); + + return reg0; +} +extern INT32 g_iCunitErrCode; +extern INT32 g_iCunitErrLineNo; + +extern void ItTestPthread001(void); +extern void ItTestPthread002(void); +extern void ItTestPthread003(void); +extern void ItTestPthread004(void); +extern void ItTestPthread005(void); +extern void ItTestPthread006(void); +extern void ItTestPthread007(void); +extern void ItTestPthread008(void); +extern void ItTestPthread009(void); +extern void ItTestPthread010(void); +extern void ItTestPthread012(void); +extern void ItTestPthread011(void); +extern void ItTestPthread013(void); +extern void ItTestPthread014(void); +extern void ItTestPthread015(void); +extern void ItTestPthread016(void); +extern void ItTestPthread017(void); +extern void ItTestPthread018(void); +extern void ItTestPthread019(void); +extern void ItTestPthread020(void); +extern void ItTestPthread021(void); +extern void ItTestPthread022(void); +extern void ItTestPthread023(void); +extern void ItTestPthread024(void); +extern void ItTestPthread025(void); +extern void ItTestPthread026(void); +extern void ItTestPthread027(void); +extern void ItTestPthreadAtfork001(void); +extern void ItTestPthreadAtfork002(void); +extern void ItTestPthreadOnce001(void); +extern void ItTestPthreadCond001(void); +extern void ItTestPthreadCond002(void); +extern void ItTestPthreadCond003(void); +extern void ItTestPthreadCond004(void); +#endif diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/process_pthread_test.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/process_pthread_test.cpp new file mode 100644 index 0000000..3c2187c --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/process_pthread_test.cpp @@ -0,0 +1,425 @@ +/* + * 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 +#include +#include "it_pthread_test.h" +#include + +using namespace testing::ext; +namespace OHOS { +class ProcessPthreadTest : 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) +/* * + * @tc.name: it_test_pthread_003 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread003, TestSize.Level0) +{ + ItTestPthread003(); +} + +#ifndef LOSCFG_USER_TEST_SMP +/* * + * @tc.name: it_test_pthread_006 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread006, TestSize.Level0) +{ + ItTestPthread006(); +} +#endif + +/* * + * @tc.name: it_test_pthread_007 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread007, TestSize.Level0) +{ + ItTestPthread007(); +} + +/* * + * @tc.name: it_test_pthread_008 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread008, TestSize.Level0) +{ + ItTestPthread008(); +} + +/* * + * @tc.name: it_test_pthread_009 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread009, TestSize.Level0) +{ + ItTestPthread009(); +} + +#ifndef LOSCFG_USER_TEST_SMP +/* * + * @tc.name: it_test_pthread_010 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread010, TestSize.Level0) +{ + ItTestPthread010(); +} +#endif + +/* * + * @tc.name: it_test_pthread_011 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread011, TestSize.Level0) +{ + ItTestPthread011(); +} + +/* * + * @tc.name: it_test_pthread_012 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread012, TestSize.Level0) +{ + ItTestPthread012(); +} + +/* * + * @tc.name: it_test_pthread_013 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread013, TestSize.Level0) +{ + ItTestPthread013(); +} + +/* * + * @tc.name: it_test_pthread_015 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread015, TestSize.Level0) +{ + ItTestPthread015(); +} + +/* * + * @tc.name: it_test_pthread_016 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread016, TestSize.Level0) +{ + ItTestPthread016(); +} + +/* * + * @tc.name: it_test_pthread_018 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread018, TestSize.Level0) +{ + ItTestPthread018(); +} + +/* * + * @tc.name: it_test_pthread_019 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread019, TestSize.Level0) +{ + ItTestPthread019(); +} + +/* * + * @tc.name: it_test_pthread_020 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + * @tc.require: issueI6T3P3 + * @tc.author: + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread020, TestSize.Level0) +{ + ItTestPthread020(); +} + +/* * + * @tc.name: it_test_pthread_021 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + * @tc.require: issueI6T3P3 + * @tc.author: + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread021, TestSize.Level0) +{ + ItTestPthread021(); +} + +/* * + * @tc.name: it_test_pthread_022 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + * @tc.require: issueI6T3P3 + * @tc.author: + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread022, TestSize.Level0) +{ + ItTestPthread022(); +} + +/* * + * @tc.name: it_test_pthread_024 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + * @tc.require: issueI6T3P3 + * @tc.author: + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread024, TestSize.Level0) +{ + ItTestPthread024(); +} + +/* * + * @tc.name: it_test_pthread_026 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + * @tc.require: issueI6T3P3 + * @tc.author: + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread026, TestSize.Level0) +{ + ItTestPthread026(); +} + +/* * + * @tc.name: it_test_pthread_027 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + * @tc.require: issueI6T3P3 + * @tc.author: + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread027, TestSize.Level0) +{ + ItTestPthread027(); +} + + +/* * + * @tc.name: it_test_pthread_023 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + * @tc.require: issueI6T3P3 + * @tc.author: + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread023, TestSize.Level0) +{ + ItTestPthread023(); +} + + +/* * + * @tc.name: it_test_pthread_025 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + * @tc.require: issueI6T3P3 + * @tc.author: + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread025, TestSize.Level0) +{ + ItTestPthread025(); +} + +/* * + * @tc.name: it_test_pthread_017 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread017, TestSize.Level0) +{ + ItTestPthread017(); +} + + +/* * + * @tc.name: it_test_pthread_once_001 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthreadOnce001, TestSize.Level0) +{ + ItTestPthreadOnce001(); +} + +/* * + * @tc.name: it_test_pthread_atfork_001 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthreadAtfork001, TestSize.Level0) +{ + ItTestPthreadAtfork001(); +} + +/* * + * @tc.name: it_test_pthread_atfork_002 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthreadAtfork002, TestSize.Level0) +{ + ItTestPthreadAtfork002(); +} + +/* * + * @tc.name: it_test_pthread_cond_001 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthreadCond001, TestSize.Level0) +{ + ItTestPthreadCond001(); +} + +/* * + * @tc.name: it_test_pthread_cond_002 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthreadCond002, TestSize.Level0) +{ + ItTestPthreadCond002(); +} + +/* * + * @tc.name: it_test_pthread_cond_003 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthreadCond003, TestSize.Level0) +{ + ItTestPthreadCond003(); +} + +/* * + * @tc.name: it_test_pthread_cond_004 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthreadCond004, TestSize.Level0) +{ + ItTestPthreadCond004(); +} + +#endif + +#if defined(LOSCFG_USER_TEST_FULL) +/* * + * @tc.name: it_test_pthread_001 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread001, TestSize.Level0) +{ + ItTestPthread001(); +} + +#ifndef LOSCFG_USER_TEST_SMP +/* * + * @tc.name: it_test_pthread_002 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread002, TestSize.Level0) +{ + ItTestPthread002(); +} +#endif +/* * + * @tc.name: it_test_pthread_004 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread004, TestSize.Level0) +{ + ItTestPthread004(); +} + +/* * + * @tc.name: it_test_pthread_005 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread005, TestSize.Level0) +{ + ItTestPthread005(); +} + +#ifndef LOSCFG_USER_TEST_SMP +/* * + * @tc.name: it_test_pthread_014 + * @tc.desc: function for ProcessPthreadTest + * @tc.type: FUNC + */ +HWTEST_F(ProcessPthreadTest, ItTestPthread014, TestSize.Level0) +{ + ItTestPthread014(); +} +#endif + +#endif +} // namespace OHOS diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_atfork_test_001.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_atfork_test_001.cpp new file mode 100644 index 0000000..9c3a3a3 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_atfork_test_001.cpp @@ -0,0 +1,175 @@ +/* + * 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 "it_pthread_test.h" + +static volatile int g_count = 0; +static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; +static int g_testAtforkCount = 0; +static int g_testAtforkPrepare = 0; +static int g_testAtforkParent = 0; +static int g_testAtforkChild = 0; +static const int SLEEP_TIME = 2; + +static void Prepare() +{ + int err; + ICUNIT_ASSERT_EQUAL_VOID(g_testAtforkCount, 1, g_testAtforkCount); + err = pthread_mutex_lock(&g_lock); + ICUNIT_ASSERT_EQUAL_VOID(err, 0, err); + g_testAtforkPrepare++; +} + +static void Parent() +{ + int err; + ICUNIT_ASSERT_EQUAL_VOID(g_testAtforkCount, 1, g_testAtforkCount); + + err = pthread_mutex_unlock(&g_lock); + ICUNIT_ASSERT_EQUAL_VOID(err, 0, err); + g_testAtforkParent++; +} + +static void child() +{ + int err; + ICUNIT_ASSERT_EQUAL_VOID(g_testAtforkCount, 1, g_testAtforkCount); + + err = pthread_mutex_unlock(&g_lock); + ICUNIT_ASSERT_EQUAL_VOID(err, 0, err); + g_testAtforkChild++; +} + +static void *ThreadProc(void *arg) +{ + int err; + + while (g_count < 5) { // 5, wait until g_count == 5. + err = pthread_mutex_lock(&g_lock); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + g_count++; + SLEEP_AND_YIELD(SLEEP_TIME); + err = pthread_mutex_unlock(&g_lock); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + SLEEP_AND_YIELD(SLEEP_TIME); + } + +EXIT: + return NULL; +} + +static void *PthreadAtforkTest(void *arg) +{ + int err; + pid_t pid; + pthread_t tid; + int status = 0; + + g_count = 0; + g_testAtforkCount = 0; + g_testAtforkPrepare = 0; + g_testAtforkParent = 0; + g_testAtforkChild = 0; + + err = pthread_create(&tid, NULL, ThreadProc, NULL); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + err = pthread_atfork(Prepare, Parent, child); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + + g_testAtforkCount++; + + SLEEP_AND_YIELD(SLEEP_TIME); + + pid = fork(); + ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, The pid will never exceed 100000. + ICUNIT_GOTO_EQUAL(g_testAtforkPrepare, 1, g_testAtforkPrepare, EXIT); + + if (pid == 0) { + ICUNIT_GOTO_EQUAL(g_testAtforkChild, 1, g_testAtforkChild, EXIT); + int status; + while (g_count < 5) { // 5, wait until g_count == 5. + err = pthread_mutex_lock(&g_lock); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + g_count++; + SLEEP_AND_YIELD(SLEEP_TIME); + err = pthread_mutex_unlock(&g_lock); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + SLEEP_AND_YIELD(SLEEP_TIME); + } + exit(15); // 15, set exit status + } + + ICUNIT_GOTO_EQUAL(g_testAtforkParent, 1, g_testAtforkParent, EXIT_WAIT); + err = pthread_join(tid, NULL); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT_WAIT); + + err = waitpid(pid, &status, 0); + status = WEXITSTATUS(status); + ICUNIT_GOTO_EQUAL(err, pid, err, EXIT); + ICUNIT_GOTO_EQUAL(status, 15, status, EXIT); // 15, get exit status. + +EXIT: + return NULL; + +EXIT_WAIT: + (void)waitpid(pid, &status, 0); + return NULL; +} + +static int Testcase() +{ + int ret; + pthread_t newPthread; + int curThreadPri, curThreadPolicy; + pthread_attr_t a = { 0 }; + struct sched_param param = { 0 }; + + ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, ¶m); + ICUNIT_ASSERT_EQUAL(ret, 0, -ret); + + curThreadPri = param.sched_priority; + + ret = pthread_attr_init(&a); + pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED); + param.sched_priority = curThreadPri + 2; // 2, adjust the priority. + pthread_attr_setschedparam(&a, ¶m); + ret = pthread_create(&newPthread, &a, PthreadAtforkTest, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_join(newPthread, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + return 0; +} + +void ItTestPthreadAtfork001(void) +{ + TEST_ADD_CASE("IT_PTHREAD_ATFORK_001", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_atfork_test_002.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_atfork_test_002.cpp new file mode 100644 index 0000000..018d613 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_atfork_test_002.cpp @@ -0,0 +1,168 @@ +/* + * 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 "it_pthread_test.h" +#include + +extern int nanosleep(const struct timespec *req, struct timespec *rem); + +static pthread_mutex_t g_mux = PTHREAD_MUTEX_INITIALIZER; +static volatile int g_testAtforkCount = 0; +static int g_testAtforkPrepare = 0; +static int g_testAtforkParent = 0; + +static void *Doit(void *arg) +{ + int err; + struct timespec ts = { 2, 0 }; // 2, set time 2s. + + ICUNIT_GOTO_EQUAL(g_testAtforkCount, 1, g_testAtforkCount, EXIT); + + err = pthread_mutex_lock(&g_mux); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + + (void)nanosleep(&ts, NULL); + err = pthread_mutex_unlock(&g_mux); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + +EXIT: + return NULL; +} + +static void *Doit1(void *arg) +{ + int err; + struct timespec ts = { 2, 0 }; // 2, set time 2s. + + ICUNIT_GOTO_EQUAL(g_testAtforkCount, 1, g_testAtforkCount, EXIT); + + err = pthread_mutex_lock(&g_mux); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + + (void)nanosleep(&ts, NULL); + err = pthread_mutex_unlock(&g_mux); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + +EXIT: + return NULL; +} + +static void Prepare(void) +{ + int err; + + err = pthread_mutex_unlock(&g_mux); + ICUNIT_ASSERT_EQUAL_VOID(err, 0, err); + g_testAtforkPrepare++; +} + +static void Parent(void) +{ + int err = pthread_mutex_lock(&g_mux); + ICUNIT_ASSERT_EQUAL_VOID(err, 0, err); + g_testAtforkParent++; +} + +static void *PthreadAtforkTest(void *arg) +{ + int err, ret; + int pid; + int status = 0; + struct timespec ts = { 1, 0 }; + pthread_t tid; + + g_testAtforkCount = 0; + g_testAtforkPrepare = 0; + g_testAtforkParent = 0; + + err = pthread_atfork(Prepare, Parent, NULL); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + g_testAtforkCount++; + err = pthread_create(&tid, NULL, Doit, NULL); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + + nanosleep(&ts, NULL); + pid = fork(); + ICUNIT_GOTO_EQUAL(g_testAtforkPrepare, 1, g_testAtforkPrepare, EXIT); + if (pid == 0) { + Doit1(NULL); + exit(10); // 10, set exit status. + } + + ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT_WAIT); // 100000, The pid will never exceed 100000. + ICUNIT_GOTO_EQUAL(g_testAtforkParent, 1, g_testAtforkParent, EXIT_WAIT); + + err = pthread_join(tid, NULL); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT_WAIT); + + + err = waitpid(pid, &status, 0); + status = WEXITSTATUS(status); + ICUNIT_GOTO_EQUAL(err, pid, err, EXIT); + ICUNIT_GOTO_EQUAL(status, 10, status, EXIT); // 10, get exit status. + +EXIT: + return NULL; + +EXIT_WAIT: + (void)waitpid(pid, 0, 0); + return NULL; +} + +static int Testcase(void) +{ + int ret; + pthread_t newPthread; + int curThreadPri, curThreadPolicy; + pthread_attr_t a = { 0 }; + struct sched_param param = { 0 }; + + ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, ¶m); + ICUNIT_ASSERT_EQUAL(ret, 0, -ret); + + curThreadPri = param.sched_priority; + + ret = pthread_attr_init(&a); + pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED); + param.sched_priority = curThreadPri + 2; // 2, adjust the priority. + pthread_attr_setschedparam(&a, ¶m); + ret = pthread_create(&newPthread, &a, PthreadAtforkTest, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_join(newPthread, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + return 0; +} + +void ItTestPthreadAtfork002(void) +{ + TEST_ADD_CASE("IT_PTHREAD_ATFORK_002", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_001.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_001.cpp new file mode 100644 index 0000000..ed5debc --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_001.cpp @@ -0,0 +1,124 @@ +/* + * 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 "it_pthread_test.h" + +static pthread_mutex_t g_pthreadMuxTest1; +static pthread_cond_t g_pthdCondTest1; + +static void *PthreadF01(void *t) +{ + int rc; + + rc = pthread_mutex_lock(&g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + g_testCount++; + LosTaskDelay(100); + ICUNIT_GOTO_EQUAL(g_testCount, 2, g_testCount, EXIT); // 2, here assert the result. + g_testCount++; + + rc = pthread_cond_wait(&g_pthdCondTest1, &g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + ICUNIT_GOTO_EQUAL(g_testCount, 5, g_testCount, EXIT); // 5, here assert the result. + rc = pthread_mutex_unlock(&g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + +EXIT: + return NULL; +} + +static void *PthreadF02(void *t) +{ + int i; + int rc; + + ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT); + g_testCount++; + rc = pthread_mutex_lock(&g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // 3, here assert the result. + g_testCount++; + rc = pthread_cond_signal(&g_pthdCondTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT); // 4, here assert the result. + g_testCount++; + + rc = pthread_mutex_unlock(&g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + LosTaskDelay(2); // 2, delay for Timing control. + +EXIT: + pthread_exit(NULL); +} + +static unsigned int TestCase(void) +{ + int i; + long t1 = 1; + long t2 = 2; + int rc; + pthread_t threads[3]; // 3, need 3 pthread for test. + pthread_attr_t attr; + const int loopNum = 2; + + g_testCount = 0; + pthread_mutex_init(&g_pthreadMuxTest1, NULL); + pthread_cond_init(&g_pthdCondTest1, NULL); + + rc = pthread_create(&threads[0], NULL, PthreadF01, (void *)t1); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + rc = pthread_create(&threads[1], NULL, PthreadF02, (void *)t2); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + for (i = 0; i < loopNum; i++) { + rc = pthread_join(threads[i], NULL); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + } + + rc = pthread_attr_destroy(&attr); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + rc = pthread_mutex_destroy(&g_pthreadMuxTest1); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + rc = pthread_cond_destroy(&g_pthdCondTest1); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + return 0; +} + +void ItTestPthreadCond001(void) +{ + TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_001", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_002.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_002.cpp new file mode 100644 index 0000000..70a2f02 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_002.cpp @@ -0,0 +1,119 @@ +/* + * 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 "it_pthread_test.h" + +static pthread_mutex_t g_pthreadMuxTest1; +static pthread_cond_t g_pthrdCondTest1; +static unsigned int g_pthreadExit = 0; + +static void *PthreadF01(void *t) +{ + int rc; + unsigned int count = 0; + const int testLoop = 2000; + while (count < testLoop) { + rc = pthread_mutex_lock(&g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + rc = pthread_cond_wait(&g_pthrdCondTest1, &g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + rc = pthread_mutex_unlock(&g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + count++; + } + + g_pthreadExit = 1; +EXIT: + return NULL; +} + +static void *PthreadF02(void *t) +{ + int i; + int rc; + + while (g_pthreadExit != 1) { + rc = pthread_mutex_lock(&g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + rc = pthread_cond_signal(&g_pthrdCondTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + rc = pthread_mutex_unlock(&g_pthreadMuxTest1); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + } + +EXIT: + pthread_exit(NULL); +} + +static unsigned int TestCase(void) +{ + int i; + long t1 = 1; + long t2 = 2; + int rc; + pthread_t threads[3]; + pthread_attr_t attr; + const int loopNum = 2; + + g_pthreadExit = 0; + g_testCount = 0; + pthread_mutex_init(&g_pthreadMuxTest1, NULL); + pthread_cond_init(&g_pthrdCondTest1, NULL); + + rc = pthread_create(&threads[0], NULL, PthreadF01, (void *)t1); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + rc = pthread_create(&threads[1], NULL, PthreadF02, (void *)t2); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + for (i = 0; i < loopNum; i++) { + rc = pthread_join(threads[i], NULL); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + } + + rc = pthread_attr_destroy(&attr); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + rc = pthread_mutex_destroy(&g_pthreadMuxTest1); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + rc = pthread_cond_destroy(&g_pthrdCondTest1); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + return 0; +} + +void ItTestPthreadCond002(void) +{ + TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_002", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_003.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_003.cpp new file mode 100644 index 0000000..736d7d5 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_003.cpp @@ -0,0 +1,151 @@ +/* + * 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 "it_pthread_test.h" + +static pthread_cond_t g_pthread_cond; +static pthread_mutex_t g_pthread_mutex; +#define TEST_THREAD_COUNT 5 +static void *pthread_cond_func001(void *arg) +{ + int ret; + struct timespec ts; + + g_testCount++; + + ret = pthread_mutex_lock(&g_pthread_mutex); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += 60; /* 60: wait 1 minute */ + + ret = pthread_cond_timedwait(&g_pthread_cond, &g_pthread_mutex, &ts); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ret = pthread_mutex_unlock(&g_pthread_mutex); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + g_testCount++; +EXIT: + return NULL; +} + +static VOID *pthread_f06(void *argument) +{ + int policy; + int ret; + int i; + pthread_attr_t attr; + struct sched_param schedParam = { 0 }; + pthread_t thread[TEST_THREAD_COUNT]; + + g_testCount = 0; + + ret = pthread_attr_init(&attr); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ret = pthread_getschedparam(pthread_self(), &policy, &schedParam); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + schedParam.sched_priority -= 1; + ret = pthread_attr_setschedparam(&attr, &schedParam); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + for (i = 0; i < TEST_THREAD_COUNT; i++) { + ret = pthread_create(&thread[i], &attr, pthread_cond_func001, NULL); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + } + + sleep(1); + + ICUNIT_GOTO_EQUAL(g_testCount, 5, g_testCount, EXIT); /* 5: Five threads */ + + ret = pthread_mutex_lock(&g_pthread_mutex); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ret = pthread_cond_broadcast(&g_pthread_cond); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ret = pthread_mutex_unlock(&g_pthread_mutex); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + for (i = 0; i < TEST_THREAD_COUNT; i++) { + ret = pthread_join(thread[i], NULL); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + } + + ICUNIT_GOTO_EQUAL(g_testCount, 10, g_testCount, EXIT); /* 10: Twice per thread */ +EXIT: + return NULL; +} + +static int TestCase(void) +{ + int policy; + pthread_attr_t attr; + pthread_t newTh; + struct sched_param schedParam = { 0 }; + int ret; + + ret = pthread_mutex_init(&g_pthread_mutex, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_cond_init(&g_pthread_cond, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_attr_init(&attr); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_getschedparam(pthread_self(), &policy, &schedParam); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + schedParam.sched_priority -= 1; + ret = pthread_attr_setschedparam(&attr, &schedParam); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_create(&newTh, &attr, pthread_f06, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_join(newTh, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + return 0; +} + +void ItTestPthreadCond003(void) +{ + TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_003", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_004.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_004.cpp new file mode 100644 index 0000000..6548031 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_cond_test_004.cpp @@ -0,0 +1,143 @@ +/* + * 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 "it_pthread_test.h" +static pthread_cond_t g_pthread_cond; +static pthread_mutex_t g_pthread_mutex; +#define TEST_THREAD_COUNT 5 + +static void *pthread_cond_func002(void *arg) +{ + int ret; + struct timespec ts; + + g_testCount++; + + ret = pthread_mutex_lock(&g_pthread_mutex); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += 2; /* 2: wait 2 seconds */ + + ret = pthread_cond_timedwait(&g_pthread_cond, &g_pthread_mutex, &ts); + ICUNIT_GOTO_EQUAL(ret, ETIMEDOUT, ret, EXIT); + + ret = pthread_mutex_unlock(&g_pthread_mutex); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + g_testCount++; +EXIT: + return NULL; +} + +static VOID *pthread_f07(void *argument) +{ + int policy; + int ret; + int i; + pthread_attr_t attr; + struct sched_param schedParam = { 0 }; + pthread_t thread[TEST_THREAD_COUNT]; + + g_testCount = 0; + + ret = pthread_attr_init(&attr); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ret = pthread_getschedparam(pthread_self(), &policy, &schedParam); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + schedParam.sched_priority -= 1; + ret = pthread_attr_setschedparam(&attr, &schedParam); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + for (i = 0; i < TEST_THREAD_COUNT; i++) { + ret = pthread_create(&thread[i], &attr, pthread_cond_func002, NULL); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + } + + sleep(1); + + ICUNIT_GOTO_EQUAL(g_testCount, 5, g_testCount, EXIT); /* 5: Five threads */ + + for (i = 0; i < TEST_THREAD_COUNT; i++) { + ret = pthread_join(thread[i], NULL); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + } + + ICUNIT_GOTO_EQUAL(g_testCount, 10, g_testCount, EXIT); /* 10: Twice per thread */ + +EXIT: + return NULL; +} + +static int TestCase(void) +{ + int policy; + pthread_attr_t attr; + pthread_t newTh; + struct sched_param schedParam = { 0 }; + int ret; + + ret = pthread_mutex_init(&g_pthread_mutex, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_cond_init(&g_pthread_cond, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_attr_init(&attr); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_getschedparam(pthread_self(), &policy, &schedParam); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + schedParam.sched_priority -= 1; + ret = pthread_attr_setschedparam(&attr, &schedParam); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_create(&newTh, &attr, pthread_f07, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_join(newTh, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + return 0; +} + +void ItTestPthreadCond004(void) +{ + TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_004", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_once_test_001.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_once_test_001.cpp new file mode 100644 index 0000000..a779ba3 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_once_test_001.cpp @@ -0,0 +1,110 @@ +/* + * 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 "it_pthread_test.h" + +static int g_number = 0; +static int g_okStatus = 777; // 777, a special number indicate the status is ok. +static pthread_once_t g_onceCtrl = PTHREAD_ONCE_INIT; + +static void InitRoutine(void) +{ + g_number++; +} + +static void *Threadfunc(void *parm) +{ + int err; + err = pthread_once(&g_onceCtrl, InitRoutine); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); + return reinterpret_cast(g_okStatus); +EXIT: + return NULL; +} + +static void *ThreadFuncTest(void *arg) +{ + pthread_t thread[3]; + int rc = 0; + int i = 3; + void *status; + const int threadsNum = 3; + + g_number = 0; + + for (i = 0; i < threadsNum; ++i) { + rc = pthread_create(&thread[i], NULL, Threadfunc, NULL); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + } + + for (i = 0; i < threadsNum; ++i) { + rc = pthread_join(thread[i], &status); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + ICUNIT_GOTO_EQUAL((unsigned int)status, (unsigned int)g_okStatus, status, EXIT); + } + + ICUNIT_GOTO_EQUAL(g_number, 1, g_number, EXIT); + +EXIT: + return NULL; +} + +static int Testcase(void) +{ + int ret; + pthread_t newPthread; + int curThreadPri, curThreadPolicy; + pthread_attr_t a = { 0 }; + struct sched_param param = { 0 }; + + g_onceCtrl = PTHREAD_ONCE_INIT; + + ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, ¶m); + ICUNIT_ASSERT_EQUAL(ret, 0, -ret); + + curThreadPri = param.sched_priority; + + ret = pthread_attr_init(&a); + pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED); + param.sched_priority = curThreadPri + 2; // 2, adjust the priority. + pthread_attr_setschedparam(&a, ¶m); + ret = pthread_create(&newPthread, &a, ThreadFuncTest, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_join(newPthread, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + return 0; +} + +void ItTestPthreadOnce001(void) +{ + TEST_ADD_CASE("IT_PTHREAD_ONCE_001", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_test_003.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_test_003.cpp new file mode 100644 index 0000000..5698c5b --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_test_003.cpp @@ -0,0 +1,127 @@ +/* + * 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 "it_pthread_test.h" + +/* *************************************************************************** + * Public Functions + * ************************************************************************** */ + +static int g_currThreadPri, g_currThreadPolicy; +static int g_testPthredCount; +static void *ThreadFuncTest2(void *arg) +{ + (void)arg; + int ret; + int policy; + struct sched_param param = { 0 }; + pthread_t pthread = pthread_self(); + + g_testPthredCount++; + + ret = pthread_getschedparam(pthread, &policy, ¶m); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ICUNIT_GOTO_EQUAL(g_currThreadPri, param.sched_priority, param.sched_priority, EXIT); + ICUNIT_GOTO_EQUAL(g_currThreadPolicy, policy, policy, EXIT); + ICUNIT_GOTO_EQUAL(g_testPthredCount, 2, g_testPthredCount, EXIT); // 2, here assert the result. +EXIT: + return NULL; +} + +static void *ThreadFuncTest3(void *arg) +{ + (void)arg; + int ret; + int policy; + struct sched_param param = { 0 }; + pthread_t pthread = pthread_self(); + + g_testPthredCount++; + + ret = pthread_getschedparam(pthread, &policy, ¶m); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + ICUNIT_GOTO_EQUAL(g_currThreadPri, param.sched_priority, param.sched_priority, EXIT); + ICUNIT_GOTO_EQUAL(policy, SCHED_FIFO, policy, EXIT); + ICUNIT_GOTO_EQUAL(g_testPthredCount, 4, g_testPthredCount, EXIT); // 4, here assert the result. + +EXIT: + return NULL; +} + +static int Testcase() +{ + struct sched_param param = { 0 }; + int ret; + void *res = NULL; + pthread_attr_t a = { 0 }; + pthread_t newPthread, newPthread1; + + g_testPthredCount = 0; + + ret = pthread_getschedparam(pthread_self(), &g_currThreadPolicy, ¶m); + ICUNIT_ASSERT_EQUAL(ret, 0, -ret); + + g_currThreadPri = param.sched_priority; + + g_testPthredCount++; + ret = pthread_create(&newPthread, NULL, ThreadFuncTest2, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_join(newPthread, &res); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ICUNIT_ASSERT_EQUAL(g_testPthredCount, 2, g_testPthredCount); // 2, here assert the result. + g_testPthredCount++; + + param.sched_priority = g_currThreadPri; + ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_create(&newPthread, NULL, ThreadFuncTest3, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_join(newPthread, &res); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ICUNIT_ASSERT_EQUAL(g_testPthredCount, 4, g_testPthredCount); // 4, here assert the result. + + param.sched_priority = g_currThreadPri; + ret = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + return 0; +} + +void ItTestPthread003(void) +{ + TEST_ADD_CASE("IT_POSIX_PTHREAD_003", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_test_006.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_test_006.cpp new file mode 100644 index 0000000..3846c74 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_test_006.cpp @@ -0,0 +1,150 @@ +/* + * 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 "it_pthread_test.h" + +static pthread_barrier_t g_barrier; +static int g_testToCount001 = 0; +static int g_threadTest[10]; + +static void *ThreadFuncTest0(void *a) +{ + int ret; + int count = *((int *)a); + g_testToCount001++; + + ret = pthread_barrier_wait(&g_barrier); + ICUNIT_GOTO_EQUAL(ret, PTHREAD_BARRIER_SERIAL_THREAD, ret, EXIT); + + g_threadTest[count] = count; + +EXIT: + return NULL; +} + +static void *ThreadFuncTest2(void *a) +{ + int ret; + int count = *((int *)a); + g_testToCount001++; + + ret = pthread_barrier_wait(&g_barrier); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + g_threadTest[count] = count; + +EXIT: + return NULL; +} + +static void *ThreadFuncTest1(void *a) +{ + int ret; + int count = *((int *)a); + g_testToCount001++; + + ret = pthread_barrier_wait(&g_barrier); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + + g_threadTest[count] = count; +EXIT: + return NULL; +} + +static int Testcase(void) +{ + struct sched_param param = { 0 }; + int ret; + void *res = NULL; + pthread_attr_t a = { 0 }; + pthread_t thread; + pthread_t newPthread[10], newPthread1; + pthread_mutexattr_t mutex; + int index = 0; + int currThreadPri, currThreadPolicy; + int threadParam[10]; + ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, ¶m); + ICUNIT_ASSERT_EQUAL(ret, 0, -ret); + currThreadPri = param.sched_priority; + const int testCount = 10; + + g_testToCount001 = 0; + + ret = pthread_attr_init(&a); + pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED); + param.sched_priority = currThreadPri - 1; + pthread_attr_setschedparam(&a, ¶m); + + ret = pthread_barrier_init(&g_barrier, NULL, testCount); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + threadParam[0] = 0; + ret = pthread_create(&newPthread[index], &a, ThreadFuncTest0, &threadParam[0]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + g_threadTest[0] = 0; + + index = 1; + while (index < (testCount - 1)) { + threadParam[index] = index; + ret = pthread_create(&newPthread[index], &a, ThreadFuncTest1, &threadParam[index]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + g_threadTest[index] = 0; + index++; + } + + ICUNIT_ASSERT_EQUAL(g_testToCount001, testCount - 1, g_testToCount001); + + threadParam[index] = index; + ret = pthread_create(&newPthread[index], &a, ThreadFuncTest2, &threadParam[index]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + sleep(1); + + ICUNIT_ASSERT_EQUAL(g_testToCount001, testCount, g_testToCount001); + + index = 0; + while (index < testCount) { + threadParam[index] = index; + ret = pthread_join(newPthread[index], NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(g_threadTest[index], index, g_threadTest[index]); + index++; + } + + ret = pthread_barrier_destroy(&g_barrier); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + return 0; +} + +void ItTestPthread006(void) +{ + TEST_ADD_CASE("IT_POSIX_PTHREAD_006", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_008.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_008.cpp new file mode 100644 index 0000000..3c4fcf6 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_008.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +static std::string GetFileMode(const char* filePath) +{ + const int MODE_COUNT = 11; + char fileProperty[MODE_COUNT] = "----------"; + char fileMode[MODE_COUNT] = "-rwxrwxrwx"; + struct stat buf; + stat(filePath, &buf); + unsigned int off = 256; + const int LOOP_VARY = 10; + for (int i = 1; i < LOOP_VARY; i++) { + if (buf.st_mode & (off >> (i - 1))) { + fileProperty[i] = fileMode[i]; + } + } + return fileProperty; +} + +static int IsFilePropertyR1(const char* filePath) +{ + std::string fileOrg = "-r--r--r--"; + std::string fileProperty = GetFileMode(filePath); + return strcmp(fileProperty.c_str(), fileOrg.c_str()); +} + +void ItProcessPlimits008(void) +{ + std::string filePath = "/proc/plimits/"; + std::vector fileName; + fileName.push_back("plimits.procs"); + fileName.push_back("plimits.limiters"); + fileName.push_back("pids.max"); + fileName.push_back("sched.period"); + fileName.push_back("sched.quota"); + fileName.push_back("sched.stat"); + fileName.push_back("memory.failcnt"); + fileName.push_back("memory.limit"); + fileName.push_back("memory.peak"); + fileName.push_back("memory.usage"); + fileName.push_back("memory.stat"); + + for (auto iter = fileName.begin(); iter != fileName.end(); ++iter) { + std::string fileFullPath = filePath + *iter; + int ret = IsFilePropertyR1(fileFullPath.c_str()); + ASSERT_EQ(ret, 0); + } + + return; +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_001.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_001.cpp new file mode 100644 index 0000000..f4b9d07 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_001.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsDevices001(void) +{ + int fd; + int ret; + mode_t mode; + char writeBuf[8]; + std::string test_dev = "/dev/hi_mipi"; + std::string path = "/proc/plimits/test"; + std::string procsTestPath = "/proc/plimits/test/plimits.procs"; + + ret = mkdir(path.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + (void)memset_s(writeBuf, sizeof(writeBuf), 0, sizeof(writeBuf)); + ret = sprintf_s(writeBuf, sizeof(writeBuf), "%d", getpid()); + ASSERT_NE(ret, -1); + + ret = WriteFile(procsTestPath.c_str(), writeBuf); + ASSERT_NE(ret, 0); + + fd = open(test_dev.c_str(), O_RDWR|O_CREAT); + ASSERT_NE(fd, -1); + (void)close(fd); + + ret = rmdir(path.c_str()); + ASSERT_EQ(ret, 0); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_004.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_004.cpp new file mode 100644 index 0000000..94a9224 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_004.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsDevices004(void) +{ + int fd; + int ret; + mode_t mode; + std::string test_dev = "/dev/hi_mipi"; + std::string device_a = "a * rwm"; + std::string device_a_m = "a * m"; + std::string path = "/proc/plimits/test"; + std::string devicesDenyPath = "/proc/plimits/test/devices.deny"; + std::string devicesAllowPath = "/proc/plimits/test/devices.allow"; + + ret = mkdir(path.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = WriteFile(devicesDenyPath.c_str(), device_a.c_str()); + ASSERT_NE(ret, -1); + ret = WriteFile(devicesAllowPath.c_str(), device_a_m.c_str()); + ASSERT_NE(ret, -1); + + fd = open(test_dev.c_str(), O_CREAT); + ASSERT_NE(fd, -1); + (void)close(fd); + + ret = rmdir(path.c_str()); + ASSERT_EQ(ret, 0); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_005.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_005.cpp new file mode 100644 index 0000000..0464ef5 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_005.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsDevices005(void) +{ + int fd; + int ret; + mode_t mode; + std::string test_dev = "/dev/mem"; + std::string device_a = "a * rwm"; + std::string device_c = "c * rwm"; + std::string path = "/proc/plimits/test"; + std::string devicesDenyPath = "/proc/plimits/test/devices.deny"; + std::string devicesAllowPath = "/proc/plimits/test/devices.allow"; + + ret = mkdir(path.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = WriteFile(devicesDenyPath.c_str(), device_a.c_str()); + ASSERT_NE(ret, -1); + ret = WriteFile(devicesAllowPath.c_str(), device_c.c_str()); + ASSERT_NE(ret, -1); + + fd = open(test_dev.c_str(), O_RDWR|O_CREAT); + ASSERT_NE(fd, -1); + (void)close(fd); + + ret = rmdir(path.c_str()); + ASSERT_EQ(ret, 0); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_006.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_006.cpp new file mode 100644 index 0000000..1b76142 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_006.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsDevices006(void) +{ + int fd; + int ret; + mode_t mode; + std::string test_dev = "/dev/mem"; + std::string device_a = "a * rwm"; + std::string device_c_r = "c * r"; + std::string path = "/proc/plimits/test"; + std::string devicesDenyPath = "/proc/plimits/test/devices.deny"; + std::string devicesAllowPath = "/proc/plimits/test/devices.allow"; + + ret = mkdir(path.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = WriteFile(devicesDenyPath.c_str(), device_a.c_str()); + ASSERT_NE(ret, -1); + ret = WriteFile(devicesAllowPath.c_str(), device_c_r.c_str()); + ASSERT_NE(ret, -1); + + fd = open(test_dev.c_str(), O_RDONLY); + ASSERT_NE(fd, -1); + (void)close(fd); + + ret = rmdir(path.c_str()); + ASSERT_EQ(ret, 0); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_007.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_007.cpp new file mode 100644 index 0000000..cd75c6d --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_007.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsDevices007(void) +{ + int fd; + int ret; + mode_t mode; + std::string test_dev = "/dev/mem"; + std::string device_a = "a * rwm"; + std::string device_c_w = "c * w"; + std::string path = "/proc/plimits/test"; + std::string devicesDenyPath = "/proc/plimits/test/devices.deny"; + std::string devicesAllowPath = "/proc/plimits/test/devices.allow"; + + ret = mkdir(path.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = WriteFile(devicesDenyPath.c_str(), device_a.c_str()); + ASSERT_NE(ret, -1); + ret = WriteFile(devicesAllowPath.c_str(), device_c_w.c_str()); + ASSERT_NE(ret, -1); + + fd = open(test_dev.c_str(), O_WRONLY); + ASSERT_NE(fd, -1); + (void)close(fd); + + ret = rmdir(path.c_str()); + ASSERT_EQ(ret, 0); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_008.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_008.cpp new file mode 100644 index 0000000..4e3b19f --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_008.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsDevices008(void) +{ + int fd; + int ret; + mode_t mode; + std::string test_dev = "/dev/mem"; + std::string device_a = "a * rwm"; + std::string device_c_m = "c * rm"; + std::string path = "/proc/plimits/test"; + std::string devicesDenyPath = "/proc/plimits/test/devices.deny"; + std::string devicesAllowPath = "/proc/plimits/test/devices.allow"; + + ret = mkdir(path.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = WriteFile(devicesDenyPath.c_str(), device_a.c_str()); + ASSERT_NE(ret, -1); + ret = WriteFile(devicesAllowPath.c_str(), device_c_m.c_str()); + ASSERT_NE(ret, -1); + + fd = open(test_dev.c_str(), O_CREAT, O_RDONLY); + ASSERT_NE(fd, -1); + (void)close(fd); + + ret = rmdir(path.c_str()); + ASSERT_EQ(ret, 0); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_009.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_009.cpp new file mode 100644 index 0000000..c1fa4a5 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_devices_009.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsDevices009(void) +{ + int fd; + int ret; + mode_t mode; + std::string test_dev = "/dev/hi_mipi"; + std::string device_a = "a"; + std::string path = "/proc/plimits/test"; + std::string devicesDenyPath = "/proc/plimits/test/devices.deny"; + + ret = mkdir(path.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = WriteFile(devicesDenyPath.c_str(), device_a.c_str()); + ASSERT_NE(ret, -1); + + fd = open(test_dev.c_str(), O_RDONLY|O_CREAT); + ASSERT_EQ(fd, -1); + (void)close(fd); + + fd = open(test_dev.c_str(), O_WRONLY|O_CREAT); + ASSERT_EQ(fd, -1); + (void)close(fd); + + ret = rmdir(path.c_str()); + ASSERT_EQ(ret, 0); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_002.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_002.cpp new file mode 100644 index 0000000..93a7de3 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_002.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsIpc002(void) +{ + mode_t mode; + int ret; + int fd; + std::string plimitsRootPath = "/proc/plimits/"; + std::vector ipcConfFileName; + ipcConfFileName.push_back("ipc.mq_limit"); + ipcConfFileName.push_back("ipc.shm_limit"); + ipcConfFileName.push_back("ipc.stat"); + + for (auto iter = ipcConfFileName.begin(); iter != ipcConfFileName.end(); ++iter) { + std::string fullPath = plimitsRootPath + *iter; + fd = access(fullPath.c_str(), W_OK | X_OK); + ASSERT_EQ(fd, -1); + fd = access(fullPath.c_str(), F_OK | R_OK); + ASSERT_EQ(fd, 0); + } + return; +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_003.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_003.cpp new file mode 100644 index 0000000..ffd3603 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_003.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsIpc003(void) +{ + mode_t mode; + int ret; + std::string plimitsPath = "/proc/plimits/test"; + std::string configFileMqCount = "/proc/plimits/test/ipc.mq_limit"; + std::string configFileShmSize = "/proc/plimits/test/ipc.shm_limit"; + std::string configFileStat = "/proc/plimits/test/ipc.stat"; + + ret = mkdir(plimitsPath.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + int fd = access(configFileMqCount.c_str(), F_OK | W_OK | R_OK); + ASSERT_EQ(fd, 0); + + fd = access(configFileShmSize.c_str(), F_OK | W_OK | R_OK); + ASSERT_EQ(fd, 0); + + fd = access(configFileStat.c_str(), F_OK | R_OK); + ASSERT_EQ(fd, 0); + + fd = access(configFileStat.c_str(), W_OK | X_OK); + ASSERT_EQ(fd, -1); + + ret = rmdir(plimitsPath.c_str()); + ASSERT_EQ(ret, 0); + return; +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_004.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_004.cpp new file mode 100644 index 0000000..b70dfa5 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_004.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsIpc004(void) +{ + mode_t mode; + mode_t chmodMode = 0777; + int ret; + int fd; + std::string rootplimitsPath = "/proc/plimits"; + std::string subPlimitsPath = "/proc/plimits/test"; + + std::vector ipcConfFileName; + ipcConfFileName.push_back("ipc.mq_limit"); + ipcConfFileName.push_back("ipc.shm_limit"); + ipcConfFileName.push_back("ipc.stat"); + + ret = mkdir(subPlimitsPath.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + for (auto iter = ipcConfFileName.begin(); iter != ipcConfFileName.end(); ++iter) { + std::string fullPath = rootplimitsPath + "/" + *iter; + ret = chmod(fullPath.c_str(), mode); + ASSERT_EQ(ret, -1); + } + + for (auto iter = ipcConfFileName.begin(); iter != ipcConfFileName.end(); ++iter) { + std::string fullPath = subPlimitsPath + "/" + *iter; + ret = chmod(fullPath.c_str(), mode); + ASSERT_EQ(ret, -1); + } + + ret = rmdir(subPlimitsPath.c_str()); + ASSERT_EQ(ret, 0); + return; +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_005.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_005.cpp new file mode 100644 index 0000000..d4d984f --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_005.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +static const int g_buffSize = 512; +static const int g_arryLen = 4; +static const int MQ_MAX_PER_PROCESS_COUNT = 255; +static const int MQUEUE_STANDARD_NAME_LENGTH = 50; + +void ItProcessPlimitsIpc005(void) +{ + INT32 ret; + mode_t mode; + char buf[g_buffSize] = { 0 }; + CHAR mqname[MQUEUE_STANDARD_NAME_LENGTH] = ""; + mqd_t mqueue[MQ_MAX_PER_PROCESS_COUNT]; + struct mq_attr attr = { 0 }; + int index = 0; + attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH; + attr.mq_maxmsg = 1; + + std::string plimitsPath = "/proc/plimits/test"; + + ret = mkdir(plimitsPath.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = ReadFile("/proc/plimits/test/ipc.mq_limit", buf); + ASSERT_STREQ(buf, "1024\n"); + + for (index = 0; index < MQ_MAX_PER_PROCESS_COUNT; ++index) { + ret = snprintf_s(mqname, sizeof(mqname), MQUEUE_STANDARD_NAME_LENGTH, "/mq_005_%d_%d", + index, LosCurTaskIDGet()); + ASSERT_NE(ret, -1); + mqueue[index] = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); + ASSERT_NE(mqueue[index], (mqd_t)-1); + } + for (int k = 0; k < MQ_MAX_PER_PROCESS_COUNT; ++k) { + ret = mq_close(mqueue[k]); + ASSERT_NE(ret, -1); + ret = snprintf_s(mqname, sizeof(mqname), MQUEUE_STANDARD_NAME_LENGTH, "/mq_005_%d_%d", k, LosCurTaskIDGet()); + ASSERT_NE(ret, -1); + ret = mq_unlink(mqname); + ASSERT_EQ(ret, 0); + } + + ret = rmdir(plimitsPath.c_str()); + ASSERT_EQ(ret, 0); + return; +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_006.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_006.cpp new file mode 100644 index 0000000..1e5b25f --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_006.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +static const int MQUEUE_STANDARD_NAME_LENGTH = 50; +static const int g_buffSize = 512; +static const int g_arryLen = 4; +static const int g_readLen = 254; +static const int MQ_MAX_LIMIT_COUNT = 10; + +static int FreeResource(mqd_t *mqueue, int index, char *mqname) +{ + int ret = -1; + for (int k = 0; k < index; ++k) { + if (snprintf_s(mqname, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH, "/mq_006_%d_%d", + k, LosCurTaskIDGet()) < 0) { + return -1; + } + ret = mq_close(mqueue[k]); + if (ret != 0) { + return -2; /* 2: errno */ + } + ret = mq_unlink(mqname); + if (ret != 0) { + return -3; /* 3: errno */ + } + (void)memset_s(mqname, MQUEUE_STANDARD_NAME_LENGTH, 0, MQUEUE_STANDARD_NAME_LENGTH); + } + return ret; +} + +// plimits_ipc_07, plimits_ipc_14, plimits_ipc_17 +void ItProcessPlimitsIpc006(void) +{ + INT32 ret; + mode_t mode; + char buf[g_buffSize] = { 0 }; + char *array[g_arryLen] = { nullptr }; + int mqSuccessCount = -1, mqFailedCount = -1; + CHAR mqname[MQUEUE_STANDARD_NAME_LENGTH] = ""; + mqd_t mqueue[g_readLen]; + std::string subPlimitsPath = "/proc/plimits/test"; + std::string configFileMqCount = "/proc/plimits/test/ipc.mq_limit"; + std::string limitMqCount = "10"; + struct mq_attr attr = { 0 }; + attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH; + attr.mq_maxmsg = 1; + int index = 0; + + ret = mkdir(subPlimitsPath.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = WriteFile(configFileMqCount.c_str(), limitMqCount.c_str()); + ASSERT_NE(ret, -1); + + for (index = 0; index < MQ_MAX_LIMIT_COUNT; ++index) { + ret = snprintf_s(mqname, sizeof(mqname), MQUEUE_STANDARD_NAME_LENGTH, "/mq_006_%d_%d", + index, LosCurTaskIDGet()); + ASSERT_NE(ret, -1); + mqueue[index] = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); + ASSERT_NE(mqueue[index], (mqd_t)-1); + (void)memset_s(mqname, sizeof(mqname), 0, sizeof(mqname)); + } + ret = snprintf_s(mqname, sizeof(mqname), MQUEUE_STANDARD_NAME_LENGTH, "/mq_006_%d_%d", index, LosCurTaskIDGet()); + ASSERT_NE(ret, -1); + mqueue[index] = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); + ASSERT_EQ(mqueue[index], (mqd_t)-1); + (void)memset_s(mqname, sizeof(mqname), 0, sizeof(mqname)); + + ret = ReadFile("/proc/plimits/test/ipc.stat", buf); + printf("/proc/plimits/test/ipc.stat: %s\n", buf); + GetLine(buf, g_arryLen, g_readLen, array); + mqSuccessCount = atoi(array[0] + strlen("mq count: ")); + mqFailedCount = atoi((array[1] + strlen("mq failed count: "))); + ASSERT_EQ(mqSuccessCount, MQ_MAX_LIMIT_COUNT); + ASSERT_EQ(mqFailedCount, 1); + ret = FreeResource(mqueue, index, mqname); + ASSERT_EQ(ret, 0); + + ret = rmdir(subPlimitsPath.c_str()); + ASSERT_EQ(ret, 0); + return; +} diff --git a/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_007.cpp b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_007.cpp new file mode 100644 index 0000000..e2ed884 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/process/plimits/smoke/It_process_plimits_ipc_007.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023-2023 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 +#include +#include +#include +#include +#include +#include +#include +#include "It_process_plimits.h" + +void ItProcessPlimitsIpc007(void) +{ + mode_t mode; + int ret; + std::string plimitsPath = "/proc/plimits/test"; + std::string configFileMqCount = "/proc/plimits/test/ipc.mq_limit"; + std::string mqLimitCount = "0123"; + + ret = mkdir(plimitsPath.c_str(), S_IFDIR | mode); + ASSERT_EQ(ret, 0); + + ret = WriteFile(configFileMqCount.c_str(), mqLimitCount.c_str()); + ASSERT_NE(ret, -1); + + ret = rmdir(plimitsPath.c_str()); + ASSERT_EQ(ret, 0); + return; +} diff --git a/kernel_liteos_a-master/testsuites/unittest/security/capability/security_capability_test.cpp b/kernel_liteos_a-master/testsuites/unittest/security/capability/security_capability_test.cpp new file mode 100644 index 0000000..4ed9c5f --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/security/capability/security_capability_test.cpp @@ -0,0 +1,56 @@ +/* + * 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 +#include + +#include "it_test_capability.h" + +using namespace testing::ext; +namespace OHOS { +class SecurityCapabilityTest : public testing::Test { +public: + static void SetUpTestCase(void) {} + static void TearDownTestCase(void) {} +}; + +#if defined(LOSCFG_USER_TEST_SMOKE) +/** + * @tc.name: It_Test_Cap_001 + * @tc.desc: function for SecurityCapbilityTest + * @tc.type: FUNC + */ +HWTEST_F(SecurityCapabilityTest, ItTestCap001, TestSize.Level0) +{ + ItTestCap001(); +} +#endif +} // namespace OHOS diff --git a/kernel_liteos_a-master/testsuites/unittest/security/capability/smoke/cap_test_001.cpp b/kernel_liteos_a-master/testsuites/unittest/security/capability/smoke/cap_test_001.cpp new file mode 100644 index 0000000..f888798 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/security/capability/smoke/cap_test_001.cpp @@ -0,0 +1,182 @@ +/* + * 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 "pthread.h" +#include "linux/capability.h" +#include +#include "it_test_capability.h" +#include +#include +#include + +#define CAP_NUM 2 +#define INVAILD_PID 65535 +#define CHANGE_CHILD_UID 1000 + +static void Sigac(int param) +{ + return; +} + +static void Child() +{ + int i = 10; + signal(25, Sigac); + + while (i--) { + sleep(1); + } + exit(0); +} + +static int TestChild(VOID) +{ + struct __user_cap_header_struct capheader; + struct __user_cap_data_struct capdata[CAP_NUM]; + struct __user_cap_data_struct capdatac[CAP_NUM]; + struct timespec tp; + int ret; + + (void)memset_s(&capheader, sizeof(struct __user_cap_header_struct), 0, sizeof(struct __user_cap_header_struct)); + (void)memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct), 0, + CAP_NUM * sizeof(struct __user_cap_data_struct)); + capdata[0].permitted = 0xffffffff; + capdata[1].permitted = 0xffffffff; + capheader.version = _LINUX_CAPABILITY_VERSION_3; + capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective |= CAP_TO_MASK(CAP_SETPCAP); + capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective |= CAP_TO_MASK(CAP_SETUID); + capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective |= CAP_TO_MASK(CAP_KILL); + capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective |= CAP_TO_MASK(CAP_SYS_TIME); + capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective |= CAP_TO_MASK(CAP_SYS_NICE); + ret = capset(&capheader, &capdata[0]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = capget(&capheader, &capdatac[0]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + capheader.pid = INVAILD_PID; + ret = capget(&capheader, &capdatac[0]); + ICUNIT_ASSERT_EQUAL(ret, -1, ret); + errno = 0; + capheader.pid = 3; + kill(capheader.pid, 0); + if (errno != ESRCH) { + ret = capget(&capheader, &capdatac[0]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("e %d,p %d\n", capdatac[0].effective, capdatac[0].permitted); + } + errno = 0; + capheader.pid = 4; + kill(capheader.pid, 0); + if (errno != ESRCH) { + ret = capget(&capheader, &capdatac[0]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("e %d,p %d\n", capdatac[0].effective, capdatac[0].permitted); + } + errno = 0; + capheader.pid = 5; + kill(capheader.pid, 0); + if (errno != ESRCH) { + ret = capget(&capheader, &capdatac[0]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("e %d,p %d\n", capdatac[0].effective, capdatac[0].permitted); + } + errno = 0; + capheader.pid = 6; + kill(capheader.pid, 0); + if (errno != ESRCH) { + ret = capget(&capheader, &capdatac[0]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("e %d,p %d\n", capdatac[0].effective, capdatac[0].permitted); + } + capheader.pid = 0; + + int pid = fork(); + if (pid == 0) { + ret = setuid(CHANGE_CHILD_UID); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + Child(); + } + sleep(1); + ret = kill(pid, SIGXFSZ); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective &= ~CAP_TO_MASK(CAP_KILL); + ret = capset(&capheader, &capdata[0]); + ret = kill(pid, SIGXFSZ); + ICUNIT_ASSERT_EQUAL(ret, -1, ret); + + tp.tv_sec = 0; + tp.tv_nsec = 0; + ret = clock_settime(CLOCK_REALTIME, &tp); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective &= ~CAP_TO_MASK(CAP_SYS_TIME); + ret = capset(&capheader, &capdata[0]); + ret = clock_settime(CLOCK_REALTIME, &tp); + ICUNIT_ASSERT_EQUAL(ret, -1, ret); + + struct sched_param param = { 0 }; + ret = sched_getparam(pid, ¶m); + param.sched_priority--; + ret = sched_setparam(pid, ¶m); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective &= ~CAP_TO_MASK(CAP_SYS_NICE); + ret = capset(&capheader, &capdata[0]); + ret = sched_setparam(pid, ¶m); + ICUNIT_ASSERT_EQUAL(ret, -1, ret); + wait(nullptr); + exit(92); + + return 0; +} + +static int TestCase(VOID) +{ + int ret; + int status = 0; + pid_t pid = fork(); + ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); + if (pid == 0) { + ret = TestChild(); + exit(__LINE__); + } + + ret = waitpid(pid, &status, 0); + ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT); + status = WEXITSTATUS(status); + ICUNIT_GOTO_EQUAL(status, 92, status, EXIT); + + return 0; + +EXIT: + return 1; +} + +void ItTestCap001(void) +{ + TEST_ADD_CASE("IT_SEC_CAP_001", TestCase, TEST_POSIX, TEST_SEC, TEST_LEVEL0, TEST_FUNCTION); +}