summaryrefslogtreecommitdiffstats
path: root/tests/test_swrap_unit.c
blob: 469aa2475a4daa5dc7d1b58aef45b17ab7bb3459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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;
}