Imported Upstream version 2.73.3
[platform/upstream/glib.git] / glib / tests / markup-parse.c
index 722a049..136a9a9 100644 (file)
@@ -108,14 +108,15 @@ static const GMarkupParser silent_parser = {
 };
 
 static int
-test_in_chunks (const gchar *contents,
-                gint         length,
-                gint         chunk_size)
+test_in_chunks (const gchar       *contents,
+                gint               length,
+                gint               chunk_size,
+                GMarkupParseFlags  flags)
 {
   GMarkupParseContext *context;
   int i = 0;
   
-  context = g_markup_parse_context_new (&silent_parser, 0, NULL, NULL);
+  context = g_markup_parse_context_new (&silent_parser, flags, NULL, NULL);
 
   while (i < length)
     {
@@ -144,89 +145,91 @@ test_in_chunks (const gchar *contents,
   return 0;
 }
 
-static int
-test_file (const gchar *filename)
+/* Load the given @filename and parse it multiple times with different chunking
+ * and length handling. All results should be equal. %TRUE is returned if the
+ * file was parsed successfully on every attempt; %FALSE if it failed to parse
+ * on every attempt. The test aborts if some attempts succeed and some fail. */
+static gboolean
+test_file (const gchar       *filename,
+           GMarkupParseFlags  flags)
 {
-  gchar *contents;
-  gsize  length;
-  GError *error;
+  gchar *contents = NULL, *contents_unterminated = NULL;
+  gsize length_bytes;
+  GError *local_error = NULL;
   GMarkupParseContext *context;
   gint line, col;
-
-  error = NULL;
-  if (!g_file_get_contents (filename,
-                            &contents,
-                            &length,
-                            &error))
-    {
-      fprintf (stderr, "%s\n", error->message);
-      g_error_free (error);
-      return 1;
-    }
-
-  context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
+  guint n_failures = 0;
+  guint n_tests = 0;
+  const gsize chunk_sizes_bytes[] = { 1, 2, 5, 12, 1024 };
+  gsize i;
+  GString *first_string = NULL;
+
+  g_file_get_contents (filename, &contents, &length_bytes, &local_error);
+  g_assert_no_error (local_error);
+
+  /* Make a copy of the contents with no trailing nul. */
+  contents_unterminated = g_malloc (length_bytes);
+  if (contents_unterminated != NULL)
+    memcpy (contents_unterminated, contents, length_bytes);
+
+  /* Test with nul termination. */
+  context = g_markup_parse_context_new (&parser, flags, NULL, NULL);
   g_assert (g_markup_parse_context_get_user_data (context) == NULL);
   g_markup_parse_context_get_position (context, &line, &col);
-  g_assert (line == 1 && col == 1);
+  g_assert_cmpint (line, ==, 1);
+  g_assert_cmpint (col, ==, 1);
 
-  if (!g_markup_parse_context_parse (context, contents, length, NULL))
-    {
-      g_markup_parse_context_free (context);
-      g_free (contents);
-      return 1;
-    }
-
-  if (!g_markup_parse_context_end_parse (context, NULL))
-    {
-      g_markup_parse_context_free (context);
-      g_free (contents);
-      return 1;
-    }
+  if (!g_markup_parse_context_parse (context, contents, -1, NULL) ||
+      !g_markup_parse_context_end_parse (context, NULL))
+    n_failures++;
+  n_tests++;
 
   g_markup_parse_context_free (context);
 
-  /* A byte at a time */
-  if (test_in_chunks (contents, length, 1) != 0)
-    {
-      g_free (contents);
-      return 1;
-    }
-
-  /* 2 bytes */
-  if (test_in_chunks (contents, length, 2) != 0)
+  /* FIXME: Swap out the error string so we only return one copy of it, not
+   * @n_tests copies. This should be fixed properly by eliminating the global
+   * state in this file. */
+  first_string = g_steal_pointer (&string);
+  string = g_string_new ("");
+
+  /* With the length specified explicitly and a nul terminator present (since
+   * g_file_get_contents() always adds one). */
+  if (test_in_chunks (contents, length_bytes, length_bytes, flags) != 0)
+    n_failures++;
+  n_tests++;
+
+  /* With the length specified explicitly and no nul terminator present. */
+  if (test_in_chunks (contents_unterminated, length_bytes, length_bytes, flags) != 0)
+    n_failures++;
+  n_tests++;
+
+  /* In various sized chunks. */
+  for (i = 0; i < G_N_ELEMENTS (chunk_sizes_bytes); i++)
     {
-      g_free (contents);
-      return 1;
+      if (test_in_chunks (contents, length_bytes, chunk_sizes_bytes[i], flags) != 0)
+        n_failures++;
+      n_tests++;
     }
 
-  /*5 bytes */
-  if (test_in_chunks (contents, length, 5) != 0)
-    {
-      g_free (contents);
-      return 1;
-    }
-
-  /* 12 bytes */
-  if (test_in_chunks (contents, length, 12) != 0)
-    {
-      g_free (contents);
-      return 1;
-    }
+  g_free (contents);
+  g_free (contents_unterminated);
 
-  /* 1024 bytes */
-  if (test_in_chunks (contents, length, 1024) != 0)
-    {
-      g_free (contents);
-      return 1;
-    }
+  /* FIXME: Restore the error string. */
+  g_string_free (string, TRUE);
+  string = g_steal_pointer (&first_string);
 
-  g_free (contents);
+  /* We expect the file to either always be parsed successfully, or never be
+   * parsed successfully. There’s a bug in GMarkup if it sometimes parses
+   * successfully depending on how you chunk or terminate the input. */
+  if (n_failures > 0)
+    g_assert_cmpint (n_failures, ==, n_tests);
 
-  return 0;
+  return (n_failures == 0);
 }
 
 static gchar *
-get_expected_filename (const gchar *filename)
+get_expected_filename (const gchar       *filename,
+                       GMarkupParseFlags  flags)
 {
   gchar *f, *p, *expected;
 
@@ -234,7 +237,13 @@ get_expected_filename (const gchar *filename)
   p = strstr (f, ".gmarkup");
   if (p)
     *p = 0;
-  expected = g_strconcat (f, ".expected", NULL);
+  if (flags == 0)
+    expected = g_strconcat (f, ".expected", NULL);
+  else if (flags == G_MARKUP_TREAT_CDATA_AS_TEXT)
+    expected = g_strconcat (f, ".cdata-as-text", NULL);
+  else
+    g_assert_not_reached ();
+
   g_free (f);
 
   return expected;
@@ -246,27 +255,46 @@ test_parse (gconstpointer d)
   const gchar *filename = d;
   gchar *expected_file;
   gchar *expected;
+  gboolean valid_input;
   GError *error = NULL;
-  gint res;
+  gboolean res;
+
+  valid_input = strstr (filename, "valid") != NULL;
+  expected_file = get_expected_filename (filename, 0);
 
   depth = 0;
   string = g_string_sized_new (0);
 
-  res = test_file (filename);
+  res = test_file (filename, 0);
+  g_assert_cmpint (res, ==, valid_input);
 
-  if (strstr (filename, "valid"))
-    g_assert_cmpint (res, ==, 0);
-  else
-    g_assert_cmpint (res, ==, 1);
-
-  expected_file = get_expected_filename (filename);
   g_file_get_contents (expected_file, &expected, NULL, &error);
   g_assert_no_error (error);
   g_assert_cmpstr (string->str, ==, expected);
   g_free (expected);
-  g_free (expected_file);
 
   g_string_free (string, TRUE);
+
+  g_free (expected_file);
+
+  expected_file = get_expected_filename (filename, G_MARKUP_TREAT_CDATA_AS_TEXT);
+  if (g_file_test (expected_file, G_FILE_TEST_EXISTS))
+    {
+      depth = 0;
+      string = g_string_sized_new (0);
+
+      res = test_file (filename, G_MARKUP_TREAT_CDATA_AS_TEXT);
+      g_assert_cmpint (res, ==, valid_input);
+
+      g_file_get_contents (expected_file, &expected, NULL, &error);
+      g_assert_no_error (error);
+      g_assert_cmpstr (string->str, ==, expected);
+      g_free (expected);
+
+      g_string_free (string, TRUE);
+    }
+
+  g_free (expected_file);
 }
 
 int
@@ -277,7 +305,7 @@ main (int argc, char *argv[])
   const gchar *name;
   gchar *path;
 
-  g_setenv ("LANG", "en_US.utf-8", TRUE);
+  g_setenv ("LC_ALL", "C", TRUE);
   setlocale (LC_ALL, "");
 
   g_test_init (&argc, &argv, NULL);
@@ -285,24 +313,32 @@ main (int argc, char *argv[])
   /* allow to easily generate expected output for new test cases */
   if (argc > 1)
     {
+      gint arg = 1;
+      GMarkupParseFlags flags = G_MARKUP_DEFAULT_FLAGS;
+
+      if (strcmp (argv[1], "--cdata-as-text") == 0)
+        {
+          flags = G_MARKUP_TREAT_CDATA_AS_TEXT;
+          arg = 2;
+        }
       string = g_string_sized_new (0);
-      test_file (argv[1]);
+      test_file (argv[arg], flags);
       g_print ("%s", string->str);
       return 0;
     }
 
   error = NULL;
-  path = g_test_build_filename (G_TEST_DISTED, "markups", NULL);
+  path = g_test_build_filename (G_TEST_DIST, "markups", NULL);
   dir = g_dir_open (path, 0, &error);
   g_free (path);
   g_assert_no_error (error);
   while ((name = g_dir_read_name (dir)) != NULL)
     {
-      if (strstr (name, "expected"))
+      if (!strstr (name, "gmarkup"))
         continue;
 
       path = g_strdup_printf ("/markup/parse/%s", name);
-      g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DISTED, "markups", name, NULL),
+      g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "markups", name, NULL),
                                  test_parse, g_free);
       g_free (path);
     }
@@ -310,4 +346,3 @@ main (int argc, char *argv[])
 
   return g_test_run ();
 }
-