[glib: 5/13] tests: Add tests for invalid DNS response header parsing




commit 51f70fe62ec6023d5a28f9283997ff47da713865
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri Mar 18 15:54:19 2022 +0000

    tests: Add tests for invalid DNS response header parsing
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 gio/tests/resolver-parsing.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
---
diff --git a/gio/tests/resolver-parsing.c b/gio/tests/resolver-parsing.c
index e6fc749366..bf1ba1b736 100644
--- a/gio/tests/resolver-parsing.c
+++ b/gio/tests/resolver-parsing.c
@@ -106,11 +106,53 @@ dns_header (void)
 }
 #endif /* HAVE_DN_COMP */
 
+static void
+test_invalid_header (void)
+{
+  const struct
+    {
+      const guint8 *answer;
+      gsize answer_len;
+      GResolverError expected_error_code;
+    }
+  vectors[] =
+    {
+      /* No answer: */
+      { (const guint8 *) "", 0, G_RESOLVER_ERROR_NOT_FOUND },
+      /* Definitely too short to be a valid header: */
+      { (const guint8 *) "\x20", 1, G_RESOLVER_ERROR_INTERNAL },
+      /* One byte too short to be a valid header: */
+      { (const guint8 *) "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 11, G_RESOLVER_ERROR_INTERNAL },
+      /* Valid header indicating no answers: */
+      { (const guint8 *) "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, G_RESOLVER_ERROR_NOT_FOUND 
},
+    };
+  gsize i;
+
+  for (i = 0; i < G_N_ELEMENTS (vectors); i++)
+    {
+      GList *records = NULL;
+      GError *local_error = NULL;
+
+      records = g_resolver_records_from_res_query ("example.org",
+                                                   g_resolver_record_type_to_rrtype (G_RESOLVER_RECORD_NS),
+                                                   vectors[i].answer,
+                                                   vectors[i].answer_len,
+                                                   0,
+                                                   &local_error);
+
+      g_assert_error (local_error, G_RESOLVER_ERROR, (gint) vectors[i].expected_error_code);
+      g_assert_null (records);
+      g_clear_error (&local_error);
+    }
+}
+
 int
 main (int   argc,
       char *argv[])
 {
   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
 
+  g_test_add_func ("/gresolver/invalid-header", test_invalid_header);
+
   return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]