| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 //! This module contains useful functions and macros for testing. | 5 //! This module contains useful functions and macros for testing. |
| 6 | 6 |
| 7 pub mod mojom_validation; |
| 8 |
| 9 use std::ffi::CString; |
| 10 use std::os::raw::c_char; |
| 11 use std::ptr; |
| 12 |
| 7 /// This macro sets up tests by adding in Mojo embedder | 13 /// This macro sets up tests by adding in Mojo embedder |
| 8 /// initialization. | 14 /// initialization. |
| 9 macro_rules! tests { | 15 macro_rules! tests { |
| 10 ( $( $( #[ $attr:meta ] )* fn $i:ident() $b:block)* ) => { | 16 ( $( $( #[ $attr:meta ] )* fn $i:ident() $b:block)* ) => { |
| 11 use std::sync::{Once, ONCE_INIT}; | 17 use std::sync::{Once, ONCE_INIT}; |
| 12 static START: Once = ONCE_INIT; | 18 static START: Once = ONCE_INIT; |
| 13 $( | 19 $( |
| 14 #[test] | 20 #[test] |
| 15 $( | 21 $( |
| 16 #[ $attr ] | 22 #[ $attr ] |
| 17 )* | 23 )* |
| 18 fn $i() { | 24 fn $i() { |
| 19 START.call_once(|| unsafe { | 25 START.call_once(|| unsafe { |
| 20 util::InitializeMojoEmbedder(); | 26 util::InitializeMojoEmbedder(); |
| 21 }); | 27 }); |
| 22 $b | 28 $b |
| 23 } | 29 } |
| 24 )* | 30 )* |
| 25 } | 31 } |
| 26 } | 32 } |
| 27 | 33 |
| 28 #[link(name = "stdc++")] | 34 #[link(name = "stdc++")] |
| 29 extern "C" {} | 35 extern "C" {} |
| 30 | 36 |
| 31 #[link(name = "rust_embedder")] | 37 #[link(name = "rust_embedder")] |
| 32 extern "C" { | 38 extern "C" { |
| 33 pub fn InitializeMojoEmbedder(); | 39 pub fn InitializeMojoEmbedder(); |
| 34 } | 40 } |
| 35 | 41 |
| 42 #[link(name = "validation_parser")] |
| 43 extern "C" { |
| 44 #[allow(dead_code)] |
| 45 fn ParseValidationTest(input: *const c_char, |
| 46 num_handles: *mut usize, |
| 47 data: *mut *mut u8, |
| 48 data_len: *mut usize) |
| 49 -> *mut c_char; |
| 50 } |
| 51 |
| 52 #[allow(dead_code)] |
| 53 pub fn parse_validation_test(input: &str) -> Result<(Vec<u8>, usize), String> { |
| 54 let input_c = CString::new(input.to_string()).unwrap(); |
| 55 let mut num_handles: usize = 0; |
| 56 let mut data: *mut u8 = ptr::null_mut(); |
| 57 let mut data_len: usize = 0; |
| 58 let error = unsafe { |
| 59 ParseValidationTest(input_c.as_ptr(), |
| 60 &mut num_handles as *mut usize, |
| 61 &mut data as *mut *mut u8, |
| 62 &mut data_len as *mut usize) |
| 63 }; |
| 64 if error == ptr::null_mut() { |
| 65 if data == ptr::null_mut() { |
| 66 // We assume we were just given an empty file |
| 67 Ok((Vec::new(), 0)) |
| 68 } else { |
| 69 let buffer = unsafe { |
| 70 Vec::from_raw_parts(data, data_len, data_len) |
| 71 }; |
| 72 Ok((buffer, num_handles)) |
| 73 } |
| 74 } else { |
| 75 Err(unsafe { |
| 76 CString::from_raw(error) |
| 77 }.into_string().expect("Could not convert error message to UTF-8!")) |
| 78 } |
| 79 } |
| OLD | NEW |