[PATCH] Add npth socket test case
yaowenbin
yaowenbin1 at huawei.com
Tue Mar 22 10:09:46 CET 2022
* tests/t-socket.c: New
Signed-off-by: yaowenbin1 <yaowenbin1 at huawei.com>
---
tests/t-socket.c | 270 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 270 insertions(+)
create mode 100644 tests/t-socket.c
diff --git a/tests/t-socket.c b/tests/t-socket.c
new file mode 100644
index 0000000..8e980d4
--- /dev/null
+++ b/tests/t-socket.c
@@ -0,0 +1,270 @@
+/* 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 "Test npth socket interfaces. . ."
+
+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");
+ exit(1);
+ }
+ server.sun_family = AF_UNIX;
+ strcpy(server.sun_path, NAME);
+ if (bind(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un))) {
+ perror("binding stream socket");
+ exit(1);
+ }
+ info_msg("Socket has name %s\n", server.sun_path);
+ listen(sock, 5);
+ 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)
+ info_msg("Ending connection\n");
+ else
+ info_msg("-->%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");
+
+ return (void*)4711;
+}
+
+
+static void *
+thread_two (void *arg)
+{
+
+ int sock;
+ struct sockaddr_un server;
+ char buf[1024];
+
+ info_msg ("thread-two started");
+ sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sock < 0) {
+ perror("opening stream socket");
+ exit(1);
+ }
+ 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");
+ exit(1);
+ }
+ if (npth_write(sock, DATA, sizeof(DATA)) < 0)
+ perror("writing on stream socket");
+ close(sock);
+
+ info_msg ("thread-two terminated");
+
+ 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");
+ exit(1);
+ }
+ server.sun_family = AF_UNIX;
+ strcpy(server.sun_path, NAME);
+ if (bind(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un))) {
+ perror("binding stream socket");
+ exit(1);
+ }
+ info_msg("Socket has name %s\n", server.sun_path);
+ listen(sock, 5);
+ 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';
+ info_msg("get message:%s", temp);
+ };
+
+ close(msgsock);
+ close(sock);
+ unlink(NAME);
+ info_msg ("thread-three terminated");
+
+ 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");
+ sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sock < 0) {
+ perror("opening stream socket");
+ exit(1);
+ }
+ 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");
+ exit(1);
+ }
+ npth_sendmsg(sock, &msg, 0);
+ close(sock);
+
+ info_msg ("thread-four terminated");
+
+ 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
More information about the Gnupg-users
mailing list