1use crate::parse::keyword::Keyword;
8use crate::types::misc::FromBytes;
9use crate::util::PeekableIterator;
10use crate::{Error, NetdocErrorKind as EK, Pos, Result};
11use base64ct::{Base64, Encoding};
12use itertools::Itertools;
13use std::cell::{Ref, RefCell};
14use std::iter::Peekable;
15use std::str::FromStr;
16use tor_error::internal;
17
18pub(crate) mod object {
20 pub(crate) const BEGIN_STR: &str = "-----BEGIN ";
22 pub(crate) const END_STR: &str = "-----END ";
24 pub(crate) const TAG_END: &str = "-----";
26 #[cfg(feature = "hs-service")]
28 pub(crate) const BASE64_PEM_MAX_LINE: usize = 64;
29}
30
31pub(crate) fn is_sp(c: char) -> bool {
34 c == ' ' || c == '\t'
35}
36fn b64check(s: &str) -> Result<()> {
41 for b in s.bytes() {
42 match b {
43 b'=' => (),
44 b'a'..=b'z' => (),
45 b'A'..=b'Z' => (),
46 b'0'..=b'9' => (),
47 b'/' | b'+' => (),
48 _ => {
49 return Err(EK::BadObjectBase64.at_pos(Pos::at(s)));
50 }
51 };
52 }
53 Ok(())
54}
55
56#[derive(Clone, Copy, Debug)]
64pub(crate) struct Object<'a> {
65 tag: &'a str,
67 data: &'a str,
70 endline: &'a str,
74}
75
76#[derive(Clone, Debug)]
84pub(crate) struct Item<'a, K: Keyword> {
85 kwd: K,
87 kwd_str: &'a str,
90 args: &'a str,
94 split_args: RefCell<Option<Vec<&'a str>>>,
97 object: Option<Object<'a>>,
100}
101
102#[derive(Debug)]
106struct NetDocReaderBase<'a, K: Keyword> {
107 s: &'a str,
109 off: usize,
111 _k: std::marker::PhantomData<K>,
113}
114
115impl<'a, K: Keyword> NetDocReaderBase<'a, K> {
116 fn new(s: &'a str) -> Result<Self> {
118 Ok(NetDocReaderBase {
119 s: validate_utf_8_rules(s)?,
120 off: 0,
121 _k: std::marker::PhantomData,
122 })
123 }
124 fn pos(&self, pos: usize) -> Pos {
126 Pos::from_offset(self.s, pos)
127 }
128 fn advance(&mut self, n: usize) -> Result<()> {
133 if n > self.remaining() {
134 return Err(
135 Error::from(internal!("tried to advance past end of document"))
136 .at_pos(Pos::from_offset(self.s, self.off)),
137 );
138 }
139 self.off += n;
140 Ok(())
141 }
142 fn remaining(&self) -> usize {
144 self.s.len() - self.off
145 }
146
147 fn starts_with(&self, s: &str) -> bool {
149 self.s[self.off..].starts_with(s)
150 }
151 fn line(&mut self) -> Result<&'a str> {
154 let remainder = &self.s[self.off..];
155 if let Some(nl_pos) = remainder.find('\n') {
156 self.advance(nl_pos + 1)?;
157 let line = &remainder[..nl_pos];
158
159 Ok(line)
162 } else {
163 self.advance(remainder.len())?; Err(EK::TruncatedLine.at_pos(self.pos(self.s.len())))
165 }
166 }
167
168 fn kwdline(&mut self) -> Result<(&'a str, &'a str)> {
172 let pos = self.off;
173 let line = self.line()?;
174 if line.is_empty() {
175 return Err(EK::EmptyLine.at_pos(self.pos(pos)));
176 }
177 let (line, anno_ok) = if let Some(rem) = line.strip_prefix("opt ") {
178 (rem, false)
179 } else {
180 (line, true)
181 };
182 let mut parts_iter = line.splitn(2, [' ', '\t']);
183 let kwd = match parts_iter.next() {
184 Some(k) => k,
185 None => return Err(EK::MissingKeyword.at_pos(self.pos(pos))),
188 };
189 if !keyword_ok(kwd, anno_ok) {
190 return Err(EK::BadKeyword.at_pos(self.pos(pos)));
191 }
192 let args = match parts_iter.next() {
195 Some(a) => a,
196 None => &kwd[kwd.len()..],
198 };
199 Ok((kwd, args))
200 }
201
202 fn object(&mut self) -> Result<Option<Object<'a>>> {
208 use object::*;
209
210 let pos = self.off;
211 if !self.starts_with(BEGIN_STR) {
212 return Ok(None);
213 }
214 let line = self.line()?;
215 if !line.ends_with(TAG_END) {
216 return Err(EK::BadObjectBeginTag.at_pos(self.pos(pos)));
217 }
218 let tag = &line[BEGIN_STR.len()..(line.len() - TAG_END.len())];
219 if !tag_keywords_ok(tag) {
220 return Err(EK::BadObjectBeginTag.at_pos(self.pos(pos)));
221 }
222 let datapos = self.off;
223 let (endlinepos, endline) = loop {
224 let p = self.off;
225 let line = self.line()?;
226 if line.starts_with(END_STR) {
227 break (p, line);
228 }
229 b64check(line).map_err(|e| e.within(self.s))?;
234 };
235 let data = &self.s[datapos..endlinepos];
236 if !endline.ends_with(TAG_END) {
237 return Err(EK::BadObjectEndTag.at_pos(self.pos(endlinepos)));
238 }
239 let endtag = &endline[END_STR.len()..(endline.len() - TAG_END.len())];
240 if endtag != tag {
241 return Err(EK::BadObjectMismatchedTag.at_pos(self.pos(endlinepos)));
242 }
243 Ok(Some(Object { tag, data, endline }))
244 }
245
246 fn item(&mut self) -> Result<Option<Item<'a, K>>> {
254 if self.remaining() == 0 {
255 return Ok(None);
256 }
257 let (kwd_str, args) = self.kwdline()?;
258 let object = self.object()?;
259 let split_args = RefCell::new(None);
260 let kwd = K::from_str(kwd_str);
261 Ok(Some(Item {
262 kwd,
263 kwd_str,
264 args,
265 split_args,
266 object,
267 }))
268 }
269}
270
271fn keyword_ok(mut s: &str, anno_ok: bool) -> bool {
275 fn kwd_char_ok(c: char) -> bool {
277 matches!(c,'A'..='Z' | 'a'..='z' |'0'..='9' | '-')
278 }
279
280 if s.is_empty() {
281 return false;
282 }
283 if anno_ok && s.starts_with('@') {
284 s = &s[1..];
285 }
286 if s.starts_with('-') {
287 return false;
288 }
289 s.chars().all(kwd_char_ok)
290}
291
292pub(crate) fn tag_keywords_ok(s: &str) -> bool {
294 s.split(' ').all(|w| keyword_ok(w, false))
295}
296
297impl<'a, K: Keyword> Iterator for NetDocReaderBase<'a, K> {
299 type Item = Result<Item<'a, K>>;
300 fn next(&mut self) -> Option<Self::Item> {
301 self.item().transpose()
302 }
303}
304
305fn base64_decode_multiline(s: &str) -> std::result::Result<Vec<u8>, base64ct::Error> {
308 let mut s = s.to_string();
310 s.retain(|ch| ch != '\n');
311 let v = Base64::decode_vec(&s)?;
312 Ok(v)
313}
314
315impl<'a, K: Keyword> Item<'a, K> {
316 pub(crate) fn kwd(&self) -> K {
318 self.kwd
319 }
320 pub(crate) fn kwd_str(&self) -> &'a str {
322 self.kwd_str
323 }
324 pub(crate) fn has_kwd_in(&self, ks: &[K]) -> bool {
326 ks.contains(&self.kwd)
327 }
328 pub(crate) fn args_as_str(&self) -> &'a str {
330 self.args
331 }
332 fn args_as_vec(&self) -> Ref<'_, Vec<&'a str>> {
334 if self.split_args.borrow().is_none() {
337 self.split_args.replace(Some(self.args().collect()));
338 }
339 Ref::map(self.split_args.borrow(), |opt| match opt {
340 Some(v) => v,
341 None => panic!(),
342 })
343 }
344 pub(crate) fn args(&self) -> impl Iterator<Item = &'a str> {
346 self.args.split(is_sp).filter(|s| !s.is_empty())
347 }
348 pub(crate) fn arg(&self, idx: usize) -> Option<&'a str> {
350 self.args_as_vec().get(idx).copied()
351 }
352 pub(crate) fn required_arg(&self, idx: usize) -> Result<&'a str> {
354 self.arg(idx)
355 .ok_or_else(|| EK::MissingArgument.at_pos(Pos::at(self.args)))
356 }
357 pub(crate) fn parse_optional_arg<V: FromStr>(&self, idx: usize) -> Result<Option<V>>
362 where
363 Error: From<V::Err>,
364 {
365 match self.arg(idx) {
366 None => Ok(None),
367 Some(s) => match s.parse() {
368 Ok(r) => Ok(Some(r)),
369 Err(e) => {
370 let e: Error = e.into();
371 Err(e.or_at_pos(Pos::at(s)))
372 }
373 },
374 }
375 }
376 pub(crate) fn parse_arg<V: FromStr>(&self, idx: usize) -> Result<V>
381 where
382 Error: From<V::Err>,
383 {
384 match self.parse_optional_arg(idx) {
385 Ok(Some(v)) => Ok(v),
386 Ok(None) => Err(EK::MissingArgument.at_pos(self.arg_pos(idx))),
387 Err(e) => Err(e),
388 }
389 }
390 pub(crate) fn n_args(&self) -> usize {
392 self.args().count()
393 }
394 pub(crate) fn has_obj(&self) -> bool {
396 self.object.is_some()
397 }
398 pub(crate) fn obj_tag(&self) -> Option<&'a str> {
400 self.object.map(|o| o.tag)
401 }
402 pub(crate) fn obj_raw(&self) -> Result<Option<(&'a str, Vec<u8>)>> {
406 match self.object {
407 None => Ok(None),
408 Some(obj) => {
409 let decoded = base64_decode_multiline(obj.data)
410 .map_err(|_| EK::BadObjectBase64.at_pos(Pos::at(obj.data)))?;
411 Ok(Some((obj.tag, decoded)))
412 }
413 }
414 }
415 pub(crate) fn obj(&self, want_tag: &str) -> Result<Vec<u8>> {
418 match self.obj_raw()? {
419 None => Err(EK::MissingObject
420 .with_msg(self.kwd.to_str())
421 .at_pos(self.end_pos())),
422 Some((tag, decoded)) => {
423 if tag != want_tag {
424 Err(EK::WrongObject.at_pos(Pos::at(tag)))
425 } else {
426 Ok(decoded)
427 }
428 }
429 }
430 }
431 pub(crate) fn parse_obj<V: FromBytes>(&self, want_tag: &str) -> Result<V> {
434 let bytes = self.obj(want_tag)?;
435 #[allow(clippy::unwrap_used)]
438 let p = Pos::at(self.object.unwrap().data);
439 V::from_vec(bytes, p).map_err(|e| e.at_pos(p))
440 }
441 pub(crate) fn pos(&self) -> Pos {
446 Pos::at(self.kwd_str)
447 }
448 pub(crate) fn offset_in(&self, s: &str) -> Option<usize> {
452 crate::util::str::str_offset(s, self.kwd_str)
453 }
454 pub(crate) fn arg_pos(&self, n: usize) -> Pos {
459 let args = self.args_as_vec();
460 if n < args.len() {
461 Pos::at(args[n])
462 } else {
463 self.last_arg_end_pos()
464 }
465 }
466 fn last_arg_end_pos(&self) -> Pos {
469 let args = self.args_as_vec();
470 if let Some(last_arg) = args.last() {
471 Pos::at_end_of(last_arg)
472 } else {
473 Pos::at_end_of(self.kwd_str)
474 }
475 }
476 pub(crate) fn end_pos(&self) -> Pos {
479 match self.object {
480 Some(o) => Pos::at_end_of(o.endline),
481 None => self.last_arg_end_pos(),
482 }
483 }
484 pub(crate) fn offset_after(&self, s: &str) -> Option<usize> {
487 self.end_pos().offset_within(s).map(|nl_pos| nl_pos + 1)
488 }
489}
490
491pub(crate) struct MaybeItem<'a, 'b, K: Keyword>(Option<&'a Item<'b, K>>);
495
496impl<'a, 'b, K: Keyword> MaybeItem<'a, 'b, K> {
498 fn pos(&self) -> Pos {
500 match self.0 {
501 Some(item) => item.pos(),
502 None => Pos::None,
503 }
504 }
505 pub(crate) fn from_option(opt: Option<&'a Item<'b, K>>) -> Self {
507 MaybeItem(opt)
508 }
509
510 #[cfg(any(test, feature = "routerdesc"))]
514 pub(crate) fn parse_arg<V: FromStr>(&self, idx: usize) -> Result<Option<V>>
515 where
516 Error: From<V::Err>,
517 {
518 match self.0 {
519 Some(item) => match item.parse_arg(idx) {
520 Ok(v) => Ok(Some(v)),
521 Err(e) => Err(e.or_at_pos(self.pos())),
522 },
523 None => Ok(None),
524 }
525 }
526 pub(crate) fn args_as_str(&self) -> Option<&str> {
528 self.0.map(|item| item.args_as_str())
529 }
530 pub(crate) fn parse_args_as_str<V: FromStr>(&self) -> Result<Option<V>>
533 where
534 Error: From<V::Err>,
535 {
536 match self.0 {
537 Some(item) => match item.args_as_str().parse::<V>() {
538 Ok(v) => Ok(Some(v)),
539 Err(e) => {
540 let e: Error = e.into();
541 Err(e.or_at_pos(self.pos()))
542 }
543 },
544 None => Ok(None),
545 }
546 }
547}
548
549pub(crate) trait ItemResult<K: Keyword> {
552 fn is_ok_with_annotation(&self) -> bool;
554 fn is_ok_with_non_annotation(&self) -> bool;
556 fn is_ok_with_kwd(&self, k: K) -> bool {
558 self.is_ok_with_kwd_in(&[k])
559 }
560 fn is_ok_with_kwd_in(&self, ks: &[K]) -> bool;
562 fn is_ok_with_kwd_not_in(&self, ks: &[K]) -> bool;
564 fn is_empty_line(&self) -> bool;
566}
567
568impl<'a, K: Keyword> ItemResult<K> for Result<Item<'a, K>> {
569 fn is_ok_with_annotation(&self) -> bool {
570 match self {
571 Ok(item) => item.kwd().is_annotation(),
572 Err(_) => false,
573 }
574 }
575 fn is_ok_with_non_annotation(&self) -> bool {
576 match self {
577 Ok(item) => !item.kwd().is_annotation(),
578 Err(_) => false,
579 }
580 }
581 fn is_ok_with_kwd_in(&self, ks: &[K]) -> bool {
582 match self {
583 Ok(item) => item.has_kwd_in(ks),
584 Err(_) => false,
585 }
586 }
587 fn is_ok_with_kwd_not_in(&self, ks: &[K]) -> bool {
588 match self {
589 Ok(item) => !item.has_kwd_in(ks),
590 Err(_) => false,
591 }
592 }
593 fn is_empty_line(&self) -> bool {
594 matches!(
595 self,
596 Err(e) if e.netdoc_error_kind() == crate::err::NetdocErrorKind::EmptyLine
597 )
598 }
599}
600
601#[derive(Debug)]
605pub(crate) struct NetDocReader<'a, K: Keyword> {
606 s: &'a str,
610 tokens: Peekable<NetDocReaderBase<'a, K>>,
612}
613
614impl<'a, K: Keyword> NetDocReader<'a, K> {
615 pub(crate) fn new(s: &'a str) -> Result<Self> {
617 Ok(NetDocReader {
618 s,
619 tokens: NetDocReaderBase::new(s)?.peekable(),
620 })
621 }
622 pub(crate) fn str(&self) -> &'a str {
624 self.s
625 }
626 pub(crate) fn pause_at<'f, 'r, F>(
630 &mut self,
631 mut f: F,
632 ) -> itertools::PeekingTakeWhile<'_, Self, impl FnMut(&Result<Item<'a, K>>) -> bool + 'f>
633 where
634 'f: 'r,
635 F: FnMut(&Result<Item<'a, K>>) -> bool + 'f,
636 K: 'f,
637 {
638 self.peeking_take_while(move |i| !f(i))
639 }
640
641 #[allow(clippy::wrong_self_convention)]
645 #[allow(dead_code)] pub(crate) fn is_exhausted(&mut self) -> bool {
647 self.peek().is_none()
648 }
649
650 pub(crate) fn should_be_exhausted(&mut self) -> Result<()> {
652 match self.peek() {
653 None => Ok(()),
654 Some(Ok(t)) => Err(EK::UnexpectedToken
655 .with_msg(t.kwd().to_str())
656 .at_pos(t.pos())),
657 Some(Err(e)) => Err(e.clone()),
658 }
659 }
660
661 #[cfg(feature = "routerdesc")]
666 pub(crate) fn should_be_exhausted_but_for_empty_lines(&mut self) -> Result<()> {
667 use crate::err::NetdocErrorKind as K;
668 while let Some(Err(e)) = self.peek() {
669 if e.netdoc_error_kind() == K::EmptyLine {
670 let _ignore = self.next();
671 } else {
672 break;
673 }
674 }
675 self.should_be_exhausted()
676 }
677
678 pub(crate) fn pos(&mut self) -> Pos {
681 match self.tokens.peek() {
682 Some(Ok(tok)) => tok.pos(),
683 Some(Err(e)) => e.pos(),
684 None => Pos::at_end_of(self.s),
685 }
686 }
687}
688
689impl<'a, K: Keyword> Iterator for NetDocReader<'a, K> {
690 type Item = Result<Item<'a, K>>;
691 fn next(&mut self) -> Option<Self::Item> {
692 self.tokens.next()
693 }
694}
695
696impl<'a, K: Keyword> PeekableIterator for NetDocReader<'a, K> {
697 fn peek(&mut self) -> Option<&Self::Item> {
698 self.tokens.peek()
699 }
700}
701
702impl<'a, K: Keyword> itertools::PeekingNext for NetDocReader<'a, K> {
703 fn peeking_next<F>(&mut self, f: F) -> Option<Self::Item>
704 where
705 F: FnOnce(&Self::Item) -> bool,
706 {
707 if f(self.peek()?) {
708 self.next()
709 } else {
710 None
711 }
712 }
713}
714
715fn validate_utf_8_rules(s: &str) -> Result<&str> {
723 let first_char = s.chars().next();
725 if [Some('\u{feff}'), Some('\u{fffe}')].contains(&first_char) {
726 return Err(EK::BomMarkerFound.at_pos(Pos::at(s)));
727 }
728 if let Some(nul_pos) = memchr::memchr(0, s.as_bytes()) {
730 return Err(EK::NulFound.at_pos(Pos::from_byte(nul_pos)));
731 }
732 Ok(s)
733}
734
735#[cfg(test)]
736mod test {
737 #![allow(clippy::bool_assert_comparison)]
739 #![allow(clippy::clone_on_copy)]
740 #![allow(clippy::dbg_macro)]
741 #![allow(clippy::mixed_attributes_style)]
742 #![allow(clippy::print_stderr)]
743 #![allow(clippy::print_stdout)]
744 #![allow(clippy::single_char_pattern)]
745 #![allow(clippy::unwrap_used)]
746 #![allow(clippy::unchecked_duration_subtraction)]
747 #![allow(clippy::useless_vec)]
748 #![allow(clippy::needless_pass_by_value)]
749 #![allow(clippy::cognitive_complexity)]
751 use super::*;
752 use crate::parse::macros::test::Fruit;
753 use crate::{NetdocErrorKind as EK, Pos, Result};
754
755 #[test]
756 fn read_simple() {
757 use Fruit::*;
758
759 let s = "\
760@tasty very much so
761opt apple 77
762banana 60
763cherry 6
764-----BEGIN CHERRY SYNOPSIS-----
7658J+NkvCfjZLwn42S8J+NkvCfjZLwn42S
766-----END CHERRY SYNOPSIS-----
767plum hello there
768";
769 let mut r: NetDocReader<'_, Fruit> = NetDocReader::new(s).unwrap();
770
771 assert_eq!(r.str(), s);
772 assert!(r.should_be_exhausted().is_err()); let toks: Result<Vec<_>> = r.by_ref().collect();
775 assert!(r.should_be_exhausted().is_ok());
776
777 let toks = toks.unwrap();
778 assert_eq!(toks.len(), 5);
779 assert_eq!(toks[0].kwd(), ANN_TASTY);
780 assert_eq!(toks[0].n_args(), 3);
781 assert_eq!(toks[0].args_as_str(), "very much so");
782 assert_eq!(toks[0].arg(1), Some("much"));
783 {
784 let a: Vec<_> = toks[0].args().collect();
785 assert_eq!(a, vec!["very", "much", "so"]);
786 }
787 assert!(toks[0].parse_arg::<usize>(0).is_err());
788 assert!(toks[0].parse_arg::<usize>(10).is_err());
789 assert!(!toks[0].has_obj());
790 assert_eq!(toks[0].obj_tag(), None);
791
792 assert_eq!(toks[2].pos().within(s), Pos::from_line(3, 1));
793 assert_eq!(toks[2].arg_pos(0).within(s), Pos::from_line(3, 8));
794 assert_eq!(toks[2].last_arg_end_pos().within(s), Pos::from_line(3, 10));
795 assert_eq!(toks[2].end_pos().within(s), Pos::from_line(3, 10));
796
797 assert_eq!(toks[3].kwd(), STONEFRUIT);
798 assert_eq!(toks[3].kwd_str(), "cherry"); assert_eq!(toks[3].n_args(), 1);
800 assert_eq!(toks[3].required_arg(0), Ok("6"));
801 assert_eq!(toks[3].parse_arg::<usize>(0), Ok(6));
802 assert_eq!(toks[3].parse_optional_arg::<usize>(0), Ok(Some(6)));
803 assert_eq!(toks[3].parse_optional_arg::<usize>(3), Ok(None));
804 assert!(toks[3].has_obj());
805 assert_eq!(toks[3].obj_tag(), Some("CHERRY SYNOPSIS"));
806 assert_eq!(
807 &toks[3].obj("CHERRY SYNOPSIS").unwrap()[..],
808 "🍒🍒🍒🍒🍒🍒".as_bytes()
809 );
810 assert!(toks[3].obj("PLUOT SYNOPSIS").is_err());
811 assert_eq!(toks[3].end_pos().within(s), Pos::from_line(7, 30));
813 }
814
815 #[test]
816 fn test_badtoks() {
817 use Fruit::*;
818
819 let s = "\
820-foobar 9090
821apple 3.14159
822$hello
823unrecognized 127.0.0.1 foo
824plum
825-----BEGIN WHATEVER-----
8268J+NkvCfjZLwn42S8J+NkvCfjZLwn42S
827-----END SOMETHING ELSE-----
828orange
829orange
830-----BEGIN WHATEVER-----
831not! base64!
832-----END WHATEVER-----
833guava paste
834opt @annotation
835orange
836-----BEGIN LOBSTER
8378J+NkvCfjZLwn42S8J+NkvCfjZLwn42S
838-----END SOMETHING ELSE-----
839orange
840-----BEGIN !!!!!!-----
8418J+NkvCfjZLwn42S8J+NkvCfjZLwn42S
842-----END !!!!!!-----
843cherry
844-----BEGIN CHERRY SYNOPSIS-----
8458J+NkvCfjZLwn42S8J+NkvCfjZLwn42S
846-----END CHERRY SYNOPSIS
847
848truncated line";
849
850 let r: NetDocReader<'_, Fruit> = NetDocReader::new(s).unwrap();
851 let toks: Vec<_> = r.collect();
852
853 assert!(toks[0].is_err());
854 assert_eq!(
855 toks[0].as_ref().err().unwrap(),
856 &EK::BadKeyword.at_pos(Pos::from_line(1, 1))
857 );
858
859 assert!(toks[1].is_ok());
860 assert!(toks[1].is_ok_with_non_annotation());
861 assert!(!toks[1].is_ok_with_annotation());
862 assert!(toks[1].is_ok_with_kwd_in(&[APPLE, ORANGE]));
863 assert!(toks[1].is_ok_with_kwd_not_in(&[ORANGE, UNRECOGNIZED]));
864 let t = toks[1].as_ref().unwrap();
865 assert_eq!(t.kwd(), APPLE);
866 assert_eq!(t.arg(0), Some("3.14159"));
867
868 assert!(toks[2].is_err());
869 assert!(!toks[2].is_ok_with_non_annotation());
870 assert!(!toks[2].is_ok_with_annotation());
871 assert!(!toks[2].is_ok_with_kwd_in(&[APPLE, ORANGE]));
872 assert!(!toks[2].is_ok_with_kwd_not_in(&[ORANGE, UNRECOGNIZED]));
873 assert_eq!(
874 toks[2].as_ref().err().unwrap(),
875 &EK::BadKeyword.at_pos(Pos::from_line(3, 1))
876 );
877
878 assert!(toks[3].is_ok());
879 let t = toks[3].as_ref().unwrap();
880 assert_eq!(t.kwd(), UNRECOGNIZED);
881 assert_eq!(t.arg(1), Some("foo"));
882
883 assert!(toks[4].is_err());
884 assert_eq!(
885 toks[4].as_ref().err().unwrap(),
886 &EK::BadObjectMismatchedTag.at_pos(Pos::from_line(8, 1))
887 );
888
889 assert!(toks[5].is_ok());
890 let t = toks[5].as_ref().unwrap();
891 assert_eq!(t.kwd(), ORANGE);
892 assert_eq!(t.args_as_str(), "");
893
894 assert!(toks[6].is_err());
897 assert_eq!(
898 toks[6].as_ref().err().unwrap(),
899 &EK::BadObjectBase64.at_pos(Pos::from_line(12, 1))
900 );
901
902 assert!(toks[7].is_err());
903 assert_eq!(
904 toks[7].as_ref().err().unwrap(),
905 &EK::BadKeyword.at_pos(Pos::from_line(13, 1))
906 );
907
908 assert!(toks[8].is_ok());
909 let t = toks[8].as_ref().unwrap();
910 assert_eq!(t.kwd(), GUAVA);
911
912 assert!(toks[9].is_err());
914 assert_eq!(
915 toks[9].as_ref().err().unwrap(),
916 &EK::BadKeyword.at_pos(Pos::from_line(15, 1))
917 );
918
919 assert!(toks[10].is_err());
921 assert_eq!(
922 toks[10].as_ref().err().unwrap(),
923 &EK::BadObjectBeginTag.at_pos(Pos::from_line(17, 1))
924 );
925 assert!(toks[11].is_err());
926 assert_eq!(
927 toks[11].as_ref().err().unwrap(),
928 &EK::BadKeyword.at_pos(Pos::from_line(18, 1))
929 );
930 assert!(toks[12].is_err());
931 assert_eq!(
932 toks[12].as_ref().err().unwrap(),
933 &EK::BadKeyword.at_pos(Pos::from_line(19, 1))
934 );
935
936 assert!(toks[13].is_err());
938 assert_eq!(
939 toks[13].as_ref().err().unwrap(),
940 &EK::BadObjectBeginTag.at_pos(Pos::from_line(21, 1))
941 );
942 assert!(toks[14].is_err());
943 assert_eq!(
944 toks[14].as_ref().err().unwrap(),
945 &EK::BadKeyword.at_pos(Pos::from_line(22, 1))
946 );
947 assert!(toks[15].is_err());
948 assert_eq!(
949 toks[15].as_ref().err().unwrap(),
950 &EK::BadKeyword.at_pos(Pos::from_line(23, 1))
951 );
952
953 assert!(toks[16].is_err());
955 assert_eq!(
956 toks[16].as_ref().err().unwrap(),
957 &EK::BadObjectEndTag.at_pos(Pos::from_line(27, 1))
958 );
959
960 assert!(toks[17].is_err());
961 assert_eq!(
962 toks[17].as_ref().err().unwrap(),
963 &EK::EmptyLine.at_pos(Pos::from_line(28, 1))
964 );
965
966 assert!(toks[18].is_err());
967 assert_eq!(
968 toks[18].as_ref().err().unwrap(),
969 &EK::TruncatedLine.at_pos(Pos::from_line(29, 15))
970 );
971 }
972
973 #[test]
974 fn test_validate_strings() {
975 use validate_utf_8_rules as v;
976 assert_eq!(v(""), Ok(""));
977 assert_eq!(v("hello world"), Ok("hello world"));
978 for s in ["\u{feff}", "\u{feff}hello world", "\u{fffe}hello world"] {
982 let e = v(s).unwrap_err();
983 assert_eq!(e.netdoc_error_kind(), EK::BomMarkerFound);
984 assert_eq!(e.pos().offset_within(s), Some(0));
985 }
986
987 for s in [
988 "\0hello world",
989 "\0",
990 "\0\0\0",
991 "hello\0world",
992 "hello world\0",
993 ] {
994 let e = v(s).unwrap_err();
995 assert_eq!(e.netdoc_error_kind(), EK::NulFound);
996 let nul_pos = e.pos().offset_within(s).unwrap();
997 assert_eq!(s.as_bytes()[nul_pos], 0);
998 }
999 }
1000}