diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs index 26851ade..ab559b46 100644 --- a/openssl/src/ssl/mod.rs +++ b/openssl/src/ssl/mod.rs @@ -899,14 +899,14 @@ impl Read for SslStream { impl Write for SslStream { fn write(&mut self, buf: &[u8]) -> io::Result { - match self.in_retry_wrapper(|ssl| ssl.write(buf)) { - Ok(len) => Ok(len as usize), - Err(SslSessionClosed) => Ok(0), + let count = match self.in_retry_wrapper(|ssl| ssl.write(buf)) { + Ok(len) => len as usize, + Err(SslSessionClosed) => 0, Err(StreamError(e)) => return Err(e), - Err(e @ OpenSslErrors(_)) => { - Err(io::Error::new(io::ErrorKind::Other, e)) - } - } + Err(e @ OpenSslErrors(_)) => return Err(io::Error::new(io::ErrorKind::Other, e)), + }; + try!(self.write_through()); + Ok(count) } fn flush(&mut self) -> io::Result<()> { diff --git a/openssl/src/ssl/tests.rs b/openssl/src/ssl/tests.rs index 688a5db6..e6af551b 100644 --- a/openssl/src/ssl/tests.rs +++ b/openssl/src/ssl/tests.rs @@ -4,9 +4,7 @@ use std::net::TcpStream; use std::io; use std::io::prelude::*; use std::path::Path; -#[cfg(feature = "npn")] use std::net::TcpListener; -#[cfg(feature = "npn")] use std::thread; use std::fs::File; @@ -17,7 +15,6 @@ use ssl::SslMethod::Sslv23; use ssl::{SslContext, SslStream, VerifyCallback}; use ssl::SSL_VERIFY_PEER; use x509::X509StoreContext; -#[cfg(feature = "npn")] use x509::X509FileType; use x509::X509; use crypto::pkey::PKey; @@ -237,6 +234,34 @@ run_test!(verify_callback_data, |method, stream| { } }); +// Make sure every write call translates to a write call to the underlying socket. +#[test] +fn test_write_hits_stream() { + let listener = TcpListener::bind("localhost:0").unwrap(); + let addr = listener.local_addr().unwrap(); + + let guard = thread::spawn(move || { + let ctx = SslContext::new(Sslv23).unwrap(); + let stream = TcpStream::connect(addr).unwrap(); + let mut stream = SslStream::new(&ctx, stream).unwrap(); + + stream.write_all(b"hello").unwrap(); + stream + }); + + let mut ctx = SslContext::new(Sslv23).unwrap(); + ctx.set_verify(SSL_VERIFY_PEER, None); + ctx.set_certificate_file(&Path::new("test/cert.pem"), X509FileType::PEM).unwrap(); + ctx.set_private_key_file(&Path::new("test/key.pem"), X509FileType::PEM).unwrap(); + let stream = listener.accept().unwrap().0; + let mut stream = SslStream::new_server(&ctx, stream).unwrap(); + + let mut buf = [0; 5]; + assert_eq!(5, stream.read(&mut buf).unwrap()); + assert_eq!(&b"hello"[..], &buf[..]); + guard.join().unwrap(); +} + #[test] fn test_set_certificate_and_private_key() { let key_path = Path::new("test/key.pem"); diff --git a/openssl/src/x509/tests.rs b/openssl/src/x509/tests.rs index 1788b556..e9a8a4a5 100644 --- a/openssl/src/x509/tests.rs +++ b/openssl/src/x509/tests.rs @@ -2,7 +2,6 @@ use serialize::hex::FromHex; use std::io; use std::path::Path; use std::fs::File; -use std::str; use crypto::hash::Type::{SHA256}; use x509::{X509, X509Generator};