json_objectwriter_test.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #include <google/protobuf/util/internal/json_objectwriter.h>
  31. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  32. #include <google/protobuf/util/internal/utility.h>
  33. #include <gtest/gtest.h>
  34. namespace google {
  35. namespace protobuf {
  36. namespace util {
  37. namespace converter {
  38. using google::protobuf::io::CodedOutputStream;
  39. using google::protobuf::io::StringOutputStream;
  40. class JsonObjectWriterTest : public ::testing::Test {
  41. protected:
  42. JsonObjectWriterTest()
  43. : str_stream_(new StringOutputStream(&output_)),
  44. out_stream_(new CodedOutputStream(str_stream_)),
  45. ow_(nullptr) {}
  46. virtual ~JsonObjectWriterTest() {
  47. delete ow_;
  48. delete out_stream_;
  49. delete str_stream_;
  50. }
  51. string output_;
  52. StringOutputStream* const str_stream_;
  53. CodedOutputStream* const out_stream_;
  54. JsonObjectWriter* ow_;
  55. };
  56. TEST_F(JsonObjectWriterTest, EmptyRootObject) {
  57. ow_ = new JsonObjectWriter("", out_stream_);
  58. ow_->StartObject("")->EndObject();
  59. EXPECT_EQ("{}", output_.substr(0, out_stream_->ByteCount()));
  60. }
  61. TEST_F(JsonObjectWriterTest, EmptyObject) {
  62. ow_ = new JsonObjectWriter("", out_stream_);
  63. ow_->StartObject("")
  64. ->RenderString("test", "value")
  65. ->StartObject("empty")
  66. ->EndObject()
  67. ->EndObject();
  68. EXPECT_EQ("{\"test\":\"value\",\"empty\":{}}",
  69. output_.substr(0, out_stream_->ByteCount()));
  70. }
  71. TEST_F(JsonObjectWriterTest, EmptyRootList) {
  72. ow_ = new JsonObjectWriter("", out_stream_);
  73. ow_->StartList("")->EndList();
  74. EXPECT_EQ("[]", output_.substr(0, out_stream_->ByteCount()));
  75. }
  76. TEST_F(JsonObjectWriterTest, EmptyList) {
  77. ow_ = new JsonObjectWriter("", out_stream_);
  78. ow_->StartObject("")
  79. ->RenderString("test", "value")
  80. ->StartList("empty")
  81. ->EndList()
  82. ->EndObject();
  83. EXPECT_EQ("{\"test\":\"value\",\"empty\":[]}",
  84. output_.substr(0, out_stream_->ByteCount()));
  85. }
  86. TEST_F(JsonObjectWriterTest, EmptyObjectKey) {
  87. ow_ = new JsonObjectWriter("", out_stream_);
  88. ow_->StartObject("")->RenderString("", "value")->EndObject();
  89. EXPECT_EQ("{\"\":\"value\"}", output_.substr(0, out_stream_->ByteCount()));
  90. }
  91. TEST_F(JsonObjectWriterTest, ObjectInObject) {
  92. ow_ = new JsonObjectWriter("", out_stream_);
  93. ow_->StartObject("")
  94. ->StartObject("nested")
  95. ->RenderString("field", "value")
  96. ->EndObject()
  97. ->EndObject();
  98. EXPECT_EQ("{\"nested\":{\"field\":\"value\"}}",
  99. output_.substr(0, out_stream_->ByteCount()));
  100. }
  101. TEST_F(JsonObjectWriterTest, ListInObject) {
  102. ow_ = new JsonObjectWriter("", out_stream_);
  103. ow_->StartObject("")
  104. ->StartList("nested")
  105. ->RenderString("", "value")
  106. ->EndList()
  107. ->EndObject();
  108. EXPECT_EQ("{\"nested\":[\"value\"]}",
  109. output_.substr(0, out_stream_->ByteCount()));
  110. }
  111. TEST_F(JsonObjectWriterTest, ObjectInList) {
  112. ow_ = new JsonObjectWriter("", out_stream_);
  113. ow_->StartList("")
  114. ->StartObject("")
  115. ->RenderString("field", "value")
  116. ->EndObject()
  117. ->EndList();
  118. EXPECT_EQ("[{\"field\":\"value\"}]",
  119. output_.substr(0, out_stream_->ByteCount()));
  120. }
  121. TEST_F(JsonObjectWriterTest, ListInList) {
  122. ow_ = new JsonObjectWriter("", out_stream_);
  123. ow_->StartList("")
  124. ->StartList("")
  125. ->RenderString("", "value")
  126. ->EndList()
  127. ->EndList();
  128. EXPECT_EQ("[[\"value\"]]", output_.substr(0, out_stream_->ByteCount()));
  129. }
  130. TEST_F(JsonObjectWriterTest, RenderPrimitives) {
  131. ow_ = new JsonObjectWriter("", out_stream_);
  132. ow_->StartObject("")
  133. ->RenderBool("bool", true)
  134. ->RenderDouble("double", std::numeric_limits<double>::max())
  135. ->RenderFloat("float", std::numeric_limits<float>::max())
  136. ->RenderInt32("int", std::numeric_limits<int32>::min())
  137. ->RenderInt64("long", std::numeric_limits<int64>::min())
  138. ->RenderBytes("bytes", "abracadabra")
  139. ->RenderString("string", "string")
  140. ->RenderBytes("emptybytes", "")
  141. ->RenderString("emptystring", string())
  142. ->EndObject();
  143. EXPECT_EQ(
  144. "{\"bool\":true,"
  145. "\"double\":" +
  146. ValueAsString<double>(std::numeric_limits<double>::max()) +
  147. ","
  148. "\"float\":" +
  149. ValueAsString<float>(std::numeric_limits<float>::max()) +
  150. ","
  151. "\"int\":-2147483648,"
  152. "\"long\":\"-9223372036854775808\","
  153. "\"bytes\":\"YWJyYWNhZGFicmE=\","
  154. "\"string\":\"string\","
  155. "\"emptybytes\":\"\","
  156. "\"emptystring\":\"\"}",
  157. output_.substr(0, out_stream_->ByteCount()));
  158. }
  159. TEST_F(JsonObjectWriterTest, BytesEncodesAsNonWebSafeBase64) {
  160. string s;
  161. s.push_back('\377');
  162. s.push_back('\357');
  163. ow_ = new JsonObjectWriter("", out_stream_);
  164. ow_->StartObject("")->RenderBytes("bytes", s)->EndObject();
  165. // Non-web-safe would encode this as "/+8="
  166. EXPECT_EQ("{\"bytes\":\"/+8=\"}",
  167. output_.substr(0, out_stream_->ByteCount()));
  168. }
  169. TEST_F(JsonObjectWriterTest, PrettyPrintList) {
  170. ow_ = new JsonObjectWriter(" ", out_stream_);
  171. ow_->StartObject("")
  172. ->StartList("items")
  173. ->RenderString("", "item1")
  174. ->RenderString("", "item2")
  175. ->RenderString("", "item3")
  176. ->EndList()
  177. ->StartList("empty")
  178. ->EndList()
  179. ->EndObject();
  180. EXPECT_EQ(
  181. "{\n"
  182. " \"items\": [\n"
  183. " \"item1\",\n"
  184. " \"item2\",\n"
  185. " \"item3\"\n"
  186. " ],\n"
  187. " \"empty\": []\n"
  188. "}\n",
  189. output_.substr(0, out_stream_->ByteCount()));
  190. }
  191. TEST_F(JsonObjectWriterTest, PrettyPrintObject) {
  192. ow_ = new JsonObjectWriter(" ", out_stream_);
  193. ow_->StartObject("")
  194. ->StartObject("items")
  195. ->RenderString("key1", "item1")
  196. ->RenderString("key2", "item2")
  197. ->RenderString("key3", "item3")
  198. ->EndObject()
  199. ->StartObject("empty")
  200. ->EndObject()
  201. ->EndObject();
  202. EXPECT_EQ(
  203. "{\n"
  204. " \"items\": {\n"
  205. " \"key1\": \"item1\",\n"
  206. " \"key2\": \"item2\",\n"
  207. " \"key3\": \"item3\"\n"
  208. " },\n"
  209. " \"empty\": {}\n"
  210. "}\n",
  211. output_.substr(0, out_stream_->ByteCount()));
  212. }
  213. TEST_F(JsonObjectWriterTest, PrettyPrintEmptyObjectInEmptyList) {
  214. ow_ = new JsonObjectWriter(" ", out_stream_);
  215. ow_->StartObject("")
  216. ->StartList("list")
  217. ->StartObject("")
  218. ->EndObject()
  219. ->EndList()
  220. ->EndObject();
  221. EXPECT_EQ(
  222. "{\n"
  223. " \"list\": [\n"
  224. " {}\n"
  225. " ]\n"
  226. "}\n",
  227. output_.substr(0, out_stream_->ByteCount()));
  228. }
  229. TEST_F(JsonObjectWriterTest, PrettyPrintDoubleIndent) {
  230. ow_ = new JsonObjectWriter(" ", out_stream_);
  231. ow_->StartObject("")
  232. ->RenderBool("bool", true)
  233. ->RenderInt32("int", 42)
  234. ->EndObject();
  235. EXPECT_EQ(
  236. "{\n"
  237. " \"bool\": true,\n"
  238. " \"int\": 42\n"
  239. "}\n",
  240. output_.substr(0, out_stream_->ByteCount()));
  241. }
  242. TEST_F(JsonObjectWriterTest, StringsEscapedAndEnclosedInDoubleQuotes) {
  243. ow_ = new JsonObjectWriter("", out_stream_);
  244. ow_->StartObject("")->RenderString("string", "'<>&amp;\\\"\r\n")->EndObject();
  245. EXPECT_EQ("{\"string\":\"'\\u003c\\u003e&amp;\\\\\\\"\\r\\n\"}",
  246. output_.substr(0, out_stream_->ByteCount()));
  247. }
  248. TEST_F(JsonObjectWriterTest, Stringification) {
  249. ow_ = new JsonObjectWriter("", out_stream_);
  250. ow_->StartObject("")
  251. ->RenderDouble("double_nan", std::numeric_limits<double>::quiet_NaN())
  252. ->RenderFloat("float_nan", std::numeric_limits<float>::quiet_NaN())
  253. ->RenderDouble("double_pos", std::numeric_limits<double>::infinity())
  254. ->RenderFloat("float_pos", std::numeric_limits<float>::infinity())
  255. ->RenderDouble("double_neg", -std::numeric_limits<double>::infinity())
  256. ->RenderFloat("float_neg", -std::numeric_limits<float>::infinity())
  257. ->EndObject();
  258. EXPECT_EQ(
  259. "{\"double_nan\":\"NaN\","
  260. "\"float_nan\":\"NaN\","
  261. "\"double_pos\":\"Infinity\","
  262. "\"float_pos\":\"Infinity\","
  263. "\"double_neg\":\"-Infinity\","
  264. "\"float_neg\":\"-Infinity\"}",
  265. output_.substr(0, out_stream_->ByteCount()));
  266. }
  267. TEST_F(JsonObjectWriterTest, TestRegularByteEncoding) {
  268. ow_ = new JsonObjectWriter("", out_stream_);
  269. ow_->StartObject("")
  270. ->RenderBytes("bytes", "\x03\xef\xc0")
  271. ->EndObject();
  272. // Test that we get regular (non websafe) base64 encoding on byte fields by
  273. // default.
  274. EXPECT_EQ("{\"bytes\":\"A+/A\"}",
  275. output_.substr(0, out_stream_->ByteCount()));
  276. }
  277. TEST_F(JsonObjectWriterTest, TestWebsafeByteEncoding) {
  278. ow_ = new JsonObjectWriter("", out_stream_);
  279. ow_->set_use_websafe_base64_for_bytes(true);
  280. ow_->StartObject("")
  281. ->RenderBytes("bytes", "\x03\xef\xc0\x10")
  282. ->EndObject();
  283. // Test that we get websafe base64 encoding when explicitly asked.
  284. EXPECT_EQ("{\"bytes\":\"A-_AEA==\"}",
  285. output_.substr(0, out_stream_->ByteCount()));
  286. }
  287. } // namespace converter
  288. } // namespace util
  289. } // namespace protobuf
  290. } // namespace google