Some bug fixing. All tests passing.

This commit is contained in:
Mauro D
2021-10-28 15:39:48 +00:00
parent 6c8650fb84
commit 345b330017
75 changed files with 1668 additions and 2120 deletions
+7 -6
View File
@@ -19,8 +19,11 @@ pub fn add_html_token(result: &mut String, token: &[u8], add_space: bool) {
} else if (2..=31).contains(&entity.len()) {
let mut hash = entity.len() as u32;
// Safe because ENTITY_HASH's size is 260 (u8::MAX + 4)
debug_assert!(ENTITY_HASH.len() == u8::MAX as usize + 4);
/* Safe because ENTITY_HASH's size is 260 (u8::MAX + 5)
and ENTITY_MAP's size is 18016 (18079 - 64 + 1) */
debug_assert_eq!(ENTITY_HASH.len(), u8::MAX as usize + 5);
debug_assert_eq!(ENTITY_MAP.len(), 18079 - 64 + 1);
unsafe {
for (pos, ch) in entity.iter().enumerate() {
match pos {
@@ -217,6 +220,7 @@ pub fn text_to_html(input: &str) -> String {
match ch {
b'\n' => result.extend_from_slice(b"<br/>"),
b'<' => result.extend_from_slice(b"&lt;"),
b'\r' => (),
_ => result.push(*ch),
}
}
@@ -287,9 +291,6 @@ mod tests {
for input in inputs {
assert_eq!(html_to_text(input.0), input.1, "Failed for '{:?}'", input.0);
}
//let input = fs::read("002.html").unwrap();
//println!("{}", html_to_text(std::str::from_utf8(&input).unwrap()));
}
#[test]
@@ -336,7 +337,7 @@ static ENTITY_HASH: &[u32; 260] = &[
18080, 18080, 18080,
];
static ENTITY_MAP: &[u32] = &[
static ENTITY_MAP: &[u32; 18016] = &[
0x02192, 0, 0, 0, 0, 0x02190, 0, 0, 0, 0, 0, 0, 0, 0, 0x02AAB, 0, 0, 0, 0, 0, 0, 0, 0, 0x0226A,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02A99, 0, 0x02191, 0, 0, 0, 0x02225, 0x00029, 0x021FE, 0, 0, 0,
0x00028, 0x021FD, 0x021A3, 0, 0x022A5, 0x02225, 0, 0x021A2, 0, 0, 0x022D5, 0, 0, 0, 0x02A95,
+28 -47
View File
@@ -201,21 +201,11 @@ impl<'x> Message<'x> {
let (is_multipart, mut is_inline, mut is_text, mut mime_type) =
get_mime_type(header.get_content_type(), &state.mime_type);
/*println!(
"--- New part {:?} parent {:?} at '{:?}'",
mime_type,
state.mime_type,
stream
.get_string(stream.get_pos(), stream.get_pos() + 20, true)
.unwrap_or_else(|| "NOTHING".into())
);*/
if is_multipart {
if let Some(mime_boundary) = header
.get_content_type()
.map_or_else(|| None, |f| f.get_attribute("boundary"))
{
//println!("Found boundary '{}'", mime_boundary,);
let mime_boundary = ("\n--".to_string() + mime_boundary).into_bytes();
if stream.seek_next_part(mime_boundary.as_ref()) {
@@ -290,7 +280,6 @@ impl<'x> Message<'x> {
// Attempt to recover contents of an invalid message
if !success {
//println!("Failed to parse part, reattempting.");
if !stream.is_eof() {
// Get raw MIME part
let (success, r_is_utf8_safe, r_bytes) = stream.get_bytes_to_boundary(
@@ -374,8 +363,6 @@ impl<'x> Message<'x> {
},
};
//println!("'{:?}' {} {} {}", text_part.contents, add_to_html, add_to_text, is_inline);
let is_html = mime_type == MimeType::TextHtml;
if add_to_html && !is_html {
@@ -495,25 +482,9 @@ impl<'x> Message<'x> {
if let Some(ref mime_boundary) = state.mime_boundary {
// Ancestor has a MIME boundary, seek it.
if stream.seek_next_part(mime_boundary) {
/*println!(
"Found ancestor MIME boundary '{:?}'",
std::str::from_utf8(mime_boundary).unwrap()
);*/
continue 'inner;
} else {
/*println!(
"Boundary '{:?} not found.",
std::str::from_utf8(mime_boundary).unwrap()
);*/
}
} else {
// Ancestor does not have a MIME boundary, end parsing.
//println!("Ancestor has no MIME boundary, parent found?");
}
} else {
// No more ancestors found, finish parsing.
//println!("Finish parsing, no ancestors found.");
}
break 'outer;
} else {
@@ -527,58 +498,68 @@ impl<'x> Message<'x> {
}
}
while let Some(mut prev_message) = message_stack.pop() {
prev_message
.attachments
.push(AttachmentPart::Message(message));
message = prev_message;
}
message
}
}
#[cfg(test)]
mod tests {
use std::{fs, path::PathBuf};
use std::{fmt::format, fs, path::PathBuf};
use crate::parsers::message::Message;
#[test]
fn body_parse() {
fn parse_full_messages() {
const SEPARATOR: &[u8] = "\n---- EXPECTED STRUCTURE ----\n".as_bytes();
for test_suite in ["rfc", "legacy", "malformed"] {
for test_suite in ["rfc", "legacy", "thirdparty", "malformed"] {
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("tests");
test_dir.push(test_suite);
for file_name in fs::read_dir(test_dir).unwrap() {
let mut input = fs::read(file_name.as_ref().unwrap().path()).unwrap();
let file_name = file_name.as_ref().unwrap().path();
let mut input = fs::read(&file_name).unwrap();
let mut pos = 0;
for sep_pos in 0..input.len() {
if input[sep_pos..sep_pos + SEPARATOR.len()].eq(SEPARATOR) {
pos = sep_pos;
break;
}
}
assert!(pos > 0, "Failed to find separator.");
let input = input.split_at_mut(pos);
assert!(
pos > 0,
"Failed to find separator in test file '{}'.",
file_name.display()
);
let input = input.split_at_mut(pos);
let message = Message::parse(input.0);
if (message
!= serde_json::from_slice::<Message>(&input.1[SEPARATOR.len()..]).unwrap())
{
println!("{}", serde_json::to_string_pretty(&message).unwrap());
return;
}
assert_eq!(
Message::parse(input.0),
message,
serde_json::from_slice::<Message>(&input.1[SEPARATOR.len()..]).unwrap(),
"Test failed for {}",
file_name.unwrap().path().display()
"Test failed for '{}', result was:\n{}",
file_name.display(),
serde_json::to_string_pretty(&message).unwrap()
);
}
}
}
#[test]
/*#[test]
fn generate_test_samples() {
const SEPARATOR: &[u8] = "\n---- EXPECTED STRUCTURE ----\n".as_bytes();
for test_suite in ["legacy" /*"legacy","malformed"*/] {
for test_suite in ["malformed" /*"legacy","malformed"*/] {
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("tests");
test_dir.push(test_suite);
@@ -622,5 +603,5 @@ mod tests {
fs::write(file_name.as_ref().unwrap().path(), &output).unwrap();
}
}
}
}*/
}
+12 -40
View File
@@ -69,11 +69,6 @@ impl<'x> MessageStream<'x> {
unsafe { (*self.data.get()).get(*self.pos.get()) }
}
#[inline(always)]
pub fn peek_pos(&self, pos: usize) -> Option<&'x u8> {
unsafe { (*self.data.get()).get(pos) }
}
#[inline(always)]
pub fn advance(&self, pos: usize) {
unsafe {
@@ -221,29 +216,12 @@ impl<'x> MessageStream<'x> {
#[inline(always)]
pub fn is_boundary_end(&self, pos: usize) -> bool {
if let Some([b'\n', ..])
matches!(unsafe { (*self.data.get()).get(pos..) }, Some([b'\n', ..])
| Some([b'-', b'-', ..])
| Some([b'\r', b'\n', ..])
| Some([b' ' | b'\t', ..])
| None = unsafe { (*self.data.get()).get(pos..) }
{
true
} else {
false
}
/*unsafe {
let data = &mut *self.data.get();
match (*data).get(pos) {
Some(b'\n') => true,
Some(b'-') if (*data).get(pos + 1) == Some(&b'-') => true,
Some(b'\r') if (*data).get(pos + 1) == Some(&b'\n') => true,
Some(b' ') | Some(b'\t') => true,
None => true,
_ => false,
}
}*/
| Some([])
| None)
}
pub fn skip_multipart_end(&self) -> bool {
@@ -251,21 +229,15 @@ impl<'x> MessageStream<'x> {
let pos = &mut *self.pos.get();
match (*self.data.get()).get(*pos..*pos + 2) {
Some(b"--") => match (*self.data.get()).get(*pos + 2..) {
Some([b'\n', ..]) => {
*pos += 3;
true
}
Some([b'\r', b'\n', ..]) => {
*pos += 4;
true
}
None | Some([b' ' | b'\t', ..]) => {
*pos += 2;
true
}
_ => false,
},
Some(b"--") => {
if let Some(byte) = (*self.data.get()).get(*pos + 2) {
if !(*byte).is_ascii_whitespace() {
return false;
}
}
*pos += 2;
true
}
_ => false,
}
}