aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2015-04-30 00:14:06 -0700
committerSteven Fackler <[email protected]>2015-04-30 00:18:23 -0700
commit73617dabfa2c74ea9787f14d5e4b220c98a894ec (patch)
tree214998f17df441ef3e8405647daba2ef6d36bf51
parentMerge pull request #207 from Byron/master (diff)
downloadrust-openssl-73617dabfa2c74ea9787f14d5e4b220c98a894ec.tar.xz
rust-openssl-73617dabfa2c74ea9787f14d5e4b220c98a894ec.zip
Write through to underlying stream for every write call
cc #208
-rw-r--r--openssl/src/ssl/mod.rs14
-rw-r--r--openssl/src/ssl/tests.rs31
-rw-r--r--openssl/src/x509/tests.rs1
3 files changed, 35 insertions, 11 deletions
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<S: Read+Write> Read for SslStream<S> {
impl<S: Read+Write> Write for SslStream<S> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
- 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};