aboutsummaryrefslogtreecommitdiff
path: root/tests/common/fragments.rs
blob: c8d5ef191c8652a51c3fe2097b5815f051384d2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use anyhow::{bail, Result};
use std::io::Write;
use std::net::TcpStream;
use std::thread;

use crate::common::constants::*;
use crate::common::*;

/// These fragments are not a generic IMAP client
/// but specialized to our specific tests. They can't take
/// arbitrary values, only enum for which the code is known
/// to be correct. The idea is that the generated message is more
/// or less hardcoded by the developer, so its clear what's expected,
/// and not generated by a library. Also don't use vector of enum,
/// as it again introduce some kind of genericity we try so hard to avoid:
/// instead add a dedicated enum, for example "All" or anything relaevent that would
/// describe your list and then hardcode it in your fragment.
/// DON'T. TRY. TO. BE. GENERIC. HERE.

pub fn connect(imap: &mut TcpStream) -> Result<()> {
    let mut buffer: [u8; 1500] = [0; 1500];

    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(&read[..4], &b"* OK"[..]);

    Ok(())
}

pub enum Account {
    Alice,
}

pub enum Extension {
    None,
    Unselect,
    Move,
    CondStore,
}

pub enum Enable {
    Utf8Accept,
    CondStore,
    All,
}

pub enum Mailbox {
    Inbox,
    Archive,
    Drafts,
}

pub enum Flag {
    Deleted,
    Important
}

pub enum Email {
    Basic,
    Multipart,
}

pub enum Selection {
    FirstId,
    SecondId,
}

pub fn capability(imap: &mut TcpStream, ext: Extension) -> Result<()> {
    imap.write(&b"5 capability\r\n"[..])?;

    let maybe_ext = match ext {
        Extension::None => None,
        Extension::Unselect => Some("UNSELECT"),
        Extension::Move => Some("MOVE"),
        Extension::CondStore => Some("CONDSTORE"),
    };

    let mut buffer: [u8; 1500] = [0; 1500];
    let read = read_lines(imap, &mut buffer, Some(&b"5 OK"[..]))?;
    let srv_msg = std::str::from_utf8(read)?;
    assert!(srv_msg.contains("IMAP4REV1"));
    if let Some(ext) = maybe_ext {
        assert!(srv_msg.contains(ext));
    }

    Ok(())
}

pub fn login(imap: &mut TcpStream, account: Account) -> Result<()> {
    let mut buffer: [u8; 1500] = [0; 1500];

    assert!(matches!(account, Account::Alice));
    imap.write(&b"10 login alice hunter2\r\n"[..])?;

    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(&read[..5], &b"10 OK"[..]);

    Ok(())
}

pub fn create_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> {
    let mut buffer: [u8; 1500] = [0; 1500];

    let mbx_str = match mbx {
        Mailbox::Inbox => "INBOX",
        Mailbox::Archive => "Archive",
        Mailbox::Drafts => "Drafts",
    };

    let cmd = format!("15 create {}\r\n", mbx_str);
    imap.write(cmd.as_bytes())?;
    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(&read[..12], &b"15 OK CREATE"[..]);

    Ok(())
}

pub fn select(imap: &mut TcpStream, mbx: Mailbox, maybe_exists: Option<u64>) -> Result<()> {
    let mut buffer: [u8; 6000] = [0; 6000];

    let mbx_str = match mbx {
        Mailbox::Inbox => "INBOX",
        Mailbox::Archive => "Archive",
        Mailbox::Drafts => "Drafts",
    };
    imap.write(format!("20 select {}\r\n", mbx_str).as_bytes())?;

    let read = read_lines(imap, &mut buffer, Some(&b"20 OK"[..]))?;
    let srv_msg = std::str::from_utf8(read)?;
    if let Some(exists) = maybe_exists {
        let expected = format!("* {} EXISTS", exists);
        assert!(srv_msg.contains(&expected));
    }

    Ok(())
}

pub fn unselect(imap: &mut TcpStream) -> Result<()> {
    imap.write(&b"70 unselect\r\n"[..])?;
    let mut buffer: [u8; 1500] = [0; 1500];
    let _read = read_lines(imap, &mut buffer, Some(&b"70 OK"[..]))?;

    Ok(())
}

pub fn check(imap: &mut TcpStream) -> Result<()> {
    let mut buffer: [u8; 1500] = [0; 1500];

    imap.write(&b"21 check\r\n"[..])?;
    let _read = read_lines(imap, &mut buffer, Some(&b"21 OK"[..]))?;

    Ok(())
}

pub fn status_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> {
    assert!(matches!(mbx, Mailbox::Archive));
    imap.write(&b"25 STATUS Archive (UIDNEXT MESSAGES)\r\n"[..])?;
    let mut buffer: [u8; 6000] = [0; 6000];
    let _read = read_lines(imap, &mut buffer, Some(&b"25 OK"[..]))?;

    Ok(())
}

pub fn lmtp_handshake(lmtp: &mut TcpStream) -> Result<()> {
    let mut buffer: [u8; 1500] = [0; 1500];

    let _read = read_lines(lmtp, &mut buffer, None)?;
    assert_eq!(&buffer[..4], &b"220 "[..]);

    lmtp.write(&b"LHLO example.tld\r\n"[..])?;
    let _read = read_lines(lmtp, &mut buffer, Some(&b"250 "[..]))?;

    Ok(())
}

pub fn lmtp_deliver_email(lmtp: &mut TcpStream, email_type: Email) -> Result<()> {
    let mut buffer: [u8; 1500] = [0; 1500];

    let email = match email_type {
        Email::Basic => EMAIL2,
        Email::Multipart => EMAIL1,
    };
    lmtp.write(&b"MAIL FROM:<bob@example.tld>\r\n"[..])?;
    let _read = read_lines(lmtp, &mut buffer, Some(&b"250 2.0.0"[..]))?;

    lmtp.write(&b"RCPT TO:<alice@example.tld>\r\n"[..])?;
    let _read = read_lines(lmtp, &mut buffer, Some(&b"250 2.1.5"[..]))?;

    lmtp.write(&b"DATA\r\n"[..])?;
    let _read = read_lines(lmtp, &mut buffer, Some(&b"354 "[..]))?;

    lmtp.write(email)?;
    lmtp.write(&b"\r\n.\r\n"[..])?;
    let _read = read_lines(lmtp, &mut buffer, Some(&b"250 2.0.0"[..]))?;

    Ok(())
}

pub fn noop_exists(imap: &mut TcpStream) -> Result<()> {
    let mut buffer: [u8; 6000] = [0; 6000];

    let mut max_retry = 20;
    loop {
        max_retry -= 1;
        imap.write(&b"30 NOOP\r\n"[..])?;
        let read = read_lines(imap, &mut buffer, Some(&b"30 OK"[..]))?;
        let srv_msg = std::str::from_utf8(read)?;

        match (max_retry, srv_msg.lines().count()) {
            (_, cnt) if cnt > 1 => break,
            (0, _) => bail!("no more retry"),
            _ => (),
        }

        thread::sleep(SMALL_DELAY);
    }

    Ok(())
}

pub fn fetch_rfc822(imap: &mut TcpStream, selection: Selection, r#ref: Email) -> Result<()> {
    let mut buffer: [u8; 65535] = [0; 65535];

    assert!(matches!(selection, Selection::FirstId));
    imap.write(&b"40 fetch 1 rfc822\r\n"[..])?;

    let read = read_lines(imap, &mut buffer, Some(&b"40 OK FETCH"[..]))?;
    let srv_msg = std::str::from_utf8(read)?;

    let ref_mail = match r#ref {
        Email::Basic => EMAIL2,
        Email::Multipart => EMAIL1,
    };
    let orig_email = std::str::from_utf8(ref_mail)?;
    assert!(srv_msg.contains(orig_email));

    Ok(())
}

pub fn copy(imap: &mut TcpStream, selection: Selection, to: Mailbox) -> Result<()> {
    let mut buffer: [u8; 65535] = [0; 65535];
    assert!(matches!(selection, Selection::FirstId));
    assert!(matches!(to, Mailbox::Archive));

    imap.write(&b"45 copy 1 Archive\r\n"[..])?;
    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(&read[..5], &b"45 OK"[..]);

    Ok(())
}

pub fn append_email(imap: &mut TcpStream, content: Email) -> Result<()> {
    let mut buffer: [u8; 6000] = [0; 6000];

    let ref_mail = match content {
        Email::Multipart => EMAIL1,
        Email::Basic => EMAIL2,
    };

    let append_cmd = format!("47 append inbox (\\Seen) {{{}}}\r\n", ref_mail.len());
    println!("append cmd: {}", append_cmd);
    imap.write(append_cmd.as_bytes())?;

    // wait for continuation
    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(read[0], b'+');

    // write our stuff
    imap.write(ref_mail)?;
    imap.write(&b"\r\n"[..])?;
    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(&read[..5], &b"47 OK"[..]);

    // we check that noop detects the change
    noop_exists(imap)?;

    Ok(())
}



pub fn add_flags_email(imap: &mut TcpStream, selection: Selection, flag: Flag) -> Result<()> {
    let mut buffer: [u8; 1500] = [0; 1500];
    assert!(matches!(selection, Selection::FirstId));
    assert!(matches!(flag, Flag::Deleted));
    imap.write(&b"50 store 1 +FLAGS (\\Deleted)\r\n"[..])?;
    let _read = read_lines(imap, &mut buffer, Some(&b"50 OK STORE"[..]))?;

    Ok(())
}

#[allow(dead_code)]
/// Not yet implemented
pub fn search(imap: &mut TcpStream) -> Result<()> {
    imap.write(&b"55 search text \"OoOoO\"\r\n"[..])?;
    let mut buffer: [u8; 1500] = [0; 1500];
    let _read = read_lines(imap, &mut buffer, Some(&b"55 OK SEARCH"[..]))?;
    Ok(())
}

pub fn expunge(imap: &mut TcpStream) -> Result<()> {
    imap.write(&b"60 expunge\r\n"[..])?;
    let mut buffer: [u8; 1500] = [0; 1500];
    let _read = read_lines(imap, &mut buffer, Some(&b"60 OK EXPUNGE"[..]))?;

    Ok(())
}

pub fn rename_mailbox(imap: &mut TcpStream, from: Mailbox, to: Mailbox) -> Result<()> {
    assert!(matches!(from, Mailbox::Archive));
    assert!(matches!(to, Mailbox::Drafts));

    imap.write(&b"70 rename Archive Drafts\r\n"[..])?;
    let mut buffer: [u8; 1500] = [0; 1500];
    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(&read[..5], &b"70 OK"[..]);

    imap.write(&b"71 list \"\" *\r\n"[..])?;
    let read = read_lines(imap, &mut buffer, Some(&b"71 OK LIST"[..]))?;
    let srv_msg = std::str::from_utf8(read)?;
    assert!(!srv_msg.contains(" Archive\r\n"));
    assert!(srv_msg.contains(" INBOX\r\n"));
    assert!(srv_msg.contains(" Drafts\r\n"));

    Ok(())
}

pub fn delete_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> {
    let mbx_str = match mbx {
        Mailbox::Inbox => "INBOX",
        Mailbox::Archive => "Archive",
        Mailbox::Drafts => "Drafts",
    };
    let cmd = format!("80 delete {}\r\n", mbx_str);

    imap.write(cmd.as_bytes())?;
    let mut buffer: [u8; 1500] = [0; 1500];
    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(&read[..5], &b"80 OK"[..]);

    imap.write(&b"81 list \"\" *\r\n"[..])?;
    let read = read_lines(imap, &mut buffer, Some(&b"81 OK"[..]))?;
    let srv_msg = std::str::from_utf8(read)?;
    assert!(srv_msg.contains(" INBOX\r\n"));
    assert!(!srv_msg.contains(format!(" {}\r\n", mbx_str).as_str()));

    Ok(())
}

pub fn close(imap: &mut TcpStream) -> Result<()> {
    imap.write(&b"60 close\r\n"[..])?;
    let mut buffer: [u8; 1500] = [0; 1500];
    let _read = read_lines(imap, &mut buffer, Some(&b"60 OK"[..]))?;

    Ok(())
}

pub fn r#move(imap: &mut TcpStream, selection: Selection, to: Mailbox) -> Result<()> {
    let mut buffer: [u8; 1500] = [0; 1500];
    assert!(matches!(to, Mailbox::Archive));
    assert!(matches!(selection, Selection::FirstId));

    imap.write(&b"35 move 1 Archive\r\n"[..])?;
    let read = read_lines(imap, &mut buffer, Some(&b"35 OK"[..]))?;
    let srv_msg = std::str::from_utf8(read)?;
    assert!(srv_msg.contains("* 1 EXPUNGE"));

    Ok(())
}

pub fn enable(imap: &mut TcpStream, ask: Enable, done: Option<Enable>) -> Result<()> {
    let mut buffer: [u8; 6000] = [0; 6000];
    assert!(matches!(ask, Enable::Utf8Accept));

    imap.write(&b"36 enable UTF8=ACCEPT\r\n"[..])?;
    let read = read_lines(imap, &mut buffer, Some(&b"36 OK"[..]))?;
    let srv_msg = std::str::from_utf8(read)?;
    match done {
        None => assert_eq!(srv_msg.lines().count(), 1),
        Some(Enable::Utf8Accept) => {
            assert_eq!(srv_msg.lines().count(), 2);
            assert!(srv_msg.contains("* ENABLED UTF8=ACCEPT"));
        },
        _ => unimplemented!(),
    }

    Ok(())
}

pub fn logout(imap: &mut TcpStream) -> Result<()> {
    imap.write(&b"99 logout\r\n"[..])?;
    let mut buffer: [u8; 1500] = [0; 1500];
    let read = read_lines(imap, &mut buffer, None)?;
    assert_eq!(&read[..5], &b"* BYE"[..]);
    Ok(())
}