diff options
author | Ralph Boehme <slow@samba.org> | 2015-10-13 16:38:26 +0200 |
---|---|---|
committer | Andreas Schneider <asn@samba.org> | 2015-10-14 11:29:19 +0200 |
commit | c5e809fed9a9e2cfce40312150322f28d3d338a6 (patch) | |
tree | 49f275acc7fd3e8c00ad677ba8d346435aa0bf85 | |
parent | 84a4e23728a558984f449610b9e84933acfb0515 (diff) | |
download | socket_wrapper-c5e809fed9a9e2cfce40312150322f28d3d338a6.tar.gz socket_wrapper-c5e809fed9a9e2cfce40312150322f28d3d338a6.tar.xz socket_wrapper-c5e809fed9a9e2cfce40312150322f28d3d338a6.zip |
tests: Add a unit test for wrap_sendmsg_filter_cmsghdr()
Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
-rw-r--r-- | tests/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/test_swrap_unit.c | 120 |
2 files changed, 122 insertions, 1 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0cb7f78..9679177 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,7 +30,8 @@ set(SWRAP_TESTS test_echo_tcp_get_peer_sock_name test_echo_udp_sendto_recvfrom test_echo_udp_send_recv - test_echo_udp_sendmsg_recvmsg) + test_echo_udp_sendmsg_recvmsg + test_swrap_unit) if (HAVE_STRUCT_MSGHDR_MSG_CONTROL) set(SWRAP_TESTS ${SWRAP_TESTS} test_sendmsg_recvmsg_fd) diff --git a/tests/test_swrap_unit.c b/tests/test_swrap_unit.c new file mode 100644 index 0000000..469aa24 --- /dev/null +++ b/tests/test_swrap_unit.c @@ -0,0 +1,120 @@ +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +#include <stdio.h> +#include <string.h> + +#include "config.h" + +#include "socket_wrapper.c" + +#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL + +/** + * test wrap_sendmsg_filter_cmsghdr() + * + * Prepare a message with two cmsg: + * - the first cmsg is a char buf with the string "Hello World" + * - the second cmsg is a char buf with the string "!\n" + * + * Both cmsgs will be copied without modification by + * wrap_sendmsg_filter_cmsghdr(), so we can check that the msg + * controllen, the cmsg sizes and the payload are the same. + * + * We use an not existing cmsg_type which triggers cmsg copying. + */ +static void test_sendmsg_cmsg(void **state) +{ + int rc = 0; + char byte = '!'; + struct iovec iov; + struct msghdr msg = { 0 }; + struct cmsghdr *cmsg; + char *cmsgbuf; + int cmsgbuf_size; + const char *s1 = "Hello World"; + const int s1_len = strlen(s1); + const char *s2 = "!\n"; + const int s2_len = strlen(s2); + uint8_t *cmbuf = NULL; + size_t cmlen = 0; + + (void)state; /* unused */ + + iov.iov_base = &byte; + iov.iov_len = 1; + + /* + * Prepare cmsgbuf and msg + */ + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + cmsgbuf_size = CMSG_SPACE(s1_len) + CMSG_SPACE(s2_len); + cmsgbuf = calloc(cmsgbuf_size, sizeof(char)); + assert_non_null(cmsgbuf); + msg.msg_control = cmsgbuf; + msg.msg_controllen = cmsgbuf_size; + + /* + * Prepare first cmsg with string "Hello World" + */ + cmsg = CMSG_FIRSTHDR(&msg); + assert_non_null(cmsg); + + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = ~0 - 1; + cmsg->cmsg_len = CMSG_LEN(s1_len); + memcpy(CMSG_DATA(cmsg), s1, s1_len); + + /* + * Prepare second cmsg with string "!\n" + */ + cmsg = CMSG_NXTHDR(&msg, cmsg); + assert_non_null(cmsg); + + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = ~0 - 2; + cmsg->cmsg_len = CMSG_LEN(s2_len); + memcpy(CMSG_DATA(cmsg), s2, s2_len); + + /* + * Now call swrap_sendmsg_filter_cmsghdr() on the msg + */ + rc = swrap_sendmsg_filter_cmsghdr(&msg, &cmbuf, &cmlen); + assert_return_code(rc, errno); + assert_int_equal(cmlen, msg.msg_controllen); + + /* + * Insert filtered cmsgbug into msg and validate cmsgs. + */ + msg.msg_control = cmbuf; + + cmsg = CMSG_FIRSTHDR(&msg); + assert_non_null(cmsg); + assert_int_equal(cmsg->cmsg_len, CMSG_LEN(s1_len)); + assert_memory_equal(CMSG_DATA(cmsg), s1, s1_len); + + cmsg = CMSG_NXTHDR(&msg, cmsg); + assert_non_null(cmsg); + assert_int_equal(cmsg->cmsg_len, CMSG_LEN(s2_len)); + assert_memory_equal(CMSG_DATA(cmsg), s2, s2_len); + + free(cmbuf); + free(cmsgbuf); +} +#endif + +int main(void) { + int rc; + + const struct CMUnitTest unit_tests[] = { +#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL + cmocka_unit_test(test_sendmsg_cmsg), +#endif + }; + + rc = cmocka_run_group_tests(unit_tests, NULL, NULL); + + return rc; +} |