summaryrefslogtreecommitdiff
path: root/src/platform/tests/test-nmp-object.c
blob: d77170b364123943aeb164839ae09d4833ebc6d4 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* nm-platform.c - Handle runtime kernel networking configuration
 *
 * 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, or (at your option)
 * any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2015 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nmp-object.h"

#include "nm-test-utils.h"

struct {
	GList *udev_devices;
} global;

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

static gboolean
_nmp_object_id_equal (const NMPObject *a, const NMPObject *b)
{
	gboolean a_b = nmp_object_id_equal (a, b);

	g_assert (NM_IN_SET (a_b, FALSE, TRUE) && a_b == nmp_object_id_equal (b, a));
	return a_b;
}
#define nmp_object_id_equal _nmp_object_id_equal

static gboolean
_nmp_object_equal (const NMPObject *a, const NMPObject *b)
{
	gboolean a_b = nmp_object_equal (a, b);

	g_assert (NM_IN_SET (a_b, FALSE, TRUE) && a_b == nmp_object_equal (b, a));
	return a_b;
}
#define nmp_object_equal _nmp_object_equal

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

static void
_assert_cache_multi_lookup_contains (const NMPCache *cache, const NMPCacheId *cache_id, const NMPObject *obj, gboolean contains)
{
	const NMPlatformObject *const *objects;
	guint i, len;
	gboolean found;

	g_assert (cache_id);
	g_assert (NMP_OBJECT_IS_VALID (obj));

	g_assert (nmp_cache_lookup_obj (cache, obj) == obj);

	objects = nmp_cache_lookup_multi (cache, cache_id, &len);

	g_assert ((len == 0 && !objects) || (len > 0 && objects && !objects[len]));

	found = FALSE;
	for (i = 0; i < len; i++) {
		NMPObject *o;

		g_assert (objects[i]);
		o = NMP_OBJECT_UP_CAST (objects[i]);
		g_assert (NMP_OBJECT_IS_VALID (o));

		if (obj == o) {
			g_assert (!found);
			found = TRUE;
		}
	}

	g_assert (!!contains == found);
}

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

typedef struct {
	NMPCache *cache;
	NMPCacheOpsType expected_ops_type;
	const NMPObject *obj_clone;
	NMPObject *new_clone;
	gboolean was_visible;
	gboolean called;
} _NMPCacheUpdateData;

static void
_nmp_cache_update_hook (NMPCache *cache, const NMPObject *old, const NMPObject *new, NMPCacheOpsType ops_type, gpointer user_data)
{
	_NMPCacheUpdateData *data = user_data;

	g_assert (data);
	g_assert (!data->called);
	g_assert (data->cache == cache);

	g_assert_cmpint (data->expected_ops_type, ==, ops_type);

	switch (ops_type) {
	case NMP_CACHE_OPS_ADDED:
		g_assert (!old);
		g_assert (NMP_OBJECT_IS_VALID (new));
		g_assert (nmp_object_is_alive (new));
		g_assert (nmp_object_id_equal (data->obj_clone, new));
		g_assert (nmp_object_equal (data->obj_clone, new));
		break;
	case NMP_CACHE_OPS_UPDATED:
		g_assert (NMP_OBJECT_IS_VALID (old));
		g_assert (NMP_OBJECT_IS_VALID (new));
		g_assert (nmp_object_is_alive (old));
		g_assert (nmp_object_is_alive (new));
		g_assert (nmp_object_id_equal (data->obj_clone, new));
		g_assert (nmp_object_id_equal (data->obj_clone, old));
		g_assert (nmp_object_id_equal (old, new));
		g_assert (nmp_object_equal (data->obj_clone, new));
		g_assert (!nmp_object_equal (data->obj_clone, old));
		g_assert (!nmp_object_equal (old, new));
		break;
	case NMP_CACHE_OPS_REMOVED:
		g_assert (!new);
		g_assert (NMP_OBJECT_IS_VALID (old));
		g_assert (nmp_object_is_alive (old));
		g_assert (nmp_object_id_equal (data->obj_clone, old));
		break;
	default:
		g_assert_not_reached ();
	}

	data->was_visible = old ? nmp_object_is_visible (old) : FALSE;
	data->new_clone = new ? nmp_object_clone (new, FALSE) : NULL;
	data->called = TRUE;
}

static void
_nmp_cache_update_netlink (NMPCache *cache, NMPObject *obj, NMPObject **out_obj, gboolean *out_was_visible, NMPCacheOpsType expected_ops_type)
{
	NMPCacheOpsType ops_type;
	NMPObject *obj2;
	gboolean was_visible;
	nm_auto_nmpobj NMPObject *obj_clone = nmp_object_clone (obj, FALSE);
	nm_auto_nmpobj NMPObject *new_clone = NULL;
	const NMPObject *obj_old;
	_NMPCacheUpdateData data = {
		.cache = cache,
		.expected_ops_type = expected_ops_type,
		.obj_clone = obj_clone,
	};

	obj_old = nmp_cache_lookup_link (cache, obj->object.ifindex);
	if (obj_old && obj_old->_link.udev.device)
		obj_clone->_link.udev.device = g_object_ref (obj_old->_link.udev.device);
	_nmp_object_fixup_link_udev_fields (obj_clone, nmp_cache_use_udev_get (cache));

	g_assert (cache);
	g_assert (NMP_OBJECT_IS_VALID (obj));

	ops_type = nmp_cache_update_netlink (cache, obj, &obj2, &was_visible, _nmp_cache_update_hook, &data);

	new_clone = data.new_clone;

	g_assert_cmpint (ops_type, ==, expected_ops_type);

	if (ops_type != NMP_CACHE_OPS_UNCHANGED) {
		g_assert (NMP_OBJECT_IS_VALID (obj2));
		g_assert (data.called);
		g_assert_cmpint (data.was_visible, ==, was_visible);

		if (ops_type == NMP_CACHE_OPS_REMOVED)
			g_assert (!data.new_clone);
		else {
			g_assert (data.new_clone);
			g_assert (nmp_object_equal (obj2, data.new_clone));
		}
	} else {
		g_assert (!data.called);
		g_assert (!obj2 || was_visible == nmp_object_is_visible (obj2));
	}

	g_assert (!obj2 || nmp_object_id_equal (obj, obj2));
	if (ops_type != NMP_CACHE_OPS_REMOVED && obj2)
		g_assert (nmp_object_equal (obj, obj2));

	if (out_obj)
		*out_obj = obj2;
	else
		nmp_object_unref (obj2);
	if (out_was_visible)
		*out_was_visible = was_visible;
}

static const NMPlatformLink pl_link_2 = {
	.ifindex = 2,
	.name = "eth0",
	.type = NM_LINK_TYPE_ETHERNET,
};

static const NMPlatformLink pl_link_3 = {
	.ifindex = 3,
	.name = "wlan0",
	.type = NM_LINK_TYPE_WIFI,
};

static void
test_cache_link (void)
{
	NMPCache *cache;
	NMPObject *obj1, *obj2;
	NMPObject objs1;
	gboolean was_visible;
	NMPCacheId cache_id_storage;
	GUdevDevice *udev_device_2 = g_list_nth_data (global.udev_devices, 0);
	GUdevDevice *udev_device_3 = g_list_nth_data (global.udev_devices, 0);
	NMPCacheOpsType ops_type;

	cache = nmp_cache_new (nmtst_get_rand_int () % 2);

	/* if we have a link, and don't set is_in_netlink, adding it has no effect. */
	obj1 = nmp_object_new (NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
	g_assert (NMP_OBJECT_UP_CAST (&obj1->object) == obj1);
	g_assert (!nmp_object_is_alive (obj1));
	_nmp_cache_update_netlink (cache, obj1, &obj2, &was_visible, NMP_CACHE_OPS_UNCHANGED);
	ASSERT_nmp_cache_is_consistent (cache);
	g_assert (!obj2);
	g_assert (!was_visible);
	g_assert (!nmp_cache_lookup_obj (cache, obj1));
	g_assert (!nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_2.ifindex)));
	nmp_object_unref (obj1);

	/* Only when setting @is_in_netlink the link is added. */
	obj1 = nmp_object_new (NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
	obj1->_link.netlink.is_in_netlink = TRUE;
	g_assert (nmp_object_is_alive (obj1));
	_nmp_cache_update_netlink (cache, obj1, &obj2, &was_visible, NMP_CACHE_OPS_ADDED);
	ASSERT_nmp_cache_is_consistent (cache);
	g_assert (nmp_object_equal (obj1, obj2));
	g_assert (!was_visible);
	g_assert (nmp_cache_lookup_obj (cache, obj1) == obj2);
	g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_2.ifindex)) == obj2);
	g_assert (nmp_object_is_visible (obj2));
	_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, TRUE), obj2, TRUE);
	_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, FALSE), obj2, TRUE);
	nmp_object_unref (obj1);
	nmp_object_unref (obj2);

	/* updating the same link with identical value, has no effect. */
	obj1 = nmp_object_new (NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
	obj1->_link.netlink.is_in_netlink = TRUE;
	g_assert (nmp_object_is_alive (obj1));
	_nmp_cache_update_netlink (cache, obj1, &obj2, &was_visible, NMP_CACHE_OPS_UNCHANGED);
	ASSERT_nmp_cache_is_consistent (cache);
	g_assert (obj2 != obj1);
	g_assert (nmp_object_equal (obj1, obj2));
	g_assert (was_visible);
	g_assert (nmp_cache_lookup_obj (cache, obj1) == obj2);
	g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_2.ifindex)) == obj2);
	nmp_object_unref (obj1);
	nmp_object_unref (obj2);

	/* remove the link from netlink */
	obj1 = nmp_object_new (NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
	g_assert (!nmp_object_is_alive (obj1));
	_nmp_cache_update_netlink (cache, obj1, &obj2, &was_visible, NMP_CACHE_OPS_REMOVED);
	ASSERT_nmp_cache_is_consistent (cache);
	g_assert (obj2 != obj1);
	g_assert (was_visible);
	g_assert (!nmp_cache_lookup_obj (cache, obj1));
	g_assert (!nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_2.ifindex)));
	nmp_object_unref (obj1);
	nmp_object_unref (obj2);

	if (udev_device_2) {
		/* now add the link only with aspect UDEV. */
		ops_type = nmp_cache_update_link_udev (cache, pl_link_2.ifindex, udev_device_2, &obj2, &was_visible, NULL, NULL);
		ASSERT_nmp_cache_is_consistent (cache);
		g_assert_cmpint (ops_type, ==, NMP_CACHE_OPS_ADDED);
		g_assert (!was_visible);
		g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_2.ifindex)) == obj2);
		g_assert (!nmp_object_is_visible (obj2));
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, TRUE), obj2, FALSE);
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, FALSE), obj2, TRUE);
		nmp_object_unref (obj2);
	}

	/* add it in netlink too. */
	obj1 = nmp_object_new (NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
	obj1->_link.netlink.is_in_netlink = TRUE;
	g_assert (nmp_object_is_alive (obj1));
	_nmp_cache_update_netlink (cache, obj1, &obj2, &was_visible, udev_device_2 ? NMP_CACHE_OPS_UPDATED : NMP_CACHE_OPS_ADDED);
	ASSERT_nmp_cache_is_consistent (cache);
	g_assert (nmp_object_equal (obj1, obj2));
	g_assert (!was_visible);
	g_assert (nmp_cache_lookup_obj (cache, obj1) == obj2);
	g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_2.ifindex)) == obj2);
	g_assert (nmp_object_is_visible (obj2));
	_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, TRUE), obj2, TRUE);
	_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, FALSE), obj2, TRUE);
	nmp_object_unref (obj1);
	nmp_object_unref (obj2);

	/* remove again from netlink. */
	obj1 = nmp_object_new (NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_2);
	obj1->_link.netlink.is_in_netlink = FALSE;
	g_assert (!nmp_object_is_alive (obj1));
	_nmp_cache_update_netlink (cache, obj1, &obj2, &was_visible, udev_device_2 ? NMP_CACHE_OPS_UPDATED : NMP_CACHE_OPS_REMOVED);
	ASSERT_nmp_cache_is_consistent (cache);
	g_assert (obj2 != obj1);
	g_assert (was_visible);
	if (udev_device_2) {
		g_assert (nmp_cache_lookup_obj (cache, obj1) == obj2);
		g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_2.ifindex)) == obj2);
		g_assert (!nmp_object_is_visible (obj2));
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, TRUE), obj2, FALSE);
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, FALSE), obj2, TRUE);
	} else {
		g_assert (nmp_cache_lookup_obj (cache, obj1) == NULL);
		g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_2.ifindex)) == NULL);
		g_assert (nmp_object_is_visible (obj2));
	}
	nmp_object_unref (obj1);
	nmp_object_unref (obj2);

	/* now another link only with aspect UDEV. */
	if (udev_device_3) {
		/* now add the link only with aspect UDEV. */
		ops_type = nmp_cache_update_link_udev (cache, pl_link_3.ifindex, udev_device_3, &obj2, &was_visible, NULL, NULL);
		g_assert_cmpint (ops_type, ==, NMP_CACHE_OPS_ADDED);
		ASSERT_nmp_cache_is_consistent (cache);
		g_assert (NMP_OBJECT_IS_VALID (obj2));
		g_assert (!was_visible);
		g_assert (!nmp_object_is_visible (obj2));
		g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_3.ifindex)) == obj2);
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, TRUE), obj2, FALSE);
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, FALSE), obj2, TRUE);
		g_assert_cmpint (obj2->_link.netlink.is_in_netlink, ==, FALSE);
		g_assert_cmpint (obj2->link.initialized, ==, FALSE);
		nmp_object_unref (obj2);

		/* add it in netlink too. */
		obj1 = nmp_object_new (NMP_OBJECT_TYPE_LINK, (NMPlatformObject *) &pl_link_3);
		obj1->_link.netlink.is_in_netlink = TRUE;
		g_assert (nmp_object_is_alive (obj1));
		_nmp_cache_update_netlink (cache, obj1, &obj2, &was_visible, NMP_CACHE_OPS_UPDATED);
		ASSERT_nmp_cache_is_consistent (cache);
		g_assert (obj2 != obj1);
		g_assert (nmp_object_equal (obj1, obj2));
		g_assert (!was_visible);
		g_assert (nmp_cache_lookup_obj (cache, obj1) == obj2);
		g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_3.ifindex)) == obj2);
		g_assert (nmp_object_is_visible (obj2));
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, TRUE), obj2, TRUE);
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, FALSE), obj2, TRUE);
		g_assert_cmpint (obj2->_link.netlink.is_in_netlink, ==, TRUE);
		g_assert_cmpint (obj2->link.initialized, ==, TRUE);
		nmp_object_unref (obj1);
		nmp_object_unref (obj2);

		/* remove UDEV. */
		ops_type = nmp_cache_update_link_udev (cache, pl_link_3.ifindex, NULL, &obj2, &was_visible, NULL, NULL);
		g_assert_cmpint (ops_type, ==, NMP_CACHE_OPS_UPDATED);
		ASSERT_nmp_cache_is_consistent (cache);
		g_assert (was_visible);
		g_assert (nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&objs1, pl_link_3.ifindex)) == obj2);
		g_assert (nmp_object_is_visible (obj2));
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, TRUE), obj2, TRUE);
		_assert_cache_multi_lookup_contains (cache, nmp_cache_id_init_object_type (&cache_id_storage, NMP_OBJECT_TYPE_LINK, FALSE), obj2, TRUE);
		g_assert_cmpint (obj2->_link.netlink.is_in_netlink, ==, TRUE);
		g_assert_cmpint (obj2->link.initialized, ==, !nmp_cache_use_udev_get (cache));
		nmp_object_unref (obj2);
	}

	nmp_cache_free (cache);
}

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

NMTST_DEFINE ();

int
main (int argc, char **argv)
{
	int result;
	gs_unref_object GUdevClient *udev_client = NULL;

	nmtst_init_assert_logging (&argc, &argv, "INFO", "DEFAULT");

	udev_client = g_udev_client_new ((const char *[]) { "net", NULL });
	{
		gs_unref_object GUdevEnumerator *udev_enumerator = g_udev_enumerator_new (udev_client);

		g_udev_enumerator_add_match_subsystem (udev_enumerator, "net");

		/* Demand that the device is initialized (udev rules ran,
		 * device has a stable name now) in case udev is running
		 * (not in a container). */
		if (access ("/sys", W_OK) == 0)
			g_udev_enumerator_add_match_is_initialized (udev_enumerator);

		global.udev_devices = g_udev_enumerator_execute (udev_enumerator);
	}

	g_test_add_func ("/nmp-object/cache_link", test_cache_link);

	result = g_test_run ();

	while (global.udev_devices) {
		g_object_unref (global.udev_devices->data);
		global.udev_devices = g_list_remove (global.udev_devices, global.udev_devices->data);
	}

	return result;
}