diff --git a/Cargo.lock b/Cargo.lock index b865dd8d682e3889698272a9ea5dc2aa3f0b50c9..6bb5f9f5773f7c17b8bb8bdd4a7371d55177c965 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1279,6 +1279,7 @@ dependencies = [ "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)", + "unwrap 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/lib/modules/ws2p/ws2p-messages/Cargo.toml b/lib/modules/ws2p/ws2p-messages/Cargo.toml index 2c4b50db52cc7b7e9b1011296187ac4ff8329554..59bd89f547cc5abce9daa56891bf151126219fc8 100644 --- a/lib/modules/ws2p/ws2p-messages/Cargo.toml +++ b/lib/modules/ws2p/ws2p-messages/Cargo.toml @@ -24,6 +24,7 @@ log = "0.4.*" serde = "1.0.*" serde_derive = "1.0.*" serde_json = "1.0.*" +unwrap = "1.2.1" [dev-dependencies] pretty_assertions = "0.6.1" diff --git a/lib/modules/ws2p/ws2p-messages/lib.rs b/lib/modules/ws2p/ws2p-messages/lib.rs index adbb46a2a785486b6e21e75f79ba5e2f9ff20f62..13df8c6e9aec00691576027d0c79f4dc557ede91 100644 --- a/lib/modules/ws2p/ws2p-messages/lib.rs +++ b/lib/modules/ws2p/ws2p-messages/lib.rs @@ -17,6 +17,8 @@ #![allow(clippy::large_enum_variant)] #![deny( + clippy::option_unwrap_used, + clippy::result_unwrap_used, missing_docs, missing_debug_implementations, missing_copy_implementations, @@ -173,6 +175,7 @@ mod tests { use durs_network_documents::*; use std::net::Ipv4Addr; use std::str::FromStr; + use unwrap::unwrap; pub fn keypair1() -> ed25519::Ed25519KeyPair { let seed = Seed32::new([ @@ -201,7 +204,7 @@ mod tests { api_version: 2, network_features: EndpointV2NetworkFeatures(vec![1u8]), api_features: ApiFeatures(vec![7u8]), - ip_v4: Some(Ipv4Addr::from_str("84.16.72.210").unwrap()), + ip_v4: Some(unwrap!(Ipv4Addr::from_str("84.16.72.210"))), ip_v6: None, domain: None, port: 443u16, @@ -235,54 +238,46 @@ mod tests { }); let sign_result = ws2p_message.sign(&signator); - if let Ok(bin_msg) = sign_result { - // Test binarization - assert_eq!( - serialize(&ws2p_message).expect("Fail to serialize WS2Pv2Message !"), - bin_msg - ); - // Test sign - ws2p_message - .verify() - .expect("WS2Pv2Message : Invalid signature !"); - // Test debinarization - let debinarization_result: Result<WS2PMessage, bincode::Error> = deserialize(&bin_msg); - if let Ok(ws2p_message2) = debinarization_result { - assert_eq!(ws2p_message, ws2p_message2); - } else { - panic!( - "Fail to debinarize ws2p_message : {:?}", - debinarization_result.err().unwrap() + match sign_result { + Ok(bin_msg) => { + // Test binarization + assert_eq!( + serialize(&ws2p_message).expect("Fail to serialize WS2Pv2Message !"), + bin_msg ); + // Test sign + ws2p_message + .verify() + .expect("WS2Pv2Message : Invalid signature !"); + // Test debinarization + let debinarization_result: Result<WS2PMessage, bincode::Error> = + deserialize(&bin_msg); + match debinarization_result { + Ok(ws2p_message2) => assert_eq!(ws2p_message, ws2p_message2), + Err(e) => panic!("Fail to debinarize ws2p_message : {:?}", e), + }; } - } else { - panic!( - "Fail to sign ws2p_message : {:?}", - sign_result.err().unwrap() - ); - } + Err(e) => panic!("Fail to sign ws2p_message : {:?}", e), + }; } pub fn create_cert_doc() -> CompactCertificationDocumentV10 { - let sig = Sig::Ed25519(ed25519::Signature::from_base64( + let sig = Sig::Ed25519(unwrap!(ed25519::Signature::from_base64( "qfR6zqT1oJbqIsppOi64gC9yTtxb6g6XA9RYpulkq9ehMvqg2VYVigCbR0yVpqKFsnYiQTrnjgFuFRSJCJDfCw==", - ).unwrap()); + ))); - let target = PubKey::Ed25519( - ed25519::PublicKey::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV") - .unwrap(), - ); + let target = PubKey::Ed25519(unwrap!(ed25519::PublicKey::from_base58( + "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV" + ))); - let blockstamp = Blockstamp::from_string( - "36-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B865", - ) - .unwrap(); + let blockstamp = unwrap!(Blockstamp::from_string( + "36-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B865" + )); CompactCertificationDocumentV10 { - issuer: PubKey::Ed25519( - ed25519::PublicKey::from_base58("4tNQ7d9pj2Da5wUVoW9mFn7JjuPoowF977au8DdhEjVR") - .unwrap(), - ), + issuer: PubKey::Ed25519(unwrap!(ed25519::PublicKey::from_base58( + "4tNQ7d9pj2Da5wUVoW9mFn7JjuPoowF977au8DdhEjVR" + ))), target, block_number: blockstamp.id, signature: sig, diff --git a/lib/modules/ws2p/ws2p-messages/v2/connect.rs b/lib/modules/ws2p/ws2p-messages/v2/connect.rs index 97f938a21c1b3eca57608e7a1df5c5f81722e1a7..1126b53856b9538b39e9c599fcd89d7797e0d9bf 100644 --- a/lib/modules/ws2p/ws2p-messages/v2/connect.rs +++ b/lib/modules/ws2p/ws2p-messages/v2/connect.rs @@ -165,6 +165,7 @@ mod tests { use crate::tests::*; use dubp_common_doc::Blockstamp; use dup_crypto::keys::text_signable::TextSignable; + use unwrap::unwrap; #[test] fn test_ws2p_connect_flags() { @@ -192,19 +193,15 @@ mod tests { let mut peer = create_peer_card_v11(); peer.sign(&signator).expect("Fail to sign peer card !"); let connect_msg = WS2Pv2ConnectMsg { - challenge: Hash::from_hex( + challenge: unwrap!(Hash::from_hex( "000007722B243094269E548F600BD34D73449F7578C05BD370A6D301D20B5F10", - ) - .unwrap(), + )), api_features: WS2PFeatures([7u8, 0, 0, 0]), flags_queries: WS2PConnectFlags(vec![]), peer_card: Some(peer), - chunkstamp: Some( - Blockstamp::from_string( - "499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", - ) - .unwrap(), - ), + chunkstamp: Some(unwrap!(Blockstamp::from_string( + "499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", + ))), }; test_ws2p_message(WS2Pv2MessagePayload::Connect(Box::new(connect_msg))); } diff --git a/lib/modules/ws2p/ws2p-messages/v2/ok.rs b/lib/modules/ws2p/ws2p-messages/v2/ok.rs index 5701efc4854c5b9d030e1df0c999d27eb636ab73..c5841d4a0bc7ae814f1435c97b3326f93350e549 100644 --- a/lib/modules/ws2p/ws2p-messages/v2/ok.rs +++ b/lib/modules/ws2p/ws2p-messages/v2/ok.rs @@ -51,25 +51,23 @@ mod tests { use crate::tests::*; use dubp_common_doc::Blockstamp; use std::num::NonZeroU16; + use unwrap::unwrap; #[test] fn test_ws2p_message_ok() { let ok_msg = WS2Pv2OkMsg { prefix: NonZeroU16::new(1), sync_target: Some(WS2Pv2SyncTarget { - target_blockstamp: Blockstamp::from_string( + target_blockstamp: unwrap!(Blockstamp::from_string( "500-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", - ) - .unwrap(), + )), chunks_hash: vec![ - Hash::from_hex( + unwrap!(Hash::from_hex( "000007722B243094269E548F600BD34D73449F7578C05BD370A6D301D20B5F10", - ) - .unwrap(), - Hash::from_hex( + )), + unwrap!(Hash::from_hex( "0000095FD4C8EA96DE2844E3A4B62FD18761E9B4C13A74FAB716A4C81F438D91", - ) - .unwrap(), + )), ], }), }; diff --git a/lib/modules/ws2p/ws2p-messages/v2/req_responses.rs b/lib/modules/ws2p/ws2p-messages/v2/req_responses.rs index 3bdfd5753c198ed107a2c31af179cd29829d2ca8..9dcbf101c97b68a6d09bcca48489a0ab15bd09c7 100644 --- a/lib/modules/ws2p/ws2p-messages/v2/req_responses.rs +++ b/lib/modules/ws2p/ws2p-messages/v2/req_responses.rs @@ -64,6 +64,7 @@ mod tests { use super::*; use crate::tests::*; use dubp_common_doc::Blockstamp; + use unwrap::unwrap; #[test] fn test_ws2p_message_req_res_none() { @@ -86,10 +87,9 @@ mod tests { #[test] fn test_ws2p_message_req_res_current() { - let blockstamp = Blockstamp::from_string( + let blockstamp = unwrap!(Blockstamp::from_string( "499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", - ) - .unwrap(); + )); let response = WS2Pv2ReqRes { id: 28, body: WS2Pv2ReqResBody::Current(blockstamp), @@ -100,10 +100,12 @@ mod tests { #[test] fn test_ws2p_message_req_res_blocks_hashs() { let hashs = vec![ - Hash::from_hex("000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F") - .unwrap(), - Hash::from_hex("0000007F8D3CCAF77CB77C5C025C4AED8A82BA2DBD2156FD92C9634DAB59BD7E") - .unwrap(), + unwrap!(Hash::from_hex( + "000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F" + )), + unwrap!(Hash::from_hex( + "0000007F8D3CCAF77CB77C5C025C4AED8A82BA2DBD2156FD92C9634DAB59BD7E" + )), ]; let response = WS2Pv2ReqRes { id: 29, diff --git a/lib/modules/ws2p/ws2p-messages/v2/requests.rs b/lib/modules/ws2p/ws2p-messages/v2/requests.rs index 4d7cb689a5fc1efcd968e85ff5be1125fd376ce2..c7cd2496c4c10c2526bb47a7d5167dd22d457a06 100644 --- a/lib/modules/ws2p/ws2p-messages/v2/requests.rs +++ b/lib/modules/ws2p/ws2p-messages/v2/requests.rs @@ -77,13 +77,13 @@ mod tests { use super::*; use crate::tests::*; use dubp_common_doc::Blockstamp; + use unwrap::unwrap; #[test] fn test_ws2p_message_request() { - let chunkstamp = Blockstamp::from_string( + let chunkstamp = unwrap!(Blockstamp::from_string( "499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", - ) - .unwrap(); + )); let request = WS2Pv2Request { id: 27, body: WS2Pv2RequestBody::ChunkByHash(chunkstamp),