Skip to content

Commit

Permalink
Add unit test for safe addition
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmittag committed Mar 25, 2024
1 parent b55e778 commit b6bd4e2
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion kuksa_databroker/databroker/src/authorization/jwt/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::permissions::{Permission, Permissions, PermissionsBuildError};

use super::scope;

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Error {
PublicKeyError(String),
DecodeError(String),
Expand Down Expand Up @@ -146,6 +146,7 @@ impl TryFrom<Claims> for Permissions {

#[cfg(test)]
mod test {

use super::*;

#[test]
Expand Down Expand Up @@ -176,4 +177,36 @@ AEiqOjPq0D6X45wCzIwjILUCAwEAAQ==
Err(err) => panic!("decode should succeed but failed with:{}", err),
}
}

#[test]
fn test_expiration_safe_addition() {
let pub_key = "-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA6ScE9EKXEWVyYhzfhfvg
+LC8NseiuEjfrdFx3HKkb31bRw/SeS0Rye0KDP7uzffwreKf6wWYGxVUPYmyKC7j
Pji5MpDBGM9r3pIZSvPUFdpTE5TiRHFBxWbqPSYt954BTLq4rMu/W+oq5Pdfnugb
voYpLf0dclBl1g9KyszkDnItz3TYbWhGMbsUSfyeSPzH0IADzLoifxbc5mgiR73N
CA/4yNSpfLoqWgQ2vdTM1182sMSmxfqSgMzIMUX/tiaXGdkoKITF1sULlLyWfTo9
79XRZ0hmUwvfzr3OjMZNoClpYSVbKY+vtxHyux9KOOtv9lPMsgYIaPXvisrsneDZ
fCS0afOfjgR96uHIe2UPSGAXru3yGziqEfpRZoxsgXaOe905ordLD5bSX14xkN7N
Cz7rxDLlxPQyxp4Vhog7p/QeUyydBpZjq2bAE5GAJtiu+XGvG8RypzJFKFQwMNsw
g1BoZVD0mb0MtU8KQmHcZIfY0FVer/CR0mUjfl1rH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juxHFtHOOCBvQQC-HuIkpAkIoz45pNjx7WRI";

let decoder = Decoder::new(pub_key).expect("Creation of decoder should succeed");

match decoder.decode(token) {
Ok(claims) => {
match Permissions::try_from(claims){
Ok(perm) => panic!("decode should fail but succeeded with:{:?}", perm),
Err(err) => assert_eq!(err, Error::ClaimsError)
}
}
Err(err) => panic!("decode should succeed but failed with:{}", err),
}
}
}

0 comments on commit b6bd4e2

Please sign in to comment.