summaryrefslogtreecommitdiffstats
path: root/tests/test_public_functions.c
blob: 816cd7d5bc09f255f92bab216fde3fb56620c51d (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
#include "torture.h"

#include <errno.h>
#include <stdio.h>
#include <cmocka.h>
#include <unistd.h>
#include <stdlib.h>

#include <socket_wrapper.h>

static int setup_enabled(void **state)
{
	torture_setup_socket_dir(state);

	return 0;
}

static int teardown_enabled(void **state)
{
	torture_teardown_socket_dir(state);

	return 0;
}

static int setup_disabled(void **state)
{
	(void) state; /* unused */

	unsetenv("SOCKET_WRAPPER_DIR");
	unsetenv("SOCKET_WRAPPER_DEFAULT_IFACE");
	unsetenv("SOCKET_WRAPPER_PCAP_FILE");

	return 0;
}

static int teardown_disabled(void **state)
{
	(void) state; /* unused */

	return 0;
}

static void test_call_enabled_true(void **state)
{
	char *s = getenv("SOCKET_WRAPPER_DIR");

	(void) state; /* unused */

	assert_true(socket_wrapper_enabled());
	assert_true(s != NULL);
}

static void test_call_enabled_false(void **state)
{
	char *s = getenv("SOCKET_WRAPPER_DIR");

	(void) state; /* unused */

	assert_false(socket_wrapper_enabled());
	assert_false(s != NULL);
}

static void test_call_indicate_no_inet_fd(void **state)
{
	int rc;
	int s = -1;

	(void) state; /* unused */

	socket_wrapper_indicate_no_inet_fd(987654321);
	socket_wrapper_indicate_no_inet_fd(-1);

	rc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (rc >= 0) {
		s = rc;
		rc = 0;
	}
	assert_return_code(rc, errno);

	socket_wrapper_indicate_no_inet_fd(987654321);
	socket_wrapper_indicate_no_inet_fd(-1);
	socket_wrapper_indicate_no_inet_fd(s);
	socket_wrapper_indicate_no_inet_fd(0);
	socket_wrapper_indicate_no_inet_fd(1);
	socket_wrapper_indicate_no_inet_fd(2);
}

int main(void) {
	int rc;

	const struct CMUnitTest max_sockets_tests[] = {
		cmocka_unit_test_setup_teardown(test_call_enabled_true,
						setup_enabled,
						teardown_enabled),
		cmocka_unit_test_setup_teardown(test_call_enabled_false,
						setup_disabled,
						teardown_disabled),
		cmocka_unit_test_setup_teardown(test_call_indicate_no_inet_fd,
						setup_enabled,
						teardown_enabled),
		cmocka_unit_test_setup_teardown(test_call_indicate_no_inet_fd,
						setup_disabled,
						teardown_disabled),
	};

	rc = cmocka_run_group_tests(max_sockets_tests, NULL, NULL);

	return rc;
}