ESock tested: Exchange OK, Session OK, Encrypted + Unencrypted reads + writes OK. All works (so far)! :^)

TODO: Inspect contents of the duplex stream throughout transit. Make sure the encryption is happening properly.

Fortune for rsh's current commit: Half curse − 半凶
master
Avril 3 years ago
parent dc307c6d06
commit 2a49d17528
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -91,7 +91,9 @@ const _:() = {
let _ref: &std::io::Stdin = a.downcast_ref::<std::io::Stdin>().unwrap(); let _ref: &std::io::Stdin = a.downcast_ref::<std::io::Stdin>().unwrap();
} }
} }
/*
XXX: This is broken on newest nightly?
const _TEST: () = _a::<dyn Test>(); const _TEST: () = _a::<dyn Test>();
const _TEST2: () = _b::<dyn TestAny>(); const _TEST2: () = _b::<dyn TestAny>();
*/
}; };

@ -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.encw { if self.state.encr {
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 = [0u8; 3];
tx.read_exact(&mut chk[..]).await?;
chk
}, [0xaau8,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![0u8; 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 = [0u8; 3];
rx.read_exact(&mut buf[..]).await?;
buf
}, [2u8,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(())
}
} }

Loading…
Cancel
Save