aboutgitcodelistschat:MatrixIRC
diff options
context:
space:
mode:
authorAlice Frosi <afrosi@redhat.com>2022-12-21 15:17:46 +0100
committerAlice Frosi <afrosi@redhat.com>2023-02-15 11:31:46 +0100
commitb47cde4553f31b033cd3d096552c0a5646cdc88d (patch)
treeacbef878efcfdf3c9a3847db25705b7cd0ec0d1d
parent4d80c91ea37b6903ca6d5cdb788db6b2f0b44b09 (diff)
downloadseitan-b47cde4553f31b033cd3d096552c0a5646cdc88d.tar
seitan-b47cde4553f31b033cd3d096552c0a5646cdc88d.tar.gz
seitan-b47cde4553f31b033cd3d096552c0a5646cdc88d.tar.bz2
seitan-b47cde4553f31b033cd3d096552c0a5646cdc88d.tar.lz
seitan-b47cde4553f31b033cd3d096552c0a5646cdc88d.tar.xz
seitan-b47cde4553f31b033cd3d096552c0a5646cdc88d.tar.zst
seitan-b47cde4553f31b033cd3d096552c0a5646cdc88d.zip
Create test utils for testing different syscall
For now, just testing the connect syscalls with a client/server small test program. Signed-off-by: Alice Frosi <afrosi@redhat.com>
-rw-r--r--tests-utils/Makefile3
-rw-r--r--tests-utils/test-server.c46
-rw-r--r--tests-utils/test-syscalls.c47
3 files changed, 96 insertions, 0 deletions
diff --git a/tests-utils/Makefile b/tests-utils/Makefile
new file mode 100644
index 0000000..c234972
--- /dev/null
+++ b/tests-utils/Makefile
@@ -0,0 +1,3 @@
+test:
+ $(CC) $(CFLAGS) -o test-syscalls test-syscalls.c
+ $(CC) $(CFLAGS) -o test-server test-server.c
diff --git a/tests-utils/test-server.c b/tests-utils/test-server.c
new file mode 100644
index 0000000..811caf4
--- /dev/null
+++ b/tests-utils/test-server.c
@@ -0,0 +1,46 @@
+#include <sys/un.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+ struct sockaddr_un addr;
+ int fd, connfd, n, addrlen;
+ char sock_path[] = "/tmp/test.sock";
+ char buffer[256];
+ unlink(sock_path);
+ addrlen = sizeof(addr);
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ perror("creating socket");
+ return 1;
+ }
+ memset(&addr, 0, sizeof(addr));
+ strcpy(addr.sun_path, sock_path);
+ addr.sun_family = AF_UNIX;
+ if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ perror("binding");
+ exit(EXIT_FAILURE);
+ }
+
+ listen(fd, 5);
+ if ((connfd = accept(fd, (struct sockaddr *)&addr,
+ (socklen_t *)&addrlen)) < 0) {
+ perror("ERROR on accept");
+ exit(EXIT_FAILURE);
+ }
+
+ bzero(buffer, 256);
+ n = read(connfd, buffer, 255);
+
+ if (n < 0) {
+ perror("ERROR reading from socket");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("%s\n", buffer);
+ unlink(sock_path);
+ close(fd);
+ return 0;
+}
diff --git a/tests-utils/test-syscalls.c b/tests-utils/test-syscalls.c
new file mode 100644
index 0000000..d017df5
--- /dev/null
+++ b/tests-utils/test-syscalls.c
@@ -0,0 +1,47 @@
+#include <sys/un.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static int test_connect()
+{
+ struct sockaddr_un addr;
+ char data[256] = "test connection";
+ int fd;
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ perror("creating socket");
+ return 1;
+ }
+ memset(&addr, 0, sizeof(addr));
+ strcpy(addr.sun_path, "/tmp/test.sock");
+ addr.sun_family = AF_UNIX;
+ if (connect(fd, (struct sockaddr *)&addr, sizeof(addr.sun_path)) < 0) {
+ perror("connect error");
+ return 1;
+ }
+ if (write(fd, data, strlen(data) + 1) < 0) {
+ perror("writing data");
+ return 1;
+ }
+ close(fd);
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int ret;
+ if (argc < 2) {
+ perror("missing syscall to test");
+ exit(EXIT_FAILURE);
+ }
+ printf("Test syscall: %s \n", argv[1]);
+ if (strcmp(argv[1], "connect") == 0)
+ ret = test_connect();
+ else {
+ perror("syscall not supported");
+ exit(EXIT_FAILURE);
+ }
+ if (ret != 0)
+ exit(EXIT_FAILURE);
+}