melib/mbox: add mbox parsing test

pull/239/head
Manos Pitsidianakis 2023-07-03 10:57:42 +03:00
parent e8e49e741b
commit 1bcc0bbece
Signed by: Manos Pitsidianakis
GPG Key ID: 7729C7707F7E09D0
10 changed files with 137 additions and 46 deletions

View File

@ -31,7 +31,7 @@ required-features = ["melib/imap_backend"]
async-task = "^4.2.0"
bitflags = "1.0"
crossbeam = { version = "^0.8" }
flate2 = { version = "1.0.16", optional = true }
flate2 = { version = "1", optional = true }
futures = "0.3.5"
indexmap = { version = "^1.6", features = ["serde-1", ] }
libc = { version = "0.2.125", default-features = false, features = ["extra_traits",] }
@ -59,12 +59,13 @@ xdg = "2.1.0"
notify-rust = { version = "^4", default-features = false, features = ["dbus", ], optional = true }
[build-dependencies]
flate2 = { version = "1.0.16", optional = true }
flate2 = { version = "1", optional = true }
proc-macro2 = "1.0.37"
quote = "^1.0"
syn = { version = "1.0.92", features = [] }
[dev-dependencies]
flate2 = { version = "1" }
regex = "1"
tempfile = "3.3"

View File

@ -341,7 +341,7 @@ impl Envelope {
}
pub fn from_bytes(bytes: &[u8], flags: Option<Flag>) -> Result<Self> {
let mut e = Self::new(EnvelopeHash::from_bytes(bytes));
let mut e = Self::new(EnvelopeHash::from_bytes(bytes.trim()));
let res = e.populate_headers(bytes).ok();
if res.is_some() {
if let Some(f) = flags {

Binary file not shown.

Binary file not shown.

View File

@ -1,41 +0,0 @@
Subject:
From:
To:
Cc:
Bcc:
MIME-Version: 1.0
Content-Type: multipart/mixed; charset="utf-8"; boundary="bzz_bzz__bzz__"
This is a MIME formatted message with attachments. Use a MIME-compliant client to view it properly.
--bzz_bzz__bzz__
hello world.
--bzz_bzz__bzz__
Content-Type: image/gif; name="test_image.gif"; charset="utf-8"
Content-Disposition: attachment
Content-Transfer-Encoding: base64
R0lGODdhKAAXAOfZAAABzAADzQAEzgQFtBEAxAAGxBcAxwALvRcFwAAPwBcLugATuQEUuxoNuxYQ
sxwOvAYVvBsStSAVtx8YsRUcuhwhth4iuCQsyDAwuDc1vTc3uDg4uT85rkc9ukJBvENCvURGukdF
wUVKt0hLuUxPvVZSvFlYu1hbt2BZuFxdul5joGhqlnNuf3FvlnBvwXJyt3Jxw3N0oXx1gH12gV99
z317f3N7spFxwHp5wH99gYB+goF/g25+26tziIOBhWqD3oiBjICAuudkjIN+zHeC2n6Bzc1vh4eF
iYaBw8F0kImHi4KFxYyHmIWIvI2Lj4uIvYaJyY+IuJGMi5iJl4qKxZSMmIuLxpONnpGPk42NvI2M
1LKGl46OvZePm5ORlZiQnJqSnpaUmLyJnJuTn5iVmZyUoJGVyZ2VoZSVw5iXoZmWrO18rJiUyp6W
opuYnKaVnZ+Xo5yZncaMoaCYpJiaqo+Z2Z2annuf5qGZpa2WoJybpZmayZ2Z0KCZypydrZ6dp6Cd
oZ6a0aGay5ucy5+eqKGeouWMgp+b0qKbzKCfqdqPnp2ezaGgqqOgpKafqrScpp+gz6ajqKujr62j
qayksKmmq62lsaiosqqorOyWnaqqtKeqzLGptaurta2rr7Kqtq+ssLOrt6+uuLGusuqhfbWtubCv
ubKvs7GwurOwtPSazbevu+ali7SxtbiwvOykjLOyvLWytuCmqOankrSzvbazuLmyvrW0vre0uba1
wLi1ury0wLm2u721wbe3wbq3vMC2vLi4wr+3w7m5w8C4xLi6yry6vsG5xbu7xcC6zMK6xry8xry+
u8O7x729x8C9wb++yMG+wsO+vMK/w8a+y8e/zMnBzcXH18nL2///////////////////////////
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////ywAAAAAKAAXAAAI/gBP4Cjh
IYMLEh0w4EgBgsMLEyFGFBEB5cOFABgzatS4AVssZAOsLOHCxooVMzCyoNmzaBOkJlS0VEDyZMjG
mxk3XOMF60CDBgsoPABK9KcDCRImPCiQYAECAgQCRMU4VSrGCjFarBgUSJCgQ10FBTrkNRCfPnz4
dA3UNa1btnDZqgU7Ntqzu3ej2X2mFy9eaHuhNRtMGJrhwYYN930G2K7eaNIY34U2mfJkwpgzI9Yr
GBqwR2KSvAlMOXHnw5pTNzPdLNoWIWtU9XjGjDEYS8LAlFm1SrVvzIKj5TH0KpORSZOryPgCZgqL
Ob+jG0YVRBErUrOiiGJ8KxgtYsh27xWL/tswnTtEbsiRVYdJNMHk4yOGhswGjR88UKjQ9Ey+/8TL
XKKGGn7Akph/8XX2WDTTcAYfguVt9hhrEPqmzIOJ3VUheb48WJiHG6amC4i+WVJKKCimqGIoYxyj
WWK8kKjaJ9bA18sxvXjYhourmbbMMrjI+OIn1QymDCVXANGFK4S1gQw0PxozzC+33FLLKUJq9gk1
gyWDhyNwrMLkYGUEM4wvuLRiCiieXIJJJVlmJskcZ9TZRht1lnFGGmTMkMoonVQSSSOFAGJHHI0w
ouiijDaaCCGQRgrpH3q4QYYXWDihxBE+7KCDDjnUIEVAADs=
--bzz_bzz__bzz__--

Binary file not shown.

View File

@ -1,7 +1,31 @@
/*
* meli
*
* Copyright 2019 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
use std::io::Read;
use flate2::bufread::GzDecoder;
use melib::email::Draft;
#[test]
fn build_draft() {
fn test_build_draft() {
let mut new_draft = Draft::default();
let attachment = melib::email::attachment_from_file(&"./tests/test_image.gif")
.expect("Could not open test_image.gif.");
@ -17,5 +41,10 @@ fn build_draft() {
let boundary_str = &boundary["bzz_bzz__bzz__".len()..];
let raw = raw.replace(boundary_str, "");
assert_eq!(include_str!("generated_email.eml"), &raw);
let mut gz = GzDecoder::new(include_bytes!("generated_email.eml.gz").as_slice());
let mut s = String::new();
gz.read_to_string(&mut s).unwrap();
assert_eq!(&s, &raw);
}

102
tests/mbox_parse.rs 100644
View File

@ -0,0 +1,102 @@
/*
* meli
*
* Copyright 2019 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
use std::{
collections::HashMap,
io::Read,
sync::{Arc, Mutex},
};
use flate2::bufread::GzDecoder;
use melib::{mbox::*, Envelope, Result};
#[test]
fn test_mbox_parse() {
fn gz_to_string(bytes: &'static [u8]) -> String {
let mut gz = GzDecoder::new(bytes);
let mut s = String::new();
gz.read_to_string(&mut s).unwrap();
s
}
{
let sha1dc_diet_op =
gz_to_string(include_bytes!("PATCH-Put-sha1dc-on-a-diet_op.mbox.gz").as_slice());
let sha1dc_diet_thread =
gz_to_string(include_bytes!("PATCH-Put-sha1dc-on-a-diet.mbox.gz").as_slice());
let message_iter = MessageIterator {
index: Arc::new(Mutex::new(HashMap::default())),
input: sha1dc_diet_thread.as_bytes(),
offset: 0,
file_offset: 0,
format: Some(MboxFormat::MboxCl2),
};
let envelopes: Vec<Envelope> = message_iter.collect::<Result<Vec<_>>>().unwrap();
assert_eq!(envelopes.len(), 36);
let mut original_post: Vec<Envelope> = MessageIterator {
index: Arc::new(Mutex::new(HashMap::default())),
input: sha1dc_diet_op.as_bytes(),
offset: 0,
file_offset: 0,
format: Some(MboxFormat::MboxCl2),
}
.collect::<Result<Vec<_>>>()
.unwrap();
assert_eq!(original_post.len(), 1);
let original_post = original_post.pop().unwrap();
assert_eq!(&original_post, &envelopes[0]);
}
{
let git_am_op = gz_to_string(
include_bytes!("git-am-breakage-with-MIME-decoding_op.mbox.gz").as_slice(),
);
let git_am_thread =
gz_to_string(include_bytes!("git-am-breakage-with-MIME-decoding.mbox.gz").as_slice());
let message_iter = MessageIterator {
index: Arc::new(Mutex::new(HashMap::default())),
input: git_am_thread.as_bytes(),
offset: 0,
file_offset: 0,
format: Some(MboxFormat::MboxCl2),
};
let envelopes: Vec<Envelope> = message_iter.collect::<Result<Vec<_>>>().unwrap();
assert_eq!(envelopes.len(), 7);
let mut original_post: Vec<Envelope> = MessageIterator {
index: Arc::new(Mutex::new(HashMap::default())),
input: git_am_op.as_bytes(),
offset: 0,
file_offset: 0,
format: Some(MboxFormat::MboxCl2),
}
.collect::<Result<Vec<_>>>()
.unwrap();
assert_eq!(original_post.len(), 1);
let original_post = original_post.pop().unwrap();
assert_eq!(&original_post, &envelopes[0]);
}
}