From 23dedb660e09b5cac4c0660a2c9954460ceab256 Mon Sep 17 00:00:00 2001 From: David LeGare Date: Tue, 29 Mar 2022 22:36:15 +0000 Subject: [PATCH] Setup tests for libtrusty-rs Test logic is based on the tests in the original tipc-test C binary, but adapted to use the Rust unit test infrastructure to make running tests easier. Test: Ran the tests Bug: 226659377 Change-Id: I998013b2f8b304299acb09d58beb49330747802a --- trusty/libtrusty-rs/Android.bp | 9 +++++++++ trusty/libtrusty-rs/src/lib.rs | 12 ++++++------ trusty/libtrusty-rs/tests/test.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 trusty/libtrusty-rs/tests/test.rs diff --git a/trusty/libtrusty-rs/Android.bp b/trusty/libtrusty-rs/Android.bp index 0c6349a57..47c64b787 100644 --- a/trusty/libtrusty-rs/Android.bp +++ b/trusty/libtrusty-rs/Android.bp @@ -26,3 +26,12 @@ rust_library { "libnix", ], } + +rust_test { + name: "libtrusty-rs-tests", + crate_name: "trusty_test", + srcs: ["tests/test.rs"], + rustlibs: [ + "libtrusty-rs", + ] +} diff --git a/trusty/libtrusty-rs/src/lib.rs b/trusty/libtrusty-rs/src/lib.rs index fe65a27ac..b3102f861 100644 --- a/trusty/libtrusty-rs/src/lib.rs +++ b/trusty/libtrusty-rs/src/lib.rs @@ -128,15 +128,15 @@ impl TipcChannel { /// Receives a message from the connected service. /// /// Returns the number of bytes in the received message, or any error that - /// occurred when reading the message. A return value of 0 indicates that - /// there were no incoming messages to receive. + /// occurred when reading the message. Blocks until there is a message to + /// receive if none is already ready to read. /// /// # Errors /// - /// Returns an error with native error code 90 (`EMSGSIZE`) if `buf` isn't - /// large enough to contain the incoming message. Use - /// [`raw_os_error`][std::io::Error::raw_os_error] to check the error code - /// to determine if you need to increase the size of `buf`. + /// Returns an error with native error code 90 (`EMSGSIZE`) if `buf` isn't large + /// enough to contain the incoming message. Use + /// [`raw_os_error`][std::io::Error::raw_os_error] to check the error code to + /// determine if you need to increase the size of `buf`. pub fn recv(&mut self, buf: &mut [u8]) -> io::Result { self.0.read(buf) } diff --git a/trusty/libtrusty-rs/tests/test.rs b/trusty/libtrusty-rs/tests/test.rs new file mode 100644 index 000000000..a6f1370e5 --- /dev/null +++ b/trusty/libtrusty-rs/tests/test.rs @@ -0,0 +1,26 @@ +use trusty::{TipcChannel, DEFAULT_DEVICE}; + +const ECHO_NAME: &str = "com.android.ipc-unittest.srv.echo"; + +#[test] +fn echo() { + let mut connection = TipcChannel::connect(DEFAULT_DEVICE, ECHO_NAME) + .expect("Failed to connect to Trusty service"); + + // Send a message to the echo TA. + let send_buf = [7u8; 32]; + connection.send(send_buf.as_slice()).unwrap(); + + // Receive the response message from the TA. + let mut recv_buf = [0u8; 32]; + let read_len = connection.recv(&mut recv_buf).expect("Failed to read from connection"); + + assert_eq!( + send_buf.len(), + read_len, + "Received data was wrong size (expected {} bytes, received {})", + send_buf.len(), + read_len, + ); + assert_eq!(send_buf, recv_buf, "Received data does not match sent data"); +}