Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/mime.rs

5.7 KiB, 5 runs

created by r1870400018:74764, 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//! RFC 2047 encoded-words, as the mail reader and IMAP SEARCH both decode them.
2
3use oxedyne_fe2o3_core::prelude::*;
4use oxedyne_fe2o3_text::{
5 base64,
6 mime::{
7 decode_qp,
8 decode_words,
9 entity,
10 leaf_parts,
11 param,
12 to_utf8,
13 Structure,
14 },
15};
16
17
18#[test]
19fn decodes_a_2047_subject() {
20 // "Grüße" base64 in UTF-8.
21 let enc = base64::encode("Grüße".as_bytes());
22 let s = fmt!("=?utf-8?B?{}?=", enc);
23 assert_eq!(decode_words(&s), "Grüße");
24}
25
26#[test]
27fn decodes_a_q_word() {
28 // "_" is a space in Q, so "=5F" is a literal underscore.
29 let s = "=?utf-8?Q?a=5Fb?=";
30 assert_eq!(decode_words(s), "a_b");
31 assert_eq!(decode_words("=?UTF-8?Q?Ren=C3=A9e_Dupont?= <renee@x.example>"),
32 "Renée Dupont <renee@x.example>");
33}
34
35#[test]
36fn joins_two_adjacent_words() {
37 let a = base64::encode("Ma".as_bytes());
38 let b = base64::encode("ría".as_bytes());
39 let s = fmt!("=?utf-8?B?{}?= =?utf-8?B?{}?=", a, b);
40 assert_eq!(decode_words(&s), "María");
41}
42
43#[test]
44fn leaves_plain_text_and_malformed_words_alone() {
45 assert_eq!(decode_words("Lunch on Friday"), "Lunch on Friday");
46 assert_eq!(decode_words("=?utf-8?X?abc?= and =?broken"), "=?utf-8?X?abc?= and =?broken");
47 // Ordinary text between two words keeps its spaces.
48 assert_eq!(decode_words("=?utf-8?Q?a?= b =?utf-8?Q?c?="), "a b c");
49}
50
51#[test]
52fn decodes_a_quoted_printable_body() {
53 assert_eq!(decode_qp("caf=C3=A9 =\r\nau lait"), "café au lait".as_bytes());
54}
55
56#[test]
57fn decodes_a_latin1_word() {
58 assert_eq!(decode_words("=?ISO-8859-1?Q?Andr=E9?= <a@b>"), "André <a@b>");
59 assert_eq!(decode_words("=?iso-8859-15*fr?Q?=A4?="), "\u{20AC}");
60}
61
62#[test]
63fn transcodes_the_three_legacy_charsets() {
64 assert_eq!(to_utf8(&[0x63, 0x61, 0x66, 0xE9], Some("ISO-8859-1")), "café");
65 assert_eq!(to_utf8(&[0xA4, 0xBD], Some("iso-8859-15")), "\u{20AC}\u{0153}");
66 assert_eq!(to_utf8(&[0x93, 0x78, 0x94, 0x80], Some("windows-1252")), "\u{201C}x\u{201D}\u{20AC}");
67 assert_eq!(to_utf8("é".as_bytes(), Some("utf-8")), "é");
68}
69
70#[test]
71fn walks_nested_multiparts_by_boundary() {
72 let mut raw: Vec<u8> = b"Content-Type: multipart/mixed; boundary=\"A\"\r\n\r\npre\r\n\
73 --A\r\nContent-Type: multipart/alternative; boundary=B\r\n\r\n\
74 --B\r\nContent-Type: text/plain; charset=iso-8859-1\r\n\
75 Content-Transfer-Encoding: 8bit\r\n\r\ncaf".to_vec();
76 raw.push(0xE9);
77 raw.extend_from_slice(b"\r\n--B\r\nContent-Type: text/html\r\n\r\n<p>x</p>\r\n--B--\r\n\
78 --A\r\nContent-Type: application/pdf; name=\"r.pdf\"\r\n\
79 Content-Transfer-Encoding: base64\r\n\r\nJVBERi0=\r\n--A--\r\nepilogue\r\n");
80 let parts = leaf_parts(&raw);
81 assert_eq!(parts.len(), 3);
82 assert_eq!(parts[0].text(), "café");
83 assert!(parts[0].is_text() && !parts[0].is_attachment());
84 assert_eq!(parts[1].media_type(), "text/html");
85 assert_eq!(parts[2].filename().as_deref(), Some("r.pdf"));
86 assert_eq!(parts[2].text(), "%PDF-");
87}
88
89#[test]
90fn param_honours_quoted_strings() {
91 // RFC 2045 §5.1: a quoted value may hold `;` and `=`, and a backslash quotes the next
92 // character.
93 assert_eq!(param("attachment; filename=\"a;b.pdf\"", "filename").as_deref(), Some("a;b.pdf"));
94 assert_eq!(param("attachment; filename=\"say \\\"hi\\\".txt\"; size=3", "filename").as_deref(),
95 Some("say \"hi\".txt"));
96 assert_eq!(param("text/plain; name=\"x=1;y\"; charset=utf-8", "charset").as_deref(), Some("utf-8"));
97 assert_eq!(param("multipart/mixed; boundary = \"SP\"", "boundary").as_deref(), Some("SP"));
98 assert_eq!(param("attachment; filename=\"open", "filename").as_deref(), Some("open"));
99 assert_eq!(param("attachment; filename=\"\"", "filename"), None);
100}
101
102#[test]
103fn param_reads_rfc_2231_values_and_continuations() {
104 assert_eq!(param("attachment; filename*=UTF-8''r%C3%A9sum%C3%A9.pdf", "filename").as_deref(),
105 Some("résumé.pdf"));
106 assert_eq!(param("attachment; filename*=iso-8859-1'fr'caf%E9.txt", "filename").as_deref(),
107 Some("café.txt"));
108 assert_eq!(param("attachment; filename*1=\"name.pdf\"; filename*0=\"long \"", "filename").as_deref(),
109 Some("long name.pdf"));
110 assert_eq!(param("attachment;\r\n filename*0*=UTF-8''r%C3%A9; filename*1*=sum%C3%A9; filename*2=.pdf",
111 "filename").as_deref(), Some("résumé.pdf"));
112 // The extended value is preferred to the plain one a sender adds for old readers.
113 assert_eq!(param("attachment; filename=\"resume.pdf\"; filename*=UTF-8''r%C3%A9sum%C3%A9.pdf",
114 "filename").as_deref(), Some("résumé.pdf"));
115}
116
117#[test]
118fn an_attached_message_is_one_part_of_its_parent() {
119 let raw = b"Content-Type: multipart/mixed; boundary=M\r\n\r\n\
120 --M\r\nContent-Type: text/plain\r\n\r\nsee below\r\n\
121 --M\r\nContent-Type: message/rfc822\r\n\r\n\
122 Subject: inner one\r\nContent-Type: text/plain\r\n\r\ninner text\r\n--M--\r\n";
123 let top = entity(raw);
124 let kids = match &top.structure {
125 Structure::Multipart(k) => k,
126 other => panic!("not a multipart: {:?}", other),
127 };
128 assert_eq!(kids.len(), 2);
129 assert!(kids[1].is_attachment());
130 assert_eq!(kids[1].attachment_name(), "inner one");
131 match &kids[1].structure {
132 Structure::Message(inner) => assert_eq!(inner.text(), "inner text"),
133 other => panic!("not a message: {:?}", other),
134 }
135 // The reader shows only the outer text; SEARCH still reads the inner one.
136 let shown: Vec<String> = top.display_parts().iter().map(|p| p.text()).collect();
137 assert_eq!(shown, vec!["see below".to_string()]);
138 let leaves: Vec<String> = leaf_parts(raw).iter().map(|p| p.text()).collect();
139 assert_eq!(leaves, vec!["see below".to_string(), "inner text".to_string()]);
140}