1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <locale.h>
#include <string.h>
#include <stdio.h>
#include <glib.h>
static int depth = 0;
static GString *string;
static void
indent (int extra)
{
int i = 0;
while (i < depth)
{
g_string_append (string, " ");
++i;
}
}
static void
start_element_handler (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
int i;
indent (0);
g_string_append_printf (string, "ELEMENT '%s'\n", element_name);
i = 0;
while (attribute_names[i] != NULL)
{
indent (1);
g_string_append_printf (string, "%s=\"%s\"\n",
attribute_names[i],
attribute_values[i]);
++i;
}
++depth;
}
static void
end_element_handler (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
--depth;
indent (0);
g_string_append_printf (string, "END '%s'\n", element_name);
}
static void
text_handler (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
indent (0);
g_string_append_printf (string, "TEXT '%.*s'\n", (int)text_len, text);
}
static void
passthrough_handler (GMarkupParseContext *context,
const gchar *passthrough_text,
gsize text_len,
gpointer user_data,
GError **error)
{
indent (0);
g_string_append_printf (string, "PASS '%.*s'\n", (int)text_len, passthrough_text);
}
static void
error_handler (GMarkupParseContext *context,
GError *error,
gpointer user_data)
{
g_string_append_printf (string, "ERROR %s\n", error->message);
}
static const GMarkupParser parser = {
start_element_handler,
end_element_handler,
text_handler,
passthrough_handler,
error_handler
};
static const GMarkupParser silent_parser = {
NULL,
NULL,
NULL,
NULL,
error_handler
};
static int
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, flags, NULL, NULL);
while (i < length)
{
int this_chunk = MIN (length - i, chunk_size);
if (!g_markup_parse_context_parse (context,
contents + i,
this_chunk,
NULL))
{
g_markup_parse_context_free (context);
return 1;
}
i += this_chunk;
}
if (!g_markup_parse_context_end_parse (context, NULL))
{
g_markup_parse_context_free (context);
return 1;
}
g_markup_parse_context_free (context);
return 0;
}
/* 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 = NULL, *contents_unterminated = NULL;
gsize length_bytes;
GError *local_error = NULL;
GMarkupParseContext *context;
gint line, col;
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_cmpint (line, ==, 1);
g_assert_cmpint (col, ==, 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);
/* 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++)
{
if (test_in_chunks (contents, length_bytes, chunk_sizes_bytes[i], flags) != 0)
n_failures++;
n_tests++;
}
g_free (contents);
g_free (contents_unterminated);
/* FIXME: Restore the error string. */
g_string_free (string, TRUE);
string = g_steal_pointer (&first_string);
/* 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 (n_failures == 0);
}
static gchar *
get_expected_filename (const gchar *filename,
GMarkupParseFlags flags)
{
gchar *f, *p, *expected;
f = g_strdup (filename);
p = strstr (f, ".gmarkup");
if (p)
*p = 0;
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;
}
static void
test_parse (gconstpointer d)
{
const gchar *filename = d;
gchar *expected_file;
gchar *expected;
gboolean valid_input;
GError *error = NULL;
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, 0);
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);
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
main (int argc, char *argv[])
{
GDir *dir;
GError *error;
const gchar *name;
gchar *path;
g_setenv ("LC_ALL", "C", TRUE);
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
/* allow to easily generate expected output for new test cases */
if (argc > 1)
{
gint arg = 1;
GMarkupParseFlags flags = 0;
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[arg], flags);
g_print ("%s", string->str);
return 0;
}
error = 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, "gmarkup"))
continue;
path = g_strdup_printf ("/markup/parse/%s", name);
g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "markups", name, NULL),
test_parse, g_free);
g_free (path);
}
g_dir_close (dir);
return g_test_run ();
}
|