Skip to content

Commit

Permalink
chore: fix building of tests
Browse files Browse the repository at this point in the history
Long overdue.

Also, I am back. And this project is not dead, not yet!

Signed-off-by: Yuxuan Shui <[email protected]>
  • Loading branch information
yshui committed Feb 29, 2024
1 parent c9ba180 commit b69b45b
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 55 deletions.
25 changes: 13 additions & 12 deletions runa-io/src/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,21 @@ mod test {

use anyhow::Result;
use arbitrary::Arbitrary;
use smol::Task;
use smol::{future::poll_fn, Task};
use tracing::debug;

use crate::{traits::buf::AsyncBufReadWithFd, BufReaderWithFd};
use crate::{
traits::{buf::AsyncBufReadWithFd, AsyncWriteWithFd},
BufReaderWithFd,
};
async fn buf_roundtrip_seeded(raw: &[u8], executor: &smol::LocalExecutor<'_>) {
let mut source = arbitrary::Unstructured::new(raw);
let (rx, tx) = std::os::unix::net::UnixStream::pair().unwrap();
let (_, tx) = crate::split_unixstream(tx).unwrap();
let (_, mut tx) = crate::split_unixstream(tx).unwrap();
let (rx, _) = crate::split_unixstream(rx).unwrap();
let mut rx = BufReaderWithFd::new(rx);
let task: Task<Result<_>> = executor.spawn(async move {
debug!("start");
use futures_lite::AsyncBufRead;

let mut bytes = Vec::new();
let mut fds = Vec::new();
Expand Down Expand Up @@ -251,25 +253,24 @@ mod test {
break
}
let has_fd = bool::arbitrary(&mut source).unwrap();
let fds = if has_fd {
let mut fds = if has_fd {
let fd: std::os::unix::io::OwnedFd =
std::fs::File::open("/dev/null").unwrap().into();
sent_fds.push(fd.as_raw_fd());
Some(fd)
vec![fd]
} else {
None
Vec::new()
};
let len = (packet.len() as u32 + 4).to_ne_bytes();
tx.reserve(packet.len() + 4, if fds.is_some() { 1 } else { 0 })
poll_fn(|cx| Pin::new(&mut tx).poll_write_with_fds(cx, &len[..], &mut vec![]))
.await
.unwrap();
poll_fn(|cx| Pin::new(&mut tx).poll_write_with_fds(cx, packet, &mut fds))
.await
.unwrap();
Pin::new(&mut tx).write(&len);
Pin::new(&mut tx).write(packet);
debug!("send len: {:?}", packet.len() + 4);
sent_bytes.extend_from_slice(packet);
Pin::new(&mut tx).push_fds(&mut fds.into_iter());
}
tx.flush().await.unwrap();
drop(tx);
let (bytes, fds) = task.await.unwrap();
assert_eq!(bytes, sent_bytes);
Expand Down
6 changes: 6 additions & 0 deletions runa-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ impl<C> Connection<C> {
buf: BytesMut::with_capacity(capacity),
}
}

/// Consumes the connection and returns the inner connection. Any unflushed
/// data and file descriptors are lost.
pub fn into_inner(self) -> C {
self.conn
}
}

impl<C: traits::AsyncWriteWithFd> traits::WriteMessage for Connection<C> {
Expand Down
7 changes: 7 additions & 0 deletions runa-io/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std::{
use super::traits::{buf::AsyncBufReadWithFd, AsyncReadWithFd, AsyncWriteWithFd, OwnedFds};
use crate::traits;

/// An in-memory buffer that implements `AsyncWrite` and `AsyncWriteWithFd`. Intended for testing
/// purposes.
#[derive(Default, Debug)]
pub struct WritePool {
inner: Vec<u8>,
Expand Down Expand Up @@ -57,18 +59,22 @@ impl AsyncWriteWithFd for WritePool {
}

impl WritePool {
/// Create a new `WritePool`.
pub fn new() -> Self {
Self {
inner: Vec::new(),
fds: Vec::new(),
}
}

/// Consume the `WritePool` and return the inner buffer and file descriptors.
pub fn into_inner(self) -> (Vec<u8>, Vec<OwnedFd>) {
(self.inner, self.fds)
}
}

/// An in-memory buffer that implements `AsyncRead` and `AsyncReadWithFd`. Intended for testing
/// purposes.
#[derive(Debug)]
pub struct ReadPool {
inner: Vec<u8>,
Expand Down Expand Up @@ -158,6 +164,7 @@ impl AsyncReadWithFd for ReadPool {
}

impl ReadPool {
/// Create a new `ReadPool` with the given data and file descriptors.
pub fn new(data: Vec<u8>, fds: Vec<RawFd>) -> Self {
Self { inner: data, fds }
}
Expand Down
2 changes: 2 additions & 0 deletions runa-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ syn = { workspace = true, features = ["visit-mut"] }
futures-executor = "0.3"
futures-lite.workspace = true
runa-wayland-scanner = { version = "0.1.0", path = "../wayland-tools/scanner" }
runa-wayland-types = { version = "0.1.0", path = "../runa-wayland-types" }
num_enum = "0.5.7"
futures-task = "0.3.24"
runa-io = { version = "0.0.1-alpha1", path = "../runa-io" }
bytes.workspace = true

[features]
default = [ "tracing" ]
Expand Down
76 changes: 40 additions & 36 deletions runa-macros/tests/generate.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,63 @@
use std::{
ffi::CStr,
os::{fd::IntoRawFd, unix::prelude::AsRawFd},
os::{
fd::{IntoRawFd, OwnedFd},
unix::prelude::AsRawFd,
},
pin::Pin,
};

use bytes::{buf, BufMut};
use futures_lite::io::AsyncWriteExt;
use wayland::wl_display::v1::{events, Event};
use wl_io::utils::{ReadPool, WritePool};
use wl_macros::generate_protocol;
use wl_scanner_support::{
io::{buf::AsyncBufReadWithFd, de::Deserialize, ser::Serialize},
wayland_types::{Fd, NewId, Object, Str},
use runa_io::{
traits::{
buf::{AsyncBufReadWithFd, Message},
de::Deserialize,
ser::Serialize,
WriteMessage,
},
utils::{ReadPool, WritePool},
Connection,
};
generate_protocol!("protocols/wayland.xml");
use runa_wayland_types::{Fd, NewId, Object, Str};
use wayland::wl_display::v1::{events, Event};
runa_wayland_scanner::generate_protocol!("runa-wayland-protocols/spec/wayland.xml");

#[test]
fn test_roundtrip() {
futures_executor::block_on(async {
let orig_item = Event::Error(events::Error {
object_id: Object(1),
code: 2,
message: Str(CStr::from_bytes_with_nul(b"test\0").unwrap()),
message: Str(b"test"),
});
let mut tx = WritePool::new();
let mut tx = Connection::new(WritePool::new(), 4096);
// Put an object id
tx.write(&[0, 0, 0, 0]).await.unwrap();
orig_item.serialize(Pin::new(&mut tx));
let (buf, fds) = tx.into_inner();
tx.send(1, orig_item).await.unwrap();
tx.flush().await.unwrap();
let pool = tx.into_inner();
let (buf, fds) = pool.into_inner();
let fds = fds
.into_iter()
.map(|fd| fd.into_raw_fd())
.collect::<Vec<_>>();

let mut rx = ReadPool::new(buf, fds);
let (object_id, _len, bytes, fds) = Pin::new(&mut rx).next_message().await.unwrap();
assert_eq!(object_id, 0);
let mut de = wl_io::de::Deserializer::new(bytes, fds);
let item = Event::deserialize(de.borrow_mut()).unwrap();
let (bytes_read, fds_read) = de.consumed();
// Drop the deserialize first to catch potential UB, i.e. the item doesnt borrow
// from the deserializer
drop(de);
let Message {
object_id, data, ..
} = Pin::new(&mut rx).next_message().await.unwrap();
assert_eq!(object_id, 1);
let item = Event::deserialize(data, &[]).unwrap();

eprintln!("{:#?}", item);
assert_eq!(
&item,
&Event::Error(events::Error {
object_id: Object(1),
code: 2,
message: Str(CStr::from_bytes_with_nul(b"test\0").unwrap()),
message: Str(b"test"),
})
);
drop(item);
Pin::new(&mut rx).consume(bytes_read, fds_read);
assert!(rx.is_eof());
})
}
Expand All @@ -70,26 +75,26 @@ fn test_roundtrip_with_fd() {
size: 100,
});

let mut tx = WritePool::new();
let mut tx = Connection::new(WritePool::new(), 4096);
// Put an object id
Pin::new(&mut tx).write(&[0, 0, 0, 0]).await.unwrap();
orig_item.serialize(Pin::new(&mut tx));
let (buf, fds) = tx.into_inner();
tx.send(1, orig_item).await.unwrap();
let (buf, fds) = tx.into_inner().into_inner();
eprintln!("{:x?}", buf);

let fds = fds
.into_iter()
.map(|fd| fd.into_raw_fd())
.collect::<Vec<_>>();
let mut rx = ReadPool::new(buf, fds);
let (object_id, _, bytes, fds) = Pin::new(&mut rx).next_message().await.unwrap();
assert_eq!(object_id, 0);
let mut de = wl_io::de::Deserializer::new(bytes, fds);
let item = Request::deserialize(de.borrow_mut()).unwrap();
let (bytes_read, fds_read) = de.consumed();
drop(de);
let Message {
object_id,
data,
fds,
..
} = Pin::new(&mut rx).next_message().await.unwrap();
assert_eq!(object_id, 1);
let item = Request::deserialize(data, fds).unwrap();
eprintln!("{:#?}", item);
Pin::new(&mut rx).consume(bytes_read, fds_read);

// Compare the result skipping the file descriptor
match item {
Expand All @@ -100,7 +105,6 @@ fn test_roundtrip_with_fd() {
},
}

// Making sure dropping the accessor advance the buffer.
drop(item);
assert!(rx.is_eof());
})
Expand Down
4 changes: 2 additions & 2 deletions runa-wayland-protocols/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use runa_wayland_spec_parser as spec;
fn main() {
use std::io::Write;

let protocols = std::path::Path::new("spec");
let protocols = std::path::Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("spec");
let out_dir = std::env::var("OUT_DIR").unwrap();
println!("cargo:rerun-if-changed={}", protocols.display());
let generate_from_dir = |name: &str| {
let dest_path = std::path::Path::new(&out_dir).join(&format!("{name}_generated.rs"));
let dest_path = std::path::Path::new(&out_dir).join(format!("{name}_generated.rs"));
let mut outfile = std::fs::File::create(dest_path).unwrap();
let dir_path = protocols.join("wayland-protocols").join(name);
for dir in std::fs::read_dir(dir_path).unwrap() {
Expand Down
1 change: 0 additions & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[toolchain]
# Blocked by https://github.com/rust-lang/rust/issues/100267
channel = "nightly"
components = [ "rustfmt", "rust-src", "miri" ]
targets = [ "x86_64-unknown-linux-gnu" ]
Expand Down
4 changes: 2 additions & 2 deletions wayland-tools/codegen/tests/generate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wl_scanner::generate_protocol;
use runa_wayland_scanner_codegen::generate_protocol;
#[test]
fn generate() {
let f = std::fs::File::open(
Expand All @@ -7,7 +7,7 @@ fn generate() {
.join("wayland.xml"),
)
.unwrap();
let proto = wl_spec::parse::parse(f).unwrap();
let proto = spec_parser::parse::parse(f).unwrap();

generate_protocol(&proto).unwrap();
}
1 change: 1 addition & 0 deletions wayland-tools/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use proc_macro_error::ResultExt;
#[proc_macro]
#[proc_macro_error::proc_macro_error]
pub fn generate_protocol(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
use std::io::Read;
let tokens2 = tokens.clone();
Expand Down
4 changes: 2 additions & 2 deletions wayland-tools/parser/tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
fn parse() {
let file = std::fs::File::open(concat!(
env!("CARGO_WORKSPACE_DIR"),
"/protocols/wayland.xml"
"/runa-wayland-protocols/wayland.xml"
))
.unwrap();
eprintln!("{:?}", wl_spec::parse::parse(file).unwrap());
eprintln!("{:?}", runa_wayland_spec_parser::parse::parse(file).unwrap());
}

0 comments on commit b69b45b

Please sign in to comment.