mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
move mime classifier unit tests into unit test crate
This commit is contained in:
parent
74c847a17f
commit
d0469dfaf3
4 changed files with 438 additions and 438 deletions
|
@ -122,7 +122,7 @@ impl MIMEClassifier {
|
|||
}
|
||||
}
|
||||
|
||||
fn as_string_option(tup: Option<(&'static str, &'static str)>) -> Option<(String,String)> {
|
||||
pub fn as_string_option(tup: Option<(&'static str, &'static str)>) -> Option<(String,String)> {
|
||||
tup.map(|(a, b)| (a.to_owned(), b.to_owned()))
|
||||
}
|
||||
|
||||
|
@ -223,10 +223,10 @@ impl MIMEChecker for TagTerminatedByteMatcher {
|
|||
}
|
||||
}
|
||||
}
|
||||
struct Mp4Matcher;
|
||||
pub struct Mp4Matcher;
|
||||
|
||||
impl Mp4Matcher {
|
||||
fn matches(&self,data: &Vec<u8>) -> bool {
|
||||
pub fn matches(&self,data: &Vec<u8>) -> bool {
|
||||
if data.len() < 12 {
|
||||
return false;
|
||||
}
|
||||
|
@ -979,437 +979,3 @@ impl ByteMatcher {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use std::old_io::File;
|
||||
use std::os;
|
||||
use super::Mp4Matcher;
|
||||
use super::MIMEClassifier;
|
||||
use super::as_string_option;
|
||||
|
||||
#[test]
|
||||
fn test_sniff_mp4_matcher() {
|
||||
let matcher = Mp4Matcher;
|
||||
|
||||
let p = Path::new("../../tests/content/parsable_mime/video/mp4/test.mp4");
|
||||
let mut file = File::open(&p);
|
||||
let read_result = file.read_to_end();
|
||||
match read_result {
|
||||
Ok(data) => {
|
||||
println!("Data Length {:?}",data.len());
|
||||
if !matcher.matches(&data) {
|
||||
panic!("Didn't read mime type")
|
||||
}
|
||||
},
|
||||
Err(e) => panic!("Couldn't read from file with error {}",e)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn test_sniff_full(filename_orig: &Path,type_string: &str,subtype_string: &str,
|
||||
supplied_type: Option<(&'static str,&'static str)>){
|
||||
let current_working_directory = os::getcwd().unwrap();
|
||||
println!("The current directory is {}", current_working_directory.display());
|
||||
|
||||
let mut filename = Path::new("../../tests/content/parsable_mime/");
|
||||
|
||||
filename.push(filename_orig);
|
||||
let classifier = MIMEClassifier::new();
|
||||
|
||||
let mut file = File::open(&filename);
|
||||
let read_result = file.read_to_end();
|
||||
match read_result {
|
||||
Ok(data) => {
|
||||
match classifier.classify(false, false, &as_string_option(supplied_type), &data) {
|
||||
Some((parsed_type, parsed_subtp)) => {
|
||||
if (parsed_type.as_slice() != type_string) ||
|
||||
(parsed_subtp.as_slice() != subtype_string) {
|
||||
panic!("File {} parsed incorrectly should be {}/{}, parsed as {}/{}",
|
||||
filename.as_str().unwrap(), type_string, subtype_string,
|
||||
parsed_type, parsed_subtp);
|
||||
}
|
||||
}
|
||||
None => panic!("No classification found for {} with supplied type {:?}",
|
||||
filename.as_str().unwrap(), supplied_type),
|
||||
}
|
||||
}
|
||||
Err(e) => panic!("Couldn't read from file {} with error {}",
|
||||
filename.as_str().unwrap(), e),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn test_sniff_classification(file: &str,type_string: &str,subtype_string: &str,
|
||||
supplied_type: Option<(&'static str,&'static str)>){
|
||||
let mut x = Path::new("./");
|
||||
x.push(type_string);
|
||||
x.push(subtype_string);
|
||||
x.push(file);
|
||||
test_sniff_full(&x,type_string,subtype_string,supplied_type);
|
||||
}
|
||||
#[cfg(test)]
|
||||
fn test_sniff_classification_sup(file: &str,type_string: &'static str,subtype_string: &str) {
|
||||
test_sniff_classification(file,type_string,subtype_string, None);
|
||||
let class_type = Some((type_string, ""));
|
||||
test_sniff_classification(file,type_string,subtype_string,class_type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_x_icon() {
|
||||
test_sniff_classification_sup("test.ico", "image", "x-icon");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_x_icon_cursor() {
|
||||
test_sniff_classification_sup("test_cursor.ico", "image", "x-icon");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_bmp() {
|
||||
test_sniff_classification_sup("test.bmp", "image", "bmp");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_gif87a() {
|
||||
test_sniff_classification_sup("test87a", "image", "gif");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_gif89a() {
|
||||
test_sniff_classification_sup("test89a.gif", "image", "gif");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_webp() {
|
||||
test_sniff_classification_sup("test.webp", "image", "webp");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_png() {
|
||||
test_sniff_classification_sup("test.png", "image", "png");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_jpg() {
|
||||
test_sniff_classification_sup("test.jpg", "image", "jpeg");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_webm() {
|
||||
test_sniff_classification_sup("test.webm", "video", "webm");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_mp4() {
|
||||
test_sniff_classification_sup("test.mp4", "video", "mp4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_avi() {
|
||||
test_sniff_classification_sup("test.avi", "video", "avi");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_basic() {
|
||||
test_sniff_classification_sup("test.au", "audio", "basic");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_aiff() {
|
||||
test_sniff_classification_sup("test.aif", "audio", "aiff");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_mpeg() {
|
||||
test_sniff_classification_sup("test.mp3", "audio", "mpeg");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_midi() {
|
||||
test_sniff_classification_sup("test.mid", "audio", "midi");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_wave() {
|
||||
test_sniff_classification_sup("test.wav", "audio", "wave");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_ogg() {
|
||||
test_sniff_classification("small.ogg", "application", "ogg", None);
|
||||
test_sniff_classification("small.ogg", "application", "ogg", Some(("audio", "")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_sniff_vsn_ms_fontobject() {
|
||||
test_sniff_classification_sup("vnd.ms-fontobject", "application", "vnd.ms-fontobject");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_sniff_true_type() {
|
||||
test_sniff_full(&Path::new("unknown/true_type.ttf"), "(TrueType)", "", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_sniff_open_type() {
|
||||
test_sniff_full(&Path::new("unknown/open_type"), "(OpenType)", "", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_sniff_true_type_collection() {
|
||||
test_sniff_full(&Path::new("unknown/true_type_collection.ttc"), "(TrueType Collection)", "", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_sniff_woff() {
|
||||
test_sniff_classification_sup("test.wof", "application", "font-woff");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_gzip() {
|
||||
test_sniff_classification("test.gz", "application", "x-gzip", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_zip() {
|
||||
test_sniff_classification("test.zip", "application", "zip", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_rar() {
|
||||
test_sniff_classification("test.rar", "application", "x-rar-compressed", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_doctype_20() {
|
||||
test_sniff_classification("text_html_doctype_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_doctype_20_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_doctype_3e() {
|
||||
test_sniff_classification("text_html_doctype_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_doctype_3e_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_page_20() {
|
||||
test_sniff_classification("text_html_page_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_page_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_page_3e() {
|
||||
test_sniff_classification("text_html_page_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_page_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_head_20() {
|
||||
test_sniff_classification("text_html_head_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_head_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_head_3e() {
|
||||
test_sniff_classification("text_html_head_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_head_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_script_20() {
|
||||
test_sniff_classification("text_html_script_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_script_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_script_3e() {
|
||||
test_sniff_classification("text_html_script_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_script_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_iframe_20() {
|
||||
test_sniff_classification("text_html_iframe_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_iframe_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_iframe_3e() {
|
||||
test_sniff_classification("text_html_iframe_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_iframe_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_h1_20() {
|
||||
test_sniff_classification("text_html_h1_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_h1_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_h1_3e() {
|
||||
test_sniff_classification("text_html_h1_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_h1_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_div_20() {
|
||||
test_sniff_classification("text_html_div_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_div_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_div_3e() {
|
||||
test_sniff_classification("text_html_div_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_div_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_font_20() {
|
||||
test_sniff_classification("text_html_font_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_font_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_font_3e() {
|
||||
test_sniff_classification("text_html_font_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_font_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_table_20() {
|
||||
test_sniff_classification("text_html_table_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_table_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_table_3e() {
|
||||
test_sniff_classification("text_html_table_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_table_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_a_20() {
|
||||
test_sniff_classification("text_html_a_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_a_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_a_3e() {
|
||||
test_sniff_classification("text_html_a_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_a_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_style_20() {
|
||||
test_sniff_classification("text_html_style_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_style_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_style_3e() {
|
||||
test_sniff_classification("text_html_style_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_style_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_title_20() {
|
||||
test_sniff_classification("text_html_title_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_title_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_title_3e() {
|
||||
test_sniff_classification("text_html_title_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_title_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_b_20() {
|
||||
test_sniff_classification("text_html_b_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_b_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_b_3e() {
|
||||
test_sniff_classification("text_html_b_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_b_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_body_20() {
|
||||
test_sniff_classification("text_html_body_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_body_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_body_3e() {
|
||||
test_sniff_classification("text_html_body_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_body_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_br_20() {
|
||||
test_sniff_classification("text_html_br_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_br_20_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_br_3e() {
|
||||
test_sniff_classification("text_html_br_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_br_3e_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_p_20() {
|
||||
test_sniff_classification("text_html_p_20.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_p_20_u.html", "text", "html", None);
|
||||
}
|
||||
#[test]
|
||||
fn test_sniff_text_html_p_3e() {
|
||||
test_sniff_classification("text_html_p_3e.html", "text", "html", None);
|
||||
test_sniff_classification("text_html_p_3e_u.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_comment_20() {
|
||||
test_sniff_classification("text_html_comment_20.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_text_html_comment_3e() {
|
||||
test_sniff_classification("text_html_comment_3e.html", "text", "html", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_xml() {
|
||||
test_sniff_classification("test.xml", "text", "xml", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_pdf() {
|
||||
test_sniff_classification("test.pdf", "application", "pdf", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_postscript() {
|
||||
test_sniff_classification("test.ps", "application", "postscript", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_utf_16be_bom() {
|
||||
test_sniff_classification("utf16bebom.txt", "text", "plain", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_utf_16le_bom() {
|
||||
test_sniff_classification("utf16lebom.txt", "text", "plain", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_utf_8_bom() {
|
||||
test_sniff_classification("utf8bom.txt", "text", "plain", None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_rss_feed() {
|
||||
test_sniff_full(&Path::new("text/xml/feed.rss"), "application", "rss+xml", Some(("text", "html")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sniff_atom_feed() {
|
||||
test_sniff_full(&Path::new("text/xml/feed.atom"), "application", "atom+xml", Some(("text", "html")));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue