Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/sync_wire.rs

11.6 KiB, 9 runs

created by r1870400018:75215, 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//! The sync records through their JSON wire form and back (`sync::wire`), as the gateway sends
2//! them and the page reads them.
3
4use oxedyne_fe2o3_core::prelude::*;
5use oxedyne_fe2o3_jdat::{
6 prelude::*,
7 bdat::limits::DecodeLimits,
8};
9use oxedyne_fe2o3_mail::sync::{
10 wire::{
11 self,
12 ActReply,
13 IdForm,
14 },
15 ActOutcome,
16 Action,
17 Budget,
18 Cursor,
19 Delta,
20 Entry,
21 FlagState,
22 LinkKey,
23 NewMessage,
24 Reset,
25 Snapshot,
26 SpecialUse,
27 Target,
28 Tier,
29 UidSet,
30};
31
32
33/// Through JSON text and back, as a JavaScript peer would see it. The text is returned with the
34/// encoder's space after each colon taken out, so the checks on it read as compact JSON.
35fn json_trip<T: ToDat + FromDat>(x: &T) -> Outcome<(String, T)> {
36 let text = res!(res!(x.to_dat()).json());
37 let back = res!(Dat::decode_json_strict(&text, &DecodeLimits::text()));
38 Ok((text.replace("\": ", "\":"), res!(T::from_dat(back))))
39}
40
41fn parse(s: &str) -> Outcome<Dat> {
42 Dat::decode_json_strict(s, &DecodeLimits::text())
43}
44
45fn cursor() -> Outcome<Cursor> {
46 Ok(Cursor {
47 uid_validity: 7,
48 highest_uid: 4_000_000_000,
49 highest_modseq: Some(1_234_567_890_123),
50 polls: 9,
51 backlog: res!(UidSet::parse("1:5,9,100:120")),
52 floor: 3_999_999_000,
53 })
54}
55
56#[test]
57fn test_delta_round_trips_with_sets_and_base64_bodies() -> Outcome<()> {
58 // Every byte value, so the base64 is not merely ASCII passed through.
59 let raw: Vec<u8> = (0..=255u8).cycle().take(1027).collect();
60 let d = Delta {
61 mailbox: fmt!("Archive/2024 \"quoted\""),
62 tier: Some(Tier::CondStore),
63 reset: Some(Reset { old: 3, new: 7 }),
64 new: vec![
65 NewMessage {
66 uid: 130,
67 flags: vec![fmt!("\\Seen"), fmt!("$Label1")],
68 internal_date: fmt!("26-Oct-2026 09:00:00 +0000"),
69 size: 1027,
70 raw: raw.clone(),
71 whole: true,
72 link: Some(LinkKey::MessageId(fmt!("a@b.c"))),
73 arrival: true,
74 },
75 NewMessage {
76 uid: 129,
77 flags: Vec::new(),
78 internal_date: String::new(),
79 size: 25_000_000,
80 raw: b"Subject: big\r\n\r\n".to_vec(),
81 whole: false,
82 link: Some(LinkKey::GmMsgId(u64::MAX)),
83 arrival: false,
84 },
85 NewMessage { uid: 128, raw: Vec::new(), ..Default::default() },
86 ],
87 flags: vec![FlagState { uid: 12, flags: vec![fmt!("\\Flagged")] }],
88 vanished: res!(UidSet::parse("3,7,40:60")),
89 cursor: res!(cursor()),
90 more: true,
91 };
92 let (text, back) = res!(json_trip(&d));
93 assert_eq!(back, d);
94 // The sets travel as their IMAP form and the bytes as base64.
95 assert!(text.contains("\"vanished\":\"3,7,40:60\""), "{}", text);
96 assert!(text.contains("\"backlog\":\"1:5,9,100:120\""), "{}", text);
97 assert!(text.contains(&fmt!("\"raw\":\"{}\"", oxedyne_fe2o3_text::base64::encode(&raw))));
98 assert!(text.contains("\"link_gm\":\"18446744073709551615\""), "{}", text);
99
100 // None of the optional parts, and a reader that keeps the bytes elsewhere leaves `raw` out.
101 let bare = Delta {
102 mailbox: fmt!("INBOX"),
103 cursor: Cursor { uid_validity: 1, ..Default::default() },
104 new: vec![NewMessage { uid: 5, whole: true, ..Default::default() }],
105 ..Default::default()
106 };
107 let (text, back) = res!(json_trip(&bare));
108 assert_eq!(back, bare);
109 assert!(text.contains("\"tier\":\"\""), "{}", text);
110 assert!(!text.contains("reset") && !text.contains("highest_modseq"), "{}", text);
111 let no_raw = res!(wire::delta_from(&res!(parse(r#"{"mailbox":"INBOX",
112 "cursor":{"uid_validity":1,"floor":0},"new":[{"uid":5,"whole":true}]}"#))));
113 assert_eq!(no_raw, bare);
114 Ok(())
115}
116
117#[test]
118fn test_delta_refuses_what_it_cannot_read() -> Outcome<()> {
119 let bad = [
120 r#"{"mailbox":"INBOX","cursor":{"uid_validity":1,"floor":0},"vanished":"0:x"}"#,
121 r#"{"mailbox":"INBOX","cursor":{"uid_validity":1,"floor":0,"backlog":"7:"}}"#,
122 r#"{"mailbox":"INBOX","cursor":{"uid_validity":0}}"#,
123 r#"{"mailbox":"INBOX","cursor":{"uid_validity":1}}"#,
124 r#"{"mailbox":"INBOX"}"#,
125 r#"{"cursor":{"uid_validity":1}}"#,
126 r#"{"mailbox":"INBOX","cursor":{"uid_validity":4294967296}}"#,
127 r#"{"mailbox":"INBOX","cursor":{"uid_validity":1,"floor":0},"new":[{"uid":3,"raw":"not base64!"}]}"#,
128 r#"{"mailbox":"INBOX","cursor":{"uid_validity":1,"floor":0},"new":[{"uid":0}]}"#,
129 r#"{"mailbox":"INBOX","cursor":{"uid_validity":1,"floor":0},"flags":[{"uid":3,"flags":[1]}]}"#,
130 r#"{"mailbox":"INBOX","cursor":{"uid_validity":1,"floor":0},"tier":"psychic"}"#,
131 ];
132 for b in bad {
133 assert!(wire::delta_from(&res!(parse(b))).is_err(), "read: {}", b);
134 }
135 // A folder the server refused comes back as an error naming it.
136 let refused = wire::refused_dat("Gone", "no such mailbox");
137 match wire::delta_from(&refused) {
138 Err(e) => assert!(e.plain().contains("Gone") && e.plain().contains("no such mailbox"),
139 "{}", e.plain()),
140 Ok(_) => return Err(err!("A refused folder read as a delta."; Test)),
141 }
142 Ok(())
143}
144
145#[test]
146fn test_snapshot_cursor_and_budget() -> Outcome<()> {
147 let s = Snapshot {
148 mailbox: fmt!("INBOX"),
149 cursor: res!(cursor()),
150 held: res!(UidSet::parse("6:40,42")),
151 window: res!(UidSet::parse("30:42")),
152 };
153 let (_, back) = res!(json_trip(&s));
154 assert_eq!(back, s);
155 let (_, c) = res!(json_trip(&s.cursor));
156 assert_eq!(c, s.cursor);
157
158 // The gateway's request form: no cursor is a folder never synced.
159 let f = res!(wire::snapshot_from(&res!(parse(r#"{"mailbox":"Sent","held":"","window":"1"}"#))));
160 assert_eq!((f.cursor, f.held.len(), f.window.len()), (Cursor::default(), 0, 1));
161 assert!(wire::snapshot_from(&res!(parse(r#"{"mailbox":"X","held":"0:x"}"#))).is_err());
162 assert!(wire::snapshot_from(&res!(parse(r#"{"mailbox":"X","cursor":7}"#))).is_err());
163 assert!(wire::snapshot_from(&res!(parse(r#"{"held":"1"}"#))).is_err());
164
165 // A request may only lower the limit.
166 let limit = Budget { max_count: 500, max_bytes: 4096, ..Budget::default() };
167 let req = res!(parse(r#"{"max_count":9999,"max_bytes":1000,"bodies":true}"#));
168 let b = res!(wire::budget_within(Some(&req), &limit));
169 assert_eq!((b.max_count, b.max_bytes, b.bodies), (500, 1000, true));
170 let b = res!(wire::budget_within(None, &limit));
171 assert_eq!((b.max_count, b.max_bytes, b.bodies), (500, 4096, false));
172 let b = res!(wire::budget_within(Some(&wire::budget_dat(&b)), &limit));
173 assert_eq!((b.max_count, b.max_bytes, b.bodies), (500, 4096, false));
174 assert!(wire::budget_within(Some(&res!(parse(r#"{"bodies":"yes"}"#))), &limit).is_err());
175 Ok(())
176}
177
178#[test]
179fn test_entries_round_trip_every_action() -> Outcome<()> {
180 let base = Entry {
181 id: u32::MAX as u64 + 5,
182 mailbox: fmt!("INBOX"),
183 uid_validity: 5,
184 uid: 9,
185 message_id: fmt!("<a@b>"),
186 size: 120,
187 internal_date: fmt!("01-Jan-2024 09:00:00 +0000"),
188 year: Some(2024),
189 action: Action::Archive,
190 };
191 let actions = vec![
192 Action::Flags { add: vec![fmt!("\\Seen")], remove: vec![fmt!("\\Flagged")] },
193 Action::Flags { add: Vec::new(), remove: vec![fmt!("\\Seen")] },
194 Action::Move { to: Target::Inbox },
195 Action::Move { to: Target::Path(fmt!("Work/2024")) },
196 Action::Archive,
197 Action::Trash,
198 Action::Junk,
199 Action::NotJunk,
200 Action::Expunge,
201 ];
202 let roles = [
203 SpecialUse::Sent, SpecialUse::Drafts, SpecialUse::Trash, SpecialUse::Junk,
204 SpecialUse::Archive, SpecialUse::All, SpecialUse::Flagged,
205 ];
206 let all = actions.into_iter()
207 .chain(roles.iter().map(|r| Action::Move { to: Target::Role(*r) }));
208 for a in all {
209 let e = Entry { action: a, ..base.clone() };
210 let (_, back) = res!(json_trip(&e));
211 assert_eq!(back, e);
212 }
213 let lean = Entry { message_id: String::new(), size: 0, year: None, ..base.clone() };
214 let (text, back) = res!(json_trip(&lean));
215 assert_eq!(back, lean);
216 assert!(!text.contains("message_id") && !text.contains("year"), "{}", text);
217
218 // The page's form: a string id, no optional fields.
219 let e = res!(wire::entry_from(&res!(parse(r#"{"id":"2","mailbox":"INBOX","uid_validity":5,
220 "uid":10,"action":"move","to":"role:junk"}"#))));
221 assert_eq!((e.id, &e.action), (2, &Action::Move { to: Target::Role(SpecialUse::Junk) }));
222 let c = Entry { action: Action::Copy { to: Target::Path("A/B".into()) }, ..e };
223 assert_eq!(res!(wire::entry_from(&wire::entry_dat(&c))), c);
224 let bad = [
225 r#"{"id":1,"mailbox":"INBOX","uid_validity":5,"uid":9,"action":"flags"}"#,
226 r#"{"id":1,"mailbox":"INBOX","uid_validity":5,"uid":9,"action":"move","to":"elsewhere"}"#,
227 r#"{"id":1,"mailbox":"INBOX","uid_validity":5,"uid":9,"action":"move","to":"role:inbox"}"#,
228 r#"{"id":1,"mailbox":"INBOX","uid_validity":5,"uid":9,"action":"burn"}"#,
229 r#"{"id":1,"mailbox":"INBOX","uid":9,"action":"trash"}"#,
230 r#"{"id":"x","mailbox":"INBOX","uid_validity":5,"uid":9,"action":"trash"}"#,
231 r#"{"mailbox":"INBOX","uid_validity":5,"uid":9,"action":"trash"}"#,
232 r#"{"id":1,"mailbox":"INBOX","uid_validity":5,"uid":9,"action":"trash","year":70000}"#,
233 ];
234 for b in bad {
235 assert!(wire::entry_from(&res!(parse(b))).is_err(), "read: {}", b);
236 }
237 assert_eq!(wire::target_of("path:"), None);
238 Ok(())
239}
240
241#[test]
242fn test_replies_round_trip_every_outcome() -> Outcome<()> {
243 let all = [
244 Ok(ActOutcome::Done),
245 Ok(ActOutcome::Gone),
246 Ok(ActOutcome::Superseded(fmt!("Archive/2024"))),
247 Ok(ActOutcome::Already(fmt!("Trash"))),
248 Ok(ActOutcome::Lost),
249 Ok(ActOutcome::Stuck(fmt!("two copies match"))),
250 Err(fmt!("The mail server could not be reached.")),
251 ];
252 for o in all {
253 let r = ActReply { id: (1u64 << 60) + 3, form: IdForm::Text, outcome: o.clone() };
254 let (text, back) = res!(json_trip(&r));
255 assert_eq!(back, r);
256 assert!(text.contains("\"id\":\"1152921504606846979\""), "{}", text);
257 let r = ActReply { id: 17, form: IdForm::Number, outcome: o };
258 let (text, back) = res!(json_trip(&r));
259 assert_eq!(back, r);
260 assert!(text.contains("\"id\":17"), "{}", text);
261 }
262 assert!(wire::reply_from(&res!(parse(r#"{"id":"1","outcome":"maybe"}"#))).is_err());
263 Ok(())
264}
265
266/// A reply's id comes back in the type the entry's came in, so the page matches it by `===`:
267/// a number for a number, a decimal string for a string.
268#[test]
269fn test_a_reply_id_has_the_type_of_its_entry_id() -> Outcome<()> {
270 for (entry, want) in [
271 (r#"{"id":7,"mailbox":"INBOX","uid_validity":5,"uid":9,"action":"trash"}"#, "\"id\":7,"),
272 (r#"{"id":"7","mailbox":"INBOX","uid_validity":5,"uid":9,"action":"trash"}"#, "\"id\":\"7\","),
273 ] {
274 let d = res!(parse(entry));
275 let e = res!(wire::entry_from(&d));
276 let r = ActReply { id: e.id, form: wire::id_form(&d), outcome: Ok(ActOutcome::Done) };
277 let text = res!(wire::reply_dat(&r).json());
278 assert!(text.replace(' ', "").contains(want), "{} answered {}", entry, text);
279 }
280 Ok(())
281}