oxedyne/fe2o3/fe2o3_mail/tests/mail_identity.rs
4.5 KiB, 19 runs
created by r2494099886:77836, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Which of his identities a message was addressed to, and which provider an account is: the |
| 2 | //! hostile cases A1 to A3, P1 and P2 of MAIL-fe, on bytes and lists alone. |
| 3 | |
| 4 | use oxedyne_fe2o3_core::prelude::*; |
| 5 | use oxedyne_fe2o3_mail::{ |
| 6 | message::addressed, |
| 7 | sync::{ |
| 8 | Caps, |
| 9 | Provider, |
| 10 | }, |
| 11 | }; |
| 12 | |
| 13 | |
| 14 | const HIM: [&str; 3] = ["me@his.example", "alias@his.example", "private@his.example"]; |
| 15 | |
| 16 | fn msg(head: &str) -> Vec<u8> { |
| 17 | fmt!("{}\r\nSubject: s\r\n\r\nbody\r\n", head).into_bytes() |
| 18 | } |
| 19 | |
| 20 | fn to(head: &str) -> Option<String> { |
| 21 | addressed(&msg(head), &HIM) |
| 22 | } |
| 23 | |
| 24 | /// A1: a display name, an encoded word or a comment is never an address. |
| 25 | #[test] |
| 26 | fn test_a_name_is_never_an_address_00() -> Outcome<()> { |
| 27 | for head in [ |
| 28 | "To: \"me@his.example\" <evil@y>", |
| 29 | "To: =?utf-8?Q?me=40his.example?= <evil@y>", |
| 30 | "To: \"=?utf-8?Q?a=2C_me=40his.example?=\" <evil@y>", |
| 31 | "To: evil@y (me@his.example)", |
| 32 | "To: me@his.example.evil, xme@his.example", |
| 33 | ] { |
| 34 | req!(None::<String>, to(head), "{}", head); |
| 35 | } |
| 36 | Ok(()) |
| 37 | } |
| 38 | |
| 39 | /// A2: a group, a folded line, odd case, Cc alone and bare LF all find the alias, spelled as he |
| 40 | /// spells it; a body line and a `+tag` do not. |
| 41 | #[test] |
| 42 | fn test_every_form_of_the_header_finds_the_alias_00() -> Outcome<()> { |
| 43 | let alias = Some("alias@his.example".to_string()); |
| 44 | for head in [ |
| 45 | "To: Team: a@x, ALIAS@His.Example;", |
| 46 | "To: a@x,\r\n\tAlias@his.example", |
| 47 | "tO: b@y\r\nCC: alias@HIS.example", |
| 48 | "To: b@y\r\nCc: Alias <alias@his.example>", |
| 49 | ] { |
| 50 | req!(alias, to(head), "{}", head); |
| 51 | } |
| 52 | req!(alias, addressed(b"To: b@y\nCc: alias@his.example\n\nbody\n", &HIM)); |
| 53 | req!(None::<String>, addressed(b"To: b@y\r\n\r\nTo: alias@his.example\r\n", &HIM)); |
| 54 | req!(None::<String>, to("To: alias+lists@his.example")); |
| 55 | Ok(()) |
| 56 | } |
| 57 | |
| 58 | /// A3: the first of his in the header's order wins; To, Cc, Delivered-To, X-Original-To in turn; |
| 59 | /// of a trace field only the top-most, which his server wrote; no other field. |
| 60 | #[test] |
| 61 | fn test_order_and_forged_delivery_headers_00() -> Outcome<()> { |
| 62 | req!(Some("private@his.example".to_string()), to("To: private@his.example, alias@his.example")); |
| 63 | req!(Some("alias@his.example".to_string()), |
| 64 | to("To: x@y\r\nDelivered-To: me@his.example\r\nCc: alias@his.example")); |
| 65 | req!(Some("me@his.example".to_string()), |
| 66 | to("Delivered-To: me@his.example\r\nTo: x@y\r\nDelivered-To: private@his.example")); |
| 67 | req!(None::<String>, to("Delivered-To: x@y\r\nDelivered-To: private@his.example\r\nTo: x@y")); |
| 68 | req!(Some("alias@his.example".to_string()), |
| 69 | to("To: x@y\r\nX-Original-To: private@his.example\r\nDelivered-To: alias@his.example")); |
| 70 | req!(None::<String>, to("Resent-To: private@his.example\r\nReply-To: private@his.example\r\n\ |
| 71 | X-To: private@his.example\r\nTo: x@y")); |
| 72 | Ok(()) |
| 73 | } |
| 74 | |
| 75 | fn table() -> Vec<(String, Provider)> { |
| 76 | vec![ |
| 77 | ("mail.his.example".to_string(), Provider::Steel), |
| 78 | ("example.com".to_string(), Provider::Office365), |
| 79 | ("imap.example.com".to_string(), Provider::Generic), |
| 80 | ("gmail.example".to_string(), Provider::Gmail), |
| 81 | ("".to_string(), Provider::Office365), |
| 82 | (".".to_string(), Provider::Office365), |
| 83 | ] |
| 84 | } |
| 85 | |
| 86 | /// P1: a host matches a row whole or at a label boundary, the most specific row wins, and an |
| 87 | /// address or an empty row matches nothing. |
| 88 | #[test] |
| 89 | fn test_provider_by_host_table_00() -> Outcome<()> { |
| 90 | let t = table(); |
| 91 | let plain = Caps::default(); |
| 92 | for (host, want) in [ |
| 93 | ("MAIL.His.Example.", Provider::Steel), |
| 94 | ("notexample.com", Provider::Generic), |
| 95 | ("outlook.example.com", Provider::Office365), |
| 96 | ("imap.example.com", Provider::Generic), |
| 97 | ("192.0.2.7", Provider::Generic), |
| 98 | ("", Provider::Generic), |
| 99 | ] { |
| 100 | req!(want, Provider::of(&plain, host, &t), "{}", host); |
| 101 | } |
| 102 | let mut back = t.clone(); |
| 103 | back.reverse(); |
| 104 | req!(Provider::Generic, Provider::of(&plain, "imap.example.com", &back)); |
| 105 | Ok(()) |
| 106 | } |
| 107 | |
| 108 | /// P2: the capability is the evidence of Gmail, whatever the table says. |
| 109 | #[test] |
| 110 | fn test_provider_gmail_by_capability_alone_00() -> Outcome<()> { |
| 111 | let t = table(); |
| 112 | let gm = Caps::from_list(&["IMAP4rev1".to_string(), "X-GM-EXT-1".to_string()]); |
| 113 | req!(Provider::Gmail, Provider::of(&gm, "imap.gmail.com", &t)); |
| 114 | req!(Provider::Gmail, Provider::of(&gm, "outlook.example.com", &t)); |
| 115 | req!(Provider::Generic, Provider::of(&Caps::default(), "imap.gmail.example", &t)); |
| 116 | Ok(()) |
| 117 | } |