Skip to content

Commit

Permalink
more tests for array trait unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Borcherding committed Mar 5, 2024
1 parent 6086d52 commit 3c55634
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
6 changes: 4 additions & 2 deletions rustbus/src/tests/fdpassing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const TEST_STRING: &str = "This will be sent over the fd\n";

#[test]
fn test_fd_passing() {
let mut con1 =
connection::rpc_conn::RpcConn::system_conn(connection::Timeout::Infinite).unwrap();
let Ok(mut con1) = connection::rpc_conn::RpcConn::system_conn(connection::Timeout::Infinite)
else {
return;
};
let mut con2 =
connection::rpc_conn::RpcConn::system_conn(connection::Timeout::Infinite).unwrap();
con1.send_message(&mut crate::standard_messages::hello())
Expand Down
27 changes: 25 additions & 2 deletions rustbus/src/wire/marshal/traits/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<E1: Marshal, E2: Marshal, E3: Marshal, E4: Marshal, E5: Marshal> Marshal

impl<E: Marshal> Marshal for Vec<E> {
fn marshal(&self, ctx: &mut MarshalContext) -> Result<(), MarshalError> {
self.as_slice().marshal(ctx)
<&[E] as Marshal>::marshal(&self.as_slice(), ctx)
}
}

Expand Down Expand Up @@ -270,7 +270,30 @@ impl<E: Signature> Signature for [E] {
}
impl<E: Marshal> Marshal for [E] {
fn marshal(&self, ctx: &mut MarshalContext) -> Result<(), MarshalError> {
(&self).marshal(ctx)
<&[E] as Marshal>::marshal(&self, ctx)
}
}

impl<E: Signature, const N: usize> Signature for [E; N] {
#[inline]
fn signature() -> crate::signature::Type {
<[E]>::signature()
}
#[inline]
fn alignment() -> usize {
<[E]>::alignment()
}
#[inline]
fn sig_str(s_buf: &mut SignatureBuffer) {
<[E]>::sig_str(s_buf)
}
fn has_sig(sig: &str) -> bool {
<[E]>::has_sig(sig)
}
}
impl<E: Marshal, const N: usize> Marshal for [E; N] {
fn marshal(&self, ctx: &mut MarshalContext) -> Result<(), MarshalError> {
<&[E] as Marshal>::marshal(&self.as_slice(), ctx)
}
}

Expand Down
18 changes: 15 additions & 3 deletions rustbus/src/wire/unmarshal/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ mod test {
x(arg);
}

pub fn roundtrip<'a, T>(original: T, fds: &'a mut Vec<UnixFd>, buf: &'a mut Vec<u8>)
fn roundtrip<'a, T>(original: T, fds: &'a mut Vec<UnixFd>, buf: &'a mut Vec<u8>)
where
T: Unmarshal<'a, 'a>,
T: Marshal,
Expand All @@ -223,8 +223,8 @@ mod test {

original
.marshal(&mut MarshalContext {
buf: buf,
fds: fds,
buf,
fds,
byteorder,
})
.unwrap();
Expand Down Expand Up @@ -277,6 +277,18 @@ mod test {
original.insert(2u64, "fgh");
roundtrip(original, &mut fds, &mut buf);

let mut original = std::collections::HashMap::new();
original.insert(0u8, "abc");
original.insert(1u8, "dce");
original.insert(2u8, "fgh");
roundtrip(original, &mut fds, &mut buf);

let mut original = std::collections::HashMap::new();
original.insert(0i16, "abc");
original.insert(1i16, "dce");
original.insert(2i16, "fgh");
roundtrip(original, &mut fds, &mut buf);

let orig = (30u8, true, 100u8, -123i32);
roundtrip(orig, &mut fds, &mut buf);

Expand Down
19 changes: 19 additions & 0 deletions rustbus/src/wire/unmarshal/traits/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,23 @@ mod tests {
.unwrap();
assert_eq!(variant.get::<u8>().unwrap(), 42);
}

#[test]
fn array() {
let mut m = MarshalledMessageBody::new();
m.push_param([0u8, 1, 2, 3, 4, 5]).unwrap(); // Array by value
m.push_param(0u8).unwrap();
m.push_param(-10i16).unwrap();
m.push_param(&[0u8, 1, 2, 3, 4, 5, 6]).unwrap(); // Array as ref
m.push_param(-2000i16).unwrap();
m.push_param(&[0u8, 1, 2, 3, 4, 5, 6, 7][..]).unwrap(); // Slice

let mut parser = m.parser();
assert_eq!(parser.get::<&[u8]>().unwrap(), &[0, 1, 2, 3, 4, 5]);
assert_eq!(parser.get::<u8>().unwrap(), 0);
assert_eq!(parser.get::<i16>().unwrap(), -10);
assert_eq!(parser.get::<&[u8]>().unwrap(), &[0, 1, 2, 3, 4, 5, 6]);
assert_eq!(parser.get::<i16>().unwrap(), -2000);
assert_eq!(parser.get::<&[u8]>().unwrap(), &[0, 1, 2, 3, 4, 5, 6, 7]);
}
}

0 comments on commit 3c55634

Please sign in to comment.