summaryrefslogtreecommitdiff
path: root/tests/json/test_json.cc
blob: 1f7d364267af0306dd97b96772c7e07372cbd735 (plain)
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
/*
 *
 * A set of tests for JSON parsing and serialization.
 */

#include "tests/json/test.upbdefs.h"
#include "tests/test_util.h"
#include "tests/upb_test.h"
#include "upb/handlers.h"
#include "upb/json/parser.h"
#include "upb/json/printer.h"
#include "upb/upb.h"

#include <string>

// Macros for readability in test case list: allows us to give TEST("...") /
// EXPECT("...") pairs.
#define TEST(x)     x
#define EXPECT_SAME NULL
#define EXPECT(x)   x
#define TEST_SENTINEL { NULL, NULL }

struct TestCase {
  const char* input;
  const char* expected;
};

bool verbose = false;

static TestCase kTestRoundtripMessages[] = {
  // Test most fields here.
  {
    TEST("{\"optionalInt32\":-42,\"optionalString\":\"Test\\u0001Message\","
         "\"optionalMsg\":{\"foo\":42},"
         "\"optionalBool\":true,\"repeatedMsg\":[{\"foo\":1},"
         "{\"foo\":2}]}"),
    EXPECT_SAME
  },
  // We must also recognize raw proto names.
  {
    TEST("{\"optional_int32\":-42,\"optional_string\":\"Test\\u0001Message\","
         "\"optional_msg\":{\"foo\":42},"
         "\"optional_bool\":true,\"repeated_msg\":[{\"foo\":1},"
         "{\"foo\":2}]}"),
    EXPECT("{\"optionalInt32\":-42,\"optionalString\":\"Test\\u0001Message\","
           "\"optionalMsg\":{\"foo\":42},"
           "\"optionalBool\":true,\"repeatedMsg\":[{\"foo\":1},"
           "{\"foo\":2}]}")
  },
  // Test special escapes in strings.
  {
    TEST("{\"repeatedString\":[\"\\b\",\"\\r\",\"\\n\",\"\\f\",\"\\t\","
         "\"\uFFFF\"]}"),
    EXPECT_SAME
  },
  // Test enum symbolic names.
  {
    // The common case: parse and print the symbolic name.
    TEST("{\"optionalEnum\":\"A\"}"),
    EXPECT_SAME
  },
  {
    // Unknown enum value: will be printed as an integer.
    TEST("{\"optionalEnum\":42}"),
    EXPECT_SAME
  },
  {
    // Known enum value: we're happy to parse an integer but we will re-emit the
    // symbolic name.
    TEST("{\"optionalEnum\":1}"),
    EXPECT("{\"optionalEnum\":\"B\"}")
  },
  // UTF-8 tests: escapes -> literal UTF8 in output.
  {
    // Note double escape on \uXXXX: we want the escape to be processed by the
    // JSON parser, not by the C++ compiler!
    TEST("{\"optionalString\":\"\\u007F\"}"),
    EXPECT("{\"optionalString\":\"\x7F\"}")
  },
  {
    TEST("{\"optionalString\":\"\\u0080\"}"),
    EXPECT("{\"optionalString\":\"\xC2\x80\"}")
  },
  {
    TEST("{\"optionalString\":\"\\u07FF\"}"),
    EXPECT("{\"optionalString\":\"\xDF\xBF\"}")
  },
  {
    TEST("{\"optionalString\":\"\\u0800\"}"),
    EXPECT("{\"optionalString\":\"\xE0\xA0\x80\"}")
  },
  {
    TEST("{\"optionalString\":\"\\uFFFF\"}"),
    EXPECT("{\"optionalString\":\"\xEF\xBF\xBF\"}")
  },
  // map-field tests
  {
    TEST("{\"mapStringString\":{\"a\":\"value1\",\"b\":\"value2\","
         "\"c\":\"value3\"}}"),
    EXPECT_SAME
  },
  {
    TEST("{\"mapInt32String\":{\"1\":\"value1\",\"-1\":\"value2\","
         "\"1234\":\"value3\"}}"),
    EXPECT_SAME
  },
  {
    TEST("{\"mapBoolString\":{\"false\":\"value1\",\"true\":\"value2\"}}"),
    EXPECT_SAME
  },
  {
    TEST("{\"mapStringInt32\":{\"asdf\":1234,\"jkl;\":-1}}"),
    EXPECT_SAME
  },
  {
    TEST("{\"mapStringBool\":{\"asdf\":true,\"jkl;\":false}}"),
    EXPECT_SAME
  },
  {
    TEST("{\"mapStringMsg\":{\"asdf\":{\"foo\":42},\"jkl;\":{\"foo\":84}}}"),
    EXPECT_SAME
  },
  TEST_SENTINEL
};

static TestCase kTestRoundtripMessagesPreserve[] = {
  // Test most fields here.
  {
    TEST("{\"optional_int32\":-42,\"optional_string\":\"Test\\u0001Message\","
         "\"optional_msg\":{\"foo\":42},"
         "\"optional_bool\":true,\"repeated_msg\":[{\"foo\":1},"
         "{\"foo\":2}]}"),
    EXPECT_SAME
  },
  TEST_SENTINEL
};

class StringSink {
 public:
  StringSink() {
    upb_byteshandler_init(&byteshandler_);
    upb_byteshandler_setstring(&byteshandler_, &str_handler, NULL);
    upb_bytessink_reset(&bytessink_, &byteshandler_, &s_);
  }
  ~StringSink() { }

  upb_bytessink Sink() { return bytessink_; }

  const std::string& Data() { return s_; }

 private:

  static size_t str_handler(void* _closure, const void* hd,
                            const char* data, size_t len,
                            const upb_bufhandle* handle) {
    UPB_UNUSED(hd);
    UPB_UNUSED(handle);
    std::string* s = static_cast<std::string*>(_closure);
    std::string appended(data, len);
    s->append(data, len);
    return len;
  }

  upb_byteshandler byteshandler_;
  upb_bytessink bytessink_;
  std::string s_;
};

void test_json_roundtrip_message(const char* json_src,
                                 const char* json_expected,
                                 const upb::Handlers* serialize_handlers,
                                 const upb::json::ParserMethodPtr parser_method,
                                 int seam) {
  VerboseParserEnvironment env(verbose);
  StringSink data_sink;
  upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create(
      env.arena(), serialize_handlers, data_sink.Sink());
  upb::json::ParserPtr parser = upb::json::ParserPtr::Create(
      env.arena(), parser_method, NULL, printer.input(), false);
  env.ResetBytesSink(parser.input());
  env.Reset(json_src, strlen(json_src), false, false);

  bool ok = env.Start() &&
            env.ParseBuffer(seam) &&
            env.ParseBuffer(-1) &&
            env.End();

  ASSERT(ok);
  ASSERT(env.CheckConsistency());

  if (memcmp(json_expected,
             data_sink.Data().data(),
             data_sink.Data().size())) {
    fprintf(stderr,
            "JSON parse/serialize roundtrip result differs:\n"
            "Original:\n%s\nParsed/Serialized:\n%s\n",
            json_src, data_sink.Data().c_str());
    abort();
  }
}

// Starts with a message in JSON format, parses and directly serializes again,
// and compares the result.
void test_json_roundtrip() {
  upb::SymbolTable symtab;
  upb::HandlerCache serialize_handlercache(
      upb::json::PrinterPtr::NewCache(false));
  upb::json::CodeCache parse_codecache;

  upb::MessageDefPtr md(upb_test_json_TestMessage_getmsgdef(symtab.ptr()));
  ASSERT(md);
  const upb::Handlers* serialize_handlers = serialize_handlercache.Get(md);
  const upb::json::ParserMethodPtr parser_method = parse_codecache.Get(md);
  ASSERT(serialize_handlers);

  for (const TestCase* test_case = kTestRoundtripMessages;
       test_case->input != NULL; test_case++) {
    const char *expected =
        (test_case->expected == EXPECT_SAME) ?
        test_case->input :
        test_case->expected;

    for (size_t i = 0; i < strlen(test_case->input); i++) {
      test_json_roundtrip_message(test_case->input, expected,
                                  serialize_handlers, parser_method, i);
    }
  }

  serialize_handlercache = upb::json::PrinterPtr::NewCache(true);
  serialize_handlers = serialize_handlercache.Get(md);

  for (const TestCase* test_case = kTestRoundtripMessagesPreserve;
       test_case->input != NULL; test_case++) {
    const char *expected =
        (test_case->expected == EXPECT_SAME) ?
        test_case->input :
        test_case->expected;

    for (size_t i = 0; i < strlen(test_case->input); i++) {
      test_json_roundtrip_message(test_case->input, expected,
                                  serialize_handlers, parser_method, i);
    }
  }
}

extern "C" {
int run_tests(int argc, char *argv[]) {
  UPB_UNUSED(argc);
  UPB_UNUSED(argv);
  test_json_roundtrip();
  return 0;
}
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback