OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium 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 //! Tests validation functionality in the bindings package | |
6 //! | |
7 //! Test failure is defined as the function returning via panicking | |
8 //! and the result being caught in the test! macro. If a test function | |
9 //! returns without panicking, it is assumed to pass. | |
10 | |
11 #[macro_use] | |
12 extern crate mojo; | |
13 | |
14 use mojo::bindings::decoding::{Decoder, ValidationError}; | |
15 use mojo::bindings::encoding; | |
16 use mojo::bindings::encoding::{Encoder, DataHeaderValue, Context}; | |
17 use mojo::bindings::mojom::{MojomEncodable, MojomPointer, MojomStruct}; | |
18 use mojo::system; | |
19 use mojo::system::UntypedHandle; | |
20 | |
21 #[macro_use] | |
22 mod util; | |
23 | |
24 const STRUCT_A_VERSIONS: [(u32, u32); 1] = [(0, 16)]; | |
25 | |
26 struct StructA<T: MojomEncodable> { | |
27 param0: [T; 3], | |
28 } | |
29 | |
30 impl<T: MojomEncodable> MojomPointer for StructA<T> { | |
31 fn header_data(&self) -> DataHeaderValue { | |
32 DataHeaderValue::Version(0) | |
33 } | |
34 fn serialized_size(&self, _context: &Context) -> usize { | |
35 16 | |
36 } | |
37 fn encode_value(self, encoder: &mut Encoder, context: Context) { | |
38 MojomEncodable::encode(self.param0, encoder, context.clone()); | |
39 } | |
40 fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, Val
idationError> { | |
41 let _version = { | |
42 let mut state = decoder.get_mut(&context); | |
43 match state.decode_struct_header(&STRUCT_A_VERSIONS) { | |
44 Ok(header) => header.data(), | |
45 Err(err) => return Err(err), | |
46 } | |
47 }; | |
48 let param0 = match <[T; 3]>::decode(decoder, context.clone()) { | |
49 Ok(value) => value, | |
50 Err(err) => return Err(err), | |
51 }; | |
52 Ok(StructA { param0: param0 }) | |
53 } | |
54 } | |
55 | |
56 impl<T: MojomEncodable> MojomEncodable for StructA<T> { | |
57 impl_encodable_for_pointer!(); | |
58 fn compute_size(&self, context: Context) -> usize { | |
59 encoding::align_default(self.serialized_size(&context)) + | |
60 self.param0.compute_size(context.clone()) | |
61 } | |
62 } | |
63 | |
64 impl<T: MojomEncodable> MojomStruct for StructA<T> {} | |
65 | |
66 tests! { | |
67 // Fixed size arrays have complex and unsafe semantics to ensure | |
68 // there are no memory leaks. We test this behavior here to make | |
69 // sure memory isn't becoming corrupted. | |
70 fn regression_fixed_size_array_error_propagates_safely() { | |
71 let handle1 = unsafe { system::acquire(0) }; | |
72 let handle2 = unsafe { system::acquire(0) }; | |
73 let handle3 = unsafe { system::acquire(0) }; | |
74 let val = StructA { | |
75 param0: [handle1, handle2, handle3], | |
76 }; | |
77 let (mut buffer, mut handles) = val.auto_serialize(); | |
78 handles.truncate(1); | |
79 let new_val = <StructA<UntypedHandle>>::deserialize(&mut buffer[..], han
dles); | |
80 match new_val { | |
81 Ok(_) => panic!("Value should not be okay!"), | |
82 Err(err) => assert_eq!(err, ValidationError::IllegalHandle), | |
83 } | |
84 } | |
85 | |
86 // Same as the above test, but verifies that drop() is called. | |
87 // For the only handle that should drop, we make the handle some | |
88 // random number which is potentially a valid handle. When on | |
89 // drop() we try to close it, we should panic. | |
90 #[should_panic] | |
91 fn regression_fixed_size_array_verify_drop() { | |
92 let handle1 = unsafe { system::acquire(42) }; | |
93 let handle2 = unsafe { system::acquire(0) }; | |
94 let handle3 = unsafe { system::acquire(0) }; | |
95 let val = StructA { | |
96 param0: [handle1, handle2, handle3], | |
97 }; | |
98 let (mut buffer, mut handles) = val.auto_serialize(); | |
99 handles.truncate(1); | |
100 let new_val = <StructA<UntypedHandle>>::deserialize(&mut buffer[..], han
dles); | |
101 match new_val { | |
102 Ok(_) => panic!("Value should not be okay!"), | |
103 Err(err) => assert_eq!(err, ValidationError::IllegalHandle), | |
104 } | |
105 } | |
106 } | |
107 | |
OLD | NEW |