1 /*
2  * Copyright (c) 2009-2022, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/json/encode.h"
29 
30 #include "google/protobuf/struct.upb.h"
31 #include "gtest/gtest.h"
32 #include "upb/json/test.upb.h"
33 #include "upb/json/test.upbdefs.h"
34 #include "upb/reflection/def.hpp"
35 #include "upb/upb.hpp"
36 
JsonEncode(const upb_test_Box * msg,int options)37 static std::string JsonEncode(const upb_test_Box* msg, int options) {
38   upb::Arena a;
39   upb::Status status;
40   upb::DefPool defpool;
41   upb::MessageDefPtr m(upb_test_Box_getmsgdef(defpool.ptr()));
42   EXPECT_TRUE(m.ptr() != nullptr);
43 
44   size_t json_size = upb_JsonEncode(msg, m.ptr(), defpool.ptr(), options, NULL,
45                                     0, status.ptr());
46   char* json_buf = (char*)upb_Arena_Malloc(a.ptr(), json_size + 1);
47 
48   size_t size = upb_JsonEncode(msg, m.ptr(), defpool.ptr(), options, json_buf,
49                                json_size + 1, status.ptr());
50   EXPECT_EQ(size, json_size);
51   return std::string(json_buf, json_size);
52 }
53 
54 // Encode a single optional enum.
TEST(JsonTest,EncodeEnum)55 TEST(JsonTest, EncodeEnum) {
56   upb::Arena a;
57 
58   upb_test_Box* foo = upb_test_Box_new(a.ptr());
59   upb_test_Box_set_first_tag(foo, upb_test_Z_BAR);
60 
61   EXPECT_EQ(R"({"firstTag":"Z_BAR"})", JsonEncode(foo, 0));
62   EXPECT_EQ(R"({"firstTag":1})",
63             JsonEncode(foo, upb_JsonEncode_FormatEnumsAsIntegers));
64 }
65 
66 // Encode a single optional negative enum.
TEST(JsonTest,EncodeNegativeEnum)67 TEST(JsonTest, EncodeNegativeEnum) {
68   upb::Arena a;
69 
70   upb_test_Box* foo = upb_test_Box_new(a.ptr());
71   upb_test_Box_set_last_tag(foo, upb_test_Z_BAZ);
72 
73   EXPECT_EQ(R"({"lastTag":"Z_BAZ"})", JsonEncode(foo, 0));
74   EXPECT_EQ(R"({"lastTag":-2})",
75             JsonEncode(foo, upb_JsonEncode_FormatEnumsAsIntegers));
76 }
77 
78 // Encode a single repeated enum.
TEST(JsonTest,EncodeRepeatedEnum)79 TEST(JsonTest, EncodeRepeatedEnum) {
80   upb::Arena a;
81 
82   upb_test_Box* foo = upb_test_Box_new(a.ptr());
83   upb_test_Box_add_more_tags(foo, upb_test_Z_BAT, a.ptr());
84 
85   EXPECT_EQ(R"({"moreTags":["Z_BAT"]})", JsonEncode(foo, 0));
86   EXPECT_EQ(R"({"moreTags":[13]})",
87             JsonEncode(foo, upb_JsonEncode_FormatEnumsAsIntegers));
88 }
89 
90 // Special case: encode null enum.
TEST(JsonTest,EncodeNullEnum)91 TEST(JsonTest, EncodeNullEnum) {
92   upb::Arena a;
93 
94   upb_test_Box* foo = upb_test_Box_new(a.ptr());
95   google_protobuf_Value_set_null_value(upb_test_Box_mutable_val(foo, a.ptr()),
96                                        google_protobuf_NULL_VALUE);
97 
98   EXPECT_EQ(R"({"val":null})", JsonEncode(foo, 0));
99   EXPECT_EQ(R"({"val":null})",
100             JsonEncode(foo, upb_JsonEncode_FormatEnumsAsIntegers));
101 }
102