#[path = "support/live.rs"]
mod live;
#[path = "support/supervisor.rs"]
mod supervisor;
use std::collections::BTreeMap;
use base64::Engine as _;
use printobserver_sdk::{Actor, Client, ImageAnswer, JobManifest};
use serde_json::Value;
fn bytes_of(answered: &ImageAnswer) -> Vec<u8> {
let path = answered
.path
.as_ref()
.expect("the supervisor answered a path on its own host");
std::fs::read(path).expect("the image at the answered path opens")
}
fn base64_of_the_image_into_a_string_field(answered: &ImageAnswer) -> ImageAnswer {
let mut variant = answered.clone();
variant.record.sha256 = base64::engine::general_purpose::STANDARD.encode(bytes_of(answered));
variant
}
fn the_images_bytes_in_place_of_a_field(answered: &ImageAnswer) -> Value {
let mut variant = serde_json::to_value(answered).expect("an answer renders back as a document");
variant["record"]["content_type"] =
serde_json::to_value(bytes_of(answered)).expect("bytes render as a sequence of them");
variant
}
#[test]
fn the_equality_the_walk_asserts_refuses_a_client_that_carries_the_image() {
let root = tempfile::tempdir().expect("this walk's own root");
let mut standing = supervisor::standing(root.path(), false);
let world = standing.at.clone();
let proxy = live::Proxy::in_front_of(&world.server);
let client = Client::new(proxy.url(), Actor::Operator).with_credential(&*world.credential);
let written = client
.manifest_set(
&world.print_id,
"a falsifying fixture is asking",
&manifest(&world),
)
.expect("a manifest write is answered by a real supervisor");
live::same("manifest_set", &written, &proxy.last().answer);
let answered = client
.image(&world.image_id)
.expect("an image read is answered by a real supervisor");
let sent = proxy.last().answer;
assert_eq!(answered.record.print_id, world.print_id);
assert_eq!(answered.record.event_id, world.event_id);
live::same("image", &answered, &sent);
let with_base64 = base64_of_the_image_into_a_string_field(&answered);
assert_ne!(
with_base64.record.sha256, answered.record.sha256,
"the first variant changed nothing, so it proves nothing"
);
assert!(
!live::matches(&with_base64, &sent),
"a client writing a base64 of the image into a string field was not refused"
);
let bytes = bytes_of(&answered);
assert!(
std::str::from_utf8(&bytes).is_err(),
"the image this world stores is text, so a byte-valued substitution of it \
would prove nothing a string one does not"
);
let with_bytes = the_images_bytes_in_place_of_a_field(&answered);
let substituted = &with_bytes["record"]["content_type"];
assert_eq!(
substituted.as_array().map(Vec::len),
Some(bytes.len()),
"the second variant carries {substituted} where the image's own {} bytes belong",
bytes.len()
);
assert!(
!live::matches(&with_bytes, &sent),
"a client returning the image's bytes in place of a field was not refused"
);
assert_eq!(
proxy.calls(),
2,
"a call reached the supervisor without going through the proxy"
);
standing.stop();
}
fn manifest(world: &supervisor::Supervisor) -> JobManifest {
JobManifest {
file_name: world.file_name.clone(),
material: "PLA".to_owned(),
nozzle_diameter_mm: 0.4,
slicer_profile: "a falsifying fixture's own profile".to_owned(),
allowed: BTreeMap::new(),
metadata: BTreeMap::new(),
}
}