Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increased coverage #57

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ Cargo.lock

# Python
.venv

# Coverage files
lcov.info
ccov.zip
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ matrix:
jobs:
include:
- stage: test
cache: false # Caching seems to break coverage reporting
name: "Nightly + coverage"
<<: *rust_template
rust: nightly
Expand Down
11 changes: 11 additions & 0 deletions rust_ipv8/coverage.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions rust_ipv8/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
110 changes: 107 additions & 3 deletions rust_ipv8/src/serialization/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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------------

Expand All @@ -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 {
Expand All @@ -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)?;
Expand Down Expand Up @@ -226,6 +232,7 @@ mod tests {
use bincode;

use super::*;
use bincode::ErrorKind;

#[test]
fn integration_test_creation() {
Expand All @@ -244,4 +251,101 @@ mod tests {
.unwrap()
);
}

#[test]
fn test_fail_deserialize_no_headertype_first_byte() {
let h: Result<Header, Box<ErrorKind>> = bincode::config().big_endian().deserialize(&[]);
assert!(h.is_err())
}

#[test]
fn test_fail_deserialize_no_headertype_second_byte() {
let h: Result<Header, Box<ErrorKind>> = bincode::config().big_endian().deserialize(&[1]);

assert!(h.is_err())
}

#[test]
fn test_fail_deserialize_no_headertype() {
let h: Result<Header, Box<ErrorKind>> = bincode::config().big_endian().deserialize(&[1, 2]);

assert!(h.is_err())
}

#[test]
fn test_fail_deserialize_no_hash() {
let h: Result<Header, Box<ErrorKind>> = bincode::config().big_endian().deserialize(&[0, 2]);

assert!(h.is_err())
}

#[test]
fn test_fail_deserialize_no_messagetype() {
let h: Result<Header, Box<ErrorKind>> = 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,
]);

assert!(h.is_err())
}

#[test]
fn test_valid_headertype() {
let h: Result<Header, Box<ErrorKind>> = 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,
]);

assert!(h.is_ok())
}

#[test]
fn test_fail_serialize_no_hash() {
let h = Header {
size: 3,
version: HeaderVersion::PyIPV8Header,
mid_hash: None,
message_type: Some(1u64),
};

dbg!(&h);

assert!(bincode::config().big_endian().serialize(&h).is_err())
}

#[test]
fn test_fail_serialize_no_message_type() {
let h = Header {
size: 3,
version: HeaderVersion::PyIPV8Header,
mid_hash: Some(vec![]),
message_type: None,
};

assert!(bincode::config().big_endian().serialize(&h).is_err())
}

#[test]
fn test_fail_serialize_hash_too_short() {
let h = Header {
size: 3,
version: HeaderVersion::PyIPV8Header,
mid_hash: Some(vec![]),
message_type: Some(1u64),
};

assert!(bincode::config().big_endian().serialize(&h).is_err())
}

#[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),
};

assert!(bincode::config().big_endian().serialize(&h).is_err())
}
}
5 changes: 1 addition & 4 deletions rust_ipv8/src/serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ mod tests {
assert_eq!(c, deser_iterator.next_payload().unwrap());

let last: Result<TestPayload1, Box<ErrorKind>> = 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())
}
}
5 changes: 1 addition & 4 deletions rust_ipv8/src/serialization/nestedpayload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
16 changes: 2 additions & 14 deletions rust_ipv8/src/serialization/varlen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,7 @@ mod tests {
fn test_varlen16_too_large() {
let tmp: Vec<u8> = 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
Expand All @@ -250,13 +244,7 @@ mod tests {
fn test_varlen32_too_large() {
let tmp: Vec<u8> = 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]
Expand Down
10 changes: 2 additions & 8 deletions rust_ipv8/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,15 @@ mod tests {
let data = &[0u8, 1u8, 2u8];
let fixed: Result<&[u8; 4], Box<dyn Error>> = as_fixed_size(data);

match fixed {
Ok(_) => assert!(false),
Err(_) => assert!(true),
};
assert!(fixed.is_err());
}

#[test]
fn test_as_fixed_size_invalid_too_small() {
let data = &[0u8, 1u8, 2u8];
let fixed: Result<&[u8; 2], Box<dyn Error>> = as_fixed_size(data);

match fixed {
Ok(_) => assert!(false),
Err(_) => assert!(true),
};
assert!(fixed.is_err());
}

}