Skip to content

Commit

Permalink
Add traces from RFC 5321 (Appendix D. Scenarios) as tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
duesee committed May 24, 2021
1 parent 2d4ea2f commit c056f62
Showing 1 changed file with 203 additions and 0 deletions.
203 changes: 203 additions & 0 deletions tests/trace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
use nom::FindSubstring;
use smtp_codec::{
parse::{
command::command,
response::{ehlo_ok_rsp, Greeting, Reply_line},
},
types::Command,
};

fn parse_trace(mut trace: &[u8]) {
let (rem, greeting) = Greeting(trace).unwrap();
println!("S: {:?}", greeting);
trace = rem;

loop {
if trace.is_empty() {
break;
}

let (rem, cmd) = command(trace).unwrap();
println!("C: {:?}", cmd);
trace = rem;

match cmd {
Command::Ehlo { .. } => {
let (rem, rsp) = ehlo_ok_rsp(trace).unwrap();
println!("S: {:?}", rsp);
trace = rem;
}
Command::Data { .. } => {
let (rem, rsp) = Reply_line(trace).unwrap();
println!("S: {:?}", rsp);
trace = rem;

let pos = trace.find_substring("\r\n.\r\n").unwrap();
let (data, rem) = trace.split_at(pos + 5);
println!("C (data): <{}>", std::str::from_utf8(data).unwrap());
trace = rem;

let (rem, rsp) = Reply_line(trace).unwrap();
println!("S: {:?}", rsp);
trace = rem;
}
_ => {
let (rem, rsp) = Reply_line(trace).unwrap();
println!("S: {:?}", rsp);
trace = rem;
}
}
}
}

#[test]
/// D.1. A Typical SMTP Transaction Scenario
fn test_trace_d_1() {
let trace = b"\
220 foo.com Simple Mail Transfer Service Ready\r
EHLO bar.com\r
250-foo.com greets bar.com\r
250-8BITMIME\r
250-SIZE\r
250-DSN\r
250 HELP\r
MAIL FROM:<[email protected]>\r
250 OK\r
RCPT TO:<[email protected]>\r
250 OK\r
RCPT TO:<[email protected]>\r
550 No such user here\r
RCPT TO:<[email protected]>\r
250 OK\r
DATA\r
354 Start mail input; end with <CRLF>.<CRLF>\r
Blah blah blah...\r
...etc. etc. etc.\r
.\r
250 OK\r
QUIT\r
221 foo.com Service closing transmission channel\r
";

parse_trace(trace);
}

#[test]
/// D.2. Aborted SMTP Transaction Scenario
fn test_trace_d_2() {
let trace = b"\
220 foo.com Simple Mail Transfer Service Ready\r
EHLO bar.com\r
250-foo.com greets bar.com\r
250-8BITMIME\r
250-SIZE\r
250-DSN\r
250 HELP\r
MAIL FROM:<[email protected]>\r
250 OK\r
RCPT TO:<[email protected]>\r
250 OK\r
RCPT TO:<[email protected]>\r
550 No such user here\r
RSET\r
250 OK\r
QUIT\r
221 foo.com Service closing transmission channel\r
";

parse_trace(trace);
}

#[test]
/// D.3. Relayed Mail Scenario
fn test_trace_d_3() {
let step_1 = b"\
220 foo.com Simple Mail Transfer Service Ready\r
EHLO bar.com\r
250-foo.com greets bar.com\r
250-8BITMIME\r
250-SIZE\r
250-DSN\r
250 HELP\r
MAIL FROM:<[email protected]>\r
250 OK\r
RCPT TO:<[email protected]>\r
250 OK\r
DATA\r
354 Start mail input; end with <CRLF>.<CRLF>\r
Date: Thu, 21 May 1998 05:33:29 -0700\r
From: John Q. Public <[email protected]>\r
Subject: The Next Meeting of the Board\r
To: [email protected]\r
\r
Bill:\r
The next meeting of the board of directors will be\r
on Tuesday.\r
John.\r
.\r
250 OK\r
QUIT\r
221 foo.com Service closing transmission channel\r
";

let step_2 = b"\
220 xyz.com Simple Mail Transfer Service Ready\r
EHLO foo.com\r
250 xyz.com is on the air\r
MAIL FROM:<[email protected]>\r
250 OK\r
RCPT TO:<[email protected]>\r
250 OK\r
DATA\r
354 Start mail input; end with <CRLF>.<CRLF>\r
Received: from bar.com by foo.com ; Thu, 21 May 1998\r
05:33:29 -0700\r
Date: Thu, 21 May 1998 05:33:22 -0700\r
From: John Q. Public <[email protected]>\r
Subject: The Next Meeting of the Board\r
To: [email protected]\r
\r
Bill:\r
The next meeting of the board of directors will be\r
on Tuesday.\r
John.\r
.\r
250 OK\r
QUIT\r
221 foo.com Service closing transmission channel\r
";

parse_trace(step_1);
parse_trace(step_2);
}

#[test]
/// D.4. Verifying and Sending Scenario
fn test_trace_d_4() {
let trace = b"\
220 foo.com Simple Mail Transfer Service Ready\r
EHLO bar.com\r
250-foo.com greets bar.com\r
250-8BITMIME\r
250-SIZE\r
250-DSN\r
250-VRFY\r
250 HELP\r
VRFY Crispin\r
250 Mark Crispin <[email protected]>\r
MAIL FROM:<[email protected]>\r
250 OK\r
RCPT TO:<[email protected]>\r
250 OK\r
DATA\r
354 Start mail input; end with <CRLF>.<CRLF>\r
Blah blah blah...\r
...etc. etc. etc.\r
.\r
250 OK\r
QUIT\r
221 foo.com Service closing transmission channel\r
";

parse_trace(trace);
}

0 comments on commit c056f62

Please sign in to comment.