[glib/pgriffis/wip/resolver-https] gresolver-test: Use GByteArray
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/pgriffis/wip/resolver-https] gresolver-test: Use GByteArray
- Date: Wed, 15 Dec 2021 16:40:07 +0000 (UTC)
commit c40697ba81745d4b9610353d763dece040e251ad
Author: Patrick Griffis <pgriffis igalia com>
Date: Wed Dec 15 10:39:52 2021 -0600
gresolver-test: Use GByteArray
gio/tests/gresolver.c | 88 ++++++++++++++++++++++++++++++---------------------
1 file changed, 52 insertions(+), 36 deletions(-)
---
diff --git a/gio/tests/gresolver.c b/gio/tests/gresolver.c
index 3ba7dbad2..08ebd1043 100644
--- a/gio/tests/gresolver.c
+++ b/gio/tests/gresolver.c
@@ -28,33 +28,48 @@
#ifdef HAVE_DN_COMP
static void
-dns_builder_add_uint16 (GString *builder, guint16 value)
+dns_builder_add_uint8 (GByteArray *builder,
+ guint8 value)
{
- g_string_append_c (builder, (value & 0xFF00) >> 8);
- g_string_append_c (builder, (value & 0xFF));
+ g_byte_array_append (builder, &value, 1);
}
static void
-dns_builder_add_uint32 (GString *builder, guint32 value)
+dns_builder_add_uint16 (GByteArray *builder,
+ guint16 value)
{
- g_string_append_c (builder, (value & 0xFF000000) >> 24);
- g_string_append_c (builder, (value & 0xFF0000) >> 16);
- g_string_append_c (builder, (value & 0xFF00) >> 16);
- g_string_append_c (builder, (value & 0xFF) >> 16);
+ dns_builder_add_uint8 (builder, (value & 0xFF00) >> 8);
+ dns_builder_add_uint8 (builder, (value & 0xFF));
}
static void
-dns_builder_add_length_prefixed_string (GString *builder, const char *string)
+dns_builder_add_uint32 (GByteArray *builder,
+ guint32 value)
{
- guint8 length = (guint8) strlen (string);
- g_string_append_c (builder, length);
+ dns_builder_add_uint8 (builder, (value & 0xFF000000) >> 24);
+ dns_builder_add_uint8 (builder, (value & 0xFF0000) >> 16);
+ dns_builder_add_uint8 (builder, (value & 0xFF00) >> 16);
+ dns_builder_add_uint8 (builder, (value & 0xFF) >> 16);
+}
+
+static void
+dns_builder_add_length_prefixed_string (GByteArray *builder,
+ const char *string)
+{
+ guint8 length;
+
+ g_assert (strlen (string) <= G_MAXUINT8);
+
+ length = (guint8) strlen (string);
+ dns_builder_add_uint8 (builder, length);
/* Don't include trailing NUL */
- g_string_append_len (builder, string, length);
+ g_byte_array_append (builder, string, length);
}
static void
-dns_builder_add_domain (GString *builder, const char *string)
+dns_builder_add_domain (GByteArray *builder,
+ const char *string)
{
int ret;
guchar buffer[256];
@@ -62,27 +77,28 @@ dns_builder_add_domain (GString *builder, const char *string)
ret = dn_comp (string, buffer, sizeof (buffer), NULL, NULL);
g_assert (ret != -1);
- g_string_append_len (builder, (char *)buffer, ret);
+ g_byte_array_append (builder, (char *)buffer, ret);
}
static void
-dns_builder_add_answer_data (GString *builder, GString *answer)
+dns_builder_add_answer_data (GByteArray *builder,
+ GByteArray *answer)
{
dns_builder_add_uint16 (builder, answer->len); /* rdlength */
- g_string_append_len (builder, answer->str, answer->len);
+ g_byte_array_append (builder, answer->data, answer->len);
}
#endif /* HAVE_DN_COMP */
typedef struct
{
- GString *answer;
+ GByteArray *answer;
} TestData;
static void
dns_test_setup (TestData *fixture,
gconstpointer user_data)
{
- fixture->answer = g_string_sized_new (2046);
+ fixture->answer = g_byte_array_sized_new (2046);
#ifdef HAVE_DN_COMP
/* Start with a header, we ignore everything except ancount.
@@ -107,7 +123,7 @@ static void
dns_test_teardown (TestData *fixture,
gconstpointer user_data)
{
- g_string_free (fixture->answer, TRUE);
+ g_byte_array_free (fixture->answer, TRUE);
}
static void
@@ -119,7 +135,7 @@ test_https_alias (TestData *fixture,
return;
#else
GList *records;
- GString *https_answer = g_string_sized_new (1024);
+ GByteArray *https_answer = g_byte_array_sized_new (1024);
guint16 priority;
const char *alias;
GError *error = NULL;
@@ -129,7 +145,7 @@ test_https_alias (TestData *fixture,
dns_builder_add_answer_data (fixture->answer, https_answer);
records = g_resolver_records_from_res_query ("example.org", 65,
- (guchar*)fixture->answer->str,
+ (guchar*)fixture->answer->data,
fixture->answer->len,
0, &error);
@@ -141,7 +157,7 @@ test_https_alias (TestData *fixture,
g_assert_cmpstr (alias, ==, "foo.example.org.");
g_list_free_full (records, (GDestroyNotify)g_variant_unref);
- g_string_free (https_answer, TRUE);
+ g_byte_array_free (https_answer, TRUE);
#endif /* HAVE_DN_COMP */
}
@@ -154,7 +170,7 @@ test_https_service (TestData *fixture,
return;
#else
GList *records;
- GString *https_answer = g_string_sized_new (1024);
+ GByteArray *https_answer = g_byte_array_sized_new (1024);
guint16 priority;
const char *target;
GVariant *params;
@@ -185,7 +201,7 @@ test_https_service (TestData *fixture,
dns_builder_add_answer_data (fixture->answer, https_answer);
records = g_resolver_records_from_res_query ("example.org", 65,
- (guchar *)fixture->answer->str,
+ (guchar *)fixture->answer->data,
fixture->answer->len,
0, &error);
@@ -209,7 +225,7 @@ test_https_service (TestData *fixture,
g_variant_dict_unref (dict);
g_variant_unref (params);
g_list_free_full (records, (GDestroyNotify)g_variant_unref);
- g_string_free (https_answer, TRUE);
+ g_byte_array_free (https_answer, TRUE);
#endif
}
@@ -222,7 +238,7 @@ test_https_invalid_1 (TestData *fixture,
return;
#else
GList *records;
- GString *https_answer = g_string_sized_new (1024);
+ GByteArray *https_answer = g_byte_array_sized_new (1024);
GError *error = NULL;
dns_builder_add_uint16 (https_answer, 1); /* priority */
@@ -235,7 +251,7 @@ test_https_invalid_1 (TestData *fixture,
dns_builder_add_answer_data (fixture->answer, https_answer);
records = g_resolver_records_from_res_query ("example.org", 65,
- (guchar *)fixture->answer->str,
+ (guchar *)fixture->answer->data,
fixture->answer->len,
0, &error);
@@ -243,7 +259,7 @@ test_https_invalid_1 (TestData *fixture,
g_assert_null (records);
g_error_free (error);
- g_string_free (https_answer, TRUE);
+ g_byte_array_free (https_answer, TRUE);
#endif
}
@@ -256,7 +272,7 @@ test_https_invalid_2 (TestData *fixture,
return;
#else
GList *records;
- GString *https_answer = g_string_sized_new (1024);
+ GByteArray *https_answer = g_byte_array_sized_new (1024);
GError *error = NULL;
dns_builder_add_uint16 (https_answer, 1); /* priority */
@@ -269,7 +285,7 @@ test_https_invalid_2 (TestData *fixture,
dns_builder_add_answer_data (fixture->answer, https_answer);
records = g_resolver_records_from_res_query ("example.org", 65,
- (guchar *)fixture->answer->str,
+ (guchar *)fixture->answer->data,
fixture->answer->len,
0, &error);
@@ -277,7 +293,7 @@ test_https_invalid_2 (TestData *fixture,
g_assert_null (records);
g_error_free (error);
- g_string_free (https_answer, TRUE);
+ g_byte_array_free (https_answer, TRUE);
#endif
}
@@ -290,18 +306,18 @@ test_https_invalid_3 (TestData *fixture,
return;
#else
GList *records;
- GString *https_answer = g_string_sized_new (1024);
+ GByteArray *https_answer = g_byte_array_sized_new (1024);
GError *error = NULL;
dns_builder_add_uint16 (https_answer, 0); /* priority */
/* Creating an invalid target string will be caught. */
- g_string_append_c (https_answer, 100);
- g_string_append_len (https_answer, "test", 4);
+ dns_builder_add_uint8 (https_answer, 100);
+ g_byte_array_append (https_answer, "test", 4);
dns_builder_add_answer_data (fixture->answer, https_answer);
records = g_resolver_records_from_res_query ("example.org", 65,
- (guchar *)fixture->answer->str,
+ (guchar *)fixture->answer->data,
fixture->answer->len,
0, &error);
@@ -309,7 +325,7 @@ test_https_invalid_3 (TestData *fixture,
g_assert_null (records);
g_error_free (error);
- g_string_free (https_answer, TRUE);
+ g_byte_array_free (https_answer, TRUE);
#endif
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]