Skip to content

Commit

Permalink
Add filter parsing and encoding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inejge committed Nov 20, 2022
1 parent 543b0ab commit 8cf0e99
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,125 @@ named!(
fn is_alnum_hyphen(c: u8) -> bool {
is_alphanumeric(c) || c == b'-'
}

#[cfg(test)]
mod test {
use super::parse;

fn ber_vec_eq(filter: &str, ber: &[u8]) {
use lber::structures::ASNTag;
use lber::write;
use bytes::BytesMut;

let mut buf = BytesMut::new();
let tag = parse(filter).unwrap();
write::encode_into(&mut buf, tag.into_structure()).unwrap();
assert_eq!(buf, ber);
}

#[test]
fn filt_bare_item() {
ber_vec_eq("a=v", b"\xa3\x06\x04\x01a\x04\x01v");
}

#[test]
fn filt_simple_eq() {
ber_vec_eq("(a=v)", b"\xa3\x06\x04\x01a\x04\x01v");
}

#[test]
fn filt_extra_garbage() {
assert!(parse("(a=v)garbage").is_err());
}

#[test]
fn filt_simple_noneq() {
ber_vec_eq("(a<=2)", b"\xa6\x06\x04\x01a\x04\x012");
}

#[test]
fn filt_pres() {
ber_vec_eq("(a=*)", b"\x87\x01a");
}

#[test]
fn filt_ast_ini() {
ber_vec_eq("(a=*v)", b"\xa4\x08\x04\x01a0\x03\x82\x01v");
}

#[test]
fn filt_ast_fin() {
ber_vec_eq("(a=v*)", b"\xa4\x08\x04\x01a0\x03\x80\x01v");
}

#[test]
fn filt_ast_multi() {
ber_vec_eq("(a=v*x*y)", b"\xa4\x0e\x04\x01a0\t\x80\x01v\x81\x01x\x82\x01y");
}

#[test]
fn filt_ast_double() {
assert!(parse("(a=f**)").is_err());
}

#[test]
fn filt_esc_ok() {
ber_vec_eq("(a=v\\2ax)", b"\xa3\x08\x04\x01a\x04\x03v*x");
}

#[test]
fn filt_esc_runt() {
assert!(parse("(a=v\\2)").is_err());
}

#[test]
fn filt_esc_invalid() {
assert!(parse("(a=v\\0x)").is_err());
}

#[test]
fn filt_oid() {
ber_vec_eq("(2.5.4.3=v)", b"\xa3\x0c\x04\x072.5.4.3\x04\x01v");
}

#[test]
fn filt_oid0() {
ber_vec_eq("(2.5.4.0=top)", b"\xa3\x0e\x04\x072.5.4.0\x04\x03top");
}

#[test]
fn filt_oidl0() {
assert!(parse("(2.5.04.0=top)").is_err());
}

#[test]
fn filt_complex() {
ber_vec_eq("(&(a=v)(b=x)(!(c=y)))", b"\xa0\x1a\xa3\x06\x04\x01a\x04\x01v\xa3\x06\x04\x01b\x04\x01x\xa2\x08\xa3\x06\x04\x01c\x04\x01y");
}

#[test]
fn filt_abs_true() {
ber_vec_eq("(&)", b"\xa0\0");
}

#[test]
fn filt_abs_false() {
ber_vec_eq("(|)", b"\xa1\0");
}

#[test]
fn filt_ext_dn() {
ber_vec_eq("(ou:dn:=People)", b"\xa9\x0f\x82\x02ou\x83\x06People\x84\x01\xff");
}

#[test]
fn filt_ext_mrule() {
ber_vec_eq("(cn:2.5.13.5:=J D)", b"\xa9\x13\x81\x082.5.13.5\x82\x02cn\x83\x03J D");
}

#[test]
fn filt_simple_utf8() {
ber_vec_eq("(a=ć)", b"\xa3\x07\x04\x01a\x04\x02\xc4\x87");
}

}

0 comments on commit 8cf0e99

Please sign in to comment.