wala-rust

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

commit 000292e482d8c174a165e588a42976eef497c9c7
parent 1633ecdaa9bf2a3a6ccd0c7891813cc856b7fa12
Author: lash <dev@holbrook.no>
Date:   Tue, 21 Jun 2022 09:05:28 +0000

Add mutable pointer test

Diffstat:
Asrc/mutable.rs | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+), 0 deletions(-)

diff --git a/src/mutable.rs b/src/mutable.rs @@ -0,0 +1,53 @@ +use std::str::FromStr; +use std::convert::Infallible; + +use sha2::{Sha256, Digest}; + +use crate::auth::AuthResult; + +pub struct ResourceKey { + v: Vec<u8>, +} + +impl FromStr for ResourceKey { + type Err = Infallible; + + fn from_str(s: &str) -> Result<ResourceKey, Infallible> { + let mut h = Sha256::new(); + h.update(&s[..]); + let k = ResourceKey{ + v: h.finalize().to_vec(), + }; + Ok(k) + } +} + +impl ResourceKey { + pub fn pointer_for(&self, subject: &AuthResult) -> Vec<u8> { + let mut h = Sha256::new(); + h.update(&self.v); + h.update(&subject.identity); + h.finalize().to_vec() + } +} + +#[cfg(test)] +mod tests { + use super::ResourceKey; + use super::AuthResult; + use hex; + + #[test] + fn test_pointer() { + let resource = ResourceKey{ + v: vec!(0x66, 0x6f, 0x6f), + }; + let subject = AuthResult{ + identity: vec!(0x62, 0x61, 0x72), + }; + let r = resource.pointer_for(&subject); + + let foobar_digest = hex::decode("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2").unwrap(); + assert_eq!(r, foobar_digest); + } +}