From 7d47ada07701d27adbbda9fb134e3ae2a6e9510f Mon Sep 17 00:00:00 2001 From: Toralf Wittner Date: Thu, 13 Aug 2020 12:49:16 +0200 Subject: [PATCH] Fix ping test in request-response. (#1702) The codec impl did not check that it actually read any bytes in `read_request` and `read_response`. The used `read_one` function does not error on EOF either, so instead of signalling connection loss the codec could produce empty `Ping` or `Pong` messages. --- protocols/request-response/tests/ping.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/protocols/request-response/tests/ping.rs b/protocols/request-response/tests/ping.rs index ad620898..0efec0a1 100644 --- a/protocols/request-response/tests/ping.rs +++ b/protocols/request-response/tests/ping.rs @@ -356,8 +356,11 @@ impl RequestResponseCodec for PingCodec { T: AsyncRead + Unpin + Send { read_one(io, 1024) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) - .map_ok(Ping) + .map(|res| match res { + Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)), + Ok(vec) if vec.is_empty() => Err(io::ErrorKind::UnexpectedEof.into()), + Ok(vec) => Ok(Ping(vec)) + }) .await } @@ -367,8 +370,11 @@ impl RequestResponseCodec for PingCodec { T: AsyncRead + Unpin + Send { read_one(io, 1024) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) - .map_ok(Pong) + .map(|res| match res { + Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)), + Ok(vec) if vec.is_empty() => Err(io::ErrorKind::UnexpectedEof.into()), + Ok(vec) => Ok(Pong(vec)) + }) .await }