File: main.rs

package info (click to toggle)
protobuf 3.25.4-4
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 45,992 kB
  • sloc: cpp: 204,199; java: 87,622; ansic: 81,204; objc: 58,434; cs: 27,303; python: 22,799; php: 11,340; ruby: 8,637; pascal: 3,324; xml: 2,333; sh: 1,331; makefile: 538; lisp: 86; awk: 17
file content (94 lines) | stat: -rw-r--r-- 2,994 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

use protobuf_cpp::__internal::PtrAndLen;
use protobuf_cpp::__internal::RawMessage;
use unittest_proto::proto2_unittest::TestAllExtensions;
use unittest_proto::proto2_unittest::TestAllTypes;

macro_rules! proto_assert_eq {
    ($lhs:expr, $rhs:expr) => {{
        let lhs = &$lhs;
        let rhs = &$rhs;

        assert_eq!(lhs.optional_int64(), rhs.optional_int64());
        assert_eq!(lhs.optional_bytes(), rhs.optional_bytes());
        assert_eq!(lhs.optional_bool(), rhs.optional_bool());
    }};
}

// Helper functions invoking C++ Protobuf APIs directly in C++.
// Defined in `test_utils.cc`.
extern "C" {
    fn DeserializeTestAllTypes(data: *const u8, len: usize) -> RawMessage;
    fn MutateTestAllTypes(msg: RawMessage);
    fn SerializeTestAllTypes(msg: RawMessage) -> protobuf_cpp::__runtime::SerializedData;

    fn NewWithExtension() -> RawMessage;
    fn GetBytesExtension(msg: RawMessage) -> PtrAndLen;
}

#[test]
fn mutate_message_in_cpp() {
    let mut msg1 = TestAllTypes::new();
    unsafe {
        MutateTestAllTypes(msg1.__unstable_cpp_repr_grant_permission_to_break());
    }

    let mut msg2 = TestAllTypes::new();
    msg2.optional_int64_set(Some(42));
    msg2.optional_bytes_mut().set(b"something mysterious");
    msg2.optional_bool_set(Some(false));

    proto_assert_eq!(msg1, msg2);
}

#[test]
fn deserialize_in_rust() {
    let mut msg1 = TestAllTypes::new();
    msg1.optional_int64_set(Some(-1));
    msg1.optional_bytes_mut().set(b"some cool data I guess");
    let serialized =
        unsafe { SerializeTestAllTypes(msg1.__unstable_cpp_repr_grant_permission_to_break()) };

    let mut msg2 = TestAllTypes::new();
    msg2.deserialize(&serialized).unwrap();
    proto_assert_eq!(msg1, msg2);
}

#[test]
fn deserialize_in_cpp() {
    let mut msg1 = TestAllTypes::new();
    msg1.optional_int64_set(Some(-1));
    msg1.optional_bytes_mut().set(b"some cool data I guess");
    let data = msg1.serialize();

    let msg2 = unsafe {
        TestAllTypes::__unstable_wrap_cpp_grant_permission_to_break(DeserializeTestAllTypes(
            (*data).as_ptr(),
            data.len(),
        ))
    };

    proto_assert_eq!(msg1, msg2);
}

// This test ensures that random fields we (Rust) don't know about don't
// accidentally get destroyed by Rust.
#[test]
fn smuggle_extension() {
    let msg1 = unsafe {
        TestAllExtensions::__unstable_wrap_cpp_grant_permission_to_break(NewWithExtension())
    };
    let data = msg1.serialize();

    let mut msg2 = TestAllExtensions::new();
    msg2.deserialize(&data).unwrap();
    let bytes =
        unsafe { GetBytesExtension(msg2.__unstable_cpp_repr_grant_permission_to_break()).as_ref() };
    assert_eq!(&*bytes, b"smuggled");
}