Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make parse_many return a Result<Vec<Pem>> #28

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 1.0 - master

> Note: This version is not yet released and is under active development.

- `pem::parse_many` now returns a `Result<Vec<Pem>>` instead of a `Vec<Pem>` that silently discarded invalid sections.
2 changes: 1 addition & 1 deletion benches/pem_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn pem_parse() {
}

fn pem_parse_many() {
pem::parse_many(SAMPLE);
pem::parse_many(SAMPLE).unwrap();
}

fn pem_encode(pem: &pem::Pem) {
Expand Down
22 changes: 14 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
//! -----END CERTIFICATE-----
//! ";
//!
//! let pems = parse_many(SAMPLE);
//! let pems = parse_many(SAMPLE).unwrap();
//! assert_eq!(pems.len(), 2);
//! assert_eq!(pems[0].tag, "INTERMEDIATE CERT");
//! assert_eq!(pems[1].tag, "CERTIFICATE");
Expand Down Expand Up @@ -278,7 +278,7 @@ pub fn parse<B: AsRef<[u8]>>(input: B) -> Result<Pem> {
/// ";
/// let SAMPLE_BYTES: Vec<u8> = SAMPLE.into();
///
/// let pems = parse_many(SAMPLE_BYTES);
/// let pems = parse_many(SAMPLE_BYTES).unwrap();
/// assert_eq!(pems.len(), 2);
/// assert_eq!(pems[0].tag, "INTERMEDIATE CERT");
/// assert_eq!(pems[1].tag, "CERTIFICATE");
Expand Down Expand Up @@ -312,16 +312,16 @@ pub fn parse<B: AsRef<[u8]>>(input: B) -> Result<Pem> {
/// ";
/// let SAMPLE_STRING: Vec<u8> = SAMPLE.into();
///
/// let pems = parse_many(SAMPLE_STRING);
/// let pems = parse_many(SAMPLE_STRING).unwrap();
/// assert_eq!(pems.len(), 2);
/// assert_eq!(pems[0].tag, "INTERMEDIATE CERT");
/// assert_eq!(pems[1].tag, "CERTIFICATE");
/// ```
pub fn parse_many<B: AsRef<[u8]>>(input: B) -> Vec<Pem> {
pub fn parse_many<B: AsRef<[u8]>>(input: B) -> Result<Vec<Pem>> {
// Each time our regex matches a PEM section, we need to decode it.
ASCII_ARMOR
.captures_iter(&input.as_ref())
.filter_map(|caps| Pem::new_from_captures(caps).ok())
.map(|caps| Pem::new_from_captures(caps))
.collect()
}

Expand Down Expand Up @@ -562,12 +562,18 @@ RzHX0lkJl9Stshd/7Gbt65/QYq+v+xvAeT0CoyIg

#[test]
fn test_parse_many_works() {
let pems = parse_many(SAMPLE_CRLF);
let pems = parse_many(SAMPLE_CRLF).unwrap();
assert_eq!(pems.len(), 2);
assert_eq!(pems[0].tag, "RSA PRIVATE KEY");
assert_eq!(pems[1].tag, "RSA PUBLIC KEY");
}

#[test]
fn test_parse_many_errors_on_invalid_section() {
let input = SAMPLE_LF.to_owned() + "-----BEGIN -----\n-----END -----";
assert_eq!(parse_many(&input), Err(PemError::MissingBeginTag));
}

#[test]
fn test_encode_empty_contents() {
let pem = Pem {
Expand Down Expand Up @@ -596,7 +602,7 @@ RzHX0lkJl9Stshd/7Gbt65/QYq+v+xvAeT0CoyIg

#[test]
fn test_encode_many() {
let pems = parse_many(SAMPLE_CRLF);
let pems = parse_many(SAMPLE_CRLF).unwrap();
let encoded = encode_many(&pems);

assert_eq!(SAMPLE_CRLF, encoded);
Expand All @@ -620,7 +626,7 @@ RzHX0lkJl9Stshd/7Gbt65/QYq+v+xvAeT0CoyIg

#[test]
fn test_encode_many_config() {
let pems = parse_many(SAMPLE_LF);
let pems = parse_many(SAMPLE_LF).unwrap();
let config = EncodeConfig {
line_ending: LineEnding::LF,
};
Expand Down