required_fields_test.cc
Go to the documentation of this file.
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 
29 
30 #include "absl/strings/string_view.h"
31 #include "gmock/gmock.h"
32 #include "gtest/gtest.h"
33 #include "upb/def.hpp"
34 #include "upb/json_decode.h"
35 #include "upb/upb.hpp"
36 #include "upb/util/required_fields_test.upb.h"
37 #include "upb/util/required_fields_test.upbdefs.h"
38 
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 
61  const std::vector<std::string>& missing) {
64  upb_util_test_TestRequiredFields* test_msg =
65  upb_util_test_TestRequiredFields_new(arena.ptr());
67  upb_util_test_TestRequiredFields_getmsgdef(symtab.ptr()));
69  EXPECT_TRUE(upb_JsonDecode(json.data(), json.size(), test_msg, m.ptr(),
70  symtab.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(), symtab.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(), symtab.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 // }
94 TEST(RequiredFieldsTest, TestRequired) {
95  CheckRequired(R"json({})json", {"required_message"});
96  CheckRequired(R"json({"required_message": {}}")json", {});
98  R"json(
99  {
100  "optional_message": {}
101  }
102  )json",
103  {"required_message", "optional_message.required_message"});
104 
105  // Repeated field.
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.
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.
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.
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.
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.
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.
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 }
upb_FieldPathEntry
Definition: required_fields.h:52
upb::SymbolTable
Definition: def.hpp:377
upb::MessageDefPtr
Definition: def.hpp:168
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
status
absl::Status status
Definition: rls.cc:251
def.hpp
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
json_decode.h
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
before
IntBeforeRegisterTypedTestSuiteP before
Definition: googletest/googletest/test/gtest-typed-test_test.cc:376
absl::string_view::size
constexpr size_type size() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:277
upb_util_HasUnsetRequired
bool upb_util_HasUnsetRequired(const upb_Message *msg, const upb_MessageDef *m, const upb_DefPool *ext_pool, upb_FieldPathEntry **fields)
Definition: required_fields.c:293
CheckRequired
void CheckRequired(absl::string_view json, const std::vector< std::string > &missing)
Definition: required_fields_test.cc:60
symtab
upb_symtab * symtab
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:774
upb::Arena
Definition: upb.hpp:68
upb.hpp
TEST
TEST(RequiredFieldsTest, TestRequired)
Definition: required_fields_test.cc:94
PathsToText
std::vector< std::string > PathsToText(upb_FieldPathEntry *entry)
Definition: required_fields_test.cc:39
upb_FieldPath_ToText
size_t upb_FieldPath_ToText(upb_FieldPathEntry **path, char *buf, size_t size)
Definition: required_fields.c:119
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
upb_JsonDecode
bool upb_JsonDecode(const char *buf, size_t size, upb_Message *msg, const upb_MessageDef *m, const upb_DefPool *symtab, int options, upb_Arena *arena, upb_Status *status)
Definition: json_decode.c:1486
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
upb_FieldPathEntry::field
const upb_FieldDef * field
Definition: required_fields.h:53
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
regress.m
m
Definition: regress/regress.py:25
upb::Status
Definition: upb.hpp:35
absl::string_view::data
constexpr const_pointer data() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:336
required_fields.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:10