Projects
openEuler:24.03:SP1:Everything:64G
npth
_service:tar_scm:add-test-cases.patch
Sign Up
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File _service:tar_scm:add-test-cases.patch of Package npth
From 9ce3797bb45fc360bf0d38a0d28911b1c1ada585 Mon Sep 17 00:00:00 2001 From: yaowenbin <yaowenbin1@huawei.com> Date: Tue, 1 Mar 2022 08:30:38 +0000 Subject: [PATCH 1/1] add test cases The following test cases are added to test more interfaces of npth: t-condlock.c test npth condlock interfaces t-fork-enhance.c test npth fork interfaces t-rwlock.c test npth rwlock interfaces t-signal.c test npth signal interfaces t-socket.c test npth socket interfaces Signed-off-by: yaowenbin <yaowenbin1@huawei.com> --- tests/Makefile.am | 3 +- tests/t-condlock.c | 106 +++++++++++++++ tests/t-fork-enhance.c | 68 ++++++++++ tests/t-rwlock.c | 196 ++++++++++++++++++++++++++++ tests/t-signal.c | 49 +++++++ tests/t-socket.c | 284 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 705 insertions(+), 1 deletion(-) create mode 100644 tests/t-condlock.c create mode 100644 tests/t-fork-enhance.c create mode 100644 tests/t-rwlock.c create mode 100644 tests/t-signal.c create mode 100644 tests/t-socket.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 8092a3c..f37e798 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -18,7 +18,7 @@ ## Process this file with automake to produce Makefile.in -TESTS = t-mutex t-thread +TESTS = t-mutex t-thread t-condlock t-rwlock t-signal t-socket # We explicitly require POSIX.1-2001 so that pthread_rwlock_t is # available when build with c99. @@ -31,6 +31,7 @@ AM_CPPFLAGS = -I../src -D_POSIX_C_SOURCE=200112L AM_LDFLAGS = LDADD = ../src/libnpth.la $(LIBSOCKET) $(LIB_CLOCK_GETTIME) TESTS += t-fork +TESTS += t-fork-enhance endif noinst_HEADERS = t-support.h diff --git a/tests/t-condlock.c b/tests/t-condlock.c new file mode 100644 index 0000000..ff7597b --- /dev/null +++ b/tests/t-condlock.c @@ -0,0 +1,106 @@ +/* t-condlock.c + * + * This file is free software; as a special exception the author gives + * unlimited permission to copy and/or distribute it, with or without + * modifications, as long as this notice is preserved. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include "t-support.h" + + +static int counter; +static npth_mutex_t cond_mutex; +static npth_cond_t cond = NPTH_COND_INITIALIZER; + +static void * +thread_one (void *arg) +{ + int rc; + struct timespec tout; + + rc = npth_mutex_lock (&cond_mutex); + fail_if_err (rc); + if (counter == 0) + npth_cond_wait(&cond, &cond_mutex); + counter--; + info_msg ("count dec"); + clock_gettime(CLOCK_REALTIME, &tout); + tout.tv_sec += 1; + npth_cond_timedwait (&cond, &cond_mutex, &tout); + npth_mutex_unlock (&cond_mutex); + return (void*)4711; +} + +static void * +thread_two (void *arg) +{ + int rc; + + rc = npth_mutex_lock (&cond_mutex); + fail_if_err (rc); + counter++; + info_msg ("count inc"); + if (counter != 0) + npth_cond_signal(&cond); + npth_mutex_unlock (&cond_mutex); + return (void*)4722; +} + +int +main (int argc, char *argv[]) +{ + int rc; + npth_attr_t tattr; + int state; + npth_t tid1, tid2; + void *retval; + + if (argc >= 2 && !strcmp (argv[1], "--verbose")) + opt_verbose = 1; + + rc = npth_init (); + fail_if_err (rc); + + rc = npth_mutex_init (&cond_mutex, NULL); + fail_if_err (rc); + + rc = npth_attr_init (&tattr); + fail_if_err (rc); + rc = npth_attr_getdetachstate (&tattr, &state); + fail_if_err (rc); + if ( state != NPTH_CREATE_JOINABLE ) + fail_msg ("new tattr is not joinable"); + + info_msg ("creating thread-one"); + rc = npth_create (&tid1, &tattr, thread_one, NULL); + fail_if_err (rc); + npth_setname_np (tid1, "thread-one"); + + npth_usleep(100); + + info_msg ("creating thread-two"); + rc = npth_create (&tid2, &tattr, thread_two, NULL); + fail_if_err (rc); + npth_setname_np (tid2, "thread-two"); + + rc = npth_attr_destroy (&tattr); + fail_if_err (rc); + + info_msg ("waiting for thread-one to terminate"); + rc = npth_join (tid1, &retval); + fail_if_err (rc); + if (retval != (void*)4711) + fail_msg ("thread-one returned an unexpected value"); + + info_msg ("waiting for thread-two to terminate"); + rc = npth_join (tid2, &retval); + fail_if_err (rc); + if (retval != (void*)4722) + fail_msg ("thread-two returned an unexpected value"); + + return 0; +} diff --git a/tests/t-fork-enhance.c b/tests/t-fork-enhance.c new file mode 100644 index 0000000..0c0a957 --- /dev/null +++ b/tests/t-fork-enhance.c @@ -0,0 +1,68 @@ +/* t-fork-enhance.c + * + * This file is free software; as a special exception the author gives + * unlimited permission to copy and/or distribute it, with or without + * modifications, as long as this notice is preserved. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include "t-support.h" + +/* This is a test if nPth can allow daemon-like applications + initializing earlier. + + For daemon-like applications, ideally, it is expected to call + npth_init after fork. This condition is not satisfied sometimes. + + Failure of this test means nPth implementation doesn't allow + npth_init after fork. In such a case, application should be + modified. + */ + +int +main (int argc, const char *argv[]) +{ + int rc; + pid_t pid; + struct timespec ts; + + if (argc >= 2 && !strcmp (argv[1], "--verbose")) + opt_verbose = 1; + + rc = npth_init (); + fail_if_err (rc); + + rc = npth_system("pwd"); + fail_if_err (rc); + + npth_clock_gettime (&ts); + + npth_unprotect (); + npth_protect (); + npth_is_protected (); + + pid = fork (); + if (pid == (pid_t)-1) + fail_msg ("fork failed"); + else if (pid) + { + int status; + + info_msg ("forked"); + npth_waitpid(pid, &status, 0); + fail_if_err (status); + } + else + { + info_msg ("child exit"); + npth_usleep (1000); /* Let NPTH enter, sleep, and leave. */ + } + + return 0; +} diff --git a/tests/t-rwlock.c b/tests/t-rwlock.c new file mode 100644 index 0000000..6b06e05 --- /dev/null +++ b/tests/t-rwlock.c @@ -0,0 +1,196 @@ +/* t-rwlock.c + * + * This file is free software; as a special exception the author gives + * unlimited permission to copy and/or distribute it, with or without + * modifications, as long as this notice is preserved. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include "t-support.h" + +#define BUFLEN 100 + +static int counter; +static npth_mutex_t counter_mutex; +static int thread_twoone_ready; +static npth_rwlock_t counter_rwlock; + +static void * +thread_one (void *arg) +{ + int rc, i; + struct timespec tout; + + clock_gettime(CLOCK_REALTIME, &tout); + tout.tv_sec += 1; + rc = npth_rwlock_timedrdlock (&counter_rwlock, &tout); + if (rc == 0) + npth_rwlock_unlock(&counter_rwlock); + + clock_gettime(CLOCK_REALTIME, &tout); + tout.tv_sec += 1; + rc = npth_rwlock_timedwrlock (&counter_rwlock, &tout); + if (rc == 0) + npth_rwlock_unlock(&counter_rwlock); + + info_msg ("thread-one started"); + npth_usleep (10); /* Give the other thread some time to start. */ + for (i=0; i < 10; i++) + { + /* We would not need the mutex here, but we use it to allow the + system to switch to another thread. */ + rc = npth_mutex_lock (&counter_mutex); + fail_if_err (rc); + + counter++; + + rc = npth_mutex_unlock (&counter_mutex); + fail_if_err (rc); + } + + info_msg ("thread-one terminated"); + + return (void*)4711; +} + + +static void * +thread_twoone (void *arg) +{ + int rc, i; + + npth_setname_np (npth_self (), "thread-twoone"); + info_msg ("thread-twoone started"); + + rc = npth_detach (npth_self ()); + fail_if_err (rc); + + while (counter < 100) + { + npth_usleep (1000); + counter++; + } + info_msg ("thread-twoone terminated"); + thread_twoone_ready = 1; + + npth_exit (&rc); + return NULL; +} + + +static void * +thread_two (void *arg) +{ + int rc, i; + + info_msg ("thread-two started"); + + for (i=0; i < 10; i++) + { + rc = npth_mutex_lock (&counter_mutex); + fail_if_err (rc); + + counter--; + + if (i == 5) + { + npth_t tid; + + info_msg ("creating thread-twoone"); + rc = npth_create (&tid, NULL, thread_twoone, NULL); + fail_if_err (rc); + npth_usleep (10); /* Give new thread some time to start. */ + } + + rc = npth_mutex_unlock (&counter_mutex); + fail_if_err (rc); + } + + info_msg ("busy waiting for thread twoone"); + while (!thread_twoone_ready) + npth_sleep (0); + + info_msg ("thread-two terminated"); + + return (void*)4722; +} + +int +main (int argc, char *argv[]) +{ + int rc; + npth_attr_t tattr; + int state; + npth_t tid1, tid2; + void *retval; + char bufname[BUFLEN]; + struct timespec tout; + + if (argc >= 2 && !strcmp (argv[1], "--verbose")) + opt_verbose = 1; + + rc = npth_init (); + fail_if_err (rc); + + rc = npth_mutex_init (&counter_mutex, NULL); + fail_if_err (rc); + + clock_gettime(CLOCK_REALTIME, &tout); + tout.tv_sec += 1; + rc = npth_mutex_timedlock (&counter_mutex, &tout); + npth_mutex_unlock (&counter_mutex); + + rc = npth_rwlock_init(&counter_rwlock, NULL); + fail_if_err (rc); + + npth_rwlock_rdlock(&counter_rwlock); + npth_rwlock_unlock(&counter_rwlock); + + npth_rwlock_wrlock(&counter_rwlock); + npth_rwlock_unlock(&counter_rwlock); + + rc = npth_attr_init (&tattr); + fail_if_err (rc); + rc = npth_attr_getdetachstate (&tattr, &state); + fail_if_err (rc); + if ( state != NPTH_CREATE_JOINABLE ) + fail_msg ("new tattr is not joinable"); + + info_msg ("creating thread-one"); + rc = npth_create (&tid1, &tattr, thread_one, NULL); + fail_if_err (rc); + npth_setname_np (tid1, "thread-one"); + npth_getname_np (tid1, bufname, BUFLEN); + + npth_rwlock_wrlock(&counter_rwlock); + npth_sleep(5); + npth_rwlock_unlock(&counter_rwlock); + + info_msg ("creating thread-two"); + rc = npth_create (&tid2, &tattr, thread_two, NULL); + fail_if_err (rc); + npth_setname_np (tid2, "thread-two"); + + rc = npth_attr_destroy (&tattr); + fail_if_err (rc); + + info_msg ("waiting for thread-one to terminate"); + rc = npth_join (tid1, &retval); + fail_if_err (rc); + if (retval != (void*)4711) + fail_msg ("thread-one returned an unexpected value"); + + info_msg ("waiting for thread-two to terminate"); + rc = npth_join (tid2, &retval); + fail_if_err (rc); + if (retval != (void*)4722) + fail_msg ("thread-two returned an unexpected value"); + + if (counter != 100) + fail_msg ("counter value not as expected"); + + return 0; +} diff --git a/tests/t-signal.c b/tests/t-signal.c new file mode 100644 index 0000000..45c7c97 --- /dev/null +++ b/tests/t-signal.c @@ -0,0 +1,49 @@ +/* t-signal.c + * + * This file is free software; as a special exception the author gives + * unlimited permission to copy and/or distribute it, with or without + * modifications, as long as this notice is preserved. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include "t-support.h" + +/* This is a test if nPth can allow daemon-like applications + initializing earlier. + + For daemon-like applications, ideally, it is expected to call + npth_init after fork. This condition is not satisfied sometimes. + + Failure of this test means nPth implementation doesn't allow + npth_init after fork. In such a case, application should be + modified. + */ + +int +main (int argc, const char *argv[]) +{ + int rc; + pid_t pid; + int r_signum; + + if (argc >= 2 && !strcmp (argv[1], "--verbose")) + opt_verbose = 1; + + rc = npth_init (); + fail_if_err (rc); + + npth_sigev_init(); + npth_sigev_add (5); + npth_sigev_add (7); + npth_sigev_fini (); + npth_sigev_sigmask (); + npth_sigev_get_pending (&r_signum); + + return 0; +} diff --git a/tests/t-socket.c b/tests/t-socket.c new file mode 100644 index 0000000..94ad0bb --- /dev/null +++ b/tests/t-socket.c @@ -0,0 +1,284 @@ +/* t-socket.c + * + * This file is free software; as a special exception the author gives + * unlimited permission to copy and/or distribute it, with or without + * modifications, as long as this notice is preserved. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include "t-support.h" +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <strings.h> + +#define NAME "Socket" +#define DATA "Hello, little Baby . . ." + +static int server1Ready; +static int server2Ready; + +static void * +thread_one (void *arg) +{ + int sock, msgsock, rval; + struct sockaddr_un server; + char buf[1024]; + struct timeval tv; + struct timespec ts; + + info_msg ("thread-one started"); + tv.tv_sec = 0; + tv.tv_usec = 100; + ts.tv_sec = 0; + ts.tv_nsec = 1000; + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + perror("opening stream socket"); + goto end; + } + server.sun_family = AF_UNIX; + strcpy(server.sun_path, NAME); + if (bind(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un))) { + perror("binding stream socket"); + goto end; + } + printf("Socket has name %s\n", server.sun_path); + listen(sock, 5); + server1Ready = 1; + msgsock = npth_accept(sock, 0, 0); + if (msgsock == -1) + perror("accept"); + else do { + bzero(buf, sizeof(buf)); + if ((rval = npth_read(msgsock, buf, 1024)) < 0) + perror("reading stream message"); + else if (rval == 0) + printf("Ending connection\n"); + else + printf("-->%s\n", buf); + } while (rval > 0); + + npth_select(0, NULL, NULL, NULL, &tv); + npth_pselect(0, NULL, NULL, NULL, &ts, NULL); + close(msgsock); + close(sock); + unlink(NAME); + info_msg ("thread-one terminated"); +end: + return (void*)4711; +} + + +static void * +thread_two (void *arg) +{ + + int sock; + struct sockaddr_un server; + char buf[1024]; + + info_msg ("thread-two started"); + if (!server1Ready) { + info_msg ("server1 not ready"); + goto end; + } + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + perror("opening stream socket"); + goto end; + } + server.sun_family = AF_UNIX; + strcpy(server.sun_path, NAME); + + if (npth_connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) { + close(sock); + perror("connecting stream socket"); + goto end; + } + if (npth_write(sock, DATA, sizeof(DATA)) < 0) + perror("writing on stream socket"); + close(sock); + + info_msg ("thread-two terminated"); + +end: + return (void*)4722; +} + +static void * +thread_three (void *arg) +{ + int sock, msgsock, rval; + struct sockaddr_un server; + char buf[1024]; + struct msghdr msg; + struct iovec io; + + msg.msg_name = NULL; + io.iov_base = buf; + io.iov_len = 1024; + msg.msg_iov = &io; + msg.msg_iovlen = 1; + + info_msg ("thread-three started"); + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + perror("opening stream socket"); + goto end; + } + server.sun_family = AF_UNIX; + strcpy(server.sun_path, NAME); + if (bind(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un))) { + perror("binding stream socket"); + goto end; + } + printf("Socket has name %s\n", server.sun_path); + listen(sock, 5); + server2Ready = 1; + msgsock = npth_accept(sock, 0, 0); + if (msgsock == -1) + perror("accept"); + else { + bzero(buf, sizeof(buf)); + ssize_t recv_size = npth_recvmsg(msgsock, &msg, 0); + char * temp = msg.msg_iov[0].iov_base; + temp[recv_size] = '\0'; + printf("get message:%s", temp); + }; + + close(msgsock); + close(sock); + unlink(NAME); + info_msg ("thread-three terminated"); +end: + return (void*)4711; +} + + +static void * +thread_four (void *arg) +{ + + int sock; + struct sockaddr_un server; + char buf[1024] = "test sendmsg and recvmsg\n"; + struct msghdr msg; + struct iovec io; + + msg.msg_name = NULL; + io.iov_base = buf; + io.iov_len = sizeof(buf); + msg.msg_iov = &io; + msg.msg_iovlen = 1; + + info_msg ("thread-four started"); + if (!server2Ready) { + info_msg ("server2 not ready"); + goto end; + } + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + perror("opening stream socket"); + goto end; + } + server.sun_family = AF_UNIX; + strcpy(server.sun_path, NAME); + + if (npth_connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) { + close(sock); + perror("connecting stream socket"); + goto end; + } + npth_sendmsg(sock, &msg, 0); + close(sock); + + info_msg ("thread-four terminated"); +end: + return (void*)4722; +} + +int +main (int argc, char *argv[]) +{ + int rc; + npth_attr_t tattr; + int state; + npth_t tid1, tid2; + npth_t tid3, tid4; + void *retval; + + if (argc >= 2 && !strcmp (argv[1], "--verbose")) + opt_verbose = 1; + + rc = npth_init (); + fail_if_err (rc); + + rc = npth_attr_init (&tattr); + fail_if_err (rc); + rc = npth_attr_getdetachstate (&tattr, &state); + fail_if_err (rc); + if ( state != NPTH_CREATE_JOINABLE ) + fail_msg ("new tattr is not joinable"); + + info_msg ("creating thread-one"); + rc = npth_create (&tid1, &tattr, thread_one, NULL); + fail_if_err (rc); + npth_setname_np (tid1, "thread-one"); + + npth_usleep(100); + + info_msg ("creating thread-two"); + rc = npth_create (&tid2, &tattr, thread_two, NULL); + fail_if_err (rc); + npth_setname_np (tid2, "thread-two"); + + rc = npth_attr_destroy (&tattr); + fail_if_err (rc); + + info_msg ("waiting for thread-one to terminate"); + rc = npth_join (tid1, &retval); + fail_if_err (rc); + if (retval != (void*)4711) + fail_msg ("thread-one returned an unexpected value"); + + info_msg ("waiting for thread-two to terminate"); + rc = npth_join (tid2, &retval); + fail_if_err (rc); + if (retval != (void*)4722) + fail_msg ("thread-two returned an unexpected value"); + + info_msg ("creating thread-three"); + rc = npth_create (&tid3, NULL, thread_three, NULL); + fail_if_err (rc); + npth_setname_np (tid3, "thread-three"); + + npth_usleep(100); + + info_msg ("creating thread-four"); + rc = npth_create (&tid4, NULL, thread_four, NULL); + fail_if_err (rc); + npth_setname_np (tid4, "thread-two"); + + info_msg ("waiting for thread-three to terminate"); + rc = npth_join (tid3, &retval); + fail_if_err (rc); + if (retval != (void*)4711) + fail_msg ("thread-three returned an unexpected value"); + + info_msg ("waiting for thread-four to terminate"); + rc = npth_join (tid4, &retval); + fail_if_err (rc); + if (retval != (void*)4722) + fail_msg ("thread-four returned an unexpected value"); + + return 0; +} -- 2.27.0
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.
浙ICP备2022010568号-2