summaryrefslogtreecommitdiffstats
path: root/tests/test_echo_udp_sendto_recvfrom.c
blob: 9596e5eea5eda6e95566be21f0a24102db0d5bbb (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
#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 <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <signal.h>

#include <sys/stat.h>
#include <fcntl.h>

#include <unistd.h>

#define ECHO_SRV_IP "127.0.0.10"
#define ECHO_SRV_PORT 7

#define ECHO_SRV_PIDFILE "echo_srv.pid"

struct test_opts {
	char *socket_wrapper_dir;
	char *pidfile;
};

static void setup(void **state)
{
	char test_tmpdir[256];
	struct test_opts *o;
	size_t len;
	const char *p;

	o = malloc(sizeof(struct test_opts));
	assert_non_null(o);

	snprintf(test_tmpdir, sizeof(test_tmpdir), "/tmp/test_socket_wrapper_XXXXXX");

	p = mkdtemp(test_tmpdir);
	assert_non_null(p);

	o->socket_wrapper_dir = strdup(p);
	assert_non_null(o->socket_wrapper_dir);

	len = strlen(p) + 1 + strlen(ECHO_SRV_PIDFILE) + 1;

	o->pidfile = malloc(len);
	assert_non_null(o->pidfile);

	snprintf(o->pidfile, len, "%s/%s", p, ECHO_SRV_PIDFILE);

	setenv("SOCKET_WRAPPER_DIR", p, 1);
	setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "21", 1);

	*state = o;
}

static void setup_echo_srv_udp(void **state)
{
	struct test_opts *o;
	char start_echo_srv[1024] = {0};
	int rc;

	setup(state);
	o = *state;

	snprintf(start_echo_srv, sizeof(start_echo_srv),
		 "%s/tests/echo_srv -b %s -D -u --pid %s",
		 BINARYDIR, ECHO_SRV_IP, o->pidfile);

	rc = system(start_echo_srv);
	assert_int_equal(rc, 0);

	sleep(1);
}

static void teardown(void **state)
{
	struct test_opts *o = *state;
	char remove_cmd[1024] = {0};
	int rc;

	(void) state; /* unused */

	snprintf(remove_cmd, sizeof(remove_cmd), "rm -rf %s", o->socket_wrapper_dir);

	rc = system(remove_cmd);
	if (rc < 0) {
		fprintf(stderr, "%s failed: %s", remove_cmd, strerror(errno));
	}

	free(o->socket_wrapper_dir);
	free(o->pidfile);
	free(o);
}

static void teardown_echo_srv_udp(void **state)
{
	struct test_opts *o = *state;
	char buf[8] = {0};
	long int tmp;
	ssize_t rc;
	pid_t pid;
	int fd;

	/* read the pidfile */
	fd = open(o->pidfile, O_RDONLY);
	if (fd < 0) {
		goto done;
	}

	rc = read(fd, buf, sizeof(buf));
	close(fd);
	if (rc <= 0) {
		goto done;
	}

	buf[sizeof(buf) - 1] = '\0';

	tmp = strtol(buf, NULL, 10);
	if (tmp == 0 || tmp > 0xFFFF || errno == ERANGE) {
		goto done;
	}

	pid = (pid_t)(tmp & 0xFFFF);

	/* kill daemon */
	kill(pid, SIGTERM);

done:
	teardown(state);
}

static void test_sendto_recvfrom_ipv4(void **state)
{
	struct sockaddr_in sin;
	socklen_t slen = sizeof(struct sockaddr_in);
	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);

	ZERO_STRUCT(sin);
	sin.sin_family = AF_INET;
	sin.sin_port = htons(ECHO_SRV_PORT);

	rc = inet_aton(ECHO_SRV_IP, &sin.sin_addr);
	assert_int_equal(rc, 1);

	for (i = 0; i < 10; i++) {
		char send_buf[64] = {0};
		char recv_buf[64] = {0};
		struct sockaddr_in cli_in;
		socklen_t clen;

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

		ret = sendto(s,
			     send_buf,
			     sizeof(send_buf),
			     0,
			     (struct sockaddr *)(void *)&sin,
			     slen);
		assert_int_not_equal(ret, -1);

		ret = recvfrom(s,
			       recv_buf,
			       sizeof(recv_buf),
			       0,
			       (struct sockaddr *)&cli_in,
			       &clen);

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

int main(void) {
	int rc;

	const UnitTest tests[] = {
		unit_test_setup_teardown(test_sendto_recvfrom_ipv4, setup_echo_srv_udp, teardown_echo_srv_udp),
	};

	rc = run_tests(tests);

	return rc;
}