mockforge-smtp 0.3.136

SMTP mocking support for MockForge
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//! Integration tests for SMTP server

use mockforge_smtp::{
    BehaviorConfig, MatchCriteria, SmtpConfig, SmtpFixture, SmtpResponse, SmtpServer,
    SmtpSpecRegistry, StorageConfig,
};
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpStream;
use tokio::time::{timeout, Duration};

/// Helper function to start SMTP server on a random port
async fn start_test_server() -> (SmtpServer, u16) {
    let config = SmtpConfig {
        port: 0, // Random port
        host: "127.0.0.1".to_string(),
        hostname: "test-smtp".to_string(),
        ..Default::default()
    };

    let mut registry = SmtpSpecRegistry::new();

    // Add a default catch-all fixture for testing
    let default_fixture = SmtpFixture {
        identifier: "default".to_string(),
        name: "Default Test Fixture".to_string(),
        description: "Default fixture for integration tests".to_string(),
        match_criteria: MatchCriteria {
            recipient_pattern: None,
            sender_pattern: None,
            subject_pattern: None,
            match_all: true, // Catch all emails
        },
        response: SmtpResponse {
            status_code: 250,
            message: "Message accepted for delivery".to_string(),
            delay_ms: 0,
        },
        auto_reply: None,
        storage: StorageConfig {
            save_to_mailbox: true,
            export_to_file: None,
        },
        behavior: BehaviorConfig::default(),
    };

    // Create a temporary directory and save the fixture
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let fixture_path = temp_dir.path().join("default.yaml");
    std::fs::write(&fixture_path, serde_yaml::to_string(&default_fixture).unwrap())
        .expect("Failed to write fixture");

    registry.load_fixtures(temp_dir.path()).expect("Failed to load fixtures");

    let port = find_available_port().await;
    let config_with_port = SmtpConfig { port, ..config };

    let server = SmtpServer::new(config_with_port, Arc::new(registry))
        .expect("Failed to create SMTP server");
    (server, port)
}

/// Find an available port for testing
async fn find_available_port() -> u16 {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("Failed to bind to random port");
    let port = listener.local_addr().unwrap().port();
    drop(listener);
    port
}

/// Connect to SMTP server and read greeting
async fn connect_and_read_greeting(
    port: u16,
) -> (
    BufReader<tokio::net::tcp::OwnedReadHalf>,
    tokio::net::tcp::OwnedWriteHalf,
    String,
) {
    let stream = timeout(Duration::from_secs(5), TcpStream::connect(format!("127.0.0.1:{}", port)))
        .await
        .expect("Connection timeout")
        .expect("Failed to connect");

    let (reader, writer) = stream.into_split();
    let mut reader = BufReader::new(reader);

    // Read greeting
    let mut greeting = String::new();
    timeout(Duration::from_secs(5), reader.read_line(&mut greeting))
        .await
        .expect("Greeting timeout")
        .expect("Failed to read greeting");

    (reader, writer, greeting)
}

#[tokio::test]
async fn test_smtp_server_starts_and_accepts_connections() {
    let (server, port) = start_test_server().await;

    // Start server in background
    tokio::spawn(async move {
        server.start().await.expect("Server failed to start");
    });

    // Give server time to start
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Try to connect
    let (_reader, _writer, greeting) = connect_and_read_greeting(port).await;

    assert!(greeting.starts_with("220"), "Expected SMTP greeting, got: {}", greeting);
    assert!(greeting.contains("test-smtp"), "Greeting should contain hostname");
}

#[tokio::test]
async fn test_smtp_basic_conversation() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    // EHLO command
    writer
        .write_all(b"EHLO client.example.com\r\n")
        .await
        .expect("Failed to write EHLO");

    // Read all EHLO response lines (it returns multiple lines)
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("Failed to read EHLO response");
        response.push_str(&line);

        // EHLO responses end with "250 " (with space) instead of "250-"
        if line.starts_with("250 ") {
            break;
        }
    }
    assert!(response.contains("250"), "Expected 250 response for EHLO");
    response.clear();

    // MAIL FROM command
    writer
        .write_all(b"MAIL FROM:<sender@example.com>\r\n")
        .await
        .expect("Failed to write MAIL FROM");
    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read MAIL FROM response");
    assert!(response.contains("250"), "Expected 250 OK for MAIL FROM");
    response.clear();

    // RCPT TO command
    writer
        .write_all(b"RCPT TO:<recipient@example.com>\r\n")
        .await
        .expect("Failed to write RCPT TO");
    reader.read_line(&mut response).await.expect("Failed to read RCPT TO response");
    assert!(response.contains("250"), "Expected 250 OK for RCPT TO");
    response.clear();

    // DATA command
    writer.write_all(b"DATA\r\n").await.expect("Failed to write DATA");
    reader.read_line(&mut response).await.expect("Failed to read DATA response");
    assert!(response.contains("354"), "Expected 354 response for DATA");
    response.clear();

    // Send email data
    writer
        .write_all(b"Subject: Test Email\r\n")
        .await
        .expect("Failed to write subject");
    writer.write_all(b"\r\n").await.expect("Failed to write blank line");
    writer
        .write_all(b"This is a test email.\r\n")
        .await
        .expect("Failed to write body");
    writer.write_all(b".\r\n").await.expect("Failed to write end of data");

    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read DATA completion response");
    assert!(
        response.contains("250") || response.contains("550"),
        "Expected 250 or 550 response after DATA, got: {}",
        response
    );
    response.clear();

    // QUIT command
    writer.write_all(b"QUIT\r\n").await.expect("Failed to write QUIT");
    reader.read_line(&mut response).await.expect("Failed to read QUIT response");
    assert!(response.contains("221"), "Expected 221 Bye response");
}

#[tokio::test]
async fn test_smtp_fixture_matching() {
    let mut registry = SmtpSpecRegistry::new();

    // Add a fixture that matches specific recipient
    let fixture = SmtpFixture {
        identifier: "test-fixture".to_string(),
        name: "Test Fixture".to_string(),
        description: "Test fixture for integration tests".to_string(),
        match_criteria: MatchCriteria {
            recipient_pattern: Some(r"^user.*@example\.com$".to_string()),
            sender_pattern: None,
            subject_pattern: None,
            match_all: false,
        },
        response: SmtpResponse {
            status_code: 250,
            message: "Test message accepted".to_string(),
            delay_ms: 0,
        },
        auto_reply: None,
        storage: StorageConfig {
            save_to_mailbox: true,
            export_to_file: None,
        },
        behavior: BehaviorConfig::default(),
    };

    // Manually add fixture
    let temp_dir = tempfile::tempdir().unwrap();
    let fixture_path = temp_dir.path().join("test.yaml");
    std::fs::write(&fixture_path, serde_yaml::to_string(&fixture).unwrap()).unwrap();

    registry.load_fixtures(temp_dir.path()).expect("Failed to load fixtures");

    // Test matching
    let matching_fixture =
        registry.find_matching_fixture("sender@test.com", "user123@example.com", "Test Subject");
    assert!(matching_fixture.is_some(), "Should find matching fixture");

    let non_matching =
        registry.find_matching_fixture("sender@test.com", "admin@example.com", "Test Subject");
    assert!(non_matching.is_none(), "Should not find non-matching fixture");
}

#[tokio::test]
async fn test_smtp_mailbox_storage() {
    use mockforge_smtp::StoredEmail;

    let registry = SmtpSpecRegistry::new();

    // Store an email
    let email = StoredEmail {
        id: "test-123".to_string(),
        from: "sender@example.com".to_string(),
        to: vec!["recipient@example.com".to_string()],
        subject: "Test Email".to_string(),
        body: "This is a test.".to_string(),
        headers: std::collections::HashMap::new(),
        received_at: chrono::Utc::now(),
        raw: None,
    };

    registry.store_email(email.clone()).expect("Failed to store email");

    // Retrieve emails
    let emails = registry.get_emails().expect("Failed to get emails");
    assert_eq!(emails.len(), 1, "Should have one email");
    assert_eq!(emails[0].from, "sender@example.com");
    assert_eq!(emails[0].subject, "Test Email");

    // Get specific email by ID
    let retrieved = registry.get_email_by_id("test-123").expect("Failed to get email by ID");
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().id, "test-123");

    // Clear mailbox
    registry.clear_mailbox().expect("Failed to clear mailbox");
    let emails_after_clear = registry.get_emails().expect("Failed to get emails");
    assert_eq!(emails_after_clear.len(), 0, "Mailbox should be empty");
}

#[tokio::test]
async fn test_smtp_mailbox_size_limit() {
    let registry = SmtpSpecRegistry::with_mailbox_size(2);

    // Store more emails than the limit
    for i in 0..5 {
        let email = mockforge_smtp::StoredEmail {
            id: format!("test-{}", i),
            from: "sender@example.com".to_string(),
            to: vec!["recipient@example.com".to_string()],
            subject: format!("Test Email {}", i),
            body: "Test".to_string(),
            headers: std::collections::HashMap::new(),
            received_at: chrono::Utc::now(),
            raw: None,
        };

        registry.store_email(email).expect("Failed to store email");
    }

    // Should only keep the last 2 emails
    let emails = registry.get_emails().expect("Failed to get emails");
    assert_eq!(emails.len(), 2, "Should only keep last 2 emails");
    assert_eq!(emails[0].subject, "Test Email 3");
    assert_eq!(emails[1].subject, "Test Email 4");
}

#[tokio::test]
async fn test_smtp_protocol_commands() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    // Test NOOP command
    writer.write_all(b"NOOP\r\n").await.expect("Failed to write NOOP");
    reader.read_line(&mut response).await.expect("Failed to read NOOP response");
    assert!(response.contains("250"), "NOOP should return 250 OK");
    response.clear();

    // Test HELP command
    writer.write_all(b"HELP\r\n").await.expect("Failed to write HELP");

    // Read all HELP response lines (it returns multiple lines)
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("Failed to read HELP response");
        response.push_str(&line);

        // HELP responses end with "214 " (with space) instead of "214-"
        if line.starts_with("214 ") {
            break;
        }
    }
    assert!(response.contains("214"), "HELP should return 214");
    response.clear();

    // Test RSET command
    writer
        .write_all(b"MAIL FROM:<sender@example.com>\r\n")
        .await
        .expect("Failed to write MAIL FROM");
    reader.read_line(&mut response).await.ok();
    response.clear();

    writer.write_all(b"RSET\r\n").await.expect("Failed to write RSET");
    reader.read_line(&mut response).await.expect("Failed to read RSET response");
    assert!(response.contains("250"), "RSET should return 250 OK");
}

#[tokio::test]
async fn test_smtp_hello_vs_ehlo() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    // Test HELLO
    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    writer
        .write_all(b"HELLO client.example.com\r\n")
        .await
        .expect("Failed to write HELLO");
    reader.read_line(&mut response).await.expect("Failed to read HELLO response");
    assert!(response.contains("250"), "HELLO should return 250");
    assert!(!response.contains("SIZE"), "HELLO should not list extensions");

    writer.write_all(b"QUIT\r\n").await.ok();

    // Test EHLO
    tokio::time::sleep(Duration::from_millis(50)).await;
    let (mut reader2, mut writer2, _greeting2) = connect_and_read_greeting(port).await;
    let mut response2 = String::new();

    writer2
        .write_all(b"EHLO client.example.com\r\n")
        .await
        .expect("Failed to write EHLO");

    // Read all EHLO response lines
    loop {
        let mut line = String::new();
        reader2.read_line(&mut line).await.expect("Failed to read line");
        response2.push_str(&line);

        // EHLO responses end with "250 " (with space) instead of "250-"
        if line.starts_with("250 ") {
            break;
        }
    }

    assert!(response2.contains("250"), "EHLO should return 250");
    // EHLO may contain extensions like SIZE, 8BITMIME, etc.
}

#[tokio::test]
async fn test_smtp_invalid_command() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    // Send invalid command
    writer
        .write_all(b"INVALID COMMAND\r\n")
        .await
        .expect("Failed to write invalid command");
    reader.read_line(&mut response).await.expect("Failed to read response");

    // Should return 502 (command not implemented) or 500 (syntax error)
    assert!(
        response.contains("502") || response.contains("500"),
        "Invalid command should return error code, got: {}",
        response
    );
}

#[tokio::test]
async fn test_smtp_error_handling_invalid_recipient() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    // EHLO
    writer
        .write_all(b"EHLO client.example.com\r\n")
        .await
        .expect("Failed to write EHLO");
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("Failed to read EHLO response");
        if line.starts_with("250 ") {
            break;
        }
    }

    // MAIL FROM
    writer
        .write_all(b"MAIL FROM:<sender@example.com>\r\n")
        .await
        .expect("Failed to write MAIL FROM");
    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read MAIL FROM response");
    assert!(response.contains("250"));
    response.clear();

    // RCPT TO with invalid format (missing @)
    writer
        .write_all(b"RCPT TO:<invalidrecipient>\r\n")
        .await
        .expect("Failed to write RCPT TO");
    reader.read_line(&mut response).await.expect("Failed to read RCPT TO response");
    // Should accept or reject based on fixture - our default fixture accepts all
    assert!(response.contains("250") || response.contains("550"));
    response.clear();

    // QUIT
    writer.write_all(b"QUIT\r\n").await.ok();
}

#[tokio::test]
async fn test_smtp_error_handling_missing_mail_from() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    // EHLO
    writer
        .write_all(b"EHLO client.example.com\r\n")
        .await
        .expect("Failed to write EHLO");
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("Failed to read EHLO response");
        if line.starts_with("250 ") {
            break;
        }
    }

    // Try RCPT TO without MAIL FROM
    writer
        .write_all(b"RCPT TO:<recipient@example.com>\r\n")
        .await
        .expect("Failed to write RCPT TO");
    reader.read_line(&mut response).await.expect("Failed to read RCPT TO response");
    // Should accept or reject - our implementation may allow this
    assert!(response.contains("250") || response.contains("550") || response.contains("503"));
    response.clear();

    // QUIT
    writer.write_all(b"QUIT\r\n").await.ok();
}

#[tokio::test]
async fn test_smtp_error_handling_missing_rcpt_to() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    // EHLO
    writer
        .write_all(b"EHLO client.example.com\r\n")
        .await
        .expect("Failed to write EHLO");
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("Failed to read EHLO response");
        if line.starts_with("250 ") {
            break;
        }
    }

    // MAIL FROM
    writer
        .write_all(b"MAIL FROM:<sender@example.com>\r\n")
        .await
        .expect("Failed to write MAIL FROM");
    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read MAIL FROM response");
    assert!(response.contains("250"));
    response.clear();

    // Try DATA without RCPT TO
    writer.write_all(b"DATA\r\n").await.expect("Failed to write DATA");
    reader.read_line(&mut response).await.expect("Failed to read DATA response");
    // SMTP allows DATA without RCPT TO in some implementations
    assert!(response.contains("354"));
    response.clear();

    // QUIT
    writer.write_all(b"QUIT\r\n").await.ok();
}

#[tokio::test]
async fn test_smtp_connection_timeout() {
    let config = SmtpConfig {
        port: 0,
        host: "127.0.0.1".to_string(),
        hostname: "test-smtp".to_string(),
        timeout_secs: 1, // Very short timeout
        ..Default::default()
    };

    let port = find_available_port().await;
    let config_with_port = SmtpConfig { port, ..config };
    let registry = Arc::new(SmtpSpecRegistry::new());
    let server = SmtpServer::new(config_with_port, registry).expect("Failed to create SMTP server");

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let stream = TcpStream::connect(format!("127.0.0.1:{}", port))
        .await
        .expect("Failed to connect");

    let (_reader, mut writer) = stream.into_split();

    // Send EHLO but don't send more data - should timeout
    writer
        .write_all(b"EHLO client.example.com\r\n")
        .await
        .expect("Failed to write EHLO");

    // Wait longer than timeout
    tokio::time::sleep(Duration::from_secs(3)).await;

    // Connection should be closed by server due to timeout
    // This is hard to test directly, but we can verify the server handles timeouts
}

#[tokio::test]
async fn test_smtp_edge_case_multiple_recipients() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    // EHLO
    writer
        .write_all(b"EHLO client.example.com\r\n")
        .await
        .expect("Failed to write EHLO");
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("Failed to read EHLO response");
        if line.starts_with("250 ") {
            break;
        }
    }

    // MAIL FROM
    writer
        .write_all(b"MAIL FROM:<sender@example.com>\r\n")
        .await
        .expect("Failed to write MAIL FROM");
    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read MAIL FROM response");
    assert!(response.contains("250"));
    response.clear();

    // Multiple RCPT TO commands
    writer
        .write_all(b"RCPT TO:<recipient1@example.com>\r\n")
        .await
        .expect("Failed to write RCPT TO 1");
    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read RCPT TO 1 response");
    assert!(response.contains("250"));
    response.clear();

    writer
        .write_all(b"RCPT TO:<recipient2@example.com>\r\n")
        .await
        .expect("Failed to write RCPT TO 2");
    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read RCPT TO 2 response");
    assert!(response.contains("250"));
    response.clear();

    // DATA
    writer.write_all(b"DATA\r\n").await.expect("Failed to write DATA");
    reader.read_line(&mut response).await.expect("Failed to read DATA response");
    assert!(response.contains("354"));
    response.clear();

    // Send email with multiple recipients in headers
    writer
        .write_all(b"To: recipient1@example.com, recipient2@example.com\r\n")
        .await
        .expect("Failed to write To header");
    writer
        .write_all(b"Subject: Test Multiple Recipients\r\n")
        .await
        .expect("Failed to write subject");
    writer.write_all(b"\r\n").await.expect("Failed to write blank line");
    writer
        .write_all(b"This email has multiple recipients.\r\n")
        .await
        .expect("Failed to write body");
    writer.write_all(b".\r\n").await.expect("Failed to write end of data");

    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read DATA completion response");
    assert!(response.contains("250"));
    response.clear();

    // QUIT
    writer.write_all(b"QUIT\r\n").await.ok();
}

#[tokio::test]
async fn test_smtp_edge_case_unicode_content() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    // EHLO
    writer
        .write_all(b"EHLO client.example.com\r\n")
        .await
        .expect("Failed to write EHLO");
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("Failed to read EHLO response");
        if line.starts_with("250 ") {
            break;
        }
    }

    // MAIL FROM
    writer
        .write_all(b"MAIL FROM:<sender@example.com>\r\n")
        .await
        .expect("Failed to write MAIL FROM");
    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read MAIL FROM response");
    assert!(response.contains("250"));
    response.clear();

    // RCPT TO
    writer
        .write_all(b"RCPT TO:<recipient@example.com>\r\n")
        .await
        .expect("Failed to write RCPT TO");
    reader.read_line(&mut response).await.expect("Failed to read RCPT TO response");
    assert!(response.contains("250"));
    response.clear();

    // DATA
    writer.write_all(b"DATA\r\n").await.expect("Failed to write DATA");
    reader.read_line(&mut response).await.expect("Failed to read DATA response");
    assert!(response.contains("354"));
    response.clear();

    // Send email with Unicode content
    writer
        .write_all(b"Subject: =?UTF-8?B?VGVzdCB3aXRoIMO2w7bDp8O8?=\r\n")
        .await
        .expect("Failed to write Unicode subject");
    writer.write_all(b"\r\n").await.expect("Failed to write blank line");
    writer
        .write_all(b"This email contains Unicode: \xc3\xb6\xc3\xa4\xc3\xbc\r\n")
        .await
        .expect("Failed to write Unicode body");
    writer.write_all(b".\r\n").await.expect("Failed to write end of data");

    reader
        .read_line(&mut response)
        .await
        .expect("Failed to read DATA completion response");
    assert!(response.contains("250"));
    response.clear();

    // QUIT
    writer.write_all(b"QUIT\r\n").await.ok();
}

#[tokio::test]
async fn test_smtp_load_concurrent_connections() {
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let mut handles = Vec::new();

    // Create 10 concurrent connections
    for i in 0..10 {
        let handle = tokio::spawn(async move {
            let stream = TcpStream::connect(format!("127.0.0.1:{}", port))
                .await
                .expect("Failed to connect");

            let (reader, mut writer) = stream.into_split();
            let mut reader = BufReader::new(reader);
            let mut response = String::new();

            // Read greeting
            reader.read_line(&mut response).await.expect("Failed to read greeting");
            response.clear();

            // EHLO
            writer
                .write_all(format!("EHLO client{}.example.com\r\n", i).as_bytes())
                .await
                .expect("Failed to write EHLO");
            loop {
                let mut line = String::new();
                reader.read_line(&mut line).await.expect("Failed to read EHLO response");
                if line.starts_with("250 ") {
                    break;
                }
            }

            // MAIL FROM
            writer
                .write_all(format!("MAIL FROM:<sender{}@example.com>\r\n", i).as_bytes())
                .await
                .expect("Failed to write MAIL FROM");
            reader
                .read_line(&mut response)
                .await
                .expect("Failed to read MAIL FROM response");
            assert!(response.contains("250"));
            response.clear();

            // RCPT TO
            writer
                .write_all(format!("RCPT TO:<recipient{}@example.com>\r\n", i).as_bytes())
                .await
                .expect("Failed to write RCPT TO");
            reader.read_line(&mut response).await.expect("Failed to read RCPT TO response");
            assert!(response.contains("250"));
            response.clear();

            // QUIT
            writer.write_all(b"QUIT\r\n").await.expect("Failed to write QUIT");
            reader.read_line(&mut response).await.expect("Failed to read QUIT response");
            assert!(response.contains("221"));
        });

        handles.push(handle);
    }

    // Wait for all connections to complete
    for handle in handles {
        handle.await.expect("Connection failed");
    }
}

#[tokio::test]
async fn test_smtp_starttls_without_cert_returns_454() {
    // With `enable_starttls=false` (the default) we advertise
    // STARTTLS in EHLO but reject the actual upgrade with 454 per
    // RFC 3207. Previously we replied 220 Ready and kept the socket
    // plaintext, which silently corrupted any real TLS client —
    // `feat/smtp-starttls-upgrade` fixes that: when no cert is
    // configured, we refuse the upgrade politely instead.
    let (server, port) = start_test_server().await;

    tokio::spawn(async move {
        server.start().await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let (mut reader, mut writer, _greeting) = connect_and_read_greeting(port).await;
    let mut response = String::new();

    writer.write_all(b"STARTTLS\r\n").await.expect("Failed to write STARTTLS");
    reader.read_line(&mut response).await.expect("Failed to read STARTTLS response");
    assert!(
        response.starts_with("454"),
        "STARTTLS without a configured cert must return 454 TLS not available; got: {response:?}"
    );

    // QUIT
    writer.write_all(b"QUIT\r\n").await.ok();
}