Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions crates/iota-sdk-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,20 @@ pub trait FromMnemonic {
Self: Sized;
}

#[cfg(test)]
#[cfg(all(
test,
feature = "mnemonic",
feature = "ed25519",
feature = "secp256k1",
feature = "secp256r1",
feature = "bech32"
))]
mod tests {
use super::*;
use crate::{
ed25519::Ed25519PrivateKey, secp256k1::Secp256k1PrivateKey, secp256r1::Secp256r1PrivateKey,
};

#[cfg(feature = "mnemonic")]
#[test]
fn test_mnemonics_ed25519() {
const TEST_CASES: [[&str; 3]; 3] = [
Expand Down Expand Up @@ -390,7 +396,6 @@ mod tests {
}
}

#[cfg(feature = "mnemonic")]
#[test]
fn test_mnemonics_secp256k1() {
const TEST_CASES: [[&str; 3]; 3] = [
Expand Down Expand Up @@ -418,7 +423,6 @@ mod tests {
}
}

#[cfg(feature = "mnemonic")]
#[test]
fn test_mnemonics_secp256r1() {
const TEST_CASES: [[&str; 3]; 3] = [
Expand Down
8 changes: 7 additions & 1 deletion crates/iota-sdk-crypto/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,13 @@ mod keypair {
}
}

#[cfg(test)]
#[cfg(all(
test,
feature = "pem",
feature = "ed25519",
feature = "secp256k1",
feature = "secp256r1"
))]
mod tests {
use test_strategy::proptest;

Expand Down
2 changes: 1 addition & 1 deletion crates/iota-sdk-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ serde = [
schemars = ["serde", "dep:schemars", "dep:serde_json"]
rand = ["dep:rand_core"]
hash = ["dep:blake2"]
proptest = ["dep:proptest", "dep:test-strategy", "serde"]
proptest = ["dep:proptest", "dep:test-strategy", "serde", "schemars"]

[dependencies]
base64ct = { workspace = true, features = ["alloc"] }
Expand Down
24 changes: 15 additions & 9 deletions crates/iota-sdk-types/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,24 @@ impl schemars::JsonSchema for Address {

#[cfg(test)]
mod tests {
use test_strategy::proptest;
#[cfg(feature = "proptest")]
mod proptests {
use test_strategy::proptest;

use super::super::Address;

#[proptest]
fn roundtrip_display_fromstr(address: Address) {
let s = address.to_string();
let a = s.parse::<Address>().unwrap();
assert_eq!(address, a);
}
}

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

use super::*;
use super::Address;

#[test]
fn hex_parsing() {
Expand All @@ -308,11 +321,4 @@ mod tests {
let a: Address = serde_json::from_str("\"0x2\"").unwrap();
println!("{a}");
}

#[proptest]
fn roundtrip_display_fromstr(address: Address) {
let s = address.to_string();
let a = s.parse::<Address>().unwrap();
assert_eq!(address, a);
}
}
2 changes: 1 addition & 1 deletion crates/iota-sdk-types/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ macro_rules! impl_base64_helper {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "proptest"))]
mod $test_module {
use test_strategy::proptest;

Expand Down
1 change: 1 addition & 0 deletions crates/iota-sdk-types/src/crypto/passkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ impl proptest::arbitrary::Arbitrary for PasskeyAuthenticator {
}
}

#[cfg(feature = "serde")]
#[cfg(test)]
mod tests {
use crate::UserSignature;
Expand Down
2 changes: 2 additions & 0 deletions crates/iota-sdk-types/src/crypto/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,13 +822,15 @@ mod serialization {
#[cfg(test)]
mod tests {
use base64ct::{Base64, Encoding};
#[cfg(feature = "proptest")]
use test_strategy::proptest;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

use super::*;

#[proptest]
#[cfg(feature = "proptest")]
fn roundtrip_signature_scheme(scheme: SignatureScheme) {
assert_eq!(Ok(scheme), SignatureScheme::from_byte(scheme.to_u8()));
}
Expand Down
62 changes: 34 additions & 28 deletions crates/iota-sdk-types/src/crypto/zklogin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,41 @@ impl std::str::FromStr for Bn254FieldElement {

#[cfg(test)]
mod tests {
use std::str::FromStr;
#[cfg(feature = "proptest")]
mod proptests {
use std::str::FromStr;

use num_bigint::BigUint;
use proptest::prelude::*;
use test_strategy::proptest;

use super::super::Bn254FieldElement;

#[proptest]
fn dont_crash_on_large_inputs(
#[strategy(proptest::collection::vec(any::<u8>(), 33..1024))] bytes: Vec<u8>,
) {
let big_int = BigUint::from_bytes_be(&bytes);
let radix10 = big_int.to_str_radix(10);

// doesn't crash
let _ = Bn254FieldElement::from_str(&radix10);
}

#[proptest]
fn valid_address_seeds(
#[strategy(proptest::collection::vec(any::<u8>(), 1..=32))] bytes: Vec<u8>,
) {
let big_int = BigUint::from_bytes_be(&bytes);
let radix10 = big_int.to_str_radix(10);

let seed = Bn254FieldElement::from_str(&radix10).unwrap();
assert_eq!(radix10, seed.to_string());
// Ensure unpadded doesn't crash
seed.unpadded();
}
}

use num_bigint::BigUint;
use proptest::prelude::*;
use test_strategy::proptest;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

Expand All @@ -672,30 +702,6 @@ mod tests {
seed.0[0] = 0;
assert_eq!(seed.unpadded(), [1; 31].as_slice());
}

#[proptest]
fn dont_crash_on_large_inputs(
#[strategy(proptest::collection::vec(any::<u8>(), 33..1024))] bytes: Vec<u8>,
) {
let big_int = BigUint::from_bytes_be(&bytes);
let radix10 = big_int.to_str_radix(10);

// doesn't crash
let _ = Bn254FieldElement::from_str(&radix10);
}

#[proptest]
fn valid_address_seeds(
#[strategy(proptest::collection::vec(any::<u8>(), 1..=32))] bytes: Vec<u8>,
) {
let big_int = BigUint::from_bytes_be(&bytes);
let radix10 = big_int.to_str_radix(10);

let seed = Bn254FieldElement::from_str(&radix10).unwrap();
assert_eq!(radix10, seed.to_string());
// Ensure unpadded doesn't crash
seed.unpadded();
}
}

#[cfg(feature = "serde")]
Expand Down
2 changes: 1 addition & 1 deletion crates/iota-sdk-types/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl std::error::Error for DigestParseError {}
// serialized
pub type SigningDigest = [u8; Digest::LENGTH];

#[cfg(test)]
#[cfg(all(test, feature = "proptest"))]
mod tests {
use test_strategy::proptest;

Expand Down
3 changes: 1 addition & 2 deletions crates/iota-sdk-types/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,14 @@ impl std::fmt::Display for GasCostSummary {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "serde"))]
mod tests {
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

use super::*;

#[test]
#[cfg(feature = "serde")]
fn formats() {
let actual = GasCostSummary {
computation_cost: 42,
Expand Down
2 changes: 1 addition & 1 deletion crates/iota-sdk-types/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ impl crate::ObjectId {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "proptest"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not asking you to change but I guess I would have preferred putting them on individual tests, even if redundant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why though if all the tests inside need it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, you also need the features for the individual imports then if you don't want the warnings for unused imports

mod tests {
use test_strategy::proptest;

Expand Down
3 changes: 2 additions & 1 deletion crates/iota-sdk-types/src/move_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod serialization {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "serde"))]
mod tests {
use super::*;

Expand All @@ -162,6 +162,7 @@ mod tests {
assert_eq!(new_json, PACKAGE);
}

#[cfg(feature = "hash")]
#[test]
fn test_digest() {
let json_package: MovePackageData = serde_json::from_str(PACKAGE).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/iota-sdk-types/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ pub struct ChangeEpochV2 {
/// write out the modules below. Modules are provided with the version they
/// will be upgraded to, their modules in serialized form (which include
/// their package ID), and a list of their transitive dependencies.
#[cfg_attr(test, any(proptest::collection::size_range(0..=2).lift()))]
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=2).lift()))]
pub system_packages: Vec<SystemPackage>,
}

Expand Down Expand Up @@ -789,7 +789,7 @@ pub struct ChangeEpochV3 {
/// write out the modules below. Modules are provided with the version they
/// will be upgraded to, their modules in serialized form (which include
/// their package ID), and a list of their transitive dependencies.
#[cfg_attr(test, any(proptest::collection::size_range(0..=2).lift()))]
#[cfg_attr(all(test, feature = "proptest"), any(proptest::collection::size_range(0..=2).lift()))]
pub system_packages: Vec<SystemPackage>,
/// Vector of active validator indices eligible to take part in committee
/// selection because they support the new, target protocol version.
Expand Down
68 changes: 37 additions & 31 deletions crates/iota-sdk-types/src/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,44 @@ const ASSERT_ENDIANNESS: () = {

#[cfg(test)]
mod tests {
use std::str::FromStr;
#[cfg(feature = "proptest")]
mod proptests {
use std::str::FromStr;

use num_bigint::BigUint;
use proptest::prelude::*;
use test_strategy::proptest;

use super::super::U256;

#[proptest]
fn dont_crash_on_large_inputs(
#[strategy(proptest::collection::vec(any::<u8>(), 33..1024))] bytes: Vec<u8>,
) {
let big_int = BigUint::from_bytes_be(&bytes);
let radix10 = big_int.to_str_radix(10);

// doesn't crash
let _ = U256::from_str_radix(&radix10, 10);
}

#[proptest]
fn valid_u256_strings(
#[strategy(proptest::collection::vec(any::<u8>(), 1..=32))] bytes: Vec<u8>,
) {
let big_int = BigUint::from_bytes_be(&bytes);
let radix10 = big_int.to_str_radix(10);

let u256 = U256::from_str_radix(&radix10, 10).unwrap();

assert_eq!(radix10, u256.to_str_radix(10));

let from_str = U256::from_str(&radix10).unwrap();
assert_eq!(from_str, u256);
assert_eq!(radix10, from_str.to_string());
}
}

use num_bigint::BigUint;
use proptest::prelude::*;
use test_strategy::proptest;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

Expand Down Expand Up @@ -107,31 +140,4 @@ mod tests {
// From big endian
assert_eq!(one_platform, U256::from_be(U256::from_digits(one_be)));
}

#[proptest]
fn dont_crash_on_large_inputs(
#[strategy(proptest::collection::vec(any::<u8>(), 33..1024))] bytes: Vec<u8>,
) {
let big_int = BigUint::from_bytes_be(&bytes);
let radix10 = big_int.to_str_radix(10);

// doesn't crash
let _ = U256::from_str_radix(&radix10, 10);
}

#[proptest]
fn valid_u256_strings(
#[strategy(proptest::collection::vec(any::<u8>(), 1..=32))] bytes: Vec<u8>,
) {
let big_int = BigUint::from_bytes_be(&bytes);
let radix10 = big_int.to_str_radix(10);

let u256 = U256::from_str_radix(&radix10, 10).unwrap();

assert_eq!(radix10, u256.to_str_radix(10));

let from_str = U256::from_str(&radix10).unwrap();
assert_eq!(from_str, u256);
assert_eq!(radix10, from_str.to_string());
}
}
3 changes: 1 addition & 2 deletions crates/iota-sdk-types/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,13 @@ pub struct ValidatorSignature {
pub signature: Bls12381Signature,
}

#[cfg(test)]
#[cfg(all(test, feature = "serde"))]
mod tests {
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

use super::*;

#[cfg(feature = "serde")]
#[test]
fn aggregated_signature_fixture() {
use base64ct::{Base64, Encoding};
Expand Down