test: add test for send_recv

Also ensure that `test/send_recvmsg.c` the recv thread finishs
before the process exits

Signed-off-by: Carter Li <carter.li@eoitek.com>
diff --git a/.gitignore b/.gitignore
index bdff558..fc0531e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,7 @@
 /test/d77a67ed5f27-test
 /test/defer
 /test/eeed8b54e0df-test
+/test/eventfd
 /test/fadvise
 /test/fallocate
 /test/fc2a85cb02ef-test
@@ -65,6 +66,7 @@
 /test/probe
 /test/read-write
 /test/ring-leak
+/test/send_recv
 /test/send_recvmsg
 /test/shared-wq
 /test/short-read
diff --git a/test/Makefile b/test/Makefile
index a311699..4ee68d2 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -19,7 +19,7 @@
 		poll-many b5837bd5311d-test accept-test d77a67ed5f27-test \
 		connect 7ad0e4b2f83c-test submit-reuse fallocate open-close \
 		file-update statx accept-reuse poll-v-poll fadvise madvise \
-		short-read openat2 probe shared-wq personality eventfd
+		short-read openat2 probe shared-wq personality eventfd send_recv
 
 include ../Makefile.quiet
 
@@ -52,6 +52,7 @@
 
 35fa71a030ca-test: XCFLAGS = -lpthread
 232c93d07b74-test: XCFLAGS = -lpthread
+send_recv: XCFLAGS = -lpthread
 send_recvmsg: XCFLAGS = -lpthread
 poll-link: XCFLAGS = -lpthread
 accept-link: XCFLAGS = -lpthread
diff --git a/test/send_recv.c b/test/send_recv.c
new file mode 100644
index 0000000..9f2c9a4
--- /dev/null
+++ b/test/send_recv.c
@@ -0,0 +1,193 @@
+/*
+ * Simple test case showing using send and recv through io_uring
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <pthread.h>
+
+#include "liburing.h"
+
+static char str[] = "This is a test of send and recv over io_uring!";
+
+#define MAX_MSG	128
+
+#define PORT	10200
+#define HOST	"127.0.0.1"
+
+#if 0
+#	define io_uring_prep_send io_uring_prep_write
+#	define io_uring_prep_recv io_uring_prep_read
+#endif
+
+static int recv_prep(struct io_uring *ring, struct iovec *iov)
+{
+	struct sockaddr_in saddr;
+	struct io_uring_sqe *sqe;
+	int sockfd, ret;
+
+	memset(&saddr, 0, sizeof(saddr));
+	saddr.sin_family = AF_INET;
+	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
+	saddr.sin_port = htons(PORT);
+
+	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+	if (sockfd < 0) {
+		perror("socket");
+		return 1;
+	}
+
+	ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
+	if (ret < 0) {
+		perror("bind");
+		goto err;
+	}
+
+	sqe = io_uring_get_sqe(ring);
+	io_uring_prep_recv(sqe, sockfd, iov->iov_base, iov->iov_len, 0);
+
+	ret = io_uring_submit(ring);
+	if (ret <= 0) {
+		printf("submit failed\n");
+		goto err;
+	}
+
+	return 0;
+err:
+	close(sockfd);
+	return 1;
+}
+
+static int do_recv(struct io_uring *ring, struct iovec *iov)
+{
+	struct io_uring_cqe *cqe;
+
+	io_uring_wait_cqe(ring, &cqe);
+	if (cqe->res < 0) {
+		printf("failed cqe: %d\n", cqe->res);
+		goto err;
+	}
+
+	if (cqe->res -1 != strlen(str)) {
+		printf("got wrong length\n");
+		goto err;
+	}
+
+	if (strcmp(str, iov->iov_base)) {
+		printf("string mismatch\n");
+		goto err;
+	}
+
+	return 0;
+err:
+	return 1;
+}
+
+static void *recv_fn(void *data)
+{
+	pthread_mutex_t *mutex = data;
+	char buf[MAX_MSG + 1];
+	struct iovec iov = {
+		.iov_base = buf,
+		.iov_len = sizeof(buf) - 1,
+	};
+	struct io_uring ring;
+	int ret;
+
+	io_uring_queue_init(1, &ring, 0);
+
+	recv_prep(&ring, &iov);
+	pthread_mutex_unlock(mutex);
+	ret = do_recv(&ring, &iov);
+
+	io_uring_queue_exit(&ring);
+	return (void *)(intptr_t)ret;
+}
+
+static int do_send(void)
+{
+	struct sockaddr_in saddr;
+	struct iovec iov = {
+		.iov_base = str,
+		.iov_len = sizeof(str),
+	};
+	struct io_uring ring;
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	int sockfd, ret;
+
+	ret = io_uring_queue_init(1, &ring, 0);
+	if (ret) {
+		printf("queue init fail\n");
+		return 1;
+	}
+
+	memset(&saddr, 0, sizeof(saddr));
+	saddr.sin_family = AF_INET;
+	saddr.sin_port = htons(PORT);
+	inet_pton(AF_INET, HOST, &saddr.sin_addr);
+
+	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+	if (sockfd < 0) {
+		perror("socket");
+		return 1;
+	}
+
+	ret = connect(sockfd, &saddr, sizeof(saddr));
+	if (ret < 0) {
+		perror("connect");
+		return 1;
+	}
+
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
+
+	ret = io_uring_submit(&ring);
+	if (ret <= 0) {
+		printf("submit failed\n");
+		goto err;
+	}
+
+	ret = io_uring_wait_cqe(&ring, &cqe);
+	if (cqe->res != iov.iov_len) {
+		printf("failed cqe: %d\n", cqe->res);
+		goto err;
+	}
+
+	close(sockfd);
+	return 0;
+err:
+	close(sockfd);
+	return 1;
+}
+
+int main(int argc, char *argv[])
+{
+	pthread_mutexattr_t attr;
+	pthread_t recv_thread;
+	pthread_mutex_t mutex;
+	int ret;
+	void *retval;
+
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_setpshared(&attr, 1);
+	pthread_mutex_init(&mutex, &attr);
+	pthread_mutex_lock(&mutex);
+
+	ret = pthread_create(&recv_thread, NULL, recv_fn, &mutex);
+	if (ret) {
+		fprintf(stderr, "Thread create failed\n");
+		return 1;
+	}
+
+	pthread_mutex_lock(&mutex);
+	do_send();
+	pthread_join(recv_thread, &retval);
+	ret = (int)(intptr_t)retval;
+
+	return ret;
+}
diff --git a/test/send_recvmsg.c b/test/send_recvmsg.c
index 9d220eb..484a4b7 100644
--- a/test/send_recvmsg.c
+++ b/test/send_recvmsg.c
@@ -97,15 +97,16 @@
 		.iov_len = sizeof(buf) - 1,
 	};
 	struct io_uring ring;
+	int ret;
 
 	io_uring_queue_init(1, &ring, 0);
 
 	recv_prep(&ring, &iov);
 	pthread_mutex_unlock(mutex);
-	do_recvmsg(&ring, &iov);
+	ret = do_recvmsg(&ring, &iov);
 
 	io_uring_queue_exit(&ring);
-	return NULL;
+	return (void *)(intptr_t)ret;
 }
 
 static int do_sendmsg(void)
@@ -172,6 +173,7 @@
 	pthread_t recv_thread;
 	pthread_mutex_t mutex;
 	int ret;
+	void *retval;
 
 	pthread_mutexattr_init(&attr);
 	pthread_mutexattr_setpshared(&attr, 1);
@@ -186,5 +188,8 @@
 
 	pthread_mutex_lock(&mutex);
 	do_sendmsg();
+	pthread_join(recv_thread, &retval);
+	ret = (int)(intptr_t)retval;
+
 	return ret;
 }