From 90eb6f2344c188b43f463bd7ee845fb5649d4eb8 Mon Sep 17 00:00:00 2001 From: Moritz Borcherding Date: Fri, 1 Mar 2024 17:54:54 +0100 Subject: [PATCH] start cleaning up unmarshal tests --- rustbus/src/connection/ll_conn.rs | 2 +- rustbus/src/wire/unmarshal/traits.rs | 153 +++++++----------- rustbus/src/wire/unmarshal/traits/base.rs | 4 +- .../src/wire/unmarshal/traits/container.rs | 4 +- 4 files changed, 58 insertions(+), 105 deletions(-) diff --git a/rustbus/src/connection/ll_conn.rs b/rustbus/src/connection/ll_conn.rs index f2b867e..08e779a 100644 --- a/rustbus/src/connection/ll_conn.rs +++ b/rustbus/src/connection/ll_conn.rs @@ -238,7 +238,7 @@ impl RecvConn { } _ => { // TODO what to do? - eprintln!("Cmsg other than ScmRights: {:?}", cmsg); + // eprintln!("Cmsg other than ScmRights: {:?}", cmsg); } } } diff --git a/rustbus/src/wire/unmarshal/traits.rs b/rustbus/src/wire/unmarshal/traits.rs index 34ed83d..50717ad 100644 --- a/rustbus/src/wire/unmarshal/traits.rs +++ b/rustbus/src/wire/unmarshal/traits.rs @@ -140,15 +140,19 @@ pub fn unmarshal<'buf, 'fds, T: Unmarshal<'buf, 'fds>>( #[cfg(test)] mod test { + use std::fmt::Debug; + use super::unmarshal; use super::Unmarshal; use super::UnmarshalContext; use super::Variant; use crate::wire::marshal::MarshalContext; + use crate::wire::UnixFd; use crate::ByteOrder; use crate::Marshal; use crate::Signature; + // TODO this is more of a doc test? #[test] fn test_generic_unmarshal() { let mut fds = Vec::new(); @@ -206,36 +210,44 @@ mod test { x(arg); } + pub fn roundtrip<'a, T>(original: T, fds: &'a mut Vec, buf: &'a mut Vec) + where + T: Unmarshal<'a, 'a>, + T: Marshal, + T: Debug, + T: Eq, + { + fds.clear(); + buf.clear(); + let byteorder = ByteOrder::LittleEndian; + + original + .marshal(&mut MarshalContext { + buf: buf, + fds: fds, + byteorder, + }) + .unwrap(); + + let unmarshalled = + T::unmarshal(&mut UnmarshalContext::new(fds, byteorder, buf, 0)).unwrap(); + eprintln!("{fds:?}"); + assert_eq!(original, unmarshalled); + } + #[test] fn test_unmarshal_byte_array() { - use crate::wire::marshal::MarshalContext; - use crate::Marshal; + let mut fds = vec![]; + let mut buf = vec![]; let mut orig = vec![]; for x in 0..1024 { orig.push((x % 255) as u8); } + roundtrip(orig, &mut fds, &mut buf); - let mut fds = Vec::new(); - let mut buf = Vec::new(); - let mut ctx = MarshalContext { - buf: &mut buf, - fds: &mut fds, - byteorder: ByteOrder::LittleEndian, - }; - let ctx = &mut ctx; - - orig.marshal(ctx).unwrap(); - assert_eq!(&ctx.buf[..4], &[0, 4, 0, 0]); - assert_eq!(ctx.buf.len(), 1028); - let unorig = <&[u8] as Unmarshal>::unmarshal(&mut UnmarshalContext::new( - ctx.fds, - ctx.byteorder, - ctx.buf, - 0, - )) - .unwrap(); - assert_eq!(orig, unorig); + assert_eq!(&buf[..4], &[0, 4, 0, 0]); + assert_eq!(buf.len(), 1028); // even slices of slices of u8 work efficiently let mut orig1 = vec![]; @@ -248,106 +260,49 @@ mod test { } let orig = vec![orig1.as_slice(), orig2.as_slice()]; - - ctx.buf.clear(); - orig.marshal(ctx).unwrap(); - - // unorig[x] points into the appropriate region in buf, and unorigs lifetime is bound to buf - let unorig = as Unmarshal>::unmarshal(&mut UnmarshalContext::new( - ctx.fds, - ctx.byteorder, - ctx.buf, - 0, - )) - .unwrap(); - assert_eq!(orig, unorig); + roundtrip(orig, &mut fds, &mut buf); } #[test] fn test_unmarshal_traits() { - use crate::wire::marshal::MarshalContext; - use crate::Marshal; - - let mut fds = Vec::new(); - let mut buf = Vec::new(); - let mut ctx = MarshalContext { - buf: &mut buf, - fds: &mut fds, - byteorder: ByteOrder::LittleEndian, - }; - let ctx = &mut ctx; - - let original = &["a", "b"]; - original.marshal(ctx).unwrap(); - - let v = Vec::<&str>::unmarshal(&mut UnmarshalContext::new( - ctx.fds, - ctx.byteorder, - ctx.buf, - 0, - )) - .unwrap(); + let mut fds = vec![]; + let mut buf = vec![]; - assert_eq!(original, v.as_slice()); - - ctx.buf.clear(); + let original = vec!["a", "b"]; + roundtrip(original, &mut fds, &mut buf); let mut original = std::collections::HashMap::new(); original.insert(0u64, "abc"); original.insert(1u64, "dce"); original.insert(2u64, "fgh"); - - original.marshal(ctx).unwrap(); - - let map = std::collections::HashMap::::unmarshal(&mut UnmarshalContext::new( - ctx.fds, - ctx.byteorder, - ctx.buf, - 0, - )) - .unwrap(); - assert_eq!(original, map); - - ctx.buf.clear(); + roundtrip(original, &mut fds, &mut buf); let orig = (30u8, true, 100u8, -123i32); - orig.marshal(ctx).unwrap(); - type ST = (u8, bool, u8, i32); - let s = ST::unmarshal(&mut UnmarshalContext::new( - ctx.fds, - ctx.byteorder, - ctx.buf, - 0, - )) - .unwrap(); - assert_eq!(orig, s); + roundtrip(orig, &mut fds, &mut buf); - ctx.buf.clear(); - - use crate::wire::UnixFd; use crate::wire::{ObjectPath, SignatureWrapper}; - let orig_fd = UnixFd::new(nix::unistd::dup(1).unwrap()); let orig = ( + 1u8, ObjectPath::new("/a/b/c").unwrap(), SignatureWrapper::new("ss(aiau)").unwrap(), - &orig_fd, + 0u32, ); - orig.marshal(ctx).unwrap(); + roundtrip(orig, &mut fds, &mut buf); assert_eq!( - ctx.buf, + &buf, &[ - 6, 0, 0, 0, b'/', b'a', b'/', b'b', b'/', b'c', 0, 8, b's', b's', b'(', b'a', b'i', - b'a', b'u', b')', 0, 0, 0, 0, 0, 0, 0, 0 + 1, 0, 0, 0, 6, 0, 0, 0, b'/', b'a', b'/', b'b', b'/', b'c', 0, 8, b's', b's', b'(', + b'a', b'i', b'a', b'u', b')', 0, 0, 0, 0, 0, 0, 0, 0 ] ); - let (p, s, _fd) = - <(ObjectPath, SignatureWrapper<&str>, UnixFd) as Unmarshal>::unmarshal( - &mut UnmarshalContext::new(ctx.fds, ctx.byteorder, ctx.buf, 0), - ) - .unwrap(); - assert_eq!(p.as_ref(), "/a/b/c"); - assert_eq!(s.as_ref(), "ss(aiau)"); + let orig = ( + 1u8, + ObjectPath::new("/a/b/c").unwrap(), + 1u8, + 0xFFFFFFFFFFFFFFFFu64, + ); + roundtrip(orig, &mut fds, &mut buf); } #[test] diff --git a/rustbus/src/wire/unmarshal/traits/base.rs b/rustbus/src/wire/unmarshal/traits/base.rs index 8185080..470d57d 100644 --- a/rustbus/src/wire/unmarshal/traits/base.rs +++ b/rustbus/src/wire/unmarshal/traits/base.rs @@ -62,8 +62,8 @@ impl<'buf, 'fds> Unmarshal<'buf, 'fds> for f64 { } } -impl<'buf, 'fds> Unmarshal<'buf, 'fds> for &'buf str { - fn unmarshal(ctx: &mut UnmarshalContext<'fds, 'buf>) -> unmarshal::UnmarshalResult { +impl<'buf> Unmarshal<'buf, '_> for &'buf str { + fn unmarshal(ctx: &mut UnmarshalContext<'_, 'buf>) -> unmarshal::UnmarshalResult { ctx.read_str() } } diff --git a/rustbus/src/wire/unmarshal/traits/container.rs b/rustbus/src/wire/unmarshal/traits/container.rs index 3ef82d1..0ef462c 100644 --- a/rustbus/src/wire/unmarshal/traits/container.rs +++ b/rustbus/src/wire/unmarshal/traits/container.rs @@ -296,15 +296,13 @@ mod tests { use crate::message_builder::MarshalledMessageBody; #[test] - fn todo_name() { + fn variant_with_sig() { let mut m = MarshalledMessageBody::new(); m.push_param("test.interface").unwrap(); m.push_param("test_property").unwrap(); m.push_param(crate::wire::marshal::traits::Variant(42u8)) .unwrap(); - eprintln!("Buffer: {:?}", m.get_buf()); - let mut parser = m.parser(); assert_eq!(parser.get::<&str>().unwrap(), "test.interface"); assert_eq!(parser.get::<&str>().unwrap(), "test_property");