Skip to content

Commit

Permalink
Fixed the identifiers limits according to updated ics spec. Added val…
Browse files Browse the repository at this point in the history
…idation tests. #168
  • Loading branch information
andynog committed Jul 31, 2020
1 parent 9c733d7 commit 6574b5d
Showing 1 changed file with 101 additions and 8 deletions.
109 changes: 101 additions & 8 deletions modules/src/ics24_host/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,125 @@ pub fn validate_identifier(id: &str, min: usize, max: usize) -> Result<(), Valid

/// Default validator function for Client identifiers.
///
/// A valid identifier must be between 9-20 characters and only contain lowercase
/// A valid identifier must be between 9-64 characters and only contain lowercase
/// alphabetic characters,
pub fn validate_client_identifier(id: &str) -> Result<(), ValidationError> {
validate_identifier(id, 9, 20)
validate_identifier(id, 9, 64)
}

/// Default validator function for Connection identifiers.
///
/// A valid Identifier must be between 10-20 characters and only contain lowercase
/// A valid Identifier must be between 10-64 characters and only contain lowercase
/// alphabetic characters,
pub fn validate_connection_identifier(id: &str) -> Result<(), ValidationError> {
validate_identifier(id, 10, 20)
validate_identifier(id, 10, 64)
}

/// Default validator function for Port identifiers.
///
/// A valid Identifier must be between 2-20 characters and only contain lowercase
/// A valid Identifier must be between 2-64 characters and only contain lowercase
/// alphabetic characters,
pub fn validate_port_identifier(id: &str) -> Result<(), ValidationError> {
validate_identifier(id, 2, 20)
validate_identifier(id, 2, 64)
}

/// Default validator function for Channel identifiers.
///
/// A valid Identifier must be between 10-20 characters and only contain lowercase
/// A valid Identifier must be between 10-64 characters and only contain lowercase
/// alphabetic characters,
pub fn validate_channel_identifier(id: &str) -> Result<(), ValidationError> {
validate_identifier(id, 10, 20)
validate_identifier(id, 10, 64)
}

#[cfg(test)]
mod tests {
use crate::ics24_host::validate::{
validate_channel_identifier, validate_client_identifier, validate_connection_identifier,
validate_identifier, validate_port_identifier,
};

#[test]
fn parse_invalid_port_id_min() {
// invalid min port id
let id = validate_port_identifier("p");
assert!(id.is_err(), true)
}

#[test]
fn parse_invalid_port_id_max() {
// invalid max port id (test string length is 65 chars)
let id = validate_port_identifier(
"9anxkcme6je544d5lnj46zqiiiygfqzf8w4bjecbnyj4lj6s7zlpst67yln64tixp",
);
assert!(id.is_err(), true)
}

#[test]
fn parse_invalid_connection_id_min() {
// invalid min connection id
let id = validate_connection_identifier("connect01");
assert!(id.is_err(), true)
}

#[test]
fn parse_connection_id_max() {
// invalid max connection id (test string length is 65)
let id = validate_connection_identifier(
"ihhankr30iy4nna65hjl2wjod7182io1t2s7u3ip3wqtbbn1sl0rgcntqc540r36r",
);
assert!(id.is_err(), true)
}

#[test]
fn parse_invalid_client_id_min() {
// invalid min client id
let id = validate_client_identifier("client");
assert!(id.is_err(), true)
}

#[test]
fn parse_client_id_max() {
// invalid max client id (test string length is 65)
let id = validate_client_identifier(
"f0isrs5enif9e4td3r2jcbxoevhz6u1fthn4aforq7ams52jn5m48eiesfht9ckpn",
);
assert!(id.is_err(), true)
}

#[test]
fn parse_invalid_channel_id_min() {
// invalid min channel id
let id = validate_channel_identifier("channel");
assert!(id.is_err(), true)
}

#[test]
fn parse_channel_id_max() {
// invalid max channel id (test string length is 65)
let id = validate_channel_identifier(
"hlkbzrbmrh0rjrh8f8a8d9lmtjuhww7a9ev3blskc58amhwfq07zwp1xevxz1p098",
);
assert!(id.is_err(), true)
}

#[test]
fn parse_invalid_id_chars() {
// invalid id chars
let id = validate_identifier("channel@01", 1, 10);
assert!(id.is_err(), true)
}

#[test]
fn parse_invalid_id_empty() {
// invalid id empty
let id = validate_identifier("", 1, 10);
assert!(id.is_err(), true)
}

#[test]
fn parse_invalid_id_path_separator() {
// invalid id with path separator
let id = validate_identifier("id/1", 1, 10);
assert!(id.is_err(), true)
}
}

0 comments on commit 6574b5d

Please sign in to comment.