diff options
Diffstat (limited to 'vendor/ciborium/tests')
-rw-r--r-- | vendor/ciborium/tests/canonical.rs | 111 | ||||
-rw-r--r-- | vendor/ciborium/tests/codec.rs | 433 | ||||
-rw-r--r-- | vendor/ciborium/tests/error.rs | 59 | ||||
-rw-r--r-- | vendor/ciborium/tests/fuzz.rs | 64 | ||||
-rw-r--r-- | vendor/ciborium/tests/macro.rs | 358 | ||||
-rw-r--r-- | vendor/ciborium/tests/no_std.rs | 39 | ||||
-rw-r--r-- | vendor/ciborium/tests/recursion.rs | 48 | ||||
-rw-r--r-- | vendor/ciborium/tests/tag.rs | 57 |
8 files changed, 1169 insertions, 0 deletions
diff --git a/vendor/ciborium/tests/canonical.rs b/vendor/ciborium/tests/canonical.rs new file mode 100644 index 000000000..d4aa33c58 --- /dev/null +++ b/vendor/ciborium/tests/canonical.rs @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: Apache-2.0 + +extern crate std; + +use ciborium::cbor; +use ciborium::tag::Required; +use ciborium::value::CanonicalValue; +use rand::prelude::*; +use std::collections::BTreeMap; + +macro_rules! cval { + ($x:expr) => { + CanonicalValue::from(val!($x)) + }; +} + +macro_rules! val { + ($x:expr) => { + cbor!($x).unwrap() + }; +} + +#[test] +fn rfc8949_example() { + let mut array: Vec<CanonicalValue> = vec![ + cval!(10), + cval!(-1), + cval!(false), + cval!(100), + cval!("z"), + cval!([-1]), + cval!("aa"), + cval!([100]), + ]; + let golden = array.clone(); + + // Shuffle the array. + array.shuffle(&mut rand::thread_rng()); + + array.sort(); + + assert_eq!(array, golden); +} + +#[test] +fn map() { + let mut map = BTreeMap::new(); + map.insert(cval!(false), val!(2)); + map.insert(cval!([-1]), val!(5)); + map.insert(cval!(-1), val!(1)); + map.insert(cval!(10), val!(0)); + map.insert(cval!(100), val!(3)); + map.insert(cval!([100]), val!(7)); + map.insert(cval!("z"), val!(4)); + map.insert(cval!("aa"), val!(6)); + + let mut bytes1 = Vec::new(); + ciborium::ser::into_writer(&map, &mut bytes1).unwrap(); + + assert_eq!( + hex::encode(&bytes1), + "a80a002001f402186403617a048120056261610681186407" + ); +} + +#[test] +fn negative_numbers() { + let mut array: Vec<CanonicalValue> = vec![ + cval!(10), + cval!(-1), + cval!(-2), + cval!(-3), + cval!(-4), + cval!(false), + cval!(100), + cval!(-100), + cval!(-200), + cval!("z"), + cval!([-1]), + cval!(-300), + cval!("aa"), + cval!([100]), + ]; + let golden = array.clone(); + + // Shuffle the array. + array.shuffle(&mut rand::thread_rng()); + + array.sort(); + + assert_eq!(array, golden); +} + +#[test] +fn tagged_option() { + let mut opt = Some(Required::<u64, 0xff>(2u32.into())); + + let mut bytes = Vec::new(); + ciborium::ser::into_writer(&opt, &mut bytes).unwrap(); + + let output = ciborium::de::from_reader(&bytes[..]).unwrap(); + assert_eq!(opt, output); + + opt = None; + + let mut bytes = Vec::new(); + ciborium::ser::into_writer(&opt, &mut bytes).unwrap(); + + let output = ciborium::de::from_reader(&bytes[..]).unwrap(); + assert_eq!(opt, output); +} diff --git a/vendor/ciborium/tests/codec.rs b/vendor/ciborium/tests/codec.rs new file mode 100644 index 000000000..d5df9a7f2 --- /dev/null +++ b/vendor/ciborium/tests/codec.rs @@ -0,0 +1,433 @@ +// SPDX-License-Identifier: Apache-2.0 + +extern crate std; + +use std::collections::{BTreeMap, HashMap}; +use std::convert::TryFrom; +use std::fmt::Debug; + +use ciborium::value::Value; +use ciborium::{cbor, de::from_reader, ser::into_writer}; + +use rstest::rstest; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; + +macro_rules! val { + ($x:expr) => { + Value::try_from($x).unwrap() + }; +} + +macro_rules! hex { + ($x:expr) => { + serde_bytes::ByteBuf::from(hex::decode($x).unwrap()) + }; +} + +macro_rules! map { + ($($k:expr => $v:expr),*) => {{ + let mut map = BTreeMap::new(); + $( + map.insert($k, $v); + )* + map + }} +} + +// Keep the first "case" aligned to a line number ending in 1 for ease in finding tests. +#[allow(clippy::excessive_precision)] +#[rstest(input, value, bytes, alternate, equality, + + case(0u8, val!(0u8), "00", false, same), + case(0u16, val!(0u16), "00", false, same), + case(0u32, val!(0u32), "00", false, same), + case(0u64, val!(0u64), "00", false, same), + case(0u128, val!(0u128), "00", false, same), + case(0i8, val!(0i8), "00", false, same), + case(0i16, val!(0i16), "00", false, same), + case(0i32, val!(0i32), "00", false, same), + case(0i64, val!(0i64), "00", false, same), + case(0i128, val!(0i128), "00", false, same), + case(1u8, val!(1u8), "01", false, same), + case(1u16, val!(1u16), "01", false, same), + case(1u32, val!(1u32), "01", false, same), + case(1u64, val!(1u64), "01", false, same), + case(1u128, val!(1u128), "01", false, same), + case(1i8, val!(1i8), "01", false, same), + case(1i16, val!(1i16), "01", false, same), + case(1i32, val!(1i32), "01", false, same), + case(1i64, val!(1i64), "01", false, same), + case(1i128, val!(1i128), "01", false, same), + case(1u8, val!(1u8), "1b0000000000000001", true, same), + case(1u16, val!(1u16), "1b0000000000000001", true, same), + case(1u32, val!(1u32), "1b0000000000000001", true, same), + case(1u64, val!(1u64), "1b0000000000000001", true, same), + case(1u128, val!(1u128), "1b0000000000000001", true, same), + case(1i8, val!(1i8), "1b0000000000000001", true, same), + case(1i16, val!(1i16), "1b0000000000000001", true, same), + case(1i32, val!(1i32), "1b0000000000000001", true, same), + case(1i64, val!(1i64), "1b0000000000000001", true, same), + case(1i128, val!(1i128), "1b0000000000000001", true, same), + case(1u8, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1u16, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1u32, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1u64, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1u128, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1i8, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1i16, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1i32, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1i64, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(1i128, bigint(), "c2540000000000000000000000000000000000000001", true, same), // Not In RFC + case(10u8, val!(10u8), "0a", false, same), + case(10u16, val!(10u16), "0a", false, same), + case(10u32, val!(10u32), "0a", false, same), + case(10u64, val!(10u64), "0a", false, same), + case(10u128, val!(10u128), "0a", false, same), + case(10i8, val!(10i8), "0a", false, same), + case(10i16, val!(10i16), "0a", false, same), + case(10i32, val!(10i32), "0a", false, same), + case(10i64, val!(10i64), "0a", false, same), + case(10i128, val!(10i128), "0a", false, same), + case(23u8, val!(23u8), "17", false, same), + case(23u16, val!(23u16), "17", false, same), + case(23u32, val!(23u32), "17", false, same), + case(23u64, val!(23u64), "17", false, same), + case(23u128, val!(23u128), "17", false, same), + case(23i8, val!(23i8), "17", false, same), + case(23i16, val!(23i16), "17", false, same), + case(23i32, val!(23i32), "17", false, same), + case(23i64, val!(23i64), "17", false, same), + case(23i128, val!(23i128), "17", false, same), + case(24u8, val!(24u8), "1818", false, same), + case(24u16, val!(24u16), "1818", false, same), + case(24u32, val!(24u32), "1818", false, same), + case(24u64, val!(24u64), "1818", false, same), + case(24u128, val!(24u128), "1818", false, same), + case(24i8, val!(24i8), "1818", false, same), + case(24i16, val!(24i16), "1818", false, same), + case(24i32, val!(24i32), "1818", false, same), + case(24i64, val!(24i64), "1818", false, same), + case(24i128, val!(24i128), "1818", false, same), + case(25u8, val!(25u8), "1819", false, same), + case(25u16, val!(25u16), "1819", false, same), + case(25u32, val!(25u32), "1819", false, same), + case(25u64, val!(25u64), "1819", false, same), + case(25u128, val!(25u128), "1819", false, same), + case(25i8, val!(25i8), "1819", false, same), + case(25i16, val!(25i16), "1819", false, same), + case(25i32, val!(25i32), "1819", false, same), + case(25i64, val!(25i64), "1819", false, same), + case(25i128, val!(25i128), "1819", false, same), + case(100u8, val!(100u8), "1864", false, same), + case(100u16, val!(100u16), "1864", false, same), + case(100u32, val!(100u32), "1864", false, same), + case(100u64, val!(100u64), "1864", false, same), + case(100u128, val!(100u128), "1864", false, same), + case(100i8, val!(100i8), "1864", false, same), + case(100i16, val!(100i16), "1864", false, same), + case(100i32, val!(100i32), "1864", false, same), + case(100i64, val!(100i64), "1864", false, same), + case(100i128, val!(100i128), "1864", false, same), + case(1000u16, val!(1000u16), "1903e8", false, same), + case(1000u32, val!(1000u32), "1903e8", false, same), + case(1000u64, val!(1000u64), "1903e8", false, same), + case(1000u128, val!(1000u128), "1903e8", false, same), + case(1000i16, val!(1000i16), "1903e8", false, same), + case(1000i32, val!(1000i32), "1903e8", false, same), + case(1000i64, val!(1000i64), "1903e8", false, same), + case(1000i128, val!(1000i128), "1903e8", false, same), + case(1000000u32, val!(1000000u32), "1a000f4240", false, same), + case(1000000u64, val!(1000000u64), "1a000f4240", false, same), + case(1000000u128, val!(1000000u128), "1a000f4240", false, same), + case(1000000i32, val!(1000000i32), "1a000f4240", false, same), + case(1000000i64, val!(1000000i64), "1a000f4240", false, same), + case(1000000i128, val!(1000000i128), "1a000f4240", false, same), + case(1000000000000u64, val!(1000000000000u64), "1b000000e8d4a51000", false, same), + case(1000000000000u128, val!(1000000000000u128), "1b000000e8d4a51000", false, same), + case(1000000000000i64, val!(1000000000000i64), "1b000000e8d4a51000", false, same), + case(1000000000000i128, val!(1000000000000i128), "1b000000e8d4a51000", false, same), + case(18446744073709551615u64, val!(18446744073709551615u64), "1bffffffffffffffff", false, same), + case(18446744073709551615u128, val!(18446744073709551615u128), "1bffffffffffffffff", false, same), + case(18446744073709551615i128, val!(18446744073709551615i128), "1bffffffffffffffff", false, same), + case(18446744073709551616u128, val!(18446744073709551616u128), "c249010000000000000000", false, same), + case(18446744073709551616i128, val!(18446744073709551616i128), "c249010000000000000000", false, same), + case(-18446744073709551617i128, val!(-18446744073709551617i128), "c349010000000000000000", false, same), + case(-18446744073709551616i128, val!(-18446744073709551616i128), "3bffffffffffffffff", false, same), + case(-1000i16, val!(-1000i16), "3903e7", false, same), + case(-1000i32, val!(-1000i32), "3903e7", false, same), + case(-1000i64, val!(-1000i64), "3903e7", false, same), + case(-1000i128, val!(-1000i128), "3903e7", false, same), + case(-100i8, val!(-100i8), "3863", false, same), + case(-100i16, val!(-100i16), "3863", false, same), + case(-100i32, val!(-100i32), "3863", false, same), + case(-100i64, val!(-100i64), "3863", false, same), + case(-100i128, val!(-100i128), "3863", false, same), + case(-10i8, val!(-10i8), "29", false, same), + case(-10i16, val!(-10i16), "29", false, same), + case(-10i32, val!(-10i32), "29", false, same), + case(-10i64, val!(-10i64), "29", false, same), + case(-10i128, val!(-10i128), "29", false, same), + case(-1i8, val!(-1i8), "20", false, same), + case(-1i16, val!(-1i16), "20", false, same), + case(-1i32, val!(-1i32), "20", false, same), + case(-1i64, val!(-1i64), "20", false, same), + case(-1i128, val!(-1i128), "20", false, same), + case(-1i8, val!(-1i8), "3b0000000000000000", true, same), + case(-1i16, val!(-1i16), "3b0000000000000000", true, same), + case(-1i32, val!(-1i32), "3b0000000000000000", true, same), + case(-1i64, val!(-1i64), "3b0000000000000000", true, same), + case(-1i128, val!(-1i128), "3b0000000000000000", true, same), + case(0.0f32, val!(0.0f32), "f90000", false, Float), + case(0.0f64, val!(0.0f64), "f90000", false, Float), + case(-0.0f32, val!(-0.0f32), "f98000", false, Float), + case(-0.0f64, val!(-0.0f64), "f98000", false, Float), + case(1.0f32, val!(1.0f32), "f93c00", false, Float), + case(1.0f64, val!(1.0f64), "f93c00", false, Float), + case(1.1f32, val!(1.1f32), "fa3f8ccccd", false, Float), // Not In RFC + case(1.1f64, val!(1.1f64), "fb3ff199999999999a", false, Float), + case(1.5f32, val!(1.5f32), "f93e00", false, Float), + case(1.5f64, val!(1.5f64), "f93e00", false, Float), + case(65504.0f32, val!(65504.0f32), "f97bff", false, Float), + case(65504.0f64, val!(65504.0f64), "f97bff", false, Float), + case(100000.0f32, val!(100000.0f32), "fa47c35000", false, Float), + case(100000.0f64, val!(100000.0f64), "fa47c35000", false, Float), + case(3.4028234663852886e+38f32, val!(3.4028234663852886e+38f32), "fa7f7fffff", false, Float), + case(3.4028234663852886e+38f64, val!(3.4028234663852886e+38f64), "fa7f7fffff", false, Float), + case(1.0e+300f64, val!(1.0e+300f64), "fb7e37e43c8800759c", false, Float), + case(5.960464477539063e-8f32, val!(5.960464477539063e-8f32), "f90001", false, Float), + case(5.960464477539063e-8f64, val!(5.960464477539063e-8f64), "f90001", false, Float), + case(0.00006103515625f32, val!(0.00006103515625f32), "f90400", false, Float), + case(0.00006103515625f64, val!(0.00006103515625f64), "f90400", false, Float), + case(-4.0f32, val!(-4.0f32), "f9c400", false, Float), + case(-4.0f64, val!(-4.0f64), "f9c400", false, Float), + case(-4.1f32, val!(-4.1f32), "fac0833333", false, Float), // Not In RFC + case(-4.1f64, val!(-4.1f64), "fbc010666666666666", false, Float), + case(core::f32::INFINITY, val!(core::f32::INFINITY), "f97c00", false, Float), + case(core::f64::INFINITY, val!(core::f64::INFINITY), "f97c00", false, Float), + case(core::f32::INFINITY, val!(core::f32::INFINITY), "fa7f800000", true, Float), + case(core::f64::INFINITY, val!(core::f64::INFINITY), "fa7f800000", true, Float), + case(core::f32::INFINITY, val!(core::f32::INFINITY), "fb7ff0000000000000", true, Float), + case(core::f64::INFINITY, val!(core::f64::INFINITY), "fb7ff0000000000000", true, Float), + case(-core::f32::INFINITY, val!(-core::f32::INFINITY), "f9fc00", false, Float), + case(-core::f64::INFINITY, val!(-core::f64::INFINITY), "f9fc00", false, Float), + case(-core::f32::INFINITY, val!(-core::f32::INFINITY), "faff800000", true, Float), + case(-core::f64::INFINITY, val!(-core::f64::INFINITY), "faff800000", true, Float), + case(-core::f32::INFINITY, val!(-core::f32::INFINITY), "fbfff0000000000000", true, Float), + case(-core::f64::INFINITY, val!(-core::f64::INFINITY), "fbfff0000000000000", true, Float), + case(core::f32::NAN, val!(core::f32::NAN), "f97e00", false, Float), + case(core::f64::NAN, val!(core::f64::NAN), "f97e00", false, Float), + case(core::f32::NAN, val!(core::f32::NAN), "fa7fc00000", true, Float), + case(core::f64::NAN, val!(core::f64::NAN), "fa7fc00000", true, Float), + case(core::f32::NAN, val!(core::f32::NAN), "fb7ff8000000000000", true, Float), + case(core::f64::NAN, val!(core::f64::NAN), "fb7ff8000000000000", true, Float), + case(-core::f32::NAN, val!(-core::f32::NAN), "f9fe00", false, Float), // Not In RFC + case(-core::f64::NAN, val!(-core::f64::NAN), "f9fe00", false, Float), // Not In RFC + case(-core::f32::NAN, val!(-core::f32::NAN), "faffc00000", true, Float), // Not In RFC + case(-core::f64::NAN, val!(-core::f64::NAN), "faffc00000", true, Float), // Not In RFC + case(-core::f32::NAN, val!(-core::f32::NAN), "fbfff8000000000000", true, Float), // Not In RFC + case(-core::f64::NAN, val!(-core::f64::NAN), "fbfff8000000000000", true, Float), // Not In RFC + case(false, val!(false), "f4", false, same), + case(true, val!(true), "f5", false, same), + case(Value::Null, Value::Null, "f6", false, same), + case(hex!(""), val!(&b""[..]), "40", false, same), + case(hex!("01020304"), val!(&b"\x01\x02\x03\x04"[..]), "4401020304", false, same), + case(hex!("0102030405"), val!(&b"\x01\x02\x03\x04\x05"[..]), "5f42010243030405ff", true, same), + case("", val!(""), "60", false, ToOwned::to_owned), + case("a", val!("a"), "6161", false, ToOwned::to_owned), + case('a', val!('a'), "6161", false, same), + case("IETF", val!("IETF"), "6449455446", false, ToOwned::to_owned), + case("\"\\", val!("\"\\"), "62225c", false, ToOwned::to_owned), + case("ü", val!("ü"), "62c3bc", false, ToOwned::to_owned), + case('ü', val!('ü'), "62c3bc", false, same), + case("水", val!("水"), "63e6b0b4", false, ToOwned::to_owned), + case('水', val!('水'), "63e6b0b4", false, same), + case("𐅑", val!("𐅑"), "64f0908591", false, ToOwned::to_owned), + case('𐅑', val!('𐅑'), "64f0908591", false, same), + case("streaming", val!("streaming"), "7f657374726561646d696e67ff", true, ToOwned::to_owned), + case(cbor!([]).unwrap(), Vec::<Value>::new().into(), "80", false, same), + case(cbor!([]).unwrap(), Vec::<Value>::new().into(), "9fff", true, same), + case(cbor!([1, 2, 3]).unwrap(), cbor!([1, 2, 3]).unwrap(), "83010203", false, same), + case(cbor!([1, [2, 3], [4, 5]]).unwrap(), cbor!([1, [2, 3], [4, 5]]).unwrap(), "8301820203820405", false, same), + case(cbor!([1, [2, 3], [4, 5]]).unwrap(), cbor!([1, [2, 3], [4, 5]]).unwrap(), "9f018202039f0405ffff", true, same), + case(cbor!([1, [2, 3], [4, 5]]).unwrap(), cbor!([1, [2, 3], [4, 5]]).unwrap(), "9f01820203820405ff", true, same), + case(cbor!([1, [2, 3], [4, 5]]).unwrap(), cbor!([1, [2, 3], [4, 5]]).unwrap(), "83018202039f0405ff", true, same), + case(cbor!([1, [2, 3], [4, 5]]).unwrap(), cbor!([1, [2, 3], [4, 5]]).unwrap(), "83019f0203ff820405", true, same), + case((1..=25).collect::<Vec<u8>>(), (1..=25).map(|x| x.into()).collect::<Vec<Value>>().into(), "98190102030405060708090a0b0c0d0e0f101112131415161718181819", false, same), + case((1..=25).collect::<Vec<u8>>(), (1..=25).map(|x| x.into()).collect::<Vec<Value>>().into(), "9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff", true, same), + case(HashMap::<u8, u8>::new(), Value::Map(vec![]), "a0", false, same), + case(BTreeMap::<u8, u8>::new(), Value::Map(vec![]), "a0", false, same), + case(map!{1 => 2, 3 => 4}, cbor!({1 => 2, 3 => 4}).unwrap(), "a201020304", false, same), + case(cbor!({"a" => 1, "b" => [2, 3]}).unwrap(), cbor!({"a" => 1, "b" => [2, 3]}).unwrap(), "a26161016162820203", false, same), + case(cbor!({"a" => 1, "b" => [2, 3]}).unwrap(), cbor!({"a" => 1, "b" => [2, 3]}).unwrap(), "bf61610161629f0203ffff", true, same), + case(cbor!(["a", {"b" => "c"}]).unwrap(), cbor!(["a", {"b" => "c"}]).unwrap(), "826161a161626163", false, same), + case(cbor!(["a", {"b" => "c"}]).unwrap(), cbor!(["a", {"b" => "c"}]).unwrap(), "826161bf61626163ff", true, same), + case(cbor!({"Fun" => true, "Amt" => -2}).unwrap(), cbor!({"Fun" => true, "Amt" => -2}).unwrap(), "bf6346756ef563416d7421ff", true, same), + case(map_big(), vmap_big(), "a56161614161626142616361436164614461656145", false, same), + case(Option::<u8>::None, Value::Null, "f6", false, same), // Not In RFC + case(Option::Some(7u8), val!(7u8), "07", false, same), // Not In RFC + case((), Value::Null, "f6", false, same), // Not In RFC + case(UnitStruct, Value::Null, "f6", false, same), // Not In RFC + case(Newtype(123), val!(123u8), "187b", false, same), // Not In RFC + case((22u8, 23u16), cbor!([22, 23]).unwrap(), "821617", false, same), // Not In RFC + case(TupleStruct(33, 34), cbor!([33, 34]).unwrap(), "8218211822", false, same), // Not In RFC + case(Enum::Unit, cbor!("Unit").unwrap(), "64556e6974", false, same), // Not In RFC + case(Enum::Newtype(45), cbor!({"Newtype" => 45}).unwrap(), "a1674e657774797065182d", false, same), // Not In RFC + case(Enum::Tuple(56, 67), cbor!({"Tuple" => [56, 67]}).unwrap(), "a1655475706c658218381843", false, same), // Not In RFC + case(Enum::Struct { first: 78, second: 89 }, cbor!({ "Struct" => { "first" => 78, "second" => 89 }}).unwrap(), "a166537472756374a2656669727374184e667365636f6e641859", false, same), // Not In RFC +)] +fn codec<'de, T: Serialize + Clone, V: Debug + PartialEq + DeserializeOwned, F: Fn(T) -> V>( + input: T, + value: Value, + bytes: &str, + alternate: bool, + equality: F, +) { + let bytes = hex::decode(bytes).unwrap(); + + if !alternate { + let mut encoded = Vec::new(); + into_writer(&input, &mut encoded).unwrap(); + eprintln!("{:x?} == {:x?}", bytes, encoded); + assert_eq!(bytes, encoded); + + let mut encoded = Vec::new(); + into_writer(&value, &mut encoded).unwrap(); + eprintln!("{:x?} == {:x?}", bytes, encoded); + assert_eq!(bytes, encoded); + + let encoded = Value::serialized(&input).unwrap(); + eprintln!("{:x?} == {:x?}", &value, &encoded); + assert!(veq(&value, &encoded)); + } + + let decoded: V = from_reader(&bytes[..]).unwrap(); + let answer = equality(input.clone()); + eprintln!("{:x?} == {:x?}", answer, decoded); + assert_eq!(answer, decoded); + + let decoded: Value = from_reader(&bytes[..]).unwrap(); + eprintln!("{:x?} == {:x?}", &value, &decoded); + assert!(veq(&value, &decoded)); + + let decoded: V = value.deserialized().unwrap(); + let answer = equality(input); + eprintln!("{:x?} == {:x?}", answer, decoded); + assert_eq!(answer, decoded); +} + +#[inline] +fn veq(lhs: &Value, rhs: &Value) -> bool { + if let Value::Float(l) = lhs { + if let Value::Float(r) = rhs { + return Float(*l) == Float(*r); + } + } + + lhs == rhs +} + +#[inline] +fn same<T>(x: T) -> T { + x +} + +#[derive(Debug, Deserialize)] +struct Float<T>(T); + +impl PartialEq for Float<f32> { + fn eq(&self, other: &Float<f32>) -> bool { + if self.0.is_nan() && other.0.is_nan() { + return true; + } + + self.0 == other.0 + } +} + +impl PartialEq for Float<f64> { + fn eq(&self, other: &Float<f64>) -> bool { + if self.0.is_nan() && other.0.is_nan() { + return true; + } + + self.0 == other.0 + } +} + +#[inline] +fn map_big() -> BTreeMap<String, String> { + let mut map = BTreeMap::new(); + map.insert("a".into(), "A".into()); + map.insert("b".into(), "B".into()); + map.insert("c".into(), "C".into()); + map.insert("d".into(), "D".into()); + map.insert("e".into(), "E".into()); + map +} + +#[inline] +fn vmap_big() -> Value { + Value::Map( + map_big() + .into_iter() + .map(|x| (x.0.into(), x.1.into())) + .collect(), + ) +} + +#[inline] +fn bigint() -> Value { + let bytes = hex::decode("0000000000000000000000000000000000000001").unwrap(); + Value::Tag(2, Value::Bytes(bytes).into()) +} + +#[derive(Deserialize, Serialize, Copy, Clone, Debug, PartialEq, Eq)] +struct UnitStruct; + +#[derive(Deserialize, Serialize, Copy, Clone, Debug, PartialEq, Eq)] +struct TupleStruct(u8, u16); + +#[derive(Deserialize, Serialize, Copy, Clone, Debug, PartialEq, Eq)] +struct Newtype(u8); + +#[derive(Deserialize, Serialize, Copy, Clone, Debug, PartialEq, Eq)] +enum Enum { + Unit, + Newtype(u8), + Tuple(u8, u16), + Struct { first: u8, second: u16 }, +} + +#[rstest( + input, + case(vec![]), + case(vec![0u8, 1, 2, 3]), +)] +fn byte_vec_serde_bytes_compatibility(input: Vec<u8>) { + use serde_bytes::ByteBuf; + + let mut buf = Vec::new(); + into_writer(&input, &mut buf).unwrap(); + let bytes: ByteBuf = from_reader(&buf[..]).unwrap(); + assert_eq!(input, bytes.to_vec()); + + let mut buf = Vec::new(); + into_writer(&ByteBuf::from(input.clone()), &mut buf).unwrap(); + let bytes: Vec<u8> = from_reader(&buf[..]).unwrap(); + assert_eq!(input, bytes); +} + +#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] +struct Foo { + bar: u8, +} + +#[rstest(input, expected, + case("a163626172182a", Foo { bar: 42 }), + case("a143626172182a", Foo { bar: 42 }), +)] +fn handle_struct_field_names(input: &str, expected: Foo) { + let buf = hex::decode(input).unwrap(); + let read = from_reader(&buf[..]).unwrap(); + assert_eq!(expected, read); +} diff --git a/vendor/ciborium/tests/error.rs b/vendor/ciborium/tests/error.rs new file mode 100644 index 000000000..13bcd5ac5 --- /dev/null +++ b/vendor/ciborium/tests/error.rs @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: Apache-2.0 + +use ciborium::{ + de::{from_reader, Error}, + ser::into_writer, + value::Value, +}; +use rstest::rstest; + +#[rstest(bytes, error, + // Invalid value + case("1e", Error::Syntax(0)), + + // Indeterminate integers are invalid + case("1f", Error::Syntax(0)), + + // Indeterminate integer in an array + case("83011f03", Error::Syntax(2)), + + // Integer in a string continuation + case("7F616101FF", Error::Syntax(3)), + + // Bytes in a string continuation + case("7F61614101FF", Error::Syntax(3)), + + // Invalid UTF-8 + case("62C328", Error::Syntax(0)), + + // Invalid UTF-8 in a string continuation + case("7F62C328FF", Error::Syntax(1)), +)] +fn test(bytes: &str, error: Error<std::io::Error>) { + let bytes = hex::decode(bytes).unwrap(); + + let correct = match error { + Error::Io(..) => panic!(), + Error::Syntax(x) => ("syntax", Some(x), None), + Error::Semantic(x, y) => ("semantic", x, Some(y)), + Error::RecursionLimitExceeded => panic!(), + }; + + let result: Result<Value, _> = from_reader(&bytes[..]); + let actual = match result.unwrap_err() { + Error::Io(..) => panic!(), + Error::Syntax(x) => ("syntax", Some(x), None), + Error::Semantic(x, y) => ("semantic", x, Some(y)), + Error::RecursionLimitExceeded => panic!(), + }; + + assert_eq!(correct, actual); +} + +#[test] +fn test_long_utf8_deserialization() { + let s = (0..2000).map(|_| 'ボ').collect::<String>(); + let mut v = Vec::new(); + into_writer(&s, &mut v).unwrap(); + let _: String = from_reader(&*v).unwrap(); +} diff --git a/vendor/ciborium/tests/fuzz.rs b/vendor/ciborium/tests/fuzz.rs new file mode 100644 index 000000000..2b9d0ecd9 --- /dev/null +++ b/vendor/ciborium/tests/fuzz.rs @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: Apache-2.0 + +use std::fs::File; +use std::io::Read; +use std::io::Write; +use std::os::raw::c_int; +use std::os::unix::io::{FromRawFd, RawFd}; + +use ciborium::{de::from_reader, value::Value}; +use rand::Rng; + +const ITERATIONS: usize = 128 * 1024; + +#[allow(non_camel_case_types)] +type pid_t = i32; + +extern "C" { + fn close(fd: RawFd) -> c_int; + fn fork() -> pid_t; + fn pipe(pipefd: &mut [RawFd; 2]) -> c_int; + fn waitpid(pid: pid_t, wstatus: *mut c_int, options: c_int) -> pid_t; +} + +#[test] +fn fuzz() { + let mut fds: [RawFd; 2] = [0; 2]; + assert_eq!(unsafe { pipe(&mut fds) }, 0); + + let pid = unsafe { fork() }; + assert!(pid >= 0); + + match pid { + 0 => { + let mut child = unsafe { File::from_raw_fd(fds[1]) }; + unsafe { close(fds[0]) }; + + let mut rng = rand::thread_rng(); + let mut buffer = [0u8; 32]; + + for _ in 0..ITERATIONS { + let len = rng.gen_range(0..buffer.len()); + rng.fill(&mut buffer[..len]); + + writeln!(child, "{}", hex::encode(&buffer[..len])).unwrap(); + writeln!(child, "{:?}", from_reader::<Value, _>(&buffer[..len])).unwrap(); + } + } + + pid => { + let mut parent = unsafe { File::from_raw_fd(fds[0]) }; + unsafe { close(fds[1]) }; + + let mut string = String::new(); + parent.read_to_string(&mut string).unwrap(); + eprint!("{}", string); + + let mut status = 0; + assert_eq!(pid, unsafe { waitpid(pid, &mut status, 0) }); + + eprintln!("exit status: {:?}", status); + assert_eq!(0, status); + } + } +} diff --git a/vendor/ciborium/tests/macro.rs b/vendor/ciborium/tests/macro.rs new file mode 100644 index 000000000..a73fc849c --- /dev/null +++ b/vendor/ciborium/tests/macro.rs @@ -0,0 +1,358 @@ +// SPDX-License-Identifier: Apache-2.0 + +extern crate alloc; + +use ciborium::{ + cbor, + value::{Integer, Value, Value::Null}, +}; + +use rstest::rstest; +use serde_bytes::Bytes; + +macro_rules! map { + ($($key:expr => $val:expr),* $(,)*) => { + Value::Map(vec![$( + ( + Value::serialized(&$key).unwrap(), + Value::serialized(&$val).unwrap() + ) + ),*]) + }; +} + +macro_rules! arr { + ($($val:expr),*) => { + Value::Array(vec![$( + Value::serialized(&$val).unwrap() + ),*]) + }; +} + +#[rstest(answer, question, + // Non-numeric simple types + case(Value::Null, cbor!(null).unwrap()), + case(Value::Bool(true), cbor!(true).unwrap()), + case(Value::Bool(false), cbor!(false).unwrap()), + case(Value::Text("foo".into()), cbor!("foo").unwrap()), + case(Value::Bytes(vec![0, 1, 2]), cbor!(Bytes::new(b"\x00\x01\x02")).unwrap()), + + // Numeric simple types + case(Value::Integer(Integer::from(123)), cbor!(123).unwrap()), + case(Value::Integer(Integer::from(-123)), cbor!(-123).unwrap()), + case(Value::Float(1.23), cbor!(1.23).unwrap()), + case(Value::Float(-1.23), cbor!(-1.23).unwrap()), + case(Value::Float(2.5e+1), cbor!(2.5e+1).unwrap()), + case(Value::Float(-2.5e+1), cbor!(-2.5e+1).unwrap()), + + // Simple array formulations + case(arr![], cbor!([]).unwrap()), + case(arr![Null], cbor!([null]).unwrap()), + case(arr![true], cbor!([true]).unwrap()), + case(arr![false], cbor!([false]).unwrap()), + case(arr!["foo"], cbor!(["foo"]).unwrap()), + case(arr![123], cbor!([123]).unwrap()), + case(arr![-123], cbor!([-123]).unwrap()), + case(arr![1.23], cbor!([1.23]).unwrap()), + case(arr![-1.23], cbor!([-1.23]).unwrap()), + case(arr![2.5e+1], cbor!([2.5e+1]).unwrap()), + case(arr![2.5e+1], cbor!([2.5e+1]).unwrap()), + case(arr![[1, 2]], cbor!([[1, 2]]).unwrap()), + case(arr![map! {1=>2,3=>4}], cbor!([{1=>2,3=>4}]).unwrap()), + + // Two-item array formluations + case(arr![Null, Null], cbor!([null, null]).unwrap()), + case(arr![Null, true], cbor!([null, true]).unwrap()), + case(arr![Null, false], cbor!([null, false]).unwrap()), + case(arr![Null, "foo"], cbor!([null, "foo"]).unwrap()), + case(arr![Null, 123], cbor!([null, 123]).unwrap()), + case(arr![Null, -123], cbor!([null, -123]).unwrap()), + case(arr![Null, 1.23], cbor!([null, 1.23]).unwrap()), + case(arr![Null, -1.23], cbor!([null, -1.23]).unwrap()), + case(arr![Null, 2.5e+1], cbor!([null, 2.5e+1]).unwrap()), + case(arr![Null, 2.5e+1], cbor!([null, 2.5e+1]).unwrap()), + case(arr![Null, [1, 2]], cbor!([null, [1, 2]]).unwrap()), + case(arr![Null, map! {1=>2,3=>4}], cbor!([null, {1=>2,3=>4}]).unwrap()), + case(arr![true, Null], cbor!([true, null]).unwrap()), + case(arr![true, true], cbor!([true, true]).unwrap()), + case(arr![true, false], cbor!([true, false]).unwrap()), + case(arr![true, "foo"], cbor!([true, "foo"]).unwrap()), + case(arr![true, 123], cbor!([true, 123]).unwrap()), + case(arr![true, -123], cbor!([true, -123]).unwrap()), + case(arr![true, 1.23], cbor!([true, 1.23]).unwrap()), + case(arr![true, -1.23], cbor!([true, -1.23]).unwrap()), + case(arr![true, 2.5e+1], cbor!([true, 2.5e+1]).unwrap()), + case(arr![true, 2.5e+1], cbor!([true, 2.5e+1]).unwrap()), + case(arr![true, [1, 2]], cbor!([true, [1, 2]]).unwrap()), + case(arr![true, map! {1=>2,3=>4}], cbor!([true, {1=>2,3=>4}]).unwrap()), + case(arr![false, Null], cbor!([false, null]).unwrap()), + case(arr![false, true], cbor!([false, true]).unwrap()), + case(arr![false, false], cbor!([false, false]).unwrap()), + case(arr![false, "foo"], cbor!([false, "foo"]).unwrap()), + case(arr![false, 123], cbor!([false, 123]).unwrap()), + case(arr![false, -123], cbor!([false, -123]).unwrap()), + case(arr![false, 1.23], cbor!([false, 1.23]).unwrap()), + case(arr![false, -1.23], cbor!([false, -1.23]).unwrap()), + case(arr![false, 2.5e+1], cbor!([false, 2.5e+1]).unwrap()), + case(arr![false, 2.5e+1], cbor!([false, 2.5e+1]).unwrap()), + case(arr![false, [1, 2]], cbor!([false, [1, 2]]).unwrap()), + case(arr![false, map! {1=>2,3=>4}], cbor!([false, {1=>2,3=>4}]).unwrap()), + case(arr!["foo", Null], cbor!(["foo", null]).unwrap()), + case(arr!["foo", true], cbor!(["foo", true]).unwrap()), + case(arr!["foo", false], cbor!(["foo", false]).unwrap()), + case(arr!["foo", "foo"], cbor!(["foo", "foo"]).unwrap()), + case(arr!["foo", 123], cbor!(["foo", 123]).unwrap()), + case(arr!["foo", -123], cbor!(["foo", -123]).unwrap()), + case(arr!["foo", 1.23], cbor!(["foo", 1.23]).unwrap()), + case(arr!["foo", -1.23], cbor!(["foo", -1.23]).unwrap()), + case(arr!["foo", 2.5e+1], cbor!(["foo", 2.5e+1]).unwrap()), + case(arr!["foo", 2.5e+1], cbor!(["foo", 2.5e+1]).unwrap()), + case(arr!["foo", [1, 2]], cbor!(["foo", [1, 2]]).unwrap()), + case(arr!["foo", map! {1=>2,3=>4}], cbor!(["foo", {1=>2,3=>4}]).unwrap()), + case(arr![123, Null], cbor!([123, null]).unwrap()), + case(arr![123, true], cbor!([123, true]).unwrap()), + case(arr![123, false], cbor!([123, false]).unwrap()), + case(arr![123, "foo"], cbor!([123, "foo"]).unwrap()), + case(arr![123, 123], cbor!([123, 123]).unwrap()), + case(arr![123, -123], cbor!([123, -123]).unwrap()), + case(arr![123, 1.23], cbor!([123, 1.23]).unwrap()), + case(arr![123, -1.23], cbor!([123, -1.23]).unwrap()), + case(arr![123, 2.5e+1], cbor!([123, 2.5e+1]).unwrap()), + case(arr![123, 2.5e+1], cbor!([123, 2.5e+1]).unwrap()), + case(arr![123, [1, 2]], cbor!([123, [1, 2]]).unwrap()), + case(arr![123, map! {1=>2,3=>4}], cbor!([123, {1=>2,3=>4}]).unwrap()), + case(arr![-123, Null], cbor!([-123, null]).unwrap()), + case(arr![-123, true], cbor!([-123, true]).unwrap()), + case(arr![-123, false], cbor!([-123, false]).unwrap()), + case(arr![-123, "foo"], cbor!([-123, "foo"]).unwrap()), + case(arr![-123, 123], cbor!([-123, 123]).unwrap()), + case(arr![-123, -123], cbor!([-123, -123]).unwrap()), + case(arr![-123, 1.23], cbor!([-123, 1.23]).unwrap()), + case(arr![-123, -1.23], cbor!([-123, -1.23]).unwrap()), + case(arr![-123, 2.5e+1], cbor!([-123, 2.5e+1]).unwrap()), + case(arr![-123, 2.5e+1], cbor!([-123, 2.5e+1]).unwrap()), + case(arr![-123, [1, 2]], cbor!([-123, [1, 2]]).unwrap()), + case(arr![-123, map! {1=>2,3=>4}], cbor!([-123, {1=>2,3=>4}]).unwrap()), + case(arr![1.23, Null], cbor!([1.23, null]).unwrap()), + case(arr![1.23, true], cbor!([1.23, true]).unwrap()), + case(arr![1.23, false], cbor!([1.23, false]).unwrap()), + case(arr![1.23, "foo"], cbor!([1.23, "foo"]).unwrap()), + case(arr![1.23, 123], cbor!([1.23, 123]).unwrap()), + case(arr![1.23, -123], cbor!([1.23, -123]).unwrap()), + case(arr![1.23, 1.23], cbor!([1.23, 1.23]).unwrap()), + case(arr![1.23, -1.23], cbor!([1.23, -1.23]).unwrap()), + case(arr![1.23, 2.5e+1], cbor!([1.23, 2.5e+1]).unwrap()), + case(arr![1.23, 2.5e+1], cbor!([1.23, 2.5e+1]).unwrap()), + case(arr![1.23, [1, 2]], cbor!([1.23, [1, 2]]).unwrap()), + case(arr![1.23, map! {1=>2,3=>4}], cbor!([1.23, {1=>2,3=>4}]).unwrap()), + case(arr![-1.23, Null], cbor!([-1.23, null]).unwrap()), + case(arr![-1.23, true], cbor!([-1.23, true]).unwrap()), + case(arr![-1.23, false], cbor!([-1.23, false]).unwrap()), + case(arr![-1.23, "foo"], cbor!([-1.23, "foo"]).unwrap()), + case(arr![-1.23, 123], cbor!([-1.23, 123]).unwrap()), + case(arr![-1.23, -123], cbor!([-1.23, -123]).unwrap()), + case(arr![-1.23, 1.23], cbor!([-1.23, 1.23]).unwrap()), + case(arr![-1.23, -1.23], cbor!([-1.23, -1.23]).unwrap()), + case(arr![-1.23, 2.5e+1], cbor!([-1.23, 2.5e+1]).unwrap()), + case(arr![-1.23, 2.5e+1], cbor!([-1.23, 2.5e+1]).unwrap()), + case(arr![-1.23, [1, 2]], cbor!([-1.23, [1, 2]]).unwrap()), + case(arr![-1.23, map! {1=>2,3=>4}], cbor!([-1.23, {1=>2,3=>4}]).unwrap()), + case(arr![2.5e+1, Null], cbor!([2.5e+1, null]).unwrap()), + case(arr![2.5e+1, true], cbor!([2.5e+1, true]).unwrap()), + case(arr![2.5e+1, false], cbor!([2.5e+1, false]).unwrap()), + case(arr![2.5e+1, "foo"], cbor!([2.5e+1, "foo"]).unwrap()), + case(arr![2.5e+1, 123], cbor!([2.5e+1, 123]).unwrap()), + case(arr![2.5e+1, -123], cbor!([2.5e+1, -123]).unwrap()), + case(arr![2.5e+1, 1.23], cbor!([2.5e+1, 1.23]).unwrap()), + case(arr![2.5e+1, -1.23], cbor!([2.5e+1, -1.23]).unwrap()), + case(arr![2.5e+1, 2.5e+1], cbor!([2.5e+1, 2.5e+1]).unwrap()), + case(arr![2.5e+1, 2.5e+1], cbor!([2.5e+1, 2.5e+1]).unwrap()), + case(arr![2.5e+1, [1, 2]], cbor!([2.5e+1, [1, 2]]).unwrap()), + case(arr![2.5e+1, map! {1=>2,3=>4}], cbor!([2.5e+1, {1=>2,3=>4}]).unwrap()), + case(arr![2.5e+1, Null], cbor!([2.5e+1, null]).unwrap()), + case(arr![2.5e+1, true], cbor!([2.5e+1, true]).unwrap()), + case(arr![2.5e+1, false], cbor!([2.5e+1, false]).unwrap()), + case(arr![2.5e+1, "foo"], cbor!([2.5e+1, "foo"]).unwrap()), + case(arr![2.5e+1, 123], cbor!([2.5e+1, 123]).unwrap()), + case(arr![2.5e+1, -123], cbor!([2.5e+1, -123]).unwrap()), + case(arr![2.5e+1, 1.23], cbor!([2.5e+1, 1.23]).unwrap()), + case(arr![2.5e+1, -1.23], cbor!([2.5e+1, -1.23]).unwrap()), + case(arr![2.5e+1, 2.5e+1], cbor!([2.5e+1, 2.5e+1]).unwrap()), + case(arr![2.5e+1, 2.5e+1], cbor!([2.5e+1, 2.5e+1]).unwrap()), + case(arr![2.5e+1, [1, 2]], cbor!([2.5e+1, [1, 2]]).unwrap()), + case(arr![2.5e+1, map! {1=>2,3=>4}], cbor!([2.5e+1, {1=>2,3=>4}]).unwrap()), + case(arr![[1, 2], Null], cbor!([[1, 2], null]).unwrap()), + case(arr![[1, 2], true], cbor!([[1, 2], true]).unwrap()), + case(arr![[1, 2], false], cbor!([[1, 2], false]).unwrap()), + case(arr![[1, 2], "foo"], cbor!([[1, 2], "foo"]).unwrap()), + case(arr![[1, 2], 123], cbor!([[1, 2], 123]).unwrap()), + case(arr![[1, 2], -123], cbor!([[1, 2], -123]).unwrap()), + case(arr![[1, 2], 1.23], cbor!([[1, 2], 1.23]).unwrap()), + case(arr![[1, 2], -1.23], cbor!([[1, 2], -1.23]).unwrap()), + case(arr![[1, 2], 2.5e+1], cbor!([[1, 2], 2.5e+1]).unwrap()), + case(arr![[1, 2], 2.5e+1], cbor!([[1, 2], 2.5e+1]).unwrap()), + case(arr![[1, 2], [1, 2]], cbor!([[1, 2], [1, 2]]).unwrap()), + case(arr![[1, 2], map! {1=>2,3=>4}], cbor!([[1, 2], {1=>2,3=>4}]).unwrap()), + case(arr![map! {1=>2,3=>4}, Null], cbor!([{1=>2,3=>4}, null]).unwrap()), + case(arr![map! {1=>2,3=>4}, true], cbor!([{1=>2,3=>4}, true]).unwrap()), + case(arr![map! {1=>2,3=>4}, false], cbor!([{1=>2,3=>4}, false]).unwrap()), + case(arr![map! {1=>2,3=>4}, "foo"], cbor!([{1=>2,3=>4}, "foo"]).unwrap()), + case(arr![map! {1=>2,3=>4}, 123], cbor!([{1=>2,3=>4}, 123]).unwrap()), + case(arr![map! {1=>2,3=>4}, -123], cbor!([{1=>2,3=>4}, -123]).unwrap()), + case(arr![map! {1=>2,3=>4}, 1.23], cbor!([{1=>2,3=>4}, 1.23]).unwrap()), + case(arr![map! {1=>2,3=>4}, -1.23], cbor!([{1=>2,3=>4}, -1.23]).unwrap()), + case(arr![map! {1=>2,3=>4}, 2.5e+1], cbor!([{1=>2,3=>4}, 2.5e+1]).unwrap()), + case(arr![map! {1=>2,3=>4}, 2.5e+1], cbor!([{1=>2,3=>4}, 2.5e+1]).unwrap()), + case(arr![map! {1=>2,3=>4}, [1, 2]], cbor!([{1=>2,3=>4}, [1, 2]]).unwrap()), + case(arr![map! {1=>2,3=>4}, map! {1=>2,3=>4}], cbor!([{1=>2,3=>4}, {1=>2,3=>4}]).unwrap()), + + // Map formulations + case(map! {}, cbor!({}).unwrap()), + case(map! {Null => Null}, cbor!({ null => null }).unwrap()), + case(map! {Null => true}, cbor!({ null => true }).unwrap()), + case(map! {Null => false}, cbor!({ null => false }).unwrap()), + case(map! {Null => "foo"}, cbor!({ null => "foo" }).unwrap()), + case(map! {Null => 123}, cbor!({ null => 123 }).unwrap()), + case(map! {Null => -123}, cbor!({ null => -123 }).unwrap()), + case(map! {Null => 1.23}, cbor!({ null => 1.23 }).unwrap()), + case(map! {Null => -1.23}, cbor!({ null => -1.23 }).unwrap()), + case(map! {Null => 2.5e+1}, cbor!({ null => 2.5e+1 }).unwrap()), + case(map! {Null => 2.5e+1}, cbor!({ null => 2.5e+1 }).unwrap()), + case(map! {Null => [1, 2]}, cbor!({ null => [1, 2] }).unwrap()), + case(map! {Null => map! {1=>2,3=>4}}, cbor!({ null => {1=>2,3=>4} }).unwrap()), + case(map! {true => Null}, cbor!({ true => null }).unwrap()), + case(map! {true => true}, cbor!({ true => true }).unwrap()), + case(map! {true => false}, cbor!({ true => false }).unwrap()), + case(map! {true => "foo"}, cbor!({ true => "foo" }).unwrap()), + case(map! {true => 123}, cbor!({ true => 123 }).unwrap()), + case(map! {true => -123}, cbor!({ true => -123 }).unwrap()), + case(map! {true => 1.23}, cbor!({ true => 1.23 }).unwrap()), + case(map! {true => -1.23}, cbor!({ true => -1.23 }).unwrap()), + case(map! {true => 2.5e+1}, cbor!({ true => 2.5e+1 }).unwrap()), + case(map! {true => 2.5e+1}, cbor!({ true => 2.5e+1 }).unwrap()), + case(map! {true => [1, 2]}, cbor!({ true => [1, 2] }).unwrap()), + case(map! {true => map! {1=>2,3=>4}}, cbor!({ true => {1=>2,3=>4} }).unwrap()), + case(map! {false => Null}, cbor!({ false => null }).unwrap()), + case(map! {false => true}, cbor!({ false => true }).unwrap()), + case(map! {false => false}, cbor!({ false => false }).unwrap()), + case(map! {false => "foo"}, cbor!({ false => "foo" }).unwrap()), + case(map! {false => 123}, cbor!({ false => 123 }).unwrap()), + case(map! {false => -123}, cbor!({ false => -123 }).unwrap()), + case(map! {false => 1.23}, cbor!({ false => 1.23 }).unwrap()), + case(map! {false => -1.23}, cbor!({ false => -1.23 }).unwrap()), + case(map! {false => 2.5e+1}, cbor!({ false => 2.5e+1 }).unwrap()), + case(map! {false => 2.5e+1}, cbor!({ false => 2.5e+1 }).unwrap()), + case(map! {false => [1, 2]}, cbor!({ false => [1, 2] }).unwrap()), + case(map! {false => map! {1=>2,3=>4}}, cbor!({ false => {1=>2,3=>4} }).unwrap()), + case(map! {"foo" => Null}, cbor!({ "foo" => null }).unwrap()), + case(map! {"foo" => true}, cbor!({ "foo" => true }).unwrap()), + case(map! {"foo" => false}, cbor!({ "foo" => false }).unwrap()), + case(map! {"foo" => "foo"}, cbor!({ "foo" => "foo" }).unwrap()), + case(map! {"foo" => 123}, cbor!({ "foo" => 123 }).unwrap()), + case(map! {"foo" => -123}, cbor!({ "foo" => -123 }).unwrap()), + case(map! {"foo" => 1.23}, cbor!({ "foo" => 1.23 }).unwrap()), + case(map! {"foo" => -1.23}, cbor!({ "foo" => -1.23 }).unwrap()), + case(map! {"foo" => 2.5e+1}, cbor!({ "foo" => 2.5e+1 }).unwrap()), + case(map! {"foo" => 2.5e+1}, cbor!({ "foo" => 2.5e+1 }).unwrap()), + case(map! {"foo" => [1, 2]}, cbor!({ "foo" => [1, 2] }).unwrap()), + case(map! {"foo" => map! {1=>2,3=>4}}, cbor!({ "foo" => {1=>2,3=>4} }).unwrap()), + case(map! {123 => Null}, cbor!({ 123 => null }).unwrap()), + case(map! {123 => true}, cbor!({ 123 => true }).unwrap()), + case(map! {123 => false}, cbor!({ 123 => false }).unwrap()), + case(map! {123 => "foo"}, cbor!({ 123 => "foo" }).unwrap()), + case(map! {123 => 123}, cbor!({ 123 => 123 }).unwrap()), + case(map! {123 => -123}, cbor!({ 123 => -123 }).unwrap()), + case(map! {123 => 1.23}, cbor!({ 123 => 1.23 }).unwrap()), + case(map! {123 => -1.23}, cbor!({ 123 => -1.23 }).unwrap()), + case(map! {123 => 2.5e+1}, cbor!({ 123 => 2.5e+1 }).unwrap()), + case(map! {123 => 2.5e+1}, cbor!({ 123 => 2.5e+1 }).unwrap()), + case(map! {123 => [1, 2]}, cbor!({ 123 => [1, 2] }).unwrap()), + case(map! {123 => map! {1=>2,3=>4}}, cbor!({ 123 => {1=>2,3=>4} }).unwrap()), + case(map! {-123 => Null}, cbor!({ -123 => null }).unwrap()), + case(map! {-123 => true}, cbor!({ -123 => true }).unwrap()), + case(map! {-123 => false}, cbor!({ -123 => false }).unwrap()), + case(map! {-123 => "foo"}, cbor!({ -123 => "foo" }).unwrap()), + case(map! {-123 => 123}, cbor!({ -123 => 123 }).unwrap()), + case(map! {-123 => -123}, cbor!({ -123 => -123 }).unwrap()), + case(map! {-123 => 1.23}, cbor!({ -123 => 1.23 }).unwrap()), + case(map! {-123 => -1.23}, cbor!({ -123 => -1.23 }).unwrap()), + case(map! {-123 => 2.5e+1}, cbor!({ -123 => 2.5e+1 }).unwrap()), + case(map! {-123 => 2.5e+1}, cbor!({ -123 => 2.5e+1 }).unwrap()), + case(map! {-123 => [1, 2]}, cbor!({ -123 => [1, 2] }).unwrap()), + case(map! {-123 => map! {1=>2,3=>4}}, cbor!({ -123 => {1=>2,3=>4} }).unwrap()), + case(map! {1.23 => Null}, cbor!({ 1.23 => null }).unwrap()), + case(map! {1.23 => true}, cbor!({ 1.23 => true }).unwrap()), + case(map! {1.23 => false}, cbor!({ 1.23 => false }).unwrap()), + case(map! {1.23 => "foo"}, cbor!({ 1.23 => "foo" }).unwrap()), + case(map! {1.23 => 123}, cbor!({ 1.23 => 123 }).unwrap()), + case(map! {1.23 => -123}, cbor!({ 1.23 => -123 }).unwrap()), + case(map! {1.23 => 1.23}, cbor!({ 1.23 => 1.23 }).unwrap()), + case(map! {1.23 => -1.23}, cbor!({ 1.23 => -1.23 }).unwrap()), + case(map! {1.23 => 2.5e+1}, cbor!({ 1.23 => 2.5e+1 }).unwrap()), + case(map! {1.23 => 2.5e+1}, cbor!({ 1.23 => 2.5e+1 }).unwrap()), + case(map! {1.23 => [1, 2]}, cbor!({ 1.23 => [1, 2] }).unwrap()), + case(map! {1.23 => map! {1=>2,3=>4}}, cbor!({ 1.23 => {1=>2,3=>4} }).unwrap()), + case(map! {-1.23 => Null}, cbor!({ -1.23 => null }).unwrap()), + case(map! {-1.23 => true}, cbor!({ -1.23 => true }).unwrap()), + case(map! {-1.23 => false}, cbor!({ -1.23 => false }).unwrap()), + case(map! {-1.23 => "foo"}, cbor!({ -1.23 => "foo" }).unwrap()), + case(map! {-1.23 => 123}, cbor!({ -1.23 => 123 }).unwrap()), + case(map! {-1.23 => -123}, cbor!({ -1.23 => -123 }).unwrap()), + case(map! {-1.23 => 1.23}, cbor!({ -1.23 => 1.23 }).unwrap()), + case(map! {-1.23 => -1.23}, cbor!({ -1.23 => -1.23 }).unwrap()), + case(map! {-1.23 => 2.5e+1}, cbor!({ -1.23 => 2.5e+1 }).unwrap()), + case(map! {-1.23 => 2.5e+1}, cbor!({ -1.23 => 2.5e+1 }).unwrap()), + case(map! {-1.23 => [1, 2]}, cbor!({ -1.23 => [1, 2] }).unwrap()), + case(map! {-1.23 => map! {1=>2,3=>4}}, cbor!({ -1.23 => {1=>2,3=>4} }).unwrap()), + case(map! {2.5e+1 => Null}, cbor!({ 2.5e+1 => null }).unwrap()), + case(map! {2.5e+1 => true}, cbor!({ 2.5e+1 => true }).unwrap()), + case(map! {2.5e+1 => false}, cbor!({ 2.5e+1 => false }).unwrap()), + case(map! {2.5e+1 => "foo"}, cbor!({ 2.5e+1 => "foo" }).unwrap()), + case(map! {2.5e+1 => 123}, cbor!({ 2.5e+1 => 123 }).unwrap()), + case(map! {2.5e+1 => -123}, cbor!({ 2.5e+1 => -123 }).unwrap()), + case(map! {2.5e+1 => 1.23}, cbor!({ 2.5e+1 => 1.23 }).unwrap()), + case(map! {2.5e+1 => -1.23}, cbor!({ 2.5e+1 => -1.23 }).unwrap()), + case(map! {2.5e+1 => 2.5e+1}, cbor!({ 2.5e+1 => 2.5e+1 }).unwrap()), + case(map! {2.5e+1 => 2.5e+1}, cbor!({ 2.5e+1 => 2.5e+1 }).unwrap()), + case(map! {2.5e+1 => [1, 2]}, cbor!({ 2.5e+1 => [1, 2] }).unwrap()), + case(map! {2.5e+1 => map! {1=>2,3=>4}}, cbor!({ 2.5e+1 => {1=>2,3=>4} }).unwrap()), + case(map! {2.5e+1 => Null}, cbor!({ 2.5e+1 => null }).unwrap()), + case(map! {2.5e+1 => true}, cbor!({ 2.5e+1 => true }).unwrap()), + case(map! {2.5e+1 => false}, cbor!({ 2.5e+1 => false }).unwrap()), + case(map! {2.5e+1 => "foo"}, cbor!({ 2.5e+1 => "foo" }).unwrap()), + case(map! {2.5e+1 => 123}, cbor!({ 2.5e+1 => 123 }).unwrap()), + case(map! {2.5e+1 => -123}, cbor!({ 2.5e+1 => -123 }).unwrap()), + case(map! {2.5e+1 => 1.23}, cbor!({ 2.5e+1 => 1.23 }).unwrap()), + case(map! {2.5e+1 => -1.23}, cbor!({ 2.5e+1 => -1.23 }).unwrap()), + case(map! {2.5e+1 => 2.5e+1}, cbor!({ 2.5e+1 => 2.5e+1 }).unwrap()), + case(map! {2.5e+1 => 2.5e+1}, cbor!({ 2.5e+1 => 2.5e+1 }).unwrap()), + case(map! {2.5e+1 => [1, 2]}, cbor!({ 2.5e+1 => [1, 2] }).unwrap()), + case(map! {2.5e+1 => map! {1=>2,3=>4}}, cbor!({ 2.5e+1 => {1=>2,3=>4} }).unwrap()), + case(map! {[1, 2] => Null}, cbor!({ [1, 2] => null }).unwrap()), + case(map! {[1, 2] => true}, cbor!({ [1, 2] => true }).unwrap()), + case(map! {[1, 2] => false}, cbor!({ [1, 2] => false }).unwrap()), + case(map! {[1, 2] => "foo"}, cbor!({ [1, 2] => "foo" }).unwrap()), + case(map! {[1, 2] => 123}, cbor!({ [1, 2] => 123 }).unwrap()), + case(map! {[1, 2] => -123}, cbor!({ [1, 2] => -123 }).unwrap()), + case(map! {[1, 2] => 1.23}, cbor!({ [1, 2] => 1.23 }).unwrap()), + case(map! {[1, 2] => -1.23}, cbor!({ [1, 2] => -1.23 }).unwrap()), + case(map! {[1, 2] => 2.5e+1}, cbor!({ [1, 2] => 2.5e+1 }).unwrap()), + case(map! {[1, 2] => 2.5e+1}, cbor!({ [1, 2] => 2.5e+1 }).unwrap()), + case(map! {[1, 2] => [1, 2]}, cbor!({ [1, 2] => [1, 2] }).unwrap()), + case(map! {[1, 2] => map! {1=>2,3=>4}}, cbor!({ [1, 2] => {1=>2,3=>4} }).unwrap()), + case(map! {map! {1=>2,3=>4} => Null}, cbor!({ {1=>2,3=>4} => null }).unwrap()), + case(map! {map! {1=>2,3=>4} => true}, cbor!({ {1=>2,3=>4} => true }).unwrap()), + case(map! {map! {1=>2,3=>4} => false}, cbor!({ {1=>2,3=>4} => false }).unwrap()), + case(map! {map! {1=>2,3=>4} => "foo"}, cbor!({ {1=>2,3=>4} => "foo" }).unwrap()), + case(map! {map! {1=>2,3=>4} => 123}, cbor!({ {1=>2,3=>4} => 123 }).unwrap()), + case(map! {map! {1=>2,3=>4} => -123}, cbor!({ {1=>2,3=>4} => -123 }).unwrap()), + case(map! {map! {1=>2,3=>4} => 1.23}, cbor!({ {1=>2,3=>4} => 1.23 }).unwrap()), + case(map! {map! {1=>2,3=>4} => -1.23}, cbor!({ {1=>2,3=>4} => -1.23 }).unwrap()), + case(map! {map! {1=>2,3=>4} => 2.5e+1}, cbor!({ {1=>2,3=>4} => 2.5e+1 }).unwrap()), + case(map! {map! {1=>2,3=>4} => 2.5e+1}, cbor!({ {1=>2,3=>4} => 2.5e+1 }).unwrap()), + case(map! {map! {1=>2,3=>4} => [1, 2]}, cbor!({ {1=>2,3=>4} => [1, 2] }).unwrap()), + case(map! {map! {1=>2,3=>4} => map! {1=>2,3=>4}}, cbor!({ {1=>2,3=>4} => {1=>2,3=>4} }).unwrap()), +)] +fn test(answer: Value, question: Value) { + assert_eq!(answer, question); +} diff --git a/vendor/ciborium/tests/no_std.rs b/vendor/ciborium/tests/no_std.rs new file mode 100644 index 000000000..6ea008a46 --- /dev/null +++ b/vendor/ciborium/tests/no_std.rs @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: Apache-2.0 + +#![cfg(all(feature = "serde", not(feature = "std")))] +#![no_std] + +extern crate alloc; + +use alloc::vec::Vec; + +use ciborium::{de::from_reader, ser::into_writer}; + +#[test] +fn decode() { + assert_eq!(from_reader::<u8, &[u8]>(&[7u8][..]).unwrap(), 7); +} + +#[test] +fn eof() { + from_reader::<u8, &[u8]>(&[]).unwrap_err(); +} + +#[test] +fn encode_slice() { + let mut buffer = [0u8; 1]; + into_writer(&3u8, &mut buffer[..]).unwrap(); + assert_eq!(buffer[0], 3); +} + +#[test] +fn encode_vec() { + let mut buffer = Vec::with_capacity(1); + into_writer(&3u8, &mut buffer).unwrap(); + assert_eq!(buffer[0], 3); +} + +#[test] +fn oos() { + into_writer(&3u8, &mut [][..]).unwrap_err(); +} diff --git a/vendor/ciborium/tests/recursion.rs b/vendor/ciborium/tests/recursion.rs new file mode 100644 index 000000000..a340b2def --- /dev/null +++ b/vendor/ciborium/tests/recursion.rs @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: Apache-2.0 + +//! This test validates that we don't get stack overflows. +//! +//! If container types cause recursion, then a long list of prefixes which +//! indicate nested container types could cause the stack to overflow. We +//! test each of these types here to ensure there is no stack overflow. + +use ciborium::{ + de::{from_reader, Error}, + value::Value, +}; + +#[test] +fn array() { + let bytes = [0x9f; 128 * 1024]; + match from_reader::<Value, _>(&bytes[..]).unwrap_err() { + Error::RecursionLimitExceeded => (), + e => panic!("incorrect error: {:?}", e), + } +} + +#[test] +fn map() { + let bytes = [0xbf; 128 * 1024]; + match from_reader::<Value, _>(&bytes[..]).unwrap_err() { + Error::RecursionLimitExceeded => (), + e => panic!("incorrect error: {:?}", e), + } +} + +#[test] +fn bytes() { + let bytes = [0x5f; 128 * 1024]; + match from_reader::<Value, _>(&bytes[..]).unwrap_err() { + Error::Io(..) => (), + e => panic!("incorrect error: {:?}", e), + } +} + +#[test] +fn text() { + let bytes = [0x7f; 128 * 1024]; + match from_reader::<Value, _>(&bytes[..]).unwrap_err() { + Error::Io(..) => (), + e => panic!("incorrect error: {:?}", e), + } +} diff --git a/vendor/ciborium/tests/tag.rs b/vendor/ciborium/tests/tag.rs new file mode 100644 index 000000000..c19ee0090 --- /dev/null +++ b/vendor/ciborium/tests/tag.rs @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: Apache-2.0 + +extern crate alloc; + +use ciborium::{de::from_reader, ser::into_writer, tag::*, value::Value}; +use rstest::rstest; +use serde::{de::DeserializeOwned, Serialize}; + +use core::fmt::Debug; + +#[rstest(item, bytes, value, encode, success, + case(Captured(Some(6), true), "c6f5", Value::Tag(6, Value::Bool(true).into()), true, true), + case(Captured(None, true), "f5", Value::Bool(true), true, true), + + case(Required::<_, 6>(true), "c6f5", Value::Tag(6, Value::Bool(true).into()), true, true), + case(Required::<_, 6>(true), "c7f5", Value::Tag(7, Value::Bool(true).into()), false, false), + case(Required::<_, 6>(true), "f5", Value::Bool(true), false, false), + + case(Accepted::<_, 6>(true), "c6f5", Value::Tag(6, Value::Bool(true).into()), true, true), + case(Accepted::<_, 6>(true), "c7f5", Value::Tag(7, Value::Bool(true).into()), false, false), + case(Accepted::<_, 6>(true), "f5", Value::Bool(true), false, true), +)] +fn test<T: Serialize + DeserializeOwned + Debug + Eq>( + item: T, + bytes: &str, + value: Value, + encode: bool, + success: bool, +) { + let bytes = hex::decode(bytes).unwrap(); + + if encode { + // Encode into bytes + let mut encoded = Vec::new(); + into_writer(&item, &mut encoded).unwrap(); + assert_eq!(bytes, encoded); + + // Encode into value + assert_eq!(value, Value::serialized(&item).unwrap()); + } + + // Decode from bytes + match from_reader(&bytes[..]) { + Ok(x) if success => assert_eq!(item, x), + Ok(..) => panic!("unexpected success"), + Err(e) if success => Err(e).unwrap(), + Err(..) => (), + } + + // Decode from value + match value.deserialized() { + Ok(x) if success => assert_eq!(item, x), + Ok(..) => panic!("unexpected success"), + Err(e) if success => Err(e).unwrap(), + Err(..) => (), + } +} |