Re-enable + enhance DAV decode tests

This commit is contained in:
Quentin 2024-03-05 19:06:04 +01:00
parent 1aafd752ca
commit 2dd6deae54
Signed by: quentin
GPG key ID: E9602264D639FF68
5 changed files with 68 additions and 170 deletions

View file

@ -7,25 +7,25 @@ use super::error;
// ---- EXTENSIONS --- // ---- EXTENSIONS ---
impl xml::QRead<Violation> for Violation { impl xml::QRead<Violation> for Violation {
async fn qread(&self, xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> { async fn qread(xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> {
unreachable!(); unreachable!();
} }
} }
impl xml::QRead<Property> for Property { impl xml::QRead<Property> for Property {
async fn qread(&self, xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> { async fn qread(xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> {
unreachable!(); unreachable!();
} }
} }
impl xml::QRead<PropertyRequest> for PropertyRequest { impl xml::QRead<PropertyRequest> for PropertyRequest {
async fn qread(&self, xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> { async fn qread(xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> {
unreachable!(); unreachable!();
} }
} }
impl xml::QRead<ResourceType> for ResourceType { impl xml::QRead<ResourceType> for ResourceType {
async fn qread(&self, xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> { async fn qread(xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> {
unreachable!(); unreachable!();
} }
} }

View file

@ -8,135 +8,25 @@ use quick_xml::reader::NsReader;
use tokio::io::AsyncBufRead; use tokio::io::AsyncBufRead;
use super::types::*; use super::types::*;
use super::error::*; use super::error::ParsingError;
use super::xml::{QRead, Reader, IRead, DAV_URN, CAL_URN};
/* impl<E: Extension> QRead<PropFind<E>> for PropFind<E> {
// --- Traits ---- async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<Self>, ParsingError> {
trait Reader = AsyncBufRead+Unpin+'static;
trait Decodable: Extension {
async fn decode_propreq(xml: &mut PeekRead<impl Reader>) -> Result<Option<Self::PropertyRequest>, ParsingError>;
}
impl Decodable for NoExtension {
async fn decode_propreq(xml: &mut PeekRead<impl Reader>) -> Result<Option<Disabled>, ParsingError> {
Ok(None)
}
}
pub trait QReadable<T: Reader>: Sized {
async fn read(xml: &mut PeekRead<T>) -> Result<Self, ParsingError>;
}
// --- Peek read with namespaces
const DAV_URN: &[u8] = b"DAV:";
const CALDAV_URN: &[u8] = b"urn:ietf:params:xml:ns:caldav";
const CARDDAV_URN: &[u8] = b"urn:ietf:params:xml:ns:carddav";
//const XML_URN: &[u8] = b"xml";
pub struct PeekRead<T: Reader> {
evt: Event<'static>,
rdr: NsReader<T>,
buf: Vec<u8>,
}
impl<T: Reader> PeekRead<T> {
async fn new(mut rdr: NsReader<T>) -> Result<Self, ParsingError> {
let mut buf: Vec<u8> = vec![];
let evt = rdr.read_event_into_async(&mut buf).await?.into_owned();
buf.clear();
Ok(Self { evt, rdr, buf })
}
fn peek(&self) -> &Event<'static> {
&self.evt
}
/// skip tag. Can't skip end, can't skip eof.
async fn skip(&mut self) -> Result<Event<'static>, ParsingError> {
match &self.evt {
Event::Start(b) => {
let _span = self.rdr.read_to_end_into_async(b.to_end().name(), &mut self.buf).await?;
self.next().await
},
Event::End(_) => Err(ParsingError::WrongToken),
Event::Eof => Err(ParsingError::Eof),
_ => self.next().await,
}
}
/// read one more tag
async fn next(&mut self) -> Result<Event<'static>, ParsingError> {
let evt = self.rdr.read_event_into_async(&mut self.buf).await?.into_owned();
self.buf.clear();
let old_evt = std::mem::replace(&mut self.evt, evt);
Ok(old_evt)
}
/// check if this is the desired tag
fn is_tag(&self, ns: &[u8], key: &str) -> bool {
let qname = match self.peek() {
Event::Start(bs) | Event::Empty(bs) => bs.name(),
Event::End(be) => be.name(),
_ => return false,
};
let (extr_ns, local) = self.rdr.resolve_element(qname);
if local.into_inner() != key.as_bytes() {
return false
}
match extr_ns {
ResolveResult::Bound(v) => v.into_inner() == ns,
_ => false,
}
}
/// find start tag
async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
loop {
match self.peek() {
Event::Start(b) if self.is_tag(ns, key) => break,
_ => { self.skip().await?; },
}
}
self.next().await
}
// find stop tag
async fn tag_stop(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
loop {
match self.peek() {
Event::End(b) if self.is_tag(ns, key) => break,
_ => { self.skip().await?; },
}
}
self.next().await
}
}
// ----- Decode ----
impl<E: Decodable, T: Reader> QReadable<T> for PropFind<E> {
async fn read(xml: &mut PeekRead<T>) -> Result<PropFind<E>, ParsingError> {
// Find propfind // Find propfind
xml.tag_start(DAV_URN, "propfind").await?; xml.tag_start(DAV_URN, "propfind").await?;
// Find any tag // Find any tag
let propfind: PropFind<E> = loop { let propfind: PropFind<E> = loop {
match xml.peek() { match xml.peek() {
Event::Start(_) if xml.is_tag(DAV_URN, "allprop") => { Event::Start(_) if xml.is_tag(DAV_URN, "allprop") => {
xml.tag_start(DAV_URN, "allprop").await?; xml.tag_start(DAV_URN, "allprop").await?;
let r = PropFind::AllProp(Some(Include::read(xml).await?)); let r = PropFind::AllProp(Include::qread(xml).await?);
xml.tag_stop(DAV_URN, "allprop").await?; xml.tag_stop(DAV_URN, "allprop").await?;
break r break r
}, },
Event::Start(_) if xml.is_tag(DAV_URN, "prop") => { Event::Start(_) if xml.is_tag(DAV_URN, "prop") => {
xml.tag_start(DAV_URN, "prop").await?; let propname = PropName::qread(xml).await?.ok_or(ParsingError::MissingChild)?;
let r = PropFind::Prop(PropName::read(xml).await?); break PropFind::Prop(propname);
xml.tag_stop(DAV_URN, "prop").await?;
break r
}, },
Event::Empty(_) if xml.is_tag(DAV_URN, "allprop") => { Event::Empty(_) if xml.is_tag(DAV_URN, "allprop") => {
xml.next().await?; xml.next().await?;
@ -153,49 +43,52 @@ impl<E: Decodable, T: Reader> QReadable<T> for PropFind<E> {
// Close tag // Close tag
xml.tag_stop(DAV_URN, "propfind").await?; xml.tag_stop(DAV_URN, "propfind").await?;
Ok(propfind) Ok(Some(propfind))
} }
} }
impl<E: Decodable, T: Reader> QReadable<T> for Include<E> { impl<E: Extension> QRead<Include<E>> for Include<E> {
async fn read(xml: &mut PeekRead<T>) -> Result<Include<E>, ParsingError> { async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<Self>, ParsingError> {
xml.tag_start(DAV_URN, "include").await?; xml.tag_start(DAV_URN, "include").await?;
let mut acc: Vec<PropertyRequest<E>> = Vec::new(); let mut acc: Vec<PropertyRequest<E>> = Vec::new();
loop { loop {
match xml.peek() { match xml.peek() {
Event::Start(_) | Event::Empty(_) => acc.push(PropertyRequest::read(xml).await?), Event::Start(_) | Event::Empty(_) => {
PropertyRequest::qread(xml).await?.map(|v| acc.push(v));
},
Event::End(_) if xml.is_tag(DAV_URN, "include") => break, Event::End(_) if xml.is_tag(DAV_URN, "include") => break,
_ => { xml.skip().await?; }, _ => { xml.skip().await?; },
} }
} }
xml.tag_stop(DAV_URN, "include").await?; xml.tag_stop(DAV_URN, "include").await?;
Ok(Include(acc)) Ok(Some(Include(acc)))
} }
} }
impl<E: Decodable, T: Reader> QReadable<T> for PropName<E> { impl<E: Extension> QRead<PropName<E>> for PropName<E> {
async fn read(xml: &mut PeekRead<T>) -> Result<PropName<E>, ParsingError> { async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<Self>, ParsingError> {
xml.tag_start(DAV_URN, "prop").await?; xml.tag_start(DAV_URN, "prop").await?;
let mut acc: Vec<PropertyRequest<E>> = Vec::new(); let mut acc: Vec<PropertyRequest<E>> = Vec::new();
loop { loop {
match xml.peek() { match xml.peek() {
Event::Start(_) | Event::Empty(_) => acc.push(PropertyRequest::read(xml).await?), Event::Start(_) | Event::Empty(_) => {
PropertyRequest::qread(xml).await?.map(|v| acc.push(v));
},
Event::End(_) if xml.is_tag(DAV_URN, "prop") => break, Event::End(_) if xml.is_tag(DAV_URN, "prop") => break,
_ => { xml.skip().await?; }, _ => { xml.skip().await?; },
} }
} }
xml.tag_stop(DAV_URN, "prop").await?; xml.tag_stop(DAV_URN, "prop").await?;
Ok(PropName(acc)) Ok(Some(PropName(acc)))
} }
} }
impl<E: Decodable, T: Reader> QReadable<T> for PropertyRequest<E> { impl<E: Extension> QRead<PropertyRequest<E>> for PropertyRequest<E> {
async fn read(xml: &mut PeekRead<T>) -> Result<PropertyRequest<E>, ParsingError> { async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<Self>, ParsingError> {
loop { loop {
let (need_close, bs) = match xml.peek() { let bs = match xml.peek() {
Event::Start(b) => (true, b), Event::Start(b) | Event::Empty(b) => b,
Event::Empty(b) => (false, b),
_ => { _ => {
xml.skip().await?; xml.skip().await?;
continue continue
@ -212,6 +105,7 @@ impl<E: Decodable, T: Reader> QReadable<T> for PropertyRequest<E> {
b"displayname" => Some(PropertyRequest::DisplayName), b"displayname" => Some(PropertyRequest::DisplayName),
b"getcontentlanguage" => Some(PropertyRequest::GetContentLanguage), b"getcontentlanguage" => Some(PropertyRequest::GetContentLanguage),
b"getcontentlength" => Some(PropertyRequest::GetContentLength), b"getcontentlength" => Some(PropertyRequest::GetContentLength),
b"getcontenttype" => Some(PropertyRequest::GetContentType),
b"getetag" => Some(PropertyRequest::GetEtag), b"getetag" => Some(PropertyRequest::GetEtag),
b"getlastmodified" => Some(PropertyRequest::GetLastModified), b"getlastmodified" => Some(PropertyRequest::GetLastModified),
b"lockdiscovery" => Some(PropertyRequest::LockDiscovery), b"lockdiscovery" => Some(PropertyRequest::LockDiscovery),
@ -223,18 +117,13 @@ impl<E: Decodable, T: Reader> QReadable<T> for PropertyRequest<E> {
// Option 2: an extension property // Option 2: an extension property
if maybe_res.is_none() { if maybe_res.is_none() {
maybe_res = E::decode_propreq(xml).await?.map(PropertyRequest::Extension); maybe_res = E::PropertyRequest::qread(xml).await?.map(PropertyRequest::Extension);
} }
// In any cases, we must close the opened tag // Close the current tag
if need_close { xml.skip().await?;
xml.skip().await?;
}
// Return if something is found - otherwise loop return Ok(maybe_res)
if let Some(res) = maybe_res {
return Ok(res)
}
} }
} }
} }
@ -242,6 +131,7 @@ impl<E: Decodable, T: Reader> QReadable<T> for PropertyRequest<E> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::dav::realization::Core;
#[tokio::test] #[tokio::test]
async fn basic_propfind_propname() { async fn basic_propfind_propname() {
@ -253,11 +143,12 @@ mod tests {
</D:propfind> </D:propfind>
"#; "#;
let mut rdr = PeekRead::new(NsReader::from_reader(src.as_bytes())).await.unwrap(); let mut rdr = Reader::new(NsReader::from_reader(src.as_bytes())).await.unwrap();
let got = PropFind::<NoExtension>::read(&mut rdr).await.unwrap(); let got = PropFind::<Core>::qread(&mut rdr).await.unwrap().unwrap();
assert!(matches!(got, PropFind::PropName));
assert_eq!(got, PropFind::<Core>::PropName);
} }
/*
#[tokio::test] #[tokio::test]
async fn basic_propfind_prop() { async fn basic_propfind_prop() {
let src = r#"<?xml version="1.0" encoding="utf-8" ?> let src = r#"<?xml version="1.0" encoding="utf-8" ?>
@ -265,19 +156,20 @@ mod tests {
<garbage><old/></garbage> <garbage><old/></garbage>
<D:propfind xmlns:D="DAV:"> <D:propfind xmlns:D="DAV:">
<D:prop> <D:prop>
<displayname/> <D:displayname/>
<getcontentlength/> <D:getcontentlength/>
<getcontenttype/> <D:getcontenttype/>
<getetag/> <D:getetag/>
<getlastmodified/> <D:getlastmodified/>
<resourcetype/> <D:resourcetype/>
<supportedlock/> <D:supportedlock/>
</D:prop> </D:prop>
</D:propfind> </D:propfind>
"#; "#;
let mut rdr = PeekRead::new(NsReader::from_reader(src.as_bytes())).await.unwrap(); let mut rdr = Reader::new(NsReader::from_reader(src.as_bytes())).await.unwrap();
let got = PropFind::<NoExtension>::read(&mut rdr).await.unwrap(); let got = PropFind::<Core>::qread(&mut rdr).await.unwrap().unwrap();
assert_eq!(got, PropFind::Prop(PropName(vec![ assert_eq!(got, PropFind::Prop(PropName(vec![
PropertyRequest::DisplayName, PropertyRequest::DisplayName,
PropertyRequest::GetContentLength, PropertyRequest::GetContentLength,
@ -288,6 +180,4 @@ mod tests {
PropertyRequest::SupportedLock, PropertyRequest::SupportedLock,
]))); ])));
} }
*/
} }
*/

View file

@ -2,6 +2,7 @@ use quick_xml::events::attributes::AttrError;
#[derive(Debug)] #[derive(Debug)]
pub enum ParsingError { pub enum ParsingError {
MissingChild,
NamespacePrefixAlreadyUsed, NamespacePrefixAlreadyUsed,
WrongToken, WrongToken,
TagNotFound, TagNotFound,

View file

@ -6,8 +6,8 @@ use super::error;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Disabled(()); pub struct Disabled(());
impl xml::QRead<Disabled> for Disabled { impl xml::QRead<Disabled> for Disabled {
async fn qread(&self, xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> { async fn qread(xml: &mut xml::Reader<impl xml::IRead>) -> Result<Option<Self>, error::ParsingError> {
unreachable!(); Ok(None)
} }
} }
impl xml::QWrite for Disabled { impl xml::QWrite for Disabled {
@ -20,6 +20,7 @@ impl xml::QWrite for Disabled {
/// ///
/// Any extension is kooh is disabled through an object we can't build /// Any extension is kooh is disabled through an object we can't build
/// due to a private inner element. /// due to a private inner element.
#[derive(Debug, PartialEq)]
pub struct Core {} pub struct Core {}
impl dav::Extension for Core { impl dav::Extension for Core {
type Error = Disabled; type Error = Disabled;
@ -29,6 +30,7 @@ impl dav::Extension for Core {
} }
// WebDAV with the base Calendar implementation (RFC4791) // WebDAV with the base Calendar implementation (RFC4791)
#[derive(Debug, PartialEq)]
pub struct Calendar {} pub struct Calendar {}
impl dav::Extension for Calendar impl dav::Extension for Calendar
{ {

View file

@ -5,6 +5,11 @@ use quick_xml::reader::NsReader;
use super::error::ParsingError; use super::error::ParsingError;
// Constants
pub const DAV_URN: &[u8] = b"DAV:";
pub const CAL_URN: &[u8] = b"urn:ietf:params:xml:ns:caldav";
pub const CARD_URN: &[u8] = b"urn:ietf:params:xml:ns:carddav";
// Async traits // Async traits
pub trait IWrite = AsyncWrite + Unpin; pub trait IWrite = AsyncWrite + Unpin;
pub trait IRead = AsyncBufRead + Unpin + 'static; pub trait IRead = AsyncBufRead + Unpin + 'static;
@ -14,7 +19,7 @@ pub trait QWrite {
async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), quick_xml::Error>; async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), quick_xml::Error>;
} }
pub trait QRead<T> { pub trait QRead<T> {
async fn qread(&self, xml: &mut Reader<impl IRead>) -> Result<Option<T>, ParsingError>; async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<T>, ParsingError>;
} }
/// Transform a Rust object into an XML stream of characters /// Transform a Rust object into an XML stream of characters
@ -42,24 +47,24 @@ impl<T: IWrite> Writer<T> {
/// Transform an XML stream of characters into a Rust object /// Transform an XML stream of characters into a Rust object
pub struct Reader<T: IRead> { pub struct Reader<T: IRead> {
pub rdr: NsReader<T>,
evt: Event<'static>, evt: Event<'static>,
rdr: NsReader<T>,
buf: Vec<u8>, buf: Vec<u8>,
} }
impl<T: IRead> Reader<T> { impl<T: IRead> Reader<T> {
async fn new(mut rdr: NsReader<T>) -> Result<Self, ParsingError> { pub async fn new(mut rdr: NsReader<T>) -> Result<Self, ParsingError> {
let mut buf: Vec<u8> = vec![]; let mut buf: Vec<u8> = vec![];
let evt = rdr.read_event_into_async(&mut buf).await?.into_owned(); let evt = rdr.read_event_into_async(&mut buf).await?.into_owned();
buf.clear(); buf.clear();
Ok(Self { evt, rdr, buf }) Ok(Self { evt, rdr, buf })
} }
fn peek(&self) -> &Event<'static> { pub fn peek(&self) -> &Event<'static> {
&self.evt &self.evt
} }
/// skip tag. Can't skip end, can't skip eof. /// skip tag. Can't skip end, can't skip eof.
async fn skip(&mut self) -> Result<Event<'static>, ParsingError> { pub async fn skip(&mut self) -> Result<Event<'static>, ParsingError> {
match &self.evt { match &self.evt {
Event::Start(b) => { Event::Start(b) => {
let _span = self.rdr.read_to_end_into_async(b.to_end().name(), &mut self.buf).await?; let _span = self.rdr.read_to_end_into_async(b.to_end().name(), &mut self.buf).await?;
@ -72,7 +77,7 @@ impl<T: IRead> Reader<T> {
} }
/// read one more tag /// read one more tag
async fn next(&mut self) -> Result<Event<'static>, ParsingError> { pub async fn next(&mut self) -> Result<Event<'static>, ParsingError> {
let evt = self.rdr.read_event_into_async(&mut self.buf).await?.into_owned(); let evt = self.rdr.read_event_into_async(&mut self.buf).await?.into_owned();
self.buf.clear(); self.buf.clear();
let old_evt = std::mem::replace(&mut self.evt, evt); let old_evt = std::mem::replace(&mut self.evt, evt);
@ -81,7 +86,7 @@ impl<T: IRead> Reader<T> {
/// check if this is the desired tag /// check if this is the desired tag
fn is_tag(&self, ns: &[u8], key: &str) -> bool { pub fn is_tag(&self, ns: &[u8], key: &str) -> bool {
let qname = match self.peek() { let qname = match self.peek() {
Event::Start(bs) | Event::Empty(bs) => bs.name(), Event::Start(bs) | Event::Empty(bs) => bs.name(),
Event::End(be) => be.name(), Event::End(be) => be.name(),
@ -101,7 +106,7 @@ impl<T: IRead> Reader<T> {
} }
/// find start tag /// find start tag
async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> { pub async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
loop { loop {
match self.peek() { match self.peek() {
Event::Start(b) if self.is_tag(ns, key) => break, Event::Start(b) if self.is_tag(ns, key) => break,
@ -112,7 +117,7 @@ impl<T: IRead> Reader<T> {
} }
// find stop tag // find stop tag
async fn tag_stop(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> { pub async fn tag_stop(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
loop { loop {
match self.peek() { match self.peek() {
Event::End(b) if self.is_tag(ns, key) => break, Event::End(b) if self.is_tag(ns, key) => break,