cmake: Export cmake config for socket_wrapper_noop
[socket_wrapper.git] / tests / test_public_functions.c
1 #include "torture.h"
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <cmocka.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8
9 #include <socket_wrapper.h>
10
11 static int setup_enabled(void **state)
12 {
13         torture_setup_socket_dir(state);
14
15         return 0;
16 }
17
18 static int teardown_enabled(void **state)
19 {
20         torture_teardown_socket_dir(state);
21
22         return 0;
23 }
24
25 static int setup_disabled(void **state)
26 {
27         (void) state; /* unused */
28
29         unsetenv("SOCKET_WRAPPER_DIR");
30         unsetenv("SOCKET_WRAPPER_DEFAULT_IFACE");
31         unsetenv("SOCKET_WRAPPER_PCAP_FILE");
32
33         return 0;
34 }
35
36 static int teardown_disabled(void **state)
37 {
38         (void) state; /* unused */
39
40         return 0;
41 }
42
43 static void test_call_enabled_true(void **state)
44 {
45         char *s = getenv("SOCKET_WRAPPER_DIR");
46
47         (void) state; /* unused */
48
49         assert_true(socket_wrapper_enabled());
50         assert_true(s != NULL);
51 }
52
53 static void test_call_enabled_false(void **state)
54 {
55         char *s = getenv("SOCKET_WRAPPER_DIR");
56
57         (void) state; /* unused */
58
59         assert_false(socket_wrapper_enabled());
60         assert_false(s != NULL);
61 }
62
63 static void test_call_indicate_no_inet_fd(void **state)
64 {
65         int rc;
66         int s = -1;
67
68         (void) state; /* unused */
69
70         socket_wrapper_indicate_no_inet_fd(987654321);
71         socket_wrapper_indicate_no_inet_fd(-1);
72
73         rc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
74         if (rc >= 0) {
75                 s = rc;
76                 rc = 0;
77         }
78         assert_return_code(rc, errno);
79
80         socket_wrapper_indicate_no_inet_fd(987654321);
81         socket_wrapper_indicate_no_inet_fd(-1);
82         socket_wrapper_indicate_no_inet_fd(s);
83         socket_wrapper_indicate_no_inet_fd(0);
84         socket_wrapper_indicate_no_inet_fd(1);
85         socket_wrapper_indicate_no_inet_fd(2);
86 }
87
88 int main(void) {
89         int rc;
90
91         const struct CMUnitTest max_sockets_tests[] = {
92                 cmocka_unit_test_setup_teardown(test_call_enabled_true,
93                                                 setup_enabled,
94                                                 teardown_enabled),
95                 cmocka_unit_test_setup_teardown(test_call_enabled_false,
96                                                 setup_disabled,
97                                                 teardown_disabled),
98                 cmocka_unit_test_setup_teardown(test_call_indicate_no_inet_fd,
99                                                 setup_enabled,
100                                                 teardown_enabled),
101                 cmocka_unit_test_setup_teardown(test_call_indicate_no_inet_fd,
102                                                 setup_disabled,
103                                                 teardown_disabled),
104         };
105
106         rc = cmocka_run_group_tests(max_sockets_tests, NULL, NULL);
107
108         return rc;
109 }