{"record":{"id":"3a26c1b804a25891","repo":"stamparm/maltrail","slug":"open-pcap","errorCode":null,"errorMessage":"open pcap","messagePattern":"open pcap","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sensor/src/testkit.rs","lineNumber":194,"sourceCode":"    }\n\n    /// Feed one raw packet (`ip_offset` is where the IP header starts).\n    pub fn feed(&mut self, packet: &[u8], sec: u64, usec: u32, ip_offset: usize) {\n        process::process_packet(&mut self.state, packet, sec, usec, ip_offset);\n    }\n\n    /// Feed a packet whose IP header starts at offset 0.\n    pub fn feed_ip(&mut self, packet: &[u8], sec: u64) {\n        self.feed(packet, sec, 0, 0);\n    }\n\n    /// Replay a pcap file through the real capture handle, DLT resolution and packet path\n    /// (everything `worker::run` does, minus the thread and the housekeeping timers).\n    ///\n    /// Returns the number of packets read. `wallclock` selects the Python-3-compatible\n    /// timestamp substitution.\n    pub fn replay(&mut self, pcap: &std::path::Path, wallclock: bool) -> usize {\n        let mut handle = crate::capture::Handle::open_offline(pcap).expect(\"open pcap\");\n        let datalink = handle.datalink();\n        let snaplen = self.state.cfg.capture_snaplen;\n        let mut count = 0usize;\n        let mut scratch: Vec<u8> = Vec::new();\n        loop {\n            match handle.next_packet() {\n                Ok(Some(captured)) => {\n                    count += 1;\n                    let data: &[u8] = if captured.data.len() > snaplen {\n                        scratch.clear();\n                        scratch.extend_from_slice(&captured.data[..snaplen]);\n                        &scratch\n                    } else {\n                        captured.data\n                    };\n                    let (sec, usec) = if wallclock {\n                        let now =\n                            std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default();","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/stamparm/maltrail/blob/77cfb06d7606506d101bbcec0786c77166c4255e/sensor/src/testkit.rs#L176-L212","documentation":"Panic from `.expect(\"open pcap\")` when `capture::Handle::open_offline` fails to open the replay pcap file. The testkit replay path needs a real capture handle over the file, so an unreadable/invalid pcap aborts the test immediately.","triggerScenarios":"`replay()` called with a path that does not exist, is not readable, or whose contents are not a valid pcap (bad magic, truncated header), causing `open_offline` to return Err.","commonSituations":"Wrong relative path for the fixture pcap; test fixture not committed or cleaned up by a previous run; a corrupted/hand-edited pcap file; running the test from a working directory other than the one the path assumes.","solutions":["Verify the pcap path exists and is readable before calling replay","Use an absolute path or the repo_root()-based path helper instead of a relative path","Regenerate the fixture pcap if it was truncated or corrupted","Check open_offline's Err by matching instead of expect to surface the underlying cause"],"exampleFix":"// before\nlet mut handle = crate::capture::Handle::open_offline(pcap).expect(\"open pcap\");\n// after\nlet mut handle = match crate::capture::Handle::open_offline(pcap) {\n    Ok(h) => h,\n    Err(e) => panic!(\"open pcap {}: {e}\", pcap.display()),\n};","handlingStrategy":"validation","validationCode":"let meta = std::fs::metadata(pcap).expect(\"pcap fixture missing\");\nassert!(meta.is_file() && meta.len() >= 24, \"not a pcap file: {}\", pcap.display());","typeGuard":null,"tryCatchPattern":"let mut handle = crate::capture::Handle::open_offline(pcap)\n    .unwrap_or_else(|e| panic!(\"open pcap {}: {e}\", pcap.display()));","preventionTips":["Anchor fixture paths with the repo-root helper, never bare relative paths","Commit and verify pcap fixtures in CI","Match on open_offline's error to report the underlying cause"],"tags":["rust","pcap","test-harness","file-io"],"backgroundTag":"file-not-found","analyzedSha":"77cfb06d7606506d101bbcec0786c77166c4255e","analyzedAt":"2026-09-13T03:50:16.010Z","contentChangedAt":"2026-09-13T03:50:16.010Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}