fd6adc21de6a8d2b241c30bcef869c673f145816
[socket_wrapper.git] / tests / test_tcp_dup2.c
1 #include "torture.h"
2
3 #include <cmocka.h>
4 #include <unistd.h>
5
6 static int setup(void **state)
7 {
8         torture_setup_socket_dir(state);
9
10         return 0;
11 }
12
13 static int teardown(void **state)
14 {
15         torture_teardown_socket_dir(state);
16
17         return 0;
18 }
19
20 static void test_dup2_existing_open_fd(void **state)
21 {
22         int s, dup_s;
23
24         (void) state; /* unused */
25
26         s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
27         assert_int_not_equal(s, -1);
28
29         /*
30          * Here we try to duplicate the existing socket fd to itself
31          * and as per man page for dup2() it must return the already
32          * open fd without any failure.
33          */
34         dup_s = dup2(s, s);
35         assert_int_equal(dup_s, s);
36
37         close(s);
38 }
39
40 int main(void) {
41         int rc;
42
43         const struct CMUnitTest tcp_dup2_tests[] = {
44                 cmocka_unit_test(test_dup2_existing_open_fd),
45         };
46
47         rc = cmocka_run_group_tests(tcp_dup2_tests, setup, teardown);
48
49         return rc;
50 }