From 17cfdc6a6568f0dbc34d863ad2d80acaed15a6ad Mon Sep 17 00:00:00 2001 From: Anoop C S Date: Mon, 22 Aug 2016 19:37:47 +0530 Subject: swrap: Add test case to validate oldfd = newfd case in dup2() Signed-off-by: Anoop C S Reviewed-by: Michael Adam Reviewed-by: Andreas Schneider --- tests/CMakeLists.txt | 1 + tests/test_tcp_dup2.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/test_tcp_dup2.c (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index aecf6b8..9b5c4bf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -21,6 +21,7 @@ target_link_libraries(${TORTURE_LIBRARY} set(SWRAP_TESTS test_ioctl test_tcp_listen + test_tcp_dup2 test_echo_tcp_socket test_echo_tcp_connect test_echo_tcp_bind diff --git a/tests/test_tcp_dup2.c b/tests/test_tcp_dup2.c new file mode 100644 index 0000000..fd6adc2 --- /dev/null +++ b/tests/test_tcp_dup2.c @@ -0,0 +1,50 @@ +#include "torture.h" + +#include +#include + +static int setup(void **state) +{ + torture_setup_socket_dir(state); + + return 0; +} + +static int teardown(void **state) +{ + torture_teardown_socket_dir(state); + + return 0; +} + +static void test_dup2_existing_open_fd(void **state) +{ + int s, dup_s; + + (void) state; /* unused */ + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + assert_int_not_equal(s, -1); + + /* + * Here we try to duplicate the existing socket fd to itself + * and as per man page for dup2() it must return the already + * open fd without any failure. + */ + dup_s = dup2(s, s); + assert_int_equal(dup_s, s); + + close(s); +} + +int main(void) { + int rc; + + const struct CMUnitTest tcp_dup2_tests[] = { + cmocka_unit_test(test_dup2_existing_open_fd), + }; + + rc = cmocka_run_group_tests(tcp_dup2_tests, setup, teardown); + + return rc; +} -- cgit