1 /*
2  * Copyright (c) 2009-2021, 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/util/required_fields.h"
29 
30 #include "gmock/gmock.h"
31 #include "gtest/gtest.h"
32 #include "absl/strings/string_view.h"
33 #include "upb/json_decode.h"
34 #include "upb/reflection/def.hpp"
35 #include "upb/upb.hpp"
36 #include "upb/util/required_fields_test.upb.h"
37 #include "upb/util/required_fields_test.upbdefs.h"
38 
PathsToText(upb_FieldPathEntry * entry)39 std::vector<std::string> PathsToText(upb_FieldPathEntry* entry) {
40   std::vector<std::string> ret;
41   char buf[1024];  // Larger than anything we'll use in this test.
42   while (entry->field) {
43     upb_FieldPathEntry* before = entry;
44     size_t len = upb_FieldPath_ToText(&entry, buf, sizeof(buf));
45     EXPECT_LT(len, sizeof(buf));
46     assert(len <= sizeof(buf));
47     ret.push_back(buf);
48 
49     // Ensure that we can have a short buffer and that it will be
50     // NULL-terminated.
51     char shortbuf[4];
52     size_t len2 = upb_FieldPath_ToText(&before, shortbuf, sizeof(shortbuf));
53     EXPECT_EQ(len, len2);
54     EXPECT_EQ(ret.back().substr(0, sizeof(shortbuf) - 1),
55               std::string(shortbuf));
56   }
57   return ret;
58 }
59 
CheckRequired(absl::string_view json,const std::vector<std::string> & missing)60 void CheckRequired(absl::string_view json,
61                    const std::vector<std::string>& missing) {
62   upb::Arena arena;
63   upb::DefPool defpool;
64   upb_util_test_TestRequiredFields* test_msg =
65       upb_util_test_TestRequiredFields_new(arena.ptr());
66   upb::MessageDefPtr m(
67       upb_util_test_TestRequiredFields_getmsgdef(defpool.ptr()));
68   upb::Status status;
69   EXPECT_TRUE(upb_JsonDecode(json.data(), json.size(), test_msg, m.ptr(),
70                              defpool.ptr(), 0, arena.ptr(), status.ptr()))
71       << status.error_message();
72   upb_FieldPathEntry* entries;
73   EXPECT_EQ(!missing.empty(), upb_util_HasUnsetRequired(
74                                   test_msg, m.ptr(), defpool.ptr(), &entries));
75   EXPECT_EQ(missing, PathsToText(entries));
76   free(entries);
77 
78   // Verify that we can pass a NULL pointer to entries when we don't care about
79   // them.
80   EXPECT_EQ(!missing.empty(),
81             upb_util_HasUnsetRequired(test_msg, m.ptr(), defpool.ptr(), NULL));
82 }
83 
84 // message HasRequiredField {
85 //   required int32 required_int32 = 1;
86 // }
87 //
88 // message TestRequiredFields {
89 //   required EmptyMessage required_message = 1;
90 //   optional TestRequiredFields optional_message = 2;
91 //   repeated HasRequiredField repeated_message = 3;
92 //   map<int32, HasRequiredField> map_int32_message = 4;
93 // }
TEST(RequiredFieldsTest,TestRequired)94 TEST(RequiredFieldsTest, TestRequired) {
95   CheckRequired(R"json({})json", {"required_message"});
96   CheckRequired(R"json({"required_message": {}}")json", {});
97   CheckRequired(
98       R"json(
99       {
100         "optional_message": {}
101       }
102       )json",
103       {"required_message", "optional_message.required_message"});
104 
105   // Repeated field.
106   CheckRequired(
107       R"json(
108       {
109         "optional_message": {
110           "repeated_message": [
111             {"required_int32": 1},
112             {},
113             {"required_int32": 2}
114           ]
115         }
116       }
117       )json",
118       {"required_message", "optional_message.required_message",
119        "optional_message.repeated_message[1].required_int32"});
120 
121   // Int32 map key.
122   CheckRequired(
123       R"json(
124       {
125         "required_message": {},
126         "map_int32_message": {
127           "1": {"required_int32": 1},
128           "5": {},
129           "9": {"required_int32": 1}
130         }
131       }
132       )json",
133       {"map_int32_message[5].required_int32"});
134 
135   // Int64 map key.
136   CheckRequired(
137       R"json(
138       {
139         "required_message": {},
140         "map_int64_message": {
141           "1": {"required_int32": 1},
142           "5": {},
143           "9": {"required_int32": 1}
144         }
145       }
146       )json",
147       {"map_int64_message[5].required_int32"});
148 
149   // Uint32 map key.
150   CheckRequired(
151       R"json(
152       {
153         "required_message": {},
154         "map_uint32_message": {
155           "1": {"required_int32": 1},
156           "5": {},
157           "9": {"required_int32": 1}
158         }
159       }
160       )json",
161       {"map_uint32_message[5].required_int32"});
162 
163   // Uint64 map key.
164   CheckRequired(
165       R"json(
166       {
167         "required_message": {},
168         "map_uint64_message": {
169           "1": {"required_int32": 1},
170           "5": {},
171           "9": {"required_int32": 1}
172         }
173       }
174       )json",
175       {"map_uint64_message[5].required_int32"});
176 
177   // Bool map key.
178   CheckRequired(
179       R"json(
180       {
181         "required_message": {},
182         "map_bool_message": {
183           "false": {"required_int32": 1},
184           "true": {}
185         }
186       }
187       )json",
188       {"map_bool_message[true].required_int32"});
189 
190   // String map key.
191   CheckRequired(
192       R"json(
193       {
194         "required_message": {},
195         "map_string_message": {
196           "abc": {"required_int32": 1},
197           "d\"ef": {}
198         }
199       }
200       )json",
201       {R"(map_string_message["d\"ef"].required_int32)"});
202 }
203