1 use trusty::{TipcChannel, DEFAULT_DEVICE};
2
3 const ECHO_NAME: &str = "com.android.ipc-unittest.srv.echo";
4
5 #[test]
recv_no_alloc()6 fn recv_no_alloc() {
7 let mut connection = TipcChannel::connect(DEFAULT_DEVICE, ECHO_NAME)
8 .expect("Failed to connect to Trusty service");
9
10 // Send a message to the echo TA.
11 let send_buf = [7u8; 32];
12 connection.send(send_buf.as_slice()).unwrap();
13
14 // Receive the response message from the TA. The response message will be the
15 // same as the message we just sent.
16 let mut recv_buf = [0u8; 32];
17 let read_len = connection.recv_no_alloc(recv_buf.as_mut_slice()).unwrap();
18
19 assert_eq!(
20 send_buf.len(),
21 read_len,
22 "Received data was wrong size (expected {} bytes, received {})",
23 send_buf.len(),
24 read_len,
25 );
26 assert_eq!(send_buf, recv_buf, "Received data does not match sent data");
27 }
28
29 #[test]
recv_small_buf()30 fn recv_small_buf() {
31 let mut connection = TipcChannel::connect(DEFAULT_DEVICE, ECHO_NAME)
32 .expect("Failed to connect to Trusty service");
33
34 // Send a long message to the echo service so that we can test receiving a long
35 // message.
36 let send_buf = [7u8; 2048];
37 connection.send(send_buf.as_slice()).unwrap();
38
39 // Attempt to receive the response message with a buffer that is too small to
40 // contain the message.
41 let mut recv_buf = [0u8; 32];
42 let err = connection.recv_no_alloc(recv_buf.as_mut_slice()).unwrap_err();
43
44 assert_eq!(
45 Some(libc::EMSGSIZE),
46 err.raw_os_error(),
47 "Unexpected error err when receiving incoming message: {:?}",
48 err,
49 );
50 }
51
52 #[test]
recv_empty_vec()53 fn recv_empty_vec() {
54 let mut connection = TipcChannel::connect(DEFAULT_DEVICE, ECHO_NAME)
55 .expect("Failed to connect to Trusty service");
56
57 // Send a message to the echo TA.
58 let send_buf = [7u8; 2048];
59 connection.send(send_buf.as_slice()).unwrap();
60
61 // Receive the response message. `recv_buf` is initially empty, and `recv` is
62 // responsible for allocating enough space to hold the message.
63 let mut recv_buf = Vec::new();
64 connection.recv(&mut recv_buf).unwrap();
65
66 assert_eq!(send_buf.as_slice(), recv_buf, "Received data does not match sent data");
67 }
68
69 #[test]
recv_vec_existing_capacity()70 fn recv_vec_existing_capacity() {
71 let mut connection = TipcChannel::connect(DEFAULT_DEVICE, ECHO_NAME)
72 .expect("Failed to connect to Trusty service");
73
74 // Send a message to the echo TA.
75 let send_buf = [7u8; 2048];
76 connection.send(send_buf.as_slice()).unwrap();
77
78 // Receive the response message into a buffer that already has enough capacity
79 // to hold the message. No additional capacity should be allocated when
80 // receiving the message.
81 let mut recv_buf = Vec::with_capacity(2048);
82 connection.recv(&mut recv_buf).unwrap();
83
84 assert_eq!(send_buf.as_slice(), recv_buf, "Received data does not match sent data");
85 assert_eq!(2048, recv_buf.capacity(), "Additional capacity was allocated when not needed");
86 }
87