De-dupe some test code

This commit is contained in:
Thomas Gideon 2017-02-17 12:54:50 -05:00
parent 3c95e1b163
commit a57e582337
3 changed files with 10 additions and 18 deletions

View file

@ -10,7 +10,7 @@ use medallion::{
Token, Token,
}; };
fn load_key(keypath: &str) -> Result<String, Error> { fn load_pem(keypath: &str) -> Result<String, Error> {
let mut key_file = File::open(keypath)?; let mut key_file = File::open(keypath)?;
let mut key = String::new(); let mut key = String::new();
key_file.read_to_string(&mut key)?; key_file.read_to_string(&mut key)?;
@ -34,13 +34,13 @@ fn new_token(user_id: &str, password: &str) -> Option<String> {
}; };
let token = Token::new(header, claims); let token = Token::new(header, claims);
token.signed(load_key("./privateKey.pem").unwrap().as_bytes()).ok() token.signed(load_pem("./privateKey.pem").unwrap().as_bytes()).ok()
} }
fn login(token: &str) -> Option<String> { fn login(token: &str) -> Option<String> {
let token = Token::<DefaultHeader, Registered>::parse(token).unwrap(); let token = Token::<DefaultHeader, Registered>::parse(token).unwrap();
if token.verify(load_key("./publicKey.pub").unwrap().as_bytes()).unwrap() { if token.verify(load_pem("./publicKey.pub").unwrap().as_bytes()).unwrap() {
token.claims.sub token.claims.sub
} else { } else {
None None

View file

@ -71,7 +71,7 @@ fn verify_rsa(signature: &str, data: &str, key: &[u8], digest: MessageDigest) ->
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub mod tests {
use header::Algorithm; use header::Algorithm;
use std::io::{Error, Read}; use std::io::{Error, Read};
use std::fs::File; use std::fs::File;
@ -99,7 +99,7 @@ mod tests {
let real_sig = "nXdpIkFQYZXZ0VlJjHmAc5_aewHCCJpT5jP1fpexUCF_9m3NxlC7uYNXAl6NKno520oh9wVT4VV_vmPeEin7BnnoIJNPcImWcUzkYpLTrDBntiF9HCuqFaniuEVzlf8dVlRJgo8QxhmUZEjyDFjPZXZxPlPV1LD6hrtItxMKZbh1qoNY3OL7Mwo-WuSRQ0mmKj-_y3weAmx_9EaTLY639uD8-o5iZxIIf85U4e55Wdp-C9FJ4RxyHpjgoG8p87IbChfleSdWcZL3NZuxjRCHVWgS1uYG0I-LqBWpWyXnJ1zk6-w4tfxOYpZFMOIyq4tY2mxJQ78Kvcu8bTO7UdI7iA=="; let real_sig = "nXdpIkFQYZXZ0VlJjHmAc5_aewHCCJpT5jP1fpexUCF_9m3NxlC7uYNXAl6NKno520oh9wVT4VV_vmPeEin7BnnoIJNPcImWcUzkYpLTrDBntiF9HCuqFaniuEVzlf8dVlRJgo8QxhmUZEjyDFjPZXZxPlPV1LD6hrtItxMKZbh1qoNY3OL7Mwo-WuSRQ0mmKj-_y3weAmx_9EaTLY639uD8-o5iZxIIf85U4e55Wdp-C9FJ4RxyHpjgoG8p87IbChfleSdWcZL3NZuxjRCHVWgS1uYG0I-LqBWpWyXnJ1zk6-w4tfxOYpZFMOIyq4tY2mxJQ78Kvcu8bTO7UdI7iA==";
let data = format!("{}.{}", header, claims); let data = format!("{}.{}", header, claims);
let key = load_key("./examples/privateKey.pem").unwrap(); let key = load_pem("./examples/privateKey.pem").unwrap();
let sig = sign(&*data, key.as_bytes(), &Algorithm::RS256); let sig = sign(&*data, key.as_bytes(), &Algorithm::RS256);
@ -123,11 +123,11 @@ mod tests {
let real_sig = "nXdpIkFQYZXZ0VlJjHmAc5_aewHCCJpT5jP1fpexUCF_9m3NxlC7uYNXAl6NKno520oh9wVT4VV_vmPeEin7BnnoIJNPcImWcUzkYpLTrDBntiF9HCuqFaniuEVzlf8dVlRJgo8QxhmUZEjyDFjPZXZxPlPV1LD6hrtItxMKZbh1qoNY3OL7Mwo-WuSRQ0mmKj-_y3weAmx_9EaTLY639uD8-o5iZxIIf85U4e55Wdp-C9FJ4RxyHpjgoG8p87IbChfleSdWcZL3NZuxjRCHVWgS1uYG0I-LqBWpWyXnJ1zk6-w4tfxOYpZFMOIyq4tY2mxJQ78Kvcu8bTO7UdI7iA"; let real_sig = "nXdpIkFQYZXZ0VlJjHmAc5_aewHCCJpT5jP1fpexUCF_9m3NxlC7uYNXAl6NKno520oh9wVT4VV_vmPeEin7BnnoIJNPcImWcUzkYpLTrDBntiF9HCuqFaniuEVzlf8dVlRJgo8QxhmUZEjyDFjPZXZxPlPV1LD6hrtItxMKZbh1qoNY3OL7Mwo-WuSRQ0mmKj-_y3weAmx_9EaTLY639uD8-o5iZxIIf85U4e55Wdp-C9FJ4RxyHpjgoG8p87IbChfleSdWcZL3NZuxjRCHVWgS1uYG0I-LqBWpWyXnJ1zk6-w4tfxOYpZFMOIyq4tY2mxJQ78Kvcu8bTO7UdI7iA";
let data = format!("{}.{}", header, claims); let data = format!("{}.{}", header, claims);
let key = load_key("./examples/publicKey.pub").unwrap(); let key = load_pem("./examples/publicKey.pub").unwrap();
assert!(verify(&real_sig, &*data, key.as_bytes(), &Algorithm::RS256).unwrap()); assert!(verify(&real_sig, &*data, key.as_bytes(), &Algorithm::RS256).unwrap());
} }
fn load_key(keypath: &str) -> Result<String, Error> { pub fn load_pem(keypath: &str) -> Result<String, Error> {
let mut key_file = File::open(keypath)?; let mut key_file = File::open(keypath)?;
let mut key = String::new(); let mut key = String::new();
key_file.read_to_string(&mut key)?; key_file.read_to_string(&mut key)?;

View file

@ -123,10 +123,9 @@ impl<H, C> PartialEq for Token<H, C>
mod tests { mod tests {
use Claims; use Claims;
use Token; use Token;
use crypt::tests::load_pem;
use header::Algorithm::{HS256,RS512}; use header::Algorithm::{HS256,RS512};
use header::DefaultHeader; use header::DefaultHeader;
use std::io::{Error, Read};
use std::fs::File;
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)] #[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
struct EmptyClaim { } struct EmptyClaim { }
@ -162,19 +161,12 @@ mod tests {
}, },
..Default::default() ..Default::default()
}; };
let private_key = load_key("./examples/privateKey.pem").unwrap(); let private_key = load_pem("./examples/privateKey.pem").unwrap();
let raw = token.signed(private_key.as_bytes()).unwrap(); let raw = token.signed(private_key.as_bytes()).unwrap();
let same = Token::parse(&*raw).unwrap(); let same = Token::parse(&*raw).unwrap();
assert_eq!(token, same); assert_eq!(token, same);
let public_key = load_key("./examples/publicKey.pub").unwrap(); let public_key = load_pem("./examples/publicKey.pub").unwrap();
assert!(same.verify(public_key.as_bytes()).unwrap()); assert!(same.verify(public_key.as_bytes()).unwrap());
} }
fn load_key(keypath: &str) -> Result<String, Error> {
let mut key_file = File::open(keypath)?;
let mut key = String::new();
key_file.read_to_string(&mut key)?;
Ok(key)
}
} }