1 // Copyright 2019 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use protobuf::Message;
6 
7 /// Asserts that a given protobuf message object can be serialized to bytes and
8 /// read back into a value of the same type, preserving equality between the
9 /// original object and the one read back.
10 ///
11 /// This is helpful for confirming that proto files have been compiled
12 /// successfully and are exposed as intended by the public API of the parent
13 /// crate. It also lets us exercise the full set of methods we intend to use for
14 /// building particular proto objects so that we notice breakage early in the
15 /// event that a proto external to this repository would make a breaking change.
16 ///
17 /// Note: assumes that the given `message` is not just `T::new` so that we can
18 /// tell that deserialization has happened successfully.
19 ///
20 /// # Example
21 ///
22 /// ```ignore
23 /// let mut request = SendCommandRequest::new();
24 /// request.set_command(b"...".to_vec());
25 /// test_round_trip(request);
26 /// ```
test_round_trip<T: Message + PartialEq>(message: T)27 pub fn test_round_trip<T: Message + PartialEq>(message: T) {
28     let serialized = message.write_to_bytes().unwrap();
29 
30     let mut back = T::new();
31     assert_ne!(message, back);
32 
33     back.merge_from_bytes(&serialized).unwrap();
34     assert_eq!(message, back);
35 }
36