def_to_proto_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 
28 #include "upb/util/def_to_proto.h"
29 
30 #include "gmock/gmock.h"
31 #include "google/protobuf/descriptor.pb.h"
33 #include "google/protobuf/dynamic_message.h"
34 #include "google/protobuf/util/message_differencer.h"
35 #include "gtest/gtest.h"
36 #include "upb/def.hpp"
37 #include "upb/upb.hpp"
38 #include "upb/util/def_to_proto_test.upbdefs.h"
39 
40 // Loads and retrieves a descriptor for `msgdef` into the given `pool`.
43  upb::Arena tmp_arena;
46  upb_FileDef_ToProto(file.ptr(), tmp_arena.ptr());
47  size_t size;
49  upb_proto, tmp_arena.ptr(), &size);
51  google_proto.ParseFromArray(buf, size);
52  const google::protobuf::FileDescriptor* file_desc =
53  pool->BuildFile(google_proto);
54  EXPECT_TRUE(file_desc != nullptr);
55  return pool->FindMessageTypeByName(msgdef.full_name());
56 }
57 
58 // Converts a upb `msg` (with type `msgdef`) into a protobuf Message object from
59 // the given factory and descriptor.
60 std::unique_ptr<google::protobuf::Message> ToProto(
61  const upb_Message* msg, const upb_MessageDef* msgdef,
65  EXPECT_TRUE(desc != nullptr);
66  std::unique_ptr<google::protobuf::Message> google_msg(
67  factory->GetPrototype(desc)->New());
68  size_t size;
69  const char* buf =
71  google_msg->ParseFromArray(buf, size);
72  return google_msg;
73 }
74 
75 // A gtest matcher that verifies that a proto is equal to `proto`. Both `proto`
76 // and `arg` must be messages of type `msgdef_func` (a .upbdefs.h function that
77 // loads a known msgdef into the given symtab).
78 MATCHER_P2(EqualsUpbProto, proto, msgdef_func,
79  negation ? "are not equal" : "are equal") {
83  upb::MessageDefPtr msgdef(msgdef_func(symtab.ptr()));
84  EXPECT_TRUE(msgdef.ptr() != nullptr);
87  EXPECT_TRUE(desc != nullptr);
88  std::unique_ptr<google::protobuf::Message> m1(
89  ToProto(proto, msgdef.ptr(), desc, &factory));
90  std::unique_ptr<google::protobuf::Message> m2(
91  ToProto(arg, msgdef.ptr(), desc, &factory));
92  std::string differences;
94  differencer.ReportDifferencesToString(&differences);
95  bool eq = differencer.Compare(*m2, *m1);
96  if (!eq) {
97  *result_listener << differences;
98  }
99  return eq;
100 }
101 
102 // Verifies that the given upb FileDef can be converted to a proto that matches
103 // `proto`.
108  upb_FileDef_ToProto(file.ptr(), arena.ptr());
109  ASSERT_THAT(
110  proto,
112 }
113 
114 // Verifies that upb/util/def_to_proto_test.proto can round-trip:
115 // serialized descriptor -> upb def -> serialized descriptor
116 TEST(DefToProto, Test) {
119  upb_StringView test_file_desc =
120  upb_util_def_to_proto_test_proto_upbdefinit.descriptor;
121  const auto* file_desc = google_protobuf_FileDescriptorProto_parse(
122  test_file_desc.data, test_file_desc.size, arena.ptr());
123 
124  upb::MessageDefPtr msgdef(pkg_Message_getmsgdef(symtab.ptr()));
126  CheckFile(file, file_desc);
127 }
128 
129 // Like the previous test, but uses a message layout built at runtime.
130 TEST(DefToProto, TestRuntimeReflection) {
133  upb_StringView test_file_desc =
134  upb_util_def_to_proto_test_proto_upbdefinit.descriptor;
135  const auto* file_desc = google_protobuf_FileDescriptorProto_parse(
136  test_file_desc.data, test_file_desc.size, arena.ptr());
137 
139  symtab.ptr(), &upb_util_def_to_proto_test_proto_upbdefinit, true);
140  upb::FileDefPtr file = symtab.FindFileByName(
141  upb_util_def_to_proto_test_proto_upbdefinit.filename);
142  CheckFile(file, file_desc);
143 }
google_protobuf_FileDescriptorProto_getmsgdef
const UPB_INLINE upb_MessageDef * google_protobuf_FileDescriptorProto_getmsgdef(upb_DefPool *s)
Definition: descriptor.upbdefs.h:29
upb::SymbolTable
Definition: def.hpp:377
upb::MessageDefPtr
Definition: def.hpp:168
google::protobuf::util::MessageDifferencer
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/message_differencer.h:120
AddMessageDescriptor
const google::protobuf::Descriptor * AddMessageDescriptor(upb::MessageDefPtr msgdef, google::protobuf::DescriptorPool *pool)
Definition: def_to_proto_test.cc:41
upb_MessageDef_MiniTable
const upb_MiniTable * upb_MessageDef_MiniTable(const upb_MessageDef *m)
Definition: upb/upb/def.c:818
upb_StringView::data
const char * data
Definition: upb/upb/upb.h:73
upb_MessageDef
Definition: upb/upb/def.c:100
def_to_proto.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
google::protobuf::MessageFactory::GetPrototype
virtual const Message * GetPrototype(const Descriptor *type)=0
def.hpp
google_protobuf_FileDescriptorProto_serialize
UPB_INLINE char * google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto *msg, upb_Arena *arena, size_t *len)
Definition: descriptor.upb.h:242
upb_msgdef::file
const upb_filedef * file
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:2963
google::protobuf::MessageFactory
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:1066
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
upb_StringView::size
size_t size
Definition: upb/upb/upb.h:74
ASSERT_THAT
#define ASSERT_THAT(value, matcher)
google::protobuf::util::MessageDifferencer::Compare
bool Compare(const Message &message1, const Message &message2)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/message_differencer.cc:481
google::protobuf::DescriptorPool
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1539
arg
Definition: cmdline.cc:40
upb::FileDefPtr
Definition: def.hpp:349
google::protobuf::util::MessageDifferencer::ReportDifferencesToString
void ReportDifferencesToString(std::string *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/message_differencer.cc:448
descriptor.upbdefs.h
_upb_DefPool_LoadDefInitEx
bool _upb_DefPool_LoadDefInitEx(upb_DefPool *s, const _upb_DefPool_Init *init, bool rebuild_minitable)
Definition: upb/upb/def.c:3149
TEST
TEST(DefToProto, Test)
Definition: def_to_proto_test.cc:116
proto2
Definition: bloaty/third_party/googletest/googlemock/test/gmock-internal-utils_test.cc:64
upb_Message
void upb_Message
Definition: msg.h:49
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
symtab
upb_symtab * symtab
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:774
google::protobuf::DynamicMessageFactory
Definition: bloaty/third_party/protobuf/src/google/protobuf/dynamic_message.h:80
upb::Arena::ptr
upb_Arena * ptr()
Definition: upb.hpp:76
upb_Encode
char * upb_Encode(const void *msg, const upb_MiniTable *l, int options, upb_Arena *arena, size_t *size)
Definition: encode.c:573
absl::compare_internal::eq
eq
Definition: abseil-cpp/absl/types/compare.h:72
upb::Arena
Definition: upb.hpp:68
ToProto
std::unique_ptr< google::protobuf::Message > ToProto(const upb_Message *msg, const upb_MessageDef *msgdef, const google::protobuf::Descriptor *desc, google::protobuf::MessageFactory *factory)
Definition: def_to_proto_test.cc:60
upb_StringView
Definition: upb/upb/upb.h:72
upb.hpp
google_protobuf_FileDescriptorProto_parse
UPB_INLINE google_protobuf_FileDescriptorProto * google_protobuf_FileDescriptorProto_parse(const char *buf, size_t size, upb_Arena *arena)
Definition: descriptor.upb.h:223
msgdef
const upb_msgdef * msgdef
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:808
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
desc
#define desc
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.h:338
google::protobuf::Message::New
Message * New() const override=0
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
pool
InternalDescriptorPool * pool
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:807
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
grpc::protobuf::FileDescriptorProto
GRPC_CUSTOM_FILEDESCRIPTORPROTO FileDescriptorProto
Definition: include/grpcpp/impl/codegen/config_protobuf.h:86
upb_FileDef_ToProto
google_protobuf_FileDescriptorProto * upb_FileDef_ToProto(const upb_FileDef *f, upb_Arena *a)
Definition: def_to_proto.c:539
upb_msgdef::full_name
const char * full_name
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:2964
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
google_protobuf_FileDescriptorProto
struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto
Definition: descriptor.upb.h:51
CheckFile
void CheckFile(const upb::FileDefPtr file, const google_protobuf_FileDescriptorProto *proto)
Definition: def_to_proto_test.cc:104
Test
Definition: hpack_parser_test.cc:43
MATCHER_P2
MATCHER_P2(EqualsUpbProto, proto, msgdef_func, negation ? "are not equal" :"are equal")
Definition: def_to_proto_test.cc:78


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:09