1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 use googletest::prelude::*; 9 use protobuf::prelude::*; 10 use protobuf::View; 11 12 use edition_unittest_rust_proto::TestAllTypes as TestAllTypesEditions; 13 use paste::paste; 14 use unittest_proto3_optional_rust_proto::TestProto3Optional; 15 use unittest_proto3_rust_proto::TestAllTypes as TestAllTypesProto3; 16 use unittest_rust_proto::TestAllTypes as TestAllTypesProto2; 17 18 macro_rules! generate_parameterized_serialization_test { 19 ($(($type: ident, $name_ext: ident)),*) => { 20 paste! { $( 21 #[gtest] 22 fn [< serialization_zero_length_ $name_ext >]() { 23 let mut msg = [< $type >]::new(); 24 25 let serialized = msg.serialize().unwrap(); 26 assert_that!(serialized.len(), eq(0)); 27 28 let serialized = msg.as_view().serialize().unwrap(); 29 assert_that!(serialized.len(), eq(0)); 30 31 let serialized = msg.as_mut().serialize().unwrap(); 32 assert_that!(serialized.len(), eq(0)); 33 } 34 35 #[gtest] 36 fn [< serialize_default_view $name_ext>]() { 37 let default = View::<[< $type >]>::default(); 38 assert_that!(default.serialize().unwrap().len(), eq(0)); 39 } 40 41 #[gtest] 42 fn [< serialize_deserialize_message_ $name_ext>]() { 43 let mut msg = [< $type >]::new(); 44 msg.set_optional_int64(42); 45 msg.set_optional_bool(true); 46 msg.set_optional_bytes(b"serialize deserialize test"); 47 48 let serialized = msg.serialize().unwrap(); 49 50 let msg2 = [< $type >]::parse(&serialized).unwrap(); 51 assert_that!(msg.optional_int64(), eq(msg2.optional_int64())); 52 assert_that!(msg.optional_bool(), eq(msg2.optional_bool())); 53 assert_that!(msg.optional_bytes(), eq(msg2.optional_bytes())); 54 } 55 56 #[gtest] 57 fn [< deserialize_empty_ $name_ext>]() { 58 assert!([< $type >]::parse(&[]).is_ok()); 59 } 60 61 #[gtest] 62 fn [< deserialize_error_ $name_ext>]() { 63 assert!([< $type >]::parse(b"not a serialized proto").is_err()); 64 } 65 66 #[gtest] 67 fn [< set_bytes_with_serialized_data_ $name_ext>]() { 68 let mut msg = [< $type >]::new(); 69 msg.set_optional_int64(42); 70 msg.set_optional_bool(true); 71 let mut msg2 = [< $type >]::new(); 72 msg2.set_optional_bytes(msg.serialize().unwrap()); 73 assert_that!(msg2.optional_bytes(), eq(msg.serialize().unwrap())); 74 } 75 76 #[gtest] 77 fn [< deserialize_on_previously_allocated_message_ $name_ext>]() { 78 let mut msg = [< $type >]::new(); 79 msg.set_optional_int64(42); 80 msg.set_optional_bool(true); 81 msg.set_optional_bytes(b"serialize deserialize test"); 82 83 let serialized = msg.serialize().unwrap(); 84 85 let mut msg2 = Box::new([< $type >]::new()); 86 assert!(msg2.clear_and_parse(&serialized).is_ok()); 87 assert_that!(msg.optional_int64(), eq(msg2.optional_int64())); 88 assert_that!(msg.optional_bool(), eq(msg2.optional_bool())); 89 assert_that!(msg.optional_bytes(), eq(msg2.optional_bytes())); 90 } 91 92 )* } 93 }; 94 } 95 96 generate_parameterized_serialization_test!( 97 (TestAllTypesProto2, proto2), 98 (TestAllTypesProto3, proto3), 99 (TestAllTypesEditions, editions), 100 (TestProto3Optional, proto3_optional) 101 ); 102 103 macro_rules! generate_parameterized_int32_byte_size_test { 104 ($(($type: ident, $name_ext: ident)),*) => { 105 paste! { $( 106 107 #[gtest] 108 fn [< test_int32_byte_size_ $name_ext>]() { 109 let args = vec![(0, 1), (127, 1), (128, 2), (-1, 10)]; 110 for arg in args { 111 let value = arg.0; 112 let expected_value_size = arg.1; 113 let mut msg = [< $type >]::new(); 114 // tag for optional_int32 only takes 1 byte 115 msg.set_optional_int32(value); 116 let serialized = msg.serialize().unwrap(); 117 // 1 byte for tag and n from expected_value_size 118 assert_that!(serialized.len(), eq(expected_value_size + 1), "Test failed. Value: {value}. Expected_value_size: {expected_value_size}."); 119 } 120 121 } 122 )* } 123 }; 124 } 125 126 generate_parameterized_int32_byte_size_test!( 127 (TestAllTypesProto2, proto2), 128 (TestProto3Optional, proto3_optional), /* Test would fail if we were to use 129 * TestAllTypesProto3: optional_int32 follows "no 130 * presence" semantics and setting it to 0 (default 131 * value) will cause it to not be serialized */ 132 (TestAllTypesEditions, editions) 133 ); 134