1#![no_std]
2
3extern crate alloc;
4
5mod addr;
6mod error;
7mod event;
8mod interface;
9
10pub use addr::*;
11pub use error::*;
12pub use event::*;
13pub use interface::*;
14pub use rdif_base::{DriverGeneric, KError, io};
15
16#[cfg(test)]
17mod tests {
18 use super::*;
19
20 struct TestVsock;
21
22 impl DriverGeneric for TestVsock {
23 fn name(&self) -> &str {
24 "test-vsock"
25 }
26 }
27
28 impl Interface for TestVsock {
29 fn guest_cid(&self) -> u64 {
30 3
31 }
32
33 fn listen(&mut self, _port: u32) -> Result<(), VsockError> {
34 Ok(())
35 }
36
37 fn connect(&mut self, _id: VsockConnId) -> Result<(), VsockError> {
38 Ok(())
39 }
40
41 fn send(&mut self, _id: VsockConnId, buf: &[u8]) -> Result<usize, VsockError> {
42 Ok(buf.len())
43 }
44
45 fn recv(&mut self, _id: VsockConnId, buf: &mut [u8]) -> Result<usize, VsockError> {
46 if !buf.is_empty() {
47 buf[0] = 7;
48 }
49 Ok(buf.len().min(1))
50 }
51
52 fn recv_avail(&mut self, _id: VsockConnId) -> Result<usize, VsockError> {
53 Ok(1)
54 }
55
56 fn disconnect(&mut self, _id: VsockConnId) -> Result<(), VsockError> {
57 Ok(())
58 }
59
60 fn abort(&mut self, _id: VsockConnId) -> Result<(), VsockError> {
61 Ok(())
62 }
63
64 fn poll_event(&mut self) -> Result<Option<VsockEvent>, VsockError> {
65 Ok(Some(VsockEvent::Connected(VsockConnId::listening(1024))))
66 }
67 }
68
69 #[test]
70 fn vsock_interface_exposes_connections_and_events() {
71 let mut vsock = TestVsock;
72 let id = VsockConnId::listening(1024);
73 assert_eq!(vsock.guest_cid(), 3);
74 assert_eq!(vsock.send(id, &[1, 2, 3]).unwrap(), 3);
75
76 let mut buf = [0; 4];
77 assert_eq!(vsock.recv(id, &mut buf).unwrap(), 1);
78 assert_eq!(buf[0], 7);
79 assert_eq!(vsock.poll_event().unwrap(), Some(VsockEvent::Connected(id)));
80 assert_eq!(vsock.handle_irq(), Event::none());
81 }
82
83 #[test]
84 fn already_exists_maps_to_not_available() {
85 let kind: io::ErrorKind = VsockError::AlreadyExists.into();
86
87 assert!(matches!(kind, io::ErrorKind::NotAvailable));
88 }
89}