Skip to content

Commit

Permalink
Add validation code for MIME checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
jongiddy committed Feb 19, 2016
1 parent 8950d7a commit 208bae7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
48 changes: 48 additions & 0 deletions components/net/mime_classifier.rs
Expand Up @@ -150,6 +150,18 @@ impl MIMEClassifier {
}
}

pub fn validate(&self) -> Result<(), String> {
try!(self.image_classifier.validate());
try!(self.audio_video_classifier.validate());
try!(self.scriptable_classifier.validate());
try!(self.plaintext_classifier.validate());
try!(self.archive_classifier.validate());
try!(self.binary_or_plaintext.validate());
try!(self.feeds_classifier.validate());
try!(self.font_classifier.validate());
Ok(())
}

//some sort of iterator over the classifiers might be better?
fn sniff_unknown_type(&self, no_sniff_flag: NoSniffFlag, data: &[u8]) -> (String, String) {
let should_sniff_scriptable = no_sniff_flag == NoSniffFlag::OFF;
Expand Down Expand Up @@ -231,6 +243,8 @@ pub fn as_string_option(tup: Option<(&'static str, &'static str)>) -> Option<(St
//Interface used for composite types
trait MIMEChecker {
fn classify(&self, data: &[u8]) -> Option<(String, String)>;
/// Validate the MIME checker configuration
fn validate(&self) -> Result<(), String>;
}

trait Matches {
Expand Down Expand Up @@ -300,6 +314,15 @@ impl MIMEChecker for ByteMatcher {
(self.content_type.0.to_owned(), self.content_type.1.to_owned())
})
}

fn validate(&self) -> Result<(), String> {
if self.pattern.len() != self.mask.len() {
Err(format!("Unequal pattern and mask length for pattern {:?}", self.pattern))
}
else {
Ok(())
}
}
}

struct TagTerminatedByteMatcher {
Expand All @@ -316,7 +339,12 @@ impl MIMEChecker for TagTerminatedByteMatcher {
None
})
}

fn validate(&self) -> Result<(), String> {
self.matcher.validate()
}
}

pub struct Mp4Matcher;

impl Mp4Matcher {
Expand Down Expand Up @@ -350,6 +378,10 @@ impl MIMEChecker for Mp4Matcher {
None
}
}

fn validate(&self) -> Result<(), String> {
Ok(())
}
}

struct BinaryOrPlaintextClassifier;
Expand All @@ -375,6 +407,11 @@ impl MIMEChecker for BinaryOrPlaintextClassifier {
fn classify(&self, data: &[u8]) -> Option<(String, String)> {
as_string_option(Some(self.classify_impl(data)))
}

fn validate(&self) -> Result<(), String> {
Ok(())
}

}
struct GroupedClassifier {
byte_matchers: Vec<Box<MIMEChecker + Send + Sync>>,
Expand Down Expand Up @@ -473,6 +510,13 @@ impl MIMEChecker for GroupedClassifier {
.filter_map(|matcher| matcher.classify(data))
.next()
}

fn validate(&self) -> Result<(), String> {
for byte_matcher in &self.byte_matchers {
try!(byte_matcher.validate())
}
Ok(())
}
}

enum Match {
Expand Down Expand Up @@ -576,6 +620,10 @@ impl MIMEChecker for FeedsClassifier {
fn classify(&self, data: &[u8]) -> Option<(String, String)> {
as_string_option(self.classify_impl(data))
}

fn validate(&self) -> Result<(), String> {
Ok(())
}
}

//Contains hard coded byte matchers
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/net/mime_classifier.rs
Expand Up @@ -50,6 +50,12 @@ fn test_sniff_mp4_matcher_long() {
assert!(matcher.matches(&data));
}

#[test]
fn test_validate_classifier() {
let classifier = MIMEClassifier::new();
classifier.validate().expect("Validation error")
}

#[cfg(test)]
fn test_sniff_with_flags(filename_orig: &path::Path,
type_string: &str,
Expand Down

0 comments on commit 208bae7

Please sign in to comment.