1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Copyright 2013 Red Hat, Inc.
18 *
19 */
20
21 #include "config.h"
22
23 #include <unistd.h>
24
25 #include <glib.h>
26
27 #include <nm-config.h>
28 #include "nm-test-device.h"
29
30 static void
31 setup_config (const char *config_file, const char *config_dir, ...)
32 {
33 va_list ap;
34 GPtrArray *args;
35 char **argv, *arg;
36 int argc;
37 GOptionContext *context;
38
39 args = g_ptr_array_new ();
40 g_ptr_array_add (args, "test-config");
41 g_ptr_array_add (args, "--config");
42 g_ptr_array_add (args, (char *)config_file);
43 g_ptr_array_add (args, "--config-dir");
44 g_ptr_array_add (args, (char *)config_dir);
45
46 va_start (ap, config_dir);
47 while ((arg = va_arg (ap, char *)))
48 g_ptr_array_add (args, arg);
49 va_end (ap);
50
51 argv = (char **)args->pdata;
52 argc = args->len;
53
54 context = g_option_context_new (NULL);
55 g_option_context_add_main_entries (context, nm_config_get_options (), NULL);
56 g_option_context_parse (context, &argc, &argv, NULL);
57 g_option_context_free (context);
58
59 g_ptr_array_free (args, TRUE);
60 }
61
62 static void
63 test_config_simple (void)
64 {
65 NMConfig *config;
66 GError *error = NULL;
67 const char **plugins;
68 char *value;
69
70 setup_config (SRCDIR "/NetworkManager.conf", "/no/such/dir", NULL);
71 config = nm_config_new (&error);
72 g_assert_no_error (error);
73
74 g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf");
75 g_assert_cmpstr (nm_config_get_dhcp_client (config), ==, "dhclient");
76 g_assert_cmpstr (nm_config_get_log_level (config), ==, "INFO");
77 g_assert_cmpint (nm_config_get_connectivity_interval (config), ==, 100);
78
79 plugins = nm_config_get_plugins (config);
80 g_assert_cmpint (g_strv_length ((char **)plugins), ==, 3);
81 g_assert_cmpstr (plugins[0], ==, "foo");
82 g_assert_cmpstr (plugins[1], ==, "bar");
83 g_assert_cmpstr (plugins[2], ==, "baz");
84
85 value = nm_config_get_value (config, "extra-section", "extra-key", NULL);
86 g_assert_cmpstr (value, ==, "some value");
87 g_free (value);
88
89 value = nm_config_get_value (config, "extra-section", "no-key", &error);
90 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
91 g_clear_error (&error);
92
93 value = nm_config_get_value (config, "no-section", "no-key", &error);
94 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
95 g_clear_error (&error);
96
97 g_object_unref (config);
98 }
99
100 static void
101 test_config_non_existent (void)
102 {
103 NMConfig *config;
104 GError *error = NULL;
105
106 setup_config (SRCDIR "/no-such-file", "/no/such/dir", NULL);
(1) Event returned_pointer: |
Pointer "config" returned by "nm_config_new(&error)" is never used. |
107 config = nm_config_new (&error);
108 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND);
109 }
110
111 static void
112 test_config_parse_error (void)
113 {
114 NMConfig *config;
115 GError *error = NULL;
116
117 setup_config (SRCDIR "/bad.conf", "/no/such/dir", NULL);
118 config = nm_config_new (&error);
119 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
120 }
121
122 static void
123 test_config_override (void)
124 {
125 NMConfig *config;
126 GError *error = NULL;
127 const char **plugins;
128
129 setup_config (SRCDIR "/NetworkManager.conf", "/no/such/dir",
130 "--plugins", "alpha,beta,gamma,delta",
131 "--connectivity-interval", "12",
132 NULL);
133 config = nm_config_new (&error);
134 g_assert_no_error (error);
135
136 g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf");
137 g_assert_cmpstr (nm_config_get_dhcp_client (config), ==, "dhclient");
138 g_assert_cmpstr (nm_config_get_log_level (config), ==, "INFO");
139 g_assert_cmpint (nm_config_get_connectivity_interval (config), ==, 12);
140
141 plugins = nm_config_get_plugins (config);
142 g_assert_cmpint (g_strv_length ((char **)plugins), ==, 4);
143 g_assert_cmpstr (plugins[0], ==, "alpha");
144 g_assert_cmpstr (plugins[1], ==, "beta");
145 g_assert_cmpstr (plugins[2], ==, "gamma");
146 g_assert_cmpstr (plugins[3], ==, "delta");
147
148 g_object_unref (config);
149 }
150
151 static void
152 test_config_no_auto_default (void)
153 {
154 NMConfig *config;
155 GError *error = NULL;
156 int fd, nwrote;
157 char *state_file;
158 NMTestDevice *dev1, *dev2, *dev3, *dev4;
159
160 fd = g_file_open_tmp (NULL, &state_file, &error);
161 g_assert_no_error (error);
162
163 nwrote = write (fd, "22:22:22:22:22:22\n", 18);
164 g_assert_cmpint (nwrote, ==, 18);
165 nwrote = write (fd, "44:44:44:44:44:44\n", 18);
166 g_assert_cmpint (nwrote, ==, 18);
167 close (fd);
168
169 setup_config (SRCDIR "/NetworkManager.conf", "/no/such/dir",
170 "--no-auto-default", state_file,
171 NULL);
172 config = nm_config_new (&error);
173 g_assert_no_error (error);
174
175 dev1 = nm_test_device_new ("11:11:11:11:11:11");
176 dev2 = nm_test_device_new ("22:22:22:22:22:22");
177 dev3 = nm_test_device_new ("33:33:33:33:33:33");
178 dev4 = nm_test_device_new ("44:44:44:44:44:44");
179
180 g_assert (!nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev1)));
181 g_assert (!nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev2)));
182 g_assert (nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev3)));
183 g_assert (!nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev4)));
184
185 nm_config_set_ethernet_no_auto_default (config, NM_CONFIG_DEVICE (dev3));
186 g_assert (!nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev3)));
187
188 g_object_unref (config);
189
190 setup_config (SRCDIR "/NetworkManager.conf", "/no/such/dir",
191 "--no-auto-default", state_file,
192 NULL);
193 config = nm_config_new (&error);
194 g_assert_no_error (error);
195
196 g_assert (!nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev1)));
197 g_assert (!nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev2)));
198 g_assert (!nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev3)));
199 g_assert (!nm_config_get_ethernet_can_auto_default (config, NM_CONFIG_DEVICE (dev4)));
200
201 g_object_unref (config);
202
203 g_object_unref (dev1);
204 g_object_unref (dev2);
205 g_object_unref (dev3);
206 g_object_unref (dev4);
207
208 unlink (state_file);
209 g_free (state_file);
210 }
211
212 static void
213 test_config_confdir (void)
214 {
215 NMConfig *config;
216 GError *error = NULL;
217 const char **plugins;
218 char *value;
219
220 setup_config (SRCDIR "/NetworkManager.conf", SRCDIR "/conf.d", NULL);
221 config = nm_config_new (&error);
222 g_assert_no_error (error);
223
224 g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf");
225 g_assert_cmpstr (nm_config_get_dhcp_client (config), ==, "dhcpcd");
226 g_assert_cmpstr (nm_config_get_log_level (config), ==, "INFO");
227 g_assert_cmpstr (nm_config_get_log_domains (config), ==, "PLATFORM,DNS,WIFI");
228 g_assert_cmpstr (nm_config_get_connectivity_uri (config), ==, "http://example.net");
229 g_assert_cmpint (nm_config_get_connectivity_interval (config), ==, 100);
230
231 plugins = nm_config_get_plugins (config);
232 g_assert_cmpint (g_strv_length ((char **)plugins), ==, 5);
233 g_assert_cmpstr (plugins[0], ==, "foo");
234 g_assert_cmpstr (plugins[1], ==, "bar");
235 g_assert_cmpstr (plugins[2], ==, "baz");
236 g_assert_cmpstr (plugins[3], ==, "one");
237 g_assert_cmpstr (plugins[4], ==, "two");
238
239 value = nm_config_get_value (config, "main", "extra", NULL);
240 g_assert_cmpstr (value, ==, "hello");
241 g_free (value);
242
243 value = nm_config_get_value (config, "main", "new", NULL);
244 g_assert_cmpstr (value, ==, "something"); /* not ",something" */
245 g_free (value);
246
247 value = nm_config_get_value (config, "order", "a", NULL);
248 g_assert_cmpstr (value, ==, "90");
249 g_free (value);
250 value = nm_config_get_value (config, "order", "b", NULL);
251 g_assert_cmpstr (value, ==, "10");
252 g_free (value);
253 value = nm_config_get_value (config, "order", "c", NULL);
254 g_assert_cmpstr (value, ==, "0");
255 g_free (value);
256
257 g_object_unref (config);
258 }
259
260 static void
261 test_config_confdir_parse_error (void)
262 {
263 NMConfig *config;
264 GError *error = NULL;
265
266 /* Using SRCDIR as the conf dir will pick up bad.conf */
267 setup_config (SRCDIR "/NetworkManager.conf", SRCDIR, NULL);
268 config = nm_config_new (&error);
269 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
270 }
271
272 int
273 main (int argc, char **argv)
274 {
275 g_type_init ();
276 g_test_init (&argc, &argv, NULL);
277
278 g_test_add_func ("/config/simple", test_config_simple);
279 g_test_add_func ("/config/non-existent", test_config_non_existent);
280 g_test_add_func ("/config/parse-error", test_config_parse_error);
281 g_test_add_func ("/config/no-auto-default", test_config_no_auto_default);
282 g_test_add_func ("/config/confdir", test_config_confdir);
283 g_test_add_func ("/config/confdir-parse-error", test_config_confdir_parse_error);
284
285 /* This one has to come last, because it leaves its values in
286 * nm-config.c's global variables, and there's no way to reset
287 * those to NULL.
288 */
289 g_test_add_func ("/config/override", test_config_override);
290
291 return g_test_run ();
292 }
293
294