From dce6599ec0923b721c356b46cedf81d77433b54a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Wed, 21 May 2014 14:09:23 +0200 Subject: [PATCH] keyfile: fix reading MAC in old format (list of integers) Don't call nm_utils_hwaddr_type () with random len, because it causes ugly (NetworkManager:25325): libnm-util-CRITICAL **: file nm-utils.c: line 1989 (nm_utils_hwaddr_type): should not be reached And add a testcase. https://bugzilla.gnome.org/show_bug.cgi?id=730514 --- src/settings/plugins/keyfile/reader.c | 8 +- .../keyfile/tests/keyfiles/Makefile.am | 2 + .../tests/keyfiles/Test_MAC_IB_Old_Format | 13 ++++ .../tests/keyfiles/Test_MAC_Old_Format | 10 +++ .../plugins/keyfile/tests/test-keyfile.c | 77 ++++++++++++++++++- 5 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 src/settings/plugins/keyfile/tests/keyfiles/Test_MAC_IB_Old_Format create mode 100644 src/settings/plugins/keyfile/tests/keyfiles/Test_MAC_Old_Format diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 5ceb764c6..735cbfc54 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -554,17 +554,17 @@ mac_address_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, cons } /* If we found enough it's probably a string-format MAC address */ - type = nm_utils_hwaddr_type (i + 1); - if (type > 0) + if (i+1 == ETH_ALEN || i+1 == INFINIBAND_ALEN) { + type = nm_utils_hwaddr_type (i + 1); array = nm_utils_hwaddr_atoba (tmp_string, type); + } } g_free (tmp_string); if (array == NULL) { /* Old format; list of ints */ tmp_list = nm_keyfile_plugin_kf_get_integer_list (keyfile, setting_name, key, &length, NULL); - type = nm_utils_hwaddr_type (length); - if (type < 0) { + if (length == ETH_ALEN || length == INFINIBAND_ALEN) { array = g_byte_array_sized_new (length); for (i = 0; i < length; i++) { int val = tmp_list[i]; diff --git a/src/settings/plugins/keyfile/tests/keyfiles/Makefile.am b/src/settings/plugins/keyfile/tests/keyfiles/Makefile.am index 2ac304e10..576164d27 100644 --- a/src/settings/plugins/keyfile/tests/keyfiles/Makefile.am +++ b/src/settings/plugins/keyfile/tests/keyfiles/Makefile.am @@ -3,6 +3,8 @@ KEYFILES = \ Test_GSM_Connection \ Test_Wireless_Connection \ Test_Wired_Connection_MAC_Case \ + Test_MAC_Old_Format \ + Test_MAC_IB_Old_Format \ Test_Wired_Connection_IP6 \ ATT_Data_Connect_BT \ ATT_Data_Connect_Plain \ diff --git a/src/settings/plugins/keyfile/tests/keyfiles/Test_MAC_IB_Old_Format b/src/settings/plugins/keyfile/tests/keyfiles/Test_MAC_IB_Old_Format new file mode 100644 index 000000000..b2bf91550 --- /dev/null +++ b/src/settings/plugins/keyfile/tests/keyfiles/Test_MAC_IB_Old_Format @@ -0,0 +1,13 @@ +[connection] +id=Test InfiniBand Connection +uuid=5680a56d-c99f-45ad-a6dd-b44d5c398c12 +type=infiniband + +[infiniband] +mac-address=0;17;34;51;68;85;102;119;136;153;1;18;35;52;69;86;103;120;137;144; +transport-mode=datagram +mtu=1400 + +[ipv4] +method=auto + diff --git a/src/settings/plugins/keyfile/tests/keyfiles/Test_MAC_Old_Format b/src/settings/plugins/keyfile/tests/keyfiles/Test_MAC_Old_Format new file mode 100644 index 000000000..9427b16c5 --- /dev/null +++ b/src/settings/plugins/keyfile/tests/keyfiles/Test_MAC_Old_Format @@ -0,0 +1,10 @@ +[connection] +id=Test MAC Old Format +uuid=8980a26d-c99f-4aad-a6bd-b439bc348ca4 +type=802-3-ethernet + +[802-3-ethernet] +mac-address=00:11:aa:BB:CC:55 +cloned-mac-address=00;22;170;187;204;254; +mtu=1400 + diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index f7d412290..a300163b7 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -15,7 +15,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2008 - 2011 Red Hat, Inc. + * Copyright (C) 2008 - 2014 Red Hat, Inc. */ #include @@ -968,6 +968,79 @@ test_read_wired_mac_case (void) g_object_unref (connection); } +#define TEST_MAC_OLD_FORMAT_FILE TEST_KEYFILES_DIR"/Test_MAC_Old_Format" + +static void +test_read_mac_old_format (void) +{ + NMConnection *connection; + NMSettingWired *s_wired; + GError *error = NULL; + gboolean success; + const GByteArray *array; + char expected_mac[ETH_ALEN] = { 0x00, 0x11, 0xaa, 0xbb, 0xcc, 0x55 }; + char expected_cloned_mac[ETH_ALEN] = { 0x00, 0x16, 0xaa, 0xbb, 0xcc, 0xfe }; + + connection = nm_keyfile_plugin_connection_from_file (TEST_MAC_OLD_FORMAT_FILE, &error); + g_assert_no_error (error); + g_assert (connection); + + success = nm_connection_verify (connection, &error); + g_assert_no_error (error); + g_assert (success); + + s_wired = nm_connection_get_setting_wired (connection); + g_assert (s_wired); + + /* MAC address */ + array = nm_setting_wired_get_mac_address (s_wired); + g_assert (array); + g_assert_cmpint (array->len, ==, ETH_ALEN); + g_assert (memcmp (array->data, expected_mac, ETH_ALEN) == 0); + + /* Cloned MAC address */ + array = nm_setting_wired_get_cloned_mac_address (s_wired); + g_assert (array); + g_assert_cmpint (array->len, ==, ETH_ALEN); + g_assert (memcmp (array->data, expected_cloned_mac, ETH_ALEN) == 0); + + g_object_unref (connection); +} + +#define TEST_MAC_IB_OLD_FORMAT_FILE TEST_KEYFILES_DIR"/Test_MAC_IB_Old_Format" + +static void +test_read_mac_ib_old_format (void) +{ + NMConnection *connection; + NMSettingInfiniband *s_ib; + GError *error = NULL; + gboolean success; + const GByteArray *array; + guint8 expected_mac[INFINIBAND_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, + 0x77, 0x88, 0x99, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, + 0x90 }; + + connection = nm_keyfile_plugin_connection_from_file (TEST_MAC_IB_OLD_FORMAT_FILE, &error); + g_assert_no_error (error); + g_assert (connection); + + success = nm_connection_verify (connection, &error); + g_assert_no_error (error); + g_assert (success); + + s_ib = nm_connection_get_setting_infiniband (connection); + g_assert (s_ib); + + /* MAC address */ + array = nm_setting_infiniband_get_mac_address (s_ib); + g_assert (array); + g_assert_cmpint (array->len, ==, INFINIBAND_ALEN); + g_assert_cmpint (memcmp (array->data, expected_mac, sizeof (expected_mac)), ==, 0); + + g_object_unref (connection); +} + static void test_read_valid_wireless_connection (void) { @@ -3359,6 +3432,8 @@ int main (int argc, char **argv) test_write_ip6_wired_connection (); test_read_wired_mac_case (); + test_read_mac_old_format (); + test_read_mac_ib_old_format (); test_read_valid_wireless_connection (); test_write_wireless_connection ();