From 4021b1692a8fda178e0c10ca3787aceef7523ce6 Mon Sep 17 00:00:00 2001 From: Adrian Cruceru Date: Fri, 11 Feb 2022 15:21:12 +0100 Subject: [PATCH] Remove bounds on establish, test that Send gets derived as needed --- mbedtls/src/ssl/context.rs | 64 +++++++++++++++++++++++++++++++++++--- mbedtls/tests/hyper.rs | 10 ++---- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/mbedtls/src/ssl/context.rs b/mbedtls/src/ssl/context.rs index 04a48a973..56d8178ad 100644 --- a/mbedtls/src/ssl/context.rs +++ b/mbedtls/src/ssl/context.rs @@ -37,7 +37,7 @@ pub trait IoCallback { fn data_ptr(&mut self) -> *mut c_void; } -impl IoCallback for IO { +impl IoCallback for IO { unsafe extern "C" fn call_recv(user_data: *mut c_void, data: *mut c_uchar, len: size_t) -> c_int { let len = if len > (c_int::max_value() as size_t) { c_int::max_value() as size_t @@ -140,7 +140,7 @@ impl Context { } } -impl Context { +impl Context { pub fn establish(&mut self, io: T, hostname: Option<&str>) -> Result<()> { unsafe { let mut io = Box::new(io); @@ -412,13 +412,69 @@ impl HandshakeContext { #[cfg(test)] mod tests { - use crate::ssl::context::HandshakeContext; - use crate::tests::TestTrait; + #[cfg(feature = "std")] + use std::io::{Read,Write, Result as IoResult}; + + #[cfg(not(feature = "std"))] + use core_io::{Read, Write, Result as IoResult}; + use crate::ssl::context::{HandshakeContext, Context}; + use crate::tests::TestTrait; + #[test] fn handshakecontext_sync() { assert!(!TestTrait::::new().impls_trait(), "HandshakeContext must be !Sync"); } + + struct NonSendStream { + _buffer: core::ptr::NonNull, + } + + impl Read for NonSendStream { + fn read(&mut self, _: &mut [u8]) -> IoResult { + unimplemented!() + } + } + + impl Write for NonSendStream { + fn write(&mut self, _: &[u8]) -> IoResult { + unimplemented!() + } + + fn flush(&mut self) -> IoResult<()> { + unimplemented!() + } + } + + struct SendStream { + _buffer: u8, + } + + impl Read for SendStream { + fn read(&mut self, _: &mut [u8]) -> IoResult { + unimplemented!() + } + } + + impl Write for SendStream { + fn write(&mut self, _: &[u8]) -> IoResult { + unimplemented!() + } + + fn flush(&mut self) -> IoResult<()> { + unimplemented!() + } + } + + #[test] + fn context_send() { + assert!(!TestTrait::::new().impls_trait(), "NonSendStream can't be send"); + assert!(!TestTrait::>::new().impls_trait(), "Context can't be send"); + + assert!(TestTrait::::new().impls_trait(), "SendStream is send"); + assert!(TestTrait::>::new().impls_trait(), "Context is send"); + } + } // ssl_get_alpn_protocol diff --git a/mbedtls/tests/hyper.rs b/mbedtls/tests/hyper.rs index 475599066..26eb9f604 100644 --- a/mbedtls/tests/hyper.rs +++ b/mbedtls/tests/hyper.rs @@ -25,18 +25,14 @@ impl TlsStream { } } -unsafe impl Send for TlsStream {} -unsafe impl Sync for TlsStream {} - - -impl io::Read for TlsStream +impl io::Read for TlsStream { fn read(&mut self, buf: &mut [u8]) -> io::Result { self.context.lock().unwrap().read(buf) } } -impl io::Write for TlsStream +impl io::Write for TlsStream { fn write(&mut self, buf: &[u8]) -> io::Result { self.context.lock().unwrap().write(buf) @@ -83,8 +79,6 @@ impl MbedSSLServer { } } -unsafe impl Send for MbedSSLServer {} - /// An abstraction to allow any SSL implementation to be used with server-side HttpsStreams. impl SslServer for MbedSSLServer where T: NetworkStream + Send + Clone + fmt::Debug + Sync