Skip to content

Commit

Permalink
Add a test for checking max. table identifier length
Browse files Browse the repository at this point in the history
  • Loading branch information
vrmiguel committed Aug 14, 2023
1 parent b0281eb commit c54400a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ pub fn check_input(input: &str) -> Result<(), PgmqError> {
// The maximum length of an identifier.
// Longer names can be used in commands, but they'll be truncated
const MAX_IDENTIFIER_LEN: usize = NAMEDATALEN - 1;
// The max length of a PGMQ table, considering its prefix
const MAX_PGMQ_TABLE_LEN: usize = MAX_IDENTIFIER_LEN - TABLE_PREFIX.len();
// The max length of a PGMQ table, considering its prefix and the underline after it (e.g. "pgmq_")
const MAX_PGMQ_TABLE_LEN: usize = MAX_IDENTIFIER_LEN - TABLE_PREFIX.len() - 1;

let is_short_enough = input.len() <= MAX_PGMQ_TABLE_LEN;
let has_valid_characters = input
Expand Down Expand Up @@ -386,6 +386,18 @@ mod tests {
}
}

#[test]
fn check_input_rejects_names_too_large() {
let table_name = "my_valid_table_name";
assert!(check_input(table_name).is_ok());

assert!(check_input(&"a".repeat(58)).is_ok());

assert!(check_input(&"a".repeat(59)).is_err());
assert!(check_input(&"a".repeat(60)).is_err());
assert!(check_input(&"a".repeat(70)).is_err());
}

#[test]
fn test_check_input() {
let invalids = vec!["bad;queue_name", "bad name", "bad--name"];
Expand Down

0 comments on commit c54400a

Please sign in to comment.