summaryrefslogtreecommitdiff
path: root/src/platform/tests/test-address.c
blob: 99be977bd57891682e314b3bcc0366806b3ddf91 (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
#include "test-common.h"

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

static void
ip4_address_callback (NMPlatform *platform, int ifindex, NMPlatformIP4Address *received, SignalData *data)
{
	g_assert (received);
	g_assert_cmpint (received->ifindex, ==, ifindex);

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

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

	if (data->received)
		g_error ("Received signal '%s' a second time.", data->name);

	data->received = TRUE;
}

static void
ip6_address_callback (NMPlatform *platform, int ifindex, NMPlatformIP6Address *received, SignalData *data)
{
	g_assert (received);
	g_assert_cmpint (received->ifindex, ==, ifindex);

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

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

	if (data->received)
		g_error ("Received signal '%s' a second time.", data->name);

	data->received = TRUE;
}

static void
test_ip4_address (void)
{
	SignalData *address_added = add_signal (NM_PLATFORM_IP4_ADDRESS_ADDED, ip4_address_callback);
	SignalData *address_removed = add_signal (NM_PLATFORM_IP4_ADDRESS_REMOVED, ip4_address_callback);
	int ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);
	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_exists (ifindex, addr, IP4_PLEN));
	no_error ();
	g_assert (nm_platform_ip4_address_add (ifindex, addr, IP4_PLEN, lifetime, preferred));
	no_error ();
	g_assert (nm_platform_ip4_address_exists (ifindex, addr, IP4_PLEN));
	no_error ();
	accept_signal (address_added);

	/* Add address again */
	g_assert (!nm_platform_ip4_address_add (ifindex, addr, IP4_PLEN, lifetime, preferred));
	error (NM_PLATFORM_ERROR_EXISTS);

	/* Test address listing */
	addresses = nm_platform_ip4_address_get_all (ifindex);
	g_assert (addresses);
	no_error ();
	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 (ifindex, addr, IP4_PLEN));
	no_error ();
	g_assert (!nm_platform_ip4_address_exists (ifindex, addr, IP4_PLEN));
	accept_signal (address_removed);

	/* Remove address again */
	g_assert (!nm_platform_ip4_address_delete (ifindex, addr, IP4_PLEN));
	error (NM_PLATFORM_ERROR_NOT_FOUND);

	free_signal (address_added);
	free_signal (address_removed);
}

static void
test_ip6_address (void)
{
	SignalData *address_added = add_signal (NM_PLATFORM_IP6_ADDRESS_ADDED, ip6_address_callback);
	SignalData *address_removed = add_signal (NM_PLATFORM_IP6_ADDRESS_REMOVED, ip6_address_callback);
	int ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);
	GArray *addresses;
	NMPlatformIP6Address *address;
	struct in6_addr addr;
	guint32 lifetime = 2000;
	guint32 preferred = 1000;

	inet_pton (AF_INET6, IP6_ADDRESS, &addr);

	/* Add address */
	g_assert (!nm_platform_ip6_address_exists (ifindex, addr, IP6_PLEN));
	no_error ();
	g_assert (nm_platform_ip6_address_add (ifindex, addr, IP6_PLEN, lifetime, preferred));
	no_error ();
	g_assert (nm_platform_ip6_address_exists (ifindex, addr, IP6_PLEN));
	no_error ();
	accept_signal (address_added);

	/* Add address again */
	g_assert (!nm_platform_ip6_address_add (ifindex, addr, IP6_PLEN, lifetime, preferred));
	error (NM_PLATFORM_ERROR_EXISTS);

	/* Test address listing */
	addresses = nm_platform_ip6_address_get_all (ifindex);
	g_assert (addresses);
	no_error ();
	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 (ifindex, addr, IP6_PLEN));
	no_error ();
	g_assert (!nm_platform_ip6_address_exists (ifindex, addr, IP6_PLEN));
	accept_signal (address_removed);

	/* Remove address again */
	g_assert (!nm_platform_ip6_address_delete (ifindex, addr, IP6_PLEN));
	error (NM_PLATFORM_ERROR_NOT_FOUND);

	free_signal (address_added);
	free_signal (address_removed);
}

static void
test_ip4_address_external (void)
{
	SignalData *address_added = add_signal (NM_PLATFORM_IP4_ADDRESS_ADDED, ip4_address_callback);
	SignalData *address_removed = add_signal (NM_PLATFORM_IP4_ADDRESS_REMOVED, ip4_address_callback);
	int ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);
	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_link_get_ifindex (DEVICE_NAME)));

	/* Add/delete notification */
	run_command ("ip address add %s/%d dev %s valid_lft %d preferred_lft %d",
			IP4_ADDRESS, IP4_PLEN, DEVICE_NAME, lifetime, preferred);
	wait_signal (address_added);
	g_assert (nm_platform_ip4_address_exists (ifindex, addr, IP4_PLEN));
	run_command ("ip address delete %s/%d dev %s", IP4_ADDRESS, IP4_PLEN, DEVICE_NAME);
	wait_signal (address_removed);
	g_assert (!nm_platform_ip4_address_exists (ifindex, addr, IP4_PLEN));

	/* Add/delete conflict */
	run_command ("ip address add %s/%d dev %s valid_lft %d preferred_lft %d",
			IP4_ADDRESS, IP4_PLEN, DEVICE_NAME, lifetime, preferred);
	g_assert (nm_platform_ip4_address_add (ifindex, addr, IP4_PLEN, lifetime, preferred));
	no_error ();
	g_assert (nm_platform_ip4_address_exists (ifindex, addr, IP4_PLEN));
	accept_signal (address_added);
	/*run_command ("ip address delete %s/%d dev %s", IP4_ADDRESS, IP4_PLEN, DEVICE_NAME);
	g_assert (nm_platform_ip4_address_delete (ifindex, addr, IP4_PLEN));
	no_error ();
	g_assert (!nm_platform_ip4_address_exists (ifindex, addr, IP4_PLEN));
	accept_signal (address_removed);*/

	free_signal (address_added);
	free_signal (address_removed);
}

static void
test_ip6_address_external (void)
{
	SignalData *address_added = add_signal (NM_PLATFORM_IP6_ADDRESS_ADDED, ip6_address_callback);
	SignalData *address_removed = add_signal (NM_PLATFORM_IP6_ADDRESS_REMOVED, ip6_address_callback);
	int ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);
	struct in6_addr addr;
	guint32 lifetime = 2000;
	guint32 preferred = 1000;

	inet_pton (AF_INET6, IP6_ADDRESS, &addr);

	/* Add/delete notification */
	run_command ("ip address add %s/%d dev %s valid_lft %d preferred_lft %d",
			IP6_ADDRESS, IP6_PLEN, DEVICE_NAME, lifetime, preferred);
	wait_signal (address_added);
	g_assert (nm_platform_ip6_address_exists (ifindex, addr, IP6_PLEN));
	run_command ("ip address delete %s/%d dev %s", IP6_ADDRESS, IP6_PLEN, DEVICE_NAME);
	wait_signal (address_removed);
	g_assert (!nm_platform_ip6_address_exists (ifindex, addr, IP6_PLEN));

	/* Add/delete conflict */
	run_command ("ip address add %s/%d dev %s valid_lft %d preferred_lft %d", 
			IP6_ADDRESS, IP6_PLEN, DEVICE_NAME, lifetime, preferred);
	g_assert (nm_platform_ip6_address_add (ifindex, addr, IP6_PLEN, lifetime, preferred));
	no_error ();
	g_assert (nm_platform_ip6_address_exists (ifindex, addr, IP6_PLEN));
	accept_signal (address_added);
	/*run_command ("ip address delete %s/%d dev %s", IP6_ADDRESS, IP6_PLEN, DEVICE_NAME);
	g_assert (nm_platform_ip6_address_delete (ifindex, addr, IP6_PLEN));
	no_error ();
	g_assert (!nm_platform_ip6_address_exists (ifindex, addr, IP6_PLEN));
	wait_signal (address_removed);*/

	free_signal (address_added);
	free_signal (address_removed);
}

void
setup_tests (void)
{
	SignalData *link_added = add_signal_ifname (NM_PLATFORM_LINK_ADDED, link_callback, DEVICE_NAME);

	nm_platform_link_delete (nm_platform_link_get_ifindex (DEVICE_NAME));
	g_assert (!nm_platform_link_exists (DEVICE_NAME));
	g_assert (nm_platform_dummy_add (DEVICE_NAME));
	wait_signal (link_added);
	free_signal (link_added);

	g_test_add_func ("/address/internal/ip4", test_ip4_address);
	g_test_add_func ("/address/internal/ip6", test_ip6_address);

	if (strcmp (g_type_name (G_TYPE_FROM_INSTANCE (nm_platform_get ())), "NMFakePlatform")) {
		g_test_add_func ("/address/external/ip4", test_ip4_address_external);
		g_test_add_func ("/address/external/ip6", test_ip6_address_external);
	}
}