//! Answers and requests, from the tests of the SOAP layer of the source.
use proptest::prelude::*;
use super::*;
use crate::codes::{DEFAULT_BACK_OFF_MS, MAX_BACK_OFF_MS, MIN_BACK_OFF_MS};
use crate::soap::{Element, Version, envelope, escape};
fn reply_with(code: &str, extra: &str) -> String {
format!(
"\
{code}{extra}\
"
)
}
/// The error the first message of a `GetFolder` answer names.
fn folder_error(body: &str) -> Error {
let answer = Answer::parse(body.as_bytes()).unwrap();
answer.messages("GetFolder").unwrap()[0].ok().unwrap_err()
}
#[test]
fn a_busy_server_says_how_long_and_is_believed_only_so_far() {
assert_eq!(
folder_error(&reply_with("ErrorServerBusy", "soon")),
Error::Busy { back_off_ms: DEFAULT_BACK_OFF_MS }
);
assert_eq!(
folder_error(&reply_with("ErrorServerBusy", "7000")),
Error::Busy { back_off_ms: 7_000 }
);
assert_eq!(
folder_error(&reply_with("ErrorServerBusy", "9999999999999999999999")),
Error::Busy { back_off_ms: MAX_BACK_OFF_MS }
);
assert_eq!(folder_error(&reply_with("ErrorServerBusy", "")), Error::Busy { back_off_ms: DEFAULT_BACK_OFF_MS });
// A wait of nothing is a wait.
assert_eq!(
folder_error(&reply_with("ErrorServerBusy", "0")),
Error::Busy { back_off_ms: MIN_BACK_OFF_MS }
);
// The whole answer says so.
let answer = Answer::parse(reply_with("ErrorServerBusy", "7000").as_bytes()).unwrap();
assert_eq!(answer.busy(), Some(Error::Busy { back_off_ms: 7_000 }));
assert_eq!(Answer::parse(reply_with("ErrorAccessDenied", "").as_bytes()).unwrap().busy(), None);
}
#[test]
fn a_fault_is_the_error_it_names() {
let fault = |code: &str| {
format!(
"s:Client\
{code}"
)
};
assert_eq!(Answer::parse(fault("ErrorSchemaValidation").as_bytes()).unwrap_err(), Error::Server(ResponseCode::SchemaValidation));
assert_eq!(Answer::parse(fault("ErrorInvalidRequest").as_bytes()).unwrap_err(), Error::Server(ResponseCode::InvalidRequest));
assert_eq!(Answer::parse(fault("not a code").as_bytes()).unwrap_err(), Error::Server(ResponseCode::Other(String::new())));
}
#[test]
fn prefixes_are_the_documents_own_choice() {
let body = "\
\
NoErrorInbox\
";
let answer = Answer::parse(body.as_bytes()).unwrap();
let messages = answer.messages("GetFolder").unwrap();
assert_eq!((messages[0].class(), messages[0].code()), (ResponseClass::Success, &ResponseCode::NoError));
let folder = messages[0].ok().unwrap().path(&["m:Folders", "t:Folder"]).unwrap();
assert_eq!(folder.child_attr("t:FolderId", "Id"), Some("AbC+/=-_"));
assert_eq!(folder.child_text("t:DisplayName").as_deref(), Some("Inbox"));
}
#[test]
fn an_element_in_the_wrong_namespace_is_not_the_one_asked_for() {
let body = reply_with("NoError", "").replace("services/2006/messages", "services/2006/elsewhere");
assert_eq!(Answer::parse(body.as_bytes()).unwrap().messages("GetFolder").unwrap_err(), Error::Unexpected);
// Nor is another operation's answer.
assert_eq!(Answer::parse(reply_with("NoError", "").as_bytes()).unwrap().messages("GetItem").unwrap_err(), Error::Unexpected);
}
#[test]
fn unknown_elements_are_skipped() {
let extra = "1";
assert_eq!(folder_error(&reply_with("ErrorAccessDenied", extra)), Error::Server(ResponseCode::AccessDenied));
}
#[test]
fn response_codes_are_kept_only_as_short_identifiers() {
assert_eq!(ResponseCode::from_name(" ErrorServerBusy "), ResponseCode::ServerBusy);
assert_eq!(ResponseCode::from_name("ErrorSomethingNew"), ResponseCode::Other("ErrorSomethingNew".into()));
assert_eq!(ResponseCode::from_name("bad code\r\nX: y"), ResponseCode::Other(String::new()));
assert_eq!(ResponseCode::from_name(&"A".repeat(500)), ResponseCode::Other(String::new()));
assert_eq!(ResponseCode::from_name("bad\r\nX: y").to_string(), "an unnamed error");
}
#[test]
fn a_message_without_a_class_or_a_code_is_unexpected() {
let no_class = reply_with("NoError", "").replace(" ResponseClass=\"Error\"", "");
assert_eq!(Answer::parse(no_class.as_bytes()).unwrap().messages("GetFolder").unwrap_err(), Error::Unexpected);
let odd_class = reply_with("NoError", "").replace("\"Error\"", "\"Maybe\"");
assert_eq!(Answer::parse(odd_class.as_bytes()).unwrap().messages("GetFolder").unwrap_err(), Error::Unexpected);
let no_code = reply_with("NoError", "").replace("NoError", "");
assert_eq!(Answer::parse(no_code.as_bytes()).unwrap().messages("GetFolder").unwrap_err(), Error::Unexpected);
}
#[test]
fn hostile_answers_are_errors_never_panics() {
for body in [
"]>".to_string(),
"".to_string(),
"not xml at all".to_string(),
format!(
"{}{}",
"".repeat(MAX_DEPTH),
"".repeat(MAX_DEPTH)
),
format!("{}", "".repeat(MAX_NODES)),
] {
assert_eq!(Answer::parse(body.as_bytes()).unwrap_err(), Error::Malformed, "{}", &body[..body.len().min(80)]);
}
assert_eq!(Answer::parse(&vec![b' '; MAX_BYTES + 1]).unwrap_err(), Error::Malformed);
assert_eq!(Answer::parse(b"Sign in").unwrap_err(), Error::Unexpected);
assert_eq!(Answer::parse(b"").unwrap_err(), Error::Unexpected);
// An entity XML does not define is left out, and the rest is read.
let body = reply_with("Error&nonsense;AccessDenied", "");
assert_eq!(folder_error(&body), Error::Server(ResponseCode::AccessDenied));
}
#[test]
fn a_busy_fault_with_its_wait_in_the_types_namespace_is_read() {
let fault = |wait: &str| {
format!(
"s:Server\
ErrorServerBusy\
{wait}\
"
)
};
assert_eq!(Answer::parse(fault("297749").as_bytes()).unwrap_err(), Error::Busy { back_off_ms: 297_749 });
assert_eq!(Answer::parse(fault("9999999").as_bytes()).unwrap_err(), Error::Busy { back_off_ms: MAX_BACK_OFF_MS });
}
#[test]
fn a_busy_server_inside_an_internal_server_error_is_busy() {
let inner = "ErrorServerBusy4000";
assert_eq!(folder_error(&reply_with("ErrorInternalServerError", inner)), Error::Busy { back_off_ms: 4_000 });
// Without the inner error it is the server's own error, and not a reason to wait.
let body =
reply_with("ErrorInternalServerError", "4000");
assert_eq!(folder_error(&body), Error::Server(ResponseCode::InternalServerError));
// The same in a fault.
let fault = "s:Server\
ErrorInternalServerError\
ErrorServerBusy2500";
assert_eq!(Answer::parse(fault.as_bytes()).unwrap_err(), Error::Busy { back_off_ms: 2_500 });
}
#[test]
fn debug_output_shows_nothing_a_server_chose() {
let body = "\
NoErrorSecret Name\
";
let answer = Answer::parse(body.as_bytes()).unwrap();
let messages = answer.messages("GetFolder").unwrap();
let shown = format!("{answer:?} {messages:?} {:?}", Element::new("t:DisplayName").attr("Id", "SecretId").text("Secret Name"));
assert!(!shown.contains("Secret") && !shown.contains("Folders"), "{shown}");
}
// ---- the request ----
#[test]
fn the_request_is_a_soap_envelope_with_the_version_and_one_operation() {
let operation = Element::new("m:GetFolder").child(Element::new("m:FolderIds").child(Element::new("t:DistinguishedFolderId").attr("Id", "inbox")));
let text = envelope(Version::Exchange2016, &operation);
let tree = parse_xml(text.as_bytes()).unwrap();
assert_eq!(tree.name, "s:Envelope");
assert_eq!(tree.path(&["s:Header", "t:RequestServerVersion"]).and_then(|v| v.attr("Version")), Some("Exchange2016"));
let body = tree.child("s:Body").unwrap();
assert_eq!(body.elements().map(|e| e.name.as_str()).collect::>(), ["m:GetFolder"]);
assert!(envelope(Version::Exchange2010Sp2, &operation).contains("Exchange2010_SP2"));
assert_eq!(Version::NAMES.len(), 9);
}
#[test]
fn nothing_written_can_break_out_of_its_place() {
let nasty = "a\r\nX-Injected: 1\0\"'&";
let element = Element::new("m:Op\r\n") && !text.contains(""));
assert_eq!(op.elements().count(), 1);
}
#[test]
fn a_text_keeps_its_lines() {
let text = "first\r\nsecond\tindented\nthird\0\u{1}\u{FFFE}<&>";
let written = Element::new("t:Body").text(text).write();
assert_eq!(written, "first
\nsecond\tindented\nthird<&>");
assert!(!written.contains(['\r', '\0']));
let tree = parse_xml(envelope(Version::default(), &Element::new("m:Op").text(text)).as_bytes()).unwrap();
let op = tree.child("s:Body").unwrap().elements().next().unwrap();
assert_eq!(op.text(), "first\r\nsecond\tindented\nthird<&>");
// An attribute, or a header, keeps none of it.
assert_eq!(escape("a\r\nb\tc\0"), "abc");
}
proptest! {
#[test]
fn arbitrary_bytes_never_panic(bytes in proptest::collection::vec(any::(), 0..4096)) {
if let Ok(answer) = Answer::parse(&bytes) {
let _ = (answer.busy(), answer.messages("GetFolder").map(|m| m.len()));
}
}
#[test]
fn any_text_written_reads_back_as_itself(text in "\\PC{0,200}", name in "[a-zA-Z][a-zA-Z0-9]{0,10}") {
let tree = parse_xml(envelope(Version::default(), &Element::new(&format!("m:{name}")).attr("A", &text).text(&text)).as_bytes()).unwrap();
let op = tree.child("s:Body").unwrap().elements().next().unwrap();
let kept: String = text.chars().filter(|c| !c.is_control() && *c != '\u{FFFE}' && *c != '\u{FFFF}').collect();
prop_assert_eq!(op.attr("A"), Some(kept.as_str()));
prop_assert_eq!(op.text().into_owned(), kept);
}
}