#![allow(dead_code)]
pub mod token_gen;
pub mod validators;
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationOutcome {
Success,
InvalidSignature,
InvalidFormat,
AlgorithmNotAllowed,
NetworkError,
MissingClaim(String),
InvalidClaim(String),
}
impl fmt::Display for ValidationOutcome {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Success => write!(f, "Success"),
Self::InvalidSignature => write!(f, "InvalidSignature"),
Self::InvalidFormat => write!(f, "InvalidFormat"),
Self::AlgorithmNotAllowed => write!(f, "AlgorithmNotAllowed"),
Self::NetworkError => write!(f, "NetworkError"),
Self::MissingClaim(claim) => write!(f, "MissingClaim({claim})"),
Self::InvalidClaim(claim) => write!(f, "InvalidClaim({claim})"),
}
}
}
#[derive(Debug)]
pub struct ParityResult {
pub jwtiny_outcome: ValidationOutcome,
pub jsonwebtoken_outcome: ValidationOutcome,
}
impl ParityResult {
pub fn is_parity(&self) -> bool {
self.jwtiny_outcome == self.jsonwebtoken_outcome
}
}
pub async fn run_parity_test<J, T>(
token: &str,
jwtiny_validator: &J,
jsonwebtoken_validator: &T,
) -> ParityResult
where
J: validators::JwtinyValidatorTrait,
T: validators::JsonwebtokenValidatorTrait,
{
let jwtiny_outcome = jwtiny_validator.validate(token).await;
let jsonwebtoken_outcome = jsonwebtoken_validator.validate(token).await;
ParityResult {
jwtiny_outcome,
jsonwebtoken_outcome,
}
}
pub fn assert_parity(result: &ParityResult) {
assert!(
result.is_parity(),
"Parity test failed!\n jwtiny: {}\n jsonwebtoken: {}",
result.jwtiny_outcome,
result.jsonwebtoken_outcome
);
}
pub fn assert_both_succeed(result: &ParityResult) {
assert!(
result.jwtiny_outcome == ValidationOutcome::Success,
"jwtiny validation failed: {}",
result.jwtiny_outcome
);
assert!(
result.jsonwebtoken_outcome == ValidationOutcome::Success,
"jsonwebtoken validation failed: {}",
result.jsonwebtoken_outcome
);
assert_parity(result);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validation_outcome_equality() {
assert_eq!(ValidationOutcome::Success, ValidationOutcome::Success);
assert_eq!(
ValidationOutcome::InvalidSignature,
ValidationOutcome::InvalidSignature
);
assert_ne!(
ValidationOutcome::Success,
ValidationOutcome::InvalidSignature
);
}
#[test]
fn test_parity_result_is_parity() {
let result = ParityResult {
jwtiny_outcome: ValidationOutcome::Success,
jsonwebtoken_outcome: ValidationOutcome::Success,
};
assert!(result.is_parity());
let result = ParityResult {
jwtiny_outcome: ValidationOutcome::Success,
jsonwebtoken_outcome: ValidationOutcome::InvalidSignature,
};
assert!(!result.is_parity());
}
}