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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <json.h>
#include <parserutils/input/inputstream.h>
#include <hubbub/hubbub.h>
#include "utils/utils.h"
#include "tokeniser/tokeniser.h"
#include "testutils.h"
typedef struct context {
const uint8_t *input;
size_t input_len;
struct array_list *output;
int output_index;
size_t char_off;
const char *last_start_tag;
struct array_list *content_model;
bool process_cdata;
} context;
static void run_test(context *ctx);
static hubbub_error token_handler(const hubbub_token *token, void *pw);
int main(int argc, char **argv)
{
struct json_object *json;
struct array_list *tests;
struct lh_entry *entry;
char *key;
struct json_object *val;
int i;
context ctx;
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
json = json_object_from_file(argv[1]);
assert(!is_error(json));
assert(strcmp((char *) ((json_object_get_object(json)->head)->k),
"tests") == 0);
/* Get array of tests */
tests = json_object_get_array((struct json_object *)
(json_object_get_object(json)->head)->v);
for (i = 0; i < array_list_length(tests); i++) {
/* Get test */
struct json_object *test =
(struct json_object *) array_list_get_idx(tests, i);
ctx.last_start_tag = NULL;
ctx.content_model = NULL;
ctx.process_cdata = false;
/* Extract settings */
for (entry = json_object_get_object(test)->head; entry;
entry = entry->next) {
key = (char *) entry->k;
val = (struct json_object *) entry->v;
if (strcmp(key, "description") == 0) {
printf("Test: %s\n",
json_object_get_string(val));
} else if (strcmp(key, "input") == 0) {
int len = json_object_get_string_len(val);
ctx.input = (const uint8_t *)
json_object_get_string(val);
ctx.input_len = len;
} else if (strcmp(key, "output") == 0) {
ctx.output = json_object_get_array(val);
ctx.output_index = 0;
ctx.char_off = 0;
} else if (strcmp(key, "lastStartTag") == 0) {
ctx.last_start_tag = (const char *)
json_object_get_string(val);
} else if (strcmp(key, "contentModelFlags") == 0) {
ctx.content_model =
json_object_get_array(val);
} else if (strcmp(key, "processCDATA") == 0) {
ctx.process_cdata =
json_object_get_boolean(val);
}
}
/* And run the test */
run_test(&ctx);
}
printf("PASS\n");
return 0;
}
void run_test(context *ctx)
{
parserutils_inputstream *stream;
hubbub_tokeniser *tok;
hubbub_tokeniser_optparams params;
int i, max_i;
size_t j;
struct array_list *outputsave = ctx->output;
if (ctx->content_model == NULL) {
max_i = 1;
} else {
max_i = array_list_length(ctx->content_model);
}
/* We test for each of the content models specified */
for (i = 0; i < max_i; i++) {
/* Reset expected output */
ctx->output = outputsave;
ctx->output_index = 0;
ctx->char_off = 0;
assert(parserutils_inputstream_create("UTF-8", 0, NULL,
&stream) == PARSERUTILS_OK);
assert(hubbub_tokeniser_create(stream, &tok) == HUBBUB_OK);
if (ctx->last_start_tag != NULL) {
/* Fake up a start tag, in PCDATA state */
size_t len = strlen(ctx->last_start_tag) + 3;
uint8_t *buf = malloc(len);
snprintf((char *) buf, len, "<%s>",
ctx->last_start_tag);
assert(parserutils_inputstream_append(stream,
buf, len - 1) == PARSERUTILS_OK);
assert(hubbub_tokeniser_run(tok) == HUBBUB_OK);
free(buf);
}
if (ctx->process_cdata) {
params.process_cdata = ctx->process_cdata;
assert(hubbub_tokeniser_setopt(tok,
HUBBUB_TOKENISER_PROCESS_CDATA,
¶ms) == HUBBUB_OK);
}
params.token_handler.handler = token_handler;
params.token_handler.pw = ctx;
assert(hubbub_tokeniser_setopt(tok,
HUBBUB_TOKENISER_TOKEN_HANDLER,
¶ms) == HUBBUB_OK);
if (ctx->content_model == NULL) {
params.content_model.model =
HUBBUB_CONTENT_MODEL_PCDATA;
} else {
const char *cm = json_object_get_string(
(struct json_object *)
array_list_get_idx(ctx->content_model, i));
if (strcmp(cm, "PCDATA") == 0) {
params.content_model.model =
HUBBUB_CONTENT_MODEL_PCDATA;
} else if (strcmp(cm, "RCDATA") == 0) {
params.content_model.model =
HUBBUB_CONTENT_MODEL_RCDATA;
} else if (strcmp(cm, "CDATA") == 0) {
params.content_model.model =
HUBBUB_CONTENT_MODEL_CDATA;
} else {
params.content_model.model =
HUBBUB_CONTENT_MODEL_PLAINTEXT;
}
}
assert(hubbub_tokeniser_setopt(tok,
HUBBUB_TOKENISER_CONTENT_MODEL,
¶ms) == HUBBUB_OK);
printf("Input: '%.*s' (%d)\n", (int) ctx->input_len,
(const char *) ctx->input,
(int) ctx->input_len);
for (j = 0; j < ctx->input_len; j++) {
assert(parserutils_inputstream_append(stream,
ctx->input + j, 1) ==
PARSERUTILS_OK);
assert(hubbub_tokeniser_run(tok) == HUBBUB_OK);
}
assert(parserutils_inputstream_append(stream, NULL, 0) ==
PARSERUTILS_OK);
assert(hubbub_tokeniser_run(tok) == HUBBUB_OK);
hubbub_tokeniser_destroy(tok);
parserutils_inputstream_destroy(stream);
}
}
hubbub_error token_handler(const hubbub_token *token, void *pw)
{
static const char *token_names[] = {
"DOCTYPE", "StartTag", "EndTag",
"Comment", "Character", "EOF"
};
size_t i;
context *ctx = (context *) pw;
struct json_object *obj = NULL;
struct array_list *items;
for (; ctx->output_index < array_list_length(ctx->output);
ctx->output_index++) {
/* Get object for index */
obj = (struct json_object *)
array_list_get_idx(ctx->output,
ctx->output_index);
/* If it's not a string, we've found the expected output */
if (json_object_get_type(obj) != json_type_string)
break;
/* Otherwise, it must be a parse error */
assert(strcmp(json_object_get_string(obj),
"ParseError") == 0);
}
/* If we've run off the end, this is an error -- the tokeniser has
* produced more tokens than expected. We allow for the generation
* of a terminating EOF token, however. */
assert("too many tokens" &&
(ctx->output_index < array_list_length(ctx->output) ||
token->type == HUBBUB_TOKEN_EOF));
/* Got a terminating EOF -- no error */
if (ctx->output_index >= array_list_length(ctx->output))
return HUBBUB_OK;
/* Now increment the output index so we don't re-expect this token */
ctx->output_index++;
/* Expected output must be an array */
assert(json_object_get_type(obj) == json_type_array);
items = json_object_get_array(obj);
printf("got %s: expected %s\n", token_names[token->type],
json_object_get_string((struct json_object *)
array_list_get_idx(items, 0)));
/* Make sure we got the token we expected */
assert(strcmp(token_names[token->type],
json_object_get_string((struct json_object *)
array_list_get_idx(items, 0))) == 0);
switch (token->type) {
case HUBBUB_TOKEN_DOCTYPE:
{
const char *expname = json_object_get_string(
array_list_get_idx(items, 1));
const char *exppub = json_object_get_string(
array_list_get_idx(items, 2));
const char *expsys = json_object_get_string(
array_list_get_idx(items, 3));
bool expquirks = !json_object_get_boolean(
array_list_get_idx(items, 4));
const char *gotname = (const char *)
token->data.doctype.name.ptr;
const char *gotpub, *gotsys;
printf("'%.*s' %sids:\n",
(int) token->data.doctype.name.len,
gotname,
token->data.doctype.force_quirks ?
"(force-quirks) " : "");
if (token->data.doctype.public_missing) {
gotpub = NULL;
printf("\tpublic: missing\n");
} else {
gotpub = (const char *)
token->data.doctype.public_id.ptr;
printf("\tpublic: '%.*s' (%d)\n",
(int) token->data.doctype.public_id.len,
gotpub,
(int) token->data.doctype.public_id.len);
}
if (token->data.doctype.system_missing) {
gotsys = NULL;
printf("\tsystem: missing\n");
} else {
gotsys = (const char *)
token->data.doctype.system_id.ptr;
printf("\tsystem: '%.*s' (%d)\n",
(int) token->data.doctype.system_id.len,
gotsys,
(int) token->data.doctype.system_id.len);
}
assert(token->data.doctype.name.len == strlen(expname));
assert(strncmp(gotname, expname, strlen(expname)) == 0);
assert((exppub == NULL) ==
(token->data.doctype.public_missing == true));
if (exppub) {
assert(token->data.doctype.public_id.len == strlen(exppub));
assert(strncmp(gotpub, exppub, strlen(exppub)) == 0);
}
assert((expsys == NULL) ==
(token->data.doctype.system_missing == true));
if (gotsys) {
assert(token->data.doctype.system_id.len == strlen(expsys));
assert(strncmp(gotsys, expsys, strlen(expsys)) == 0);
}
assert(expquirks == token->data.doctype.force_quirks);
}
break;
case HUBBUB_TOKEN_START_TAG:
{
const char *expname = json_object_get_string(
array_list_get_idx(items, 1));
struct lh_entry *expattrs = json_object_get_object(
array_list_get_idx(items, 2))->head;
bool self_closing = json_object_get_boolean(
array_list_get_idx(items, 3));
const char *tagname = (const char *)
token->data.tag.name.ptr;
printf("expected: '%s' %s\n",
expname,
(self_closing) ? "(self-closing) " : "");
printf(" got: '%.*s' %s\n",
(int) token->data.tag.name.len,
tagname,
(token->data.tag.self_closing) ?
"(self-closing) " : "");
if (token->data.tag.n_attributes > 0) {
printf("attributes:\n");
}
assert(token->data.tag.name.len == strlen(expname));
assert(strncmp(tagname, expname, strlen(expname)) == 0);
assert((token->data.tag.n_attributes == 0) ==
(expattrs == NULL));
assert(self_closing == token->data.tag.self_closing);
for (i = 0; i < token->data.tag.n_attributes; i++) {
char *expname = (char *) expattrs->k;
const char *expval = json_object_get_string(
(struct json_object *) expattrs->v);
const char *gotname = (const char *)
token->data.tag.attributes[i].name.ptr;
size_t namelen =
token->data.tag.attributes[i].name.len;
const char *gotval = (const char *)
token->data.tag.attributes[i].value.ptr;
size_t vallen =
token->data.tag.attributes[i].value.len;
printf("\t'%.*s' = '%.*s'\n",
(int) namelen, gotname,
(int) vallen, gotval);
assert(namelen == strlen(expname));
assert(strncmp(gotname, expname,
strlen(expname)) == 0);
assert(vallen == strlen(expval));
assert(strncmp(gotval, expval, strlen(expval)) == 0);
expattrs = expattrs->next;
}
assert(expattrs == NULL);
}
break;
case HUBBUB_TOKEN_END_TAG:
{
const char *expname = json_object_get_string(
array_list_get_idx(items, 1));
const char *tagname = (const char *)
token->data.tag.name.ptr;
printf("'%.*s' %s\n",
(int) token->data.tag.name.len,
tagname,
(token->data.tag.n_attributes > 0) ?
"attributes:" : "");
assert(token->data.tag.name.len == strlen(expname));
assert(strncmp(tagname, expname, strlen(expname)) == 0);
}
break;
case HUBBUB_TOKEN_COMMENT:
{
const char *expstr = json_object_get_string(
array_list_get_idx(items, 1));
const char *gotstr = (const char *)
token->data.comment.ptr;
printf("expected: '%s'\n", expstr);
printf(" got: '%.*s'\n",
(int) token->data.comment.len, gotstr);
assert(token->data.comment.len == strlen(expstr));
assert(strncmp(gotstr, expstr, strlen(expstr)) == 0);
}
break;
case HUBBUB_TOKEN_CHARACTER:
{
int expstrlen = json_object_get_string_len(
array_list_get_idx(items, 1));
const char *expstr = json_object_get_string(
array_list_get_idx(items, 1));
const char *gotstr = (const char *)
token->data.character.ptr;
size_t len = min(token->data.character.len,
expstrlen - ctx->char_off);
printf("expected: '%.*s'\n",
(int) len, expstr + ctx->char_off);
printf(" got: '%.*s'\n",
(int) token->data.character.len, gotstr);
assert(memcmp(gotstr, expstr + ctx->char_off, len) == 0);
if (len < token->data.character.len) {
/* Expected token only contained part of the data
* Calculate how much is left, then try again with
* the next expected token */
hubbub_token t;
t.type = HUBBUB_TOKEN_CHARACTER;
t.data.character.ptr += len;
t.data.character.len -= len;
ctx->char_off = 0;
token_handler(&t, pw);
} else if (strlen(expstr + ctx->char_off) >
token->data.character.len) {
/* Tokeniser output only contained part of the data
* in the expected token; calculate the offset into
* the token and process the remainder next time */
ctx->char_off += len;
ctx->output_index--;
} else {
/* Exact match - clear offset */
ctx->char_off = 0;
}
}
break;
case HUBBUB_TOKEN_EOF:
printf("\n");
break;
}
return HUBBUB_OK;
}
|