@ -14,6 +14,7 @@
# include "google/protobuf/unknown_field_set.h"
# include <cstddef>
# include <string>
# include <vector>
@ -39,10 +40,27 @@
namespace google {
namespace protobuf {
namespace internal {
struct UnknownFieldSetTestPeer {
static auto AddLengthDelimitedUninitialized ( UnknownFieldSet & set , int number ,
size_t length ) {
return set . AddLengthDelimitedUninitialized ( number , length ) ;
}
} ;
} // namespace internal
using internal : : WireFormat ;
using : : testing : : ElementsAre ;
template < typename T >
T UnknownToProto ( const UnknownFieldSet & set ) {
T message ;
std : : string serialized_message ;
ABSL_CHECK ( set . SerializeToString ( & serialized_message ) ) ;
ABSL_CHECK ( message . ParseFromString ( serialized_message ) ) ;
return message ;
}
class UnknownFieldSetTest : public testing : : Test {
protected :
void SetUp ( ) override {
@ -181,7 +199,13 @@ static void PopulateUFS(UnknownFieldSet& set) {
node - > AddVarint ( 1 , 100 ) ;
const char * long_str = " This is a very long string, not sso " ;
node - > AddLengthDelimited ( 2 , long_str ) ;
node - > AddLengthDelimited ( 2 , std : : string ( long_str ) ) ;
node - > AddLengthDelimited ( 2 , absl : : Cord ( long_str ) ) ;
internal : : UnknownFieldSetTestPeer : : AddLengthDelimitedUninitialized ( * node , 2 ,
100 ) ;
# if !defined(PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE)
* node - > AddLengthDelimited ( 3 ) = long_str ;
# endif // PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE
// Test some recursion too.
node = node - > AddGroup ( 4 ) ;
}
@ -643,12 +667,20 @@ TEST_F(UnknownFieldSetTest, SpaceUsed) {
shadow_vector . Add ( ) ;
EXPECT_EQ ( total ( ) , empty_message . SpaceUsedLong ( ) ) < < " Var " ;
# if !defined(PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE)
str = unknown_fields - > AddLengthDelimited ( 1 ) ;
shadow_vector . Add ( ) ;
EXPECT_EQ ( total ( ) , empty_message . SpaceUsedLong ( ) ) < < " Str " ;
str - > assign ( sizeof ( std : : string ) + 1 , ' x ' ) ;
EXPECT_EQ ( total ( ) , empty_message . SpaceUsedLong ( ) ) < < " Str2 " ;
# else
std : : string fake_str ( 31 , ' a ' ) ;
str = & fake_str ;
unknown_fields - > AddLengthDelimited ( 1 , fake_str ) ;
shadow_vector . Add ( ) ;
EXPECT_EQ ( total ( ) , empty_message . SpaceUsedLong ( ) ) < < " Str2 " ;
# endif // PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE
group = unknown_fields - > AddGroup ( 1 ) ;
shadow_vector . Add ( ) ;
@ -751,7 +783,14 @@ TEST_F(UnknownFieldSetTest, SerializeToString) {
field_set . AddVarint ( 1 , - 1 ) ;
field_set . AddVarint ( 2 , - 2 ) ;
field_set . AddLengthDelimited ( 44 , " str " ) ;
field_set . AddLengthDelimited ( 44 , " byv " ) ;
field_set . AddLengthDelimited ( 44 , std : : string ( " byv " ) ) ;
field_set . AddLengthDelimited ( 44 ,
absl : : Cord ( " this came from cord and is long " ) ) ;
memcpy ( internal : : UnknownFieldSetTestPeer : : AddLengthDelimitedUninitialized (
field_set , 44 , 10 )
. data ( ) ,
" 0123456789 " , 10 ) ;
field_set . AddFixed32 ( 7 , 7 ) ;
field_set . AddFixed64 ( 8 , 8 ) ;
@ -760,10 +799,8 @@ TEST_F(UnknownFieldSetTest, SerializeToString) {
group_field_set = field_set . AddGroup ( 46 ) ;
group_field_set - > AddVarint ( 47 , 2048 ) ;
unittest : : TestAllTypes message ;
std : : string serialized_message ;
ASSERT_TRUE ( field_set . SerializeToString ( & serialized_message ) ) ;
ASSERT_TRUE ( message . ParseFromString ( serialized_message ) ) ;
unittest : : TestAllTypes message =
UnknownToProto < unittest : : TestAllTypes > ( field_set ) ;
EXPECT_EQ ( message . optional_int32 ( ) , - 1 ) ;
EXPECT_EQ ( message . optional_int64 ( ) , - 2 ) ;
@ -771,8 +808,9 @@ TEST_F(UnknownFieldSetTest, SerializeToString) {
EXPECT_EQ ( message . optional_uint64 ( ) , 4 ) ;
EXPECT_EQ ( message . optional_fixed32 ( ) , 7 ) ;
EXPECT_EQ ( message . optional_fixed64 ( ) , 8 ) ;
EXPECT_EQ ( message . repeated_string ( 0 ) , " str " ) ;
EXPECT_EQ ( message . repeated_string ( 1 ) , " byv " ) ;
EXPECT_THAT ( message . repeated_string ( ) ,
ElementsAre ( " str " , " byv " , " this came from cord and is long " ,
" 0123456789 " ) ) ;
EXPECT_EQ ( message . repeatedgroup ( 0 ) . a ( ) , 1024 ) ;
EXPECT_EQ ( message . repeatedgroup ( 1 ) . a ( ) , 2048 ) ;
}
@ -818,6 +856,42 @@ TEST_F(UnknownFieldSetTest, SerializeToCord_TestPackedTypes) {
EXPECT_THAT ( message . packed_uint64 ( ) , ElementsAre ( 5 , 6 , 7 ) ) ;
}
TEST ( UnknownFieldTest , SettersOverrideTheDataProperly ) {
using T = unittest : : TestAllTypes ;
UnknownFieldSet set ;
set . AddVarint ( T : : kOptionalInt32FieldNumber , 2 ) ;
set . AddFixed32 ( T : : kOptionalFixed32FieldNumber , 3 ) ;
set . AddFixed64 ( T : : kOptionalFixed64FieldNumber , 4 ) ;
set . AddLengthDelimited ( T : : kOptionalStringFieldNumber , " 5 " ) ;
T message = UnknownToProto < T > ( set ) ;
EXPECT_EQ ( message . optional_int32 ( ) , 2 ) ;
EXPECT_EQ ( message . optional_fixed32 ( ) , 3 ) ;
EXPECT_EQ ( message . optional_fixed64 ( ) , 4 ) ;
EXPECT_EQ ( message . optional_string ( ) , " 5 " ) ;
set . mutable_field ( 0 ) - > set_varint ( 22 ) ;
set . mutable_field ( 1 ) - > set_fixed32 ( 33 ) ;
set . mutable_field ( 2 ) - > set_fixed64 ( 44 ) ;
set . mutable_field ( 3 ) - > set_length_delimited ( " 55 " ) ;
message = UnknownToProto < T > ( set ) ;
EXPECT_EQ ( message . optional_int32 ( ) , 22 ) ;
EXPECT_EQ ( message . optional_fixed32 ( ) , 33 ) ;
EXPECT_EQ ( message . optional_fixed64 ( ) , 44 ) ;
EXPECT_EQ ( message . optional_string ( ) , " 55 " ) ;
set . mutable_field ( 3 ) - > set_length_delimited ( std : : string ( " 555 " ) ) ;
message = UnknownToProto < T > ( set ) ;
EXPECT_EQ ( message . optional_string ( ) , " 555 " ) ;
set . mutable_field ( 3 ) - > set_length_delimited ( absl : : Cord ( " 5555 " ) ) ;
message = UnknownToProto < T > ( set ) ;
EXPECT_EQ ( message . optional_string ( ) , " 5555 " ) ;
}
} // namespace
} // namespace protobuf
} // namespace google