Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkyang committed Jun 28, 2020
1 parent 0a66d00 commit f5a9350
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/token/verified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub(crate) fn split_components(token: &str) -> Result<[&str; 3], Error> {
mod tests {
use crate::algorithm::VerifyingAlgorithm;
use crate::error::Error;
use crate::token::verified::VerifyWithStore;
use crate::token::verified::{VerifyWithKey, VerifyWithStore};
use hmac::{Hmac, NewMac};
use sha2::{Sha256, Sha512};
use std::collections::BTreeMap;
Expand All @@ -150,6 +150,32 @@ mod tests {
name: String,
}

#[test]
pub fn component_errors() {
let key: Hmac<Sha256> = Hmac::new_varkey(b"first").unwrap();

let no_claims = "header";
match VerifyWithKey::<String>::verify_with_key(no_claims, &key) {
Err(Error::NoClaimsComponent) => (),
Ok(s) => panic!("Verify should not have succeeded with output {:?}", s),
x => panic!("Incorrect error type {:?}", x),
}

let no_signature = "header.claims";
match VerifyWithKey::<String>::verify_with_key(no_signature, &key) {
Err(Error::NoSignatureComponent) => (),
Ok(s) => panic!("Verify should not have succeeded with output {:?}", s),
x => panic!("Incorrect error type {:?}", x),
}

let too_many = "header.claims.signature.";
match VerifyWithKey::<String>::verify_with_key(too_many, &key) {
Err(Error::TooManyComponents) => (),
Ok(s) => panic!("Verify should not have succeeded with output {:?}", s),
x => panic!("Incorrect error type {:?}", x),
}
}

#[test]
pub fn verify_claims_with_store() -> Result<(), Error> {
let mut key_store = BTreeMap::new();
Expand Down

0 comments on commit f5a9350

Please sign in to comment.