Skip to content
Snippets Groups Projects
Commit d6f3e950 authored by dvermd's avatar dvermd
Browse files

[fix] ws2p-messages: #134 remove unwrap and deny its use

parent 82d5942b
Branches
No related tags found
1 merge request!244Resolve "Reliability: remove all "unwrap()" and deny their use"
...@@ -1279,6 +1279,7 @@ dependencies = [ ...@@ -1279,6 +1279,7 @@ dependencies = [
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "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_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)", "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]] [[package]]
......
...@@ -24,6 +24,7 @@ log = "0.4.*" ...@@ -24,6 +24,7 @@ log = "0.4.*"
serde = "1.0.*" serde = "1.0.*"
serde_derive = "1.0.*" serde_derive = "1.0.*"
serde_json = "1.0.*" serde_json = "1.0.*"
unwrap = "1.2.1"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.6.1" pretty_assertions = "0.6.1"
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
#![allow(clippy::large_enum_variant)] #![allow(clippy::large_enum_variant)]
#![deny( #![deny(
clippy::option_unwrap_used,
clippy::result_unwrap_used,
missing_docs, missing_docs,
missing_debug_implementations, missing_debug_implementations,
missing_copy_implementations, missing_copy_implementations,
...@@ -173,6 +175,7 @@ mod tests { ...@@ -173,6 +175,7 @@ mod tests {
use durs_network_documents::*; use durs_network_documents::*;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::str::FromStr; use std::str::FromStr;
use unwrap::unwrap;
pub fn keypair1() -> ed25519::Ed25519KeyPair { pub fn keypair1() -> ed25519::Ed25519KeyPair {
let seed = Seed32::new([ let seed = Seed32::new([
...@@ -201,7 +204,7 @@ mod tests { ...@@ -201,7 +204,7 @@ mod tests {
api_version: 2, api_version: 2,
network_features: EndpointV2NetworkFeatures(vec![1u8]), network_features: EndpointV2NetworkFeatures(vec![1u8]),
api_features: ApiFeatures(vec![7u8]), 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, ip_v6: None,
domain: None, domain: None,
port: 443u16, port: 443u16,
...@@ -235,7 +238,8 @@ mod tests { ...@@ -235,7 +238,8 @@ mod tests {
}); });
let sign_result = ws2p_message.sign(&signator); let sign_result = ws2p_message.sign(&signator);
if let Ok(bin_msg) = sign_result { match sign_result {
Ok(bin_msg) => {
// Test binarization // Test binarization
assert_eq!( assert_eq!(
serialize(&ws2p_message).expect("Fail to serialize WS2Pv2Message !"), serialize(&ws2p_message).expect("Fail to serialize WS2Pv2Message !"),
...@@ -246,43 +250,34 @@ mod tests { ...@@ -246,43 +250,34 @@ mod tests {
.verify() .verify()
.expect("WS2Pv2Message : Invalid signature !"); .expect("WS2Pv2Message : Invalid signature !");
// Test debinarization // Test debinarization
let debinarization_result: Result<WS2PMessage, bincode::Error> = deserialize(&bin_msg); let debinarization_result: Result<WS2PMessage, bincode::Error> =
if let Ok(ws2p_message2) = debinarization_result { deserialize(&bin_msg);
assert_eq!(ws2p_message, ws2p_message2); match debinarization_result {
} else { Ok(ws2p_message2) => assert_eq!(ws2p_message, ws2p_message2),
panic!( Err(e) => panic!("Fail to debinarize ws2p_message : {:?}", e),
"Fail to debinarize ws2p_message : {:?}", };
debinarization_result.err().unwrap()
);
}
} 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 { pub fn create_cert_doc() -> CompactCertificationDocumentV10 {
let sig = Sig::Ed25519(ed25519::Signature::from_base64( let sig = Sig::Ed25519(unwrap!(ed25519::Signature::from_base64(
"qfR6zqT1oJbqIsppOi64gC9yTtxb6g6XA9RYpulkq9ehMvqg2VYVigCbR0yVpqKFsnYiQTrnjgFuFRSJCJDfCw==", "qfR6zqT1oJbqIsppOi64gC9yTtxb6g6XA9RYpulkq9ehMvqg2VYVigCbR0yVpqKFsnYiQTrnjgFuFRSJCJDfCw==",
).unwrap()); )));
let target = PubKey::Ed25519( let target = PubKey::Ed25519(unwrap!(ed25519::PublicKey::from_base58(
ed25519::PublicKey::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV") "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"
.unwrap(), )));
);
let blockstamp = Blockstamp::from_string( let blockstamp = unwrap!(Blockstamp::from_string(
"36-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B865", "36-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B865"
) ));
.unwrap();
CompactCertificationDocumentV10 { CompactCertificationDocumentV10 {
issuer: PubKey::Ed25519( issuer: PubKey::Ed25519(unwrap!(ed25519::PublicKey::from_base58(
ed25519::PublicKey::from_base58("4tNQ7d9pj2Da5wUVoW9mFn7JjuPoowF977au8DdhEjVR") "4tNQ7d9pj2Da5wUVoW9mFn7JjuPoowF977au8DdhEjVR"
.unwrap(), ))),
),
target, target,
block_number: blockstamp.id, block_number: blockstamp.id,
signature: sig, signature: sig,
......
...@@ -165,6 +165,7 @@ mod tests { ...@@ -165,6 +165,7 @@ mod tests {
use crate::tests::*; use crate::tests::*;
use dubp_common_doc::Blockstamp; use dubp_common_doc::Blockstamp;
use dup_crypto::keys::text_signable::TextSignable; use dup_crypto::keys::text_signable::TextSignable;
use unwrap::unwrap;
#[test] #[test]
fn test_ws2p_connect_flags() { fn test_ws2p_connect_flags() {
...@@ -192,19 +193,15 @@ mod tests { ...@@ -192,19 +193,15 @@ mod tests {
let mut peer = create_peer_card_v11(); let mut peer = create_peer_card_v11();
peer.sign(&signator).expect("Fail to sign peer card !"); peer.sign(&signator).expect("Fail to sign peer card !");
let connect_msg = WS2Pv2ConnectMsg { let connect_msg = WS2Pv2ConnectMsg {
challenge: Hash::from_hex( challenge: unwrap!(Hash::from_hex(
"000007722B243094269E548F600BD34D73449F7578C05BD370A6D301D20B5F10", "000007722B243094269E548F600BD34D73449F7578C05BD370A6D301D20B5F10",
) )),
.unwrap(),
api_features: WS2PFeatures([7u8, 0, 0, 0]), api_features: WS2PFeatures([7u8, 0, 0, 0]),
flags_queries: WS2PConnectFlags(vec![]), flags_queries: WS2PConnectFlags(vec![]),
peer_card: Some(peer), peer_card: Some(peer),
chunkstamp: Some( chunkstamp: Some(unwrap!(Blockstamp::from_string(
Blockstamp::from_string(
"499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", "499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F",
) ))),
.unwrap(),
),
}; };
test_ws2p_message(WS2Pv2MessagePayload::Connect(Box::new(connect_msg))); test_ws2p_message(WS2Pv2MessagePayload::Connect(Box::new(connect_msg)));
} }
......
...@@ -51,25 +51,23 @@ mod tests { ...@@ -51,25 +51,23 @@ mod tests {
use crate::tests::*; use crate::tests::*;
use dubp_common_doc::Blockstamp; use dubp_common_doc::Blockstamp;
use std::num::NonZeroU16; use std::num::NonZeroU16;
use unwrap::unwrap;
#[test] #[test]
fn test_ws2p_message_ok() { fn test_ws2p_message_ok() {
let ok_msg = WS2Pv2OkMsg { let ok_msg = WS2Pv2OkMsg {
prefix: NonZeroU16::new(1), prefix: NonZeroU16::new(1),
sync_target: Some(WS2Pv2SyncTarget { sync_target: Some(WS2Pv2SyncTarget {
target_blockstamp: Blockstamp::from_string( target_blockstamp: unwrap!(Blockstamp::from_string(
"500-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", "500-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F",
) )),
.unwrap(),
chunks_hash: vec![ chunks_hash: vec![
Hash::from_hex( unwrap!(Hash::from_hex(
"000007722B243094269E548F600BD34D73449F7578C05BD370A6D301D20B5F10", "000007722B243094269E548F600BD34D73449F7578C05BD370A6D301D20B5F10",
) )),
.unwrap(), unwrap!(Hash::from_hex(
Hash::from_hex(
"0000095FD4C8EA96DE2844E3A4B62FD18761E9B4C13A74FAB716A4C81F438D91", "0000095FD4C8EA96DE2844E3A4B62FD18761E9B4C13A74FAB716A4C81F438D91",
) )),
.unwrap(),
], ],
}), }),
}; };
......
...@@ -64,6 +64,7 @@ mod tests { ...@@ -64,6 +64,7 @@ mod tests {
use super::*; use super::*;
use crate::tests::*; use crate::tests::*;
use dubp_common_doc::Blockstamp; use dubp_common_doc::Blockstamp;
use unwrap::unwrap;
#[test] #[test]
fn test_ws2p_message_req_res_none() { fn test_ws2p_message_req_res_none() {
...@@ -86,10 +87,9 @@ mod tests { ...@@ -86,10 +87,9 @@ mod tests {
#[test] #[test]
fn test_ws2p_message_req_res_current() { fn test_ws2p_message_req_res_current() {
let blockstamp = Blockstamp::from_string( let blockstamp = unwrap!(Blockstamp::from_string(
"499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", "499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F",
) ));
.unwrap();
let response = WS2Pv2ReqRes { let response = WS2Pv2ReqRes {
id: 28, id: 28,
body: WS2Pv2ReqResBody::Current(blockstamp), body: WS2Pv2ReqResBody::Current(blockstamp),
...@@ -100,10 +100,12 @@ mod tests { ...@@ -100,10 +100,12 @@ mod tests {
#[test] #[test]
fn test_ws2p_message_req_res_blocks_hashs() { fn test_ws2p_message_req_res_blocks_hashs() {
let hashs = vec![ let hashs = vec![
Hash::from_hex("000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F") unwrap!(Hash::from_hex(
.unwrap(), "000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F"
Hash::from_hex("0000007F8D3CCAF77CB77C5C025C4AED8A82BA2DBD2156FD92C9634DAB59BD7E") )),
.unwrap(), unwrap!(Hash::from_hex(
"0000007F8D3CCAF77CB77C5C025C4AED8A82BA2DBD2156FD92C9634DAB59BD7E"
)),
]; ];
let response = WS2Pv2ReqRes { let response = WS2Pv2ReqRes {
id: 29, id: 29,
......
...@@ -77,13 +77,13 @@ mod tests { ...@@ -77,13 +77,13 @@ mod tests {
use super::*; use super::*;
use crate::tests::*; use crate::tests::*;
use dubp_common_doc::Blockstamp; use dubp_common_doc::Blockstamp;
use unwrap::unwrap;
#[test] #[test]
fn test_ws2p_message_request() { fn test_ws2p_message_request() {
let chunkstamp = Blockstamp::from_string( let chunkstamp = unwrap!(Blockstamp::from_string(
"499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F", "499-000011BABEEE1020B1F6B2627E2BC1C35BCD24375E114349634404D2C266D84F",
) ));
.unwrap();
let request = WS2Pv2Request { let request = WS2Pv2Request {
id: 27, id: 27,
body: WS2Pv2RequestBody::ChunkByHash(chunkstamp), body: WS2Pv2RequestBody::ChunkByHash(chunkstamp),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment