Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: mojo/public/rust/tests/util/mod.rs

Issue 2213563002: Rust: Add encoder/decoder tests (Closed) Base URL: git@github.com:domokit/mojo.git@decoding
Patch Set: Test with changes Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/rust/tests/encoding.rs ('k') | mojo/public/rust/tests/util/mojom_validation.rs » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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::{CStr, CString};
10 use std::os::raw::c_char;
11 use std::slice;
12 use std::ptr;
13
7 /// This macro sets up tests by adding in Mojo embedder 14 /// This macro sets up tests by adding in Mojo embedder
8 /// initialization. 15 /// initialization.
9 macro_rules! tests { 16 macro_rules! tests {
10 ( $( $( #[ $attr:meta ] )* fn $i:ident() $b:block)* ) => { 17 ( $( $( #[ $attr:meta ] )* fn $i:ident() $b:block)* ) => {
11 use std::sync::{Once, ONCE_INIT}; 18 use std::sync::{Once, ONCE_INIT};
12 static START: Once = ONCE_INIT; 19 static START: Once = ONCE_INIT;
13 $( 20 $(
14 #[test] 21 #[test]
15 $( 22 $(
16 #[ $attr ] 23 #[ $attr ]
17 )* 24 )*
18 fn $i() { 25 fn $i() {
19 START.call_once(|| unsafe { 26 START.call_once(|| unsafe {
20 util::InitializeMojoEmbedder(); 27 util::InitializeMojoEmbedder();
21 }); 28 });
22 $b 29 $b
23 } 30 }
24 )* 31 )*
25 } 32 }
26 } 33 }
27 34
28 #[link(name = "stdc++")] 35 #[link(name = "stdc++")]
29 extern "C" {} 36 extern "C" {}
30 37
38 #[link(name = "c")]
39 extern "C" {
40 fn free(ptr: *mut u8);
41 }
42
31 #[link(name = "rust_embedder")] 43 #[link(name = "rust_embedder")]
32 extern "C" { 44 extern "C" {
33 pub fn InitializeMojoEmbedder(); 45 pub fn InitializeMojoEmbedder();
34 } 46 }
35 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() {
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 }
OLDNEW
« no previous file with comments | « mojo/public/rust/tests/encoding.rs ('k') | mojo/public/rust/tests/util/mojom_validation.rs » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698