Skip to content

Commit

Permalink
Fixed the identifiers limits according to updated ics spec (#189)
Browse files Browse the repository at this point in the history
* Fixed the identifiers limits according to updated ics spec. Added validation tests. #168

* Fixing test cases after limit updates #189
  • Loading branch information
andynog committed Jul 31, 2020
1 parent 9482fd2 commit acba7ad
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 22 deletions.
8 changes: 6 additions & 2 deletions modules/src/ics03_connection/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ mod tests {
Test {
name: "Bad destination connection id, name too long".to_string(),
params: ConOpenInitParams {
counterparty_connection_id: "abcdefghijklmnopqrstu".to_string(),
counterparty_connection_id:
"abcdefghijksdffjssdkflweldflsfladfsfwjkrekcmmsdfsdfjflddmnopqrstu"
.to_string(),
..default_con_params.clone()
},
want_pass: false,
Expand Down Expand Up @@ -431,7 +433,9 @@ mod tests {
Test {
name: "Bad destination connection id, name too long".to_string(),
params: ConOpenTryParams {
counterparty_connection_id: "abcdefghijklmnopqrstu".to_string(),
counterparty_connection_id:
"abcdasdfasdfsdfasfdwefwfsdfsfsfasfwewvxcvdvwgadvaadsefghijklmnopqrstu"
.to_string(),
..default_con_params.clone()
},
want_pass: false,
Expand Down
28 changes: 16 additions & 12 deletions modules/src/ics04_channel/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ mod tests {
Test {
name: "Bad port, name too long".to_string(),
params: OpenTryParams {
port_id: "abcdefghijklmnopqrstu".to_string(),
port_id: "abcdefghijasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadgasgasdfasdfasdfasdfaklmnopqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand All @@ -782,7 +782,7 @@ mod tests {
Test {
name: "Bad channel, name too long".to_string(),
params: OpenTryParams {
channel_id: "abcdefghijklmnopqrstu".to_string(),
channel_id: "abcdefghijkasdfasdfasdfasgdasdgasdfasdfadflmnoasdasdasdfasdfasdfasdfadadgadgadsfpqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand Down Expand Up @@ -822,7 +822,7 @@ mod tests {
Test {
name: "Bad connection hops, connection id too long".to_string(),
params: OpenTryParams {
connection_hops: vec!["abcdefghijklmnopqrstu".to_string()]
connection_hops: vec!["abcdefghadvvxvczxcvzxvxvzxvcsddsfsdsdfasdfasfasdasdgasdfasdfasdfadsfasdfijklmnopqrstu".to_string()]
.into_iter()
.collect(),
..default_params.clone()
Expand Down Expand Up @@ -857,7 +857,7 @@ mod tests {
Test {
name: "Bad counterparty port, name too long".to_string(),
params: OpenTryParams {
counterparty_port_id: "abcdefghijklmnopqrstu".to_string(),
counterparty_port_id: "abcdefgaszdsfgasdasdvsdfasdfasdfdfasdfasdfadsgasdfasdfasdfasdfasdfasdfhijklmnopqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand Down Expand Up @@ -955,7 +955,7 @@ mod tests {
Test {
name: "Bad port, name too long".to_string(),
params: OpenAckParams {
port_id: "abcdefghijklmnopqrstu".to_string(),
port_id: "abcdezdfDfsdfgfddsfsfdsdfdfvxcvzxcvsgdfsdfwefwvsdfdsfdasgagadgsadgsdffghijklmnopqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand All @@ -979,7 +979,7 @@ mod tests {
Test {
name: "Bad channel, name too long".to_string(),
params: OpenAckParams {
channel_id: "abcdefghijklmnopqrstu".to_string(),
channel_id: "abcdefghsdfasdfasfdasfdwewefsdfasdfasdfasdfasdfasdfsfdijklmnopqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand Down Expand Up @@ -1078,7 +1078,7 @@ mod tests {
Test {
name: "Bad port, name too long".to_string(),
params: OpenConfirmParams {
port_id: "abcdefghijklmnopqrstu".to_string(),
port_id: "abcdesdfasdsdffasdfasdfasfasdgasdfgasdfasdfasdfasdfasdfasdffghijklmnopqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand All @@ -1102,7 +1102,7 @@ mod tests {
Test {
name: "Bad channel, name too long".to_string(),
params: OpenConfirmParams {
channel_id: "abcdefghijklmnopqrstu".to_string(),
channel_id: "abcdefghijklmnoasdfasdfasdfasdfasdgsdghasdfasdfasdfasdfadsfasgdasdfasdfasfdpqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand Down Expand Up @@ -1188,7 +1188,7 @@ mod tests {
Test {
name: "Bad port, name too long".to_string(),
params: CloseInitParams {
port_id: "abcdefghijklmnopqrstu".to_string(),
port_id: "abcdefsdfasdfasdfasdfasdfasdfadsfasdgafsgadfasdfasdfasdfsdfasdfaghijklmnopqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand All @@ -1212,7 +1212,7 @@ mod tests {
Test {
name: "Bad channel, name too long".to_string(),
params: CloseInitParams {
channel_id: "abcdefghijklmnopqrstu".to_string(),
channel_id: "abcdeasdfasdfasdfasdfasdfasdfasdfasdfdgasdfasdfasdfghijklmnopqrstu".to_string(),
..default_params.clone()
},
want_pass: false,
Expand Down Expand Up @@ -1288,7 +1288,9 @@ mod tests {
Test {
name: "Bad port, name too long".to_string(),
params: CloseConfirmParams {
port_id: "abcdefghijklmnopqrstu".to_string(),
port_id:
"abcdefghijklmnsdfasdfasdfasdfasdgafgadsfasdfasdfasdasfdasdfsadfopqrstu"
.to_string(),
..default_params.clone()
},
want_pass: false,
Expand All @@ -1312,7 +1314,9 @@ mod tests {
Test {
name: "Bad channel, name too long".to_string(),
params: CloseConfirmParams {
channel_id: "abcdefghijklmnopqrstu".to_string(),
channel_id:
"abcdefghiasdfadsfasdfgdfsadfasdasdfasdasdfasddsfasdfasdjklmnopqrstu"
.to_string(),
..default_params.clone()
},
want_pass: false,
Expand Down
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 acba7ad

Please sign in to comment.