diff options
| author | Michael Biebl <biebl@debian.org> | 2015-07-14 19:38:58 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2015-07-14 19:38:58 +0200 |
| commit | 50a58f0fabd8a34c1b6108a107e08abe3c1ccd24 (patch) | |
| tree | 6790165f39daee79e2b6c6617483320613493367 /src/tests/config/test-config.c | |
| parent | f408e27bccfacf347605a8d98649975a68f38a17 (diff) | |
Imported Upstream version 1.0.4 upstream/1.0.4
Diffstat (limited to 'src/tests/config/test-config.c')
| -rw-r--r-- | src/tests/config/test-config.c | 253 |
1 files changed, 183 insertions, 70 deletions
diff --git a/src/tests/config/test-config.c b/src/tests/config/test-config.c index e027eab8..8b8028e6 100644 --- a/src/tests/config/test-config.c +++ b/src/tests/config/test-config.c @@ -32,8 +32,8 @@ #include "nm-test-utils.h" -static void -setup_config (const char *config_file, const char *config_dir, ...) +static NMConfig * +setup_config (GError **error, const char *config_file, const char *config_dir, ...) { va_list ap; GPtrArray *args; @@ -41,6 +41,11 @@ setup_config (const char *config_file, const char *config_dir, ...) int argc; GOptionContext *context; gboolean success; + NMConfig *config; + GError *local_error = NULL; + NMConfigCmdLineOptions *cli; + + g_assert (!error || !*error); args = g_ptr_array_new (); g_ptr_array_add (args, "test-config"); @@ -57,8 +62,10 @@ setup_config (const char *config_file, const char *config_dir, ...) argv = (char **)args->pdata; argc = args->len; + cli = nm_config_cmd_line_options_new (); + context = g_option_context_new (NULL); - g_option_context_add_main_entries (context, nm_config_get_options (), NULL); + nm_config_cmd_line_options_add_to_entries (cli, context); success = g_option_context_parse (context, &argc, &argv, NULL); g_option_context_free (context); @@ -66,24 +73,36 @@ setup_config (const char *config_file, const char *config_dir, ...) g_printerr ("Invalid options.\n"); g_ptr_array_free (args, TRUE); + + config = nm_config_setup (cli, &local_error); + if (error) { + g_assert (!config); + g_assert (local_error); + g_propagate_error (error, local_error); + } else { + g_assert (config); + g_assert_no_error (local_error); + } + nm_config_cmd_line_options_free (cli); + return config; } static void test_config_simple (void) { NMConfig *config; - GError *error = NULL; const char **plugins; char *value; + gs_unref_object NMDevice *dev50 = nm_test_device_new ("00:00:00:00:00:50"); + gs_unref_object NMDevice *dev51 = nm_test_device_new ("00:00:00:00:00:51"); + gs_unref_object NMDevice *dev52 = nm_test_device_new ("00:00:00:00:00:52"); - setup_config (SRCDIR "/NetworkManager.conf", "/no/such/dir", NULL); - config = nm_config_new (&error); - g_assert_no_error (error); + config = setup_config (NULL, SRCDIR "/NetworkManager.conf", "/no/such/dir", NULL); - g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf"); + g_assert_cmpstr (nm_config_data_get_config_main_file (nm_config_get_data_orig (config)), ==, SRCDIR "/NetworkManager.conf"); g_assert_cmpstr (nm_config_get_dhcp_client (config), ==, "dhclient"); g_assert_cmpstr (nm_config_get_log_level (config), ==, "INFO"); - g_assert_cmpint (nm_config_get_connectivity_interval (config), ==, 100); + g_assert_cmpint (nm_config_data_get_connectivity_interval (nm_config_get_data_orig (config)), ==, 100); plugins = nm_config_get_plugins (config); g_assert_cmpint (g_strv_length ((char **)plugins), ==, 3); @@ -91,19 +110,63 @@ test_config_simple (void) g_assert_cmpstr (plugins[1], ==, "bar"); g_assert_cmpstr (plugins[2], ==, "baz"); - value = nm_config_get_value (config, "extra-section", "extra-key", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "extra-section", "extra-key", NM_CONFIG_GET_VALUE_NONE); g_assert_cmpstr (value, ==, "some value"); g_free (value); - value = nm_config_get_value (config, "extra-section", "no-key", &error); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "extra-section", "no-key", NM_CONFIG_GET_VALUE_NONE); g_assert (!value); - g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND); - g_clear_error (&error); - value = nm_config_get_value (config, "no-section", "no-key", &error); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "no-section", "no-key", NM_CONFIG_GET_VALUE_NONE); g_assert (!value); - g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND); - g_clear_error (&error); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), "connection", "ipv6.ip6_privacy", NM_CONFIG_GET_VALUE_NONE); + g_assert_cmpstr (value, ==, "0"); + g_free (value); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), "connection.dev51", "ipv4.route-metric", NM_CONFIG_GET_VALUE_NONE); + g_assert_cmpstr (value, ==, "51"); + g_free (value); + + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "ipv6.route-metric", NULL); + g_assert_cmpstr (value, ==, NULL); + g_free (value); + + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "ipv4.route-metric", NULL); + g_assert_cmpstr (value, ==, "50"); + g_free (value); + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "ipv4.route-metric", dev50); + g_assert_cmpstr (value, ==, "50"); + g_free (value); + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "ipv4.route-metric", dev51); + g_assert_cmpstr (value, ==, "51"); + g_free (value); + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "ipv4.route-metric", dev52); + g_assert_cmpstr (value, ==, "52"); + g_free (value); + + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "dummy.test1", dev51); + g_assert_cmpstr (value, ==, "yes"); + g_free (value); + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "dummy.test1", dev50); + g_assert_cmpstr (value, ==, "no"); + g_free (value); + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "dummy.test2", dev51); + g_assert_cmpstr (value, ==, NULL); + g_free (value); + + value = nm_config_data_get_connection_default (nm_config_get_data_orig (config), "dummy.test2", dev50); + g_assert_cmpstr (value, ==, "no"); + g_free (value); + g_object_unref (config); } @@ -111,45 +174,38 @@ test_config_simple (void) static void test_config_non_existent (void) { - NMConfig *config; gs_free_error GError *error = NULL; - setup_config (SRCDIR "/no-such-file", "/no/such/dir", NULL); - config = nm_config_new (&error); - g_assert (!config); + setup_config (&error, SRCDIR "/no-such-file", "/no/such/dir", NULL); g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND); + g_clear_error (&error); } static void test_config_parse_error (void) { - NMConfig *config; gs_free_error GError *error = NULL; - setup_config (SRCDIR "/bad.conf", "/no/such/dir", NULL); - config = nm_config_new (&error); - g_assert (!config); + setup_config (&error, SRCDIR "/bad.conf", "/no/such/dir", NULL); g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE); + g_clear_error (&error); } static void test_config_override (void) { NMConfig *config; - GError *error = NULL; const char **plugins; - setup_config (SRCDIR "/NetworkManager.conf", "/no/such/dir", - "--plugins", "alpha,beta,gamma,delta", - "--connectivity-interval", "12", - NULL); - config = nm_config_new (&error); - g_assert_no_error (error); + config = setup_config (NULL, SRCDIR "/NetworkManager.conf", "/no/such/dir", + "--plugins", "alpha,beta,gamma,delta", + "--connectivity-interval", "12", + NULL); - g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf"); + g_assert_cmpstr (nm_config_data_get_config_main_file (nm_config_get_data_orig (config)), ==, SRCDIR "/NetworkManager.conf"); g_assert_cmpstr (nm_config_get_dhcp_client (config), ==, "dhclient"); g_assert_cmpstr (nm_config_get_log_level (config), ==, "INFO"); - g_assert_cmpint (nm_config_get_connectivity_interval (config), ==, 12); + g_assert_cmpint (nm_config_data_get_connectivity_interval (nm_config_get_data_orig (config)), ==, 12); plugins = nm_config_get_plugins (config); g_assert_cmpint (g_strv_length ((char **)plugins), ==, 4); @@ -179,37 +235,36 @@ test_config_no_auto_default (void) g_assert_cmpint (nwrote, ==, 18); close (fd); - setup_config (SRCDIR "/NetworkManager.conf", "/no/such/dir", - "--no-auto-default", state_file, - NULL); - config = nm_config_new (&error); - g_assert_no_error (error); + config = setup_config (NULL, SRCDIR "/NetworkManager.conf", "/no/such/dir", + "--no-auto-default", state_file, + NULL); dev1 = nm_test_device_new ("11:11:11:11:11:11"); dev2 = nm_test_device_new ("22:22:22:22:22:22"); dev3 = nm_test_device_new ("33:33:33:33:33:33"); dev4 = nm_test_device_new ("44:44:44:44:44:44"); - g_assert (!nm_config_get_ethernet_can_auto_default (config, dev1)); - g_assert (!nm_config_get_ethernet_can_auto_default (config, dev2)); - g_assert (nm_config_get_ethernet_can_auto_default (config, dev3)); - g_assert (!nm_config_get_ethernet_can_auto_default (config, dev4)); + g_assert (nm_config_get_no_auto_default_for_device (config, dev1)); + g_assert (nm_config_get_no_auto_default_for_device (config, dev2)); + g_assert (!nm_config_get_no_auto_default_for_device (config, dev3)); + g_assert (nm_config_get_no_auto_default_for_device (config, dev4)); + + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_MESSAGE, "*config: update * (no-auto-default)*"); + nm_config_set_no_auto_default_for_device (config, dev3); + g_test_assert_expected_messages (); - nm_config_set_ethernet_no_auto_default (config, dev3); - g_assert (!nm_config_get_ethernet_can_auto_default (config, dev3)); + g_assert (nm_config_get_no_auto_default_for_device (config, dev3)); g_object_unref (config); - setup_config (SRCDIR "/NetworkManager.conf", "/no/such/dir", - "--no-auto-default", state_file, - NULL); - config = nm_config_new (&error); - g_assert_no_error (error); + config = setup_config (NULL, SRCDIR "/NetworkManager.conf", "/no/such/dir", + "--no-auto-default", state_file, + NULL); - g_assert (!nm_config_get_ethernet_can_auto_default (config, dev1)); - g_assert (!nm_config_get_ethernet_can_auto_default (config, dev2)); - g_assert (!nm_config_get_ethernet_can_auto_default (config, dev3)); - g_assert (!nm_config_get_ethernet_can_auto_default (config, dev4)); + g_assert (nm_config_get_no_auto_default_for_device (config, dev1)); + g_assert (nm_config_get_no_auto_default_for_device (config, dev2)); + g_assert (nm_config_get_no_auto_default_for_device (config, dev3)); + g_assert (nm_config_get_no_auto_default_for_device (config, dev4)); g_object_unref (config); @@ -226,20 +281,18 @@ static void test_config_confdir (void) { NMConfig *config; - GError *error = NULL; const char **plugins; char *value; + GSList *specs; - setup_config (SRCDIR "/NetworkManager.conf", SRCDIR "/conf.d", NULL); - config = nm_config_new (&error); - g_assert_no_error (error); + config = setup_config (NULL, SRCDIR "/NetworkManager.conf", SRCDIR "/conf.d", NULL); - g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf"); + g_assert_cmpstr (nm_config_data_get_config_main_file (nm_config_get_data_orig (config)), ==, SRCDIR "/NetworkManager.conf"); g_assert_cmpstr (nm_config_get_dhcp_client (config), ==, "dhcpcd"); g_assert_cmpstr (nm_config_get_log_level (config), ==, "INFO"); g_assert_cmpstr (nm_config_get_log_domains (config), ==, "PLATFORM,DNS,WIFI"); - g_assert_cmpstr (nm_config_get_connectivity_uri (config), ==, "http://example.net"); - g_assert_cmpint (nm_config_get_connectivity_interval (config), ==, 100); + g_assert_cmpstr (nm_config_data_get_connectivity_uri (nm_config_get_data_orig (config)), ==, "http://example.net"); + g_assert_cmpint (nm_config_data_get_connectivity_interval (nm_config_get_data_orig (config)), ==, 100); plugins = nm_config_get_plugins (config); g_assert_cmpint (g_strv_length ((char **)plugins), ==, 5); @@ -249,38 +302,98 @@ test_config_confdir (void) g_assert_cmpstr (plugins[3], ==, "one"); g_assert_cmpstr (plugins[4], ==, "two"); - value = nm_config_get_value (config, "main", "extra", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "extra", NM_CONFIG_GET_VALUE_NONE); g_assert_cmpstr (value, ==, "hello"); g_free (value); - value = nm_config_get_value (config, "main", "new", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "no-auto-default", NM_CONFIG_GET_VALUE_TYPE_SPEC); + specs = nm_match_spec_split (value); + g_free (value); + g_assert_cmpint (g_slist_length (specs), ==, 2); + g_assert_cmpstr (g_slist_nth_data (specs, 0), ==, "spec2"); + g_assert_cmpstr (g_slist_nth_data (specs, 1), ==, "spec3"); + g_slist_free_full (specs, g_free); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "ignore-carrier", NM_CONFIG_GET_VALUE_TYPE_SPEC); + specs = nm_match_spec_split (value); + g_free (value); + g_assert_cmpint (g_slist_length (specs), ==, 2); + g_assert_cmpstr (g_slist_nth_data (specs, 0), ==, " space1 "); + g_assert_cmpstr (g_slist_nth_data (specs, 1), ==, " space2\t"); + g_slist_free_full (specs, g_free); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), NM_CONFIG_KEYFILE_GROUPPREFIX_TEST_APPEND_STRINGLIST".0", "new", NM_CONFIG_GET_VALUE_NONE); g_assert_cmpstr (value, ==, "something"); /* not ",something" */ g_free (value); - value = nm_config_get_value (config, "order", "a", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "a", NM_CONFIG_GET_VALUE_NONE); g_assert_cmpstr (value, ==, "90"); g_free (value); - value = nm_config_get_value (config, "order", "b", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "b", NM_CONFIG_GET_VALUE_NONE); g_assert_cmpstr (value, ==, "10"); g_free (value); - value = nm_config_get_value (config, "order", "c", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "c", NM_CONFIG_GET_VALUE_NONE); g_assert_cmpstr (value, ==, "0"); g_free (value); + g_assert (!nm_config_data_has_value (nm_config_get_data_orig (config), "appendable-test", "non-appendable-key1", NM_CONFIG_GET_VALUE_RAW)); + g_assert (!nm_config_data_has_value (nm_config_get_data_orig (config), "appendable-test", "non-appendable-key1+", NM_CONFIG_GET_VALUE_RAW)); + g_assert (!nm_config_data_has_value (nm_config_get_data_orig (config), "appendable-test", "non-appendable-key1-", NM_CONFIG_GET_VALUE_RAW)); + g_assert (!nm_config_data_has_value (nm_config_get_data_orig (config), "appendable-test", "non-appendable-key2", NM_CONFIG_GET_VALUE_RAW)); + g_assert (!nm_config_data_has_value (nm_config_get_data_orig (config), "appendable-test", "non-appendable-key2+", NM_CONFIG_GET_VALUE_RAW)); + g_assert (!nm_config_data_has_value (nm_config_get_data_orig (config), "appendable-test", "non-appendable-key2-", NM_CONFIG_GET_VALUE_RAW)); + +#define ASSERT_GET_CONN_DEFAULT(xconfig, xname, xvalue) \ + G_STMT_START { \ + gs_free char *_value = nm_config_data_get_connection_default (nm_config_get_data_orig (xconfig), (xname), NULL); \ + g_assert_cmpstr (_value, ==, (xvalue)); \ + } G_STMT_END + ASSERT_GET_CONN_DEFAULT (config, "ord.key00", "A-0.0.00"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key01", "A-0.3.01"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key02", "A-0.2.02"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key03", "A-0.1.03"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key04", "B-1.3.04"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key05", "B-1.2.05"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key06", "B-1.1.06"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key07", "C-2.3.07"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key08", "C-2.2.08"); + ASSERT_GET_CONN_DEFAULT (config, "ord.key09", "C-2.1.09"); + ASSERT_GET_CONN_DEFAULT (config, "ord.ovw01", "C-0.1.ovw01"); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), NM_CONFIG_KEYFILE_GROUPPREFIX_TEST_APPEND_STRINGLIST".1", "val1", NM_CONFIG_GET_VALUE_NONE); + g_assert_cmpstr (value, ==, "a,c"); + g_free (value); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), NM_CONFIG_KEYFILE_GROUPPREFIX_TEST_APPEND_STRINGLIST".1", "val2", NM_CONFIG_GET_VALUE_NONE); + g_assert_cmpstr (value, ==, "VAL2"); + g_free (value); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), NM_CONFIG_KEYFILE_GROUPPREFIX_TEST_APPEND_STRINGLIST".1", "val3", NM_CONFIG_GET_VALUE_NONE); + g_assert_cmpstr (value, ==, NULL); + g_free (value); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), NM_CONFIG_KEYFILE_GROUPPREFIX_TEST_APPEND_STRINGLIST".1", "val4", NM_CONFIG_GET_VALUE_NONE); + g_assert_cmpstr (value, ==, "vb,vb"); + g_free (value); + + value = nm_config_data_get_value (nm_config_get_data_orig (config), NM_CONFIG_KEYFILE_GROUPPREFIX_TEST_APPEND_STRINGLIST".1", "val5", NM_CONFIG_GET_VALUE_NONE); + g_assert_cmpstr (value, ==, "VAL5"); + g_free (value); + + nm_config_data_log (nm_config_get_data_orig (config), ">>> TEST: "); + g_object_unref (config); } static void test_config_confdir_parse_error (void) { - NMConfig *config; - gs_free_error GError *error = NULL; + GError *error = NULL; /* Using SRCDIR as the conf dir will pick up bad.conf */ - setup_config (SRCDIR "/NetworkManager.conf", SRCDIR, NULL); - config = nm_config_new (&error); - g_assert (!config); + setup_config (&error, SRCDIR "/NetworkManager.conf", SRCDIR, NULL); g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE); + g_clear_error (&error); } NMTST_DEFINE (); |