Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2009, 2010, 2014 Nicira, Inc.
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at:
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #include <config.h>
18 : : #undef NDEBUG
19 : : #include "openvswitch/json.h"
20 : : #include <ctype.h>
21 : : #include <errno.h>
22 : : #include <getopt.h>
23 : : #include <stdio.h>
24 : : #include "ovstest.h"
25 : : #include "util.h"
26 : :
27 : : /* --pretty: If set, the JSON output is pretty-printed, instead of printed as
28 : : * compactly as possible. */
29 : : static int pretty = 0;
30 : :
31 : : /* --multiple: If set, the input is a sequence of JSON objects or arrays,
32 : : * instead of exactly one object or array. */
33 : : static int multiple = 0;
34 : :
35 : : static bool
36 : 123 : print_and_free_json(struct json *json)
37 : : {
38 : : bool ok;
39 [ + + ]: 123 : if (json->type == JSON_STRING) {
40 : 40 : printf("error: %s\n", json->u.string);
41 : 40 : ok = false;
42 : : } else {
43 [ - + ]: 83 : char *s = json_to_string(json, JSSF_SORT | (pretty ? JSSF_PRETTY : 0));
44 : 83 : puts(s);
45 : 83 : free(s);
46 : 83 : ok = true;
47 : : }
48 : 123 : json_destroy(json);
49 : 123 : return ok;
50 : : }
51 : :
52 : : static bool
53 : 16 : refill(FILE *file, void *buffer, size_t buffer_size, size_t *n, size_t *used)
54 : : {
55 : 16 : *used = 0;
56 [ + + ]: 16 : if (feof(file)) {
57 : 8 : *n = 0;
58 : 8 : return false;
59 : : } else {
60 : 8 : *n = fread(buffer, 1, buffer_size, file);
61 [ - + ]: 8 : if (ferror(file)) {
62 : 0 : ovs_fatal(errno, "Error reading input file");
63 : : }
64 : 8 : return *n > 0;
65 : : }
66 : : }
67 : :
68 : : static bool
69 : 8 : parse_multiple(FILE *stream)
70 : : {
71 : : struct json_parser *parser;
72 : : char buffer[BUFSIZ];
73 : : size_t n, used;
74 : : bool ok;
75 : :
76 : 8 : parser = NULL;
77 : 8 : n = used = 0;
78 : 8 : ok = true;
79 [ + + ][ + + ]: 109 : while (used < n || refill(stream, buffer, sizeof buffer, &n, &used)) {
80 [ + - ][ + + ]: 101 : if (!parser && isspace((unsigned char) buffer[used])) {
81 : : /* Skip white space. */
82 : 44 : used++;
83 : : } else {
84 [ + - ]: 57 : if (!parser) {
85 : 57 : parser = json_parser_create(0);
86 : : }
87 : :
88 : 57 : used += json_parser_feed(parser, &buffer[used], n - used);
89 [ + + ]: 57 : if (used < n) {
90 [ + + ]: 51 : if (!print_and_free_json(json_parser_finish(parser))) {
91 : 1 : ok = false;
92 : : }
93 : 51 : parser = NULL;
94 : : }
95 : : }
96 : : }
97 [ + + ]: 8 : if (parser) {
98 [ + + ]: 6 : if (!print_and_free_json(json_parser_finish(parser))) {
99 : 1 : ok = false;
100 : : }
101 : : }
102 : 8 : return ok;
103 : : }
104 : :
105 : : static void
106 : 74 : test_json_main(int argc, char *argv[])
107 : : {
108 : : const char *input_file;
109 : : FILE *stream;
110 : : bool ok;
111 : :
112 : 74 : set_program_name(argv[0]);
113 : :
114 : : for (;;) {
115 : : static const struct option options[] = {
116 : : {"pretty", no_argument, &pretty, 1},
117 : : {"multiple", no_argument, &multiple, 1},
118 : : };
119 : 82 : int option_index = 0;
120 : 82 : int c = getopt_long (argc, argv, "", options, &option_index);
121 : :
122 [ + + ]: 82 : if (c == -1) {
123 : 74 : break;
124 : : }
125 [ + - - ]: 8 : switch (c) {
126 : : case 0:
127 : 8 : break;
128 : :
129 : : case '?':
130 : 0 : exit(1);
131 : :
132 : : default:
133 : 0 : abort();
134 : : }
135 : 8 : }
136 : :
137 [ - + ]: 74 : if (argc - optind != 1) {
138 : 0 : ovs_fatal(0, "usage: %s [--pretty] [--multiple] INPUT.json",
139 : : program_name);
140 : : }
141 : :
142 : 74 : input_file = argv[optind];
143 [ + + ]: 74 : stream = !strcmp(input_file, "-") ? stdin : fopen(input_file, "r");
144 [ - + ]: 74 : if (!stream) {
145 : 0 : ovs_fatal(errno, "Cannot open \"%s\"", input_file);
146 : : }
147 : :
148 [ + + ]: 74 : if (multiple) {
149 : 8 : ok = parse_multiple(stream);
150 : : } else {
151 : 66 : ok = print_and_free_json(json_from_stream(stream));
152 : : }
153 : :
154 : 74 : fclose(stream);
155 : :
156 : 74 : exit(!ok);
157 : : }
158 : :
159 : 1248 : OVSTEST_REGISTER("test-json", test_json_main);
|