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_cpp::prelude::*;
10
11 use protobuf_cpp::__runtime::PtrAndLen;
12 use protobuf_cpp::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop};
13 use std::ffi::c_void;
14 use unittest_rust_proto::{TestAllExtensions, TestAllTypes, TestAllTypesMut, TestAllTypesView};
15
16 macro_rules! proto_assert_eq {
17 ($lhs:expr, $rhs:expr) => {{
18 let lhs = &$lhs;
19 let rhs = &$rhs;
20
21 assert_that!(lhs.optional_int64(), eq(rhs.optional_int64()));
22 assert_that!(lhs.optional_bytes(), eq(rhs.optional_bytes()));
23 assert_that!(lhs.optional_bool(), eq(rhs.optional_bool()));
24 }};
25 }
26
27 // Helper functions invoking C++ Protobuf APIs directly in C++.
28 // Defined in `test_utils.cc`.
29 extern "C" {
TakeOwnershipAndGetOptionalInt32(msg: *mut c_void) -> i3230 fn TakeOwnershipAndGetOptionalInt32(msg: *mut c_void) -> i32;
DeserializeTestAllTypes(data: *const u8, len: usize) -> *mut c_void31 fn DeserializeTestAllTypes(data: *const u8, len: usize) -> *mut c_void;
MutateTestAllTypes(msg: *mut c_void)32 fn MutateTestAllTypes(msg: *mut c_void);
SerializeTestAllTypes(msg: *const c_void) -> protobuf_cpp::__runtime::SerializedData33 fn SerializeTestAllTypes(msg: *const c_void) -> protobuf_cpp::__runtime::SerializedData;
DeleteTestAllTypes(msg: *mut c_void)34 fn DeleteTestAllTypes(msg: *mut c_void);
35
NewWithExtension() -> *mut c_void36 fn NewWithExtension() -> *mut c_void;
GetBytesExtension(msg: *const c_void) -> PtrAndLen37 fn GetBytesExtension(msg: *const c_void) -> PtrAndLen;
38
GetConstStaticTestAllTypes() -> *const c_void39 fn GetConstStaticTestAllTypes() -> *const c_void;
40 }
41
42 #[gtest]
send_to_cpp()43 fn send_to_cpp() {
44 let mut msg1 = TestAllTypes::new();
45 msg1.set_optional_int32(7);
46 let i = unsafe { TakeOwnershipAndGetOptionalInt32(msg1.__unstable_leak_raw_message()) };
47 assert_eq!(i, 7);
48 }
49
50 #[gtest]
mutate_message_mut_in_cpp()51 fn mutate_message_mut_in_cpp() {
52 let mut msg1 = TestAllTypes::new();
53 unsafe {
54 MutateTestAllTypes(msg1.as_mut().__unstable_as_raw_message_mut());
55 }
56
57 let mut msg2 = TestAllTypes::new();
58 msg2.set_optional_int64(42);
59 msg2.set_optional_bytes(b"something mysterious");
60 msg2.set_optional_bool(false);
61
62 proto_assert_eq!(msg1, msg2);
63 }
64
65 #[gtest]
deserialize_in_rust()66 fn deserialize_in_rust() {
67 let mut msg1 = TestAllTypes::new();
68 msg1.set_optional_int64(-1);
69 msg1.set_optional_bytes(b"some cool data I guess");
70 let serialized = unsafe { SerializeTestAllTypes(msg1.as_view().__unstable_as_raw_message()) };
71
72 let msg2 = TestAllTypes::parse(&serialized).unwrap();
73 proto_assert_eq!(msg1, msg2);
74 }
75
76 #[gtest]
deserialize_in_cpp()77 fn deserialize_in_cpp() {
78 let mut msg1 = TestAllTypes::new();
79 msg1.set_optional_int64(-1);
80 msg1.set_optional_bytes(b"some cool data I guess");
81 let data = msg1.serialize().unwrap();
82
83 let msg2 = unsafe {
84 TestAllTypes::__unstable_take_ownership_of_raw_message(DeserializeTestAllTypes(
85 (*data).as_ptr(),
86 data.len(),
87 ))
88 };
89
90 proto_assert_eq!(msg1, msg2);
91 }
92
93 #[gtest]
deserialize_in_cpp_into_mut()94 fn deserialize_in_cpp_into_mut() {
95 let mut msg1 = TestAllTypes::new();
96 msg1.set_optional_int64(-1);
97 msg1.set_optional_bytes(b"some cool data I guess");
98 let data = msg1.serialize().unwrap();
99
100 let mut raw_msg = unsafe { DeserializeTestAllTypes((*data).as_ptr(), data.len()) };
101 let msg2 = unsafe { TestAllTypesMut::__unstable_wrap_raw_message_mut(&mut raw_msg) };
102
103 proto_assert_eq!(msg1, msg2);
104
105 // The C++ still owns the message here and needs to delete it.
106 unsafe {
107 DeleteTestAllTypes(raw_msg);
108 }
109 }
110
111 #[gtest]
deserialize_in_cpp_into_view()112 fn deserialize_in_cpp_into_view() {
113 let mut msg1 = TestAllTypes::new();
114 msg1.set_optional_int64(-1);
115 msg1.set_optional_bytes(b"some cool data I guess");
116 let data = msg1.serialize().unwrap();
117
118 let raw_msg = unsafe { DeserializeTestAllTypes((*data).as_ptr(), data.len()) };
119 let const_msg = raw_msg as *const _;
120 let msg2 = unsafe { TestAllTypesView::__unstable_wrap_raw_message(&const_msg) };
121
122 proto_assert_eq!(msg1, msg2);
123
124 // The C++ still owns the message here and needs to delete it.
125 unsafe {
126 DeleteTestAllTypes(raw_msg);
127 }
128 }
129
130 // This test ensures that random fields we (Rust) don't know about don't
131 // accidentally get destroyed by Rust.
132 #[gtest]
smuggle_extension()133 fn smuggle_extension() {
134 let msg1 =
135 unsafe { TestAllExtensions::__unstable_take_ownership_of_raw_message(NewWithExtension()) };
136 let data = msg1.serialize().unwrap();
137
138 let mut msg2 = TestAllExtensions::parse(&data).unwrap();
139 let bytes =
140 unsafe { GetBytesExtension(msg2.as_mut().__unstable_as_raw_message_mut()).as_ref() };
141 assert_eq!(bytes, b"smuggled");
142 }
143
144 #[gtest]
view_of_const_static()145 fn view_of_const_static() {
146 let view: TestAllTypesView<'static> = unsafe {
147 TestAllTypesView::__unstable_wrap_raw_message_unchecked_lifetime(
148 GetConstStaticTestAllTypes(),
149 )
150 };
151 assert_eq!(view.optional_int64(), 0);
152 assert_eq!(view.default_int32(), 41);
153 }
154