diff options
author | Thomas Haller <thaller@redhat.com> | 2021-09-16 16:50:14 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-09-21 08:29:39 +0200 |
commit | f88a60805003ca44912c52e399638438033df356 (patch) | |
tree | e548fc18aebe7b82835e6028807c618d9ff998a8 | |
parent | a8866095dd656ce187c4ed2951f60ebac7f440ff (diff) | |
download | NetworkManager-f88a60805003ca44912c52e399638438033df356.tar.gz |
initrd/tests: refactor tests for "rd.ethtool" (test_rd_ethtool())
It tests the same input as before (except, dropping the duplicate test
for "rd.ethtool=eth0:on:100:bogus").
-rw-r--r-- | src/nm-initrd-generator/tests/test-cmdline-reader.c | 216 |
1 files changed, 63 insertions, 153 deletions
diff --git a/src/nm-initrd-generator/tests/test-cmdline-reader.c b/src/nm-initrd-generator/tests/test-cmdline-reader.c index 6bc3052265..63c29e3f0d 100644 --- a/src/nm-initrd-generator/tests/test-cmdline-reader.c +++ b/src/nm-initrd-generator/tests/test-cmdline-reader.c @@ -2272,203 +2272,113 @@ test_carrier_timeout(void) g_assert_cmpint(carrier_timeout_sec, ==, 20); } -/* Obs1.: this function is implemented as macro, and not as a function, - * to show the correct line in g_assert() debug */ -#define _ethtool_connection_check_and_get(connection) \ - ({ \ - NMSettingWired *_s_wired = NULL; \ - NMConnection * _connection = connection; \ - \ - g_assert(nm_connection_get_setting_connection(_connection)); \ - g_assert(nm_connection_is_type(_connection, NM_SETTING_WIRED_SETTING_NAME)); \ - g_assert(nm_connection_get_setting_ip4_config(_connection)); \ - g_assert(nm_connection_get_setting_ip6_config(_connection)); \ - _s_wired = nm_connection_get_setting_wired(_connection); \ - g_assert(NM_IS_SETTING_WIRED(_s_wired)); \ - \ - _s_wired; \ - }) +#define _ethtool_check_inval(arg) \ + G_STMT_START \ + { \ + gs_unref_hashtable GHashTable *_connections2 = NULL; \ + \ + _connections2 = _parse_cons(NM_MAKE_STRV(arg)); \ + \ + g_test_assert_expected_messages(); \ + \ + g_assert_cmpint(g_hash_table_size(_connections2), ==, 0); \ + } \ + G_STMT_END + +#define _ethtool_check_v(strv, autoneg, speed) \ + G_STMT_START \ + { \ + gs_unref_object NMConnection *_connection = NULL; \ + NMSettingWired * _s_wired; \ + typeof(speed) _speed = speed; \ + \ + _connection = _parse_con(strv, "eth0"); \ + \ + g_test_assert_expected_messages(); \ + \ + g_assert(nm_connection_get_setting_connection(_connection)); \ + g_assert(nm_connection_is_type(_connection, NM_SETTING_WIRED_SETTING_NAME)); \ + g_assert(nm_connection_get_setting_ip4_config(_connection)); \ + g_assert(nm_connection_get_setting_ip6_config(_connection)); \ + _s_wired = nm_connection_get_setting_wired(_connection); \ + g_assert(NM_IS_SETTING_WIRED(_s_wired)); \ + \ + g_assert_cmpint(nm_setting_wired_get_auto_negotiate(_s_wired), ==, (autoneg)); \ + g_assert_cmpint(nm_setting_wired_get_speed(_s_wired), ==, _speed); \ + g_assert_cmpstr(nm_setting_wired_get_duplex(_s_wired), ==, (_speed == 0 ? NULL : "full")); \ + } \ + G_STMT_END + +#define _ethtool_check(arg, autoneg, speed) \ + _ethtool_check_v(NM_MAKE_STRV("" arg ""), (autoneg), (speed)) static void test_rd_ethtool(void) { - const char *const *ARGV = NULL; - NMConnection * connection = NULL; - GHashTable * connections = NULL; - NMSettingWired * s_wired = NULL; - - ARGV = NM_MAKE_STRV("rd.ethtool="); NMTST_EXPECT_NM_WARN("cmdline-reader: Impossible to set rd.ethtool options: invalid format"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_hash_table_unref(connections); - g_test_assert_expected_messages(); + _ethtool_check_inval("rd.ethtool="); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0"); NMTST_EXPECT_NM_WARN("cmdline-reader: Could not find rd.ethtool options to set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_hash_table_unref(connections); - g_test_assert_expected_messages(); + _ethtool_check_inval("rd.ethtool=eth0"); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:"); NMTST_EXPECT_NM_WARN("cmdline-reader: Could not find rd.ethtool options to set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_hash_table_unref(connections); - g_test_assert_expected_messages(); + _ethtool_check_inval("rd.ethtool=eth0:"); - ARGV = NM_MAKE_STRV("rd.ethtool=::"); NMTST_EXPECT_NM_WARN("cmdline-reader: Impossible to set rd.ethtool options: invalid format"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check_inval("rd.ethtool=::"); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:on"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:on", TRUE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:off"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:off", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:true"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:true", TRUE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:false"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:false", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:1"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:1", TRUE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:0"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:0", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:randomstring"); NMTST_EXPECT_NM_WARN( "cmdline-reader: Invalid value for rd.ethtool.autoneg, rd.ethtool.autoneg was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0:randomstring", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0::", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::astring"); NMTST_EXPECT_NM_WARN( "cmdline-reader: Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0::astring", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::1000000000000000000000000000000000000"); NMTST_EXPECT_NM_WARN( "cmdline-reader: Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0::1000000000000000000000000000000000000", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::0.67"); NMTST_EXPECT_NM_WARN( "cmdline-reader: Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0::0.67", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::-23"); NMTST_EXPECT_NM_WARN( "cmdline-reader: Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0::-23", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:1:10"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 10); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:1:10", TRUE, 10); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::100"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 100); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0::100", FALSE, 100); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:::bogus"); NMTST_EXPECT_NM_WARN( "cmdline-reader: Invalid extra argument 'bogus' for rd.ethtool, this value was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0:::bogus", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::10:bogus"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid extra argument 'bogus' for rd.ethtool, this value was not set"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 10); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_test_assert_expected_messages(); - g_object_unref(connection); - - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:on:100:bogus"); NMTST_EXPECT_NM_WARN( "cmdline-reader: Invalid extra argument 'bogus' for rd.ethtool, this value was not set"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 100); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_test_assert_expected_messages(); - g_object_unref(connection); - - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:on:100:bogus"); + _ethtool_check("rd.ethtool=eth0::10:bogus", FALSE, 10); + NMTST_EXPECT_NM_WARN( "cmdline-reader: Invalid extra argument 'bogus' for rd.ethtool, this value was not set"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 100); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_test_assert_expected_messages(); - g_object_unref(connection); - - ARGV = NM_MAKE_STRV("rd.ethtool=:::"); + _ethtool_check("rd.ethtool=eth0:on:100:bogus", TRUE, 100); + NMTST_EXPECT_NM_WARN("cmdline-reader: Impossible to set rd.ethtool options: invalid format"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check_inval("rd.ethtool=:::"); } /*****************************************************************************/ |