From bb03102c2167a72966c052c5c2b9ab0ccb341960 Mon Sep 17 00:00:00 2001 From: Jonathan Donszelmann Date: Sat, 22 Jun 2019 16:29:55 +0200 Subject: [PATCH 1/3] upped header.rs --- rust_ipv8/src/serialization/header.rs | 141 +++++++++++++++++++++++++- 1 file changed, 138 insertions(+), 3 deletions(-) diff --git a/rust_ipv8/src/serialization/header.rs b/rust_ipv8/src/serialization/header.rs index a6b33918..66083aba 100644 --- a/rust_ipv8/src/serialization/header.rs +++ b/rust_ipv8/src/serialization/header.rs @@ -76,6 +76,8 @@ struct PyIPV8HeaderPattern( /// 2 bytes magic + 20 bytes hash + 1 byte message type = 23 bytes const PY_IPV8_HEADER_SIZE: usize = 23; +const PY_IPV8_HASH_SIZE: usize = 20; +const PY_IPV8_VERSION: u16 = 2; //------------end header constants------------ @@ -89,9 +91,7 @@ impl Serialize for Header { match self.version { HeaderVersion::PyIPV8Header => { let mut state = serializer.serialize_tuple(PY_IPV8_HEADER_SIZE)?; - match self.version { - HeaderVersion::PyIPV8Header => state.serialize_element(&(2 as u16))?, - } + state.serialize_element(&(PY_IPV8_VERSION as u16))?; // Unwrap the hash let hash = match &self.mid_hash { @@ -103,6 +103,12 @@ impl Serialize for Header { } }; + if hash.len() != PY_IPV8_HASH_SIZE { + return Err(serde::ser::Error::custom( + "mid_hash size did not match the expected 20 bytes", + )); + } + // Serialize the hash for i in hash { state.serialize_element(&i)?; @@ -226,6 +232,7 @@ mod tests { use bincode; use super::*; + use bincode::ErrorKind; #[test] fn integration_test_creation() { @@ -244,4 +251,132 @@ mod tests { .unwrap() ); } + + #[test] + fn test_fail_deserialize_no_headertype_first_byte() { + let h: Result> = bincode::config().big_endian().deserialize(&[]); + + match h { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } + + #[test] + fn test_fail_deserialize_no_headertype_second_byte() { + let h: Result> = bincode::config().big_endian().deserialize(&[1]); + + match h { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } + + #[test] + fn test_fail_deserialize_no_headertype() { + let h: Result> = bincode::config().big_endian().deserialize(&[1, 2]); + + match h { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } + + #[test] + fn test_fail_deserialize_no_hash() { + let h: Result> = bincode::config().big_endian().deserialize(&[0, 2]); + + match h { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } + + #[test] + fn test_fail_deserialize_no_messagetype() { + let h: Result> = bincode::config().big_endian().deserialize(&[ + 0, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + ]); + + match h { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } + + #[test] + fn test_valid_headertype() { + let h: Result> = bincode::config().big_endian().deserialize(&[ + 0, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 42, + ]); + + match h { + Err(_) => assert!(false), + Ok(_) => assert!(true), + }; + } + + #[test] + fn test_fail_serialize_no_hash() { + let h = Header { + size: 3, + version: HeaderVersion::PyIPV8Header, + mid_hash: None, + message_type: Some(1u64), + }; + + dbg!(&h); + + match bincode::config().big_endian().serialize(&h) { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } + + #[test] + fn test_fail_serialize_no_message_type() { + let h = Header { + size: 3, + version: HeaderVersion::PyIPV8Header, + mid_hash: Some(vec![]), + message_type: None, + }; + + match bincode::config().big_endian().serialize(&h) { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } + + #[test] + fn test_fail_serialize_hash_too_short() { + let h = Header { + size: 3, + version: HeaderVersion::PyIPV8Header, + mid_hash: Some(vec![]), + message_type: Some(1u64), + }; + + match bincode::config().big_endian().serialize(&h) { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } + + #[test] + fn test_fail_serialize_hash_too_large() { + let h = Header { + size: 3, + version: HeaderVersion::PyIPV8Header, + mid_hash: Some(vec![ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + ]), + message_type: Some(1u64), + }; + + match bincode::config().big_endian().serialize(&h) { + Err(_) => assert!(true), + Ok(_) => assert!(false), + }; + } } From 43aabcc53086d9e5754d994bdf882d5cd053b938 Mon Sep 17 00:00:00 2001 From: Victor Roest Date: Sun, 23 Jun 2019 23:07:43 +0200 Subject: [PATCH 2/3] converted match statements in tests to increase coverage --- rust_ipv8/src/lib.rs | 1 + rust_ipv8/src/serialization/header.rs | 51 ++++---------------- rust_ipv8/src/serialization/mod.rs | 5 +- rust_ipv8/src/serialization/nestedpayload.rs | 5 +- rust_ipv8/src/serialization/varlen.rs | 16 +----- rust_ipv8/src/util.rs | 10 +--- 6 files changed, 17 insertions(+), 71 deletions(-) diff --git a/rust_ipv8/src/lib.rs b/rust_ipv8/src/lib.rs index 6f5b7f34..bc9f900d 100644 --- a/rust_ipv8/src/lib.rs +++ b/rust_ipv8/src/lib.rs @@ -1,5 +1,6 @@ //! [README](https://github.com/ip-v8/rust-ipv8/blob/develop/README.md) #![deny(clippy::missing_docs_in_private_items)] +#![recursion_limit = "99999999999999999"] #[macro_use] extern crate log; diff --git a/rust_ipv8/src/serialization/header.rs b/rust_ipv8/src/serialization/header.rs index 66083aba..afbe33d4 100644 --- a/rust_ipv8/src/serialization/header.rs +++ b/rust_ipv8/src/serialization/header.rs @@ -255,41 +255,28 @@ mod tests { #[test] fn test_fail_deserialize_no_headertype_first_byte() { let h: Result> = bincode::config().big_endian().deserialize(&[]); - - match h { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(h.is_err()) } #[test] fn test_fail_deserialize_no_headertype_second_byte() { let h: Result> = bincode::config().big_endian().deserialize(&[1]); - match h { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(h.is_err()) } #[test] fn test_fail_deserialize_no_headertype() { let h: Result> = bincode::config().big_endian().deserialize(&[1, 2]); - match h { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(h.is_err()) } #[test] fn test_fail_deserialize_no_hash() { let h: Result> = bincode::config().big_endian().deserialize(&[0, 2]); - match h { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(h.is_err()) } #[test] @@ -298,10 +285,7 @@ mod tests { 0, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ]); - match h { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(h.is_err()) } #[test] @@ -310,10 +294,7 @@ mod tests { 0, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 42, ]); - match h { - Err(_) => assert!(false), - Ok(_) => assert!(true), - }; + assert!(h.is_ok()) } #[test] @@ -327,10 +308,7 @@ mod tests { dbg!(&h); - match bincode::config().big_endian().serialize(&h) { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(bincode::config().big_endian().serialize(&h).is_err()) } #[test] @@ -342,10 +320,7 @@ mod tests { message_type: None, }; - match bincode::config().big_endian().serialize(&h) { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(bincode::config().big_endian().serialize(&h).is_err()) } #[test] @@ -357,10 +332,7 @@ mod tests { message_type: Some(1u64), }; - match bincode::config().big_endian().serialize(&h) { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(bincode::config().big_endian().serialize(&h).is_err()) } #[test] @@ -374,9 +346,6 @@ mod tests { message_type: Some(1u64), }; - match bincode::config().big_endian().serialize(&h) { - Err(_) => assert!(true), - Ok(_) => assert!(false), - }; + assert!(bincode::config().big_endian().serialize(&h).is_err()) } } diff --git a/rust_ipv8/src/serialization/mod.rs b/rust_ipv8/src/serialization/mod.rs index bd3cf4e3..a1f4ca83 100644 --- a/rust_ipv8/src/serialization/mod.rs +++ b/rust_ipv8/src/serialization/mod.rs @@ -345,9 +345,6 @@ mod tests { assert_eq!(c, deser_iterator.next_payload().unwrap()); let last: Result> = deser_iterator.next_payload(); - match last { - Ok(_) => assert!(false, "this should throw an error as there is no next"), - Err(_) => assert!(true), - }; + assert!(last.is_err()) } } diff --git a/rust_ipv8/src/serialization/nestedpayload.rs b/rust_ipv8/src/serialization/nestedpayload.rs index 1f37ef76..9509a75d 100644 --- a/rust_ipv8/src/serialization/nestedpayload.rs +++ b/rust_ipv8/src/serialization/nestedpayload.rs @@ -151,9 +151,6 @@ mod tests { }; let mut packet = Packet::new(create_test_header!()).unwrap(); - match packet.add(&i) { - Ok(_) => assert!(false), - Err(_) => assert!(true), - } + assert!(packet.add(&i).is_err()) } } diff --git a/rust_ipv8/src/serialization/varlen.rs b/rust_ipv8/src/serialization/varlen.rs index 72f74841..cb87b930 100644 --- a/rust_ipv8/src/serialization/varlen.rs +++ b/rust_ipv8/src/serialization/varlen.rs @@ -235,13 +235,7 @@ mod tests { fn test_varlen16_too_large() { let tmp: Vec = vec![0; (1u32 << 17) as usize]; let i = VarLen16(tmp); - match Packet::new(create_test_header!()).unwrap().add(&i) { - Ok(_) => assert!( - false, - "this should throw an error as 2^17 bytes is too large for a varlen16" - ), - Err(_) => assert!(true), - }; + assert!(Packet::new(create_test_header!()).unwrap().add(&i).is_err()) } // fucking ci cant run this @@ -250,13 +244,7 @@ mod tests { fn test_varlen32_too_large() { let tmp: Vec = vec![0; (1u64 << 32 + 1) as usize]; let i = VarLen32(tmp); - match Packet::new(create_test_header!()).unwrap().add(&i) { - Ok(_) => assert!( - false, - "this should throw an error as 2^33 bytes is too large for a varlen32" - ), - Err(_) => assert!(true), - }; + assert!(Packet::new(create_test_header!()).unwrap().add(&i).is_err()) } #[test] diff --git a/rust_ipv8/src/util.rs b/rust_ipv8/src/util.rs index 12799fcb..680fcc22 100644 --- a/rust_ipv8/src/util.rs +++ b/rust_ipv8/src/util.rs @@ -32,10 +32,7 @@ mod tests { let data = &[0u8, 1u8, 2u8]; let fixed: Result<&[u8; 4], Box> = as_fixed_size(data); - match fixed { - Ok(_) => assert!(false), - Err(_) => assert!(true), - }; + assert!(fixed.is_err()); } #[test] @@ -43,10 +40,7 @@ mod tests { let data = &[0u8, 1u8, 2u8]; let fixed: Result<&[u8; 2], Box> = as_fixed_size(data); - match fixed { - Ok(_) => assert!(false), - Err(_) => assert!(true), - }; + assert!(fixed.is_err()); } } From 21a1800c96f3ae84f781f8795b1dee041fb64d2c Mon Sep 17 00:00:00 2001 From: Victor Roest Date: Mon, 24 Jun 2019 00:02:32 +0200 Subject: [PATCH 3/3] Various minor Coverage related changes * Added coverage script for local generation * Added relevant files to .gitignore * Disable caching for coverage on travis as it mostly just breaks it --- .gitignore | 4 ++++ .travis.yml | 1 + rust_ipv8/coverage.sh | 11 +++++++++++ 3 files changed, 16 insertions(+) create mode 100644 rust_ipv8/coverage.sh diff --git a/.gitignore b/.gitignore index d3ec32b8..2f5a4b95 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ Cargo.lock # Python .venv + +# Coverage files +lcov.info +ccov.zip diff --git a/.travis.yml b/.travis.yml index 2627fd9c..37bd7d81 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,6 +23,7 @@ matrix: jobs: include: - stage: test + cache: false # Caching seems to break coverage reporting name: "Nightly + coverage" <<: *rust_template rust: nightly diff --git a/rust_ipv8/coverage.sh b/rust_ipv8/coverage.sh new file mode 100644 index 00000000..b2d21d82 --- /dev/null +++ b/rust_ipv8/coverage.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +export CARGO_INCREMENTAL=0 +export RUSTFLAGS="-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zno-landing-pads" +cargo build --verbose $CARGO_OPTIONS +cargo test --verbose $CARGO_OPTIONS +zip -0 ccov.zip `find ../ \( -name "*ipv8*.gc*" \) -print` +grcov ccov.zip -s src/ -t lcov --llvm --branch --ignore-not-existing --ignore-dir "/*" -o lcov.info +rm ccov.zip +cd src +genhtml -o ../../target/coverage/ ../lcov.info +xdg-open ../../target/coverage/index.html