mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-28 14:23:30 +01:00
rust: Refactor protover tests with new methods; note altered behaviours.
Previously, the rust implementation of protover considered an empty string to be a valid ProtoEntry, while the C version did not (it must have a "=" character). Other differences include that unknown protocols must now be parsed as `protover::UnknownProtocol`s, and hence their entries as `protover::UnvalidatedProtoEntry`s, whereas before (nearly) all protoentries could be parsed regardless of how erroneous they might be considered by the C version. My apologies for this somewhat messy and difficult to read commit, if any part is frustrating to the reviewer, please feel free to ask me to split this into smaller changes (possibly hard to do, since so much changed), or ask me to comment on a specific line/change and clarify how/when the behaviours differ. The tests here should more closely match the behaviours exhibited by the C implementation, but I do not yet personally guarantee they match precisely. * REFACTOR unittests in protover::protover. * ADD new integration tests for previously untested behaviour. * FIXES part of #24031: https://bugs.torproject.org/24031.
This commit is contained in:
parent
aa241e99de
commit
15e59a1fed
@ -650,154 +650,118 @@ mod test {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
macro_rules! assert_protoentry_is_parseable {
|
||||||
fn test_versions_from_version_string() {
|
($e:expr) => (
|
||||||
use std::collections::HashSet;
|
let protoentry: Result<ProtoEntry, ProtoverError> = $e.parse();
|
||||||
|
|
||||||
use super::Versions;
|
assert!(protoentry.is_ok(), format!("{:?}", protoentry.err()));
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
assert_eq!(Err("invalid protocol entry"), Versions::from_version_string("a,b"));
|
macro_rules! assert_protoentry_is_unparseable {
|
||||||
assert_eq!(Err("invalid protocol entry"), Versions::from_version_string("1,!"));
|
($e:expr) => (
|
||||||
|
let protoentry: Result<ProtoEntry, ProtoverError> = $e.parse();
|
||||||
|
|
||||||
{
|
assert!(protoentry.is_err());
|
||||||
let mut versions: HashSet<Version> = HashSet::new();
|
)
|
||||||
versions.insert(1);
|
|
||||||
assert_eq!(versions, Versions::from_version_string("1").unwrap().0);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
let mut versions: HashSet<Version> = HashSet::new();
|
|
||||||
versions.insert(1);
|
|
||||||
versions.insert(2);
|
|
||||||
assert_eq!(versions, Versions::from_version_string("1,2").unwrap().0);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
let mut versions: HashSet<Version> = HashSet::new();
|
|
||||||
versions.insert(1);
|
|
||||||
versions.insert(2);
|
|
||||||
versions.insert(3);
|
|
||||||
assert_eq!(versions, Versions::from_version_string("1-3").unwrap().0);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
let mut versions: HashSet<Version> = HashSet::new();
|
|
||||||
versions.insert(1);
|
|
||||||
versions.insert(2);
|
|
||||||
versions.insert(5);
|
|
||||||
assert_eq!(versions, Versions::from_version_string("1-2,5").unwrap().0);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
let mut versions: HashSet<Version> = HashSet::new();
|
|
||||||
versions.insert(1);
|
|
||||||
versions.insert(3);
|
|
||||||
versions.insert(4);
|
|
||||||
versions.insert(5);
|
|
||||||
assert_eq!(versions, Versions::from_version_string("1,3-5").unwrap().0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_contains_only_supported_protocols() {
|
fn test_protoentry_from_str_multiple_protocols_multiple_versions() {
|
||||||
use super::contains_only_supported_protocols;
|
assert_protoentry_is_parseable!("Cons=3-4 Link=1,3-5");
|
||||||
|
|
||||||
assert_eq!(false, contains_only_supported_protocols(""));
|
|
||||||
assert_eq!(true, contains_only_supported_protocols("Cons="));
|
|
||||||
assert_eq!(true, contains_only_supported_protocols("Cons=1"));
|
|
||||||
assert_eq!(false, contains_only_supported_protocols("Cons=0"));
|
|
||||||
assert_eq!(false, contains_only_supported_protocols("Cons=0-1"));
|
|
||||||
assert_eq!(false, contains_only_supported_protocols("Cons=5"));
|
|
||||||
assert_eq!(false, contains_only_supported_protocols("Cons=1-5"));
|
|
||||||
assert_eq!(false, contains_only_supported_protocols("Cons=1,5"));
|
|
||||||
assert_eq!(false, contains_only_supported_protocols("Cons=5,6"));
|
|
||||||
assert_eq!(false, contains_only_supported_protocols("Cons=1,5,6"));
|
|
||||||
assert_eq!(true, contains_only_supported_protocols("Cons=1,2"));
|
|
||||||
assert_eq!(true, contains_only_supported_protocols("Cons=1-2"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_range() {
|
fn test_protoentry_from_str_empty() {
|
||||||
use super::find_range;
|
assert_protoentry_is_unparseable!("");
|
||||||
|
|
||||||
assert_eq!((false, 0), find_range(&vec![]));
|
|
||||||
assert_eq!((false, 1), find_range(&vec![1]));
|
|
||||||
assert_eq!((true, 2), find_range(&vec![1, 2]));
|
|
||||||
assert_eq!((true, 3), find_range(&vec![1, 2, 3]));
|
|
||||||
assert_eq!((true, 3), find_range(&vec![1, 2, 3, 5]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_expand_version_range() {
|
fn test_protoentry_from_str_single_protocol_single_version() {
|
||||||
use super::expand_version_range;
|
assert_protoentry_is_parseable!("HSDir=1");
|
||||||
|
}
|
||||||
|
|
||||||
assert_eq!(Err("version string empty"), expand_version_range(""));
|
#[test]
|
||||||
assert_eq!(Ok(1..3), expand_version_range("1-2"));
|
fn test_protoentry_from_str_unknown_protocol() {
|
||||||
assert_eq!(Ok(1..5), expand_version_range("1-4"));
|
assert_protoentry_is_unparseable!("Ducks=5-7,8");
|
||||||
assert_eq!(
|
}
|
||||||
Err("cannot parse protocol range lower bound"),
|
|
||||||
expand_version_range("a")
|
#[test]
|
||||||
);
|
fn test_protoentry_from_str_too_many_versions() {
|
||||||
assert_eq!(
|
assert_protoentry_is_unparseable!("Desc=1-65537");
|
||||||
Err("cannot parse protocol range upper bound"),
|
}
|
||||||
expand_version_range("1-a")
|
|
||||||
);
|
#[test]
|
||||||
assert_eq!(Ok(1000..66536), expand_version_range("1000-66535"));
|
fn test_protoentry_from_str_() {
|
||||||
assert_eq!(Err("Too many protocols in expanded range"),
|
assert_protoentry_is_unparseable!("");
|
||||||
expand_version_range("1000-66536"));
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_protoentry_all_supported_single_protocol_single_version() {
|
||||||
|
let protocol: UnvalidatedProtoEntry = "Cons=1".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocol.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_protoentry_all_supported_multiple_protocol_multiple_versions() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "Link=3-4 Desc=2".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_protoentry_all_supported_three_values() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "LinkAuth=1 Microdesc=1-2 Relay=2".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_protoentry_all_supported_unknown_protocol() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "Wombat=9".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_some());
|
||||||
|
assert_eq!("Wombat=9", &unsupported.unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_protoentry_all_supported_unsupported_high_version() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "HSDir=12-100".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_some());
|
||||||
|
assert_eq!("HSDir=12-100", &unsupported.unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_protoentry_all_supported_unsupported_low_version() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "Cons=0-1".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_some());
|
||||||
|
assert_eq!("Cons=0", &unsupported.unwrap().to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_contract_protocol_list() {
|
fn test_contract_protocol_list() {
|
||||||
use std::collections::HashSet;
|
let mut versions = "";
|
||||||
use super::contract_protocol_list;
|
assert_eq!(String::from(versions), ProtoSet::from_str(&versions).unwrap().to_string());
|
||||||
|
|
||||||
{
|
versions = "1";
|
||||||
let mut versions = HashSet::<Version>::new();
|
assert_eq!(String::from(versions), ProtoSet::from_str(&versions).unwrap().to_string());
|
||||||
assert_eq!(String::from(""), contract_protocol_list(&versions));
|
|
||||||
|
|
||||||
versions.insert(1);
|
versions = "1-2";
|
||||||
assert_eq!(String::from("1"), contract_protocol_list(&versions));
|
assert_eq!(String::from(versions), ProtoSet::from_str(&versions).unwrap().to_string());
|
||||||
|
|
||||||
versions.insert(2);
|
versions = "1,3";
|
||||||
assert_eq!(String::from("1-2"), contract_protocol_list(&versions));
|
assert_eq!(String::from(versions), ProtoSet::from_str(&versions).unwrap().to_string());
|
||||||
}
|
|
||||||
|
|
||||||
{
|
versions = "1-4";
|
||||||
let mut versions = HashSet::<Version>::new();
|
assert_eq!(String::from(versions), ProtoSet::from_str(&versions).unwrap().to_string());
|
||||||
versions.insert(1);
|
|
||||||
versions.insert(3);
|
|
||||||
assert_eq!(String::from("1,3"), contract_protocol_list(&versions));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
versions = "1,3,5-7";
|
||||||
let mut versions = HashSet::<Version>::new();
|
assert_eq!(String::from(versions), ProtoSet::from_str(&versions).unwrap().to_string());
|
||||||
versions.insert(1);
|
|
||||||
versions.insert(2);
|
|
||||||
versions.insert(3);
|
|
||||||
versions.insert(4);
|
|
||||||
assert_eq!(String::from("1-4"), contract_protocol_list(&versions));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
versions = "1-3,500";
|
||||||
let mut versions = HashSet::<Version>::new();
|
assert_eq!(String::from(versions), ProtoSet::from_str(&versions).unwrap().to_string());
|
||||||
versions.insert(1);
|
|
||||||
versions.insert(3);
|
|
||||||
versions.insert(5);
|
|
||||||
versions.insert(6);
|
|
||||||
versions.insert(7);
|
|
||||||
assert_eq!(
|
|
||||||
String::from("1,3,5-7"),
|
|
||||||
contract_protocol_list(&versions)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut versions = HashSet::<Version>::new();
|
|
||||||
versions.insert(1);
|
|
||||||
versions.insert(2);
|
|
||||||
versions.insert(3);
|
|
||||||
versions.insert(500);
|
|
||||||
assert_eq!(
|
|
||||||
String::from("1-3,500"),
|
|
||||||
contract_protocol_list(&versions)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,289 +3,326 @@
|
|||||||
|
|
||||||
extern crate protover;
|
extern crate protover;
|
||||||
|
|
||||||
|
use protover::ProtoEntry;
|
||||||
|
use protover::ProtoverVote;
|
||||||
|
use protover::UnvalidatedProtoEntry;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_protocol_list_with_single_proto_and_single_version() {
|
fn parse_protocol_with_single_proto_and_single_version() {
|
||||||
let protocol = "Cons=1";
|
let _: ProtoEntry = "Cons=1".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_protocol_list_with_single_protocol_and_multiple_versions() {
|
fn parse_protocol_with_single_protocol_and_multiple_versions() {
|
||||||
let protocol = "Cons=1-2";
|
let _: ProtoEntry = "Cons=1-2".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_protocol_list_with_different_single_protocol_and_single_version() {
|
fn parse_protocol_with_different_single_protocol_and_single_version() {
|
||||||
let protocol = "HSDir=1";
|
let _: ProtoEntry = "HSDir=1".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_protocol_list_with_single_protocol_and_supported_version() {
|
fn parse_protocol_with_single_protocol_and_supported_version() {
|
||||||
let protocol = "Desc=2";
|
let _: ProtoEntry = "Desc=2".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_protocol_list_with_two_protocols_and_single_version() {
|
fn parse_protocol_with_two_protocols_and_single_version() {
|
||||||
let protocols = "Cons=1 HSDir=1";
|
let _: ProtoEntry = "Cons=1 HSDir=1".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_protocol_list_with_single_protocol_and_two_nonsequential_versions() {
|
|
||||||
let protocol = "Desc=1,2";
|
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_protocol_list_with_single_protocol_and_two_sequential_versions() {
|
|
||||||
let protocol = "Desc=1-2";
|
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_protocol_list_with_single_protocol_and_protocol_range_returns_set() {
|
fn parse_protocol_with_single_protocol_and_two_sequential_versions() {
|
||||||
let protocol = "Link=1-4";
|
let _: ProtoEntry = "Desc=1-2".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_protocol_list_with_single_protocol_and_protocol_set() {
|
fn parse_protocol_with_single_protocol_and_protocol_range() {
|
||||||
let protocols = "Link=3-4 Desc=2";
|
let _: ProtoEntry = "Link=1-4".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_two_values() {
|
fn parse_protocol_with_single_protocol_and_protocol_set() {
|
||||||
let protocols = "Microdesc=1-2 Relay=2";
|
let _: ProtoEntry = "Link=3-4 Desc=2".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_one_value() {
|
fn protocol_all_supported_with_single_protocol_and_protocol_set() {
|
||||||
let protocols = "Microdesc=1-2";
|
let protocols: UnvalidatedProtoEntry = "Link=3-4 Desc=2".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
assert_eq!("", &unsupported);
|
assert_eq!(true, unsupported.is_none());
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_empty() {
|
fn protocol_all_supported_with_two_values() {
|
||||||
let protocols = "";
|
let protocols: UnvalidatedProtoEntry = "Microdesc=1-2 Relay=2".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
assert_eq!(true, is_supported);
|
assert_eq!(true, unsupported.is_none());
|
||||||
assert_eq!("", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_three_values() {
|
fn protocol_all_supported_with_one_value() {
|
||||||
let protocols = "LinkAuth=1 Microdesc=1-2 Relay=2";
|
let protocols: UnvalidatedProtoEntry = "Microdesc=1-2".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
assert_eq!("", &unsupported);
|
assert_eq!(true, unsupported.is_none());
|
||||||
assert_eq!(true, is_supported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_unsupported_protocol() {
|
#[should_panic]
|
||||||
let protocols = "Wombat=9";
|
fn parse_protocol_unvalidated_with_empty() {
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let _: UnvalidatedProtoEntry = "".parse().unwrap();
|
||||||
assert_eq!(false, is_supported);
|
|
||||||
assert_eq!("Wombat=9", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_unsupported_versions() {
|
#[should_panic]
|
||||||
let protocols = "Link=3-999";
|
fn parse_protocol_validated_with_empty() {
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let _: UnvalidatedProtoEntry = "".parse().unwrap();
|
||||||
assert_eq!(false, is_supported);
|
|
||||||
assert_eq!("Link=3-999", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_unsupported_low_version() {
|
fn protocol_all_supported_with_three_values() {
|
||||||
let protocols = "Cons=0-1";
|
let protocols: UnvalidatedProtoEntry = "LinkAuth=1 Microdesc=1-2 Relay=2".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
assert_eq!(false, is_supported);
|
assert_eq!(true, unsupported.is_none());
|
||||||
assert_eq!("Cons=0-1", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_unsupported_high_version() {
|
fn protocol_all_supported_with_unsupported_protocol() {
|
||||||
let protocols = "Cons=1-3";
|
let protocols: UnvalidatedProtoEntry = "Wombat=9".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
assert_eq!(false, is_supported);
|
assert_eq!(true, unsupported.is_some());
|
||||||
assert_eq!("Cons=1-3", &unsupported);
|
assert_eq!("Wombat=9", &unsupported.unwrap().to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_mix_of_supported_and_unsupproted() {
|
fn protocol_all_supported_with_unsupported_versions() {
|
||||||
let protocols = "Link=3-4 Wombat=9";
|
let protocols: UnvalidatedProtoEntry = "Link=3-999".parse().unwrap();
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
assert_eq!(false, is_supported);
|
assert_eq!(true, unsupported.is_some());
|
||||||
assert_eq!("Wombat=9", &unsupported);
|
assert_eq!("Link=6-999", &unsupported.unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_unsupported_low_version() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "Cons=0-1".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_some());
|
||||||
|
assert_eq!("Cons=0", &unsupported.unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_unsupported_high_version() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "Cons=1-2,999".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_some());
|
||||||
|
assert_eq!("Cons=999", &unsupported.unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_mix_of_supported_and_unsupproted() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "Link=3-4 Wombat=9".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_some());
|
||||||
|
assert_eq!("Wombat=9", &unsupported.unwrap().to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_string_supports_protocol_returns_true_for_single_supported() {
|
fn protover_string_supports_protocol_returns_true_for_single_supported() {
|
||||||
let protocols = "Link=3-4 Cons=1";
|
let protocols: UnvalidatedProtoEntry = "Link=3-4 Cons=1".parse().unwrap();
|
||||||
let is_supported = protover::protover_string_supports_protocol(
|
let is_supported = protocols.supports_protocol(&protover::Protocol::Cons.into(), &1);
|
||||||
protocols,
|
|
||||||
protover::Proto::Cons,
|
|
||||||
1,
|
|
||||||
);
|
|
||||||
assert_eq!(true, is_supported);
|
assert_eq!(true, is_supported);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_string_supports_protocol_returns_false_for_single_unsupported() {
|
fn protover_string_supports_protocol_returns_false_for_single_unsupported() {
|
||||||
let protocols = "Link=3-4 Cons=1";
|
let protocols: UnvalidatedProtoEntry = "Link=3-4 Cons=1".parse().unwrap();
|
||||||
let is_supported = protover::protover_string_supports_protocol(
|
let is_supported = protocols.supports_protocol(&protover::Protocol::Cons.into(), &2);
|
||||||
protocols,
|
|
||||||
protover::Proto::Cons,
|
|
||||||
2,
|
|
||||||
);
|
|
||||||
assert_eq!(false, is_supported);
|
assert_eq!(false, is_supported);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_string_supports_protocol_returns_false_for_unsupported() {
|
fn protover_string_supports_protocol_returns_false_for_unsupported() {
|
||||||
let protocols = "Link=3-4";
|
let protocols: UnvalidatedProtoEntry = "Link=3-4".parse().unwrap();
|
||||||
let is_supported = protover::protover_string_supports_protocol(
|
let is_supported = protocols.supports_protocol(&protover::Protocol::Cons.into(), &2);
|
||||||
protocols,
|
|
||||||
protover::Proto::Cons,
|
|
||||||
2,
|
|
||||||
);
|
|
||||||
assert_eq!(false, is_supported);
|
assert_eq!(false, is_supported);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_all_supported_with_unexpected_characters() {
|
#[should_panic]
|
||||||
let protocols = "Cons=*-%";
|
fn parse_protocol_with_unexpected_characters() {
|
||||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
let _: UnvalidatedProtoEntry = "Cons=*-%".parse().unwrap();
|
||||||
assert_eq!(false, is_supported);
|
|
||||||
assert_eq!("Cons=*-%", &unsupported);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn protover_compute_vote_returns_empty_for_empty_string() {
|
fn protover_compute_vote_returns_empty_for_empty_string() {
|
||||||
let protocols = vec![String::from("")];
|
let protocols: &[UnvalidatedProtoEntry] = &["".parse().unwrap()];
|
||||||
let listed = protover::compute_vote(protocols, 1);
|
let listed = ProtoverVote::compute(protocols, &1);
|
||||||
assert_eq!("", listed);
|
assert_eq!("", listed.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_returns_single_protocol_for_matching() {
|
fn protover_compute_vote_returns_single_protocol_for_matching() {
|
||||||
let protocols = vec![String::from("Cons=1")];
|
let protocols: &[UnvalidatedProtoEntry] = &["Cons=1".parse().unwrap()];
|
||||||
let listed = protover::compute_vote(protocols, 1);
|
let listed = ProtoverVote::compute(protocols, &1);
|
||||||
assert_eq!("Cons=1", listed);
|
assert_eq!("Cons=1", listed.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_returns_two_protocols_for_two_matching() {
|
fn protover_compute_vote_returns_two_protocols_for_two_matching() {
|
||||||
let protocols = vec![String::from("Link=1 Cons=1")];
|
let protocols: &[UnvalidatedProtoEntry] = &["Link=1 Cons=1".parse().unwrap()];
|
||||||
let listed = protover::compute_vote(protocols, 1);
|
let listed = ProtoverVote::compute(protocols, &1);
|
||||||
assert_eq!("Cons=1 Link=1", listed);
|
assert_eq!("Cons=1 Link=1", listed.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_returns_one_protocol_when_one_out_of_two_matches() {
|
fn protover_compute_vote_returns_one_protocol_when_one_out_of_two_matches() {
|
||||||
let protocols = vec![String::from("Cons=1 Link=2"), String::from("Cons=1")];
|
let protocols: &[UnvalidatedProtoEntry] = &["Cons=1 Link=2".parse().unwrap(), "Cons=1".parse().unwrap()];
|
||||||
let listed = protover::compute_vote(protocols, 2);
|
let listed = ProtoverVote::compute(protocols, &2);
|
||||||
assert_eq!("Cons=1", listed);
|
assert_eq!("Cons=1", listed.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_returns_protocols_that_it_doesnt_currently_support() {
|
fn protover_compute_vote_returns_protocols_that_it_doesnt_currently_support() {
|
||||||
let protocols = vec![String::from("Foo=1 Cons=2"), String::from("Bar=1")];
|
let protocols: &[UnvalidatedProtoEntry] = &["Foo=1 Cons=2".parse().unwrap(), "Bar=1".parse().unwrap()];
|
||||||
let listed = protover::compute_vote(protocols, 1);
|
let listed = ProtoverVote::compute(protocols, &1);
|
||||||
assert_eq!("Bar=1 Cons=2 Foo=1", listed);
|
assert_eq!("Bar=1 Cons=2 Foo=1", listed.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_returns_matching_for_mix() {
|
fn protover_compute_vote_returns_matching_for_mix() {
|
||||||
let protocols = vec![String::from("Link=1-10,500 Cons=1,3-7,8")];
|
let protocols: &[UnvalidatedProtoEntry] = &["Link=1-10,500 Cons=1,3-7,8".parse().unwrap()];
|
||||||
let listed = protover::compute_vote(protocols, 1);
|
let listed = ProtoverVote::compute(protocols, &1);
|
||||||
assert_eq!("Cons=1,3-8 Link=1-10,500", listed);
|
assert_eq!("Cons=1,3-8 Link=1-10,500", listed.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_returns_matching_for_longer_mix() {
|
fn protover_compute_vote_returns_matching_for_longer_mix() {
|
||||||
let protocols = vec![
|
let protocols: &[UnvalidatedProtoEntry] = &[
|
||||||
String::from("Desc=1-10,500 Cons=1,3-7,8"),
|
"Desc=1-10,500 Cons=1,3-7,8".parse().unwrap(),
|
||||||
String::from("Link=123-456,78 Cons=2-6,8 Desc=9"),
|
"Link=123-456,78 Cons=2-6,8 Desc=9".parse().unwrap(),
|
||||||
];
|
];
|
||||||
|
|
||||||
let listed = protover::compute_vote(protocols, 1);
|
let listed = ProtoverVote::compute(protocols, &1);
|
||||||
assert_eq!("Cons=1-8 Desc=1-10,500 Link=78,123-456", listed);
|
assert_eq!("Cons=1-8 Desc=1-10,500 Link=78,123-456", listed.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_returns_matching_for_longer_mix_with_threshold_two() {
|
fn protover_compute_vote_returns_matching_for_longer_mix_with_threshold_two() {
|
||||||
let protocols = vec![
|
let protocols: &[UnvalidatedProtoEntry] = &[
|
||||||
String::from("Desc=1-10,500 Cons=1,3-7,8"),
|
"Desc=1-10,500 Cons=1,3-7,8".parse().unwrap(),
|
||||||
String::from("Link=123-456,78 Cons=2-6,8 Desc=9"),
|
"Link=123-456,78 Cons=2-6,8 Desc=9".parse().unwrap(),
|
||||||
];
|
];
|
||||||
|
|
||||||
let listed = protover::compute_vote(protocols, 2);
|
let listed = ProtoverVote::compute(protocols, &2);
|
||||||
assert_eq!("Cons=3-6,8 Desc=9", listed);
|
assert_eq!("Cons=3-6,8 Desc=9", listed.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_handles_duplicated_versions() {
|
fn protover_compute_vote_handles_duplicated_versions() {
|
||||||
let protocols = vec![String::from("Cons=1"), String::from("Cons=1")];
|
let protocols: &[UnvalidatedProtoEntry] = &["Cons=1".parse().unwrap(), "Cons=1".parse().unwrap()];
|
||||||
assert_eq!("Cons=1", protover::compute_vote(protocols, 2));
|
assert_eq!("Cons=1", ProtoverVote::compute(protocols, &2).to_string());
|
||||||
|
|
||||||
let protocols = vec![String::from("Cons=1-2"), String::from("Cons=1-2")];
|
let protocols: &[UnvalidatedProtoEntry] = &["Cons=1-2".parse().unwrap(), "Cons=1-2".parse().unwrap()];
|
||||||
assert_eq!("Cons=1-2", protover::compute_vote(protocols, 2));
|
assert_eq!("Cons=1-2", ProtoverVote::compute(protocols, &2).to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_compute_vote_handles_invalid_proto_entries() {
|
fn protover_compute_vote_handles_invalid_proto_entries() {
|
||||||
let protocols = vec![
|
let protocols: &[UnvalidatedProtoEntry] = &[
|
||||||
String::from("Cons=1"),
|
"Cons=1".parse().unwrap(),
|
||||||
String::from("Cons=1"),
|
"Cons=1".parse().unwrap(),
|
||||||
String::from("Link=a"),
|
"Dinosaur=1".parse().unwrap(),
|
||||||
];
|
];
|
||||||
assert_eq!("Cons=1", protover::compute_vote(protocols, 2));
|
assert_eq!("Cons=1", ProtoverVote::compute(protocols, &2).to_string());
|
||||||
|
}
|
||||||
|
|
||||||
let protocols = vec![
|
#[test]
|
||||||
String::from("Cons=1"),
|
fn parse_protocol_with_single_protocol_and_two_nonsequential_versions() {
|
||||||
String::from("Cons=1"),
|
let _: ProtoEntry = "Desc=1,2".parse().unwrap();
|
||||||
String::from("Link=1-%"),
|
|
||||||
];
|
|
||||||
assert_eq!("Cons=1", protover::compute_vote(protocols, 2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_is_supported_here_returns_true_for_supported_protocol() {
|
fn protover_is_supported_here_returns_true_for_supported_protocol() {
|
||||||
assert_eq!(true, protover::is_supported_here(protover::Proto::Cons, 1));
|
assert_eq!(true, protover::is_supported_here(&protover::Protocol::Cons, &1));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn protover_is_supported_here_returns_false_for_unsupported_protocol() {
|
fn protover_is_supported_here_returns_false_for_unsupported_protocol() {
|
||||||
assert_eq!(false, protover::is_supported_here(protover::Proto::Cons, 5));
|
assert_eq!(false, protover::is_supported_here(&protover::Protocol::Cons, &5));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_single_proto_and_single_version() {
|
||||||
|
let protocol: UnvalidatedProtoEntry = "Cons=1".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocol.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_single_protocol_and_multiple_versions() {
|
||||||
|
let protocol: UnvalidatedProtoEntry = "Cons=1-2".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocol.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_different_single_protocol_and_single_version() {
|
||||||
|
let protocol: UnvalidatedProtoEntry = "HSDir=1".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocol.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_single_protocol_and_supported_version() {
|
||||||
|
let protocol: UnvalidatedProtoEntry = "Desc=2".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocol.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_two_protocols_and_single_version() {
|
||||||
|
let protocols: UnvalidatedProtoEntry = "Cons=1 HSDir=1".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocols.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_single_protocol_and_two_nonsequential_versions() {
|
||||||
|
let protocol: UnvalidatedProtoEntry = "Desc=1,2".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocol.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_single_protocol_and_two_sequential_versions() {
|
||||||
|
let protocol: UnvalidatedProtoEntry = "Desc=1-2".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocol.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_all_supported_with_single_protocol_and_protocol_range() {
|
||||||
|
let protocol: UnvalidatedProtoEntry = "Link=1-4".parse().unwrap();
|
||||||
|
let unsupported: Option<UnvalidatedProtoEntry> = protocol.all_supported();
|
||||||
|
assert_eq!(true, unsupported.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
// By allowing us to add to votes, the C implementation allows us to
|
||||||
|
// exceed the limit.
|
||||||
|
#[test]
|
||||||
|
fn protover_compute_vote_may_exceed_limit() {
|
||||||
|
let proto1: UnvalidatedProtoEntry = "Sleen=1-65535".parse().unwrap();
|
||||||
|
let proto2: UnvalidatedProtoEntry = "Sleen=100000".parse().unwrap();
|
||||||
|
|
||||||
|
let _result: UnvalidatedProtoEntry = ProtoverVote::compute(&[proto1, proto2], &1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protover_all_supported_should_include_version_we_actually_do_support() {
|
||||||
|
let proto: UnvalidatedProtoEntry = "Link=3-999".parse().unwrap();
|
||||||
|
let _result: String = proto.all_supported().unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(_result, "Link=3-999".to_string());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user