summaryrefslogtreecommitdiffstats
path: root/tests/test_echo_tcp_socket.c
blob: 04f15aa107be86242bd31c6c49c36d0d381736e0 (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
#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 void test_socket_getsockname(void **state)
{
	struct sockaddr_in sin;
	socklen_t slen = sizeof(struct sockaddr_in);
	int rc;
	int s;

	(void) state; /* unused */

	s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	assert_int_not_equal(s, -1);

	ZERO_STRUCT(sin);
	rc = getsockname(s, (struct sockaddr *)&sin, &slen);
	assert_return_code(rc, errno);
	assert_int_equal(sin.sin_family, AF_INET);
}

#ifdef HAVE_IPV6
static void test_socket_getsockname6(void **state)
{
	struct sockaddr_in6 sin6;
	socklen_t slen = sizeof(struct sockaddr_in6);
	int rc;
	int s;

	(void) state; /* unused */

	s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	assert_int_not_equal(s, -1);

	ZERO_STRUCT(sin6);
	rc = getsockname(s, (struct sockaddr *)&sin6, &slen);
	assert_return_code(rc, errno);
	assert_int_equal(sin6.sin6_family, AF_INET6);
}
#endif

int main(void) {
	int rc;

	const UnitTest tests[] = {
		unit_test(test_socket_getsockname),
#ifdef HAVE_IPV6
		unit_test(test_socket_getsockname6),
#endif
	};

	rc = run_tests(tests);

	return rc;
}