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

Fixed the identifiers limits according to updated ics spec #189

Merged
merged 3 commits into from
Jul 31, 2020
Merged
Changes from 2 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
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)
}
}