diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-12-09 04:13:14 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-12-09 04:13:14 +0000 |
commit | 10a08f8d05967956dff4f079ad8c25e560445784 (patch) | |
tree | 9f89c233c73e1a795c04460860b3848084b135b9 | |
parent | b842edaf69fc9febb2f9c06c92afe58408c186a8 (diff) | |
parent | e0a2b1ebbdf722605842885a7ff1395037df3e12 (diff) | |
download | arbitrary-10a08f8d05967956dff4f079ad8c25e560445784.tar.gz |
Snap for 9386131 from e0a2b1ebbdf722605842885a7ff1395037df3e12 to udc-d1-release
Change-Id: Ic9d532f61f5710bd01391171ffa8323af7d11c52
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | .github/workflows/rust.yml | 20 | ||||
-rw-r--r-- | Android.bp | 6 | ||||
-rw-r--r-- | CHANGELOG.md | 58 | ||||
-rw-r--r-- | Cargo.lock.saved | 56 | ||||
-rw-r--r-- | Cargo.toml | 8 | ||||
-rw-r--r-- | Cargo.toml.orig | 8 | ||||
-rw-r--r-- | METADATA | 12 | ||||
-rw-r--r-- | README.md | 33 | ||||
-rw-r--r-- | src/error.rs | 15 | ||||
-rw-r--r-- | src/lib.rs | 124 | ||||
-rw-r--r-- | src/unstructured.rs | 269 | ||||
-rw-r--r--[-rwxr-xr-x] | tests/derive.rs | 45 |
13 files changed, 545 insertions, 111 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index b93af77..b532b6d 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,6 @@ { "git": { - "sha1": "8e7b857f4f78b06920a36212ed2f392bc523c1f6" + "sha1": "48c4bfd52b167da94a5db906fe7dd2da7b92215e" }, "path_in_vcs": "" }
\ No newline at end of file diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 14c2d65..63b927f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -25,6 +25,14 @@ jobs: run: cargo test --verbose --all-features --all - name: Build Examples run: cargo build --examples --all-features --all + clippy: + name: Clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - run: rustup update + - run: rustup component add clippy + - run: cargo clippy --all-features --workspace -- -Dclippy::all rustfmt: name: Check rustfmt runs-on: ubuntu-latest @@ -33,3 +41,15 @@ jobs: - run: rustup update - run: rustup component add rustfmt --toolchain stable - run: cargo +stable fmt --all -- --check + fuzz: + name: Run `int_in_range` fuzz target + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - run: rustup update + - name: "Install nightly" + run: rustup toolchain install nightly && rustup default nightly + - name: "Install `cargo-fuzz`" + run: cargo install cargo-fuzz + - name: "Fuzz for 3 minutes" + run: cargo fuzz run int_in_range -- -max_total_time=$((3 * 60)) @@ -42,7 +42,7 @@ rust_test { host_supported: true, crate_name: "arbitrary", cargo_env_compat: true, - cargo_pkg_version: "1.1.3", + cargo_pkg_version: "1.2.0", srcs: ["src/lib.rs"], test_suites: ["general-tests"], auto_gen_config: true, @@ -62,7 +62,7 @@ rust_test { host_supported: true, crate_name: "arbitrary", cargo_env_compat: true, - cargo_pkg_version: "1.1.3", + cargo_pkg_version: "1.2.0", srcs: ["tests/derive.rs"], test_suites: ["general-tests"], auto_gen_config: true, @@ -85,7 +85,7 @@ rust_library_rlib { host_supported: true, crate_name: "arbitrary", cargo_env_compat: true, - cargo_pkg_version: "1.1.3", + cargo_pkg_version: "1.2.0", srcs: ["src/lib.rs"], edition: "2018", features: [ diff --git a/CHANGELOG.md b/CHANGELOG.md index 656329c..e0ed4d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,64 @@ Released YYYY-MM-DD. -------------------------------------------------------------------------------- +## 1.2.0 + +Released 2022-10-20. + +### Added + +* Support custom arbitrary implementation for fields on + derive. [#129](https://github.com/rust-fuzz/arbitrary/pull/129) + +-------------------------------------------------------------------------------- + +## 1.1.6 + +Released 2022-09-08. + +### Fixed + +* Fixed a potential panic due to an off-by-one error in the `Arbitrary` + implementation for `std::ops::Bound<T>`. + +-------------------------------------------------------------------------------- + +## 1.1.5 + +Released 2022-09-20. + +### Added + +* Implemented `Arbitrary` for `std::ops::Bound<T>`. + +### Fixed + +* Fixed a bug where `Unstructured::int_in_range` could return out-of-range + integers when generating arbitrary signed integers. + +-------------------------------------------------------------------------------- + +## 1.1.4 + +Released 2022-08-29. + +### Added + +* Implemented `Arbitrary` for `Rc<str>` and `Arc<str>` + +### Changed + +* Allow overriding the error type in `arbitrary::Result` +* The `Unstructured::arbitrary_loop` method will consume fewer bytes of input + now. + +### Fixed + +* Fixed a bug where `Unstructured::int_in_range` could return out-of-range + integers. + +-------------------------------------------------------------------------------- + ## 1.1.3 Released 2022-06-23. diff --git a/Cargo.lock.saved b/Cargo.lock.saved new file mode 100644 index 0000000..4a05331 --- /dev/null +++ b/Cargo.lock.saved @@ -0,0 +1,56 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "arbitrary" +version = "1.2.0" +dependencies = [ + "derive_arbitrary", +] + +[[package]] +name = "derive_arbitrary" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4903dff04948f22033ca30232ab8eca2c3fc4c913a8b6a34ee5199699814817f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" @@ -11,8 +11,9 @@ [package] edition = "2018" +rust-version = "1.63.0" name = "arbitrary" -version = "1.1.3" +version = "1.2.0" authors = [ "The Rust-Fuzz Project Developers", "Nick Fitzgerald <fitzgen@gmail.com>", @@ -31,6 +32,7 @@ keywords = [ categories = ["development-tools::testing"] license = "MIT OR Apache-2.0" repository = "https://github.com/rust-fuzz/arbitrary/" +resolver = "1" [[example]] name = "derive_enum" @@ -42,10 +44,8 @@ path = "./tests/derive.rs" required-features = ["derive"] [dependencies.derive_arbitrary] -version = "1.1.3" +version = "1.1.6" optional = true -[dev-dependencies] - [features] derive = ["derive_arbitrary"] diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 9d600f3..9a72d15 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "arbitrary" -version = "1.1.3" # Make sure this matches the derive crate version +version = "1.2.0" # Make sure this matches the derive crate version authors = [ "The Rust-Fuzz Project Developers", "Nick Fitzgerald <fitzgen@gmail.com>", @@ -17,11 +17,10 @@ description = "The trait for generating structured data from unstructured data" license = "MIT OR Apache-2.0" repository = "https://github.com/rust-fuzz/arbitrary/" documentation = "https://docs.rs/arbitrary/" +rust-version = "1.63.0" [dependencies] -derive_arbitrary = { version = "1.1.3", path = "./derive", optional = true } - -[dev-dependencies] +derive_arbitrary = { version = "1.1.6", path = "./derive", optional = true } [features] # Turn this feature on to enable support for `#[derive(Arbitrary)]`. @@ -37,3 +36,4 @@ path = "./tests/derive.rs" required-features = ["derive"] [workspace] +members = ["./fuzz"] @@ -1,3 +1,7 @@ +# This project was upgraded with external_updater. +# Usage: tools/external_updater/updater.sh update rust/crates/arbitrary +# For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md + name: "arbitrary" description: "The trait for generating structured data from unstructured data" third_party { @@ -7,13 +11,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/arbitrary/arbitrary-1.1.3.crate" + value: "https://static.crates.io/crates/arbitrary/arbitrary-1.2.0.crate" } - version: "1.1.3" + version: "1.2.0" license_type: NOTICE last_upgrade_date { year: 2022 - month: 6 - day: 27 + month: 12 + day: 5 } } @@ -61,6 +61,39 @@ pub struct Rgb { } ``` +#### Customizing single fields + +This can be particular handy if your structure uses a type that does not implement `Arbitrary` or you want to have more customization for particular fields. + +```rust +#[derive(Arbitrary)] +pub struct Rgba { + // set `r` to Default::default() + #[arbitrary(default)] + pub r: u8, + + // set `g` to 255 + #[arbitrary(value = 255)] + pub g: u8, + + // Generate `b` with a custom function of type + // + // fn(&mut Unstructured) -> arbitrary::Result<T> + // + // where `T` is the field's type. + #[arbitrary(with = arbitrary_b)] + pub b: u8, + + // Generate `a` with a custom closure (shortuct to avoid a custom funciton) + #[arbitrary(with = |u: &mut Unstructured| u.int_in_range(0..=64))] + pub a: u8, +} + +fn arbitrary_b(u: &mut Unstructured) -> arbitrary::Result<u8> { + u.int_in_range(64..=128) +} +``` + ### Implementing `Arbitrary` By Hand Alternatively, you can write an `Arbitrary` implementation by hand: diff --git a/src/error.rs b/src/error.rs index f590c12..1d3df2a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -37,4 +37,17 @@ impl error::Error for Error {} /// A `Result` with the error type fixed as `arbitrary::Error`. /// /// Either an `Ok(T)` or `Err(arbitrary::Error)`. -pub type Result<T> = std::result::Result<T, Error>; +pub type Result<T, E = Error> = std::result::Result<T, E>; + +#[cfg(test)] +mod tests { + // Often people will import our custom `Result` type because 99.9% of + // results in a file will be `arbitrary::Result` but then have that one last + // 0.1% that want to have a custom error type. Don't make them prefix that + // 0.1% as `std::result::Result`; instead, let `arbitrary::Result` have an + // overridable error type. + #[test] + fn can_use_custom_error_types_with_result() -> super::Result<(), String> { + Ok(()) + } +} @@ -34,6 +34,7 @@ pub use unstructured::Unstructured; pub mod size_hint; +use core::array; use core::cell::{Cell, RefCell, UnsafeCell}; use core::iter; use core::mem; @@ -47,6 +48,7 @@ use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedL use std::ffi::{CString, OsString}; use std::hash::BuildHasher; use std::net::{Ipv4Addr, Ipv6Addr}; +use std::ops::Bound; use std::path::PathBuf; use std::rc::Rc; use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize}; @@ -138,7 +140,7 @@ pub trait Arbitrary<'a>: Sized { /// perhaps given to you by a fuzzer like AFL or libFuzzer. You wrap this /// raw data in an `Unstructured`, and then you can call `<MyType as /// Arbitrary>::arbitrary` to construct an arbitrary instance of `MyType` - /// from that unstuctured data. + /// from that unstructured data. /// /// Implementations may return an error if there is not enough data to /// construct a full instance of `Self`, or they may fill out the rest of @@ -609,27 +611,6 @@ impl<T, const N: usize> Drop for ArrayGuard<T, N> { } } -fn create_array<F, T, const N: usize>(mut cb: F) -> [T; N] -where - F: FnMut(usize) -> T, -{ - let mut array: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninit(); - let array_ptr = array.as_mut_ptr(); - let dst = array_ptr as _; - let mut guard: ArrayGuard<T, N> = ArrayGuard { - dst, - initialized: 0, - }; - unsafe { - for (idx, value_ptr) in (&mut *array.as_mut_ptr()).iter_mut().enumerate() { - core::ptr::write(value_ptr, cb(idx)); - guard.initialized += 1; - } - mem::forget(guard); - array.assume_init() - } -} - fn try_create_array<F, T, const N: usize>(mut cb: F) -> Result<[T; N]> where F: FnMut(usize) -> Result<T>, @@ -642,7 +623,7 @@ where initialized: 0, }; unsafe { - for (idx, value_ptr) in (&mut *array.as_mut_ptr()).iter_mut().enumerate() { + for (idx, value_ptr) in (*array.as_mut_ptr()).iter_mut().enumerate() { core::ptr::write(value_ptr, cb(idx)?); guard.initialized += 1; } @@ -671,7 +652,7 @@ where #[inline] fn size_hint(d: usize) -> (usize, Option<usize>) { - crate::size_hint::and_all(&create_array::<_, (usize, Option<usize>), N>(|_| { + crate::size_hint::and_all(&array::from_fn::<_, N, _>(|_| { <T as Arbitrary>::size_hint(d) })) } @@ -738,6 +719,25 @@ impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BTreeSet<A> { } } +impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Bound<A> { + fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { + match u.int_in_range::<u8>(0..=2)? { + 0 => Ok(Bound::Included(A::arbitrary(u)?)), + 1 => Ok(Bound::Excluded(A::arbitrary(u)?)), + 2 => Ok(Bound::Unbounded), + _ => unreachable!(), + } + } + + #[inline] + fn size_hint(depth: usize) -> (usize, Option<usize>) { + size_hint::or( + size_hint::and((1, Some(1)), A::size_hint(depth)), + (1, Some(1)), + ) + } +} + impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> { fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { u.arbitrary_iter()?.collect() @@ -837,7 +837,7 @@ where impl<'a> Arbitrary<'a> for &'a str { fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { let size = u.arbitrary_len::<u8>()?; - match str::from_utf8(&u.peek_bytes(size).unwrap()) { + match str::from_utf8(u.peek_bytes(size).unwrap()) { Ok(s) => { u.bytes(size).unwrap(); Ok(s) @@ -856,9 +856,7 @@ impl<'a> Arbitrary<'a> for &'a str { fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { let bytes = u.take_rest(); - str::from_utf8(bytes) - .map_err(|_| Error::IncorrectFormat) - .map(Into::into) + str::from_utf8(bytes).map_err(|_| Error::IncorrectFormat) } #[inline] @@ -975,6 +973,17 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> { } } +impl<'a> Arbitrary<'a> for Arc<str> { + fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { + <&str as Arbitrary>::arbitrary(u).map(Into::into) + } + + #[inline] + fn size_hint(depth: usize) -> (usize, Option<usize>) { + <&str as Arbitrary>::size_hint(depth) + } +} + impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> { fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { Arbitrary::arbitrary(u).map(Self::new) @@ -986,6 +995,17 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> { } } +impl<'a> Arbitrary<'a> for Rc<str> { + fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { + <&str as Arbitrary>::arbitrary(u).map(Into::into) + } + + #[inline] + fn size_hint(depth: usize) -> (usize, Option<usize>) { + <&str as Arbitrary>::size_hint(depth) + } +} + impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Cell<A> { fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { Arbitrary::arbitrary(u).map(Self::new) @@ -1272,3 +1292,51 @@ mod test { assert_eq!((1, None), <(u8, Vec<u8>) as Arbitrary>::size_hint(0)); } } + +/// Multiple conflicting arbitrary attributes are used on the same field: +/// ```compile_fail +/// #[derive(::arbitrary::Arbitrary)] +/// struct Point { +/// #[arbitrary(value = 2)] +/// #[arbitrary(value = 2)] +/// x: i32, +/// } +/// ``` +/// +/// An unknown attribute: +/// ```compile_fail +/// #[derive(::arbitrary::Arbitrary)] +/// struct Point { +/// #[arbitrary(unknown_attr)] +/// x: i32, +/// } +/// ``` +/// +/// An unknown attribute with a value: +/// ```compile_fail +/// #[derive(::arbitrary::Arbitrary)] +/// struct Point { +/// #[arbitrary(unknown_attr = 13)] +/// x: i32, +/// } +/// ``` +/// +/// `value` without RHS: +/// ```compile_fail +/// #[derive(::arbitrary::Arbitrary)] +/// struct Point { +/// #[arbitrary(value)] +/// x: i32, +/// } +/// ``` +/// +/// `with` without RHS: +/// ```compile_fail +/// #[derive(::arbitrary::Arbitrary)] +/// struct Point { +/// #[arbitrary(with)] +/// x: i32, +/// } +/// ``` +#[cfg(all(doctest, feature = "derive"))] +pub struct CompileFailTests; diff --git a/src/unstructured.rs b/src/unstructured.rs index 07ba932..0bfdff2 100644 --- a/src/unstructured.rs +++ b/src/unstructured.rs @@ -216,7 +216,7 @@ impl<'a> Unstructured<'a> { { let byte_size = self.arbitrary_byte_size()?; let (lower, upper) = <ElementType as Arbitrary>::size_hint(0); - let elem_size = upper.unwrap_or_else(|| lower * 2); + let elem_size = upper.unwrap_or(lower * 2); let elem_size = std::cmp::max(1, elem_size); Ok(byte_size / elem_size) } @@ -274,7 +274,7 @@ impl<'a> Unstructured<'a> { /// /// # Panics /// - /// Panics if `range.start >= range.end`. That is, the given range must be + /// Panics if `range.start > range.end`. That is, the given range must be /// non-empty. /// /// # Example @@ -306,8 +306,8 @@ impl<'a> Unstructured<'a> { where T: Int, { - let start = range.start(); - let end = range.end(); + let start = *range.start(); + let end = *range.end(); assert!( start <= end, "`arbitrary::Unstructured::int_in_range` requires a non-empty range" @@ -316,30 +316,59 @@ impl<'a> Unstructured<'a> { // When there is only one possible choice, don't waste any entropy from // the underlying data. if start == end { - return Ok((*start, 0)); + return Ok((start, 0)); } - let range: T::Widest = end.as_widest() - start.as_widest(); - let mut result = T::Widest::ZERO; - let mut offset: usize = 0; + // From here on out we work with the unsigned representation. All of the + // operations performed below work out just as well whether or not `T` + // is a signed or unsigned integer. + let start = start.to_unsigned(); + let end = end.to_unsigned(); + + let delta = end.wrapping_sub(start); + debug_assert_ne!(delta, T::Unsigned::ZERO); + + // Compute an arbitrary integer offset from the start of the range. We + // do this by consuming `size_of(T)` bytes from the input to create an + // arbitrary integer and then clamping that int into our range bounds + // with a modulo operation. + let mut arbitrary_int = T::Unsigned::ZERO; + let mut bytes_consumed: usize = 0; - while offset < mem::size_of::<T>() - && (range >> T::Widest::from_usize(offset * 8)) > T::Widest::ZERO + while (bytes_consumed < mem::size_of::<T>()) + && (delta >> T::Unsigned::from_usize(bytes_consumed * 8)) > T::Unsigned::ZERO { - let byte = bytes.next().ok_or(Error::NotEnoughData)?; - result = (result << 8) | T::Widest::from_u8(byte); - offset += 1; - } + let byte = match bytes.next() { + None => break, + Some(b) => b, + }; + bytes_consumed += 1; - // Avoid division by zero. - if let Some(range) = range.checked_add(T::Widest::ONE) { - result = result % range; + // Combine this byte into our arbitrary integer, but avoid + // overflowing the shift for `u8` and `i8`. + arbitrary_int = if mem::size_of::<T>() == 1 { + T::Unsigned::from_u8(byte) + } else { + (arbitrary_int << 8) | T::Unsigned::from_u8(byte) + }; } - Ok(( - T::from_widest(start.as_widest().wrapping_add(result)), - offset, - )) + let offset = if delta == T::Unsigned::MAX { + arbitrary_int + } else { + arbitrary_int % (delta.checked_add(T::Unsigned::ONE).unwrap()) + }; + + // Finally, we add `start` to our offset from `start` to get the result + // actual value within the range. + let result = start.wrapping_add(offset); + + // And convert back to our maybe-signed representation. + let result = T::from_unsigned(result); + debug_assert!(*range.start() <= result); + debug_assert!(result <= *range.end()); + + Ok((result, bytes_consumed)) } /// Choose one of the given choices. @@ -567,7 +596,7 @@ impl<'a> Unstructured<'a> { /// assert_eq!(remaining, [1, 2, 3]); /// ``` pub fn take_rest(mut self) -> &'a [u8] { - mem::replace(&mut self.data, &[]) + mem::take(&mut self.data) } /// Provide an iterator over elements for constructing a collection @@ -674,20 +703,8 @@ impl<'a> Unstructured<'a> { ) -> Result<()> { let min = min.unwrap_or(0); let max = max.unwrap_or(u32::MAX); - assert!(min <= max); - for _ in 0..min { - match f(self)? { - ControlFlow::Continue(_) => continue, - ControlFlow::Break(_) => return Ok(()), - } - } - - for _ in 0..(max - min) { - let keep_going = self.arbitrary().unwrap_or(false); - if !keep_going { - break; - } + for _ in 0..self.int_in_range(min..=max)? { match f(self)? { ControlFlow::Continue(_) => continue, ControlFlow::Break(_) => break, @@ -761,6 +778,7 @@ impl<'a, ElementType: Arbitrary<'a>> Iterator for ArbitraryTakeRestIter<'a, Elem /// Don't implement this trait yourself. pub trait Int: Copy + + std::fmt::Debug + PartialOrd + Ord + ops::Sub<Self, Output = Self> @@ -770,7 +788,7 @@ pub trait Int: + ops::BitOr<Self, Output = Self> { #[doc(hidden)] - type Widest: Int; + type Unsigned: Int; #[doc(hidden)] const ZERO: Self; @@ -779,10 +797,7 @@ pub trait Int: const ONE: Self; #[doc(hidden)] - fn as_widest(self) -> Self::Widest; - - #[doc(hidden)] - fn from_widest(w: Self::Widest) -> Self; + const MAX: Self; #[doc(hidden)] fn from_u8(b: u8) -> Self; @@ -795,26 +810,28 @@ pub trait Int: #[doc(hidden)] fn wrapping_add(self, rhs: Self) -> Self; + + #[doc(hidden)] + fn wrapping_sub(self, rhs: Self) -> Self; + + #[doc(hidden)] + fn to_unsigned(self) -> Self::Unsigned; + + #[doc(hidden)] + fn from_unsigned(unsigned: Self::Unsigned) -> Self; } macro_rules! impl_int { - ( $( $ty:ty : $widest:ty ; )* ) => { + ( $( $ty:ty : $unsigned_ty: ty ; )* ) => { $( impl Int for $ty { - type Widest = $widest; + type Unsigned = $unsigned_ty; const ZERO: Self = 0; const ONE: Self = 1; - fn as_widest(self) -> Self::Widest { - self as $widest - } - - fn from_widest(w: Self::Widest) -> Self { - let x = <$ty>::max_value().as_widest(); - (w % x) as Self - } + const MAX: Self = Self::MAX; fn from_u8(b: u8) -> Self { b as Self @@ -831,24 +848,36 @@ macro_rules! impl_int { fn wrapping_add(self, rhs: Self) -> Self { <$ty>::wrapping_add(self, rhs) } + + fn wrapping_sub(self, rhs: Self) -> Self { + <$ty>::wrapping_sub(self, rhs) + } + + fn to_unsigned(self) -> Self::Unsigned { + self as $unsigned_ty + } + + fn from_unsigned(unsigned: $unsigned_ty) -> Self { + unsigned as Self + } } )* } } impl_int! { - u8: u128; - u16: u128; - u32: u128; - u64: u128; + u8: u8; + u16: u16; + u32: u32; + u64: u64; u128: u128; - usize: u128; - i8: i128; - i16: i128; - i32: i128; - i64: i128; - i128: i128; - isize: i128; + usize: usize; + i8: u8; + i16: u16; + i32: u32; + i64: u64; + i128: u128; + isize: usize; } #[cfg(test)] @@ -882,13 +911,121 @@ mod tests { #[test] fn int_in_range_uses_minimal_amount_of_bytes() { - let mut u = Unstructured::new(&[1]); - u.int_in_range::<u8>(0..=u8::MAX).unwrap(); + let mut u = Unstructured::new(&[1, 2]); + assert_eq!(1, u.int_in_range::<u8>(0..=u8::MAX).unwrap()); + assert_eq!(u.len(), 1); - let mut u = Unstructured::new(&[1]); - u.int_in_range::<u32>(0..=u8::MAX as u32).unwrap(); + let mut u = Unstructured::new(&[1, 2]); + assert_eq!(1, u.int_in_range::<u32>(0..=u8::MAX as u32).unwrap()); + assert_eq!(u.len(), 1); let mut u = Unstructured::new(&[1]); - u.int_in_range::<u32>(0..=u8::MAX as u32 + 1).unwrap_err(); + assert_eq!(1, u.int_in_range::<u32>(0..=u8::MAX as u32 + 1).unwrap()); + assert!(u.is_empty()); + } + + #[test] + fn int_in_range_in_bounds() { + for input in u8::MIN..=u8::MAX { + let input = [input]; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(1..=u8::MAX).unwrap(); + assert_ne!(x, 0); + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(0..=u8::MAX - 1).unwrap(); + assert_ne!(x, u8::MAX); + } + } + + #[test] + fn int_in_range_covers_unsigned_range() { + // Test that we generate all values within the range given to + // `int_in_range`. + + let mut full = [false; u8::MAX as usize + 1]; + let mut no_zero = [false; u8::MAX as usize]; + let mut no_max = [false; u8::MAX as usize]; + let mut narrow = [false; 10]; + + for input in u8::MIN..=u8::MAX { + let input = [input]; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(0..=u8::MAX).unwrap(); + full[x as usize] = true; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(1..=u8::MAX).unwrap(); + no_zero[x as usize - 1] = true; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(0..=u8::MAX - 1).unwrap(); + no_max[x as usize] = true; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(100..=109).unwrap(); + narrow[x as usize - 100] = true; + } + + for (i, covered) in full.iter().enumerate() { + assert!(covered, "full[{}] should have been generated", i); + } + for (i, covered) in no_zero.iter().enumerate() { + assert!(covered, "no_zero[{}] should have been generated", i); + } + for (i, covered) in no_max.iter().enumerate() { + assert!(covered, "no_max[{}] should have been generated", i); + } + for (i, covered) in narrow.iter().enumerate() { + assert!(covered, "narrow[{}] should have been generated", i); + } + } + + #[test] + fn int_in_range_covers_signed_range() { + // Test that we generate all values within the range given to + // `int_in_range`. + + let mut full = [false; u8::MAX as usize + 1]; + let mut no_min = [false; u8::MAX as usize]; + let mut no_max = [false; u8::MAX as usize]; + let mut narrow = [false; 21]; + + let abs_i8_min: isize = 128; + + for input in 0..=u8::MAX { + let input = [input]; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(i8::MIN..=i8::MAX).unwrap(); + full[(x as isize + abs_i8_min) as usize] = true; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(i8::MIN + 1..=i8::MAX).unwrap(); + no_min[(x as isize + abs_i8_min - 1) as usize] = true; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(i8::MIN..=i8::MAX - 1).unwrap(); + no_max[(x as isize + abs_i8_min) as usize] = true; + + let mut u = Unstructured::new(&input); + let x = u.int_in_range(-10..=10).unwrap(); + narrow[(x as isize + 10) as usize] = true; + } + + for (i, covered) in full.iter().enumerate() { + assert!(covered, "full[{}] should have been generated", i); + } + for (i, covered) in no_min.iter().enumerate() { + assert!(covered, "no_min[{}] should have been generated", i); + } + for (i, covered) in no_max.iter().enumerate() { + assert!(covered, "no_max[{}] should have been generated", i); + } + for (i, covered) in narrow.iter().enumerate() { + assert!(covered, "narrow[{}] should have been generated", i); + } } } diff --git a/tests/derive.rs b/tests/derive.rs index 2d666f6..f29d227 100755..100644 --- a/tests/derive.rs +++ b/tests/derive.rs @@ -231,3 +231,48 @@ fn recursive_and_empty_input() { let _ = Nat5::arbitrary(&mut Unstructured::new(&[])); } + +#[test] +fn test_field_attributes() { + // A type that DOES NOT implement Arbitrary + #[derive(Debug)] + struct Weight(u8); + + #[derive(Debug, Arbitrary)] + struct Parcel { + #[arbitrary(with = arbitrary_weight)] + weight: Weight, + + #[arbitrary(default)] + width: u8, + + #[arbitrary(value = 2 + 2)] + length: u8, + + height: u8, + + #[arbitrary(with = |u: &mut Unstructured| u.int_in_range(0..=100))] + price: u8, + } + + fn arbitrary_weight(u: &mut Unstructured) -> arbitrary::Result<Weight> { + u.int_in_range(45..=56).map(Weight) + } + + let parcel: Parcel = arbitrary_from(&[6, 199, 17]); + + // 45 + 6 = 51 + assert_eq!(parcel.weight.0, 51); + + // u8::default() + assert_eq!(parcel.width, 0); + + // 2 + 2 = 4 + assert_eq!(parcel.length, 4); + + // 199 is the 2nd byte used by arbitrary + assert_eq!(parcel.height, 199); + + // 17 is the 3rd byte used by arbitrary + assert_eq!(parcel.price, 17); +} |