nmtst: add nmtst_assert_connection_verify() and don't normalize connection in assert
authorThomas Haller <thaller@redhat.com>
Sun, 14 Feb 2016 23:37:22 +0000 (00:37 +0100)
committerThomas Haller <thaller@redhat.com>
Mon, 15 Feb 2016 13:26:26 +0000 (14:26 +0100)
It is ugly that nmtst_assert_connection_verifies_after_normalization() would
normalize the argument and modify it. An assertion should not have side-effects.

libnm-core/tests/test-general.c
libnm-core/tests/test-keyfile.c
shared/nm-test-utils.h

index 8fb7fb2..7496530 100644 (file)
@@ -3306,6 +3306,7 @@ _test_connection_normalize_type_normalizable_setting (const char *type,
 
        g_assert (!nm_connection_get_setting_by_name (con, type));
        nmtst_assert_connection_verifies_after_normalization (con, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_SETTING);
+       nmtst_connection_normalize (con);
 
        s_base = nm_connection_get_setting_by_name (con, type);
        g_assert (s_base);
@@ -3362,6 +3363,7 @@ _test_connection_normalize_type_normalizable_type (const char *type,
        g_assert (nm_connection_get_setting_by_name (con, type) == s_base);
 
        nmtst_assert_connection_verifies_after_normalization (con, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_PROPERTY);
+       nmtst_connection_normalize (con);
 
        g_assert_cmpstr (nm_connection_get_connection_type (con), ==, type);
        g_assert (nm_connection_get_setting_by_name (con, type) == s_base);
@@ -3626,6 +3628,7 @@ test_connection_normalize_slave_type_1 (void)
 
        g_assert (!nm_connection_get_setting_by_name (con, NM_SETTING_BRIDGE_PORT_SETTING_NAME));
        nmtst_assert_connection_verifies_after_normalization (con, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_SETTING);
+       nmtst_connection_normalize (con);
        g_assert (nm_connection_get_setting_by_name (con, NM_SETTING_BRIDGE_PORT_SETTING_NAME));
        g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_BRIDGE_SETTING_NAME);
 }
@@ -3656,6 +3659,7 @@ test_connection_normalize_slave_type_2 (void)
        g_assert (nm_connection_get_setting_by_name (con, NM_SETTING_BRIDGE_PORT_SETTING_NAME));
        g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NULL);
        nmtst_assert_connection_verifies_after_normalization (con, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_PROPERTY);
+       nmtst_connection_normalize (con);
        g_assert (nm_connection_get_setting_by_name (con, NM_SETTING_BRIDGE_PORT_SETTING_NAME));
        g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_BRIDGE_SETTING_NAME);
 }
@@ -3679,7 +3683,8 @@ test_connection_normalize_infiniband_mtu (void)
                      NM_SETTING_INFINIBAND_TRANSPORT_MODE, "datagram",
                      NM_SETTING_INFINIBAND_MTU, (guint) 2044,
                      NULL);
-       nmtst_assert_connection_verifies_without_normalization (con);
+       nmtst_assert_connection_verifies_and_normalizable (con);
+       nmtst_connection_normalize (con);
        g_assert_cmpint (2044, ==, nm_setting_infiniband_get_mtu (s_infini));
 
        g_object_set (s_infini,
@@ -3687,6 +3692,7 @@ test_connection_normalize_infiniband_mtu (void)
                      NM_SETTING_INFINIBAND_MTU, (guint) 2045,
                      NULL);
        nmtst_assert_connection_verifies_after_normalization (con, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY);
+       nmtst_connection_normalize (con);
        g_assert_cmpint (2044, ==, nm_setting_infiniband_get_mtu (s_infini));
 
        g_object_set (s_infini,
@@ -3701,6 +3707,7 @@ test_connection_normalize_infiniband_mtu (void)
                      NM_SETTING_INFINIBAND_MTU, (guint) 65521,
                      NULL);
        nmtst_assert_connection_verifies_after_normalization (con, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY);
+       nmtst_connection_normalize (con);
        g_assert_cmpint (65520, ==, nm_setting_infiniband_get_mtu (s_infini));
 }
 
@@ -3940,6 +3947,7 @@ test_setting_compare_default_strv (void)
        c1 = nmtst_create_minimal_connection ("test_compare_default_strv", NULL,
                                              NM_SETTING_WIRED_SETTING_NAME, NULL);
        nmtst_assert_connection_verifies_and_normalizable (c1);
+       nmtst_connection_normalize (c1);
 
        c2 = nm_simple_connection_new_clone (c1);
        nmtst_assert_connection_verifies_without_normalization (c2);
index 3b32f52..afc2884 100644 (file)
@@ -113,9 +113,10 @@ _nm_keyfile_read (GKeyFile *keyfile,
        con = nm_keyfile_read (keyfile, keyfile_name, base_dir, read_handler, read_data, &error);
        g_assert_no_error (error);
        g_assert (NM_IS_CONNECTION (con));
-       if (needs_normalization)
+       if (needs_normalization) {
                nmtst_assert_connection_verifies_after_normalization (con, 0, 0);
-       else
+               nmtst_connection_normalize (con);
+       } else
                nmtst_assert_connection_verifies_without_normalization (con);
        return con;
 }
@@ -334,6 +335,7 @@ test_8021x_cert (void)
 
        nm_connection_add_setting (con, NM_SETTING (s_8021x));
        nmtst_assert_connection_verifies_and_normalizable (con);
+       nmtst_connection_normalize (con);
 
 
        _test_8021x_cert_check (con, scheme, full_TEST_WIRED_TLS_CA_CERT, -1);
index 055d636..d059022 100644 (file)
@@ -1383,6 +1383,18 @@ nmtst_setting_ip_config_add_route (NMSettingIPConfig *s_ip,
 
 #if (defined(__NM_SIMPLE_CONNECTION_H__) && defined(__NM_SETTING_CONNECTION_H__)) || (defined(NM_CONNECTION_H))
 
+inline static NMConnection *
+nmtst_clone_connection (NMConnection *connection)
+{
+       g_assert (NM_IS_CONNECTION (connection));
+
+#if defined(__NM_SIMPLE_CONNECTION_H__)
+       return nm_simple_connection_new_clone (connection);
+#else
+       return nm_connection_duplicate (connection);
+#endif
+}
+
 inline static NMConnection *
 nmtst_create_minimal_connection (const char *id, const char *uuid, const char *type, NMSettingConnection **out_s_con)
 {
@@ -1487,13 +1499,7 @@ _nmtst_connection_duplicate_and_normalize (NMConnection *connection, ...)
        gboolean was_modified;
        va_list args;
 
-       g_assert (NM_IS_CONNECTION (connection));
-
-#if defined(__NM_SIMPLE_CONNECTION_H__)
-       connection = nm_simple_connection_new_clone (connection);
-#else
-       connection = nm_connection_duplicate (connection);
-#endif
+       connection = nmtst_clone_connection (connection);
 
        va_start (args, connection);
        was_modified = _nmtst_connection_normalize_v (connection, args);
@@ -1564,28 +1570,33 @@ nmtst_assert_connection_equals (NMConnection *a, gboolean normalize_a, NMConnect
 }
 
 inline static void
-nmtst_assert_connection_verifies_without_normalization (NMConnection *con)
+nmtst_assert_connection_verifies (NMConnection *con)
 {
-       /* assert that the connection verifies and does not need any normalization */
-
+       /* assert that the connection does verify, it might be normaliziable or not */
        GError *error = NULL;
        gboolean success;
-       gboolean was_modified = FALSE;
-       gs_unref_object NMConnection *clone = NULL;
 
        g_assert (NM_IS_CONNECTION (con));
 
-#if defined(__NM_SIMPLE_CONNECTION_H__)
-       clone = nm_simple_connection_new_clone (con);
-#else
-       clone = nm_connection_duplicate (con);
-#endif
-
        success = nm_connection_verify (con, &error);
        g_assert_no_error (error);
        g_assert (success);
+}
+
+inline static void
+nmtst_assert_connection_verifies_without_normalization (NMConnection *con)
+{
+       /* assert that the connection verifies and does not need any normalization */
+       GError *error = NULL;
+       gboolean success;
+       gboolean was_modified = FALSE;
+       gs_unref_object NMConnection *clone = NULL;
+
+       clone = nmtst_clone_connection (con);
+
+       nmtst_assert_connection_verifies (con);
 
-       success = nm_connection_normalize (con, NULL, &was_modified, &error);
+       success = nm_connection_normalize (clone, NULL, &was_modified, &error);
        g_assert_no_error (error);
        g_assert (success);
        nmtst_assert_connection_equals (con, FALSE, clone, FALSE);
@@ -1599,21 +1610,19 @@ nmtst_assert_connection_verifies_and_normalizable (NMConnection *con)
        GError *error = NULL;
        gboolean success;
        gboolean was_modified = FALSE;
+       gs_unref_object NMConnection *clone = NULL;
 
-       g_assert (NM_IS_CONNECTION (con));
+       clone = nmtst_clone_connection (con);
 
-       success = nm_connection_verify (con, &error);
-       g_assert_no_error (error);
-       g_assert (success);
-       g_clear_error (&error);
+       nmtst_assert_connection_verifies (con);
 
-       success = nm_connection_normalize (con, NULL, &was_modified, &error);
+       success = nm_connection_normalize (clone, NULL, &was_modified, &error);
        g_assert_no_error (error);
        g_assert (success);
        g_assert (was_modified);
 
        /* again! */
-       nmtst_assert_connection_verifies_without_normalization (con);
+       nmtst_assert_connection_verifies_without_normalization (clone);
 }
 
 inline static void
@@ -1625,21 +1634,22 @@ nmtst_assert_connection_verifies_after_normalization (NMConnection *con,
        GError *error = NULL;
        gboolean success;
        gboolean was_modified = FALSE;
+       gs_unref_object NMConnection *clone = NULL;
 
-       g_assert (NM_IS_CONNECTION (con));
+       clone = nmtst_clone_connection (con);
 
        success = nm_connection_verify (con, &error);
        nmtst_assert_error (error, expect_error_domain, expect_error_code, NULL);
        g_assert (!success);
        g_clear_error (&error);
 
-       success = nm_connection_normalize (con, NULL, &was_modified, &error);
+       success = nm_connection_normalize (clone, NULL, &was_modified, &error);
        g_assert_no_error (error);
        g_assert (success);
        g_assert (was_modified);
 
        /* again! */
-       nmtst_assert_connection_verifies_without_normalization (con);
+       nmtst_assert_connection_verifies_without_normalization (clone);
 }
 
 inline static void
@@ -1652,18 +1662,20 @@ nmtst_assert_connection_unnormalizable (NMConnection *con,
        GError *error = NULL;
        gboolean success;
        gboolean was_modified = FALSE;
+       gs_unref_object NMConnection *clone = NULL;
 
-       g_assert (NM_IS_CONNECTION (con));
+       clone = nmtst_clone_connection (con);
 
        success = nm_connection_verify (con, &error);
        nmtst_assert_error (error, expect_error_domain, expect_error_code, NULL);
        g_assert (!success);
        g_clear_error (&error);
 
-       success = nm_connection_normalize (con, NULL, &was_modified, &error);
+       success = nm_connection_normalize (clone, NULL, &was_modified, &error);
        nmtst_assert_error (error, expect_error_domain, expect_error_code, NULL);
        g_assert (!success);
        g_assert (!was_modified);
+       nmtst_assert_connection_equals (con, FALSE, clone, FALSE);
        g_clear_error (&error);
 }