0000: 75 73 65 20 73 6d 74 70 32 74 67 3a 3a 75 74 69 use smtp2tg::uti
0010: 6c 73 3a 3a 7b 0a 09 76 61 6c 69 64 61 74 65 2c ls::{..validate,
0020: 0a 09 52 45 5f 43 4c 4f 53 49 4e 47 2c 0a 09 52 ..RE_CLOSING,..R
0030: 45 5f 44 4f 4d 41 49 4e 2c 0a 7d 3b 0a 0a 75 73 E_DOMAIN,.};..us
0040: 65 20 73 74 64 3a 3a 7b 0a 09 62 6f 72 72 6f 77 e std::{..borrow
0050: 3a 3a 43 6f 77 2c 0a 09 6d 65 6d 3a 3a 64 69 73 ::Cow,..mem::dis
0060: 63 72 69 6d 69 6e 61 6e 74 2c 0a 7d 3b 0a 0a 75 criminant,.};..u
0070: 73 65 20 73 74 61 63 6b 65 64 5f 65 72 72 6f 72 se stacked_error
0080: 73 3a 3a 7b 0a 09 52 65 73 75 6c 74 2c 0a 09 65 s::{..Result,..e
0090: 6e 73 75 72 65 5f 65 71 2c 0a 7d 3b 0a 0a 23 5b nsure_eq,.};..#[
00a0: 74 65 73 74 5d 0a 66 6e 20 74 65 73 74 5f 76 61 test].fn test_va
00b0: 6c 69 64 61 74 65 5f 65 73 63 61 70 69 6e 67 5f lidate_escaping_
00c0: 62 65 68 61 76 69 6f 72 20 28 29 20 2d 3e 20 52 behavior () -> R
00d0: 65 73 75 6c 74 3c 28 29 3e 20 7b 0a 09 6c 65 74 esult<()> {..let
00e0: 20 63 61 73 65 73 3a 20 26 5b 28 26 73 74 72 2c cases: &[(&str,
00f0: 20 43 6f 77 3c 73 74 72 3e 29 5d 20 3d 20 26 5b Cow<str>)] = &[
0100: 0a 09 09 2f 2f 20 60 76 61 6c 69 64 61 74 65 60 ...// `validate`
0110: 20 65 73 63 61 70 65 73 20 48 54 4d 4c 20 73 70 escapes HTML sp
0120: 65 63 69 61 6c 20 63 68 61 72 61 63 74 65 72 73 ecial characters
0130: 2e 0a 09 09 28 22 3c 70 3e 53 6f 6d 65 20 3c 62 ....("<p>Some <b
0140: 3e 76 61 6c 69 64 3c 2f 62 3e 20 48 54 4d 4c 3c >valid</b> HTML<
0150: 2f 70 3e 22 2c 20 43 6f 77 3a 3a 4f 77 6e 65 64 /p>", Cow::Owned
0160: 28 22 26 6c 74 3b 70 26 67 74 3b 53 6f 6d 65 20 ("<p>Some
0170: 26 6c 74 3b 62 26 67 74 3b 76 61 6c 69 64 26 6c <b>valid&l
0180: 74 3b 2f 62 26 67 74 3b 20 48 54 4d 4c 26 6c 74 t;/b> HTML<
0190: 3b 2f 70 26 67 74 3b 22 2e 69 6e 74 6f 28 29 29 ;/p>".into())
01a0: 29 2c 0a 09 09 2f 2f 20 45 6d 70 74 79 20 69 6e ),...// Empty in
01b0: 70 75 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 put is returned
01c0: 75 6e 63 68 61 6e 67 65 64 2e 0a 09 09 28 22 22 unchanged....(""
01d0: 2c 20 43 6f 77 3a 3a 42 6f 72 72 6f 77 65 64 28 , Cow::Borrowed(
01e0: 22 22 29 29 2c 0a 09 09 2f 2f 20 57 68 69 74 65 "")),...// White
01f0: 73 70 61 63 65 2d 6f 6e 6c 79 20 69 6e 70 75 74 space-only input
0200: 20 6e 65 65 64 73 20 6e 6f 20 65 73 63 61 70 69 needs no escapi
0210: 6e 67 2e 0a 09 09 28 22 20 20 20 5c 74 5c 6e 22 ng....(" \t\n"
0220: 2c 20 43 6f 77 3a 3a 42 6f 72 72 6f 77 65 64 28 , Cow::Borrowed(
0230: 22 20 20 20 5c 74 5c 6e 22 29 29 2c 0a 09 09 2f " \t\n")),.../
0240: 2f 20 60 76 61 6c 69 64 61 74 65 60 20 72 65 74 / `validate` ret
0250: 75 72 6e 73 20 60 43 6f 77 3c 27 61 2c 20 73 74 urns `Cow<'a, st
0260: 72 3e 60 20 62 6f 72 72 6f 77 65 64 20 66 72 6f r>` borrowed fro
0270: 6d 20 69 74 73 20 69 6e 70 75 74 20 6c 69 66 65 m its input life
0280: 74 69 6d 65 20 60 27 61 60 2e 0a 09 09 2f 2f 20 time `'a`....//
0290: 54 68 65 73 65 20 74 77 6f 20 63 61 73 65 73 20 These two cases
02a0: 65 78 65 72 63 69 73 65 20 62 6f 74 68 20 62 72 exercise both br
02b0: 61 6e 63 68 65 73 20 6f 66 20 74 68 61 74 20 60 anches of that `
02c0: 43 6f 77 60 20 74 6f 20 6d 61 6b 65 20 73 75 72 Cow` to make sur
02d0: 65 20 74 68 65 0a 09 09 2f 2f 20 65 78 70 6c 69 e the...// expli
02e0: 63 69 74 20 6c 69 66 65 74 69 6d 65 20 69 6e 74 cit lifetime int
02f0: 72 6f 64 75 63 65 64 20 6f 6e 20 60 76 61 6c 69 roduced on `vali
0300: 64 61 74 65 60 20 73 74 69 6c 6c 20 6c 65 74 73 date` still lets
0310: 20 63 61 6c 6c 65 72 73 20 6f 62 73 65 72 76 65 callers observe
0320: 20 61 0a 09 09 2f 2f 20 7a 65 72 6f 2d 63 6f 70 a...// zero-cop
0330: 79 20 62 6f 72 72 6f 77 20 77 68 65 6e 20 6e 6f y borrow when no
0340: 20 65 73 63 61 70 69 6e 67 20 69 73 20 72 65 71 escaping is req
0350: 75 69 72 65 64 2e 0a 09 09 28 22 70 6c 61 69 6e uired....("plain
0360: 20 74 65 78 74 20 77 69 74 68 6f 75 74 20 73 70 text without sp
0370: 65 63 69 61 6c 20 68 74 6d 6c 20 63 68 61 72 61 ecial html chara
0380: 63 74 65 72 73 22 2c 20 43 6f 77 3a 3a 42 6f 72 cters", Cow::Bor
0390: 72 6f 77 65 64 28 22 70 6c 61 69 6e 20 74 65 78 rowed("plain tex
03a0: 74 20 77 69 74 68 6f 75 74 20 73 70 65 63 69 61 t without specia
03b0: 6c 20 68 74 6d 6c 20 63 68 61 72 61 63 74 65 72 l html character
03c0: 73 22 29 29 2c 0a 09 09 28 22 35 20 3e 20 33 20 s")),...("5 > 3
03d0: 26 20 32 20 3c 20 34 22 2c 20 43 6f 77 3a 3a 4f & 2 < 4", Cow::O
03e0: 77 6e 65 64 28 22 35 20 26 67 74 3b 20 33 20 26 wned("5 > 3 &
03f0: 61 6d 70 3b 20 32 20 26 6c 74 3b 20 34 22 2e 69 amp; 2 < 4".i
0400: 6e 74 6f 28 29 29 29 2c 0a 09 5d 3b 0a 09 66 6f nto())),..];..fo
0410: 72 20 28 69 6e 70 75 74 2c 20 65 78 70 65 63 74 r (input, expect
0420: 65 64 29 20 69 6e 20 63 61 73 65 73 20 7b 0a 09 ed) in cases {..
0430: 09 6c 65 74 20 72 65 73 75 6c 74 20 3d 20 76 61 .let result = va
0440: 6c 69 64 61 74 65 28 69 6e 70 75 74 29 3f 3b 0a lidate(input)?;.
0450: 09 09 65 6e 73 75 72 65 5f 65 71 21 28 26 72 65 ..ensure_eq!(&re
0460: 73 75 6c 74 2c 20 65 78 70 65 63 74 65 64 2c 20 sult, expected,
0470: 66 6f 72 6d 61 74 21 28 22 75 6e 65 78 70 65 63 format!("unexpec
0480: 74 65 64 20 6f 75 74 70 75 74 20 66 6f 72 20 69 ted output for i
0490: 6e 70 75 74 20 7b 69 6e 70 75 74 3a 3f 7d 22 29 nput {input:?}")
04a0: 29 3b 0a 09 09 65 6e 73 75 72 65 5f 65 71 21 28 );...ensure_eq!(
04b0: 64 69 73 63 72 69 6d 69 6e 61 6e 74 28 26 72 65 discriminant(&re
04c0: 73 75 6c 74 29 2c 20 64 69 73 63 72 69 6d 69 6e sult), discrimin
04d0: 61 6e 74 28 65 78 70 65 63 74 65 64 29 2c 20 66 ant(expected), f
04e0: 6f 72 6d 61 74 21 28 22 77 72 6f 6e 67 20 43 6f ormat!("wrong Co
04f0: 77 20 76 61 72 69 61 6e 74 20 66 6f 72 20 69 6e w variant for in
0500: 70 75 74 20 7b 69 6e 70 75 74 3a 3f 7d 22 29 29 put {input:?}"))
0510: 3b 0a 09 7d 0a 09 4f 6b 28 28 29 29 0a 7d 0a 0a ;..}..Ok(()).}..
0520: 23 5b 74 65 73 74 5d 0a 66 6e 20 74 65 73 74 5f #[test].fn test_
0530: 76 61 6c 69 64 61 74 65 5f 63 6c 6f 73 69 6e 67 validate_closing
0540: 5f 74 61 67 5f 62 65 68 61 76 69 6f 72 20 28 29 _tag_behavior ()
0550: 20 2d 3e 20 52 65 73 75 6c 74 3c 28 29 3e 20 7b -> Result<()> {
0560: 0a 09 6c 65 74 20 63 61 73 65 73 20 3d 20 5b 0a ..let cases = [.
0570: 09 09 28 22 3c 2f 20 20 70 72 65 20 20 3e 22 2c ..("</ pre >",
0580: 20 74 72 75 65 29 2c 0a 09 09 28 22 3c 2f 5c 74 true),...("</\t
0590: 63 6f 64 65 5c 74 3e 22 2c 20 74 72 75 65 29 2c code\t>", true),
05a0: 0a 09 09 28 22 3c 2f 62 3e 22 2c 20 66 61 6c 73 ...("</b>", fals
05b0: 65 29 2c 0a 09 09 28 22 3c 2f 43 6f 64 65 3e 22 e),...("</Code>"
05c0: 2c 20 74 72 75 65 29 2c 0a 09 09 28 22 3c 2f 63 , true),...("</c
05d0: 6f 64 65 3e 22 2c 20 74 72 75 65 29 2c 0a 09 09 ode>", true),...
05e0: 28 22 3c 2f 63 6f 64 65 3e 5c 74 22 2c 20 74 72 ("</code>\t", tr
05f0: 75 65 29 2c 0a 09 09 28 22 3c 2f 63 6f 64 65 3e ue),...("</code>
0600: 5c 74 3e 22 2c 20 74 72 75 65 29 2c 0a 09 09 28 \t>", true),...(
0610: 22 3c 2f 64 69 76 3e 22 2c 20 66 61 6c 73 65 29 "</div>", false)
0620: 2c 20 2f 2f 20 4e 6f 74 20 61 20 70 72 65 2f 63 , // Not a pre/c
0630: 6f 64 65 20 74 61 67 0a 09 09 28 22 3c 2f 50 52 ode tag...("</PR
0640: 45 3e 22 2c 20 74 72 75 65 29 2c 0a 09 09 28 22 E>", true),...("
0650: 3c 2f 70 72 65 3e 22 2c 20 74 72 75 65 29 2c 0a </pre>", true),.
0660: 09 09 28 22 3c 2f 70 72 65 3e 5c 6e 22 2c 20 74 ..("</pre>\n", t
0670: 72 75 65 29 2c 0a 09 09 28 22 3c 70 3e 53 6f 6d rue),...("<p>Som
0680: 65 20 3c 62 3e 76 61 6c 69 64 3c 2f 62 3e 20 48 e <b>valid</b> H
0690: 54 4d 4c 3c 2f 70 3e 3c 2f 63 6f 64 65 3e 3c 61 TML</p></code><a
06a0: 20 68 72 65 66 3d 27 68 74 74 70 3a 2f 2f 73 6f href='http://so
06b0: 6d 65 77 65 72 65 2e 63 6f 6d 27 3e 4c 69 6e 6b mewere.com'>Link
06c0: 20 69 6e 6a 65 63 74 69 6f 6e 21 3c 2f 61 3e 22 injection!</a>"
06d0: 2c 20 74 72 75 65 29 2c 0a 09 09 28 22 3c 70 72 , true),...("<pr
06e0: 65 3e 22 2c 20 66 61 6c 73 65 29 2c 0a 09 5d 3b e>", false),..];
06f0: 0a 09 66 6f 72 20 28 69 6e 70 75 74 2c 20 65 78 ..for (input, ex
0700: 70 65 63 74 65 64 29 20 69 6e 20 63 61 73 65 73 pected) in cases
0710: 20 7b 0a 09 09 65 6e 73 75 72 65 5f 65 71 21 28 {...ensure_eq!(
0720: 52 45 5f 43 4c 4f 53 49 4e 47 2e 69 73 5f 6d 61 RE_CLOSING.is_ma
0730: 74 63 68 28 69 6e 70 75 74 29 2c 20 65 78 70 65 tch(input), expe
0740: 63 74 65 64 2c 20 66 6f 72 6d 61 74 21 28 22 75 cted, format!("u
0750: 6e 65 78 70 65 63 74 65 64 20 6d 61 74 63 68 20 nexpected match
0760: 72 65 73 75 6c 74 20 66 6f 72 20 7b 69 6e 70 75 result for {inpu
0770: 74 3a 3f 7d 22 29 29 3b 0a 09 7d 0a 09 4f 6b 28 t:?}"));..}..Ok(
0780: 28 29 29 0a 7d 0a 0a 23 5b 74 65 73 74 5d 0a 66 ()).}..#[test].f
0790: 6e 20 74 65 73 74 5f 72 65 67 65 78 5f 64 6f 6d n test_regex_dom
07a0: 61 69 6e 5f 62 65 68 61 76 69 6f 72 28 29 20 2d ain_behavior() -
07b0: 3e 20 52 65 73 75 6c 74 3c 28 29 3e 20 7b 0a 09 > Result<()> {..
07c0: 6c 65 74 20 63 61 73 65 73 20 3d 20 5b 0a 09 09 let cases = [...
07d0: 28 22 22 2c 20 66 61 6c 73 65 29 2c 0a 09 09 28 ("", false),...(
07e0: 22 2d 65 78 61 6d 70 6c 65 2e 63 6f 6d 22 2c 20 "-example.com",
07f0: 66 61 6c 73 65 29 2c 0a 09 09 28 22 2e 65 78 61 false),...(".exa
0800: 6d 70 6c 65 2e 63 6f 6d 22 2c 20 66 61 6c 73 65 mple.com", false
0810: 29 2c 0a 09 09 28 22 31 32 33 2e 34 35 36 22 2c ),...("123.456",
0820: 20 74 72 75 65 29 2c 0a 09 09 28 22 45 58 41 4d true),...("EXAM
0830: 50 4c 45 2e 43 4f 4d 22 2c 20 66 61 6c 73 65 29 PLE.COM", false)
0840: 2c 0a 09 09 28 22 61 22 2c 20 74 72 75 65 29 2c ,...("a", true),
0850: 0a 09 09 28 22 61 2e 62 22 2c 20 74 72 75 65 29 ...("a.b", true)
0860: 2c 0a 09 09 28 22 65 78 61 6d 70 6c 65 2d 2e 63 ,...("example-.c
0870: 6f 6d 22 2c 20 66 61 6c 73 65 29 2c 0a 09 09 28 om", false),...(
0880: 22 65 78 61 6d 70 6c 65 2e 2e 63 6f 6d 22 2c 20 "example..com",
0890: 66 61 6c 73 65 29 2c 0a 09 09 28 22 65 78 61 6d false),...("exam
08a0: 70 6c 65 2e 63 6f 6d 22 2c 20 74 72 75 65 29 2c ple.com", true),
08b0: 0a 09 09 28 22 65 78 61 6d 70 6c 65 2e 63 6f 6d ...("example.com
08c0: 2e 22 2c 20 66 61 6c 73 65 29 2c 0a 09 09 28 22 .", false),...("
08d0: 69 6e 76 61 6c 69 64 40 64 6f 6d 61 69 6e 2e 63 invalid@domain.c
08e0: 6f 6d 22 2c 20 66 61 6c 73 65 29 2c 0a 09 09 28 om", false),...(
08f0: 22 6d 79 2d 68 6f 73 74 2e 65 78 61 6d 70 6c 65 "my-host.example
0900: 2e 63 6f 6d 22 2c 20 74 72 75 65 29 2c 0a 09 09 .com", true),...
0910: 28 22 73 75 62 2e 65 78 61 6d 70 6c 65 2e 63 6f ("sub.example.co
0920: 2e 75 6b 22 2c 20 74 72 75 65 29 2c 0a 09 5d 3b .uk", true),..];
0930: 0a 09 66 6f 72 20 28 69 6e 70 75 74 2c 20 65 78 ..for (input, ex
0940: 70 65 63 74 65 64 29 20 69 6e 20 63 61 73 65 73 pected) in cases
0950: 20 7b 0a 09 09 65 6e 73 75 72 65 5f 65 71 21 28 {...ensure_eq!(
0960: 52 45 5f 44 4f 4d 41 49 4e 2e 69 73 5f 6d 61 74 RE_DOMAIN.is_mat
0970: 63 68 28 69 6e 70 75 74 29 2c 20 65 78 70 65 63 ch(input), expec
0980: 74 65 64 2c 20 66 6f 72 6d 61 74 21 28 22 75 6e ted, format!("un
0990: 65 78 70 65 63 74 65 64 20 6d 61 74 63 68 20 72 expected match r
09a0: 65 73 75 6c 74 20 66 6f 72 20 7b 69 6e 70 75 74 esult for {input
09b0: 3a 3f 7d 22 29 29 3b 0a 09 7d 0a 09 4f 6b 28 28 :?}"));..}..Ok((
09c0: 29 29 0a 7d 0a )).}.