summaryrefslogtreecommitdiff
path: root/src/platform/tests/test-address.c
blob: 3c62e5513919fa3e14c5db7daef8177d58fffe9d (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "config.h"

#include "test-common.h"

#define DEVICE_NAME "nm-test-device"
#define IP4_ADDRESS "192.0.2.1"
#define IP4_ADDRESS_PEER "192.0.2.2"
#define IP4_ADDRESS_PEER2 "192.0.3.1"
#define IP4_PLEN 24
#define IP6_ADDRESS "2001:db8:a:b:1:2:3:4"
#define IP6_PLEN 64

static int DEVICE_IFINDEX = -1;

static void
ip4_address_callback (NMPlatform *platform, NMPObjectType obj_type, int ifindex, NMPlatformIP4Address *received, NMPlatformSignalChangeType change_type, NMPlatformReason reason, SignalData *data)
{
	g_assert (received);
	g_assert_cmpint (received->ifindex, ==, ifindex);
	g_assert (data && data->name);
	g_assert_cmpstr (data->name, ==, NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED);

	if (data->ifindex && data->ifindex != received->ifindex)
		return;
	if (data->change_type != change_type)
		return;

	if (data->loop)
		g_main_loop_quit (data->loop);

	data->received_count++;
	_LOGD ("Received signal '%s' %dth time.", data->name, data->received_count);
}

static void
ip6_address_callback (NMPlatform *platform, NMPObjectType obj_type, int ifindex, NMPlatformIP6Address *received, NMPlatformSignalChangeType change_type, NMPlatformReason reason, SignalData *data)
{
	g_assert (received);
	g_assert_cmpint (received->ifindex, ==, ifindex);
	g_assert (data && data->name);
	g_assert_cmpstr (data->name, ==, NM_PLATFORM_SIGNAL_IP6_ADDRESS_CHANGED);

	if (data->ifindex && data->ifindex != received->ifindex)
		return;
	if (data->change_type != change_type)
		return;

	if (data->loop)
		g_main_loop_quit (data->loop);

	data->received_count++;
	_LOGD ("Received signal '%s' %dth time.", data->name, data->received_count);
}

static void
test_ip4_address (void)
{
	const int ifindex = DEVICE_IFINDEX;
	SignalData *address_added = add_signal_ifindex (NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_ADDED, ip4_address_callback, ifindex);
	SignalData *address_changed = add_signal_ifindex (NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_CHANGED, ip4_address_callback, ifindex);
	SignalData *address_removed = add_signal_ifindex (NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, ip4_address_callback, ifindex);
	GArray *addresses;
	NMPlatformIP4Address *address;
	in_addr_t addr;
	guint32 lifetime = 2000;
	guint32 preferred = 1000;

	inet_pton (AF_INET, IP4_ADDRESS, &addr);

	/* Add address */
	g_assert (!nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0));
	g_assert (nm_platform_ip4_address_add (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0, lifetime, preferred, NULL));
	g_assert (nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0));
	accept_signal (address_added);

	/* Add address again (aka update) */
	g_assert (nm_platform_ip4_address_add (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0, lifetime, preferred, NULL));
	accept_signals (address_changed, 0, 1);

	/* Test address listing */
	addresses = nm_platform_ip4_address_get_all (NM_PLATFORM_GET, ifindex);
	g_assert (addresses);
	g_assert_cmpint (addresses->len, ==, 1);
	address = &g_array_index (addresses, NMPlatformIP4Address, 0);
	g_assert_cmpint (address->ifindex, ==, ifindex);
	g_assert_cmphex (address->address, ==, addr);
	g_assert_cmpint (address->plen, ==, IP4_PLEN);
	g_array_unref (addresses);

	/* Remove address */
	g_assert (nm_platform_ip4_address_delete (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0));
	g_assert (!nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0));
	accept_signal (address_removed);

	/* Remove address again */
	g_assert (nm_platform_ip4_address_delete (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0));

	free_signal (address_added);
	free_signal (address_changed);
	free_signal (address_removed);
}

static void
test_ip6_address (void)
{
	const int ifindex = DEVICE_IFINDEX;
	SignalData *address_added = add_signal_ifindex (NM_PLATFORM_SIGNAL_IP6_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_ADDED, ip6_address_callback, ifindex);
	SignalData *address_changed = add_signal_ifindex (NM_PLATFORM_SIGNAL_IP6_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_CHANGED, ip6_address_callback, ifindex);
	SignalData *address_removed = add_signal_ifindex (NM_PLATFORM_SIGNAL_IP6_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, ip6_address_callback, ifindex);
	GArray *addresses;
	NMPlatformIP6Address *address;
	struct in6_addr addr;
	guint32 lifetime = 2000;
	guint32 preferred = 1000;
	guint flags = 0;

	inet_pton (AF_INET6, IP6_ADDRESS, &addr);

	/* Add address */
	g_assert (!nm_platform_ip6_address_get (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));
	g_assert (nm_platform_ip6_address_add (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN, in6addr_any, lifetime, preferred, flags));
	g_assert (nm_platform_ip6_address_get (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));
	accept_signal (address_added);

	/* Add address again (aka update) */
	g_assert (nm_platform_ip6_address_add (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN, in6addr_any, lifetime, preferred, flags));
	accept_signals (address_changed, 0, 1);

	/* Test address listing */
	addresses = nm_platform_ip6_address_get_all (NM_PLATFORM_GET, ifindex);
	g_assert (addresses);
	g_assert_cmpint (addresses->len, ==, 1);
	address = &g_array_index (addresses, NMPlatformIP6Address, 0);
	g_assert_cmpint (address->ifindex, ==, ifindex);
	g_assert (!memcmp (&address->address, &addr, sizeof (addr)));
	g_assert_cmpint (address->plen, ==, IP6_PLEN);
	g_array_unref (addresses);

	/* Remove address */
	g_assert (nm_platform_ip6_address_delete (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));
	g_assert (!nm_platform_ip6_address_get (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));
	accept_signal (address_removed);

	/* Remove address again */
	g_assert (nm_platform_ip6_address_delete (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));

	free_signal (address_added);
	free_signal (address_changed);
	free_signal (address_removed);
}

static void
test_ip4_address_external (void)
{
	const int ifindex = DEVICE_IFINDEX;
	SignalData *address_added = add_signal (NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_ADDED, ip4_address_callback);
	SignalData *address_removed = add_signal (NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, ip4_address_callback);
	in_addr_t addr;
	guint32 lifetime = 2000;
	guint32 preferred = 1000;

	inet_pton (AF_INET, IP4_ADDRESS, &addr);
	g_assert (ifindex > 0);

	/* Looks like addresses are not announced by kerenl when the interface
	 * is down. Link-local IPv6 address is automatically added.
	 */
	g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, DEVICE_IFINDEX, NULL));

	/* Add/delete notification */
	nmtstp_ip4_address_add (-1, ifindex, addr, IP4_PLEN, 0, lifetime, preferred, NULL);
	accept_signal (address_added);
	g_assert (nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0));
	nmtstp_ip4_address_del (-1, ifindex, addr, IP4_PLEN, 0);
	accept_signal (address_removed);
	g_assert (!nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0));

	/* Add/delete conflict */
	nmtstp_ip4_address_add (-1, ifindex, addr, IP4_PLEN, 0, lifetime, preferred, NULL);
	g_assert (nm_platform_ip4_address_add (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0, lifetime, preferred, NULL));
	g_assert (nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, 0));
	accept_signal (address_added);

	free_signal (address_added);
	free_signal (address_removed);
}

static void
test_ip6_address_external (void)
{
	const int ifindex = DEVICE_IFINDEX;
	SignalData *address_added = add_signal (NM_PLATFORM_SIGNAL_IP6_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_ADDED, ip6_address_callback);
	SignalData *address_removed = add_signal (NM_PLATFORM_SIGNAL_IP6_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, ip6_address_callback);
	struct in6_addr addr;
	guint32 lifetime = 2000;
	guint32 preferred = 1000;
	guint flags = 0;

	inet_pton (AF_INET6, IP6_ADDRESS, &addr);

	/* Add/delete notification */
	nmtstp_ip6_address_add (-1, ifindex, addr, IP6_PLEN, in6addr_any, lifetime, preferred, 0);
	accept_signal (address_added);
	g_assert (nm_platform_ip6_address_get (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));

	nmtstp_ip6_address_del (-1, ifindex, addr, IP6_PLEN);
	accept_signal (address_removed);
	g_assert (!nm_platform_ip6_address_get (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));

	/* Add/delete conflict */
	nmtstp_ip6_address_add (-1, ifindex, addr, IP6_PLEN, in6addr_any, lifetime, preferred, 0);
	accept_signal (address_added);
	g_assert (nm_platform_ip6_address_get (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));

	g_assert (nm_platform_ip6_address_add (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN, in6addr_any, lifetime, preferred, flags));
	ensure_no_signal (address_added);
	g_assert (nm_platform_ip6_address_get (NM_PLATFORM_GET, ifindex, addr, IP6_PLEN));

	free_signal (address_added);
	free_signal (address_removed);
}

/*****************************************************************************/

static void
test_ip4_address_external_peer (void)
{
	const int ifindex = DEVICE_IFINDEX;
	SignalData *address_added = add_signal (NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_ADDED, ip4_address_callback);
	SignalData *address_removed = add_signal (NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, ip4_address_callback);
	in_addr_t addr, addr_peer, addr_peer2;
	guint32 lifetime = 2000;
	guint32 preferred = 1000;
	const NMPlatformIP4Address *a;

	inet_pton (AF_INET, IP4_ADDRESS, &addr);
	inet_pton (AF_INET, IP4_ADDRESS_PEER, &addr_peer);
	inet_pton (AF_INET, IP4_ADDRESS_PEER2, &addr_peer2);
	g_assert (ifindex > 0);

	g_assert (addr != addr_peer);

	g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, ifindex, NULL));
	accept_signals (address_removed, 0, G_MAXINT);
	accept_signals (address_added, 0, G_MAXINT);

	/* Add/delete notification */
	nmtstp_ip4_address_add (-1, ifindex, addr, IP4_PLEN, addr_peer, lifetime, preferred, NULL);
	accept_signal (address_added);
	g_assert ((a = nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, addr_peer)));
	g_assert (!nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, addr_peer2));

	nmtstp_ip_address_assert_lifetime ((NMPlatformIPAddress *) a, -1, lifetime, preferred);

	nmtstp_ip4_address_add (-1, ifindex, addr, IP4_PLEN, addr_peer2, lifetime, preferred, NULL);
	accept_signal (address_added);
	g_assert (nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, addr_peer));
	g_assert ((a = nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, addr_peer2)));

	nmtstp_ip_address_assert_lifetime ((NMPlatformIPAddress *) a, -1, lifetime, preferred);

	g_assert (addr != addr_peer);
	nmtstp_ip4_address_del (-1, ifindex, addr, IP4_PLEN, addr_peer);
	accept_signal (address_removed);
	g_assert (!nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, addr_peer));
	g_assert (nm_platform_ip4_address_get (NM_PLATFORM_GET, ifindex, addr, IP4_PLEN, addr_peer2));

	free_signal (address_added);
	free_signal (address_removed);
}

/*****************************************************************************/

void
init_tests (int *argc, char ***argv)
{
	nmtst_init_with_logging (argc, argv, NULL, "ALL");
}

/*****************************************************************************
 * SETUP TESTS
 *****************************************************************************/

typedef struct {
	const char *testpath;
	GTestFunc test_func;
} TestSetup;

static void
_test_setup_free (gpointer data)
{
	g_free (data);
}

static void
_g_test_run (gconstpointer user_data)
{
	const TestSetup *s = user_data;
	int ifindex;

	nm_log_trace (LOGD_PLATFORM, ">>> TEST: start %s", s->testpath);

	nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME));
	g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME));
	g_assert_cmpint (nm_platform_dummy_add (NM_PLATFORM_GET, DEVICE_NAME, NULL), ==, NM_PLATFORM_ERROR_SUCCESS);

	ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
	g_assert_cmpint (ifindex, >, 0);
	g_assert_cmpint (DEVICE_IFINDEX, ==, -1);

	DEVICE_IFINDEX = ifindex;

	s->test_func ();

	g_assert_cmpint (DEVICE_IFINDEX, ==, ifindex);
	DEVICE_IFINDEX = -1;

	g_assert_cmpint (ifindex, ==, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME));
	g_assert (nm_platform_link_delete (NM_PLATFORM_GET, ifindex));
	nm_log_trace (LOGD_PLATFORM, ">>> TEST: finished %s", s->testpath);
}

static void
_g_test_add_func (const char *testpath,
                  GTestFunc   test_func)
{
	TestSetup *s;

	s = g_new0 (TestSetup, 1);
	s->testpath = testpath;
	s->test_func = test_func;

	g_test_add_data_func_full (testpath, s, _g_test_run, _test_setup_free);
}

void
setup_tests (void)
{
	_g_test_add_func ("/address/internal/ip4", test_ip4_address);
	_g_test_add_func ("/address/internal/ip6", test_ip6_address);

	_g_test_add_func ("/address/external/ip4", test_ip4_address_external);
	_g_test_add_func ("/address/external/ip6", test_ip6_address_external);

	_g_test_add_func ("/address/external/ip4-peer", test_ip4_address_external_peer);
}