61 lines
1.9 KiB
Rust
61 lines
1.9 KiB
Rust
|
|
//! Milestone 1: the daemon binds its socket and answers the protocol.
|
||
|
|
|
||
|
|
mod support;
|
||
|
|
|
||
|
|
use varde_proto::{ErrorCode, Request, ResponseData, PROTOCOL_VERSION};
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn status_round_trips_on_empty_daemon() {
|
||
|
|
let daemon = support::spawn_daemon();
|
||
|
|
let mut client = support::Client::connect(&daemon.socket);
|
||
|
|
|
||
|
|
let reply = client.request(&Request::Status { hash: None });
|
||
|
|
assert!(reply.ok, "status failed: {:?}", reply.error);
|
||
|
|
match reply.data {
|
||
|
|
Some(ResponseData::Status(s)) => {
|
||
|
|
assert_eq!(s.protocol, PROTOCOL_VERSION);
|
||
|
|
assert_eq!(s.pins, 0);
|
||
|
|
assert_eq!(s.store_path, daemon.store.display().to_string());
|
||
|
|
}
|
||
|
|
other => panic!("unexpected status payload: {other:?}"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn list_is_empty_on_fresh_daemon() {
|
||
|
|
let daemon = support::spawn_daemon();
|
||
|
|
let mut client = support::Client::connect(&daemon.socket);
|
||
|
|
|
||
|
|
let reply = client.request(&Request::List {});
|
||
|
|
assert!(reply.ok);
|
||
|
|
match reply.data {
|
||
|
|
Some(ResponseData::Pins { pins }) => assert!(pins.is_empty()),
|
||
|
|
other => panic!("unexpected list payload: {other:?}"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn malformed_json_gets_structured_error_and_connection_survives() {
|
||
|
|
let daemon = support::spawn_daemon();
|
||
|
|
let mut client = support::Client::connect(&daemon.socket);
|
||
|
|
|
||
|
|
let reply = client.send_raw("this is not json");
|
||
|
|
assert!(!reply.ok);
|
||
|
|
assert_eq!(reply.error.expect("error body").code, ErrorCode::BadRequest);
|
||
|
|
|
||
|
|
// The same connection keeps working after a bad request.
|
||
|
|
let reply = client.request(&Request::Status { hash: None });
|
||
|
|
assert!(reply.ok);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn multiple_concurrent_clients() {
|
||
|
|
let daemon = support::spawn_daemon();
|
||
|
|
let mut a = support::Client::connect(&daemon.socket);
|
||
|
|
let mut b = support::Client::connect(&daemon.socket);
|
||
|
|
|
||
|
|
assert!(a.request(&Request::Status { hash: None }).ok);
|
||
|
|
assert!(b.request(&Request::List {}).ok);
|
||
|
|
assert!(a.request(&Request::List {}).ok);
|
||
|
|
}
|