sccommunity / crates-sgx

crates-sgx is a monorepo of all Rust crates ported for Teaclave SGX SDK targeting Intel SGX.
https://sccommunity.github.io/crates-sgx
Apache License 2.0
12 stars 2 forks source link

add adler32-rs #11

Closed sammyne closed 4 years ago

sammyne commented 4 years ago

Add

Description

Port in adler32-rs@v1.0.4

Crate Meta

NOTICES

The original project failed to compile when upgrading to rust 2018, so this commit consists of two commits

  1. format and fix syntax errors for upgrading
  2. port to teaclave-sgx-sdk

Hence, the Diff of Changes reported below is the difference of 2 against 1.

Diff of Changes

diff --git a/crates/adler32-rs/Cargo.toml b/crates/adler32-rs/Cargo.toml
index 7e1d81b..c850f44 100644
--- a/crates/adler32-rs/Cargo.toml
+++ b/crates/adler32-rs/Cargo.toml
@@ -11,4 +11,20 @@ license = "Zlib"
 edition = "2018"

 [dev-dependencies]
-rand = "0.4"
+#rand = "0.4"
+
+[features]
+default = ["mesalock_sgx"]
+mesalock_sgx = ["sgx_tstd"]
+enclave_unit_test = ["rand", "sgx_tunittest"]
+
+[dependencies]
+sgx_tstd        = { git = "https://github.com/apache/incubator-teaclave-sgx-sdk.git", tag = "v1.1.1", optional = true }
+sgx_tunittest   = { git = "https://github.com/apache/incubator-teaclave-sgx-sdk.git", tag = "v1.1.1", optional = true }
+
+[dependencies.rand]
+package = "sgx_rand"
+git = "https://github.com/apache/incubator-teaclave-sgx-sdk.git"
+optional = true
+tag = "v1.1.1"
+
diff --git a/crates/adler32-rs/src/lib.rs b/crates/adler32-rs/src/lib.rs
index 4613c66..8185943 100644
--- a/crates/adler32-rs/src/lib.rs
+++ b/crates/adler32-rs/src/lib.rs
@@ -8,7 +8,17 @@
 //! The adler32 code has been translated (as accurately as I could manage) from
 //! the zlib implementation.

-#[cfg(test)]
+#![cfg_attr(all(feature = "mesalock_sgx", not(target_env = "sgx")), no_std)]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock"),
+    feature(rustc_private)
+)]
+#[cfg(all(feature = "mesalock_sgx", not(target_env = "sgx")))]
+#[macro_use]
+extern crate sgx_tstd as std;
+
+#[cfg(feature = "enclave_unit_test")]
+//#[cfg(test)]
 extern crate rand;

 use std::io;
@@ -203,14 +213,23 @@ pub fn adler32<R: io::Read>(mut reader: R) -> io::Result<u32> {
     Ok(hash.hash())
 }

-#[cfg(test)]
-mod test {
+#[cfg(feature = "enclave_unit_test")]
+//#[cfg(test)]
+pub mod test {
+    use std::prelude::v1::*;
+
     use rand;
     use rand::Rng;
     use std::io;

     use super::{adler32, RollingAdler32, BASE};

+    use sgx_tunittest::*;
+
+    pub fn run_tests() {
+        rsgx_unit_tests!(testvectors, compare, rolling, long_window_remove);
+    }
+
     fn adler32_slow<R: io::Read>(reader: R) -> io::Result<u32> {
         let mut a: u32 = 1;
         let mut b: u32 = 0;
@@ -224,7 +243,7 @@ mod test {
         Ok((b << 16) | a)
     }

-    #[test]
+    //#[test]
     fn testvectors() {
         fn do_test(v: u32, bytes: &[u8]) {
             let mut hash = RollingAdler32::new();
@@ -253,7 +272,7 @@ mod test {
         do_test(0xD6251498, &[255; 64000]);
     }

-    #[test]
+    //#[test]
     fn compare() {
         let mut rng = rand::thread_rng();
         let mut data = vec![0u8; 5589];
@@ -272,7 +291,7 @@ mod test {
         }
     }

-    #[test]
+    //#[test]
     fn rolling() {
         assert_eq!(RollingAdler32::from_value(0x01020304).hash(), 0x01020304);

@@ -293,7 +312,7 @@ mod test {
         do_test(b"this a ", b"test");
     }

-    #[test]
+    //#[test]
     fn long_window_remove() {
         let mut hash = RollingAdler32::new();
         let w = 65536;

Help want :(

The test for compare is failed

thread '<unnamed>' panicked at 'TLS access error: AccessError { msg: "If TLS data needs to be destructed, TCS policy must be Bound." }', /root/.cargo/git/checkouts/incubator-teaclave-sgx-sdk-c63c8825343e87f0/779acdd/sgx_tstd/src/thread/local.rs:152:9
testing compare ... failed!