summaryrefslogtreecommitdiff
path: root/src/libcharon/tests/utils/exchange_test_asserts.c
blob: 2602b97b74adc5fda4c04808ed5ed8f080526328 (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
/*
 * Copyright (C) 2016 Tobias Brunner
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <inttypes.h>

#include <test_suite.h>

#include "exchange_test_asserts.h"

/*
 * Described in header
 */
bool exchange_test_asserts_hook(listener_t *listener)
{
	listener_hook_assert_t *this = (listener_hook_assert_t*)listener;

	this->count++;
	return TRUE;
}

/*
 * Described in header
 */
bool exchange_test_asserts_ike_updown(listener_t *listener, ike_sa_t *ike_sa,
									  bool up)
{
	listener_hook_assert_t *this = (listener_hook_assert_t*)listener;

	this->count++;
	assert_listener_msg(this->up == up, this, "IKE_SA not '%s'",
						this->up ? "up" : "down");
	return TRUE;
}

/*
 * Described in header
 */
bool exchange_test_asserts_child_updown(listener_t *listener, ike_sa_t *ike_sa,
										child_sa_t *child_sa, bool up)
{
	listener_hook_assert_t *this = (listener_hook_assert_t*)listener;

	this->count++;
	assert_listener_msg(this->up == up, this, "CHILD_SA not '%s'",
						this->up ? "up" : "down");
	return TRUE;
}

/*
 * Described in header
 */
bool exchange_test_asserts_ike_rekey(listener_t *listener, ike_sa_t *old,
									 ike_sa_t *new)
{
	listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
	ike_sa_id_t *id;
	uint64_t spi;

	this->count++;
	id = old->get_id(old);
	spi = id->get_initiator_spi(id);
	assert_listener_msg(this->spi_old == spi, this, "unexpected old IKE_SA "
						"%.16"PRIx64"_i instead of %.16"PRIx64"_i",
						be64toh(spi), be64toh(this->spi_old));
	id = new->get_id(new);
	spi = id->get_initiator_spi(id);
	assert_listener_msg(this->spi_new == spi, this, "unexpected new IKE_SA "
						"%.16"PRIx64"_i instead of %.16"PRIx64"_i",
						be64toh(spi), be64toh(this->spi_new));
	return TRUE;
}

/*
 * Described in header
 */
bool exchange_test_asserts_child_rekey(listener_t *listener, ike_sa_t *ike_sa,
									   child_sa_t *old, child_sa_t *new)
{
	listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
	uint32_t spi, expected;

	this->count++;
	spi = old->get_spi(old, TRUE);
	expected = this->spi_old;
	assert_listener_msg(expected == spi, this, "unexpected old CHILD_SA %.8x "
						"instead of %.8x", spi, expected);
	spi = new->get_spi(new, TRUE);
	expected = this->spi_new;
	assert_listener_msg(expected == spi, this, "unexpected new CHILD_SA %.8x "
						"instead of %.8x", spi, expected);
	return TRUE;
}

/**
 * Assert a given message rule
 */
static void assert_message_rule(listener_message_assert_t *this, message_t *msg,
								listener_message_rule_t *rule)
{
	if (rule->expected)
	{
		if (rule->payload)
		{
			assert_listener_msg(msg->get_payload(msg, rule->payload),
								this, "expected payload (%N) not found",
								payload_type_names, rule->payload);

		}
		if (rule->notify)
		{
			assert_listener_msg(msg->get_notify(msg, rule->notify),
								this, "expected notify payload (%N) not found",
								notify_type_names, rule->notify);
		}
	}
	else
	{
		if (rule->payload)
		{
			assert_listener_msg(!msg->get_payload(msg, rule->payload),
								this, "unexpected payload (%N) found",
								payload_type_names, rule->payload);

		}
		if (rule->notify)
		{
			assert_listener_msg(!msg->get_notify(msg, rule->notify),
								this, "unexpected notify payload (%N) found",
								notify_type_names, rule->notify);
		}
	}
}

/*
 * Described in header
 */
bool exchange_test_asserts_message(listener_t *listener, ike_sa_t *ike_sa,
								message_t *message, bool incoming, bool plain)
{
	listener_message_assert_t *this = (listener_message_assert_t*)listener;

	if (plain && this->incoming == incoming)
	{
		if (this->count >= 0)
		{
			enumerator_t *enumerator;
			int count = 0;
			enumerator = message->create_payload_enumerator(message);
			while (enumerator->enumerate(enumerator, NULL))
			{
				count++;
			}
			enumerator->destroy(enumerator);
			assert_listener_msg(this->count == count, this, "unexpected payload "
								"count in message (%d != %d)", this->count,
								count);
		}
		if (this->num_rules)
		{
			int i;

			for (i = 0; i < this->num_rules; i++)
			{
				assert_message_rule(this, message, &this->rules[i]);
			}
		}
		return FALSE;
	}
	return TRUE;
}