1 //! The fault binary against the real client.2 //!3 //! Each test starts the binary on a private filesystem socket, connects a4 //! `Peer`, and asserts what a core would see. A fault that the client survives is a5 //! requirement; a fault that it turns into a hang is a bug.6 7 use std::fs;8 use std::os::unix::fs::DirBuilderExt;9 use std::os::unix::net::UnixListener;10 use std::path::{Path, PathBuf};11 use std::process::{Child, Command, Stdio};12 use std::sync::atomic::{AtomicU32, Ordering};13 use std::time::Duration;14 15 use inf3r_proto::body::{self, Grant, PartKind};16 use inf3r_proto::client::{Item, Peer};17 18 const WAIT: Duration = Duration::from_secs(5);19 const BIN: &str = env!("CARGO_BIN_EXE_inf3r-mock-fault");20 21 /// A private socket directory that is removed when the test ends.22 struct SocketDir(PathBuf);23 24 impl Drop for SocketDir {25 fn drop(&mut self) {26 let _ = fs::remove_dir_all(&self.0);27 }28 }29 30 /// Runs the fault binary on a fresh filesystem socket and returns the child,31 /// the caller end, and the directory guard.32 fn start(fault: &str) -> (Child, Peer, SocketDir) {33 static NEXT: AtomicU32 = AtomicU32::new(1);34 let dir = Path::new("/tmp").join(format!(35 "inf3r-fault-{}-{}",36 std::process::id(),37 NEXT.fetch_add(1, Ordering::Relaxed)38 ));39 fs::DirBuilder::new()40 .mode(0o700)41 .create(&dir)42 .expect("the private directory");43 let dir = SocketDir(dir);44 let path = dir.0.join("socket");45 // The core listens and the plugin connects; the test is the core here.46 let listener = UnixListener::bind(&path).expect("bind");47 listener.set_nonblocking(true).expect("nonblocking");48 49 let child = Command::new(BIN)50 .arg("--socket")51 .arg(&path)52 .arg("--fault")53 .arg(fault)54 .stdout(Stdio::null())55 .stderr(Stdio::null())56 .spawn()57 .expect("spawn");58 59 // The child connects as soon as it starts. Retry until it does, so the test60 // does not race the process table.61 let mut caller = None;62 for _ in 0..400 {63 match listener.accept() {64 Ok((socket, _)) => {65 caller = Some(socket);66 break;67 }68 Err(_) => std::thread::sleep(Duration::from_millis(5)),69 }70 }71 let caller = caller.expect("the child connects");72 caller.set_nonblocking(false).expect("blocking");73 let peer = Peer::connect(caller, "fault-test", WAIT).expect("handshake");74 (child, peer, dir)75 }76 77 fn open(peer: &Peer, credit: u64) -> inf3r_proto::client::Job {78 peer.load(&Grant { deps: Vec::new() }, WAIT).expect("load");79 peer.open(80 body::Open {81 request_id: "r".into(),82 model: "faulty".into(),83 capability: "chat".into(),84 credit,85 budget_ms: 1000,86 parts: vec![body::Part {87 kind: PartKind::Text,88 mime: "text/plain".into(),89 role: None,90 len: 2,91 hold: body::Hold::Inline(b"hi".to_vec()),92 }],93 params: inf3r_proto::Value::Map(Vec::new()),94 },95 &[],96 )97 .expect("open")98 }99 100 #[test]101 fn a_reject_arrives_as_a_refusal_with_its_code() {102 let (_child, peer, _dir) = start("reject");103 let mut job = open(&peer, 1 << 20);104 match job.next().expect("read").expect("an item") {105 Item::Reject(refusal) => assert_eq!(refusal.code, body::ErrorCode::Busy),106 other => panic!("expected a reject, got {other:?}"),107 }108 }109 110 #[test]111 fn a_crash_after_accept_ends_the_stream() {112 let (_child, peer, _dir) = start("crash-accept");113 let mut job = open(&peer, 1 << 20);114 // The accept is followed by nothing: the reader reports the closed socket.115 let mut ended = false;116 for _ in 0..10 {117 match job.next() {118 Ok(Some(Item::Accepted(_))) => {}119 Ok(Some(other)) => panic!("unexpected {other:?}"),120 Ok(None) | Err(_) => {121 ended = true;122 break;123 }124 }125 }126 assert!(ended, "the stream must end when the plugin exits");127 }128 129 #[test]130 fn a_bad_frame_is_an_error_not_a_panic() {131 let (_child, peer, _dir) = start("bad-frame");132 // The grant triggers the bad frame: the process exits after it.133 let result = peer.load(&Grant { deps: Vec::new() }, WAIT);134 assert!(result.is_err(), "a bad frame must fail the load");135 }136 137 #[test]138 fn an_unknown_frame_type_is_ignored_and_the_job_finishes() {139 let (_child, peer, _dir) = start("unknown");140 let mut job = open(&peer, 1 << 20);141 let mut done = false;142 while let Ok(Some(item)) = job.next() {143 if let Item::Done(done_body, _) = item {144 assert_eq!(done_body.outcome, body::Outcome::Completed);145 done = true;146 break;147 }148 }149 assert!(done, "an unknown frame must not end the job");150 }151 152 #[test]153 fn a_window_violation_is_refused_by_the_client() {154 let (_child, peer, _dir) = start("credit");155 let mut job = open(&peer, 4096);156 let mut refused = false;157 for _ in 0..4 {158 match job.next() {159 Ok(Some(Item::Accepted(_))) => {}160 Ok(Some(Item::Event { .. })) => {}161 Ok(Some(other)) => panic!("unexpected {other:?}"),162 Err(_) => {163 refused = true;164 break;165 }166 Ok(None) => break,167 }168 }169 assert!(refused, "an event beyond the window must be an error");170 }