daaki-smtp 0.2.0

An async SMTP client library
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
use super::*;

#[test]
fn smtp_types_module_and_core_types_carry_rfc_section_citations() {
    let source = include_str!("mod.rs")
        .split("#[cfg(test)]")
        .next()
        .unwrap_or("");

    for required in [
        "//! - RFC 5321 (SMTP)",
        "//! - RFC 2033 (LMTP)",
        "//! - RFC 2034 (Enhanced Status Codes)",
        "//! - RFC 4954 (SMTP AUTH)",
        "/// A parsed SMTP server response (RFC 5321 Section 4.2).",
        "/// Enhanced status code (RFC 1893 Section 2 / RFC 2034 Section 3).",
        "/// SMTP server extension capabilities, parsed from EHLO response",
        "RFC 5321 Section 4.1.1.1",
        "/// SMTP authentication mechanism (RFC 4954 Section 3 / RFC 4422 Section 3.1).",
        "/// Server capabilities parsed from EHLO response",
    ] {
        assert!(
            source.contains(required),
            "smtp::types must cite RFC sections for module docs and core protocol types; missing: {required}"
        );
    }
}

#[test]
fn smtp_response_classification() {
    let ok = SmtpResponse {
        code: 250,
        enhanced_code: None,
        lines: vec!["OK".into()],
    };
    assert!(ok.is_success());
    assert!(!ok.is_transient_error());
    assert!(!ok.is_permanent_error());

    let transient = SmtpResponse {
        code: 421,
        enhanced_code: None,
        lines: vec!["Try again later".into()],
    };
    assert!(transient.is_transient_error());
    assert!(!transient.is_success());

    let permanent = SmtpResponse {
        code: 550,
        enhanced_code: None,
        lines: vec!["Mailbox not found".into()],
    };
    assert!(permanent.is_permanent_error());
    assert!(!permanent.is_transient_error());
}

#[test]
fn smtp_response_text() {
    let resp = SmtpResponse {
        code: 250,
        enhanced_code: None,
        lines: vec!["line1".into(), "line2".into()],
    };
    assert_eq!(resp.text(), "line1\nline2");
}

#[test]
fn server_capabilities_starttls() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::StartTls, SmtpExtension::Pipelining],
    };
    assert!(caps.supports_starttls());
    assert!(!caps.supports_chunking());
}

#[test]
fn server_capabilities_auth() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Auth(vec![
            AuthMechanism::Plain,
            AuthMechanism::XOAuth2,
        ])],
    };
    assert!(caps.supports_auth(&AuthMechanism::Plain));
    assert!(caps.supports_auth(&AuthMechanism::XOAuth2));
    assert!(!caps.supports_auth(&AuthMechanism::Other("CRAM-MD5".into())));
}

#[test]
fn intermediate_response() {
    let resp = SmtpResponse {
        code: 354,
        enhanced_code: None,
        lines: vec!["Start mail input".into()],
    };
    assert!(resp.is_intermediate());
    assert!(!resp.is_success());
}

#[test]
fn supports_8bitmime() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::EightBitMime],
    };
    assert!(caps.supports_8bitmime());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_8bitmime());
}

#[test]
fn supports_binarymime() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::BinaryMime],
    };
    assert!(caps.supports_binarymime());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_binarymime());
}

#[test]
fn supports_8bit_or_binary() {
    // RFC 1652 / RFC 3030: either extension satisfies the 8-bit requirement.
    let with_8bit = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::EightBitMime],
    };
    assert!(with_8bit.supports_8bit_or_binary());

    let with_binary = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::BinaryMime],
    };
    assert!(with_binary.supports_8bit_or_binary());

    let with_both = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::EightBitMime, SmtpExtension::BinaryMime],
    };
    assert!(with_both.supports_8bit_or_binary());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_8bit_or_binary());
}

#[test]
fn supports_pipelining() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Pipelining],
    };
    assert!(caps.supports_pipelining());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_pipelining());
}

#[test]
fn supports_smtputf8() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::SmtpUtf8],
    };
    assert!(caps.supports_smtputf8());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_smtputf8());
}

// ── RFC 4954 §3 / RFC 4422 §3.1 — mechanism names are case-insensitive ──

#[test]
fn supports_auth_case_insensitive_other_mechanism() {
    // RFC 4954 Section 3 / RFC 4422 Section 3.1: SASL mechanism
    // names are case-insensitive. supports_auth must match
    // Other("login") against a stored Other("LOGIN") and vice versa.
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Auth(vec![
            AuthMechanism::Other("LOGIN".into()),
            AuthMechanism::Other("CRAM-MD5".into()),
        ])],
    };
    // Exact case — should match.
    assert!(
        caps.supports_auth(&AuthMechanism::Other("LOGIN".into())),
        "exact case must match"
    );
    // Different case — must still match per RFC 4954 Section 3.
    assert!(
        caps.supports_auth(&AuthMechanism::Other("login".into())),
        "RFC 4954 Section 3: mechanism names are case-insensitive; \
         'login' must match stored 'LOGIN'"
    );
    assert!(
        caps.supports_auth(&AuthMechanism::Other("Login".into())),
        "RFC 4954 Section 3: mixed-case 'Login' must match stored 'LOGIN'"
    );
    assert!(
        caps.supports_auth(&AuthMechanism::Other("cram-md5".into())),
        "RFC 4954 Section 3: 'cram-md5' must match stored 'CRAM-MD5'"
    );
    // Non-existent mechanism — must not match.
    assert!(
        !caps.supports_auth(&AuthMechanism::Other("NTLM".into())),
        "non-existent mechanism must not match"
    );
}

// ── RFC 4954 §3 — cross-variant mechanism name matching ──────────

#[test]
fn eq_mechanism_cross_variant_other_plain() {
    // RFC 4954 Section 3 / RFC 4422 Section 3.1: SASL mechanism
    // names are case-insensitive. Other("PLAIN") must match the
    // dedicated Plain variant, because they represent the same
    // SASL mechanism regardless of how the enum was constructed.
    assert!(
        AuthMechanism::Other("PLAIN".into()).eq_mechanism(&AuthMechanism::Plain),
        "Other(\"PLAIN\") must match Plain (RFC 4954 Section 3)"
    );
    assert!(
        AuthMechanism::Plain.eq_mechanism(&AuthMechanism::Other("plain".into())),
        "Plain must match Other(\"plain\") (RFC 4954 Section 3)"
    );
}

#[test]
fn auth_mechanism_public_equality_is_case_insensitive() {
    assert_eq!(
        AuthMechanism::Other("plain".into()),
        AuthMechanism::Plain,
        "public AuthMechanism equality must treat SASL names case-insensitively"
    );
}

#[test]
fn auth_mechanism_public_hash_is_case_insensitive() {
    let mut set = std::collections::HashSet::new();
    set.insert(AuthMechanism::Other("LOGIN".into()));
    set.insert(AuthMechanism::Login);
    assert_eq!(
        set.len(),
        1,
        "equivalent SASL mechanisms must hash identically for HashSet/HashMap use"
    );
}

#[test]
fn eq_mechanism_cross_variant_other_xoauth2() {
    // Same cross-variant matching for XOAUTH2.
    assert!(
        AuthMechanism::Other("XOAUTH2".into()).eq_mechanism(&AuthMechanism::XOAuth2),
        "Other(\"XOAUTH2\") must match XOAuth2"
    );
    assert!(
        AuthMechanism::XOAuth2.eq_mechanism(&AuthMechanism::Other("xoauth2".into())),
        "XOAuth2 must match Other(\"xoauth2\")"
    );
}

#[test]
fn supports_auth_cross_variant_other_plain() {
    // When the server advertises PLAIN (stored as AuthMechanism::Plain),
    // querying with Other("PLAIN") must return true.
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Auth(vec![AuthMechanism::Plain])],
    };
    assert!(
        caps.supports_auth(&AuthMechanism::Other("PLAIN".into())),
        "RFC 4954 Section 3: Other(\"PLAIN\") query must match \
         stored Plain variant"
    );
    assert!(
        caps.supports_auth(&AuthMechanism::Other("plain".into())),
        "RFC 4954 Section 3: Other(\"plain\") query must match \
         stored Plain variant (case-insensitive)"
    );
}

#[test]
fn supports_chunking() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Chunking],
    };
    assert!(caps.supports_chunking());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_chunking());
}

#[test]
fn supports_size() {
    // SIZE with a limit (RFC 1870 Section 2).
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Size(Some(10_485_760))],
    };
    assert!(caps.supports_size());

    // SIZE without a limit (server advertises SIZE with no value).
    let caps_no_limit = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Size(None)],
    };
    assert!(caps_no_limit.supports_size());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_size());
}

#[test]
fn supports_sasl_ir() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::SaslIr],
    };
    assert!(caps.supports_sasl_ir());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_sasl_ir());
}

#[test]
fn supports_enhanced_status_codes() {
    // RFC 2034 Section 2: the server advertises ENHANCEDSTATUSCODES
    // in its EHLO response. There must be a convenience method to
    // check for this extension, consistent with all other extensions.
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::EnhancedStatusCodes],
    };
    assert!(caps.supports_enhanced_status_codes());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_enhanced_status_codes());
}

#[test]
fn size_limit_with_value() {
    // RFC 1870 Section 2: server advertises a numeric size limit.
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Size(Some(10_485_760))],
    };
    assert_eq!(caps.size_limit(), Some(10_485_760));
}

#[test]
fn size_limit_without_value() {
    // RFC 1870 Section 2: server advertises SIZE with no numeric limit.
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Size(None)],
    };
    assert_eq!(caps.size_limit(), None);
}

#[test]
fn size_limit_not_advertised() {
    let empty = ServerCapabilities::default();
    assert_eq!(empty.size_limit(), None);
}

// ── DSN — RFC 3461 ────────────────────────────────────────────────

#[test]
fn supports_dsn() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Dsn],
    };
    assert!(caps.supports_dsn());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_dsn());
}

// ── REQUIRETLS — RFC 8689 ──────────────────────────────────────────

#[test]
fn supports_requiretls() {
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::RequireTls],
    };
    assert!(caps.supports_requiretls());

    let empty = ServerCapabilities::default();
    assert!(!empty.supports_requiretls());
}

// ── AuthMechanism::Login — de-facto standard AUTH LOGIN ───────────

#[test]
fn supports_auth_login() {
    // AUTH LOGIN is a de-facto standard (draft-murchison-sasl-login).
    // The dedicated Login variant must be detected by supports_auth.
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Auth(vec![
            AuthMechanism::Plain,
            AuthMechanism::Login,
        ])],
    };
    assert!(caps.supports_auth(&AuthMechanism::Login));
    assert!(caps.supports_auth(&AuthMechanism::Plain));
}

#[test]
fn eq_mechanism_login_identity() {
    // Login variant must match itself.
    assert!(AuthMechanism::Login.eq_mechanism(&AuthMechanism::Login));
}

#[test]
fn eq_mechanism_cross_variant_other_login() {
    // RFC 4954 Section 3 / RFC 4422 Section 3.1: SASL mechanism
    // names are case-insensitive. Other("LOGIN") must match the
    // dedicated Login variant.
    assert!(
        AuthMechanism::Other("LOGIN".into()).eq_mechanism(&AuthMechanism::Login),
        "Other(\"LOGIN\") must match Login"
    );
    assert!(
        AuthMechanism::Login.eq_mechanism(&AuthMechanism::Other("login".into())),
        "Login must match Other(\"login\") (case-insensitive)"
    );
    assert!(
        AuthMechanism::Login.eq_mechanism(&AuthMechanism::Other("Login".into())),
        "Login must match Other(\"Login\") (mixed case)"
    );
}

#[test]
fn supports_auth_cross_variant_other_login() {
    // When the server advertises LOGIN (stored as AuthMechanism::Login),
    // querying with Other("LOGIN") must return true.
    let caps = ServerCapabilities {
        greeting_name: "mail.example.com".into(),
        extensions: vec![SmtpExtension::Auth(vec![AuthMechanism::Login])],
    };
    assert!(
        caps.supports_auth(&AuthMechanism::Other("LOGIN".into())),
        "Other(\"LOGIN\") query must match stored Login variant"
    );
    assert!(
        caps.supports_auth(&AuthMechanism::Other("login".into())),
        "Other(\"login\") query must match stored Login variant"
    );
}

#[test]
fn eq_mechanism_login_does_not_match_plain() {
    // Login and Plain are distinct mechanisms.
    assert!(!AuthMechanism::Login.eq_mechanism(&AuthMechanism::Plain));
    assert!(!AuthMechanism::Plain.eq_mechanism(&AuthMechanism::Login));
}

// ── is_data_ready — RFC 5321 Section 4.1.1.4 ─────────────────────

#[test]
fn is_data_ready_354() {
    // RFC 5321 Section 4.1.1.4: 354 is the only valid intermediate
    // response to the DATA command.
    let resp = SmtpResponse {
        code: 354,
        enhanced_code: None,
        lines: vec!["Start mail input".into()],
    };
    assert!(resp.is_data_ready());
}

#[test]
fn is_data_ready_rejects_other_3xx() {
    // RFC 5321 Section 4.1.1.4: other 3xx codes (e.g. 355) are not
    // valid DATA intermediate responses. is_intermediate() would
    // incorrectly accept them; is_data_ready() must not.
    let resp_355 = SmtpResponse {
        code: 355,
        enhanced_code: None,
        lines: vec!["Not a real code".into()],
    };
    assert!(!resp_355.is_data_ready());
    assert!(resp_355.is_intermediate(), "355 is still a 3xx code");

    let resp_300 = SmtpResponse {
        code: 300,
        enhanced_code: None,
        lines: vec!["Not a real code".into()],
    };
    assert!(!resp_300.is_data_ready());
}

#[test]
fn is_data_ready_rejects_non_3xx() {
    // Success (2xx), transient error (4xx), and permanent error (5xx)
    // codes must not be treated as DATA-ready.
    for code in [250, 450, 550] {
        let resp = SmtpResponse {
            code,
            enhanced_code: None,
            lines: vec!["test".into()],
        };
        assert!(!resp.is_data_ready(), "code {code} must not be data-ready");
    }
}