diff options
Diffstat (limited to 'src/settings/plugins/ifupdown/tests')
| -rw-r--r-- | src/settings/plugins/ifupdown/tests/meson.build | 1 | ||||
| -rw-r--r-- | src/settings/plugins/ifupdown/tests/test-ifupdown.c | 259 |
2 files changed, 113 insertions, 147 deletions
diff --git a/src/settings/plugins/ifupdown/tests/meson.build b/src/settings/plugins/ifupdown/tests/meson.build index ee3b6a34..5a2383d9 100644 --- a/src/settings/plugins/ifupdown/tests/meson.build +++ b/src/settings/plugins/ifupdown/tests/meson.build @@ -4,7 +4,6 @@ exe = executable( test_unit, test_unit + '.c', dependencies: test_nm_dep, - c_args: nm_build_cflags, link_with: libnms_ifupdown_core ) diff --git a/src/settings/plugins/ifupdown/tests/test-ifupdown.c b/src/settings/plugins/ifupdown/tests/test-ifupdown.c index 18bad65e..82ee1c4a 100644 --- a/src/settings/plugins/ifupdown/tests/test-ifupdown.c +++ b/src/settings/plugins/ifupdown/tests/test-ifupdown.c @@ -130,46 +130,50 @@ expected_free (Expected *e) } static void -compare_expected_to_ifparser (Expected *e) +compare_expected_to_ifparser (if_parser *parser, Expected *e) { if_block *n; GSList *biter, *kiter; - g_assert_cmpint (g_slist_length (e->blocks), ==, ifparser_get_num_blocks ()); + g_assert_cmpint (g_slist_length (e->blocks), ==, ifparser_get_num_blocks (parser)); - for (n = ifparser_getfirst (), biter = e->blocks; - n && biter; - n = n->next, biter = g_slist_next (biter)) { + biter = e->blocks; + c_list_for_each_entry (n, &parser->block_lst_head, block_lst) { if_data *m; ExpectedBlock *b = biter->data; g_assert (b->type && n->type); g_assert_cmpstr (b->type, ==, n->type); - g_assert (b->name && n->name); + g_assert (b->name); g_assert_cmpstr (b->name, ==, n->name); g_assert_cmpint (g_slist_length (b->keys), ==, ifparser_get_num_info (n)); - for (m = n->info, kiter = b->keys; - m && kiter; - m = m->next, kiter = g_slist_next (kiter)) { + kiter = b->keys; + c_list_for_each_entry (m, &n->data_lst_head, data_lst) { ExpectedKey *k = kiter->data; - g_assert (k->key && m->key); + g_assert (k->key); g_assert_cmpstr (k->key, ==, m->key); g_assert (k->data && m->data); g_assert_cmpstr (k->data, ==, m->data); + + kiter = g_slist_next (kiter); } + g_assert (!kiter); + + biter = g_slist_next (biter); } + g_assert (!biter); } static void -dump_blocks (void) +dump_blocks (if_parser *parser) { if_block *n; g_message ("\n***************************************************"); - for (n = ifparser_getfirst (); n != NULL; n = n->next) { + c_list_for_each_entry (n, &parser->block_lst_head, block_lst) { if_data *m; // each block start with its type & name @@ -178,8 +182,8 @@ dump_blocks (void) // each key-value pair within a block is indented & separated by a tab // (single quotes used to show typ & name baoundaries) - for (m = n->info; m != NULL; m = m->next) - g_print("\t'%s'\t'%s'\n", m->key, m->data); + c_list_for_each_entry (m, &n->data_lst_head, data_lst) + g_print("\t'%s'\t'%s'\n", m->key, m->data); // blocks are separated by an empty line g_print("\n"); @@ -187,21 +191,24 @@ dump_blocks (void) g_message ("##################################################\n"); } -static void -init_ifparser_with_file (const char *path, const char *file) +static if_parser * +init_ifparser_with_file (const char *file) { - char *tmp; + if_parser *parser; + gs_free char *tmp = NULL; - tmp = g_strdup_printf ("%s/%s", path, file); - ifparser_init (tmp, 1); - g_free (tmp); + tmp = g_strdup_printf ("%s/%s", TEST_DIR, file); + parser = ifparser_parse (tmp, 1); + g_assert (parser); + return parser; } static void -test1_ignore_line_before_first_block (const char *path) +test1_ignore_line_before_first_block (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test1"); e = expected_new (); b = expected_block_new ("auto", "eth0"); @@ -210,35 +217,33 @@ test1_ignore_line_before_first_block (const char *path) expected_add_block (e, b); expected_block_add_key (b, expected_key_new ("inet", "dhcp")); - init_ifparser_with_file (path, "test1"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test2_wrapped_line (const char *path) +test2_wrapped_line (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test2"); e = expected_new (); b = expected_block_new ("auto", "lo"); expected_add_block (e, b); - init_ifparser_with_file (path, "test2"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test3_wrapped_multiline_multiarg (const char *path) +test3_wrapped_multiline_multiarg (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test3"); e = expected_new (); b = expected_block_new ("allow-hotplug", "eth0"); @@ -248,35 +253,33 @@ test3_wrapped_multiline_multiarg (const char *path) b = expected_block_new ("allow-hotplug", "bnep0"); expected_add_block (e, b); - init_ifparser_with_file (path, "test3"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test4_allow_auto_is_auto (const char *path) +test4_allow_auto_is_auto (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test4"); e = expected_new (); b = expected_block_new ("auto", "eth0"); expected_add_block (e, b); - init_ifparser_with_file (path, "test4"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test5_allow_auto_multiarg (const char *path) +test5_allow_auto_multiarg (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test5"); e = expected_new (); b = expected_block_new ("allow-hotplug", "eth0"); @@ -284,52 +287,50 @@ test5_allow_auto_multiarg (const char *path) b = expected_block_new ("allow-hotplug", "wlan0"); expected_add_block (e, b); - init_ifparser_with_file (path, "test5"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test6_mixed_whitespace (const char *path) +test6_mixed_whitespace (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test6"); e = expected_new (); b = expected_block_new ("iface", "lo"); expected_block_add_key (b, expected_key_new ("inet", "loopback")); expected_add_block (e, b); - init_ifparser_with_file (path, "test6"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test7_long_line (const char *path) +test7_long_line (void) { - init_ifparser_with_file (path, "test7"); - g_assert_cmpint (ifparser_get_num_blocks (), ==, 0); - ifparser_destroy (); + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test7"); + + g_assert_cmpint (ifparser_get_num_blocks (parser), ==, 0); } static void -test8_long_line_wrapped (const char *path) +test8_long_line_wrapped (void) { - init_ifparser_with_file (path, "test8"); - g_assert_cmpint (ifparser_get_num_blocks (), ==, 0); - ifparser_destroy (); + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test8"); + + g_assert_cmpint (ifparser_get_num_blocks (parser), ==, 0); } static void -test9_wrapped_lines_in_block (const char *path) +test9_wrapped_lines_in_block (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test9"); e = expected_new (); b = expected_block_new ("iface", "eth0"); @@ -340,18 +341,17 @@ test9_wrapped_lines_in_block (const char *path) expected_block_add_key (b, expected_key_new ("broadcast", "10.250.2.63")); expected_block_add_key (b, expected_key_new ("gateway", "10.250.2.50")); - init_ifparser_with_file (path, "test9"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test11_complex_wrap (const char *path) +test11_complex_wrap (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test11"); e = expected_new (); b = expected_block_new ("iface", "pppoe"); @@ -359,18 +359,17 @@ test11_complex_wrap (const char *path) expected_block_add_key (b, expected_key_new ("inet", "manual")); expected_block_add_key (b, expected_key_new ("pre-up", "/sbin/ifconfig eth0 up")); - init_ifparser_with_file (path, "test11"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test12_complex_wrap_split_word (const char *path) +test12_complex_wrap_split_word (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test12"); e = expected_new (); b = expected_block_new ("iface", "pppoe"); @@ -378,36 +377,34 @@ test12_complex_wrap_split_word (const char *path) expected_block_add_key (b, expected_key_new ("inet", "manual")); expected_block_add_key (b, expected_key_new ("up", "ifup ppp0=dsl")); - init_ifparser_with_file (path, "test12"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test13_more_mixed_whitespace (const char *path) +test13_more_mixed_whitespace (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test13"); e = expected_new (); b = expected_block_new ("iface", "dsl"); expected_block_add_key (b, expected_key_new ("inet", "ppp")); expected_add_block (e, b); - init_ifparser_with_file (path, "test13"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test14_mixed_whitespace_block_start (const char *path) +test14_mixed_whitespace_block_start (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test14"); e = expected_new (); b = expected_block_new ("iface", "wlan0"); @@ -420,47 +417,43 @@ test14_mixed_whitespace_block_start (const char *path) expected_block_add_key (b, expected_key_new ("inet", "dhcp")); expected_add_block (e, b); - init_ifparser_with_file (path, "test14"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test15_trailing_space (const char *path) +test15_trailing_space (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test15"); e = expected_new (); b = expected_block_new ("iface", "bnep0"); expected_block_add_key (b, expected_key_new ("inet", "static")); expected_add_block (e, b); - init_ifparser_with_file (path, "test15"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test16_missing_newline (const char *path) +test16_missing_newline (void) { Expected *e; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test16"); e = expected_new (); expected_add_block (e, expected_block_new ("mapping", "eth0")); - init_ifparser_with_file (path, "test16"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test17_read_static_ipv4 (const char *path) +test17_read_static_ipv4 (void) { NMConnection *connection; NMSettingConnection *s_con; @@ -470,13 +463,13 @@ test17_read_static_ipv4 (const char *path) gboolean success; NMIPAddress *ip4_addr; if_block *block = NULL; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test17-wired-static-verify-ip4"); - init_ifparser_with_file (path, "test17-wired-static-verify-ip4"); - block = ifparser_getfirst (); + block = ifparser_getfirst (parser); connection = nm_simple_connection_new(); g_assert (connection); - ifupdown_update_connection_from_if_block(connection, block, &error); + ifupdown_update_connection_from_if_block (connection, block, &error); g_assert_no_error (error); success = nm_connection_verify (connection, &error); @@ -511,12 +504,11 @@ test17_read_static_ipv4 (const char *path) g_assert_cmpstr (nm_setting_ip_config_get_dns_search (s_ip4, 0), ==, "example.com"); g_assert_cmpstr (nm_setting_ip_config_get_dns_search (s_ip4, 1), ==, "foo.example.com"); - ifparser_destroy (); g_object_unref (connection); } static void -test18_read_static_ipv6 (const char *path) +test18_read_static_ipv6 (void) { NMConnection *connection; NMSettingConnection *s_con; @@ -526,12 +518,12 @@ test18_read_static_ipv6 (const char *path) gboolean success; NMIPAddress *ip6_addr; if_block *block = NULL; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test18-wired-static-verify-ip6"); - init_ifparser_with_file (path, "test18-wired-static-verify-ip6"); - block = ifparser_getfirst (); + block = ifparser_getfirst (parser); connection = nm_simple_connection_new(); g_assert (connection); - ifupdown_update_connection_from_if_block(connection, block, &error); + ifupdown_update_connection_from_if_block (connection, block, &error); g_assert_no_error (error); success = nm_connection_verify (connection, &error); @@ -566,12 +558,11 @@ test18_read_static_ipv6 (const char *path) g_assert_cmpstr (nm_setting_ip_config_get_dns_search (s_ip6, 0), ==, "example.com"); g_assert_cmpstr (nm_setting_ip_config_get_dns_search (s_ip6, 1), ==, "foo.example.com"); - ifparser_destroy (); g_object_unref (connection); } static void -test19_read_static_ipv4_plen (const char *path) +test19_read_static_ipv4_plen (void) { NMConnection *connection; NMSettingIPConfig *s_ip4; @@ -579,12 +570,12 @@ test19_read_static_ipv4_plen (const char *path) NMIPAddress *ip4_addr; if_block *block = NULL; gboolean success; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test19-wired-static-verify-ip4-plen"); - init_ifparser_with_file (path, "test19-wired-static-verify-ip4-plen"); - block = ifparser_getfirst (); + block = ifparser_getfirst (parser); connection = nm_simple_connection_new(); g_assert (connection); - ifupdown_update_connection_from_if_block(connection, block, &error); + ifupdown_update_connection_from_if_block (connection, block, &error); g_assert_no_error (error); success = nm_connection_verify (connection, &error); @@ -601,15 +592,15 @@ test19_read_static_ipv4_plen (const char *path) g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); - ifparser_destroy (); g_object_unref (connection); } static void -test20_source_stanza (const char *path) +test20_source_stanza (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test20-source-stanza"); e = expected_new (); @@ -625,18 +616,17 @@ test20_source_stanza (const char *path) expected_add_block (e, b); expected_block_add_key (b, expected_key_new ("inet", "dhcp")); - init_ifparser_with_file (path, "test20-source-stanza"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } static void -test21_source_dir_stanza (const char *path) +test21_source_dir_stanza (void) { Expected *e; ExpectedBlock *b; + nm_auto_ifparser if_parser *parser = init_ifparser_with_file ("test21-source-dir-stanza"); e = expected_new (); @@ -646,10 +636,8 @@ test21_source_dir_stanza (const char *path) expected_add_block (e, b); expected_block_add_key (b, expected_key_new ("inet", "dhcp")); - init_ifparser_with_file (path, "test21-source-dir-stanza"); - compare_expected_to_ifparser (e); + compare_expected_to_ifparser (parser, e); - ifparser_destroy (); expected_free (e); } @@ -660,49 +648,28 @@ main (int argc, char **argv) { nmtst_init_assert_logging (&argc, &argv, "WARN", "DEFAULT"); - if (0) - dump_blocks (); - - g_test_add_data_func ("/ifupdate/ignore_line_before_first_block", TEST_DIR, - (GTestDataFunc) test1_ignore_line_before_first_block); - g_test_add_data_func ("/ifupdate/wrapped_line", TEST_DIR, - (GTestDataFunc) test2_wrapped_line); - g_test_add_data_func ("/ifupdate/wrapped_multiline_multiarg", TEST_DIR, - (GTestDataFunc) test3_wrapped_multiline_multiarg); - g_test_add_data_func ("/ifupdate/allow_auto_is_auto", TEST_DIR, - (GTestDataFunc) test4_allow_auto_is_auto); - g_test_add_data_func ("/ifupdate/allow_auto_multiarg", TEST_DIR, - (GTestDataFunc) test5_allow_auto_multiarg); - g_test_add_data_func ("/ifupdate/mixed_whitespace", TEST_DIR, - (GTestDataFunc) test6_mixed_whitespace); - g_test_add_data_func ("/ifupdate/long_line", TEST_DIR, - (GTestDataFunc) test7_long_line); - g_test_add_data_func ("/ifupdate/long_line_wrapped", TEST_DIR, - (GTestDataFunc) test8_long_line_wrapped); - g_test_add_data_func ("/ifupdate/wrapped_lines_in_block", TEST_DIR, - (GTestDataFunc) test9_wrapped_lines_in_block); - g_test_add_data_func ("/ifupdate/complex_wrap", TEST_DIR, - (GTestDataFunc) test11_complex_wrap); - g_test_add_data_func ("/ifupdate/complex_wrap_split_word", TEST_DIR, - (GTestDataFunc) test12_complex_wrap_split_word); - g_test_add_data_func ("/ifupdate/more_mixed_whitespace", TEST_DIR, - (GTestDataFunc) test13_more_mixed_whitespace); - g_test_add_data_func ("/ifupdate/mixed_whitespace_block_start", TEST_DIR, - (GTestDataFunc) test14_mixed_whitespace_block_start); - g_test_add_data_func ("/ifupdate/trailing_space", TEST_DIR, - (GTestDataFunc) test15_trailing_space); - g_test_add_data_func ("/ifupdate/missing_newline", TEST_DIR, - (GTestDataFunc) test16_missing_newline); - g_test_add_data_func ("/ifupdate/read_static_ipv4", TEST_DIR, - (GTestDataFunc) test17_read_static_ipv4); - g_test_add_data_func ("/ifupdate/read_static_ipv6", TEST_DIR, - (GTestDataFunc) test18_read_static_ipv6); - g_test_add_data_func ("/ifupdate/read_static_ipv4_plen", TEST_DIR, - (GTestDataFunc) test19_read_static_ipv4_plen); - g_test_add_data_func ("/ifupdate/source_stanza", TEST_DIR, - (GTestDataFunc) test20_source_stanza); - g_test_add_data_func ("/ifupdate/source_dir_stanza", TEST_DIR, - (GTestDataFunc) test21_source_dir_stanza); + (void) dump_blocks; + + g_test_add_func ("/ifupdate/ignore_line_before_first_block", test1_ignore_line_before_first_block); + g_test_add_func ("/ifupdate/wrapped_line", test2_wrapped_line); + g_test_add_func ("/ifupdate/wrapped_multiline_multiarg", test3_wrapped_multiline_multiarg); + g_test_add_func ("/ifupdate/allow_auto_is_auto", test4_allow_auto_is_auto); + g_test_add_func ("/ifupdate/allow_auto_multiarg", test5_allow_auto_multiarg); + g_test_add_func ("/ifupdate/mixed_whitespace", test6_mixed_whitespace); + g_test_add_func ("/ifupdate/long_line", test7_long_line); + g_test_add_func ("/ifupdate/long_line_wrapped", test8_long_line_wrapped); + g_test_add_func ("/ifupdate/wrapped_lines_in_block", test9_wrapped_lines_in_block); + g_test_add_func ("/ifupdate/complex_wrap", test11_complex_wrap); + g_test_add_func ("/ifupdate/complex_wrap_split_word", test12_complex_wrap_split_word); + g_test_add_func ("/ifupdate/more_mixed_whitespace", test13_more_mixed_whitespace); + g_test_add_func ("/ifupdate/mixed_whitespace_block_start", test14_mixed_whitespace_block_start); + g_test_add_func ("/ifupdate/trailing_space", test15_trailing_space); + g_test_add_func ("/ifupdate/missing_newline", test16_missing_newline); + g_test_add_func ("/ifupdate/read_static_ipv4", test17_read_static_ipv4); + g_test_add_func ("/ifupdate/read_static_ipv6", test18_read_static_ipv6); + g_test_add_func ("/ifupdate/read_static_ipv4_plen", test19_read_static_ipv4_plen); + g_test_add_func ("/ifupdate/source_stanza", test20_source_stanza); + g_test_add_func ("/ifupdate/source_dir_stanza", test21_source_dir_stanza); return g_test_run (); } |