Skip to content

Commit

Permalink
fuzz: test against libtelnet-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
cpu committed Nov 6, 2023
1 parent d9602f4 commit 86245ae
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
9 changes: 9 additions & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }

[dependencies.libmudtelnet]
path = ".."
[dependencies.libtelnet-rs]
git = "https://github.com/cpu/libtelnet-rs"
rev = "f127040a8419c92c034dee2ff331137d21dc1940"

# Prevent this from interfering with workspaces
[workspace]
Expand Down
51 changes: 48 additions & 3 deletions fuzz/parser/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use libfuzzer_sys::arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;

use libmudtelnet::compatibility::CompatibilityTable;
use libmudtelnet::events::TelnetEvents;
use libmudtelnet::Parser;

#[derive(Arbitrary, Debug)]
Expand All @@ -13,10 +14,54 @@ struct TelnetApplication {
received_data: Vec<Vec<u8>>,
}

fuzz_target!(|app: TelnetApplication| {
fn test_app(app: &TelnetApplication) {
let mut parser = Parser::with_support(CompatibilityTable::from_options(&app.options));
let mut og_parser = libtelnet_rs::Parser::with_support(
libtelnet_rs::compatibility::CompatibilityTable::from_options(&app.options),
);

for data in &app.received_data {
let events = parser.receive(&data);
let og_events = og_parser.receive(&data);

use libtelnet_rs::events::TelnetEvents as og_events;

for data in app.received_data {
parser.receive(&data);
assert_eq!(events.len(), og_events.len());
for (i, event) in events.iter().enumerate() {
let og_event = &og_events[i];
match (event, og_event) {
(TelnetEvents::IAC(iac), og_events::IAC(og_iac)) => {
assert_eq!(iac.to_bytes(), og_iac.into_bytes());
}
(TelnetEvents::Negotiation(neg), og_events::Negotiation(og_neg)) => {
assert_eq!(neg.to_bytes(), og_neg.into_bytes());
}
(TelnetEvents::Subnegotiation(subneg), og_events::Subnegotiation(og_subneg)) => {
assert_eq!(subneg.clone().to_bytes(), og_subneg.clone().into_bytes());
}

(TelnetEvents::DataReceive(dr), og_events::DataReceive(og_dr)) => {
assert_eq!(dr, og_dr);
}
(TelnetEvents::DataSend(ds), og_events::DataSend(og_ds)) => {
assert_eq!(ds, og_ds);
}
(TelnetEvents::DecompressImmediate(di), og_events::DecompressImmediate(og_di)) => {
assert_eq!(di, og_di);
}
_ => panic!("mismatched events: {:?} {:?}", event, og_event),
}
}
}

for i in 0..255 {
assert_eq!(
parser.options.get_option(i).into_u8(),
og_parser.options.get_option(i).into_u8()
);
}
}

fuzz_target!(|app: TelnetApplication| {
test_app(&app);
});

0 comments on commit 86245ae

Please sign in to comment.