summaryrefslogtreecommitdiff
path: root/libjsonpb/verify/test.cpp
blob: cb98f47fbbe67eac49da9986741bf49ee10862bf (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
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
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <limits>

#include <sstream>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <json/writer.h>
#include <jsonpb/jsonpb.h>
#include <jsonpb/verify.h>

#include "test.pb.h"

using ::android::jsonpb::internal::FormatJson;
using ::testing::ElementsAre;
using ::testing::HasSubstr;

namespace android {
namespace jsonpb {

// Unit tests for libjsonpbverify.

class LibJsonpbVerifyTest : public ::testing::Test {};

class JsonKeyTest : public LibJsonpbVerifyTest {
 public:
  template <typename T>
  std::string GetFieldJsonName(const std::string& field_name) {
    return T{}.GetDescriptor()->FindFieldByName(field_name)->json_name();
  }

  template <typename T>
  void TestParseOkWithUnknownKey(const std::string& field_name,
                                 const std::string& json_key) {
    std::string json = "{\"" + json_key + "\": \"test\"}";
    auto object = JsonStringToMessage<T>(json);
    ASSERT_TRUE(object.ok()) << object.error();
    EXPECT_EQ(
        "test",
        object->GetReflection()->GetString(
            *object, object->GetDescriptor()->FindFieldByName(field_name)));
    std::string error;
    ASSERT_FALSE(AllFieldsAreKnown(*object, json, &error))
        << "AllFieldsAreKnown should return false";
    EXPECT_THAT(error, HasSubstr("unknown keys"));
    EXPECT_THAT(error, HasSubstr(json_key));
  }
};

TEST_F(JsonKeyTest, WithJsonNameOk) {
  std::string json =
      "{\n"
      "    \"FOOBAR\": \"foo_bar\",\n"
      "    \"BarBaz\": \"barBaz\",\n"
      "    \"baz_qux\": \"BazQux\",\n"
      "    \"quxQuux\": \"QUX_QUUX\"\n"
      "\n}";
  auto object = JsonStringToMessage<WithJsonName>(json);
  ASSERT_TRUE(object.ok()) << object.error();

  EXPECT_EQ("foo_bar", object->foo_bar());
  EXPECT_EQ("barBaz", object->barbaz());
  EXPECT_EQ("BazQux", object->bazqux());
  EXPECT_EQ("QUX_QUUX", object->qux_quux());

  std::string error;
  EXPECT_TRUE(AllFieldsAreKnown(*object, json, &error)) << error;
}

// If Prototype field name as keys while json_name is present, AllFieldsAreKnown
// should return false.
TEST_F(JsonKeyTest, WithJsonNameFooBar) {
  TestParseOkWithUnknownKey<WithJsonName>("foo_bar", "foo_bar");
}

TEST_F(JsonKeyTest, WithJsonNameBarBaz) {
  TestParseOkWithUnknownKey<WithJsonName>("barBaz", "barBaz");
}

TEST_F(JsonKeyTest, WithJsonNameBazQux) {
  TestParseOkWithUnknownKey<WithJsonName>("BazQux", "BazQux");
}

TEST_F(JsonKeyTest, WithJsonNameQuxQuux) {
  TestParseOkWithUnknownKey<WithJsonName>("QUX_QUUX", "QUX_QUUX");
}

// JSON field name matches Proto field name
TEST_F(JsonKeyTest, NoJsonNameOk) {
  std::string json =
      "{\n"
      "    \"foo_bar\": \"foo_bar\",\n"
      "    \"barBaz\": \"barBaz\",\n"
      "    \"BazQux\": \"BazQux\",\n"
      "    \"QUX_QUUX\": \"QUX_QUUX\"\n"
      "\n}";
  auto object = JsonStringToMessage<NoJsonName>(json);
  ASSERT_TRUE(object.ok()) << object.error();

  EXPECT_EQ("foo_bar", object->foo_bar());
  EXPECT_EQ("barBaz", object->barbaz());
  EXPECT_EQ("BazQux", object->bazqux());
  EXPECT_EQ("QUX_QUUX", object->qux_quux());

  std::string error;
  EXPECT_TRUE(AllFieldsAreKnown(*object, json, &error)) << error;
}

// JSON field name is lower/UpperCamelCase of Proto field name;
// AllFieldsAreKnown should return false. Although the lower/UpperCamelCase name
// is a valid key accepted by Protobuf's JSON parser, we explicitly disallow the
// behavior.
TEST_F(JsonKeyTest, NoJsonNameFooBar) {
  EXPECT_EQ("fooBar", GetFieldJsonName<NoJsonName>("foo_bar"));
  TestParseOkWithUnknownKey<NoJsonName>("foo_bar", "fooBar");
}

TEST_F(JsonKeyTest, NoJsonNameBarBaz) {
  EXPECT_EQ("barBaz", GetFieldJsonName<NoJsonName>("barBaz"));
  // No test for barBaz because its JSON name is the same as field_name
}

TEST_F(JsonKeyTest, NoJsonNameBazQux) {
  EXPECT_EQ("BazQux", GetFieldJsonName<NoJsonName>("BazQux"));
  // No test for BazQux because its JSON name is the same as field_name
}

TEST_F(JsonKeyTest, NoJsonNameQuxQuux) {
  EXPECT_EQ("QUXQUUX", GetFieldJsonName<NoJsonName>("QUX_QUUX"));
  TestParseOkWithUnknownKey<NoJsonName>("QUX_QUUX", "QUXQUUX");
}

class EmbeddedJsonKeyTest : public LibJsonpbVerifyTest {
 public:
  ErrorOr<Parent> TestEmbeddedError(const std::string& json,
                                    const std::string& unknown_key) {
    auto object = JsonStringToMessage<Parent>(json);
    if (!object.ok()) return object;
    std::string error;
    EXPECT_FALSE(AllFieldsAreKnown(*object, json, &error))
        << "AllFieldsAreKnown should return false";
    EXPECT_THAT(error, HasSubstr("unknown keys"));
    EXPECT_THAT(error, HasSubstr(unknown_key));
    return object;
  }
};

TEST_F(EmbeddedJsonKeyTest, Ok) {
  std::string json =
      "{"
      "    \"with_json_name\": {\"FOOBAR\": \"foo_bar\"},\n"
      "    \"repeated_with_json_name\": [{\"BarBaz\": \"barBaz\"}],\n"
      "    \"no_json_name\": {\"BazQux\": \"BazQux\"},\n"
      "    \"repeated_no_json_name\": [{\"QUX_QUUX\": \"QUX_QUUX\"}]\n"
      "}";
  auto object = JsonStringToMessage<Parent>(json);
  ASSERT_TRUE(object.ok()) << object.error();

  EXPECT_EQ("foo_bar", object->with_json_name().foo_bar());
  ASSERT_EQ(1u, object->repeated_with_json_name().size());
  EXPECT_EQ("barBaz", object->repeated_with_json_name().begin()->barbaz());
  EXPECT_EQ("BazQux", object->no_json_name().bazqux());
  ASSERT_EQ(1u, object->repeated_no_json_name().size());
  EXPECT_EQ("QUX_QUUX", object->repeated_no_json_name().begin()->qux_quux());

  std::string error;
  EXPECT_TRUE(AllFieldsAreKnown(*object, json, &error)) << error;
}

TEST_F(EmbeddedJsonKeyTest, FooBar) {
  auto object = TestEmbeddedError(
      "{\"with_json_name\": {\"foo_bar\": \"test\"}}", "foo_bar");
  ASSERT_TRUE(object.ok()) << object.error();
  EXPECT_EQ("test", object->with_json_name().foo_bar());
}

TEST_F(EmbeddedJsonKeyTest, BarBaz) {
  auto object = TestEmbeddedError(
      "{\"repeated_with_json_name\": [{\"barBaz\": \"test\"}]}", "barBaz");
  ASSERT_TRUE(object.ok()) << object.error();
  ASSERT_EQ(1u, object->repeated_with_json_name().size());
  EXPECT_EQ("test", object->repeated_with_json_name().begin()->barbaz());
}

TEST_F(EmbeddedJsonKeyTest, NoJsonName) {
  auto object = TestEmbeddedError(
      "{\"no_json_name\": {\"QUXQUUX\": \"test\"}}", "QUXQUUX");
  ASSERT_TRUE(object.ok()) << object.error();
  EXPECT_EQ("test", object->no_json_name().qux_quux());
}

TEST_F(EmbeddedJsonKeyTest, QuxQuux) {
  auto object = TestEmbeddedError(
      "{\"repeated_no_json_name\": [{\"QUXQUUX\": \"test\"}]}", "QUXQUUX");
  ASSERT_TRUE(object.ok()) << object.error();
  ASSERT_EQ(1u, object->repeated_no_json_name().size());
  EXPECT_EQ("test", object->repeated_no_json_name().begin()->qux_quux());
}

class ScalarTest : public LibJsonpbVerifyTest {
 public:
  ::testing::AssertionResult IsJsonEq(const std::string& l,
                                      const std::string& r) {
    Json::Reader reader;
    Json::Value lvalue;
    if (!reader.parse(l, lvalue))
      return ::testing::AssertionFailure()
             << reader.getFormattedErrorMessages();
    Json::Value rvalue;
    if (!reader.parse(r, rvalue))
      return ::testing::AssertionFailure()
             << reader.getFormattedErrorMessages();
    Json::StyledWriter writer;
    return lvalue == rvalue
               ? (::testing::AssertionSuccess() << "Both are \n"
                                                << writer.write(lvalue))
               : (::testing::AssertionFailure()
                  << writer.write(lvalue) << "\n does not equal \n"
                  << writer.write(rvalue));
  }

  bool EqReformattedJson(const std::string& json, std::string* error) {
    return android::jsonpb::EqReformattedJson(json, &scalar_, error);
  }

  Scalar scalar_;
  std::string error_;
};

TEST_F(ScalarTest, Ok) {
  std::string json =
      "{\n"
      "    \"i32\": 1,\n"
      "    \"si32\": 1,\n"
      "    \"i64\": \"1\",\n"
      "    \"si64\": \"1\",\n"
      "    \"f\": 1.5,\n"
      "    \"d\": 1.5,\n"
      "    \"e\": \"FOO\"\n"
      "}";
  auto formatted = FormatJson(json, &scalar_);
  ASSERT_TRUE(formatted.ok()) << formatted.error();
  EXPECT_TRUE(IsJsonEq(json, *formatted));

  EXPECT_TRUE(EqReformattedJson(json, &error_)) << error_;
}

using ScalarTestErrorParam = std::tuple<const char*, const char*>;
class ScalarTestError
    : public ScalarTest,
      public ::testing::WithParamInterface<ScalarTestErrorParam> {};

TEST_P(ScalarTestError, Test) {
  std::string json;
  std::string message;
  std::tie(json, message) = GetParam();
  auto formatted = FormatJson(json, &scalar_);
  ASSERT_TRUE(formatted.ok()) << formatted.error();
  EXPECT_FALSE(IsJsonEq(json, *formatted)) << message;
  EXPECT_FALSE(EqReformattedJson(json, &error_))
      << "EqReformattedJson should return false";
}

static const std::vector<ScalarTestErrorParam> gScalarTestErrorParams = {
    {"{\"i32\": \"1\"}", "Should not allow int32 values to be quoted"},
    {"{\"si32\": \"1\"}", "Should not allow sint32 values to be quoted"},
    {"{\"i64\": 1}", "Should require int64 values to be quoted"},
    {"{\"si64\": 1}", "Should require sint64 values to be quoted"},
    {"{\"f\": \"1.5\"}", "Should not allow float values to be quoted"},
    {"{\"d\": \"1.5\"}", "Should not allow double values to be quoted"},
    {"{\"e\": 1}", "Should not allow integers for enums"},
};

INSTANTIATE_TEST_SUITE_P(Jsonpb, ScalarTestError,
                         ::testing::ValuesIn(gScalarTestErrorParams));

int main(int argc, char** argv) {
  using ::testing::AddGlobalTestEnvironment;
  using ::testing::InitGoogleTest;

  InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

}  // namespace jsonpb
}  // namespace android