DemesneGH commented on code in PR #209:
URL: 
https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/209#discussion_r2200101997


##########
optee-utee/src/object/persistent_object.rs:
##########
@@ -529,26 +484,186 @@ impl PersistentObject {
     }
 }
 
-impl ObjHandle for PersistentObject {
+impl GenericObject for PersistentObject {
     fn handle(&self) -> raw::TEE_ObjectHandle {
         self.0.handle()
     }
 }
 
-impl Drop for PersistentObject {
-    /// Close an opened [PersistentObject](PersistentObject).
-    ///
-    /// # Panics
-    ///
-    /// 1) If object is not a valid opened object.
-    /// 2) If the Implementation detects any other error associated with this 
function which is not
-    ///    explicitly associated with a defined return code for this function.
-    fn drop(&mut self) {
-        unsafe {
-            if self.0.raw != Box::into_raw(Box::new(ptr::null_mut())) {
-                raw::TEE_CloseObject(self.0.handle());
-            }
-            drop(Box::from_raw(self.0.raw));
+#[cfg(test)]
+mod tests {
+    extern crate std;
+
+    use std::sync::Arc;
+
+    use optee_utee_mock::{
+        object::{set_global_object_mock, MockObjectController, 
SERIAL_TEST_LOCK},
+        raw,
+    };
+
+    use super::*;
+
+    #[test]
+    // If a persistent object is successfully created, TEE_CloseObject will be
+    // called when it is dropped.
+    fn test_create_and_drop() {
+        let _lock = SERIAL_TEST_LOCK.lock();
+
+        let mut mock = MockObjectController::new();
+        let mut value: raw::__TEE_ObjectHandle = unsafe { core::mem::zeroed() 
};
+        let handle: Arc<raw::TEE_ObjectHandle> = Arc::new(&mut value);
+        {
+            let handle = handle.clone();
+            mock.expect_TEE_CreatePersistentObject()
+                .return_once_st(
+                    move |_, _, _, _, _, _, _, obj: *mut 
raw::TEE_ObjectHandle| {
+                        unsafe {
+                            *obj = *handle;
+                        }
+                        raw::TEE_SUCCESS
+                    },
+                )
+                .once();
+        }
+

Review Comment:
   How about defining macros:
   ```
   macro_rules! expect_create_success {
       ($mock:expr, $handle:expr) => {{
           let h = $handle.clone();
           $mock.expect_TEE_CreatePersistentObject()
               .return_once_st(move |_, _, _, _, _, _, _, obj| {
                   unsafe {
                       *obj = *h.get();
                   }
                   raw::TEE_SUCCESS
               })
               .once();
       }};
   }
   macro_rules! expect_create_fail {
       ($mock:expr, $code:expr) => {{
           $mock.expect_TEE_CreatePersistentObject()
               .return_once_st(move |_, _, _, _, _, _, _, _| $code)
               .once();
       }};
   }
   ...
   ```
   In testcases:
   ```
   #[test]
   fn test_create_and_successfully_close_delete() {
       let _lock = SERIAL_TEST_LOCK.lock();
   
       let mut mock = MockObjectController::new();
       let handle = 
std::sync::Arc::new(std::cell::UnsafeCell::new(core::ptr::null_mut()));
   
       expect_create_success!(mock, handle);
       expect_close_and_delete_success!(mock, handle);
   
       set_global_object_mock(mock);
   
       let obj = PersistentObject::create(
           ObjectStorageConstants::Private,
           &[],
           DataFlag::ACCESS_WRITE,
           None,
           &[],
       )
       .expect("it should be ok");
   
       obj.close_and_delete().expect("it should be ok");
   }
   ```
   If it makes sense, how about defining the macros in `optee-utee-mock` crate? 
This would keep test cases cleaner, and ensure the basic mock expectations 
remain with the mock implementation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org
For additional commands, e-mail: dev-h...@teaclave.apache.org

Reply via email to