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

Add more tests for error cases #26

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Changes from all 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
118 changes: 116 additions & 2 deletions stun-types/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1841,10 +1841,21 @@ mod tests {
let mtype = MessageType::from_class_method(c, m);
assert_eq!(mtype.class(), c);
assert_eq!(mtype.method(), m);
let bytes = mtype.to_bytes();
let ptype = MessageType::from_bytes(&bytes).unwrap();
assert_eq!(mtype, ptype);
}
}
}

#[test]
fn msg_type_not_stun() {
assert!(matches!(
MessageType::from_bytes(&[0xc0, 0x00]),
Err(StunParseError::NotStun)
));
}

#[test]
fn msg_roundtrip() {
let _log = crate::tests::test_init_log();
Expand Down Expand Up @@ -1937,6 +1948,22 @@ mod tests {
}
}

#[test]
fn write_into_short_destination() {
let _log = crate::tests::test_init_log();
let mut msg = Message::builder_request(BINDING);
let software = Software::new("s").unwrap();
msg.add_attribute(&software).unwrap();
let len = msg.byte_len();
let mut dest = vec![0; len - 1];
assert!(
matches!(msg.write_into(&mut dest), Err(StunWriteError::TooSmall {
expected,
actual,
}) if expected == len && actual == len - 1)
);
}

#[test]
fn add_duplicate_integrity() {
let _log = crate::tests::test_init_log();
Expand Down Expand Up @@ -1993,6 +2020,22 @@ mod tests {
}
}

#[test]
fn add_raw_attribute_after_integrity() {
let _log = crate::tests::test_init_log();
for algorithm in [IntegrityAlgorithm::Sha1, IntegrityAlgorithm::Sha256] {
let credentials = ShortTermCredentials::new("secret".to_owned()).into();
let mut msg = Message::builder_request(BINDING);
msg.add_message_integrity(&credentials, algorithm).unwrap();
let software = Software::new("s").unwrap();
let raw = software.to_raw();
assert!(matches!(
msg.add_raw_attribute(raw),
Err(StunWriteError::MessageIntegrityExists)
));
}
}

#[test]
fn add_integrity_after_fingerprint() {
let _log = crate::tests::test_init_log();
Expand All @@ -2007,6 +2050,31 @@ mod tests {
}
}

#[test]
fn duplicate_add_attribute() {
let _log = crate::tests::test_init_log();
let mut msg = Message::builder_request(BINDING);
let software = Software::new("s").unwrap();
msg.add_attribute(&software).unwrap();
assert!(matches!(
msg.add_attribute(&software),
Err(StunWriteError::AttributeExists(ty)) if ty == Software::TYPE
));
}

#[test]
fn duplicate_add_raw_attribute() {
let _log = crate::tests::test_init_log();
let mut msg = Message::builder_request(BINDING);
let software = Software::new("s").unwrap();
let raw = software.to_raw();
msg.add_raw_attribute(raw.clone()).unwrap();
assert!(matches!(
msg.add_raw_attribute(raw),
Err(StunWriteError::AttributeExists(ty)) if ty == Software::TYPE
));
}

#[test]
fn duplicate_fingerprint() {
let _log = crate::tests::test_init_log();
Expand Down Expand Up @@ -2132,6 +2200,19 @@ mod tests {
));
}

#[test]
fn add_raw_attribute_after_fingerprint() {
let _log = crate::tests::test_init_log();
let mut msg = Message::builder_request(BINDING);
msg.add_fingerprint().unwrap();
let software = Software::new("s").unwrap();
let raw = software.to_raw();
assert!(matches!(
msg.add_raw_attribute(raw),
Err(StunWriteError::FingerprintExists)
));
}

#[test]
fn parse_truncated_message_header() {
let _log = crate::tests::test_init_log();
Expand Down Expand Up @@ -2263,6 +2344,17 @@ mod tests {
msg.add_attribute(&integrity).unwrap();
}

#[test]
#[should_panic(expected = "Use add_message_integrity() instead")]
fn builder_add_raw_attribute_integrity_panic() {
let _log = crate::tests::test_init_log();
let mut msg = Message::builder_request(BINDING);
let hmac = [2; 20];
let integrity = MessageIntegrity::new(hmac);
let raw = integrity.to_raw();
msg.add_raw_attribute(raw).unwrap();
}

#[test]
#[should_panic(expected = "Use add_message_integrity() instead")]
fn builder_add_attribute_integrity_sha256_panic() {
Expand All @@ -2273,14 +2365,36 @@ mod tests {
msg.add_attribute(&integrity).unwrap();
}

#[test]
#[should_panic(expected = "Use add_message_integrity() instead")]
fn builder_add_raw_attribute_integrity_sha256_panic() {
let _log = crate::tests::test_init_log();
let mut msg = Message::builder_request(BINDING);
let hmac = [2; 16];
let integrity = MessageIntegritySha256::new(&hmac).unwrap();
let raw = integrity.to_raw();
msg.add_raw_attribute(raw).unwrap();
}

#[test]
#[should_panic(expected = "Use add_fingerprint() instead")]
fn builder_add_attribute_fingerprint_panic() {
let _log = crate::tests::test_init_log();
let mut msg = Message::builder_request(BINDING);
let fingerprint = [2; 4];
let integrity = Fingerprint::new(fingerprint);
msg.add_attribute(&integrity).unwrap();
let fingerprint = Fingerprint::new(fingerprint);
msg.add_attribute(&fingerprint).unwrap();
}

#[test]
#[should_panic(expected = "Use add_fingerprint() instead")]
fn builder_add_raw_attribute_fingerprint_panic() {
let _log = crate::tests::test_init_log();
let mut msg = Message::builder_request(BINDING);
let fingerprint = [2; 4];
let fingerprint = Fingerprint::new(fingerprint);
let raw = fingerprint.to_raw();
msg.add_raw_attribute(raw).unwrap();
}

#[test]
Expand Down
Loading