1 #![feature(cfg_version)]
2 // C string literals were stabilized in Rust 1.77
3 #![cfg_attr(not(version("1.77")), feature(c_str_literals))]
4 
5 #[cfg(test)]
6 mod unittests;
7 
8 #[cfg(test)]
9 mod tests {
10     use crate::define_tests_for;
11     use android_hardware_security_see_storage::aidl::android::hardware::security::see::storage::{
12         FileAvailability::FileAvailability, FileIntegrity::FileIntegrity,
13         FileProperties::FileProperties, ISecureStorage::ISecureStorage,
14         IStorageSession::IStorageSession,
15     };
16     use binder::{Status, StatusCode, Strong};
17     use core::ffi::CStr;
18     use rpcbinder::RpcSession;
19     use test::assert_ok;
20 
21     //This line is needed in order to run the unit tests in Trusty
22     test::init!();
23 
connect() -> Result<Strong<dyn ISecureStorage>, StatusCode>24     fn connect() -> Result<Strong<dyn ISecureStorage>, StatusCode> {
25         const STORAGE_AIDL_PORT_NAME: &CStr = c"com.android.hardware.security.see.storage";
26 
27         RpcSession::new().setup_trusty_client(STORAGE_AIDL_PORT_NAME)
28     }
29 
start_session(properties: &FileProperties) -> Result<Strong<dyn IStorageSession>, Status>30     fn start_session(properties: &FileProperties) -> Result<Strong<dyn IStorageSession>, Status> {
31         connect()?.startSession(properties)
32     }
33 
34     #[test]
ping()35     fn ping() {
36         use binder::IBinder as _;
37 
38         let secure_storage = assert_ok!(connect());
39         assert_ok!(secure_storage.as_binder().ping_binder());
40     }
41 
42     const TP: &'static FileProperties = &FileProperties {
43         integrity: FileIntegrity::TAMPER_PROOF_AT_REST,
44         availability: FileAvailability::AFTER_USERDATA,
45         persistent: false,
46     };
47     const TDEA: &'static FileProperties = &FileProperties {
48         integrity: FileIntegrity::TAMPER_DETECT,
49         availability: FileAvailability::BEFORE_USERDATA,
50         persistent: false,
51     };
52     #[cfg(feature = "has_ns")]
53     const TDP: &'static FileProperties = &FileProperties {
54         integrity: FileIntegrity::TAMPER_DETECT,
55         availability: FileAvailability::AFTER_USERDATA,
56         persistent: true,
57     };
58     #[cfg(feature = "has_ns")]
59     const TD: &'static FileProperties = &FileProperties {
60         integrity: FileIntegrity::TAMPER_DETECT,
61         availability: FileAvailability::AFTER_USERDATA,
62         persistent: false,
63     };
64 
65     define_tests_for!(tp, TP);
66     define_tests_for!(tdea, TDEA);
67     #[cfg(feature = "has_ns")]
68     define_tests_for!(tdp, TDP);
69     #[cfg(feature = "has_ns")]
70     define_tests_for!(td, TD);
71 
72     #[macro_export]
73     macro_rules! define_tests_for {
74         ($mod_name:ident, $file_properties:ident) => {
75             mod $mod_name {
76                 use super::$file_properties;
77                 use test::assert_ok;
78                 use $crate::{tests::start_session, unittests};
79 
80                 #[test]
81                 fn create_delete() {
82                     let ss = assert_ok!(start_session($file_properties));
83                     unittests::create_delete(&*ss);
84                 }
85                 #[test]
86                 fn create_move_delete() {
87                     let ss = assert_ok!(start_session($file_properties));
88                     unittests::create_move_delete(&*ss);
89                 }
90                 #[test]
91                 fn file_list() {
92                     let ss = assert_ok!(start_session($file_properties));
93                     unittests::file_list(&*ss);
94                 }
95                 #[test]
96                 fn write_read_sequential() {
97                     let ss = assert_ok!(start_session($file_properties));
98                     unittests::write_read_sequential(&*ss);
99                 }
100             }
101         };
102     }
103 }
104