Tunal/crates/tunal-elevate/tests/protocol.rs
astar76 35715817aa S032: tests for core, ops, trash, dirmeta and the elevation protocol
Fixes: file over folder with Overwrite, CRLF .directory files, stray blank line
after removing an added group.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-19 01:00:47 -06:00

203 lines
6.4 KiB
Rust

//! Wire format: frames, messages, errors and entries survive the pipe.
use std::io::Cursor;
use std::time::{Duration, UNIX_EPOCH};
use tunal_core::{Entry, Error, FileType, Kind};
use tunal_elevate::protocol::{
Frame, Reply, Request, VERSION, WireEntry, WireError, read_frame, recv, recv_data, send,
send_data,
};
fn entry(name: &[u8], kind: Kind) -> Entry {
Entry {
display_name: tunal_core::display_name(name),
hidden: name.first() == Some(&b'.'),
mime: tunal_core::mime::mime_type(name, &kind),
kind,
name: name.to_vec(),
size: 1234,
mtime: Some(UNIX_EPOCH + Duration::new(1_700_000_000, 123_456_789)),
mode: 0o640,
uid: 1000,
gid: 100,
}
}
fn round_trip(e: Entry) -> Entry {
let mut buf = Vec::new();
send(&mut buf, &Reply::Entry(e.into())).unwrap();
match recv::<Reply>(&mut Cursor::new(buf)).unwrap().unwrap() {
Reply::Entry(w) => w.into(),
other => panic!("{other:?}"),
}
}
#[test]
fn requests_round_trip_with_raw_paths() {
let mut buf = Vec::new();
let request = Request::Rename {
from: b"/a/\xff odd".to_vec(),
to: b"/b".to_vec(),
};
send(&mut buf, &request).unwrap();
send(&mut buf, &Request::Hello { version: VERSION }).unwrap();
let mut input = Cursor::new(buf);
match recv::<Request>(&mut input).unwrap().unwrap() {
Request::Rename { from, to } => {
assert_eq!((from, to), (b"/a/\xff odd".to_vec(), b"/b".to_vec()))
}
other => panic!("{other:?}"),
}
assert!(matches!(
recv::<Request>(&mut input).unwrap().unwrap(),
Request::Hello { version } if version == VERSION
));
// Clean end of stream.
assert!(recv::<Request>(&mut input).unwrap().is_none());
}
#[test]
fn data_frames_keep_every_byte() {
let bytes: Vec<u8> = (0..=255).collect();
let mut buf = Vec::new();
send_data(&mut buf, &bytes).unwrap();
send_data(&mut buf, &[]).unwrap();
let mut input = Cursor::new(buf);
assert_eq!(recv_data(&mut input).unwrap(), bytes);
assert!(recv_data(&mut input).unwrap().is_empty());
}
#[test]
fn frame_layout_is_length_kind_payload() {
let mut buf = Vec::new();
send_data(&mut buf, b"abc").unwrap();
assert_eq!(buf, [0, 0, 0, 4, 1, b'a', b'b', b'c']);
match read_frame(&mut Cursor::new(buf)).unwrap().unwrap() {
Frame::Data(d) => assert_eq!(d, b"abc"),
Frame::Message(_) => panic!("kind"),
}
}
#[test]
fn bad_frames_are_errors_not_panics() {
let bad = |bytes: Vec<u8>| read_frame(&mut Cursor::new(bytes)).err().map(|e| e.kind());
use std::io::ErrorKind::{InvalidData, UnexpectedEof};
// Zero length, oversized length, unknown kind.
assert_eq!(bad(vec![0, 0, 0, 0]), Some(InvalidData));
assert_eq!(bad(vec![0xff, 0xff, 0xff, 0xff, 0]), Some(InvalidData));
assert_eq!(bad(vec![0, 0, 0, 1, 9]), Some(InvalidData));
// Cut off in the middle of a frame.
assert_eq!(bad(vec![0, 0, 0, 10, 0, b'x']), Some(UnexpectedEof));
// A short header is a clean end.
assert!(read_frame(&mut Cursor::new(vec![0, 0])).unwrap().is_none());
assert!(
read_frame(&mut Cursor::new(Vec::<u8>::new()))
.unwrap()
.is_none()
);
}
#[test]
fn wrong_frame_kind_for_the_caller_is_rejected() {
let mut buf = Vec::new();
send_data(&mut buf, b"x").unwrap();
assert!(recv::<Reply>(&mut Cursor::new(buf)).is_err());
let mut buf = Vec::new();
send(&mut buf, &Reply::Done).unwrap();
assert!(recv_data(&mut Cursor::new(buf)).is_err());
}
#[test]
fn garbage_json_is_an_error() {
let mut buf = Vec::new();
let payload = b"{not json";
buf.extend_from_slice(&(payload.len() as u32 + 1).to_be_bytes());
buf.push(0);
buf.extend_from_slice(payload);
assert!(recv::<Request>(&mut Cursor::new(buf)).is_err());
// Valid JSON, unknown message.
let mut buf = Vec::new();
send(&mut buf, &"Teleport").unwrap();
assert!(recv::<Request>(&mut Cursor::new(buf)).is_err());
}
#[test]
fn every_error_survives_the_wire() {
for e in [
Error::NotFound,
Error::PermissionDenied,
Error::Exists,
Error::NotEmpty,
Error::Unsupported,
Error::Cancelled,
Error::Io("disk \"full\"\n".into()),
Error::Remote("héllo".into()),
] {
let mut buf = Vec::new();
send(&mut buf, &Reply::Error(WireError::from(e.clone()))).unwrap();
match recv::<Reply>(&mut Cursor::new(buf)).unwrap().unwrap() {
Reply::Error(w) => assert_eq!(Error::from(w), e),
other => panic!("{other:?}"),
}
}
}
#[test]
fn entries_survive_the_wire() {
for kind in [
Kind::File,
Kind::Dir,
Kind::Special,
Kind::Symlink {
target: b"../t\xff".to_vec(),
target_type: Some(FileType::Dir),
},
Kind::Symlink {
target: b"x".to_vec(),
target_type: Some(FileType::Special),
},
Kind::Symlink {
target: b"gone".to_vec(),
target_type: None,
},
] {
let original = entry(b".odd \xff name.txt", kind);
let back = round_trip(original.clone());
assert_eq!(back, original);
}
}
#[test]
fn special_mime_types_are_kept_and_others_recomputed() {
let mut fifo = entry(b"pipe", Kind::Special);
fifo.mime = "inode/fifo";
assert_eq!(round_trip(fifo).mime, "inode/fifo");
let file = entry(b"song.mp3", Kind::File);
assert_eq!(round_trip(file.clone()).mime, file.mime);
// A made-up MIME on the wire is not trusted.
let mut buf = Vec::new();
send(&mut buf, &Reply::Entry(entry(b"a.txt", Kind::File).into())).unwrap();
let text = String::from_utf8_lossy(&buf[5..]).replace("\"mime\":null", "\"mime\":\"x/evil\"");
let mut forged = Vec::new();
forged.extend_from_slice(&(text.len() as u32 + 1).to_be_bytes());
forged.push(0);
forged.extend_from_slice(text.as_bytes());
match recv::<Reply>(&mut Cursor::new(forged)).unwrap().unwrap() {
Reply::Entry(w) => assert_ne!(Entry::from(w).mime, "x/evil"),
other => panic!("{other:?}"),
}
}
#[test]
fn missing_mtime_stays_missing() {
let mut e = entry(b"a", Kind::File);
e.mtime = None;
assert_eq!(round_trip(e).mtime, None);
}
#[test]
fn chunk_and_version_constants_are_sane() {
assert_eq!(VERSION, 1);
let _ = WireEntry::from(entry(b"x", Kind::File));
}