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

feature(integer-encoding-rs): add integer-encoding-rs@v1.0.7 #18

Closed sammyne closed 4 years ago

sammyne commented 4 years ago

Add Crates

Description

Port integer-encoding-rs@v1.0.7 based on teaclave-sgx-sdk@v1.1.1

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 from 1.

Diff of Changes

diff --git a/crates/integer-encoding-rs/Cargo.toml b/crates/integer-encoding-rs/Cargo.toml
index 1e52591..c6a48b5 100644
--- a/crates/integer-encoding-rs/Cargo.toml
+++ b/crates/integer-encoding-rs/Cargo.toml
@@ -10,3 +10,10 @@ keywords = ["integer", "varint", "zigzag", "protobuf", "serialize"]
 edition = "2018"

 [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 }
+
+[features]
+default = ["mesalock_sgx"]
+mesalock_sgx = ["sgx_tstd"]
+enclave_unit_test = ["sgx_tunittest"]
\ No newline at end of file
diff --git a/crates/integer-encoding-rs/src/fixed.rs b/crates/integer-encoding-rs/src/fixed.rs
index d228cdc..7b1272e 100644
--- a/crates/integer-encoding-rs/src/fixed.rs
+++ b/crates/integer-encoding-rs/src/fixed.rs
@@ -1,3 +1,5 @@
+use std::prelude::v1::*;
+
 use std::mem::transmute;

 /// FixedInt provides encoding/decoding to and from fixed int representations.
diff --git a/crates/integer-encoding-rs/src/fixed_tests.rs b/crates/integer-encoding-rs/src/fixed_tests.rs
index 4176d9b..786cbcd 100644
--- a/crates/integer-encoding-rs/src/fixed_tests.rs
+++ b/crates/integer-encoding-rs/src/fixed_tests.rs
@@ -1,26 +1,28 @@
-#[cfg(test)]
+//#[cfg(test)]
 mod tests {
+    use std::prelude::v1::*;
+
     use crate::fixed::FixedInt;

     use crate::reader::FixedIntReader;
     use crate::writer::FixedIntWriter;

-    #[test]
+    //#[test]
     fn test_u32_enc() {
         let result = (32 as u32).encode_fixed_vec();
         assert_eq!(result, vec![32, 0, 0, 0]);
     }
-    #[test]
+    //#[test]
     fn test_u16_enc() {
         let result = (256 as u16).encode_fixed_vec();
         assert_eq!(result, vec![0, 1]);
     }
-    #[test]
+    //#[test]
     fn test_i16_enc() {
         let result = (-32768 as i16).encode_fixed_vec();
         assert_eq!(result, vec![0, 128]);
     }
-    #[test]
+    //#[test]
     fn test_i32_enc() {
         let result = (-32767 as i32).encode_fixed_vec();
         assert_eq!(result, vec![1, 128, 255, 255]);
@@ -36,13 +38,13 @@ mod tests {
     }
     */

-    #[test]
+    //#[test]
     fn test_i32_enc_light() {
         let int = -32767 as i32;
         let result = int.encode_fixed_light();
         assert_eq!(result, &[1, 128, 255, 255]);
     }
-    #[test]
+    //#[test]
     fn test_all_identity() {
         let a: u16 = 17;
         let b: u32 = 17;
@@ -66,7 +68,7 @@ mod tests {
         assert_eq!(f, FixedInt::decode_fixed(&f.encode_fixed_light()));
     }

-    #[test]
+    //#[test]
     fn test_reader_writer() {
         let mut buf = Vec::with_capacity(128);

@@ -93,15 +95,41 @@ mod tests {
         assert!(reader.read_fixedint::<u32>().is_err());
     }

-    #[should_panic]
-    #[test]
+    //#[should_panic]
+    //#[test]
     fn test_invalid_decode_size() {
         assert_eq!(33, u64::decode_fixed(&[1, 0, 0, 0, 0, 1]));
     }
-    #[should_panic]
-    #[test]
+    //#[should_panic]
+    //#[test]
     fn test_invalid_encode_size() {
         let mut buf = [0 as u8; 4];
         (11 as u64).encode_fixed(&mut buf);
     }
+
+    use sgx_tunittest::*;
+
+    fn test_invalid_decode_size_panic() {
+        should_panic!(test_invalid_decode_size());
+    }
+
+    fn test_invalid_encode_size_panic() {
+        should_panic!(test_invalid_encode_size());
+    }
+
+    pub fn run_tests() {
+        rsgx_unit_tests!(
+            test_u32_enc,
+            test_u16_enc,
+            test_i16_enc,
+            test_i32_enc,
+            test_i32_enc_light,
+            test_all_identity,
+            test_reader_writer,
+            test_invalid_decode_size_panic,
+            test_invalid_encode_size_panic,
+        );
+    }
 }
+
+pub use tests::run_tests;
diff --git a/crates/integer-encoding-rs/src/lib.rs b/crates/integer-encoding-rs/src/lib.rs
index 561ba25..0d7f686 100644
--- a/crates/integer-encoding-rs/src/lib.rs
+++ b/crates/integer-encoding-rs/src/lib.rs
@@ -15,10 +15,21 @@
 //! }
 //! ```

+#![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;
+
 mod fixed;
+#[cfg(feature = "enclave_unit_test")]
 mod fixed_tests;

 mod varint;
+#[cfg(feature = "enclave_unit_test")]
 mod varint_tests;

 mod reader;
@@ -31,3 +42,6 @@ pub use reader::FixedIntReader;
 pub use reader::VarIntReader;
 pub use writer::FixedIntWriter;
 pub use writer::VarIntWriter;
+
+#[cfg(feature = "enclave_unit_test")]
+pub mod test;
diff --git a/crates/integer-encoding-rs/src/test/mod.rs b/crates/integer-encoding-rs/src/test/mod.rs
new file mode 100644
index 0000000..57ea780
--- /dev/null
+++ b/crates/integer-encoding-rs/src/test/mod.rs
@@ -0,0 +1,4 @@
+pub fn run_tests() {
+    crate::fixed_tests::run_tests();
+    crate::varint_tests::run_tests();
+}
diff --git a/crates/integer-encoding-rs/src/varint.rs b/crates/integer-encoding-rs/src/varint.rs
index 25063aa..266a0d6 100644
--- a/crates/integer-encoding-rs/src/varint.rs
+++ b/crates/integer-encoding-rs/src/varint.rs
@@ -1,3 +1,5 @@
+use std::prelude::v1::*;
+
 pub const MSB: u8 = 0b10000000;
 const DROP_MSB: u8 = 0b01111111;
 const EXTRACT_SEVEN: u8 = DROP_MSB;
diff --git a/crates/integer-encoding-rs/src/varint_tests.rs b/crates/integer-encoding-rs/src/varint_tests.rs
index 3d9438d..b4cb611 100644
--- a/crates/integer-encoding-rs/src/varint_tests.rs
+++ b/crates/integer-encoding-rs/src/varint_tests.rs
@@ -1,10 +1,12 @@
-#[cfg(test)]
+//#[cfg(test)]
 mod tests {
+    use std::prelude::v1::*;
+
     use crate::reader::VarIntReader;
     use crate::varint::VarInt;
     use crate::writer::VarIntWriter;

-    #[test]
+    //#[test]
     fn test_required_space() {
         assert_eq!((0 as u32).required_space(), 1);
         assert_eq!((1 as u32).required_space(), 1);
@@ -14,13 +16,13 @@ mod tests {
         assert_eq!((2097152 as u32).required_space(), 4);
     }

-    #[test]
+    //#[test]
     fn test_encode_u64() {
         assert_eq!((0 as u32).encode_var_vec(), vec![0b00000000]);
         assert_eq!((300 as u32).encode_var_vec(), vec![0b10101100, 0b00000010]);
     }

-    #[test]
+    //#[test]
     fn test_identity_u64() {
         for i in 1 as u64..100 {
             assert_eq!(u64::decode_var_vec(&i.encode_var_vec()), (i, 1));
@@ -30,13 +32,13 @@ mod tests {
         }
     }

-    #[test]
+    //#[test]
     fn test_decode_max_u64() {
         let max_vec_encoded = vec![0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01];
         assert_eq!(u64::decode_var_vec(&max_vec_encoded).0, u64::max_value());
     }

-    #[test]
+    //#[test]
     fn test_encode_i64() {
         assert_eq!((0 as i64).encode_var_vec(), (0 as u32).encode_var_vec());
         assert_eq!((150 as i64).encode_var_vec(), (300 as u32).encode_var_vec());
@@ -58,19 +60,19 @@ mod tests {
         );
     }

-    #[test]
+    //#[test]
     fn test_decode_min_i64() {
         let min_vec_encoded = vec![0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01];
         assert_eq!(i64::decode_var_vec(&min_vec_encoded).0, i64::min_value());
     }

-    #[test]
+    //#[test]
     fn test_decode_max_i64() {
         let max_vec_encoded = vec![0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01];
         assert_eq!(i64::decode_var_vec(&max_vec_encoded).0, i64::max_value());
     }

-    #[test]
+    //#[test]
     fn test_encode_i16() {
         assert_eq!((150 as i16).encode_var_vec(), (300 as u32).encode_var_vec());
         assert_eq!(
@@ -79,7 +81,7 @@ mod tests {
         );
     }

-    #[test]
+    //#[test]
     fn test_reader_writer() {
         let mut buf = Vec::with_capacity(128);

@@ -105,4 +107,22 @@ mod tests {

         assert!(reader.read_varint::<u32>().is_err());
     }
+
+    use sgx_tunittest::*;
+
+    pub fn run_tests() {
+        rsgx_unit_tests!(
+            test_required_space,
+            test_encode_u64,
+            test_identity_u64,
+            test_decode_max_u64,
+            test_encode_i64,
+            test_decode_min_i64,
+            test_decode_max_i64,
+            test_encode_i16,
+            test_reader_writer,
+        );
+    }
 }
+
+pub use tests::run_tests;
mssun commented 4 years ago

Thank you so much!