Skip to content

Commit

Permalink
test: Add tests for \r handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jakoschiko committed Feb 21, 2024
1 parent 2c4a1b8 commit 2a51bd2
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 3 deletions.
22 changes: 19 additions & 3 deletions flow-test/src/client_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ impl ClientTester {
}
}

pub async fn receive_error_because_malformed_message(&mut self, expected_bytes: &[u8]) {
let error = match self.connection_state.take() {
async fn receive_error(&mut self) -> ClientFlowError {
match self.connection_state.take() {
ConnectionState::Connected { stream } => {
let stream = AnyStream::new(stream);
ClientFlow::receive_greeting(stream, self.client_flow_options.clone())
Expand All @@ -154,7 +154,23 @@ impl ClientTester {
ConnectionState::Disconnected => {
panic!("Client is already disconnected")
}
};
}
}

pub async fn receive_error_because_expected_crlf_got_lf(&mut self, expected_bytes: &[u8]) {
let error = self.receive_error().await;
match error {
ClientFlowError::ExpectedCrlfGotLf { discarded_bytes } => {
assert_eq!(expected_bytes.as_bstr(), discarded_bytes.as_bstr());
}
error => {
panic!("Client emitted unexpected error: {error:?}");
}
}
}

pub async fn receive_error_because_malformed_message(&mut self, expected_bytes: &[u8]) {
let error = self.receive_error().await;
match error {
ClientFlowError::MalformedMessage { discarded_bytes } => {
assert_eq!(expected_bytes.as_bstr(), discarded_bytes.as_bstr());
Expand Down
13 changes: 13 additions & 0 deletions flow-test/src/server_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ impl ServerTester {
}
}

pub async fn receive_error_because_expected_crlf_got_lf(&mut self, expected_bytes: &[u8]) {
let server = self.connection_state.greeted();
let error = server.progress().await.unwrap_err();
match error {
ServerFlowError::ExpectedCrlfGotLf { discarded_bytes } => {
assert_eq!(expected_bytes.as_bstr(), discarded_bytes.as_bstr());
}
error => {
panic!("Server has unexpected error: {error:?}");
}
}
}

pub async fn receive_error_because_malformed_message(&mut self, expected_bytes: &[u8]) {
let server = self.connection_state.greeted();
let error = server.progress().await.unwrap_err();
Expand Down
56 changes: 56 additions & 0 deletions flow-test/tests/flow-test-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,62 @@ fn gibberish_instead_of_response() {
);
}

#[test]
fn greeting_with_missing_cr() {
let (rt, mut server, mut client) = TestSetup::default().setup_client();

// Greeting with missing \r
let greeting = b"* OK ...\n";
rt.run2(
server.send(greeting),
client.receive_error_because_expected_crlf_got_lf(greeting),
);
}

#[test]
fn response_with_missing_cr() {
let (rt, mut server, mut client) = TestSetup::default().setup_client();

let greeting = b"* OK ...\r\n";
rt.run2(server.send(greeting), client.receive_greeting(greeting));

let noop = b"A1 NOOP\r\n";
rt.run2(client.send_command(noop), server.receive(noop));

// Response with missing \r
let status = b"A1 OK ...\n";
rt.run2(
server.send(status),
client.receive_error_because_expected_crlf_got_lf(status),
);
}

#[test]
fn crlf_relaxed() {
let mut setup = TestSetup::default();
setup.client_flow_options.crlf_relaxed = true;

let (rt, mut server, mut client) = setup.setup_client();

// Greeting with missing \r
let greeting = b"* OK ...\n";
rt.run2(server.send(greeting), client.receive_greeting(greeting));

let noop = b"A1 NOOP\r\n";
rt.run2(client.send_command(noop), server.receive(noop));

// Response with missing \r
let status = b"A1 OK ...\n";
rt.run2(server.send(status), client.receive_status(status));

let noop = b"A2 NOOP\r\n";
rt.run2(client.send_command(noop), server.receive(noop));

// Response with \r still works
let status = b"A2 OK ...\r\n";
rt.run2(server.send(status), client.receive_status(status));
}

#[test]
fn login_with_literal() {
let (rt, mut server, mut client) = TestSetup::default().setup_client();
Expand Down
40 changes: 40 additions & 0 deletions flow-test/tests/flow-test-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,46 @@ fn gibberish_instead_of_command() {
);
}

#[test]
fn command_with_missing_cr() {
let (rt, mut server, mut client) = TestSetup::default().setup_server();

let greeting = b"* OK ...\r\n";
rt.run2(server.send_greeting(greeting), client.receive(greeting));

// Command with missing \r
let noop = b"A1 NOOP\n";
rt.run2(
client.send(noop),
server.receive_error_because_expected_crlf_got_lf(noop),
);
}

#[test]
fn crlf_relaxed() {
let mut setup = TestSetup::default();
setup.server_flow_options.crlf_relaxed = true;

let (rt, mut server, mut client) = setup.setup_server();

let greeting = b"* OK ...\r\n";
rt.run2(server.send_greeting(greeting), client.receive(greeting));

// Command with missing \r
let noop = b"A1 NOOP\n";
rt.run2(client.send(noop), server.receive_command(noop));

let status = b"A1 OK ...\r\n";
rt.run2(server.send_status(status), client.receive(status));

// Command with \r still works
let noop = b"A2 NOOP\r\n";
rt.run2(client.send(noop), server.receive_command(noop));

let status = b"A2 OK ...\r\n";
rt.run2(server.send_status(status), client.receive(status));
}

#[test]
fn login_with_literal() {
// The server will accept the literal ABCDE because it's smaller than the max size
Expand Down
1 change: 1 addition & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct ClientFlowOptions {
pub crlf_relaxed: bool,
}

#[allow(clippy::derivable_impls)]
impl Default for ClientFlowOptions {
fn default() -> Self {
Self {
Expand Down

0 comments on commit 2a51bd2

Please sign in to comment.