summaryrefslogtreecommitdiff
path: root/src/tests/test-policy-hosts.c
blob: 5fe88691826c850dd9d7e3c76007eff0c9d946d1 (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
/* -*- 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 DEBUG 0

static void
test_generic (const char *before, const char *after)
{
	GString *newc;

	/* Get the new /etc/hosts contents */
	newc = nm_policy_get_etc_hosts (before, strlen (before));

	if (after == NULL) {
		/* No change to /etc/hosts required */
#if DEBUG
		if (newc != NULL) {
			g_message ("\n- NEW ---------------------------------\n"
			           "%s"
			           "+ EXPECTED NONE +++++++++++++++++++++++++\n",
			           newc->str);
		}
#endif
		g_assert (newc == NULL);
	} else {
		g_assert (newc != NULL);

#if DEBUG
		g_message ("\n- NEW ---------------------------------\n"
		           "%s"
		           "+ EXPECTED ++++++++++++++++++++++++++++++\n"
		           "%s"
		           "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
		           newc->str, after);
#endif
		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);
}

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

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);
}

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

static const char *leftover_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"192.168.1.2	comet	# Added by NetworkManager\n"
	"127.0.0.1	localhost.localdomain	localhost\n"
	"::1	localhost6.localdomain6	localhost6\n"
	"192.168.1.3	comet\n"
	"3001:abba::3234	comet\n"
	"\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static const char *leftover_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"
	"192.168.1.3	comet\n"
	"3001:abba::3234	comet\n"
	"\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n";

static void
test_hosts_leftover (void)
{
	test_generic (leftover_before, leftover_after);
}

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

static const char *leftover_double_newline_before = \
	"# Do not remove the following line, or various programs\n"
	"# that require network functionality will fail.\n"
	"192.168.1.2	comet	# Added by NetworkManager\n"
	"127.0.0.1	localhost.localdomain	localhost\n"
	"::1	localhost6.localdomain6	localhost6\n"
	"192.168.1.3	comet\n"
	"3001:abba::3234	comet\n"
	"\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"\n";

static const char *leftover_double_newline_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"
	"192.168.1.3	comet\n"
	"3001:abba::3234	comet\n"
	"\n"
	"127.0.0.1	lcmd.us.intellitxt.com\n"
	"\n";

static void
test_hosts_leftover_double_newline (void)
{
	test_generic (leftover_double_newline_before, leftover_double_newline_after);
}

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

int
main (int argc, char **argv)
{
	g_test_init (&argc, &argv, NULL);

	g_test_add_func ("/hosts/generic", test_hosts_generic);
	g_test_add_func ("/hosts/generic_no_boilerplate", test_hosts_generic_no_boilerplate);
	g_test_add_func ("/hosts/leftover", test_hosts_leftover);
	g_test_add_func ("/hosts/leftover_double_newline", test_hosts_leftover_double_newline);

	return g_test_run ();
}