Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ctz committed Feb 5, 2022
1 parent c204f5c commit e8e0b86
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
7 changes: 6 additions & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ fn parse_cert() {
let data = include_bytes!("../tests/data/certificate.chain.pem");
let mut reader = BufReader::new(&data[..]);

assert_eq!(rustls_pemfile::certs(&mut reader).unwrap().len(), 3);
assert_eq!(
rustls_pemfile::certs(&mut reader)
.unwrap()
.len(),
3
);
}

fn criterion_benchmark(c: &mut Criterion) {
Expand Down
9 changes: 1 addition & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,9 @@
#[cfg(test)]
mod tests;


/// --- Main crate APIs:
mod pemfile;
pub use pemfile::{
Item,
read_one,
read_all,
};
pub use pemfile::{read_all, read_one, Item};

/// --- Legacy APIs:
use std::io;
Expand Down Expand Up @@ -106,4 +100,3 @@ pub fn pkcs8_private_keys(rd: &mut dyn io::BufRead) -> Result<Vec<Vec<u8>>, io::
};
}
}

19 changes: 14 additions & 5 deletions src/pemfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,25 @@ pub fn read_one(rd: &mut dyn io::BufRead) -> Result<Option<Item>, io::Error> {
if len == 0 {
// EOF
if end_marker.is_some() {
return Err(io::Error::new(ErrorKind::InvalidData, format!("section end {:?} missing", end_marker.unwrap())));
return Err(io::Error::new(
ErrorKind::InvalidData,
format!("section end {:?} missing", end_marker.unwrap()),
));
}
return Ok(None);
}

if line.starts_with("-----BEGIN ") {
let trailer = line[11..].find("-----")
.ok_or_else(|| io::Error::new(ErrorKind::InvalidData, format!("illegal section start: {:?}", line)))?;

let ty = &line[11..11+trailer];
let trailer = line[11..]
.find("-----")
.ok_or_else(|| {
io::Error::new(
ErrorKind::InvalidData,
format!("illegal section start: {:?}", line),
)
})?;

let ty = &line[11..11 + trailer];

section_type = Some(ty.to_string());
end_marker = Some(format!("-----END {}-----", ty).to_string());
Expand Down
37 changes: 25 additions & 12 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,42 @@ mod unit {
#[test]
fn skips_leading_junk() {
assert_eq!(
check(b"junk\n\
check(
b"junk\n\
-----BEGIN RSA PRIVATE KEY-----\n\
qw\n\
-----END RSA PRIVATE KEY-----\n").unwrap(),
vec![ crate::Item::RSAKey(vec![ 0xab ]) ]
-----END RSA PRIVATE KEY-----\n"
)
.unwrap(),
vec![crate::Item::RSAKey(vec![0xab])]
);
}

#[test]
fn skips_trailing_junk() {
assert_eq!(
check(b"-----BEGIN RSA PRIVATE KEY-----\n\
check(
b"-----BEGIN RSA PRIVATE KEY-----\n\
qw\n\
-----END RSA PRIVATE KEY-----\n\
junk").unwrap(),
vec![ crate::Item::RSAKey(vec![ 0xab ]) ]
junk"
)
.unwrap(),
vec![crate::Item::RSAKey(vec![0xab])]
);
}

#[test]
fn rejects_invalid_base64() {
assert_eq!(
format!("{:?}",
check(b"-----BEGIN RSA PRIVATE KEY-----\n\
format!(
"{:?}",
check(
b"-----BEGIN RSA PRIVATE KEY-----\n\
q=w\n\
-----END RSA PRIVATE KEY-----\n")),
-----END RSA PRIVATE KEY-----\n"
)
),
"Err(Custom { kind: InvalidData, error: InvalidByte(1, 61) })"
);
}
Expand Down Expand Up @@ -62,11 +72,14 @@ mod unit {
#[test]
fn skips_unrecognised_section() {
assert_eq!(
check(b"junk\n\
check(
b"junk\n\
-----BEGIN BREAKFAST CLUB-----\n\
qw\n\
-----END BREAKFAST CLUB-----\n").unwrap(),
vec![ ]
-----END BREAKFAST CLUB-----\n"
)
.unwrap(),
vec![]
);
}
}
27 changes: 19 additions & 8 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,38 @@ fn test_rsa_private_keys() {
let data = include_bytes!("data/zen2.pem");
let mut reader = BufReader::new(&data[..]);

assert_eq!(rustls_pemfile::rsa_private_keys(&mut reader).unwrap().len(),
2);
assert_eq!(
rustls_pemfile::rsa_private_keys(&mut reader)
.unwrap()
.len(),
2
);
}

#[test]
fn test_certs() {
let data = include_bytes!("data/certificate.chain.pem");
let mut reader = BufReader::new(&data[..]);

assert_eq!(rustls_pemfile::certs(&mut reader).unwrap().len(),
3);
assert_eq!(
rustls_pemfile::certs(&mut reader)
.unwrap()
.len(),
3
);
}

#[test]
fn test_pkcs8() {
let data = include_bytes!("data/zen.pem");
let mut reader = BufReader::new(&data[..]);

assert_eq!(rustls_pemfile::pkcs8_private_keys(&mut reader).unwrap().len(),
2);
assert_eq!(
rustls_pemfile::pkcs8_private_keys(&mut reader)
.unwrap()
.len(),
2
);
}

#[test]
Expand Down Expand Up @@ -58,8 +70,7 @@ fn parse_in_order() {
let data = include_bytes!("data/zen.pem");
let mut reader = BufReader::new(&data[..]);

let items = rustls_pemfile::read_all(&mut reader)
.unwrap();
let items = rustls_pemfile::read_all(&mut reader).unwrap();
assert_eq!(items.len(), 8);
assert!(matches!(items[0], rustls_pemfile::Item::X509Certificate(_)));
assert!(matches!(items[1], rustls_pemfile::Item::X509Certificate(_)));
Expand Down

0 comments on commit e8e0b86

Please sign in to comment.