summaryrefslogtreecommitdiffstats
path: root/tests/unittests/test_packets.c
blob: be2e57083aa5f66166545ad6f41cef8c0952eb9f (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
/*
 * This file is part of RTRlib.
 *
 * This file is subject to the terms and conditions of the MIT license.
 * See the file LICENSE in the top level directory for more details.
 *
 * Website: http://rtrlib.realmv6.org/
 */

#include "rtrlib_unittests.h"
#include "test_packets.h"

#include "rtrlib/rtr/packets_private.h"

int __wrap_tr_send_all(const struct tr_socket *socket, const void *pdu, const size_t len, const time_t timeout)
{
	UNUSED(socket);
	UNUSED(pdu);
	UNUSED(len);
	UNUSED(timeout);
	return (int)mock();
}

void __wrap_rtr_change_socket_state(struct rtr_socket *rtr_socket, const enum rtr_socket_state new_state)
{
	UNUSED(rtr_socket);
	UNUSED(new_state);
}

static void test_rtr_send_reset_query(void **state)
{
	struct rtr_socket socket;

	UNUSED(state);

	socket.connection_state_fp = NULL;

	will_return(__wrap_tr_send_all, 0);
	assert_int_equal(rtr_send_reset_query(&socket), RTR_ERROR);

	will_return(__wrap_tr_send_all, 10);
	assert_int_equal(rtr_send_reset_query(&socket), RTR_SUCCESS);
}

static void test_rtr_change_socket_state(void **state)
{
	struct rtr_socket socket;

	UNUSED(state);

	socket.connection_state_fp = NULL;
	socket.state = RTR_RESET;

	__real_rtr_change_socket_state(&socket, RTR_SYNC);
	assert_int_equal(socket.state, RTR_SYNC);

	__real_rtr_change_socket_state(&socket, RTR_SYNC);
	assert_int_equal(socket.state, RTR_SYNC);

	__real_rtr_change_socket_state(&socket, RTR_SHUTDOWN);
	assert_int_equal(socket.state, RTR_SHUTDOWN);

	__real_rtr_change_socket_state(&socket, RTR_ESTABLISHED);
	assert_int_equal(socket.state, RTR_SHUTDOWN);
}

int main(void)
{
	const struct CMUnitTest tests[] = {cmocka_unit_test(test_rtr_send_reset_query),
					   cmocka_unit_test(test_rtr_change_socket_state)};
	return cmocka_run_group_tests(tests, NULL, NULL);
}