Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/tests/email.rs

3.3 KiB, 28 runs

created by r1870400018:613, 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 whole suite exercises the tokio-driven mbox reader, so it belongs to the
2// async build alone; the lean build compiles no test binary here.
3#![cfg(feature = "async")]
4
5use oxedyne_fe2o3_net::{
6 //dns::Fqdn,
7 email::{
8 file::{
9 MboxEmailIterator,
10 },
11 msg::{
12 EmailHeader,
13 EmailMessage,
14 },
15 },
16 //smtp::{
17 // cmd::SmtpCommand,
18 //},
19};
20
21use oxedyne_fe2o3_core::{
22 prelude::*,
23 test::test_it,
24};
25use oxedyne_fe2o3_text::string::Stringer;
26
27use std::{
28 path::Path,
29 pin::Pin,
30};
31
32
33pub fn test_email(filter: &'static str) -> Outcome<()> {
34
35 res!(test_it(filter, &["Email message 000", "all", "email"], || {
36
37 // Note: Every line ends with an implicit \n character.
38 let wire = "Received: from servera.example.org (servera.example.org [192.168.0.1])\r
39 by serverb.example.com (ExampleMTA) with ESMTP id 12345678\r
40 for <recipient@example.com>; Fri, 11 Jun 2023 09:30:00 -0500 (EST)\r
41From: Sender <sender@example.org>\r
42To: Recipient <recipient@example.com>\r
43Subject: Test message\r
44Date: Fri, 11 Jun 2023 09:30:00 -0500\r
45Message-ID: <12345.67890@servera.example.org>\r
46\r
47This is a test message sent from Server A to Server B.\r
48\r
49Best regards,\r
50Sender\r
51.\r\n";
52
53 let byts = wire.as_bytes();
54 let mut stream = std::io::Cursor::new(byts);
55 let rt = res!(tokio::runtime::Runtime::new());
56
57 let result = rt.block_on(async {
58 EmailMessage::read(&mut Pin::new(&mut stream)).await
59 });
60
61 let email = res!(result);
62 debug!("{:?}", email);
63 let expected = EmailMessage {
64 from: fmt!("Sender <sender@example.org>"),
65 to: vec![fmt!("Recipient <recipient@example.com>")],
66 subject: fmt!("Test message"),
67 body: fmt!("This is a test message sent from Server A to Server B.\n\nBest regards,\nSender\n"),
68 headers: vec![
69 EmailHeader::Received(fmt!("from servera.example.org (servera.example.org [192.168.0.1]) by serverb.example.com (ExampleMTA) with ESMTP id 12345678 for <recipient@example.com>; Fri, 11 Jun 2023 09:30:00 -0500 (EST)")),
70 EmailHeader::Date(fmt!("Fri, 11 Jun 2023 09:30:00 -0500")),
71 ],
72 };
73 for line in Stringer::new(fmt!("{:?}", email)).to_lines(" ") {
74 test!("{}", line);
75 }
76 req!(expected, email, "(L: expected, R: actual)");
77 Ok(())
78 }));
79
80 res!(test_it(filter, &["Mbox reader 000", "all", "email", "mbox"], || {
81
82 let rt = res!(tokio::runtime::Runtime::new());
83
84 let home = res!(std::env::var("HOME"));
85 let path = Path::new(&home).join("tmp/Inbox");
86 let result = rt.block_on(async {
87 MboxEmailIterator::new(path).await
88 });
89
90 let mut mbox_iter = res!(result);
91 match mbox_iter.next() {
92 Some(Ok(email)) => {
93 test!("{:?}", email);
94 }
95 Some(Err(e)) => {
96 return Err(err!(e,
97 "While reading first email from mbox file.";
98 Test, IO, File, Read));
99 }
100 None => {
101 return Err(err!(
102 "Expected to read an email from the mbox file.";
103 Test, Missing, Data));
104 }
105 }
106 Ok(())
107 }));
108
109 Ok(())
110}