onionwire/tests/dispatch.rs

54 lines
1.5 KiB
Rust
Raw Permalink Normal View History

//! M6: typed-frame dispatcher — classify before append_message.
use onionwire::dispatch::{classify, Kind};
#[test]
fn chat_is_chat() {
assert_eq!(classify(b"hello wire"), Kind::Chat);
}
#[test]
fn loc_still_loc() {
assert_eq!(classify(b"loc abc.onion\n1\n00"), Kind::Loc);
}
#[test]
fn unknown_typed_prefix_is_drop() {
assert_eq!(classify(b"zzz not a real type"), Kind::Drop);
}
#[test]
fn short_or_binary_without_prefix_is_chat() {
assert_eq!(classify(b"hi"), Kind::Chat);
assert_eq!(classify(&[0xff, 0xfe]), Kind::Chat);
}
#[test]
fn known_prefixes_are_typed() {
assert_eq!(classify(b"prf name"), Kind::Profile);
assert_eq!(classify(b"inv 1"), Kind::Invoice);
assert_eq!(classify(b"rcp tx"), Kind::Receipt);
assert_eq!(classify(b"tip tx"), Kind::Tip);
assert_eq!(classify(b"png 1"), Kind::Ping);
}
#[test]
fn ratchet_frame_is_ratchet() {
// `rat ` is a typed prefix, not chat: the plaintext is a ratchet frame.
assert_eq!(classify(b"rat \x01rest-of-frame"), Kind::Ratchet);
// Matched before the unknown-lowercase-prefix rule, which would Drop it.
assert_eq!(classify(b"rat x"), Kind::Ratchet);
}
#[test]
fn unknown_lowercase_prefix_still_drops() {
assert_eq!(classify(b"rax not a real type"), Kind::Drop);
assert_eq!(classify(b"rat"), Kind::Chat, "3 letters is not a prefix");
}
#[test]
fn uppercase_or_digit_prefix_is_chat() {
assert_eq!(classify(b"ZZZ not typed"), Kind::Chat);
assert_eq!(classify(b"ab1 leftover"), Kind::Chat);
assert_eq!(classify(b"abcd"), Kind::Chat);
}