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
This commit is contained in:
David LeGare 2022-03-29 22:36:15 +00:00
parent a3ab0a41f2
commit 23dedb660e
3 changed files with 41 additions and 6 deletions

View file

@ -26,3 +26,12 @@ rust_library {
"libnix",
],
}
rust_test {
name: "libtrusty-rs-tests",
crate_name: "trusty_test",
srcs: ["tests/test.rs"],
rustlibs: [
"libtrusty-rs",
]
}

View file

@ -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<usize> {
self.0.read(buf)
}

View file

@ -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");
}