X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=tests%2Ftest-json.c;h=24bc745354138cacd349f1bc60fd41cb94c5eb48;hb=90b4feffb9a9031775b949090db6c1c3f963779a;hp=6261786db0a0e4a6139d33230b50dbd6553a7b69;hpb=f212909325be9bc7e296e1a32e2fc89694a0049f;p=sliver-openvswitch.git diff --git a/tests/test-json.c b/tests/test-json.c index 6261786db..24bc74535 100644 --- a/tests/test-json.c +++ b/tests/test-json.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009 Nicira Networks. + * Copyright (c) 2009, 2010, 2013 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ print_and_free_json(struct json *json) ok = false; } else { char *s = json_to_string(json, JSSF_SORT | (pretty ? JSSF_PRETTY : 0)); + ovs_assert(pretty || json_serialized_length(json) == strlen(s)); puts(s); free(s); ok = true; @@ -67,23 +68,17 @@ refill(FILE *file, void *buffer, size_t buffer_size, size_t *n, size_t *used) } static bool -parse_multiple(const char *input_file) +parse_multiple(FILE *stream) { struct json_parser *parser; char buffer[BUFSIZ]; size_t n, used; - FILE *file; bool ok; - file = fopen(input_file, "r"); - if (!file) { - ovs_fatal(errno, "Cannot open \"%s\"", input_file); - } - parser = NULL; n = used = 0; ok = true; - while (used < n || refill(file, buffer, sizeof buffer, &n, &used)) { + while (used < n || refill(stream, buffer, sizeof buffer, &n, &used)) { if (!parser && isspace((unsigned char) buffer[used])) { /* Skip white space. */ used++; @@ -113,6 +108,7 @@ int main(int argc, char *argv[]) { const char *input_file; + FILE *stream; bool ok; set_program_name(argv[0]); @@ -146,15 +142,18 @@ main(int argc, char *argv[]) } input_file = argv[optind]; - if (!strcmp(input_file, "-")) { - input_file = "/dev/stdin"; + stream = !strcmp(input_file, "-") ? stdin : fopen(input_file, "r"); + if (!stream) { + ovs_fatal(errno, "Cannot open \"%s\"", input_file); } if (multiple) { - ok = parse_multiple(input_file); + ok = parse_multiple(stream); } else { - ok = print_and_free_json(json_from_file(input_file)); + ok = print_and_free_json(json_from_stream(stream)); } + fclose(stream); + return !ok; }