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
diff --git a/trusty/libtrusty-rs/Android.bp b/trusty/libtrusty-rs/Android.bp
index 0c6349a..47c64b7 100644
--- a/trusty/libtrusty-rs/Android.bp
+++ b/trusty/libtrusty-rs/Android.bp
@@ -26,3 +26,12 @@
"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 fe65a27..b3102f8 100644
--- a/trusty/libtrusty-rs/src/lib.rs
+++ b/trusty/libtrusty-rs/src/lib.rs
@@ -128,15 +128,15 @@
/// 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<usize> {
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 0000000..a6f1370
--- /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");
+}