From 133e97df611f116b918729a08b624bf7c8c38e18 Mon Sep 17 00:00:00 2001 From: Jeremy Rubin Date: Tue, 27 Apr 2021 21:46:43 -0700 Subject: [PATCH] Add extended_tests directory to test schemars only if not using 1.29 rust --- .github/workflows/rust.yml | 1 + contrib/test.sh | 4 + extended_tests/schemars/Cargo.toml | 17 +++ extended_tests/schemars/src/main.rs | 194 ++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+) create mode 100644 extended_tests/schemars/Cargo.toml create mode 100644 extended_tests/schemars/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 578a499..37a0ebe 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -78,5 +78,6 @@ jobs: - name: Running cargo env: DO_FEATURE_MATRIX: true + DO_SCHEMARS_TESTS: ${{matrix.rust != '1.29.0'}} run: ./contrib/test.sh diff --git a/contrib/test.sh b/contrib/test.sh index 91e98e7..8267a3b 100755 --- a/contrib/test.sh +++ b/contrib/test.sh @@ -36,6 +36,10 @@ if [ "$DO_FEATURE_MATRIX" = true ]; then cargo test --all --features="serde-std" fi +if [ "$DO_SCHEMARS_TESTS" = true ]; then + (cd extended_tests/schemars && cargo test) +fi + # Docs if [ "$DO_DOCS" = true ]; then cargo doc --all --features="$FEATURES" diff --git a/extended_tests/schemars/Cargo.toml b/extended_tests/schemars/Cargo.toml new file mode 100644 index 0000000..c860e76 --- /dev/null +++ b/extended_tests/schemars/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "schemars" +version = "0.1.0" +authors = ["Jeremy Rubin "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies.bitcoin_hashes] +path = "../.." +features = ['schemars', 'serde'] +[dependencies] +jsonschema-valid = "^0.4.0" +serde = { version = "1.0", default-features = false} +schemars = { version = "0.8.0"} +serde_test = "1.0" +serde_json = "1.0" diff --git a/extended_tests/schemars/src/main.rs b/extended_tests/schemars/src/main.rs new file mode 100644 index 0000000..bae37ba --- /dev/null +++ b/extended_tests/schemars/src/main.rs @@ -0,0 +1,194 @@ +fn main() {} +#[cfg(test)] +mod tests { + use bitcoin_hashes::*; + #[test] + fn it_works() {} + + #[test] + fn hash160() { + static HASH_BYTES: [u8; 20] = [ + 0x13, 0x20, 0x72, 0xdf, 0x69, 0x09, 0x33, 0x83, 0x5e, 0xb8, 0xb6, 0xad, 0x0b, 0x77, + 0xe7, 0xb6, 0xf1, 0x4a, 0xca, 0xd7, + ]; + + let hash = hash160::Hash::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(hash160::Hash); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } + + #[test] + fn HMAC_SHA512() { + static HASH_BYTES: [u8; 64] = [ + 0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21, 0x11, 0x3c, 0x52, 0xff, 0x18, 0x2a, + 0x1b, 0x8e, 0x0a, 0x19, 0x57, 0x54, 0xaa, 0x52, 0x7f, 0xcd, 0x00, 0xa4, 0x11, 0x62, + 0x0b, 0x46, 0xf2, 0x0f, 0xff, 0xfb, 0x80, 0x88, 0xcc, 0xf8, 0x54, 0x97, 0x12, 0x1a, + 0xd4, 0x49, 0x9e, 0x08, 0x45, 0xb8, 0x76, 0xf6, 0xdd, 0x66, 0x40, 0x08, 0x8a, 0x2f, + 0x0b, 0x2d, 0x8a, 0x60, 0x0b, 0xdf, 0x4c, 0x0c, + ]; + + let hash = Hmac::::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(Hmac::); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } + + #[test] + fn ripemd160() { + static HASH_BYTES: [u8; 20] = [ + 0x13, 0x20, 0x72, 0xdf, 0x69, 0x09, 0x33, 0x83, 0x5e, 0xb8, 0xb6, 0xad, 0x0b, 0x77, + 0xe7, 0xb6, 0xf1, 0x4a, 0xca, 0xd7, + ]; + + let hash = ripemd160::Hash::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(ripemd160::Hash); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } + + #[test] + fn sha1() { + static HASH_BYTES: [u8; 20] = [ + 0x13, 0x20, 0x72, 0xdf, 0x69, 0x09, 0x33, 0x83, 0x5e, 0xb8, 0xb6, 0xad, 0x0b, 0x77, + 0xe7, 0xb6, 0xf1, 0x4a, 0xca, 0xd7, + ]; + + let hash = sha1::Hash::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(sha1::Hash); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } + + #[test] + fn sha256d() { + static HASH_BYTES: [u8; 32] = [ + 0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, 0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6, + 0x3d, 0x97, 0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2, 0xb7, 0x65, 0x44, 0x8c, + 0x86, 0x35, 0xfb, 0x6c, + ]; + + let hash = sha256d::Hash::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(sha256d::Hash); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } + + #[test] + fn sha256() { + static HASH_BYTES: [u8; 32] = [ + 0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, 0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6, + 0x3d, 0x97, 0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2, 0xb7, 0x65, 0x44, 0x8c, + 0x86, 0x35, 0xfb, 0x6c, + ]; + + let hash = sha256::Hash::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(sha256::Hash); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } + + #[test] + fn TestHash() { + const TEST_MIDSTATE: [u8; 32] = [ + 156, 224, 228, 230, 124, 17, 108, 57, 56, 179, 202, 242, 195, 15, 80, 137, 211, 243, + 147, 108, 71, 99, 110, 96, 125, 179, 62, 234, 221, 198, 240, 201, + ]; + + #[derive( + Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash, schemars::JsonSchema, + )] + pub struct TestHashTag; + + impl sha256t::Tag for TestHashTag { + fn engine() -> sha256::HashEngine { + // The TapRoot TapLeaf midstate. + let midstate = sha256::Midstate::from_inner(TEST_MIDSTATE); + sha256::HashEngine::from_midstate(midstate, 64) + } + } + + /// A hash tagged with `$name`. + pub type TestHash = sha256t::Hash; + + sha256t_hash_newtype!( + NewTypeHash, + NewTypeTag, + TEST_MIDSTATE, + 64, + doc = "test hash", + true + ); + static HASH_BYTES: [u8; 32] = [ + 0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, 0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6, + 0x3d, 0x97, 0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2, 0xb7, 0x65, 0x44, 0x8c, + 0x86, 0x35, 0xfb, 0x6c, + ]; + + let hash = TestHash::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(TestHash); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } + #[test] + fn sha512() { + static HASH_BYTES: [u8; 64] = [ + 0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21, 0x11, 0x3c, 0x52, 0xff, 0x18, 0x2a, + 0x1b, 0x8e, 0x0a, 0x19, 0x57, 0x54, 0xaa, 0x52, 0x7f, 0xcd, 0x00, 0xa4, 0x11, 0x62, + 0x0b, 0x46, 0xf2, 0x0f, 0xff, 0xfb, 0x80, 0x88, 0xcc, 0xf8, 0x54, 0x97, 0x12, 0x1a, + 0xd4, 0x49, 0x9e, 0x08, 0x45, 0xb8, 0x76, 0xf6, 0xdd, 0x66, 0x40, 0x08, 0x8a, 0x2f, + 0x0b, 0x2d, 0x8a, 0x60, 0x0b, 0xdf, 0x4c, 0x0c, + ]; + + let hash = sha512::Hash::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(sha512::Hash); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } + + #[test] + fn siphash24() { + static HASH_BYTES: [u8; 8] = [0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21]; + + let hash = siphash24::Hash::from_slice(&HASH_BYTES).expect("right number of bytes"); + let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap(); + let s = schemars::schema_for!(siphash24::Hash); + let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert!(jsonschema_valid::Config::from_schema(&schema, None) + .unwrap() + .validate(&js) + .is_ok()); + } +}