Skip to content

Commit

Permalink
chore: Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaan26 committed Oct 8, 2024
1 parent b1ec8b0 commit 0bd0906
Showing 1 changed file with 69 additions and 10 deletions.
79 changes: 69 additions & 10 deletions zung_parsers/src/bencode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'a> Bencode<'a> {
let value = self.parse_dictionary()?;
Ok(Value::Dictionary(value))
}
t => bail!("Invalid bencode format: {t}"),
_ => bail!("Invalid bencode format"),
}
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl<'a> Bencode<'a> {

// Handle leading zeros (only '0' is allowed to start with zero, otherwise it's invalid)
if int_bytes.starts_with(b"0") && int_bytes.len() > 1 {
bail!("Invalid integer bencode format: leading zeros");
bail!("Invalid integer bencode integer format: leading zeros");
}

// Apply the negative sign if necessary
Expand Down Expand Up @@ -156,7 +156,10 @@ impl<'a> Bencode<'a> {
// • acc = 12: after the third byte (b'3'), it becomes acc = 12 * 10 + 3 = 123.
Ok(acc * 10 + (byte - b'0') as usize)
} else {
bail!("Non Digit character found in the length of the string: {byte}")
bail!(
"Non Digit character found in the length of the string: '{}'",
String::from_utf8([*byte].to_vec())?
)
}
})?;

Expand All @@ -182,16 +185,11 @@ impl<'a> Bencode<'a> {
list.push(self.parse()?);
}

// Ensure there is an 'e' tag to consume
if self.input.is_empty() {
bail!("Unexpected EOF, missing list end character 'e'");
}

// eat the 'e' tag
if self.input.first() == Some(&b'e') {
self.input = &self.input[1..];
} else {
bail!("Invalid dictionary format: missing 'e'");
bail!("Invalid list format: missing 'e'");
}

Ok(list)
Expand Down Expand Up @@ -240,19 +238,43 @@ mod tests {
"Invalid string bencode format: length is higher than the remaining bytes",
bencode_err.unwrap_err().to_string()
);

let bencode_err = Bencode::from_bytes(b"1d0:hello");
assert!(bencode_err.is_err());
assert_eq!(
"Non Digit character found in the length of the string: 'd'",
bencode_err.unwrap_err().to_string()
);
}

#[test]
fn test_parse_integer() {
let bencode = Bencode::from_bytes(b"i21e").unwrap();
assert_eq!(Value::Integer(21), bencode);

let bencode = Bencode::from_bytes(b"i-21e").unwrap();
assert_eq!(Value::Integer(-21), bencode);

let bencode_err = Bencode::from_bytes(b"i32je");
assert!(bencode_err.is_err());
assert_eq!(
"Invalid character in bencode integer",
bencode_err.unwrap_err().to_string()
);

let bencode_err = Bencode::from_bytes(b"ie");
assert!(bencode_err.is_err());
assert_eq!(
"Invalid integer bencode format: empty integer",
bencode_err.unwrap_err().to_string()
);

let bencode_err = Bencode::from_bytes(b"i004e");
assert!(bencode_err.is_err());
assert_eq!(
"Invalid integer bencode integer format: leading zeros",
bencode_err.unwrap_err().to_string()
);
}

#[test]
Expand All @@ -271,8 +293,45 @@ mod tests {
let bencode_err = Bencode::from_bytes(b"li32ei42ei52e5:hello");
assert!(bencode_err.is_err());
assert_eq!(
"Unexpected EOF, missing list end character 'e'",
"Invalid list format: missing 'e'",
bencode_err.unwrap_err().to_string()
);
}

#[test]
fn test_dictionary_bencode() {
let bencode = Bencode::from_string("d3:cow3:moo4:spam4:eggse").unwrap();
let mut dictionary = HashMap::new();
dictionary.insert("cow".to_string(), Value::String("moo".to_string()));
dictionary.insert("spam".to_string(), Value::String("eggs".to_string()));
assert_eq!(bencode, Value::Dictionary(dictionary));

let bencode_err = Bencode::from_string("di2e3:moo4:spam4:eggse");
assert!(bencode_err.is_err());
assert_eq!(
"Only string values are allowed as dictionary keys",
bencode_err.unwrap_err().to_string()
);
}

#[test]
fn invalid_becode() {
let bencode_err = Bencode::from_string("werd");

assert!(bencode_err.is_err());
assert_eq!(
"Invalid bencode format",
bencode_err.unwrap_err().to_string()
);
}

#[test]
fn test_empty_input() {
let bencode = Bencode::from_string("");
assert!(bencode.is_err());
assert_eq!(
"Unexpected end of input while parsing list",
bencode.unwrap_err().to_string()
);
}
}

0 comments on commit 0bd0906

Please sign in to comment.