about summary refs log tree commit diff
path: root/src/tests/test-policy-hosts.c
blob: e14f15e5f226180bfe7bb3b014b2fbbcf5bdb17c (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * 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) 2010 Red Hat, Inc.
 *
 */

#include <glib.h>
#include <string.h>

#include "nm-policy-hosts.h"

#define FALLBACK_HOSTNAME "localhost.localdomain"

static void
test_generic (const char *before,
              const char *after,
              const char *hostname,
              gboolean expect_error)
{
	char **lines;
	GString *newc;
	GError *error = NULL;

	/* Get the new /etc/hosts contents */
	lines = g_strsplit_set (before, "\n\r", 0);
	newc = nm_policy_get_etc_hosts ((const char **) lines,
	                                strlen (before),
	                                hostname,
	                                FALLBACK_HOSTNAME,
	                                &error);
	g_strfreev (lines);

	if (expect_error) {
		g_assert (newc == NULL);
		g_assert (error != NULL);
		g_clear_error (&error);
	} else if (after == NULL) {
		/* No change to /etc/hosts required */
		g_assert (newc == NULL);
		g_assert (error == NULL);
	} else {
		g_assert (newc != NULL);
		g_assert (error == NULL);

#if 0
		g_message ("\n--------------------------------------\n"
		           "%s"
		           "--------------------------------------",
		           newc->str);
#endif
		g_assert (strlen (newc->str) == strlen (after));
		g_assert (strcmp (newc->str, after) == 0);
		g_string_free (newc, TRUE);
	}
}

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

static const char *generic_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	localhost.localdomain localhost\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static void
test_hosts_generic (void)
{
	test_generic (generic_before, NULL, "localhost.localdomain", FALSE);
}

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

static const char *generic_no_boilerplate_before = \
	"127.0.0.1	localhost.localdomain localhost\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static void
test_hosts_generic_no_boilerplate (void)
{
	test_generic (generic_no_boilerplate_before, NULL, "localhost.localdomain", FALSE);
}

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

static const char *generic_no_boilerplate_no_lh_before = \
	"127.0.0.1	localhost.localdomain\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static const char *generic_no_boilerplate_no_lh_after = \
	"127.0.0.1	localhost\n"
	"127.0.0.1	localhost.localdomain\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static void
test_hosts_generic_no_boilerplate_no_lh (void)
{
	test_generic (generic_no_boilerplate_no_lh_before,
	              generic_no_boilerplate_no_lh_after,
	              "localhost.localdomain",
	              FALSE);
}

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


static const char *generic_no_boilerplate_no_lh_no_host_before = \
	"127.0.0.1	localhost.localdomain\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static const char *generic_no_boilerplate_no_lh_no_host_after = \
	"127.0.0.1	comet	localhost.localdomain	localhost\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static void
test_hosts_generic_no_boilerplate_no_lh_no_host (void)
{
	test_generic (generic_no_boilerplate_no_lh_no_host_before,
	              generic_no_boilerplate_no_lh_no_host_after,
	              "comet",
	              FALSE);
}

/*******************************************/
static const char *named_generic_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	playboy localhost\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static void
test_hosts_named_generic (void)
{
	test_generic (named_generic_before, NULL, "playboy", FALSE);
}

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

static const char *named_non127_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	localhost.localdomain localhost\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"192.168.1.2	tomcat\n";

static void
test_hosts_named_non127 (void)
{
	test_generic (named_non127_before, NULL, "tomcat", FALSE);
}

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

static const char *named2_non127_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	localhost.localdomain localhost\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"192.168.1.2	tomcat\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"127.0.0.1	srx.main.ebayrtm.com\n"
	"127.0.0.1	cdn5.tribalfusion.com\n";

static void
test_hosts_named2_non127 (void)
{
	test_generic (named2_non127_before, NULL, "tomcat", FALSE);
}

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

static const char *named_no_lh_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	localhost.localdomain\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"192.168.1.2	tomcat\n";

static const char *named_no_lh_after = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	localhost.localdomain	localhost\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"192.168.1.2	tomcat\n";

static void
test_hosts_named_no_localhost (void)
{
	test_generic (named_no_lh_before, named_no_lh_after, "tomcat", FALSE);
}

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

static const char *no_lh_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	tomcat\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static const char *no_lh_after = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	localhost.localdomain	localhost\n"
	"127.0.0.1	tomcat\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static void
test_hosts_no_localhost (void)
{
	test_generic (no_lh_before, no_lh_after, "tomcat", FALSE);
}

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

static const char *named_last_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 sparcbook.ausil.us\n"
	"::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 sparcbook.ausil.us\n";

static void
test_hosts_named_last (void)
{
	test_generic (named_last_before, NULL, "sparcbook.ausil.us", FALSE);
}

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

static const char *no_host_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"127.0.0.1	srx.main.ebayrtm.com\n"
	"127.0.0.1	cdn5.tribalfusion.com\n"
	"127.0.0.1	a.tribalfusion.com\n";

static const char *no_host_after = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	comet	localhost.localdomain	localhost\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"127.0.0.1	srx.main.ebayrtm.com\n"
	"127.0.0.1	cdn5.tribalfusion.com\n"
	"127.0.0.1	a.tribalfusion.com\n";

static void
test_hosts_no_host (void)
{
	test_generic (no_host_before, no_host_after, "comet", FALSE);
}

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

static const char *long_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"127.0.0.1	localhost.localdomain	localhost	comet\n"
	"::1		localhost6.localdomain6 localhost6\n"
	"\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"127.0.0.1	adserver.adtech.de\n"
	"127.0.0.1	a.as-us.falkag.net\n"
	"127.0.0.1	a.as-eu.falkag.net\n"
	"127.0.0.1	ads.doubleclick.com\n"
	"\n"
	"# random comment\n"
	"127.0.0.1	m1.2mdn.net\n"
	"127.0.0.1	ds.serving-sys.com\n"
	"127.0.0.1	pagead2.googlesyndication.com\n"
	"127.0.0.1	ad.doubleclick.com\n"
	"127.0.0.1	ad.doubleclick.net\n"
	"127.0.0.1	oascentral.movietickets.com\n"
	"127.0.0.1	view.atdmt.com\n"
	"127.0.0.1	ads.chumcity.com\n"
	"127.0.0.1	ads.as4x.tmcs.net\n"
	"127.0.0.1	n4403ad.doubleclick.net\n"
	"127.0.0.1	www.assoc-amazon.com\n"
	"127.0.0.1	s25.sitemeter.com\n"
	"127.0.0.1	adlog.com.com\n"
	"127.0.0.1	ahs.laptopmag.com\n"
	"127.0.0.1	altfarm.mediaplex.com\n"
	"127.0.0.1	ads.addynamix.com\n"
	"127.0.0.1	srx.main.ebayrtm.com\n"
	"127.0.0.1	cdn5.tribalfusion.com\n"
	"127.0.0.1	a.tribalfusion.com\n";


static void
test_hosts_long (void)
{
	test_generic (long_before, NULL, "comet", FALSE);
}

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

typedef struct {
	const char *line;
	const char *token;
	gboolean expected;
} Foo;

static Foo foo[] = {
	/* Using \t here to easily differentiate tabs vs. spaces for testing */
	{ "127.0.0.1\tfoobar\tblah", "blah", TRUE },
	{ "", "blah", FALSE },
	{ "1.1.1.1\tbork\tfoo", "blah", FALSE },
	{ "127.0.0.1 foobar\tblah", "blah", TRUE },
	{ "127.0.0.1 foobar blah", "blah", TRUE },
	{ "127.0.0.1 localhost", "localhost.localdomain", FALSE },
	{ "192.168.1.1 blah borkbork", "blah", TRUE },
	{ "192.168.1.1 foobar\tblah borkbork", "blah", TRUE },
	{ "192.168.1.1\tfoobar\tblah\tborkbork", "blah", TRUE },
	{ "192.168.1.1 \tfoobar \tblah \tborkbork\t ", "blah", TRUE },
	{ "\t\t\t\t   \t\t\tasdfadf  a\t\t\t\t\t   \t\t\t\t\t ", "blah", FALSE },
	{ NULL, NULL, FALSE }
};

static void
test_find_token (void)
{
	Foo *iter = &foo[0];

	while (iter->line) {
		gboolean found;

		found = nm_policy_hosts_find_token (iter->line, iter->token);
		if (found != iter->expected) {
			g_warning ("find-token: unexpected token result %d for '%s' <= '%s' (expected %d)",
			           found, iter->line, iter->token, iter->expected);
		}
		g_assert (found == iter->expected);
		iter++;
	}
}

typedef void (*TCFunc)(void);

#define TESTCASE(t, d) g_test_create_case (#t, 0, d, NULL, (TCFunc) t, NULL)

int main (int argc, char **argv)
{
	GTestSuite *suite;

	g_test_init (&argc, &argv, NULL);

	suite = g_test_get_root ();

	g_test_suite_add (suite, TESTCASE (test_find_token, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_generic, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_generic_no_boilerplate, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_generic_no_boilerplate_no_lh, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_generic_no_boilerplate_no_lh_no_host, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_named_generic, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_named_non127, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_named2_non127, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_named_no_localhost, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_no_localhost, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_named_last, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_no_host, NULL));
	g_test_suite_add (suite, TESTCASE (test_hosts_long, NULL));

	return g_test_run ();
}