raw: Run insufficient_file_descriptos function in child process.
[swifty.git] / src / raw / test / test_sw_socket.c
1 /*
2  * Test sw_socket "syscall".
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <errno.h>
13
14 #include "swift_raw.h"
15 #include "swift_types.h"
16
17 #include "debug.h"
18 #include "util.h"
19
20 #include "test_sw.h"
21 #include "test.h"
22
23 static void socket_dummy(void);
24 static void socket_invalid_domain(void);
25 static void socket_invalid_type(void);
26 static void socket_invalid_protocol(void);
27 static void socket_insufficient_file_descriptors(void);
28 static void socket_ok(void);
29
30 void socket_test_suite(void)
31 {
32         start_suite();
33         socket_dummy();
34         socket_invalid_domain();
35         socket_invalid_type();
36         socket_invalid_protocol();
37         run_as_child_process(socket_insufficient_file_descriptors);
38         socket_ok();
39 }
40
41 /* Dummy function for testing purposes only. */
42 static void socket_dummy(void)
43 {
44         test(1 == 1);
45 }
46
47 /* Use invalid domain when calling sw_socket. */
48 static void socket_invalid_domain(void)
49 {
50         int rc;
51
52         rc = sw_socket(PF_UNIX, SOCK_DGRAM, IPPROTO_SWIFT);
53
54         test(rc < 0 && errno == EINVAL);
55 }
56
57 /* Use invalid type when calling sw_socket. */
58 static void socket_invalid_type(void)
59 {
60         int rc;
61
62         rc = sw_socket(PF_INET, SOCK_STREAM, IPPROTO_SWIFT);
63
64         test(rc < 0 && errno == EINVAL);
65 }
66
67 /* Use invalid protocol when calling sw_socket. */
68 static void socket_invalid_protocol(void)
69 {
70         int rc;
71
72         rc = sw_socket(PF_INET, SOCK_DGRAM, -1);
73
74         test(rc < 0 && errno == EINVAL);
75 }
76
77 /*
78  * Use dup to fill the number of file descriptors for current process.
79  * Calling sw_socket must result in error.
80  *
81  * File descriptors are not closed. Test processes must be restarted.
82  */
83 static void socket_insufficient_file_descriptors(void)
84 {
85         int fd;
86         int rc;
87
88         while (1) {
89                 /* Duplicate standard output. */
90                 fd = dup(STDOUT_FILENO);
91                 if (fd < 0)
92                         break;
93         }
94
95         rc = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
96
97         dprintf("errno = %d\n", errno);
98         test(rc < 0 && errno == EMFILE);
99 }
100
101 /* Valid call of sw_socket. */
102 static void socket_ok(void)
103 {
104         int rc;
105
106         rc = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
107         dprintf("rc = %d, errno = %d\n", rc, errno);
108
109         test(rc > 0);
110 }