From f2812e4fe256015415c41b42b35f6f701c91cfb5 Mon Sep 17 00:00:00 2001 From: David Drysdale Date: Wed, 16 Jun 2021 13:40:18 +0000 Subject: [PATCH] cbor: drop std feature, use core in tests (#327) --- .github/workflows/cbor_test.yml | 4 ++-- Cargo.toml | 2 +- fuzz/fuzz_helper/Cargo.toml | 2 +- libraries/cbor/Cargo.toml | 3 --- libraries/cbor/src/lib.rs | 4 +--- libraries/cbor/src/macros.rs | 27 ++++++++++++++------------- libraries/cbor/src/reader.rs | 7 ++++--- libraries/cbor/src/values.rs | 9 +++++---- libraries/cbor/src/writer.rs | 17 +++++++++-------- libraries/crypto/Cargo.toml | 2 +- 10 files changed, 38 insertions(+), 39 deletions(-) diff --git a/.github/workflows/cbor_test.yml b/.github/workflows/cbor_test.yml index a8f1b01..7d341dc 100644 --- a/.github/workflows/cbor_test.yml +++ b/.github/workflows/cbor_test.yml @@ -29,10 +29,10 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: --manifest-path libraries/cbor/Cargo.toml --release --features std + args: --manifest-path libraries/cbor/Cargo.toml --release - name: Unit testing of CBOR library (debug mode) uses: actions-rs/cargo@v1 with: command: test - args: --manifest-path libraries/cbor/Cargo.toml --features std + args: --manifest-path libraries/cbor/Cargo.toml diff --git a/Cargo.toml b/Cargo.toml index 63b109a..3ed2531 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ subtle = { version = "2.2", default-features = false, features = ["nightly"] } debug_allocations = ["lang_items/debug_allocations"] debug_ctap = ["libtock_drivers/debug_ctap"] panic_console = ["lang_items/panic_console"] -std = ["cbor/std", "crypto/std", "lang_items/std", "persistent_store/std"] +std = ["crypto/std", "lang_items/std", "persistent_store/std"] verbose = ["debug_ctap", "libtock_drivers/verbose_usb"] with_ctap1 = ["crypto/with_ctap1"] with_nfc = ["libtock_drivers/with_nfc"] diff --git a/fuzz/fuzz_helper/Cargo.toml b/fuzz/fuzz_helper/Cargo.toml index 2f2a5c1..eccf972 100644 --- a/fuzz/fuzz_helper/Cargo.toml +++ b/fuzz/fuzz_helper/Cargo.toml @@ -9,6 +9,6 @@ edition = "2018" arrayref = "0.3.6" libtock_drivers = { path = "../../third_party/libtock-drivers" } crypto = { path = "../../libraries/crypto", features = ['std'] } -cbor = { path = "../../libraries/cbor", features = ['std'] } +cbor = { path = "../../libraries/cbor" } ctap2 = { path = "../..", features = ['std'] } lang_items = { path = "../../third_party/lang-items", features = ['std'] } diff --git a/libraries/cbor/Cargo.toml b/libraries/cbor/Cargo.toml index bcae013..913a9fd 100644 --- a/libraries/cbor/Cargo.toml +++ b/libraries/cbor/Cargo.toml @@ -10,6 +10,3 @@ license = "Apache-2.0" edition = "2018" [dependencies] - -[features] -std = [] diff --git a/libraries/cbor/src/lib.rs b/libraries/cbor/src/lib.rs index 0667484..ed378f5 100644 --- a/libraries/cbor/src/lib.rs +++ b/libraries/cbor/src/lib.rs @@ -12,11 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] extern crate alloc; -#[cfg(feature = "std")] -extern crate core; pub mod macros; pub mod reader; diff --git a/libraries/cbor/src/macros.rs b/libraries/cbor/src/macros.rs index 7dac984..3cde9f5 100644 --- a/libraries/cbor/src/macros.rs +++ b/libraries/cbor/src/macros.rs @@ -352,6 +352,7 @@ macro_rules! cbor_bytes_lit { #[cfg(test)] mod test { use super::super::values::{SimpleValue, Value}; + use alloc::{string::String, vec, vec::Vec}; #[test] fn test_cbor_simple_values() { @@ -373,8 +374,8 @@ mod test { assert_eq!(cbor_int!(1), Value::Unsigned(1)); assert_eq!(cbor_int!(123456), Value::Unsigned(123456)); assert_eq!( - cbor_int!(std::i64::MAX), - Value::Unsigned(std::i64::MAX as u64) + cbor_int!(core::i64::MAX), + Value::Unsigned(core::i64::MAX as u64) ); } @@ -382,34 +383,34 @@ mod test { fn test_cbor_int_negative() { assert_eq!(cbor_int!(-1), Value::Negative(-1)); assert_eq!(cbor_int!(-123456), Value::Negative(-123456)); - assert_eq!(cbor_int!(std::i64::MIN), Value::Negative(std::i64::MIN)); + assert_eq!(cbor_int!(core::i64::MIN), Value::Negative(core::i64::MIN)); } #[test] fn test_cbor_int_literals() { let a = cbor_array![ - std::i64::MIN, - std::i32::MIN, + core::i64::MIN, + core::i32::MIN, -123456, -1, 0, 1, 123456, - std::i32::MAX, - std::i64::MAX, - std::u64::MAX, + core::i32::MAX, + core::i64::MAX, + core::u64::MAX, ]; let b = Value::Array(vec![ - Value::Negative(std::i64::MIN), - Value::Negative(std::i32::MIN as i64), + Value::Negative(core::i64::MIN), + Value::Negative(core::i32::MIN as i64), Value::Negative(-123456), Value::Negative(-1), Value::Unsigned(0), Value::Unsigned(1), Value::Unsigned(123456), - Value::Unsigned(std::i32::MAX as u64), - Value::Unsigned(std::i64::MAX as u64), - Value::Unsigned(std::u64::MAX), + Value::Unsigned(core::i32::MAX as u64), + Value::Unsigned(core::i64::MAX as u64), + Value::Unsigned(core::u64::MAX), ]); assert_eq!(a, b); } diff --git a/libraries/cbor/src/reader.rs b/libraries/cbor/src/reader.rs index 865cd02..d8f159e 100644 --- a/libraries/cbor/src/reader.rs +++ b/libraries/cbor/src/reader.rs @@ -208,6 +208,7 @@ mod test { cbor_array, cbor_bytes, cbor_false, cbor_int, cbor_map, cbor_null, cbor_true, cbor_undefined, }; + use alloc::vec; #[test] fn test_read_unsigned() { @@ -227,7 +228,7 @@ mod test { vec![0x1B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00], ), ( - std::i64::MAX, + core::i64::MAX, vec![0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], ), ]; @@ -289,7 +290,7 @@ mod test { (-1000000, vec![0x3A, 0x00, 0x0F, 0x42, 0x3F]), (-4294967296, vec![0x3A, 0xFF, 0xFF, 0xFF, 0xFF]), ( - std::i64::MIN, + core::i64::MIN, vec![0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], ), ]; @@ -326,7 +327,7 @@ mod test { ("\"\\", vec![0x62, 0x22, 0x5C]), ("ü", vec![0x62, 0xC3, 0xBC]), ( - std::str::from_utf8(&unicode_3byte).unwrap(), + core::str::from_utf8(&unicode_3byte).unwrap(), vec![0x63, 0xE6, 0xB0, 0xB4], ), ("𐅑", vec![0x64, 0xF0, 0x90, 0x85, 0x91]), diff --git a/libraries/cbor/src/values.rs b/libraries/cbor/src/values.rs index 1e1324d..9326fc7 100644 --- a/libraries/cbor/src/values.rs +++ b/libraries/cbor/src/values.rs @@ -232,6 +232,7 @@ where mod test { use super::*; use crate::{cbor_array, cbor_bool, cbor_bytes, cbor_int, cbor_map, cbor_text}; + use alloc::vec; #[test] fn test_value_ordering() { @@ -239,14 +240,14 @@ mod test { assert!(cbor_int!(23) < cbor_int!(24)); assert!(cbor_int!(24) < cbor_int!(1000)); assert!(cbor_int!(1000) < cbor_int!(1000000)); - assert!(cbor_int!(1000000) < cbor_int!(std::i64::MAX)); - assert!(cbor_int!(std::i64::MAX) < cbor_int!(-1)); + assert!(cbor_int!(1000000) < cbor_int!(core::i64::MAX)); + assert!(cbor_int!(core::i64::MAX) < cbor_int!(-1)); assert!(cbor_int!(-1) < cbor_int!(-23)); assert!(cbor_int!(-23) < cbor_int!(-24)); assert!(cbor_int!(-24) < cbor_int!(-1000)); assert!(cbor_int!(-1000) < cbor_int!(-1000000)); - assert!(cbor_int!(-1000000) < cbor_int!(std::i64::MIN)); - assert!(cbor_int!(std::i64::MIN) < cbor_bytes!(vec![])); + assert!(cbor_int!(-1000000) < cbor_int!(core::i64::MIN)); + assert!(cbor_int!(core::i64::MIN) < cbor_bytes!(vec![])); assert!(cbor_bytes!(vec![]) < cbor_bytes!(vec![0x00])); assert!(cbor_bytes!(vec![0x00]) < cbor_bytes!(vec![0x01])); assert!(cbor_bytes!(vec![0x01]) < cbor_bytes!(vec![0xFF])); diff --git a/libraries/cbor/src/writer.rs b/libraries/cbor/src/writer.rs index c8a4808..924ce48 100644 --- a/libraries/cbor/src/writer.rs +++ b/libraries/cbor/src/writer.rs @@ -101,6 +101,7 @@ mod test { cbor_array, cbor_array_vec, cbor_bytes, cbor_false, cbor_int, cbor_map, cbor_null, cbor_text, cbor_true, cbor_undefined, }; + use alloc::vec; fn write_return(value: Value) -> Option> { let mut encoded_cbor = Vec::new(); @@ -129,7 +130,7 @@ mod test { vec![0x1B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00], ), ( - std::i64::MAX, + core::i64::MAX, vec![0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], ), ]; @@ -154,7 +155,7 @@ mod test { vec![0x3B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00], ), ( - std::i64::MIN, + core::i64::MIN, vec![0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], ), ]; @@ -187,7 +188,7 @@ mod test { ("\"\\", vec![0x62, 0x22, 0x5C]), ("ü", vec![0x62, 0xC3, 0xBC]), ( - std::str::from_utf8(&unicode_3byte).unwrap(), + core::str::from_utf8(&unicode_3byte).unwrap(), vec![0x63, 0xE6, 0xB0, 0xB4], ), ("𐅑", vec![0x64, 0xF0, 0x90, 0x85, 0x91]), @@ -217,13 +218,13 @@ mod test { 0 => "a", 23 => "b", 24 => "c", - std::u8::MAX as i64 => "d", + core::u8::MAX as i64 => "d", 256 => "e", - std::u16::MAX as i64 => "f", + core::u16::MAX as i64 => "f", 65536 => "g", - std::u32::MAX as i64 => "h", + core::u32::MAX as i64 => "h", 4294967296_i64 => "i", - std::i64::MAX => "j", + core::i64::MAX => "j", -1 => "k", -24 => "l", -25 => "m", @@ -232,7 +233,7 @@ mod test { -65537 => "p", -4294967296_i64 => "q", -4294967297_i64 => "r", - std::i64::MIN => "s", + core::i64::MIN => "s", b"a" => 2, b"bar" => 3, b"foo" => 4, diff --git a/libraries/crypto/Cargo.toml b/libraries/crypto/Cargo.toml index aa1a597..a73b694 100644 --- a/libraries/crypto/Cargo.toml +++ b/libraries/crypto/Cargo.toml @@ -24,5 +24,5 @@ serde_json = { version = "1.0", optional = true } regex = { version = "1", optional = true } [features] -std = ["cbor/std", "hex", "rand", "ring", "untrusted", "serde", "serde_json", "regex"] +std = ["hex", "rand", "ring", "untrusted", "serde", "serde_json", "regex"] with_ctap1 = []