|
@@ -25,6 +25,7 @@ impl ModuleTag {
|
|
/// will return `--test cmdline-option`.
|
|
/// will return `--test cmdline-option`.
|
|
pub fn cmdline(&self) -> Result<&str, Utf8Error> {
|
|
pub fn cmdline(&self) -> Result<&str, Utf8Error> {
|
|
use core::{mem, slice, str};
|
|
use core::{mem, slice, str};
|
|
|
|
+ // strlen without null byte
|
|
let strlen = self.size as usize - mem::size_of::<ModuleTag>();
|
|
let strlen = self.size as usize - mem::size_of::<ModuleTag>();
|
|
let bytes = unsafe { slice::from_raw_parts((&self.cmdline_str) as *const u8, strlen) };
|
|
let bytes = unsafe { slice::from_raw_parts((&self.cmdline_str) as *const u8, strlen) };
|
|
str::from_utf8(bytes)
|
|
str::from_utf8(bytes)
|
|
@@ -88,3 +89,39 @@ impl<'a> Debug for ModuleIter<'a> {
|
|
list.finish()
|
|
list.finish()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+#[cfg(test)]
|
|
|
|
+mod tests {
|
|
|
|
+ use crate::TagType;
|
|
|
|
+
|
|
|
|
+ const MSG: &str = "hello";
|
|
|
|
+
|
|
|
|
+ /// Returns the tag structure in bytes in native endian format.
|
|
|
|
+ fn get_bytes() -> std::vec::Vec<u8> {
|
|
|
|
+ // size is: 4 bytes for tag + 4 bytes for size + length of null-terminated string
|
|
|
|
+ // 4 bytes mod_start + 4 bytes mod_end
|
|
|
|
+ let size = (4 + 4 + 4 + 4 + MSG.as_bytes().len() + 1) as u32;
|
|
|
|
+ [
|
|
|
|
+ &((TagType::Module as u32).to_ne_bytes()),
|
|
|
|
+ &size.to_ne_bytes(),
|
|
|
|
+ &0_u32.to_ne_bytes(),
|
|
|
|
+ &0_u32.to_ne_bytes(),
|
|
|
|
+ MSG.as_bytes(),
|
|
|
|
+ // Null Byte
|
|
|
|
+ &[0],
|
|
|
|
+ ]
|
|
|
|
+ .iter()
|
|
|
|
+ .flat_map(|bytes| bytes.iter())
|
|
|
|
+ .copied()
|
|
|
|
+ .collect()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// Tests to parse a string with a terminating null byte from the tag (as the spec defines).
|
|
|
|
+ #[test]
|
|
|
|
+ fn test_parse_str() {
|
|
|
|
+ let tag = get_bytes();
|
|
|
|
+ let tag = unsafe { tag.as_ptr().cast::<super::ModuleTag>().as_ref().unwrap() };
|
|
|
|
+ assert_eq!({ tag.typ }, TagType::Module);
|
|
|
|
+ assert_eq!(tag.cmdline().expect("must be valid UTF-8"), MSG);
|
|
|
|
+ }
|
|
|
|
+}
|