| 1 |
use alloc::ffi::CString; |
| 2 |
use core::slice; |
| 3 |
use core::{mem, ptr}; |
| 4 |
use std::ffi::OsStr; |
| 5 |
use std::io; |
| 6 |
use std::io::Write; |
| 7 |
use std::os::raw::{c_char, c_void}; |
| 8 |
use std::path::Path; |
| 9 |
|
| 10 |
use assert_matches::assert_matches; |
| 11 |
|
| 12 |
#[test] |
| 13 |
fn str_to_c_string() { |
| 14 |
assert_eq!( |
| 15 |
super::str_to_c_string("").unwrap(), |
| 16 |
CString::new("").unwrap() |
| 17 |
); |
| 18 |
|
| 19 |
assert_eq!( |
| 20 |
super::str_to_c_string("xyz").unwrap(), |
| 21 |
CString::new("xyz").unwrap() |
| 22 |
); |
| 23 |
|
| 24 |
super::str_to_c_string("abc\0xyz").unwrap_err(); |
| 25 |
} |
| 26 |
|
| 27 |
#[test] |
| 28 |
fn os_str_to_c_string() { |
| 29 |
assert_eq!( |
| 30 |
super::os_str_to_c_string(OsStr::new("")).unwrap(), |
| 31 |
CString::new("").unwrap() |
| 32 |
); |
| 33 |
|
| 34 |
assert_eq!( |
| 35 |
super::os_str_to_c_string(OsStr::new("xyz")).unwrap(), |
| 36 |
CString::new("xyz").unwrap() |
| 37 |
); |
| 38 |
|
| 39 |
super::os_str_to_c_string(OsStr::new("abc\0xyz")).unwrap_err(); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn c_str_ptr_to_str() { |
| 44 |
super::c_str_ptr_to_str(ptr::null()).unwrap_err(); |
| 45 |
|
| 46 |
assert_eq!(super::c_str_ptr_to_str(c"".as_ptr()).unwrap(), ""); |
| 47 |
assert_eq!(super::c_str_ptr_to_str(c"xyz".as_ptr()).unwrap(), "xyz"); |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn c_str_ptr_to_path() { |
| 52 |
assert_eq!(super::c_str_ptr_to_path(c"".as_ptr()), Path::new("")); |
| 53 |
assert_eq!(super::c_str_ptr_to_path(c"xyz".as_ptr()), Path::new("xyz")); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn ret_val_to_result() { |
| 58 |
super::ret_val_to_result("xyz", 0).unwrap(); |
| 59 |
super::ret_val_to_result("xyz", 1).unwrap(); |
| 60 |
|
| 61 |
errno::set_errno(errno::Errno(1)); |
| 62 |
let err = super::ret_val_to_result("xyz", -1).unwrap_err(); |
| 63 |
assert_matches!(err, crate::errors::Error::IO { .. }); |
| 64 |
if let crate::errors::Error::IO { source, .. } = err { |
| 65 |
assert_eq!(source.kind(), io::ErrorKind::PermissionDenied); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn ret_val_to_result_with_path() { |
| 71 |
let test_path = Path::new("/abc"); |
| 72 |
|
| 73 |
super::ret_val_to_result_with_path("xyz", 0, test_path).unwrap(); |
| 74 |
super::ret_val_to_result_with_path("xyz", 1, test_path).unwrap(); |
| 75 |
|
| 76 |
errno::set_errno(errno::Errno(1)); |
| 77 |
let err = super::ret_val_to_result_with_path("xyz", -1, test_path).unwrap_err(); |
| 78 |
crate::errors::Error::clear_errno(); |
| 79 |
|
| 80 |
assert_matches!(err, crate::errors::Error::IO1Path { .. }); |
| 81 |
if let crate::errors::Error::IO1Path { source, path, .. } = err { |
| 82 |
assert_eq!(source.kind(), io::ErrorKind::PermissionDenied); |
| 83 |
assert_eq!(path, test_path); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn c_allocated_block() { |
| 89 |
assert!(super::CAllocatedBlock::<c_void>::new(ptr::null_mut()).is_none()); |
| 90 |
|
| 91 |
let block_ptr: *mut u64 = unsafe { libc::calloc(16, mem::size_of::<u64>()) }.cast(); |
| 92 |
let block = super::CAllocatedBlock::new(block_ptr).unwrap(); |
| 93 |
assert_eq!(block.as_ptr(), block_ptr); |
| 94 |
|
| 95 |
let block_ptr: *mut c_char = unsafe { libc::calloc(8, 1) }.cast(); |
| 96 |
{ |
| 97 |
let mut block_slice: &mut [u8] = unsafe { slice::from_raw_parts_mut(block_ptr.cast(), 8) }; |
| 98 |
write!(block_slice, "xyz\0").unwrap(); |
| 99 |
}; |
| 100 |
let mut block = super::CAllocatedBlock::new(block_ptr).unwrap(); |
| 101 |
assert_eq!(block.as_ptr(), block_ptr); |
| 102 |
assert_eq!(block.as_mut_ptr(), block_ptr); |
| 103 |
assert_eq!(block.as_c_str(), c"xyz"); |
| 104 |
let _ignored = format!("{:?}", &block); |
| 105 |
} |
| 106 |
|
| 107 |
unsafe extern "C" fn null_ptr() -> *const c_char { |
| 108 |
ptr::null() |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn get_static_path() { |
| 113 |
super::get_static_path(null_ptr, "null_ptr()").unwrap_err(); |
| 114 |
} |