Fix BinaryOrPLaintextClassifier bug with utf16-be & utf16-le and correct tests

This commit is contained in:
Mathieu Rheaume 2015-09-12 19:59:20 -04:00
parent 68a088bef6
commit cc44448b09
2 changed files with 9 additions and 10 deletions

View file

@ -39,7 +39,6 @@ impl MIMEClassifier {
apache_bug_flag: ApacheBugFlag, apache_bug_flag: ApacheBugFlag,
supplied_type: &Option<(String, String)>, supplied_type: &Option<(String, String)>,
data: &[u8]) -> Option<(String, String)> { data: &[u8]) -> Option<(String, String)> {
match *supplied_type { match *supplied_type {
None => self.sniff_unknown_type(no_sniff_flag, data), None => self.sniff_unknown_type(no_sniff_flag, data),
Some((ref media_type, ref media_subtype)) => { Some((ref media_type, ref media_subtype)) => {
@ -279,8 +278,8 @@ struct BinaryOrPlaintextClassifier;
impl BinaryOrPlaintextClassifier { impl BinaryOrPlaintextClassifier {
fn classify_impl(&self, data: &[u8]) -> (&'static str, &'static str) { fn classify_impl(&self, data: &[u8]) -> (&'static str, &'static str) {
if data == &[0xFFu8, 0xFEu8] || if data.starts_with(&[0xFFu8, 0xFEu8]) ||
data == &[0xFEu8, 0xFFu8] || data.starts_with(&[0xFEu8, 0xFFu8]) ||
data.starts_with(&[0xEFu8, 0xBBu8, 0xBFu8]) data.starts_with(&[0xEFu8, 0xBBu8, 0xBFu8])
{ {
("text", "plain") ("text", "plain")

View file

@ -494,7 +494,7 @@ fn test_sniff_utf_8_bom_with_apache_flag_on() {
test_sniff_with_flags(&PathBuf::from("text/plain/utf8bom.txt"), test_sniff_with_flags(&PathBuf::from("text/plain/utf8bom.txt"),
"text", "text",
"plain", "plain",
None, Some(("dummy", "text")),
NoSniffFlag::OFF, NoSniffFlag::OFF,
ApacheBugFlag::ON); ApacheBugFlag::ON);
} }
@ -504,7 +504,7 @@ fn test_sniff_utf_16be_bom_with_apache_flag_on() {
test_sniff_with_flags(&PathBuf::from("text/plain/utf16bebom.txt"), test_sniff_with_flags(&PathBuf::from("text/plain/utf16bebom.txt"),
"text", "text",
"plain", "plain",
None, Some(("dummy", "text")),
NoSniffFlag::OFF, NoSniffFlag::OFF,
ApacheBugFlag::ON); ApacheBugFlag::ON);
} }
@ -514,7 +514,7 @@ fn test_sniff_utf_16le_bom_with_apache_flag_on() {
test_sniff_with_flags(&PathBuf::from("text/plain/utf16lebom.txt"), test_sniff_with_flags(&PathBuf::from("text/plain/utf16lebom.txt"),
"text", "text",
"plain", "plain",
None, Some(("dummy", "text")),
NoSniffFlag::OFF, NoSniffFlag::OFF,
ApacheBugFlag::ON); ApacheBugFlag::ON);
} }
@ -524,7 +524,7 @@ fn test_sniff_octet_stream_apache_flag_on() {
test_sniff_with_flags(&PathBuf::from("unknown/binary_file"), test_sniff_with_flags(&PathBuf::from("unknown/binary_file"),
"application", "application",
"octet-stream", "octet-stream",
None, Some(("dummy", "binary")),
NoSniffFlag::OFF, NoSniffFlag::OFF,
ApacheBugFlag::ON); ApacheBugFlag::ON);
} }
@ -532,9 +532,9 @@ fn test_sniff_octet_stream_apache_flag_on() {
#[test] #[test]
fn test_sniff_mp4_video_apache_flag_on() { fn test_sniff_mp4_video_apache_flag_on() {
test_sniff_with_flags(&PathBuf::from("video/mp4/test.mp4"), test_sniff_with_flags(&PathBuf::from("video/mp4/test.mp4"),
"video", "application",
"mp4", "octet-stream",
Some("video", "mp4"), Some(("video", "mp4")),
NoSniffFlag::OFF, NoSniffFlag::OFF,
ApacheBugFlag::ON); ApacheBugFlag::ON);
} }