mailin 0.6.5

A library for writing SMTP servers
Documentation
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
use std::net::IpAddr;
use std::str;

use crate::fsm::StateMachine;
use crate::response::*;
use crate::{AuthMechanism, Handler};
use either::{Left, Right};

//------ Types -----------------------------------------------------------------

// Smtp commands sent by the client
#[derive(Clone)]
pub enum Cmd<'a> {
    Ehlo {
        domain: &'a str,
    },
    Helo {
        domain: &'a str,
    },
    Mail {
        reverse_path: &'a str,
        is8bit: bool,
    },
    Rcpt {
        forward_path: &'a str,
    },
    Data,
    Rset,
    Noop,
    StartTls,
    Quit,
    Vrfy,
    AuthLogin {
        username: String,
    },
    AuthPlain {
        authorization_id: String,
        authentication_id: String,
        password: String,
    },
    AuthLoginEmpty,
    AuthPlainEmpty,
    // Dummy command containing client authentication
    AuthResponse {
        response: &'a [u8],
    },
    // Dummy command to signify end of data
    DataEnd,
    // Dummy command sent when STARTTLS was successful
    StartedTls,
}

pub(crate) struct Credentials {
    pub authorization_id: String,
    pub authentication_id: String,
    pub password: String,
}

/// A single smtp session connected to a single client
pub struct Session<H: Handler> {
    name: String,
    handler: H,
    fsm: StateMachine,
}

#[derive(Clone)]
/// Builds an smtp `Session`
///
/// # Examples
/// ```
/// # use mailin::{Session, SessionBuilder, Handler, AuthMechanism};
///
/// # use std::net::{IpAddr, Ipv4Addr};
/// # struct EmptyHandler{};
/// # impl Handler for EmptyHandler{};
/// # let addr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
/// # let handler = EmptyHandler{};
/// // Create a session builder that holds the configuration
/// let mut builder = SessionBuilder::new("server_name");
/// builder.enable_start_tls()
///        .enable_auth(AuthMechanism::Plain);
/// // Then when a client connects
/// let mut session = builder.build(addr, handler);
///
pub struct SessionBuilder {
    name: String,
    start_tls_extension: bool,
    insecure_allow_plaintext_auth: bool,
    auth_mechanisms: Vec<AuthMechanism>,
}

impl SessionBuilder {
    /// Create a new session for the given mailserver name
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self {
            name: name.into(),
            start_tls_extension: false,
            insecure_allow_plaintext_auth: false,
            auth_mechanisms: Vec::with_capacity(4),
        }
    }

    /// Enable support for StartTls
    pub fn enable_start_tls(&mut self) -> &mut Self {
        self.start_tls_extension = true;
        self
    }

    /// Enable support for authentication
    pub fn enable_auth(&mut self, auth: AuthMechanism) -> &mut Self {
        self.auth_mechanisms.push(auth);
        self
    }

    /// Allow authentication over plaintext and advertise authentication mechanisms before a connection
    /// was upgraded to TLS with STARTTLS.
    ///
    /// This allows supporting non-compliant clients that either don't attempt STARTTLS if their
    /// preferred authentication mechanism is not advertised before using STARTTLS or that don't
    /// support TLS at all.
    ///
    /// Don't use this option if you don't know what you are doing and why it is fine to do in your
    /// case, because this opens up the potential for clients to accidentally send credentials over
    /// an insecure connection even if they actually support TLS.
    pub fn insecure_enable_plaintext_auth(&mut self) -> &mut Self {
        self.insecure_allow_plaintext_auth = true;
        self
    }

    /// Build a new session to handle a connection from the given ip address
    pub fn build<H: Handler>(&self, remote: IpAddr, handler: H) -> Session<H> {
        Session {
            name: self.name.clone(),
            handler,
            fsm: StateMachine::new(
                remote,
                self.auth_mechanisms.clone(),
                self.start_tls_extension,
                self.insecure_allow_plaintext_auth,
            ),
        }
    }
}

impl<H: Handler> Session<H> {
    /// Get a greeting to send to the client
    pub fn greeting(&self) -> Response {
        Response::dynamic(220, format!("{} ESMTP", self.name), Vec::new())
    }

    /// STARTTLS active
    pub fn tls_active(&mut self) {
        self.command(Cmd::StartedTls);
    }

    /// Process a line sent by the client.
    ///
    /// Returns a response that should be written back to the client.
    ///
    /// # Examples
    /// ```
    /// use mailin::{Session, SessionBuilder, Handler, Action};
    ///
    /// # use std::net::{IpAddr, Ipv4Addr};
    /// # struct EmptyHandler{};
    /// # impl Handler for EmptyHandler{};
    /// # let addr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
    /// # let handler = EmptyHandler{};
    /// # let mut session = SessionBuilder::new("name").build(addr, handler);
    /// let response = session.process(b"HELO example.com\r\n");
    ///
    /// // Check the response
    /// assert_eq!(response.is_error, false);
    /// assert_eq!(response.action, Action::Reply);
    ///
    /// // Write the response
    /// let mut msg = Vec::new();
    /// response.write_to(&mut msg);
    /// assert_eq!(&msg, b"250 OK\r\n");
    /// ```
    pub fn process(&mut self, line: &[u8]) -> Response {
        // TODO: process within fsm
        let response = match self.fsm.process_line(&mut self.handler, line) {
            Left(cmd) => self.command(cmd),
            Right(res) => res,
        };
        response.log();
        response
    }

    fn command(&mut self, cmd: Cmd) -> Response {
        self.fsm.command(&mut self.handler, cmd)
    }
}

//----- Tests ------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fsm::SmtpState;
    use std::net::Ipv4Addr;
    use ternop::ternary;

    struct EmptyHandler {}
    impl Handler for EmptyHandler {}
    struct DataHandler(Vec<u8>);
    impl Handler for DataHandler {
        fn data(&mut self, buf: &[u8]) -> std::io::Result<()> {
            self.0.extend(buf);
            Ok(())
        }
    }

    // Check that the state machine matches the given state pattern
    macro_rules! assert_state {
        ($val:expr, $n:pat ) => {{
            assert!(
                match $val {
                    $n => true,
                    _ => false,
                },
                "{:?} !~ {}",
                $val,
                stringify!($n)
            )
        }};
    }

    fn new_session() -> Session<EmptyHandler> {
        let addr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        SessionBuilder::new("some.name").build(addr, EmptyHandler {})
    }

    fn new_data_session() -> Session<DataHandler> {
        let addr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        SessionBuilder::new("some.name").build(addr, DataHandler(vec![]))
    }

    #[test]
    fn helo_ehlo() {
        let mut session = new_session();
        let res1 = session.process(b"helo a.domain\r\n");
        assert_eq!(res1.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
        let res2 = session.process(b"ehlo b.domain\r\n");
        assert_eq!(res2.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
    }

    #[test]
    fn mail_from() {
        let mut session = new_session();
        session.process(b"helo a.domain\r\n");
        let res = session.process(b"mail from:<ship@sea.com>\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Mail);
    }

    #[test]
    fn domain_badchars() {
        let mut session = new_session();
        let res = session.process(b"helo world\x40\xff\r\n");
        assert_eq!(res.code, 500);
        assert_state!(session.fsm.current_state(), SmtpState::Idle);
    }

    #[test]
    fn rcpt_to() {
        let mut session = new_session();
        session.process(b"helo a.domain\r\n");
        session.process(b"mail from:<ship@sea.com>\r\n");
        let res1 = session.process(b"rcpt to:<fish@sea.com>\r\n");
        assert_eq!(res1.code, 250);
        let res2 = session.process(b"rcpt to:<kraken@sea.com>\r\n");
        assert_eq!(res2.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Rcpt);
    }

    #[test]
    fn helo_noop() {
        let mut session = new_session();
        let res1 = session.process(b"helo a.domain\r\n");
        assert_eq!(res1.code, 250);
        let res2 = session.process(b"noop\r\n");
        assert_eq!(res2.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
        session.process(b"mail from:<ship@sea.com>\r\n");
        let res3 = session.process(b"noop\r\n");
        assert_eq!(res3.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Mail);
        session.process(b"rcpt to:<fish@sea.com>\r\n");
        let res4 = session.process(b"noop\r\n");
        assert_eq!(res4.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Rcpt);
    }

    #[test]
    fn data() {
        let mut session = new_data_session();
        session.process(b"helo a.domain\r\n");
        session.process(b"mail from:<ship@sea.com>\r\n");
        session.process(b"rcpt to:<fish@sea.com>\r\n");
        let res1 = session.process(b"data\r\n");
        assert_eq!(res1.code, 354);
        let res2 = session.process(b"Hello World\r\n");
        assert_eq!(res2.action, Action::NoReply);
        let res3 = session.process(b".\r\n");
        assert_eq!(res3.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
        assert_eq!(&session.handler.0, b"Hello World\r\n");
    }

    #[test]
    fn dot_stuffed_data() {
        let mut session = new_data_session();
        session.process(b"helo a.domain\r\n");
        session.process(b"mail from:<ship@sea.com>\r\n");
        session.process(b"rcpt to:<fish@sea.com>\r\n");
        let res1 = session.process(b"data\r\n");
        assert_eq!(res1.code, 354);
        let res2 = session.process(b"Hello World\r\n");
        assert_eq!(res2.action, Action::NoReply);
        let res3 = session.process(b"..\r\n");
        assert_eq!(res3.action, Action::NoReply);
        let res3 = session.process(b".\r\n");
        assert_eq!(res3.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
        assert_eq!(&session.handler.0, b"Hello World\r\n.\r\n");
    }

    #[test]
    fn data_8bit() {
        let mut session = new_session();
        session.process(b"helo a.domain\r\n");
        session.process(b"mail from:<ship@sea.com> body=8bitmime\r\n");
        session.process(b"rcpt to:<fish@sea.com>\r\n");
        let res1 = session.process(b"data\r\n");
        assert_eq!(res1.code, 354);
        // Send illegal utf-8 but valid 8bit mime
        let res2 = session.process(b"Hello 8bit world \x40\x7f\r\n");
        assert_eq!(res2.action, Action::NoReply);
        let res3 = session.process(b".\r\n");
        assert_eq!(res3.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
    }

    #[test]
    fn rset_hello() {
        let mut session = new_session();
        session.process(b"helo some.domain\r\n");
        session.process(b"mail from:<ship@sea.com>\r\n");
        let res = session.process(b"rset\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
    }

    #[test]
    fn rset_idle() {
        let mut session = new_session();
        let res = session.process(b"rset\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Idle);
    }

    #[test]
    fn quit() {
        let mut session = new_session();
        session.process(b"helo a.domain\r\n");
        session.process(b"mail from:<ship@sea.com>\r\n");
        let res = session.process(b"quit\r\n");
        assert_eq!(res.code, 221);
        assert_eq!(res.action, Action::Close);
        assert_state!(session.fsm.current_state(), SmtpState::Invalid);
    }

    #[test]
    fn vrfy() {
        let mut session = new_session();
        session.process(b"helo a.domain\r\n");
        let res1 = session.process(b"vrfy kraken\r\n");
        assert_eq!(res1.code, 252);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
        session.process(b"mail from:<ship@sea.com>\r\n");
        let res2 = session.process(b"vrfy boat\r\n");
        assert_eq!(res2.code, 503);
        assert_state!(session.fsm.current_state(), SmtpState::Mail);
    }

    struct AuthHandler {}
    impl Handler for AuthHandler {
        fn auth_plain(
            &mut self,
            authorization_id: &str,
            authentication_id: &str,
            password: &str,
        ) -> Response {
            ternary!(
                authorization_id == "test" && authentication_id == "test" && password == "1234",
                AUTH_OK,
                INVALID_CREDENTIALS
            )
        }

        fn auth_login(&mut self, username: &str, password: &str) -> Response {
            ternary!(
                username == "test" && password == "1234",
                AUTH_OK,
                INVALID_CREDENTIALS
            )
        }
    }

    fn new_auth_session(with_start_tls: bool) -> Session<AuthHandler> {
        let addr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        let mut builder = SessionBuilder::new("some.domain");
        builder.enable_auth(AuthMechanism::Plain);
        builder.enable_auth(AuthMechanism::Login);
        if with_start_tls {
            builder.enable_start_tls();
        }
        builder.build(addr, AuthHandler {})
    }

    fn start_tls(session: &mut Session<AuthHandler>) {
        let res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        let res = session.process(b"starttls\r\n");
        assert_eq!(res.code, 220);
        session.tls_active();
    }

    #[test]
    fn noauth_denied() {
        let mut session = new_auth_session(true);
        session.process(b"ehlo a.domain\r\n");
        let res = session.process(b"mail from:<ship@sea.com>\r\n");
        assert_eq!(res.code, 503);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
    }

    #[test]
    fn auth_ehlo() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        let res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        let greeting = String::from_utf8(res.buffer().unwrap()).unwrap();
        assert_eq!(
            greeting,
            "250-server offers extensions:\r\n250-8BITMIME\r\n250 AUTH PLAIN LOGIN\r\n".to_string()
        )
    }

    #[test]
    fn auth_plain_param() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        let mut res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        res = session.process(b"auth plain dGVzdAB0ZXN0ADEyMzQ=\r\n");
        assert_eq!(res.code, 235);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
    }

    #[test]
    fn auth_login_param() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        let mut res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        res = session.process(b"auth login dGVzdA==\r\n"); // "test"
        assert_eq!(res, PASSWORD_AUTH_CHALLENGE);
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        res = session.process(b"MTIzNA==\r\n"); // "1234"
        assert_eq!(res.code, 235);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
    }

    #[test]
    fn bad_auth_plain_param() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        let mut res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        res = session.process(b"auth plain eGVzdAB0ZXN0ADEyMzQ=\r\n");
        assert_eq!(res.code, 535);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
    }

    #[test]
    fn bad_auth_login_param() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        let mut res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        res = session.process(b"auth login dGVzdA==\r\n"); // "test"
        assert_eq!(res, PASSWORD_AUTH_CHALLENGE);
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        res = session.process(b"YmFkLXBhc3N3b3Jk\r\n"); // "bad-password"
        assert_eq!(res.code, 535);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
    }

    #[test]
    fn auth_plain_challenge() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        let res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        let res = session.process(b"auth plain\r\n");
        assert_eq!(res.code, 334);
        if res != EMPTY_AUTH_CHALLENGE {
            panic!("Server did not send empty challenge");
        }
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        let res = session.process(b"dGVzdAB0ZXN0ADEyMzQ=\r\n");
        assert_eq!(res.code, 235);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
    }

    #[test]
    fn auth_login_challenge() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        let res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        let res = session.process(b"auth login\r\n");
        assert_eq!(res, USERNAME_AUTH_CHALLENGE);
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        let res = session.process(b"dGVzdA==\r\n"); // "test"
        assert_eq!(res, PASSWORD_AUTH_CHALLENGE);
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        let res = session.process(b"MTIzNA==\r\n"); // "1234"
        assert_eq!(res.code, 235);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
    }

    #[test]
    fn auth_without_tls() {
        let mut session = new_auth_session(true);
        let mut res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        res = session.process(b"auth plain dGVzdAB0ZXN0ADEyMzQ=\r\n");
        assert_eq!(res.code, 503);
    }

    #[test]
    fn auth_insecure_without_tls() {
        let addr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        let mut builder = SessionBuilder::new("some.domain");
        builder.enable_auth(AuthMechanism::Plain);
        builder.insecure_enable_plaintext_auth();
        let mut session = builder.build(addr, AuthHandler {});
        let mut res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
        res = session.process(b"auth plain dGVzdAB0ZXN0ADEyMzQ=\r\n");
        assert_eq!(res.code, 235);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
    }

    #[test]
    fn auth_insecure_without_auth() {
        let addr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        let mut builder = SessionBuilder::new("some.domain");
        builder.insecure_enable_plaintext_auth();
        let mut session = builder.build(addr, AuthHandler {});
        let mut res = session.process(b"ehlo a.domain\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::Hello);
        res = session.process(b"auth plain dGVzdAB0ZXN0ADEyMzQ=\r\n");
        assert_eq!(res.code, 503);
    }

    #[test]
    fn bad_auth_plain_challenge() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        session.process(b"ehlo a.domain\r\n");
        session.process(b"auth plain\r\n");
        let res = session.process(b"eGVzdAB0ZXN0ADEyMzQ=\r\n");
        assert_eq!(res.code, 535);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
    }

    #[test]
    fn bad_auth_login_username_challenge() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        session.process(b"ehlo a.domain\r\n");
        let res = session.process(b"auth login\r\n");
        assert_eq!(res, USERNAME_AUTH_CHALLENGE);
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        let res = session.process(b"YmFkLXVzZXJuYW1l\r\n"); // "bad-username"
        assert_eq!(res, PASSWORD_AUTH_CHALLENGE);
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        let res = session.process(b"MTIzNA==\r\n"); // "1234"
        assert_eq!(res.code, 535);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
    }

    #[test]
    fn bad_auth_login_password_challenge() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        session.process(b"ehlo a.domain\r\n");
        let res = session.process(b"auth login\r\n");
        assert_eq!(res, USERNAME_AUTH_CHALLENGE);
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        let res = session.process(b"dGVzdA==\r\n"); // "test"
        assert_eq!(res, PASSWORD_AUTH_CHALLENGE);
        assert_state!(session.fsm.current_state(), SmtpState::Auth);
        let res = session.process(b"YmFkLXBhc3N3b3Jk\r\n"); // "bad-password"
        assert_eq!(res.code, 535);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
    }

    #[test]
    fn rset_with_auth() {
        let mut session = new_auth_session(true);
        start_tls(&mut session);
        let res = session.process(b"ehlo some.domain\r\n");
        assert_eq!(res.code, 250);
        let res = session.process(b"auth plain dGVzdAB0ZXN0ADEyMzQ=\r\n");
        assert_eq!(res.code, 235);
        let res = session.process(b"mail from:<ship@sea.com>\r\n");
        assert_eq!(res.code, 250);
        let res = session.process(b"rset\r\n");
        assert_eq!(res.code, 250);
        assert_state!(session.fsm.current_state(), SmtpState::HelloAuth);
    }
}