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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/rust/tests/util/mod.rs
diff --git a/mojo/public/rust/tests/util/mod.rs b/mojo/public/rust/tests/util/mod.rs
index 8464d5834789bef8062de85c5c9e5bfb0de98992..e36b8ac393f6f099e2c0632b896340ce362c96ec 100644
--- a/mojo/public/rust/tests/util/mod.rs
+++ b/mojo/public/rust/tests/util/mod.rs
@@ -4,6 +4,13 @@
//! This module contains useful functions and macros for testing.
+pub mod mojom_validation;
+
+use std::ffi::{CStr, CString};
+use std::os::raw::c_char;
+use std::slice;
+use std::ptr;
+
/// This macro sets up tests by adding in Mojo embedder
/// initialization.
macro_rules! tests {
@@ -28,8 +35,61 @@ macro_rules! tests {
#[link(name = "stdc++")]
extern "C" {}
+#[link(name = "c")]
+extern "C" {
+ fn free(ptr: *mut u8);
+}
+
#[link(name = "rust_embedder")]
extern "C" {
pub fn InitializeMojoEmbedder();
}
+#[link(name = "validation_parser")]
+extern "C" {
+ #[allow(dead_code)]
+ fn ParseValidationTest(input: *const c_char,
+ num_handles: *mut usize,
+ data: *mut *mut u8,
+ data_len: *mut usize)
+ -> *mut c_char;
+}
+
+#[allow(dead_code)]
+pub fn parse_validation_test(input: &str) -> Result<(Vec<u8>, usize), String> {
+ let input_c = CString::new(input.to_string()).unwrap();
+ let mut num_handles: usize = 0;
+ let mut data: *mut u8 = ptr::null_mut();
+ let mut data_len: usize = 0;
+ let error = unsafe {
+ ParseValidationTest(input_c.as_ptr(),
+ &mut num_handles as *mut usize,
+ &mut data as *mut *mut u8,
+ &mut data_len as *mut usize)
+ };
+ if error == ptr::null_mut() {
+ if data == ptr::null_mut() {
+ // We assume we were just given an empty file
+ Ok((Vec::new(), 0))
+ } else {
+ // Make a copy of the buffer
+ let buffer;
+ unsafe {
+ buffer = slice::from_raw_parts(data, data_len).to_vec();
+ free(data);
+ }
+ Ok((buffer, num_handles))
+ }
+ } else {
+ let err_str;
+ unsafe {
+ // Copy the error string
+ err_str = CStr::from_ptr(error)
+ .to_str()
+ .expect("Could not convert error message to UTF-8!")
+ .to_owned();
+ free(error as *mut u8);
+ }
+ Err(err_str)
+ }
+}
« 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