raw: Add test suite for sw_socket "syscall".
[swifty.git] / src / raw / test / test_sw_socket.c
index 4292044..3b4b4d7 100644 (file)
@@ -2,6 +2,21 @@
  * Test sw_socket "syscall".
  */
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_raw.h"
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
 #include "test_sw.h"
 #include "test.h"
 
@@ -16,6 +31,11 @@ void socket_test_suite(void)
 {
        start_suite();
        socket_dummy();
+       socket_invalid_domain();
+       socket_invalid_type();
+       socket_invalid_protocol();
+       socket_insufficient_file_descriptors();
+       socket_ok();
 }
 
 /* Dummy function for testing purposes only. */
@@ -23,3 +43,68 @@ static void socket_dummy(void)
 {
        test(1 == 1);
 }
+
+/* Use invalid domain when calling sw_socket. */
+static void socket_invalid_domain(void)
+{
+       int rc;
+
+       rc = sw_socket(PF_UNIX, SOCK_DGRAM, IPPROTO_SWIFT);
+
+       test(rc < 0 && errno == EINVAL);
+}
+
+/* Use invalid type when calling sw_socket. */
+static void socket_invalid_type(void)
+{
+       int rc;
+
+       rc = sw_socket(PF_INET, SOCK_STREAM, IPPROTO_SWIFT);
+
+       test(rc < 0 && errno == EINVAL);
+}
+
+/* Use invalid protocol when calling sw_socket. */
+static void socket_invalid_protocol(void)
+{
+       int rc;
+
+       rc = sw_socket(PF_INET, SOCK_DGRAM, -1);
+
+       test(rc < 0 && errno == EINVAL);
+}
+
+/*
+ * Use dup to fill the number of file descriptors for current process.
+ * Calling sw_socket must result in error.
+ *
+ * File descriptors are not closed. Test processes must be restarted.
+ */
+static void socket_insufficient_file_descriptors(void)
+{
+       int fd;
+       int rc;
+
+       while (1) {
+               /* Duplicate standard output. */
+               fd = dup(STDOUT_FILENO);
+               if (fd < 0)
+                       break;
+       }
+
+       rc = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+
+       dprintf("errno = %d\n", errno);
+       test(rc < 0 && errno == EMFILE);
+}
+
+/* Valid call of sw_socket. */
+static void socket_ok(void)
+{
+       int rc;
+
+       rc = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+       dprintf("rc = %d, errno = %d\n", rc, errno);
+
+       test(rc > 0);
+}