Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: third_party/protobuf/src/google/protobuf/util/json_util_test.cc

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update defines Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include <google/protobuf/util/json_util.h>
32
33 #include <list>
34 #include <string>
35
36 #include <google/protobuf/io/zero_copy_stream.h>
37 #include <google/protobuf/util/json_format_proto3.pb.h>
38 #include <google/protobuf/util/type_resolver.h>
39 #include <google/protobuf/util/type_resolver_util.h>
40 #include <gtest/gtest.h>
41
42 namespace google {
43 namespace protobuf {
44 namespace util {
45 namespace {
46
47 using proto3::FOO;
48 using proto3::BAR;
49 using proto3::TestMessage;
50 using proto3::TestMap;
51
52 static const char kTypeUrlPrefix[] = "type.googleapis.com";
53
54 static string GetTypeUrl(const Descriptor* message) {
55 return string(kTypeUrlPrefix) + "/" + message->full_name();
56 }
57
58 // As functions defined in json_util.h are just thin wrappers around the
59 // JSON conversion code in //net/proto2/util/converter, in this test we
60 // only cover some very basic cases to make sure the wrappers have forwarded
61 // parameters to the underlying implementation correctly. More detailed
62 // tests are contained in the //net/proto2/util/converter directory.
63 class JsonUtilTest : public testing::Test {
64 protected:
65 JsonUtilTest() {
66 resolver_.reset(NewTypeResolverForDescriptorPool(
67 kTypeUrlPrefix, DescriptorPool::generated_pool()));
68 }
69
70 string ToJson(const Message& message, const JsonOptions& options) {
71 string result;
72 GOOGLE_CHECK_OK(BinaryToJsonString(resolver_.get(),
73 GetTypeUrl(message.GetDescriptor()),
74 message.SerializeAsString(), &result, options));
75 return result;
76 }
77
78 bool FromJson(const string& json, Message* message) {
79 string binary;
80 GOOGLE_CHECK_OK(JsonToBinaryString(
81 resolver_.get(), GetTypeUrl(message->GetDescriptor()), json, &binary));
82 return message->ParseFromString(binary);
83 }
84
85 google::protobuf::scoped_ptr<TypeResolver> resolver_;
86 };
87
88 TEST_F(JsonUtilTest, TestWhitespaces) {
89 TestMessage m;
90 m.mutable_message_value();
91
92 JsonOptions options;
93 EXPECT_EQ("{\"messageValue\":{}}", ToJson(m, options));
94 options.add_whitespace = true;
95 EXPECT_EQ(
96 "{\n"
97 " \"messageValue\": {}\n"
98 "}\n",
99 ToJson(m, options));
100 }
101
102 // TODO(skarvaje): Uncomment after cl/96232915 is submitted.
103 // TEST_F(JsonUtilTest, TestDefaultValues) {
104 // TestMessage m;
105 // JsonOptions options;
106 // EXPECT_EQ("{}", ToJson(m, options));
107 // options.always_print_primitive_fields = true;
108 // EXPECT_EQ(
109 // "{\"boolValue\":false,"
110 // "\"int32Value\":0,"
111 // "\"int64Value\":\"0\","
112 // "\"uint32Value\":0,"
113 // "\"uint64Value\":\"0\","
114 // "\"floatValue\":0,"
115 // "\"doubleValue\":0,"
116 // "\"stringValue\":\"\","
117 // "\"bytesValue\":\"\","
118 // // TODO(xiaofeng): The default enum value should be FOO. I believe
119 // // this is a bug in DefaultValueObjectWriter.
120 // "\"enumValue\":null"
121 // "}",
122 // ToJson(m, options));
123 // }
124
125 TEST_F(JsonUtilTest, ParseMessage) {
126 // Some random message but good enough to verify that the parsing warpper
127 // functions are working properly.
128 string input =
129 "{\n"
130 " \"int32Value\": 1024,\n"
131 " \"repeatedInt32Value\": [1, 2],\n"
132 " \"messageValue\": {\n"
133 " \"value\": 2048\n"
134 " },\n"
135 " \"repeatedMessageValue\": [\n"
136 " {\"value\": 40}, {\"value\": 96}\n"
137 " ]\n"
138 "}\n";
139 TestMessage m;
140 ASSERT_TRUE(FromJson(input, &m));
141 EXPECT_EQ(1024, m.int32_value());
142 ASSERT_EQ(2, m.repeated_int32_value_size());
143 EXPECT_EQ(1, m.repeated_int32_value(0));
144 EXPECT_EQ(2, m.repeated_int32_value(1));
145 EXPECT_EQ(2048, m.message_value().value());
146 ASSERT_EQ(2, m.repeated_message_value_size());
147 EXPECT_EQ(40, m.repeated_message_value(0).value());
148 EXPECT_EQ(96, m.repeated_message_value(1).value());
149 }
150
151 TEST_F(JsonUtilTest, ParseMap) {
152 TestMap message;
153 (*message.mutable_string_map())["hello"] = 1234;
154 JsonOptions options;
155 EXPECT_EQ("{\"stringMap\":{\"hello\":1234}}", ToJson(message, options));
156 TestMap other;
157 ASSERT_TRUE(FromJson(ToJson(message, options), &other));
158 EXPECT_EQ(message.DebugString(), other.DebugString());
159 }
160
161 typedef pair<char*, int> Segment;
162 // A ZeroCopyOutputStream that writes to multiple buffers.
163 class SegmentedZeroCopyOutputStream : public io::ZeroCopyOutputStream {
164 public:
165 explicit SegmentedZeroCopyOutputStream(list<Segment> segments)
166 : segments_(segments), last_segment_(static_cast<char*>(NULL), 0), byte_co unt_(0) {}
167
168 virtual bool Next(void** buffer, int* length) {
169 if (segments_.empty()) {
170 return false;
171 }
172 last_segment_ = segments_.front();
173 segments_.pop_front();
174 *buffer = last_segment_.first;
175 *length = last_segment_.second;
176 byte_count_ += *length;
177 return true;
178 }
179
180 virtual void BackUp(int length) {
181 GOOGLE_CHECK(length <= last_segment_.second);
182 segments_.push_front(
183 Segment(last_segment_.first + last_segment_.second - length, length));
184 last_segment_ = Segment(last_segment_.first, last_segment_.second - length);
185 byte_count_ -= length;
186 }
187
188 virtual int64 ByteCount() const { return byte_count_; }
189
190 private:
191 list<Segment> segments_;
192 Segment last_segment_;
193 int64 byte_count_;
194 };
195
196 // This test splits the output buffer and also the input data into multiple
197 // segments and checks that the implementation of ZeroCopyStreamByteSink
198 // handles all possible cases correctly.
199 TEST(ZeroCopyStreamByteSinkTest, TestAllInputOutputPatterns) {
200 static const int kOutputBufferLength = 10;
201 // An exhaustive test takes too long, skip some combinations to make the test
202 // run faster.
203 static const int kSkippedPatternCount = 7;
204
205 char buffer[kOutputBufferLength];
206 for (int split_pattern = 0; split_pattern < (1 << (kOutputBufferLength - 1));
207 split_pattern += kSkippedPatternCount) {
208 // Split the buffer into small segments according to the split_pattern.
209 list<Segment> segments;
210 int segment_start = 0;
211 for (int i = 0; i < kOutputBufferLength - 1; ++i) {
212 if (split_pattern & (1 << i)) {
213 segments.push_back(
214 Segment(buffer + segment_start, i - segment_start + 1));
215 segment_start = i + 1;
216 }
217 }
218 segments.push_back(
219 Segment(buffer + segment_start, kOutputBufferLength - segment_start));
220
221 // Write exactly 10 bytes through the ByteSink.
222 string input_data = "0123456789";
223 for (int input_pattern = 0; input_pattern < (1 << (input_data.size() - 1));
224 input_pattern += kSkippedPatternCount) {
225 memset(buffer, 0, sizeof(buffer));
226 {
227 SegmentedZeroCopyOutputStream output_stream(segments);
228 internal::ZeroCopyStreamByteSink byte_sink(&output_stream);
229 int start = 0;
230 for (int j = 0; j < input_data.length() - 1; ++j) {
231 if (input_pattern & (1 << j)) {
232 byte_sink.Append(&input_data[start], j - start + 1);
233 start = j + 1;
234 }
235 }
236 byte_sink.Append(&input_data[start], input_data.length() - start);
237 }
238 EXPECT_EQ(input_data, string(buffer, input_data.length()));
239 }
240
241 // Write only 9 bytes through the ByteSink.
242 input_data = "012345678";
243 for (int input_pattern = 0; input_pattern < (1 << (input_data.size() - 1));
244 input_pattern += kSkippedPatternCount) {
245 memset(buffer, 0, sizeof(buffer));
246 {
247 SegmentedZeroCopyOutputStream output_stream(segments);
248 internal::ZeroCopyStreamByteSink byte_sink(&output_stream);
249 int start = 0;
250 for (int j = 0; j < input_data.length() - 1; ++j) {
251 if (input_pattern & (1 << j)) {
252 byte_sink.Append(&input_data[start], j - start + 1);
253 start = j + 1;
254 }
255 }
256 byte_sink.Append(&input_data[start], input_data.length() - start);
257 }
258 EXPECT_EQ(input_data, string(buffer, input_data.length()));
259 EXPECT_EQ(0, buffer[input_data.length()]);
260 }
261
262 // Write 11 bytes through the ByteSink. The extra byte will just
263 // be ignored.
264 input_data = "0123456789A";
265 for (int input_pattern = 0; input_pattern < (1 << (input_data.size() - 1));
266 input_pattern += kSkippedPatternCount) {
267 memset(buffer, 0, sizeof(buffer));
268 {
269 SegmentedZeroCopyOutputStream output_stream(segments);
270 internal::ZeroCopyStreamByteSink byte_sink(&output_stream);
271 int start = 0;
272 for (int j = 0; j < input_data.length() - 1; ++j) {
273 if (input_pattern & (1 << j)) {
274 byte_sink.Append(&input_data[start], j - start + 1);
275 start = j + 1;
276 }
277 }
278 byte_sink.Append(&input_data[start], input_data.length() - start);
279 }
280 EXPECT_EQ(input_data.substr(0, kOutputBufferLength),
281 string(buffer, kOutputBufferLength));
282 }
283 }
284 }
285
286 } // namespace
287 } // namespace util
288 } // namespace protobuf
289 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698