aboutsummaryrefslogtreecommitdiff
path: root/tests/test_echo_udp_sendto_recvfrom.c
blob: 354247af57c3b8b974f1eaa64f6dc7e6835b6d1c (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include "config.h"
#include "torture.h"

#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

static int setup_echo_srv_udp_ipv4(void **state)
{
	torture_setup_echo_srv_udp_ipv4(state);

	return 0;
}

#ifdef HAVE_IPV6
static int setup_echo_srv_udp_ipv6(void **state)
{
	torture_setup_echo_srv_udp_ipv6(state);

	return 0;
}
#endif

static int teardown(void **state)
{
	torture_teardown_echo_srv(state);

	return 0;
}

static void test_sendto_recvfrom_ipv4(void **state)
{
	struct torture_address addr = {
		.sa_socklen = sizeof(struct sockaddr_in),
	};
	char send_buf[64] = {0};
	char recv_buf[64] = {0};
	ssize_t ret;
	int rc;
	int i;
	int s;

	(void) state; /* unused */

	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	assert_int_not_equal(s, -1);

	addr.sa.in.sin_family = AF_INET;
	addr.sa.in.sin_port = htons(torture_server_port());

	rc = inet_pton(AF_INET,
		       torture_server_address(AF_INET),
		       &addr.sa.in.sin_addr);
	assert_int_equal(rc, 1);

	for (i = 0; i < 10; i++) {
		char ip[INET_ADDRSTRLEN] = {0};
		const char *a;
		struct torture_address srv_in = {
			.sa_socklen = sizeof(struct sockaddr_in),
		};

		snprintf(send_buf, sizeof(send_buf), "packet.%d", i);

		ret = sendto(s,
			     send_buf,
			     sizeof(send_buf),
			     0,
			     &addr.sa.s,
			     addr.sa_socklen);
		assert_return_code(ret, errno);

		ret = recvfrom(s,
			       recv_buf,
			       sizeof(recv_buf),
			       0,
			       &srv_in.sa.s,
			       &srv_in.sa_socklen);
		assert_return_code(ret, errno);

		a = inet_ntop(AF_INET, &srv_in.sa.in.sin_addr, ip, sizeof(ip));
		assert_non_null(a);
		assert_string_equal(a, torture_server_address(AF_INET));

		assert_memory_equal(send_buf, recv_buf, sizeof(send_buf));
	}

	ret = sendto(s,
		     send_buf,
		     sizeof(send_buf),
		     0,
		     &addr.sa.s,
		     addr.sa_socklen);
	assert_return_code(ret, errno);

	ret = recvfrom(s,
		       recv_buf,
		       sizeof(recv_buf),
		       0,
		       NULL,
		       NULL);
	assert_return_code(ret, errno);

	close(s);
}

#ifdef HAVE_IPV6
static void test_sendto_recvfrom_ipv6(void **state)
{
	struct torture_address addr = {
		.sa_socklen = sizeof(struct sockaddr_in6),
	};
	char send_buf[64] = {0};
	char recv_buf[64] = {0};
	ssize_t ret;
	int rc;
	int i;
	int s;

	(void) state; /* unused */

	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
	assert_int_not_equal(s, -1);

	addr.sa.in6.sin6_family = AF_INET6;
	addr.sa.in6.sin6_port = htons(torture_server_port());

	rc = inet_pton(AF_INET6,
		       torture_server_address(AF_INET6),
		       &addr.sa.in6.sin6_addr);
	assert_int_equal(rc, 1);

	for (i = 0; i < 10; i++) {
		char ip[INET6_ADDRSTRLEN] = {0};
		const char *a;
		struct torture_address srv_in6 = {
			.sa_socklen = sizeof(struct sockaddr_in6),
		};

		snprintf(send_buf, sizeof(send_buf), "packet.%d", i);

		ret = sendto(s,
			     send_buf,
			     sizeof(send_buf),
			     0,
			     &addr.sa.s,
			     addr.sa_socklen);
		assert_return_code(ret, errno);

		ret = recvfrom(s,
			       recv_buf,
			       sizeof(recv_buf),
			       0,
			       &srv_in6.sa.s,
			       &srv_in6.sa_socklen);
		assert_return_code(ret, errno);

		a = inet_ntop(AF_INET6, &srv_in6.sa.in6.sin6_addr, ip, sizeof(ip));
		assert_non_null(a);
		assert_string_equal(a, torture_server_address(AF_INET6));

		assert_memory_equal(send_buf, recv_buf, sizeof(send_buf));
	}

	ret = sendto(s,
		     send_buf,
		     sizeof(send_buf),
		     0,
		     &addr.sa.s,
		     addr.sa_socklen);
	assert_return_code(ret, errno);

	ret = recvfrom(s,
		       recv_buf,
		       sizeof(recv_buf),
		       0,
		       NULL,
		       NULL);
	assert_return_code(ret, errno);

	close(s);
}
#endif

static void test_connect_sendto_ipv4(void **state)
{
	struct torture_address addr = {
		.sa_socklen = sizeof(struct sockaddr_in),
	};
	char send_buf[] = "packet.0";
	char recv_buf[64] = {0};
	ssize_t ret;
	int rc;
	int s;

	(void) state; /* unused */

	addr.sa.in.sin_family = AF_INET;
	addr.sa.in.sin_port = htons(torture_server_port());

	rc = inet_pton(AF_INET,
		       torture_server_address(AF_INET),
		       &addr.sa.in.sin_addr);
	assert_int_equal(rc, 1);

	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	assert_int_not_equal(s, -1);

	/* Now, connect */
	rc = connect(s, &addr.sa.s, addr.sa_socklen);
	assert_return_code(rc, errno);

	ret = sendto(s,
		     send_buf,
		     sizeof(send_buf),
		     0,
		     &addr.sa.s,
		     addr.sa_socklen);
	assert_return_code(ret, errno);

	ret = recvfrom(s,
		       recv_buf,
		       sizeof(recv_buf),
		       0,
		       NULL,
		       0);
	assert_return_code(ret, errno);

	assert_memory_equal(send_buf, recv_buf, sizeof(send_buf));

	close(s);
}

static void test_connect_sendto_null_ipv4(void **state)
{
	struct torture_address addr = {
		.sa_socklen = sizeof(struct sockaddr_in),
	};
	char send_buf[] = "packet.0";
	char recv_buf[64] = {0};
	ssize_t ret;
	int rc;
	int s;

	(void) state; /* unused */

	addr.sa.in.sin_family = AF_INET;
	addr.sa.in.sin_port = htons(torture_server_port());

	rc = inet_pton(AF_INET,
		       torture_server_address(AF_INET),
		       &addr.sa.in.sin_addr);
	assert_int_equal(rc, 1);

	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	assert_int_not_equal(s, -1);

	/* Now, connect */
	rc = connect(s, &addr.sa.s, addr.sa_socklen);
	assert_return_code(rc, errno);

	ret = sendto(s,
		     send_buf,
		     sizeof(send_buf),
		     0,
		     NULL,
		     0);
	assert_return_code(ret, errno);

	ret = recvfrom(s,
		       recv_buf,
		       sizeof(recv_buf),
		       0,
		       NULL,
		       0);
	assert_return_code(ret, errno);

	assert_memory_equal(send_buf, recv_buf, sizeof(send_buf));

	close(s);
}

int main(void) {
	int rc;

	const struct CMUnitTest sendto_tests[] = {
		cmocka_unit_test_setup_teardown(test_sendto_recvfrom_ipv4,
						setup_echo_srv_udp_ipv4,
						teardown),
#ifdef HAVE_IPV6
		cmocka_unit_test_setup_teardown(test_sendto_recvfrom_ipv6,
						setup_echo_srv_udp_ipv6,
						teardown),
#endif
		cmocka_unit_test_setup_teardown(test_connect_sendto_ipv4,
						setup_echo_srv_udp_ipv4,
						teardown),
		cmocka_unit_test_setup_teardown(test_connect_sendto_null_ipv4,
						setup_echo_srv_udp_ipv4,
						teardown),
	};

	rc = cmocka_run_group_tests(sendto_tests, NULL, NULL);

	return rc;
}