use std::collections::VecDeque;
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
use std::time::Duration;
use futures_io::{AsyncRead, AsyncWrite};
use futures_lite::future;
use futures_lite::io::AsyncReadExt;
use ostrya_core::RefSelector;
use ostrya_fetch::{FetcherOptions, Proxy};
use ostrya_remote::proto::{
ErrorMessage, FrameReader, FrameWriter, GetReply, ListRefsRequest, MAX_REF_LISTING, Message,
PULL_PROTOCOL_VERSION, PullHelloReply,
};
use ostrya_remote::transport::Endpoint;
use ostrya_remote::{
Error, ErrorCode, PullBody, PullSession, Result, StreamSession, StreamSessionOptions,
};
const PIPE_CAP: usize = 16 * 1024;
fn selector<S: AsRef<str>>(entries: &[S]) -> RefSelector {
RefSelector::parse(entries.iter().map(AsRef::as_ref)).unwrap()
}
const LIMIT: Duration = Duration::from_secs(30);
struct PipeState {
buf: VecDeque<u8>,
writer_closed: bool,
reader_closed: bool,
read_waker: Option<Waker>,
write_waker: Option<Waker>,
}
struct PipeWriter(Arc<Mutex<PipeState>>);
struct PipeReader(Arc<Mutex<PipeState>>);
fn pipe() -> (PipeWriter, PipeReader) {
let state = Arc::new(Mutex::new(PipeState {
buf: VecDeque::new(),
writer_closed: false,
reader_closed: false,
read_waker: None,
write_waker: None,
}));
(PipeWriter(state.clone()), PipeReader(state))
}
impl AsyncWrite for PipeWriter {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
let mut st = self.0.lock().unwrap();
if st.reader_closed {
return Poll::Ready(Err(io::ErrorKind::BrokenPipe.into()));
}
let room = PIPE_CAP - st.buf.len();
if room == 0 {
st.write_waker = Some(cx.waker().clone());
return Poll::Pending;
}
let n = room.min(buf.len());
st.buf.extend(&buf[..n]);
if let Some(w) = st.read_waker.take() {
w.wake();
}
Poll::Ready(Ok(n))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}
impl Drop for PipeWriter {
fn drop(&mut self) {
let mut st = self.0.lock().unwrap();
st.writer_closed = true;
if let Some(w) = st.read_waker.take() {
w.wake();
}
}
}
impl AsyncRead for PipeReader {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let mut st = self.0.lock().unwrap();
if st.buf.is_empty() {
if st.writer_closed {
return Poll::Ready(Ok(0));
}
st.read_waker = Some(cx.waker().clone());
return Poll::Pending;
}
let n = buf.len().min(st.buf.len());
for (dst, src) in buf[..n].iter_mut().zip(st.buf.drain(..n)) {
*dst = src;
}
if let Some(w) = st.write_waker.take() {
w.wake();
}
Poll::Ready(Ok(n))
}
}
impl Drop for PipeReader {
fn drop(&mut self) {
let mut st = self.0.lock().unwrap();
st.reader_closed = true;
if let Some(w) = st.write_waker.take() {
w.wake();
}
}
}
struct Server {
reader: FrameReader<PipeReader>,
writer: FrameWriter<PipeWriter>,
}
impl Server {
async fn recv(&mut self) -> Option<Message> {
self.reader.read_message().await.unwrap()
}
async fn send(&mut self, msg: &Message) {
self.writer.write_message(msg).await.unwrap();
self.writer.flush().await.unwrap();
}
async fn hello(&mut self, version: u32) {
match self.recv().await {
Some(Message::PullHello(hello)) => assert_eq!(hello.version, PULL_PROTOCOL_VERSION),
other => panic!("{other:?}"),
}
self.send(&Message::PullHelloReply(PullHelloReply { version }))
.await;
}
async fn path(&mut self) -> String {
match self.recv().await {
Some(Message::Get(path)) => path,
other => panic!("{other:?}"),
}
}
async fn body(&mut self, len: Option<u64>, body: &[u8], chunk: usize) {
self.send(&Message::GetReply(GetReply { found: true, len }))
.await;
for piece in body.chunks(chunk) {
self.writer.write_object_data(piece).await.unwrap();
}
self.writer.end_object().await.unwrap();
self.writer.flush().await.unwrap();
}
async fn list_refs(&mut self) -> Vec<String> {
let request = self.list_refs_request().await;
assert_eq!(request.collections, None);
request.refs
}
async fn list_refs_request(&mut self) -> ListRefsRequest {
match self.recv().await {
Some(Message::ListRefs(request)) => request,
other => panic!("{other:?}"),
}
}
async fn not_found(&mut self) {
self.send(&Message::GetReply(GetReply {
found: false,
len: None,
}))
.await;
}
async fn error(&mut self, code: ErrorCode, message: &str) {
self.send(&Message::Error(error_message(code, message)))
.await;
}
}
fn error_message(code: ErrorCode, message: &str) -> ErrorMessage {
ErrorMessage {
code,
message: message.to_owned(),
missing: Vec::new(),
current: None,
}
}
fn ends() -> ((PipeReader, PipeWriter), Server) {
let (to_server, server_in) = pipe();
let (server_out, from_server) = pipe();
(
(from_server, to_server),
Server {
reader: FrameReader::new(server_in),
writer: FrameWriter::new(server_out),
},
)
}
fn options(max_outstanding: Option<usize>) -> StreamSessionOptions {
StreamSessionOptions {
agent: Some("pull-session-test".to_owned()),
max_outstanding,
}
}
fn run<C, S, T>(client: C, server: S) -> T
where
C: Future<Output = T>,
S: Future<Output = ()>,
{
ostrya_rt::block_on(future::or(
async {
let (out, ()) = future::zip(client, server).await;
out
},
async {
ostrya_rt::Timer::after(LIMIT).await;
panic!("the session took longer than {LIMIT:?}");
},
))
}
type Call<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
async fn join_all<T>(futures: Vec<Call<'_, T>>) -> Vec<T> {
let mut futures: Vec<_> = futures.into_iter().map(Some).collect();
let mut outputs: Vec<Option<T>> = futures.iter().map(|_| None).collect();
future::poll_fn(|cx| {
let mut pending = false;
for (slot, out) in futures.iter_mut().zip(outputs.iter_mut()) {
if let Some(fut) = slot {
match fut.as_mut().poll(cx) {
Poll::Ready(value) => {
*out = Some(value);
*slot = None;
}
Poll::Pending => pending = true,
}
}
}
if pending {
Poll::Pending
} else {
Poll::Ready(())
}
})
.await;
outputs.into_iter().map(Option::unwrap).collect()
}
async fn read_all(mut body: PullBody) -> io::Result<Vec<u8>> {
let mut out = Vec::new();
body.read_to_end(&mut out).await?;
Ok(out)
}
fn carried(e: io::Error) -> Error {
*e.into_inner()
.expect("the read error carries an error")
.downcast::<Error>()
.expect("the read error carries the error of the session")
}
fn body_of(path: &str) -> Vec<u8> {
path.bytes().cycle().take(100 + path.len() * 1000).collect()
}
#[test]
fn concurrent_calls_share_the_pipeline_in_order() {
let paths: Vec<String> = (0..8).map(|i| format!("objects/{i:02}/file")).collect();
let ((input, output), mut server) = ends();
let expected = paths.clone();
let results = run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let calls: Vec<Call<'_, Option<Vec<u8>>>> = paths
.iter()
.map(|path| {
let session = &session;
Box::pin(async move {
match session.get(path, u64::MAX).await.unwrap() {
Some(body) => Some(read_all(body).await.unwrap()),
None => None,
}
}) as Call<'_, _>
})
.collect();
let results = join_all(calls).await;
session.finish().await.unwrap();
results
},
async {
server.hello(1).await;
let mut seen = Vec::new();
for _ in 0..8 {
seen.push(server.path().await);
}
let mut sorted = seen.clone();
sorted.sort();
assert_eq!(sorted, expected);
for path in &seen {
if path.ends_with("03/file") {
server.not_found().await;
} else {
let body = body_of(path);
server.body(Some(body.len() as u64), &body, 1000).await;
}
}
assert!(server.recv().await.is_none());
},
);
for (path, got) in paths.iter().zip(results) {
if path.ends_with("03/file") {
assert_eq!(got, None);
} else {
assert_eq!(got.unwrap(), body_of(path), "{path}");
}
}
}
#[test]
fn a_later_reply_arrives_while_the_caller_holds_a_body_read_to_its_end() {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let mut first = session.get("a", 100).await.unwrap().unwrap();
let mut bytes = Vec::new();
first.read_to_end(&mut bytes).await.unwrap();
assert_eq!(bytes, b"first");
let second = session.get("b", 100).await.unwrap().unwrap();
assert_eq!(second.len(), None);
assert_eq!(read_all(second).await.unwrap(), b"second");
drop(first);
session.finish().await.unwrap();
},
async {
server.hello(1).await;
assert_eq!(server.path().await, "a");
server.body(Some(5), b"first", 2).await;
assert_eq!(server.path().await, "b");
server.body(None, b"second", 4).await;
assert!(server.recv().await.is_none());
},
);
}
#[test]
fn a_stated_length_that_differs_from_the_body_is_protocol() {
for (stated, body) in [(10u64, &b"short"[..]), (3, &b"longer"[..])] {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let body = session.get("x", 100).await.unwrap().unwrap();
assert_eq!(body.len(), Some(stated));
let e = carried(read_all(body).await.unwrap_err());
assert!(matches!(e, Error::Protocol(_)), "{e:?}");
let later = session.get("y", 100).await.unwrap_err();
assert_eq!(later.to_string(), e.to_string());
assert!(matches!(session.finish().await, Err(Error::Protocol(_))));
},
async {
server.hello(1).await;
server.path().await;
server.body(Some(stated), body, 100).await;
},
);
}
}
#[test]
fn a_body_over_the_cap_ends_the_session() {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let e = session.get("big", 4).await.unwrap_err();
assert!(
matches!(&e, Error::LimitExceeded(m) if m.contains("big")),
"{e:?}"
);
let later = session.get("other", 4).await.unwrap_err();
assert!(matches!(later, Error::LimitExceeded(_)), "{later:?}");
assert_eq!(later.to_string(), e.to_string());
},
async {
server.hello(1).await;
server.path().await;
server
.send(&Message::GetReply(GetReply {
found: true,
len: Some(5),
}))
.await;
assert!(server.recv_or_eof().await);
},
);
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let body = session.get("big", 4).await.unwrap().unwrap();
let e = carried(read_all(body).await.unwrap_err());
assert!(matches!(e, Error::LimitExceeded(_)), "{e:?}");
},
async {
server.hello(1).await;
server.path().await;
server
.send(&Message::GetReply(GetReply {
found: true,
len: None,
}))
.await;
let _ = server.writer.write_object_data(b"12345").await;
let _ = server.writer.flush().await;
},
);
}
impl Server {
async fn recv_or_eof(&mut self) -> bool {
matches!(self.reader.read_message().await, Ok(None) | Err(_))
}
}
#[test]
fn a_dropped_call_or_body_ends_the_session() {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
{
let mut call = Box::pin(session.get("dropped", 100));
assert!(future::poll_once(&mut call).await.is_none());
}
let e = session.get("later", 100).await.unwrap_err();
assert!(
matches!(&e, Error::InvalidInput(m) if m.contains("dropped")),
"{e:?}"
);
assert!(matches!(
session.finish().await,
Err(Error::InvalidInput(_))
));
},
async {
server.hello(1).await;
assert_eq!(server.path().await, "dropped");
assert!(server.recv_or_eof().await);
},
);
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let mut body = session.get("held", 100).await.unwrap().unwrap();
let mut two = [0u8; 2];
body.read_exact(&mut two).await.unwrap();
drop(body);
let e = session.get("later", 100).await.unwrap_err();
assert!(
matches!(&e, Error::InvalidInput(m) if m.contains("body")),
"{e:?}"
);
},
async {
server.hello(1).await;
server.path().await;
server.body(Some(6), b"abcdef", 2).await;
},
);
}
#[test]
fn later_calls_repeat_the_first_error() {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let (first, second) = future::zip(session.get("a", 100), session.get("b", 100)).await;
assert!(matches!(&first, Err(Error::Internal(m)) if m == "cannot read a"));
assert!(matches!(&second, Err(Error::Internal(m)) if m == "cannot read a"));
let third = session.get("c", 100).await.unwrap_err();
assert_eq!(third.code(), Some(ErrorCode::Internal));
assert_eq!(third.to_string(), "internal: cannot read a");
assert!(matches!(session.finish().await, Err(Error::Internal(_))));
},
async {
server.hello(1).await;
server.path().await;
server.error(ErrorCode::Internal, "cannot read a").await;
},
);
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let first = session.get("a", 100).await.unwrap_err();
let Error::Io(io) = &first else {
panic!("{first:?}");
};
assert_eq!(io.kind(), io::ErrorKind::UnexpectedEof);
let later = session.get("b", 100).await.unwrap_err();
let Error::Io(again) = &later else {
panic!("{later:?}");
};
assert_eq!(again.kind(), io::ErrorKind::UnexpectedEof);
assert_eq!(again.to_string(), io.to_string());
},
async {
server.hello(1).await;
server.path().await;
let Server { reader, writer } = server;
drop(writer);
drop(reader);
},
);
}
#[test]
fn an_abandoned_body_gives_the_code_of_its_error() {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let body = session.get("a", 100).await.unwrap().unwrap();
let e = carried(read_all(body).await.unwrap_err());
assert!(
matches!(&e, Error::Internal(m) if m == "read failed"),
"{e:?}"
);
let later = session.get("b", 100).await.unwrap_err();
assert!(matches!(later, Error::Internal(_)), "{later:?}");
},
async {
server.hello(1).await;
server.path().await;
server
.send(&Message::GetReply(GetReply {
found: true,
len: Some(10),
}))
.await;
server.writer.write_object_data(b"abc").await.unwrap();
server
.writer
.abandon_body(&error_message(ErrorCode::Internal, "read failed"))
.await
.unwrap();
server.writer.flush().await.unwrap();
},
);
}
#[test]
fn the_hello_reply_gives_the_version_of_the_session() {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
assert!(session.get("config", 100).await.unwrap().is_none());
session.finish().await.unwrap();
},
async {
server.hello(1).await;
server.path().await;
server.not_found().await;
assert!(server.recv().await.is_none());
},
);
for version in [0, PULL_PROTOCOL_VERSION + 1] {
let ((input, output), mut server) = ends();
run(
async {
match StreamSession::over_stream(input, output, options(None)).await {
Err(Error::VersionUnsupported(m)) => assert!(m.contains("version"), "{m}"),
Err(other) => panic!("{other:?}"),
Ok(_) => panic!("version {version} accepted"),
}
},
async {
server.hello(version).await;
assert!(server.recv().await.is_none());
},
);
}
let ((input, output), mut server) = ends();
run(
async {
match StreamSession::over_stream(input, output, options(None)).await {
Err(Error::VersionUnsupported(m)) => assert_eq!(m, "no such version"),
Err(other) => panic!("{other:?}"),
Ok(_) => panic!("accepted"),
}
},
async {
server.recv().await;
server
.error(ErrorCode::VersionUnsupported, "no such version")
.await;
},
);
}
#[test]
fn a_depth_of_zero_keeps_one_get_in_flight() {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(Some(0)))
.await
.unwrap();
let calls: Vec<Call<'_, Vec<u8>>> = ["a", "b", "c"]
.into_iter()
.map(|path| {
let session = &session;
Box::pin(async move {
read_all(session.get(path, 100).await.unwrap().unwrap())
.await
.unwrap()
}) as Call<'_, _>
})
.collect();
let bodies = join_all(calls).await;
assert_eq!(bodies, [b"a".to_vec(), b"b".to_vec(), b"c".to_vec()]);
session.finish().await.unwrap();
},
async {
server.hello(1).await;
for _ in 0..3 {
let path = server.path().await;
let early = future::poll_once(server.reader.read_message()).await;
assert!(early.is_none(), "a second Get came early: {early:?}");
server.body(Some(1), path.as_bytes(), 1).await;
}
assert!(server.recv().await.is_none());
},
);
}
#[test]
fn finish_with_an_unread_body_is_an_unclean_end() {
let ((input, output), mut server) = ends();
let held = run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let mut body = session.get("a", 100).await.unwrap().unwrap();
let e = session.finish().await.unwrap_err();
assert!(
matches!(&e, Error::InvalidInput(m) if m.contains("unread")),
"{e:?}"
);
let mut bytes = Vec::new();
let later = carried(body.read_to_end(&mut bytes).await.unwrap_err());
assert_eq!(later.to_string(), e.to_string());
assert!(bytes.is_empty(), "{bytes:?}");
body
},
async {
server.hello(1).await;
server.path().await;
server.body(Some(3), b"abc", 3).await;
assert!(server.recv_or_eof().await);
let write = server.writer.write_message(&Message::Abort).await;
assert!(
matches!(&write, Err(Error::Io(e)) if e.kind() == io::ErrorKind::BrokenPipe),
"the client kept its input open: {write:?}"
);
},
);
drop(held);
}
#[test]
fn a_body_read_after_a_failure_repeats_the_failure() {
let ((input, output), mut server) = ends();
run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let mut body = session.get("a", 100).await.unwrap().unwrap();
{
let mut call = Box::pin(session.get("b", 100));
assert!(future::poll_once(&mut call).await.is_none());
}
let mut bytes = Vec::new();
let e = carried(body.read_to_end(&mut bytes).await.unwrap_err());
assert!(
matches!(&e, Error::InvalidInput(m) if m.contains("dropped")),
"{e:?}"
);
assert!(bytes.is_empty(), "{bytes:?}");
},
async {
server.hello(1).await;
assert_eq!(server.path().await, "a");
server.body(Some(3), b"abc", 3).await;
assert_eq!(server.path().await, "b");
},
);
}
#[test]
fn a_dropped_session_closes_its_output_while_a_body_is_held() {
let ((input, output), mut server) = ends();
let held = run(
async {
let session = StreamSession::over_stream(input, output, options(None))
.await
.unwrap();
let body = session.get("a", 100).await.unwrap().unwrap();
drop(session);
body
},
async {
server.hello(1).await;
server.path().await;
server.body(Some(3), b"abc", 3).await;
assert!(server.recv().await.is_none());
},
);
drop(held);
}
fn refused(opened: Result<PullSession>) -> Error {
match opened {
Ok(_) => panic!("the session opened"),
Err(e) => e,
}
}
#[test]
fn connect_refuses_stream_options_for_an_http_endpoint() {
for http in [FetcherOptions::new("ftp://h/"), FetcherOptions::default()] {
for (opts, message) in [
(
StreamSessionOptions {
agent: Some("agent".to_owned()),
max_outstanding: None,
},
"an HTTP pull takes no agent of a stream session",
),
(
StreamSessionOptions {
agent: None,
max_outstanding: Some(4),
},
"an HTTP pull takes no request limit of a stream session",
),
] {
let e = refused(ostrya_rt::block_on(PullSession::connect(
Endpoint::Http(http.clone()),
opts,
)));
assert!(
matches!(&e, Error::InvalidInput(m) if m.starts_with(message)),
"{e:?}"
);
}
}
let e = refused(ostrya_rt::block_on(PullSession::connect(
Endpoint::Http(FetcherOptions::new("ftp://h/")),
StreamSessionOptions::default(),
)));
assert!(matches!(e, Error::Fetch(_)), "{e:?}");
}
#[test]
fn connect_refuses_an_http_endpoint_with_no_mirror() {
let e = refused(ostrya_rt::block_on(PullSession::connect(
Endpoint::Http(FetcherOptions::default()),
StreamSessionOptions::default(),
)));
assert!(
matches!(&e, Error::InvalidInput(m) if m == "the HTTP endpoint of a pull names no mirror"),
"{e:?}"
);
}
#[test]
fn connect_for_http_gives_the_fetcher_and_its_limit() {
for (asked, admitted) in [(5, 5), (0, 1)] {
let mut http = FetcherOptions::new("http://127.0.0.1:9/");
http.max_redirects = 0;
http.proxy = Proxy::None;
http.max_outstanding = asked;
ostrya_rt::block_on(async {
let session =
PullSession::connect(Endpoint::Http(http), StreamSessionOptions::default())
.await
.unwrap();
assert!(matches!(session, PullSession::Http(_)));
assert_eq!(session.max_outstanding(), admitted);
session.finish().await.unwrap();
});
}
}
#[test]
fn over_stream_gives_the_limit_of_the_stream_session() {
for (asked, admitted) in [(Some(3), 3), (None, 8), (Some(0), 1)] {
let ((input, output), mut server) = ends();
run(
async {
let session = PullSession::over_stream(input, output, options(asked))
.await
.unwrap();
assert!(matches!(session, PullSession::Stream(_)));
assert_eq!(session.max_outstanding(), admitted);
session.finish().await.unwrap();
},
async {
server.hello(1).await;
assert!(server.recv().await.is_none());
},
);
}
}
#[test]
fn fetch_summary_over_a_stream_returns_both_files() {
let summary = body_of("summary");
let signature = body_of("summary.sig");
for (sig_served, summary_served) in [(true, true), (false, true), (true, false)] {
let ((input, output), mut server) = ends();
let got = run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let got = session.fetch_summary().await.unwrap();
session.finish().await.unwrap();
got
},
async {
server.hello(1).await;
assert_eq!(server.path().await, "summary.sig");
assert_eq!(server.path().await, "summary");
for (served, body) in [(sig_served, &signature), (summary_served, &summary)] {
if served {
server.body(Some(body.len() as u64), body, 1000).await;
} else {
server.not_found().await;
}
}
assert!(server.recv().await.is_none());
},
);
assert_eq!(got.0, summary_served.then(|| summary.clone()));
assert_eq!(got.1, sig_served.then(|| signature.clone()));
}
}
#[test]
fn fetch_summary_refuses_a_stated_length_over_64_mib() {
const CAP: u64 = 64 * 1024 * 1024;
let ((input, output), mut server) = ends();
run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let e = session.fetch_summary().await.unwrap_err();
assert!(
matches!(&e, Error::LimitExceeded(m) if m.contains(&format!("over the limit {CAP}"))),
"{e:?}"
);
},
async {
server.hello(1).await;
server.path().await;
server.path().await;
server
.send(&Message::GetReply(GetReply {
found: true,
len: Some(CAP + 1),
}))
.await;
assert!(server.recv_or_eof().await);
},
);
}
#[test]
fn fetch_refs_and_get_keep_their_order() {
let listing = body_of("listing");
let config = body_of("config");
for refs_first in [false, true] {
let ((input, output), mut server) = ends();
let (refs, got) = run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let PullSession::Stream(stream) = &session else {
unreachable!("over_stream gives a stream session");
};
let get = async {
let body = stream.get("config", u64::MAX).await.unwrap().unwrap();
read_all(body).await.unwrap()
};
let refs = async {
session
.fetch_refs(&selector(&["a", "b/c"]), None)
.await
.unwrap()
};
let out = if refs_first {
future::zip(refs, get).await
} else {
let (got, refs) = future::zip(get, refs).await;
(refs, got)
};
session.finish().await.unwrap();
out
},
async {
server.hello(1).await;
let prefixes = vec!["a".to_owned(), "b/c".to_owned()];
if refs_first {
assert_eq!(server.list_refs().await, prefixes);
assert_eq!(server.path().await, "config");
server
.body(Some(listing.len() as u64), &listing, 1000)
.await;
server.body(Some(config.len() as u64), &config, 1000).await;
} else {
assert_eq!(server.path().await, "config");
assert_eq!(server.list_refs().await, prefixes);
server.body(Some(config.len() as u64), &config, 1000).await;
server
.body(Some(listing.len() as u64), &listing, 1000)
.await;
}
assert!(server.recv().await.is_none());
},
);
assert_eq!(refs, Some(listing.clone()), "{refs_first}");
assert_eq!(got, config, "{refs_first}");
}
}
#[test]
fn fetch_refs_sends_an_empty_list_and_reads_the_listing() {
let listing = body_of("all");
let ((input, output), mut server) = ends();
let got = run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let got = session
.fetch_refs(&selector::<&str>(&[]), None)
.await
.unwrap();
session.finish().await.unwrap();
got
},
async {
server.hello(1).await;
assert!(server.list_refs().await.is_empty());
server.body(Some(listing.len() as u64), &listing, 700).await;
assert!(server.recv().await.is_none());
},
);
assert_eq!(got, Some(listing));
}
#[test]
fn fetch_refs_refuses_a_stated_length_over_the_cap() {
let ((input, output), mut server) = ends();
run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let e = session
.fetch_refs(&selector(&["main"]), None)
.await
.unwrap_err();
assert!(
matches!(&e, Error::LimitExceeded(m)
if m.contains(&format!("over the limit {MAX_REF_LISTING}"))),
"{e:?}"
);
},
async {
server.hello(1).await;
server.list_refs().await;
server
.send(&Message::GetReply(GetReply {
found: true,
len: Some(MAX_REF_LISTING + 1),
}))
.await;
assert!(server.recv_or_eof().await);
},
);
}
#[test]
fn fetch_refs_gives_the_limit_exceeded_of_the_server() {
let ((input, output), mut server) = ends();
run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let e = session
.fetch_refs(&selector::<&str>(&[]), None)
.await
.unwrap_err();
assert!(
matches!(&e, Error::LimitExceeded(m) if m == "too large"),
"{e:?}"
);
let e = session
.fetch_refs(&selector::<&str>(&[]), None)
.await
.unwrap_err();
assert!(
matches!(&e, Error::LimitExceeded(m) if m == "too large"),
"{e:?}"
);
},
async {
server.hello(1).await;
server.list_refs().await;
server.error(ErrorCode::LimitExceeded, "too large").await;
assert!(server.recv_or_eof().await);
},
);
}
#[test]
fn fetch_refs_refuses_an_entry_over_a_limit_and_writes_nothing() {
let ((input, output), mut server) = ends();
let config = body_of("config");
let got = run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let parts = |n: usize| vec!["a"; n].join("/");
for bad in [
parts(256 + 1),
"a".repeat(4096 + 1),
format!("!{}", "a".repeat(4096)),
] {
let e = session
.fetch_refs(&selector(&["main", bad.as_str()]), None)
.await
.unwrap_err();
assert!(matches!(e, Error::InvalidInput(_)), "{e:?}");
}
let PullSession::Stream(stream) = &session else {
unreachable!("over_stream gives a stream session");
};
let body = stream.get("config", u64::MAX).await.unwrap().unwrap();
let got = read_all(body).await.unwrap();
session.finish().await.unwrap();
got
},
async {
server.hello(1).await;
assert_eq!(server.path().await, "config");
server.body(Some(config.len() as u64), &config, 1000).await;
assert!(server.recv().await.is_none());
},
);
assert_eq!(got, config);
}
#[test]
fn a_not_found_reply_to_list_refs_is_protocol() {
let ((input, output), mut server) = ends();
run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let e = session
.fetch_refs(&selector(&["main"]), None)
.await
.unwrap_err();
assert!(
matches!(&e, Error::Protocol(m) if m.contains("ListRefs")),
"{e:?}"
);
let PullSession::Stream(stream) = &session else {
unreachable!("over_stream gives a stream session");
};
let e = stream.get("config", 1).await.unwrap_err();
assert!(matches!(e, Error::Protocol(_)), "{e:?}");
},
async {
server.hello(1).await;
server.list_refs().await;
server.not_found().await;
assert!(server.recv_or_eof().await);
},
);
}
#[test]
fn a_reply_to_list_refs_with_no_length_is_protocol() {
let ((input, output), mut server) = ends();
run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let e = session
.fetch_refs(&selector(&["main"]), None)
.await
.unwrap_err();
assert!(
matches!(&e, Error::Protocol(m) if m.contains("no length")),
"{e:?}"
);
let PullSession::Stream(stream) = &session else {
unreachable!("over_stream gives a stream session");
};
let e = stream.get("config", 1).await.unwrap_err();
assert!(
matches!(&e, Error::Protocol(m) if m.contains("no length")),
"{e:?}"
);
},
async {
server.hello(1).await;
server.list_refs().await;
server
.send(&Message::GetReply(GetReply {
found: true,
len: None,
}))
.await;
assert!(server.recv_or_eof().await);
},
);
}
fn dead_http_session() -> PullSession {
let mut http = FetcherOptions::new("http://127.0.0.1:9/");
http.max_redirects = 0;
http.max_retries = 0;
http.proxy = Proxy::None;
ostrya_rt::block_on(PullSession::connect(
Endpoint::Http(http),
StreamSessionOptions::default(),
))
.unwrap()
}
fn mixed_entries(count: usize) -> Vec<String> {
(0..count)
.map(|n| {
if n % 2 == 0 {
format!("r{n}")
} else {
format!("!r{n}")
}
})
.collect()
}
#[test]
fn fetch_refs_over_http_refuses_bad_input_before_a_request() {
let session = dead_http_session();
let many = mixed_entries(257);
let long = (0..256)
.map(|n| format!("{n:0>64}"))
.collect::<Vec<String>>();
let accents: Vec<String> = (0..4).map(|n| format!("{}{n}", "é".repeat(700))).collect();
let parts = "a/".repeat(256) + "a";
let bytes = "a".repeat(4096 + 1);
let negative_bytes = format!("!{}", "a".repeat(4096));
ostrya_rt::block_on(async {
for (case, entries) in [
("257 entries", &many),
("long target", &long),
("escaped target", &accents),
("257 parts", &vec![parts]),
("4097 bytes", &vec![bytes]),
("negative 4097 bytes", &vec![negative_bytes]),
] {
let e = session
.fetch_refs(&selector(entries), None)
.await
.unwrap_err();
assert!(matches!(e, Error::InvalidInput(_)), "{case}: {e:?}");
}
let e = session
.fetch_refs(&selector(&["main"; 300]), None)
.await
.unwrap_err();
assert!(matches!(e, Error::Fetch(_)), "{e:?}");
let e = session
.fetch_refs(&selector(&many[..256]), None)
.await
.unwrap_err();
assert!(matches!(e, Error::Fetch(_)), "{e:?}");
});
ostrya_rt::block_on(session.finish()).unwrap();
}
#[test]
fn fetch_refs_over_a_stream_counts_the_entries_that_stay() {
let ((input, output), mut server) = ends();
let listing = body_of("listing");
let got = run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let many = mixed_entries(257);
let e = session
.fetch_refs(&selector(&many), None)
.await
.unwrap_err();
assert!(matches!(e, Error::InvalidInput(_)), "{e:?}");
let mut entries = vec!["!a/*", "a/b", "main", "a", "!main"];
entries.extend(["main"; 300]);
let got = session.fetch_refs(&selector(&entries), None).await.unwrap();
session.finish().await.unwrap();
got
},
async {
server.hello(1).await;
assert_eq!(
server.list_refs().await,
["a/b", "main", "a", "!a/*", "!main"]
);
server.body(Some(listing.len() as u64), &listing, 700).await;
assert!(server.recv().await.is_none());
},
);
assert_eq!(got, Some(listing));
}
#[test]
fn fetch_refs_over_a_stream_sends_the_collection_entries() {
let ((input, output), mut server) = ends();
let listing = body_of("listing");
let served = listing.clone();
let got = run(
async {
let session = PullSession::over_stream(input, output, options(None))
.await
.unwrap();
let many = mixed_entries(200);
let e = session
.fetch_refs(&selector(&many), Some(&selector(&many[..57])))
.await
.unwrap_err();
assert!(matches!(e, Error::InvalidInput(_)), "{e:?}");
let mut got = Vec::new();
let collections = selector(&["!org.b/**", "org.a/*", "org.a/*"]);
for collections in [Some(&collections), Some(&selector::<&str>(&[])), None] {
let listing = session
.fetch_refs(&selector(&["main"]), collections)
.await
.unwrap();
got.push(listing);
}
session.finish().await.unwrap();
got
},
async {
server.hello(1).await;
let entries =
|list: &[&str]| -> Vec<String> { list.iter().map(|e| (*e).to_owned()).collect() };
for collections in [
Some(entries(&["org.a/*", "!org.b/**"])),
Some(Vec::new()),
None,
] {
assert_eq!(
server.list_refs_request().await,
ListRefsRequest {
refs: entries(&["main"]),
collections,
}
);
server.body(Some(served.len() as u64), &served, 700).await;
}
assert!(server.recv().await.is_none());
},
);
assert_eq!(got, vec![Some(listing); 3]);
}