| 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 //! This module contains useful functions and macros for testing. | |
| 6 | |
| 7 pub mod mojom_validation; | |
| 8 | |
| 9 use std::ffi::{CStr, CString}; | |
| 10 use std::os::raw::c_char; | |
| 11 use std::slice; | |
| 12 use std::ptr; | |
| 13 | |
| 14 /// This macro sets up tests by adding in Mojo embedder | |
| 15 /// initialization. | |
| 16 macro_rules! tests { | |
| 17 ( $( $( #[ $attr:meta ] )* fn $i:ident() $b:block)* ) => { | |
| 18 use std::sync::{Once, ONCE_INIT}; | |
| 19 static START: Once = ONCE_INIT; | |
| 20 $( | |
| 21 #[test] | |
| 22 $( | |
| 23 #[ $attr ] | |
| 24 )* | |
| 25 fn $i() { | |
| 26 START.call_once(|| unsafe { | |
| 27 util::InitializeMojoEmbedder(); | |
| 28 }); | |
| 29 $b | |
| 30 } | |
| 31 )* | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 #[link(name = "stdc++")] | |
| 36 extern "C" {} | |
| 37 | |
| 38 #[link(name = "c")] | |
| 39 extern "C" { | |
| 40 fn free(ptr: *mut u8); | |
| 41 } | |
| 42 | |
| 43 #[link(name = "rust_embedder")] | |
| 44 extern "C" { | |
| 45 pub fn InitializeMojoEmbedder(); | |
| 46 } | |
| 47 | |
| 48 #[link(name = "validation_parser")] | |
| 49 extern "C" { | |
| 50 #[allow(dead_code)] | |
| 51 fn ParseValidationTest(input: *const c_char, | |
| 52 num_handles: *mut usize, | |
| 53 data: *mut *mut u8, | |
| 54 data_len: *mut usize) | |
| 55 -> *mut c_char; | |
| 56 } | |
| 57 | |
| 58 #[allow(dead_code)] | |
| 59 pub fn parse_validation_test(input: &str) -> Result<(Vec<u8>, usize), String> { | |
| 60 let input_c = CString::new(input.to_string()).unwrap(); | |
| 61 let mut num_handles: usize = 0; | |
| 62 let mut data: *mut u8 = ptr::null_mut(); | |
| 63 let mut data_len: usize = 0; | |
| 64 let error = unsafe { | |
| 65 ParseValidationTest(input_c.as_ptr(), | |
| 66 &mut num_handles as *mut usize, | |
| 67 &mut data as *mut *mut u8, | |
| 68 &mut data_len as *mut usize) | |
| 69 }; | |
| 70 if error == ptr::null_mut() { | |
| 71 if data == ptr::null_mut() || data_len == 0 { | |
| 72 // We assume we were just given an empty file | |
| 73 Ok((Vec::new(), 0)) | |
| 74 } else { | |
| 75 // Make a copy of the buffer | |
| 76 let buffer; | |
| 77 unsafe { | |
| 78 buffer = slice::from_raw_parts(data, data_len).to_vec(); | |
| 79 free(data); | |
| 80 } | |
| 81 Ok((buffer, num_handles)) | |
| 82 } | |
| 83 } else { | |
| 84 let err_str; | |
| 85 unsafe { | |
| 86 // Copy the error string | |
| 87 err_str = CStr::from_ptr(error) | |
| 88 .to_str() | |
| 89 .expect("Could not convert error message to UTF-8!") | |
| 90 .to_owned(); | |
| 91 free(error as *mut u8); | |
| 92 } | |
| 93 Err(err_str) | |
| 94 } | |
| 95 } | |
| OLD | NEW |