From 40b139bc65aa61ba61516b6cf9af133a4511c064 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Tue, 26 Nov 2024 17:49:01 +0100 Subject: [PATCH] keyfile: test that the output is stable We already check that a connection doesn't not change when it's written and re-read from disk. Add another check to verify that the generated keyfile matches a static one, so that we don't introduce unwanted changes. The reference keyfiles can be generated by running the test with "NM_TEST_REGENERATE=1". --- .../tests/keyfiles/Test_Write_Bluetooth_DUN | 24 +++++ .../keyfiles/Test_Write_Bridge_Component | 14 +++ .../tests/keyfiles/Test_Write_Bridge_Main | 19 ++++ .../keyfile/tests/keyfiles/Test_Write_Enum | 16 ++++ .../keyfile/tests/keyfiles/Test_Write_Flags | 18 ++++ .../keyfile/tests/keyfiles/Test_Write_GSM | 26 +++++ .../tests/keyfiles/Test_Write_Infiniband | 19 ++++ .../keyfile/tests/keyfiles/Test_Write_TC | 21 ++++ .../keyfile/tests/keyfiles/Test_Write_Wired | 35 +++++++ .../tests/keyfiles/Test_Write_Wired_IP6 | 19 ++++ .../tests/keyfiles/Test_Write_Wireless | 20 ++++ .../keyfile/tests/test-keyfile-settings.c | 95 ++++++++++++++----- 12 files changed, 300 insertions(+), 26 deletions(-) create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bluetooth_DUN create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bridge_Component create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bridge_Main create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Enum create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Flags create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_GSM create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Infiniband create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_TC create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wired create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wired_IP6 create mode 100644 src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wireless diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bluetooth_DUN b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bluetooth_DUN new file mode 100644 index 0000000000..80de393e0e --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bluetooth_DUN @@ -0,0 +1,24 @@ +[connection] +id=T-Mobile Funkadelic +uuid=76c59c25-c27c-57a4-8357-1409491cea45 +type=bluetooth +autoconnect=false +timestamp=305415219 + +[gsm] +apn=internet2.voicestream.com +password=parliament +username=george.clinton + +[bluetooth] +bdaddr=AA:B9:A1:74:55:44 +type=dun + +[ipv4] +method=auto + +[ipv6] +addr-gen-mode=default +method=auto + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bridge_Component b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bridge_Component new file mode 100644 index 0000000000..81e1ba8010 --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bridge_Component @@ -0,0 +1,14 @@ +[connection] +id=Test Write Bridge Component +uuid=7c4e34eb-419f-531c-b6ca-486f51a08d1d +type=ethernet +controller=br0 +port-type=bridge + +[ethernet] +mac-address=99:88:77:66:55:44 +mtu=1300 + +[bridge-port] +path-cost=99 +priority=3 diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bridge_Main b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bridge_Main new file mode 100644 index 0000000000..49a7372384 --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Bridge_Main @@ -0,0 +1,19 @@ +[connection] +id=Test Write Bridge Main +uuid=b23c15e0-815b-5e6e-a5f9-aea49237aa35 +type=bridge +interface-name=br0 + +[ethernet] + +[bridge] + +[ipv4] +address1=1.2.3.4/24,1.1.1.1 +method=manual + +[ipv6] +addr-gen-mode=default +method=auto + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Enum b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Enum new file mode 100644 index 0000000000..3ebd665750 --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Enum @@ -0,0 +1,16 @@ +[connection] +id=Test Write Enum Property +uuid=7e4cb57c-33ff-51fc-ae49-4ee414ef0b63 +type=ethernet + +[ethernet] + +[ipv4] +method=auto + +[ipv6] +addr-gen-mode=default +ip6-privacy=2 +method=auto + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Flags b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Flags new file mode 100644 index 0000000000..403a3fd22d --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Flags @@ -0,0 +1,18 @@ +[connection] +id=Test Write Flags Property +uuid=19febe12-db48-5661-94cc-16529548d772 +type=gsm + +[gsm] +apn=myapn +password-flags=6 +username=adfasdfasdf + +[ipv4] +method=auto + +[ipv6] +addr-gen-mode=default +method=auto + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_GSM b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_GSM new file mode 100644 index 0000000000..2f6dc26c9e --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_GSM @@ -0,0 +1,26 @@ +[connection] +id=T-Mobile Funkadelic 2 +uuid=952d369e-52c7-5686-ad8c-37ca7e47f8b2 +type=gsm +autoconnect=false +timestamp=305415219 + +[gsm] +apn=internet2.voicestream.com +device-id=da812de91eec16620b06cd0ca5cbc7ea25245222 +home-only=true +network-id=254098 +password=parliament2 +pin=123456 +sim-id=89148000000060671234 +sim-operator-id=310260 +username=george.clinton.again + +[ipv4] +method=auto + +[ipv6] +addr-gen-mode=default +method=auto + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Infiniband b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Infiniband new file mode 100644 index 0000000000..f72b669eed --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Infiniband @@ -0,0 +1,19 @@ +[connection] +id=Work InfiniBand +uuid=c27325f5-9eff-517a-9d96-d7897785fa5b +type=infiniband +autoconnect=false + +[infiniband] +mac-address=99:88:77:66:55:44:AB:BC:CD:DE:EF:F0:0A:1B:2C:3D:4E:5F:6F:BA +mtu=900 +transport-mode=datagram + +[ipv4] +method=auto + +[ipv6] +addr-gen-mode=default +method=auto + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_TC b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_TC new file mode 100644 index 0000000000..496e7bd952 --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_TC @@ -0,0 +1,21 @@ +[connection] +id=Test TC +uuid=ed1cb963-ff64-5129-99ca-e05ab3195ce4 +type=ethernet + +[ethernet] + +[ipv4] +method=auto + +[ipv6] +addr-gen-mode=default +method=auto + +[proxy] + +[tc] +qdisc.root=handle 1234: fq_codel +qdisc.ffff:fff1=ingress +tfilter.1234:=matchall action drop +tfilter.ffff:=matchall action simple sdata Hello diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wired b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wired new file mode 100644 index 0000000000..0b377c826b --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wired @@ -0,0 +1,35 @@ +[connection] +id=Work Wired +uuid=9342d47a-1bab-5709-9869-c840b2eac501 +type=ethernet +autoconnect=false +timestamp=305419896 + +[ethernet] +mac-address=99:88:77:66:55:44 +mtu=900 + +[ipv4] +address1=192.168.0.5/24,192.168.0.1 +address2=1.2.3.4/8 +dns=4.2.2.1;4.2.2.2; +method=manual +route1=10.10.10.2/24,10.10.10.1,3 +route2=1.1.1.1/8,1.2.1.1,1 +route3=2.2.2.2/7 +route4=3.3.3.3/6,0.0.0.0,4 +route4_options=cwnd=10,mtu=1492,src=1.2.3.4,weight=5 + +[ipv6] +addr-gen-mode=default +address1=abcd::beef/64 +address2=dcba::beef/56 +dns=1::cafe;2::cafe; +dns-search=wallaceandgromit.com; +method=manual +route1=1:2:3:4:5:6:7:8/64,8:7:6:5:4:3:2:1,3 +route2=2001::1000/56,2001::1111,1 +route3=4:5:6:7:8:9:0:1/63,::,5 +route4=5:6:7:8:9:0:1:2/62 + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wired_IP6 b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wired_IP6 new file mode 100644 index 0000000000..1efa7406c2 --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wired_IP6 @@ -0,0 +1,19 @@ +[connection] +id=Work Wired IP6 +uuid=0bef2d09-50a3-56b9-912a-5dc941284e3e +type=ethernet +autoconnect=false + +[ethernet] + +[ipv4] +method=disabled + +[ipv6] +addr-gen-mode=default +address1=abcd::beef/64,dcba::beef +dns=1::cafe; +dns-search=wallaceandgromit.com; +method=manual + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wireless b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wireless new file mode 100644 index 0000000000..2fc15098d0 --- /dev/null +++ b/src/core/settings/plugins/keyfile/tests/keyfiles/Test_Write_Wireless @@ -0,0 +1,20 @@ +[connection] +id=Work Wireless +uuid=e9b337d3-6aa0-552a-822c-4b71c8ddec2e +type=wifi +autoconnect=false +timestamp=305415219 + +[wifi] +bssid=AA:B9:A1:74:55:44 +mtu=1000 +ssid=1337 + +[ipv4] +method=auto + +[ipv6] +addr-gen-mode=default +method=auto + +[proxy] diff --git a/src/core/settings/plugins/keyfile/tests/test-keyfile-settings.c b/src/core/settings/plugins/keyfile/tests/test-keyfile-settings.c index f999105ad2..7f2927a86c 100644 --- a/src/core/settings/plugins/keyfile/tests/test-keyfile-settings.c +++ b/src/core/settings/plugins/keyfile/tests/test-keyfile-settings.c @@ -163,13 +163,48 @@ write_test_connection(NMConnection *connection, char **testfile) } static void -write_test_connection_and_reread(NMConnection *connection, gboolean normalize_connection) +write_test_connection_and_reread(NMConnection *connection, + gboolean normalize_connection, + const char *reference) { gs_free char *testfile = NULL; g_assert(NM_IS_CONNECTION(connection)); write_test_connection(connection, &testfile); + + if (reference) { + gs_free char *data1 = NULL; + gs_free char *data2 = NULL; + gsize len1; + gsize len2; + gs_free_error GError *error = NULL; + gboolean success; + + success = g_file_get_contents(testfile, &data1, &len1, &error); + nmtst_assert_success(success, error); + + if (nm_streq0(g_getenv("NM_TEST_REGENERATE"), "1")) { + success = g_file_set_contents(reference, data1, len1, &error); + nmtst_assert_success(success, error); + } else { + success = g_file_get_contents(reference, &data2, &len2, &error); + nmtst_assert_success(success, error); + + if (len1 != len2 || !nm_streq0(data1, data2)) { + g_error("The content of \"%s\" (%zu) differs from \"%s\" (%zu). Set " + "NM_TEST_REGENERATE=1 to update the files " + "in place\n\n>>>%s<<<\n\n>>>%s<<<\n", + testfile, + len1, + reference, + len2, + data1, + data2); + } + } + } + assert_reread_and_unlink(connection, normalize_connection, testfile); } @@ -408,13 +443,13 @@ add_one_ip_route(NMSettingIPConfig *s_ip, static void test_write_wired_connection(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; NMIPRoute *rt; + const char *uuid = "9342d47a-1bab-5709-9869-c840b2eac501"; const char *mac = "99:88:77:66:55:44"; const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; @@ -529,7 +564,7 @@ test_write_wired_connection(void) /* DNS searches */ nm_setting_ip_config_add_dns_search(s_ip6, "wallaceandgromit.com"); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, FALSE, TEST_KEYFILES_DIR "/Test_Write_Wired"); } static void @@ -573,12 +608,12 @@ test_read_ip6_wired_connection(void) static void test_write_ip6_wired_connection(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + const char *uuid = "0bef2d09-50a3-56b9-912a-5dc941284e3e"; const char *dns = "1::cafe"; const char *address = "abcd::beef"; const char *gw = "dcba::beef"; @@ -634,7 +669,7 @@ test_write_ip6_wired_connection(void) /* DNS searches */ nm_setting_ip_config_add_dns_search(s_ip6, "wallaceandgromit.com"); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, FALSE, TEST_KEYFILES_DIR "/Test_Write_Wired_IP6"); } static void @@ -746,12 +781,12 @@ test_read_valid_wireless_connection(void) static void test_write_wireless_connection(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + const char *uuid = "e9b337d3-6aa0-552a-822c-4b71c8ddec2e"; const char *bssid = "aa:b9:a1:74:55:44"; GBytes *ssid; unsigned char tmpssid[] = {0x31, 0x33, 0x33, 0x37}; @@ -809,7 +844,7 @@ test_write_wireless_connection(void) g_object_set(s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, FALSE, TEST_KEYFILES_DIR "/Test_Write_Wireless"); } static void @@ -1181,12 +1216,12 @@ test_read_bt_dun_connection(void) static void test_write_bt_dun_connection(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBluetooth *s_bt; NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; + const char *uuid = "76c59c25-c27c-57a4-8357-1409491cea45"; const char *bdaddr = "aa:b9:a1:74:55:44"; guint64 timestamp = 0x12344433L; @@ -1242,7 +1277,9 @@ test_write_bt_dun_connection(void) "parliament", NULL); - write_test_connection_and_reread(connection, TRUE); + write_test_connection_and_reread(connection, + TRUE, + TEST_KEYFILES_DIR "/Test_Write_Bluetooth_DUN"); } static void @@ -1286,12 +1323,12 @@ test_read_gsm_connection(void) static void test_write_gsm_connection(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; guint64 timestamp = 0x12344433L; + const char *uuid = "952d369e-52c7-5686-ad8c-37ca7e47f8b2"; connection = nm_simple_connection_new(); @@ -1345,7 +1382,7 @@ test_write_gsm_connection(void) "310260", NULL); - write_test_connection_and_reread(connection, TRUE); + write_test_connection_and_reread(connection, TRUE, TEST_KEYFILES_DIR "/Test_Write_GSM"); } static void @@ -1830,12 +1867,12 @@ test_read_infiniband_connection(void) static void test_write_infiniband_connection(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingInfiniband *s_ib; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + const char *uuid = "c27325f5-9eff-517a-9d96-d7897785fa5b"; const char *mac = "99:88:77:66:55:44:ab:bc:cd:de:ef:f0:0a:1b:2c:3d:4e:5f:6f:ba"; connection = nm_simple_connection_new(); @@ -1883,7 +1920,7 @@ test_write_infiniband_connection(void) nm_connection_add_setting(connection, NM_SETTING(s_ip6)); g_object_set(s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, FALSE, TEST_KEYFILES_DIR "/Test_Write_Infiniband"); } static void @@ -1922,13 +1959,13 @@ test_read_bridge_main(void) static void test_write_bridge_main(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBridge *s_bridge; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + const char *uuid = "b23c15e0-815b-5e6e-a5f9-aea49237aa35"; connection = nm_simple_connection_new(); g_assert(connection); @@ -1982,7 +2019,9 @@ test_write_bridge_main(void) nm_connection_add_setting(connection, NM_SETTING(s_ip6)); g_object_set(s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, + FALSE, + TEST_KEYFILES_DIR "/Test_Write_Bridge_Main"); } static void @@ -2022,12 +2061,12 @@ test_read_bridge_component(void) static void test_write_bridge_component(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBridgePort *s_port; NMSettingWired *s_wired; - const char *mac = "99:88:77:66:55:44"; + const char *mac = "99:88:77:66:55:44"; + const char *uuid = "7c4e34eb-419f-531c-b6ca-486f51a08d1d"; connection = nm_simple_connection_new(); g_assert(connection); @@ -2071,7 +2110,9 @@ test_write_bridge_component(void) 99, NULL); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, + FALSE, + TEST_KEYFILES_DIR "/Test_Write_Bridge_Component"); } static void @@ -2423,11 +2464,11 @@ test_read_enum_property(void) static void test_write_enum_property(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip6; + const char *uuid = "7e4cb57c-33ff-51fc-ae49-4ee414ef0b63"; connection = nm_simple_connection_new(); @@ -2461,7 +2502,7 @@ test_write_enum_property(void) nmtst_connection_normalize(connection); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, FALSE, TEST_KEYFILES_DIR "/Test_Write_Enum"); } static void @@ -2482,10 +2523,10 @@ test_read_flags_property(void) static void test_write_flags_property(void) { - NMTST_UUID_INIT(uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSetting *s_gsm; + const char *uuid = "19febe12-db48-5661-94cc-16529548d772"; connection = nm_simple_connection_new(); @@ -2517,7 +2558,7 @@ test_write_flags_property(void) nmtst_connection_normalize(connection); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, FALSE, TEST_KEYFILES_DIR "/Test_Write_Flags"); } /*****************************************************************************/ @@ -2586,9 +2627,11 @@ test_write_tc_config(void) NMTCAction *action; GError *error = NULL; - connection = - nmtst_create_minimal_connection("Test TC", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); - s_tc = nm_setting_tc_config_new(); + connection = nmtst_create_minimal_connection("Test TC", + "ed1cb963-ff64-5129-99ca-e05ab3195ce4", + NM_SETTING_WIRED_SETTING_NAME, + NULL); + s_tc = nm_setting_tc_config_new(); qdisc1 = nm_tc_qdisc_new("fq_codel", TC_H_ROOT, &error); nmtst_assert_success(qdisc1, error); @@ -2622,7 +2665,7 @@ test_write_tc_config(void) nm_connection_add_setting(connection, s_tc); nmtst_connection_normalize(connection); - write_test_connection_and_reread(connection, FALSE); + write_test_connection_and_reread(connection, FALSE, TEST_KEYFILES_DIR "/Test_Write_TC"); nm_tc_qdisc_unref(qdisc1); nm_tc_qdisc_unref(qdisc2);