diff options
Diffstat (limited to 'tests-utils')
-rw-r--r-- | tests-utils/Makefile | 3 | ||||
-rw-r--r-- | tests-utils/test-server.c | 46 | ||||
-rw-r--r-- | tests-utils/test-syscalls.c | 47 |
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); +} |