Add serialization test and parameterize tests to verify behavior in multiple versions of the protocol buffer language.

New serialization test will verify the amount of bytes it takes to serialize a message with a field of type INT32 set to different values.

PiperOrigin-RevId: 641044189
pull/17021/head
Protobuf Team Bot 10 months ago committed by Copybara-Service
parent 851ca21f59
commit 7dcae81096
  1. 16
      rust/test/BUILD
  2. 12
      rust/test/shared/BUILD
  3. 106
      rust/test/shared/serialization_test.rs

@ -10,6 +10,8 @@ UNITTEST_PROTO3_TARGET = "//src/google/protobuf:test_protos"
UNITTEST_PROTO3_OPTIONAL_TARGET = "//src/google/protobuf:test_protos" UNITTEST_PROTO3_OPTIONAL_TARGET = "//src/google/protobuf:test_protos"
UNITTEST_EDITION_TARGET = "//src/google/protobuf:test_protos"
rust_upb_proto_library( rust_upb_proto_library(
name = "unittest_upb_rust_proto", name = "unittest_upb_rust_proto",
testonly = True, testonly = True,
@ -55,6 +57,20 @@ rust_upb_proto_library(
deps = [UNITTEST_PROTO3_OPTIONAL_TARGET], deps = [UNITTEST_PROTO3_OPTIONAL_TARGET],
) )
rust_cc_proto_library(
name = "unittest_edition_cpp_rust_proto",
testonly = True,
visibility = ["//rust/test/shared:__subpackages__"],
deps = [UNITTEST_EDITION_TARGET],
)
rust_upb_proto_library(
name = "unittest_edition_upb_rust_proto",
testonly = True,
visibility = ["//rust/test/shared:__subpackages__"],
deps = [UNITTEST_EDITION_TARGET],
)
proto_library( proto_library(
name = "parent_proto", name = "parent_proto",
srcs = ["parent.proto"], srcs = ["parent.proto"],

@ -227,7 +227,13 @@ rust_test(
rust_test( rust_test(
name = "serialization_upb_test", name = "serialization_upb_test",
srcs = ["serialization_test.rs"], srcs = ["serialization_test.rs"],
proc_macro_deps = [
"@crate_index//:paste",
],
deps = [ deps = [
"//rust/test:unittest_edition_upb_rust_proto",
"//rust/test:unittest_proto3_optional_upb_rust_proto",
"//rust/test:unittest_proto3_upb_rust_proto",
"//rust/test:unittest_upb_rust_proto", "//rust/test:unittest_upb_rust_proto",
"@crate_index//:googletest", "@crate_index//:googletest",
], ],
@ -236,8 +242,14 @@ rust_test(
rust_test( rust_test(
name = "serialization_cpp_test", name = "serialization_cpp_test",
srcs = ["serialization_test.rs"], srcs = ["serialization_test.rs"],
proc_macro_deps = [
"@crate_index//:paste",
],
deps = [ deps = [
"//rust/test:unittest_cpp_rust_proto", "//rust/test:unittest_cpp_rust_proto",
"//rust/test:unittest_edition_upb_rust_proto",
"//rust/test:unittest_proto3_cpp_rust_proto",
"//rust/test:unittest_proto3_optional_cpp_rust_proto",
"@crate_index//:googletest", "@crate_index//:googletest",
], ],
) )

@ -6,11 +6,18 @@
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
use googletest::prelude::*; use googletest::prelude::*;
use unittest_rust_proto::TestAllTypes; use paste::paste;
use unittest_edition_rust_proto::TestAllTypes as TestAllTypesEditions;
use unittest_proto3_optional_rust_proto::TestProto3Optional;
use unittest_proto3_rust_proto::TestAllTypes as TestAllTypesProto3;
use unittest_rust_proto::TestAllTypes as TestAllTypesProto2;
#[test] macro_rules! generate_parameterized_serialization_test {
fn serialize_zero_length() { ($(($type: ident, $name_ext: ident)),*) => {
let mut msg = TestAllTypes::new(); paste! { $(
#[test]
fn [< serialization_zero_length_ $name_ext >]() {
let mut msg = [< $type >]::new();
let serialized = msg.serialize().unwrap(); let serialized = msg.serialize().unwrap();
assert_that!(serialized.len(), eq(0)); assert_that!(serialized.len(), eq(0));
@ -20,55 +27,98 @@ fn serialize_zero_length() {
let serialized = msg.as_mut().serialize().unwrap(); let serialized = msg.as_mut().serialize().unwrap();
assert_that!(serialized.len(), eq(0)); assert_that!(serialized.len(), eq(0));
} }
#[test] #[test]
fn serialize_deserialize_message() { fn [< serialize_deserialize_message_ $name_ext>]() {
let mut msg = TestAllTypes::new(); let mut msg = [< $type >]::new();
msg.set_optional_int64(42); msg.set_optional_int64(42);
msg.set_optional_bool(true); msg.set_optional_bool(true);
msg.set_optional_bytes(b"serialize deserialize test"); msg.set_optional_bytes(b"serialize deserialize test");
let serialized = msg.serialize().unwrap(); let serialized = msg.serialize().unwrap();
let msg2 = TestAllTypes::parse(&serialized).unwrap(); let msg2 = [< $type >]::parse(&serialized).unwrap();
assert_that!(msg.optional_int64(), eq(msg2.optional_int64())); assert_that!(msg.optional_int64(), eq(msg2.optional_int64()));
assert_that!(msg.optional_bool(), eq(msg2.optional_bool())); assert_that!(msg.optional_bool(), eq(msg2.optional_bool()));
assert_that!(msg.optional_bytes(), eq(msg2.optional_bytes())); assert_that!(msg.optional_bytes(), eq(msg2.optional_bytes()));
} }
#[test] #[test]
fn deserialize_empty() { fn [< deserialize_empty_ $name_ext>]() {
assert!(TestAllTypes::parse(&[]).is_ok()); assert!([< $type >]::parse(&[]).is_ok());
} }
#[test] #[test]
fn deserialize_error() { fn [< deserialize_error_ $name_ext>]() {
assert!(TestAllTypes::parse(b"not a serialized proto").is_err()); assert!([< $type >]::parse(b"not a serialized proto").is_err());
} }
#[test] #[test]
fn set_bytes_with_serialized_data() { fn [< set_bytes_with_serialized_data_ $name_ext>]() {
let mut msg = TestAllTypes::new(); let mut msg = [< $type >]::new();
msg.set_optional_int64(42); msg.set_optional_int64(42);
msg.set_optional_bool(true); msg.set_optional_bool(true);
let mut msg2 = TestAllTypes::new(); let mut msg2 = [< $type >]::new();
msg2.set_optional_bytes(msg.serialize().unwrap()); msg2.set_optional_bytes(msg.serialize().unwrap());
assert_that!(msg2.optional_bytes(), eq(msg.serialize().unwrap().as_ref())); assert_that!(msg2.optional_bytes(), eq(msg.serialize().unwrap().as_ref()));
} }
#[test] #[test]
fn deserialize_on_previously_allocated_message() { fn [< deserialize_on_previously_allocated_message_ $name_ext>]() {
let mut msg = TestAllTypes::new(); let mut msg = [< $type >]::new();
msg.set_optional_int64(42); msg.set_optional_int64(42);
msg.set_optional_bool(true); msg.set_optional_bool(true);
msg.set_optional_bytes(b"serialize deserialize test"); msg.set_optional_bytes(b"serialize deserialize test");
let serialized = msg.serialize().unwrap(); let serialized = msg.serialize().unwrap();
let mut msg2 = Box::new(TestAllTypes::new()); let mut msg2 = Box::new([< $type >]::new());
assert!(msg2.clear_and_parse(&serialized).is_ok()); assert!(msg2.clear_and_parse(&serialized).is_ok());
assert_that!(msg.optional_int64(), eq(msg2.optional_int64())); assert_that!(msg.optional_int64(), eq(msg2.optional_int64()));
assert_that!(msg.optional_bool(), eq(msg2.optional_bool())); assert_that!(msg.optional_bool(), eq(msg2.optional_bool()));
assert_that!(msg.optional_bytes(), eq(msg2.optional_bytes())); assert_that!(msg.optional_bytes(), eq(msg2.optional_bytes()));
} }
)* }
};
}
generate_parameterized_serialization_test!(
(TestAllTypesProto2, proto2),
(TestAllTypesProto3, proto3),
(TestAllTypesEditions, editions),
(TestProto3Optional, proto3_optional)
);
macro_rules! generate_parameterized_int32_byte_size_test {
($(($type: ident, $name_ext: ident)),*) => {
paste! { $(
#[test]
fn [< test_int32_byte_size_ $name_ext>]() {
let args = vec![(0, 1), (127, 1), (128, 2), (-1, 10)];
for arg in args {
let value = arg.0;
let expected_value_size = arg.1;
let mut msg = [< $type >]::new();
// tag for optional_int32 only takes 1 byte
msg.set_optional_int32(value);
let serialized = msg.serialize().unwrap();
// 1 byte for tag and n from expected_value_size
assert_that!(serialized.len(), eq(expected_value_size + 1), "Test failed. Value: {value}. Expected_value_size: {expected_value_size}.");
}
}
)* }
};
}
generate_parameterized_int32_byte_size_test!(
(TestAllTypesProto2, proto2),
(TestProto3Optional, proto3_optional), /* Test would fail if we were to use
* TestAllTypesProto3: optional_int32 follows "no
* presence" semantics and setting it to 0 (default
* value) will cause it to not be serialized */
(TestAllTypesEditions, editions)
);

Loading…
Cancel
Save