protocol.rs
⎇
Raw
1//! XML request parsing, response building, and PUT body validation.
2
3use pimdav::object::{self, CalendarObject, Invalid};
4use pimdav::xml::{self, CALDAV, DAV, Name, Propfind};
5use xmltree::Element;
6
7#[test]
8fn propfind_bodies() {
9 assert_eq!(xml::propfind(b""), Ok(Propfind::AllProp(vec![])));
10 let body = br#"<?xml version="1.0"?>
11 <d:propfind xmlns:d="DAV:" xmlns:x="urn:x">
12 <d:prop><d:getetag/><x:odd/></d:prop>
13 </d:propfind>"#;
14 assert_eq!(
15 xml::propfind(body),
16 Ok(Propfind::Prop(vec![
17 Name::new(DAV, "getetag"),
18 Name::new("urn:x", "odd")
19 ]))
20 );
21 let body = br#"<propfind xmlns="DAV:"><allprop/><include><getetag/></include></propfind>"#;
22 assert_eq!(
23 xml::propfind(body),
24 Ok(Propfind::AllProp(vec![Name::new(DAV, "getetag")]))
25 );
26 let body = br#"<propfind xmlns="DAV:"><propname/></propfind>"#;
27 assert_eq!(xml::propfind(body), Ok(Propfind::PropName));
28 assert!(xml::propfind(b"<nope/>").is_err());
29 assert!(xml::propfind(b"<propfind").is_err());
30}
31
32#[test]
33fn update_bodies() {
34 let body = br#"<d:propertyupdate xmlns:d="DAV:" xmlns:i="http://apple.com/ns/ical/">
35 <d:set><d:prop><d:displayname>Work</d:displayname><i:calendar-color>#ff0000</i:calendar-color></d:prop></d:set>
36 <d:remove><d:prop><d:displayname/></d:prop></d:remove>
37 </d:propertyupdate>"#;
38 let u = xml::update(body).unwrap();
39 assert_eq!(u.set.len(), 2);
40 assert_eq!(xml::text(&u.set[0]), "Work");
41 assert_eq!(u.remove, vec![Name::new(DAV, "displayname")]);
42
43 let body = br#"<c:mkcalendar xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
44 <d:set><d:prop><c:supported-calendar-component-set><c:comp name="VTODO"/></c:supported-calendar-component-set></d:prop></d:set>
45 </c:mkcalendar>"#;
46 let u = xml::update(body).unwrap();
47 assert!(Name::of(&u.set[0]).is(CALDAV, "supported-calendar-component-set"));
48 assert!(xml::update(b"").unwrap().set.is_empty());
49 assert!(xml::update(b"<d:other xmlns:d=\"DAV:\"/>").is_err());
50}
51
52#[test]
53fn multistatus_round_trips() {
54 let mut r = xml::Response::new("/a b/");
55 r.push(
56 200,
57 xml::with_text(xml::el(DAV, "displayname"), "x < y & \"z\""),
58 );
59 r.push(404, Name::new("urn:x", "odd").element());
60 r.push(
61 200,
62 xml::with_attr(xml::el(CALDAV, "comp"), "name", "VEVENT"),
63 );
64 let out = xml::multistatus(&Name::new(DAV, "multistatus"), &[r]);
65
66 let root = Element::parse(out.as_bytes()).unwrap();
67 assert!(Name::of(&root).is(DAV, "multistatus"));
68 let response = xml::child(&root, DAV, "response").unwrap();
69 assert_eq!(
70 xml::text(xml::child(response, DAV, "href").unwrap()),
71 "/a b/"
72 );
73 let stats: Vec<_> = xml::elements(response)
74 .filter(|e| e.name == "propstat")
75 .collect();
76 assert_eq!(stats.len(), 2);
77 let ok = xml::child(stats[0], DAV, "prop").unwrap();
78 assert_eq!(
79 xml::text(xml::child(ok, DAV, "displayname").unwrap()),
80 "x < y & \"z\""
81 );
82 assert_eq!(
83 xml::child(ok, CALDAV, "comp").unwrap().attributes["name"],
84 "VEVENT"
85 );
86 let missing = xml::child(stats[1], DAV, "prop").unwrap();
87 assert!(xml::child(missing, "urn:x", "odd").is_some());
88 assert_eq!(
89 xml::text(xml::child(stats[1], DAV, "status").unwrap()),
90 "HTTP/1.1 404 Not Found"
91 );
92}
93
94fn ics(body: &str) -> Vec<u8> {
95 format!("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\n{body}END:VCALENDAR\r\n")
96 .into_bytes()
97}
98
99const EVENT: &str = "BEGIN:VEVENT\r\nUID:a\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260101T100000Z\r\nEND:VEVENT\r\n";
100
101#[test]
102fn calendar_objects() {
103 let all = ["VEVENT", "VTODO"];
104 assert_eq!(
105 object::calendar(&ics(EVENT), &all),
106 Ok(CalendarObject {
107 uid: "a".into(),
108 component: "VEVENT"
109 })
110 );
111 let override_ = EVENT.replace("DTSTART", "RECURRENCE-ID:20260102T100000Z\r\nDTSTART");
112 assert!(object::calendar(&ics(&format!("{EVENT}{override_}")), &all).is_ok());
113
114 let cases = [
115 (
116 ics(&format!("METHOD:REQUEST\r\n{EVENT}")),
117 Invalid::CalendarResource,
118 ),
119 (
120 ics(&format!("{EVENT}{}", EVENT.replace("UID:a", "UID:b"))),
121 Invalid::CalendarResource,
122 ),
123 (
124 ics(&format!("{EVENT}{}", EVENT.replace("VEVENT", "VTODO"))),
125 Invalid::CalendarResource,
126 ),
127 (
128 ics(&EVENT.replace("UID:a\r\n", "")),
129 Invalid::CalendarResource,
130 ),
131 (ics(""), Invalid::CalendarResource),
132 (
133 ics(&EVENT.replace("VEVENT", "VJOURNAL")),
134 Invalid::CalendarComponent,
135 ),
136 (b"not a calendar".to_vec(), Invalid::CalendarData),
137 (vec![0xff, 0xfe], Invalid::CalendarData),
138 ([ics(EVENT), ics(EVENT)].concat(), Invalid::CalendarResource),
139 ];
140 for (body, want) in cases {
141 assert_eq!(
142 object::calendar(&body, &all),
143 Err(want),
144 "{}",
145 String::from_utf8_lossy(&body)
146 );
147 }
148}
149
150#[test]
151fn vcards() {
152 let card = "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:c1\r\nFN:Bob\r\nEND:VCARD\r\n";
153 assert_eq!(object::vcard(card.as_bytes()), Ok(Some("c1".into())));
154 let no_uid = card.replace("UID:c1\r\n", "");
155 assert_eq!(object::vcard(no_uid.as_bytes()), Ok(None));
156 assert_eq!(object::vcard(&ics(EVENT)), Err(Invalid::AddressData));
157 assert_eq!(
158 object::vcard(format!("{card}{card}").as_bytes()),
159 Err(Invalid::AddressData)
160 );
161}
162