@ -476,7 +476,7 @@ where R: AsyncRead
{
{
fn poll_read ( self : Pin < & mut Self > , cx : & mut Context < ' _ > , buf : & mut [ u8 ] ) -> Poll < io ::Result < usize > > {
fn poll_read ( self : Pin < & mut Self > , cx : & mut Context < ' _ > , buf : & mut [ u8 ] ) -> Poll < io ::Result < usize > > {
//XXX: If the encryption state of the socket is changed between polls, this breaks. Idk if we can do anything about that tho.
//XXX: If the encryption state of the socket is changed between polls, this breaks. Idk if we can do anything about that tho.
if self . state . enc w {
if self . state . enc r {
self . project ( ) . rx . poll_read ( cx , buf )
self . project ( ) . rx . poll_read ( cx , buf )
} else {
} else {
// SAFETY: Uhh... well I think this is fine? Because we can project the container.
// SAFETY: Uhh... well I think this is fine? Because we can project the container.
@ -538,4 +538,82 @@ mod tests
Ok ( ( ) )
Ok ( ( ) )
}
}
#[ tokio::test ]
async fn esock_exchange ( ) -> crate ::eyre ::Result < ( ) >
{
use crate ::* ;
const VALUE : & ' static [ u8 ] = b" Hello world! " ;
let ( atx , brx ) = tokio ::io ::duplex ( super ::TRANS_KEY_MAX_SIZE * 4 ) ;
let ( btx , arx ) = tokio ::io ::duplex ( super ::TRANS_KEY_MAX_SIZE * 4 ) ;
let mut tx = super ::ESock ::new ( atx , arx ) ? ;
let mut rx = super ::ESock ::new ( btx , brx ) ? ;
let writer = tokio ::spawn ( async move {
use tokio ::prelude ::* ;
tx . exchange ( ) . await ? ;
assert! ( tx . has_exchanged ( ) ) ;
tx . set_encrypted_write ( true ) . await ? ;
assert_eq! ( ( true , false ) , tx . is_encrypted ( ) ) ;
tx . write_all ( VALUE ) . await ? ;
// Check resp
tx . set_encrypted_read ( true ) . await ? ;
assert_eq! ( {
let mut chk = [ 0 u8 ; 3 ] ;
tx . read_exact ( & mut chk [ .. ] ) . await ? ;
chk
} , [ 0xaa u8 , 0 , 0 ] , "Failed response check" ) ;
// Write unencrypted
tx . set_encrypted_write ( false ) . await ? ;
tx . write_all ( & [ 2 , 1 , 0xfa ] ) . await ? ;
Result ::< _ , eyre ::Report > ::Ok ( VALUE )
} ) ;
let reader = tokio ::spawn ( async move {
use tokio ::prelude ::* ;
rx . exchange ( ) . await ? ;
assert! ( rx . has_exchanged ( ) ) ;
rx . set_encrypted_read ( true ) . await ? ;
assert_eq! ( ( false , true ) , rx . is_encrypted ( ) ) ;
let mut val = vec! [ 0 u8 ; VALUE . len ( ) ] ;
rx . read_exact ( & mut val [ .. ] ) . await ? ;
// Send resp
rx . set_encrypted_write ( true ) . await ? ;
rx . write_all ( & [ 0xaa , 0 , 0 ] ) . await ? ;
// Read unencrypted
rx . set_encrypted_read ( false ) . await ? ;
assert_eq! ( {
let mut buf = [ 0 u8 ; 3 ] ;
rx . read_exact ( & mut buf [ .. ] ) . await ? ;
buf
} , [ 2 u8 , 1 , 0xfa ] , "2nd response incorrect" ) ;
Result ::< _ , eyre ::Report > ::Ok ( val )
} ) ;
let ( writer , reader ) = tokio ::join ! [ writer , reader ] ;
let writer = writer . expect ( "Tx task panic" ) ;
let reader = reader . expect ( "Rx task panic" ) ;
eprintln! ( "Txr: {:?}" , writer ) ;
eprintln! ( "Rxr: {:?}" , reader ) ;
writer ? ;
let val = reader ? ;
println! ( "Read: {:?}" , val ) ;
assert_eq! ( & val , VALUE ) ;
Ok ( ( ) )
}
}
}