shared/tests: add nmtst_keyfile_assert_data() test util

This commit is contained in:
Thomas Haller 2019-01-03 13:22:28 +01:00
parent b93a2cf728
commit 04c6c912b0

View file

@ -1976,8 +1976,8 @@ nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const cha
static inline NMConnection *
nmtst_create_connection_from_keyfile (const char *keyfile_str, const char *full_filename)
{
GKeyFile *keyfile;
GError *error = NULL;
gs_unref_keyfile GKeyFile *keyfile = NULL;
gs_free_error GError *error = NULL;
gboolean success;
NMConnection *con;
gs_free char *filename = g_path_get_basename (full_filename);
@ -1988,14 +1988,10 @@ nmtst_create_connection_from_keyfile (const char *keyfile_str, const char *full_
keyfile = g_key_file_new ();
success = g_key_file_load_from_data (keyfile, keyfile_str, strlen (keyfile_str), G_KEY_FILE_NONE, &error);
g_assert_no_error (error);
g_assert (success);
nmtst_assert_success (success, error);
con = nm_keyfile_read (keyfile, base_dir, NULL, NULL, &error);
g_assert_no_error (error);
g_assert (NM_IS_CONNECTION (con));
g_key_file_unref (keyfile);
nmtst_assert_success (NM_IS_CONNECTION (con), error);
nm_keyfile_read_ensure_id (con, filename);
nm_keyfile_read_ensure_uuid (con, full_filename);
@ -2151,4 +2147,50 @@ typedef enum {
#endif /* __NM_CONNECTION_H__ */
/*****************************************************************************/
static inline void
nmtst_keyfile_assert_data (GKeyFile *kf, const char *data, gssize data_len)
{
gs_unref_keyfile GKeyFile *kf2 = NULL;
gs_free_error GError *error = NULL;
gs_free char *d1 = NULL;
gs_free char *d2 = NULL;
gboolean success;
gsize d1_len;
gsize d2_len;
g_assert (kf);
g_assert (data || data_len == 0);
g_assert (data_len >= -1);
d1 = g_key_file_to_data (kf, &d1_len, &error);
nmtst_assert_success (d1, error);
if (data_len == -1) {
g_assert_cmpint (strlen (d1), ==, d1_len);
data_len = strlen (data);
g_assert_cmpstr (d1, ==, data);
}
g_assert_cmpmem (d1, d1_len, data, (gsize) data_len);
/* also check that we can re-generate the same keyfile from the data. */
kf2 = g_key_file_new ();
success = g_key_file_load_from_data (kf2,
d1,
d1_len,
G_KEY_FILE_NONE,
&error);
nmtst_assert_success (success, error);
d2 = g_key_file_to_data (kf2, &d2_len, &error);
nmtst_assert_success (d2, error);
g_assert_cmpmem (d2, d2_len, d1, d1_len);
}
/*****************************************************************************/
#endif /* __NM_TEST_UTILS_H__ */