You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by ms...@apache.org on 2020/02/14 23:59:56 UTC

[incubator-teaclave] branch develop updated: [common] Fix read/write buffer check with leveldb test case (#225)

This is an automated email from the ASF dual-hosted git repository.

mssun pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git


The following commit(s) were added to refs/heads/develop by this push:
     new d8264f0  [common] Fix read/write buffer check with leveldb test case (#225)
d8264f0 is described below

commit d8264f0c2c31f7f4add82b76a5a63f6198629fd8
Author: Zhaofeng Chen <zf...@apache.org>
AuthorDate: Fri Feb 14 15:59:46 2020 -0800

    [common] Fix read/write buffer check with leveldb test case (#225)
---
 common/protected_fs_rs/src/sgx_tprotected_fs.rs    |  4 +-
 tests/integration/enclave/src/rusty_leveldb_sgx.rs | 48 +++++++++++++++++++++-
 2 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/common/protected_fs_rs/src/sgx_tprotected_fs.rs b/common/protected_fs_rs/src/sgx_tprotected_fs.rs
index 1ff337c..ff81f11 100644
--- a/common/protected_fs_rs/src/sgx_tprotected_fs.rs
+++ b/common/protected_fs_rs/src/sgx_tprotected_fs.rs
@@ -108,7 +108,7 @@ unsafe fn rsgx_fopen_auto_key(filename: &CStr, mode: &CStr) -> SysResult<SGX_FIL
 }
 
 unsafe fn rsgx_fwrite(stream: SGX_FILE, buf: &[u8]) -> SysResult<usize> {
-    if stream.is_null() || buf.is_empty() {
+    if stream.is_null() {
         return Err(libc::EINVAL);
     }
 
@@ -122,7 +122,7 @@ unsafe fn rsgx_fwrite(stream: SGX_FILE, buf: &[u8]) -> SysResult<usize> {
 }
 
 unsafe fn rsgx_fread(stream: SGX_FILE, buf: &mut [u8]) -> SysResult<usize> {
-    if stream.is_null() || buf.is_empty() {
+    if stream.is_null() {
         return Err(libc::EINVAL);
     }
 
diff --git a/tests/integration/enclave/src/rusty_leveldb_sgx.rs b/tests/integration/enclave/src/rusty_leveldb_sgx.rs
index b215490..d407550 100644
--- a/tests/integration/enclave/src/rusty_leveldb_sgx.rs
+++ b/tests/integration/enclave/src/rusty_leveldb_sgx.rs
@@ -46,6 +46,27 @@ fn fill_db(db: &mut DB, entries: usize) -> Result<(), Box<dyn Error>> {
     Ok(())
 }
 
+fn fill_db_with_sequential_elements(db: &mut DB, entries: usize) -> Result<(), Box<dyn Error>> {
+    for i in 0..entries {
+        let (k, v) = (i.to_string(), i.to_string());
+        db.put(k.as_bytes(), v.as_bytes())?;
+        db.flush()?;
+    }
+    Ok(())
+}
+
+fn validate_sequential_elements(db: &mut DB, entries: usize) -> Result<(), Box<dyn Error>> {
+    for i in 0..entries {
+        let (k, v_expected) = (i.to_string(), i.to_string());
+        let v = db.get(k.as_bytes()).ok_or_else(|| {
+            error!("key: {}", k);
+            Box::new(io::Error::new(ErrorKind::NotFound, "Key not found"))
+        })?;
+        assert_eq!(&v_expected.as_bytes()[..], &v[..]);
+    }
+    Ok(())
+}
+
 fn test_write_a_lot() {
     let key = [
         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
@@ -62,8 +83,33 @@ fn test_write_a_lot() {
     fs::remove_dir_all("/tmp/leveldb_testdb").expect("Cannot remove directory");
 }
 
+fn test_write_and_reopen() {
+    let key = [
+        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
+        0x08,
+    ];
+    let elements_count = 2000;
+    let db_location = "/tmp/leveldb_testdb";
+
+    {
+        let mut opt = Options::new_disk_db_with(key);
+        opt.compression_type = CompressionType::CompressionSnappy;
+        let mut db = DB::open(&db_location, opt).unwrap();
+        fill_db_with_sequential_elements(&mut db, elements_count).unwrap();
+    }
+
+    {
+        let mut opt = Options::new_disk_db_with(key);
+        opt.compression_type = CompressionType::CompressionSnappy;
+        let mut db = DB::open(&db_location, opt).unwrap();
+        validate_sequential_elements(&mut db, 2000).unwrap();
+    }
+
+    fs::remove_dir_all("/tmp/leveldb_testdb").expect("Cannot remove directory");
+}
+
 pub fn run_tests() -> bool {
     use teaclave_test_utils::*;
 
-    run_tests!(test_write_a_lot)
+    run_tests!(test_write_a_lot, test_write_and_reopen,)
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org