From 3bd067a13b95e907df4202d197baa2c2d668d230 Mon Sep 17 00:00:00 2001 From: Dmitrii Kuvaiskii Date: Mon, 27 May 2019 19:08:23 -0700 Subject: [PATCH] [LibOS/regression] Add test for Unix domain sockets This commit moves and modifies unix.c test from native/ to regression tests. This tests Unix domain sockets. --- LibOS/shim/test/native/manifest.template | 1 - LibOS/shim/test/regression/80_unix.py | 24 +++++ LibOS/shim/test/{native => regression}/unix.c | 89 +++++++++++-------- 3 files changed, 77 insertions(+), 37 deletions(-) create mode 100644 LibOS/shim/test/regression/80_unix.py rename LibOS/shim/test/{native => regression}/unix.c (65%) diff --git a/LibOS/shim/test/native/manifest.template b/LibOS/shim/test/native/manifest.template index be02e5f1..7ac339a5 100644 --- a/LibOS/shim/test/native/manifest.template +++ b/LibOS/shim/test/native/manifest.template @@ -26,5 +26,4 @@ sgx.trusted_files.libdl = file:$(LIBCDIR)/libdl.so.2 sgx.trusted_files.libm = file:$(LIBCDIR)/libm.so.6 sgx.trusted_files.libpthread = file:$(LIBCDIR)/libpthread.so.0 -sgx.trusted_files.unix_pipe = file:unix.c sgx.trusted_files.tcp_c = file:tcp.c diff --git a/LibOS/shim/test/regression/80_unix.py b/LibOS/shim/test/regression/80_unix.py new file mode 100644 index 00000000..37c277f3 --- /dev/null +++ b/LibOS/shim/test/regression/80_unix.py @@ -0,0 +1,24 @@ +import sys +from regression import Regression + +loader = sys.argv[1] + +# Running udp +regression = Regression(loader, "unix", None) + +regression.add_check(name="Unix domain socket", + check=lambda res: + "Data: This is packet 0" in res[0].out and + "Data: This is packet 1" in res[0].out and + "Data: This is packet 2" in res[0].out and + "Data: This is packet 3" in res[0].out and + "Data: This is packet 4" in res[0].out and + "Data: This is packet 5" in res[0].out and + "Data: This is packet 6" in res[0].out and + "Data: This is packet 7" in res[0].out and + "Data: This is packet 8" in res[0].out and + "Data: This is packet 9" in res[0].out) + +rv = regression.run_checks() +if rv: + sys.exit(rv) diff --git a/LibOS/shim/test/native/unix.c b/LibOS/shim/test/regression/unix.c similarity index 65% rename from LibOS/shim/test/native/unix.c rename to LibOS/shim/test/regression/unix.c index 63542a71..904fd73b 100644 --- a/LibOS/shim/test/native/unix.c +++ b/LibOS/shim/test/regression/unix.c @@ -1,7 +1,8 @@ -/* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */ -/* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */ - -/* copied from http://www.daniweb.com/software-development/c/threads/179814 */ +/* NOTE: Under Graphene, this test must be run only in fork mode. + * This is due to Graphene restricting communication via Unix + * domain sockets only for processes in same Graphene instance + * (i.e. only between parent and its child in this test). + */ #include #include @@ -14,21 +15,43 @@ #include #include -#define SRV_IP "127.0.0.1" -#define PORT 9930 -#define BUFLEN 512 -#define NPACK 10 - -const char * fname; - enum { SINGLE, PARALLEL } mode = PARALLEL; int do_fork = 0; int pipefds[2]; +int server_dummy_socket(void) +{ + int create_socket; + struct sockaddr_un address; + + if ((create_socket = socket(AF_UNIX,SOCK_STREAM, + 0)) > 0) + printf("Dummy socket was created\n"); + + address.sun_family = AF_UNIX; + strncpy(address.sun_path, "dummy", sizeof(address.sun_path)); + + if (bind(create_socket,(struct sockaddr *)&address, + sizeof(address)) < 0) { + perror("bind"); + close(create_socket); + exit(-1); + } + + if (listen(create_socket,3) < 0) { + perror("listen"); + close(create_socket); + exit(-1); + } + + /* do not close this socket to test two sockets in parallel */ + return 0; +} + int server(void) { - int conn,create_socket,new_socket,fd; + int create_socket,new_socket; socklen_t addrlen; int bufsize = 1024; char *buffer = malloc(bufsize); @@ -39,7 +62,7 @@ int server(void) printf("The socket was created\n"); address.sun_family = AF_UNIX; - memcpy(address.sun_path,"u",10); + strncpy(address.sun_path, "u", sizeof(address.sun_path)); if (bind(create_socket,(struct sockaddr *)&address, sizeof(address)) < 0) { @@ -83,17 +106,14 @@ int server(void) } } - if ((fd = open(fname,O_RDONLY,0)) < 0) { - perror("File Open Failed"); - close(new_socket); - exit(-1); + for (int i = 0; i < 10; i++) { + sprintf(buffer, "Data: This is packet %d\n", i); + if (sendto(new_socket, buffer, strlen(buffer), 0, 0, 0) == -1) { + fprintf(stderr, "sendto() failed\n"); + exit(-1); + } } - while((conn = read(fd,buffer, - bufsize)) > 0) - sendto(new_socket,buffer,conn,0,0,0); - - printf("Request completed\n"); close(new_socket); if (do_fork) @@ -118,7 +138,7 @@ int client(void) printf("The socket was created\n"); address.sun_family = AF_UNIX; - memcpy(address.sun_path,"u",10); + strncpy(address.sun_path, "u", sizeof(address.sun_path)); if (connect(create_socket,(struct sockaddr *)&address, sizeof(address)) == 0) @@ -136,13 +156,12 @@ int client(void) } } - printf("The contents of file are...\n\n"); - while((count=recv(create_socket,buffer,bufsize,0))>0) - write(1,buffer,count); + puts("Receiving:"); + while ((count = recv(create_socket,buffer,bufsize,0)) > 0) { + fwrite(buffer, count, 1, stdout); + } + puts("Done"); - printf("\nEOF\n"); - - buffer[0] = 0; close(create_socket); if (do_fork) exit(0); @@ -151,11 +170,6 @@ int client(void) int main(int argc, char ** argv) { - char fnamebuf[40]; - strcpy(fnamebuf, "unix"); - strcat(fnamebuf, ".c"); - fname = fnamebuf; - if (argc > 1) { if (strcmp(argv[1], "client") == 0) { mode = SINGLE; @@ -165,6 +179,7 @@ int main(int argc, char ** argv) if (strcmp(argv[1], "server") == 0) { mode = SINGLE; + server_dummy_socket(); server(); return 0; } @@ -180,10 +195,12 @@ old: int pid = fork(); - if (pid == 0) + if (pid == 0) { client(); - else + } else { + server_dummy_socket(); server(); + } } return 0;