tests: Add a unit test for wrap_sendmsg_filter_cmsghdr()
authorRalph Boehme <slow@samba.org>
Tue, 13 Oct 2015 14:38:26 +0000 (16:38 +0200)
committerAndreas Schneider <asn@samba.org>
Wed, 14 Oct 2015 09:29:19 +0000 (11:29 +0200)
Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
tests/CMakeLists.txt
tests/test_swrap_unit.c [new file with mode: 0644]

index 0cb7f780c3b6d8ff9b91311cc0758a5f3878c1fd..96791772ea4bb9179e71f079d896fff36f6ccd41 100644 (file)
@@ -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 (file)
index 0000000..469aa24
--- /dev/null
@@ -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;
+}