wala-rust

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

commit 8b0ce2f008f15edfeb9f7280bbd8af2f298095e9
parent a4383d875428ca16f46c10e7f58b3fcb449daac3
Author: lash <dev@holbrook.no>
Date:   Thu, 23 Jun 2022 06:40:28 +0000

Add mutable,immutable tests, add missing request source file

Diffstat:
Msrc/record.rs | 1+
Asrc/request.rs | 246+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 247 insertions(+), 0 deletions(-)

diff --git a/src/record.rs b/src/record.rs @@ -83,6 +83,7 @@ impl FromStr for ResourceKey { impl ResourceKey { pub fn pointer_for(&self, subject: &AuthResult) -> Vec<u8> { let mut h = Sha256::new(); + debug!("update {:?} {:?}", hex::encode(&self.v), hex::encode(&subject.identity)); h.update(&self.v); h.update(&subject.identity); h.finalize().to_vec() diff --git a/src/request.rs b/src/request.rs @@ -0,0 +1,246 @@ +use std::path::Path; +use std::str::FromStr; +use tiny_http::{ + Method, + Response, + Request, + StatusCode, +}; +use crate::record::{ + put_immutable, + put_mutable, + get as get_record, + ResourceKey, + RequestResult, + RequestResultType, +}; +use crate::auth::{ + AuthResult, +}; +use std::io::Read; + + +pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_size: usize, path: &Path, auth_result: AuthResult) -> RequestResult { + match method { + Method::Put => { + if !auth_result.valid() { + return RequestResult{ + typ: RequestResultType::AuthError, + v: None, + }; + } + if auth_result.active() { + let res: RequestResult; + let rk = ResourceKey::from_str(url.as_str()).unwrap(); + let ptr = rk.pointer_for(&auth_result); + match put_mutable(ptr, path, f, expected_size) { + Ok(v) => { + let digest_hex = hex::encode(v.digest); + res = RequestResult{ + typ: RequestResultType::Changed, + v: Some(digest_hex), + }; + }, + Err(e) => { + let err_str = format!("{}", e); + res = RequestResult { + typ: RequestResultType::RecordError, + v: Some(String::from(err_str)), + }; + }, + }; + return res; + } else { + let res: RequestResult; + match put_immutable(path, f, expected_size) { + Ok(v) => { + let digest_hex = hex::encode(v.digest); + res = RequestResult{ + typ: RequestResultType::Changed, + v: Some(digest_hex), + }; + }, + Err(e) => { + let err_str = format!("{}", e); + res = RequestResult { + typ: RequestResultType::RecordError, + v: Some(String::from(err_str)), + }; + }, + }; + return res; + } + }, + Method::Get => { + let digest = match hex::decode(&url) { + Err(e) => { + let err_str = format!("{}", e); + return RequestResult { + typ: RequestResultType::InputError, + v: Some(String::from(err_str)), + }; + }, + Ok(v) => { + v + }, + }; + let full_path_buf = path.join(&url); + + match get_record(digest, full_path_buf.as_path()) { + Some(v) => { + // CONTINUE FROM HERE, v IS Read + return RequestResult { + typ: RequestResultType::Found, + v: Some(String::new()), + }; + }, + None => { + return RequestResult { + typ: RequestResultType::RecordError, + v: Some(String::from(&url)), + }; + }, + }; + }, + _ => {}, + }; + RequestResult { + typ: RequestResultType::InputError, + v: Some(String::new()), + } +} + +#[cfg(test)] +mod tests { + use tempfile::tempdir; + use tiny_http::Method; + use super::process_method; + use std::fs::{ + read, + write, + File, + }; + use std::path::Path; + use crate::auth::AuthResult; + use crate::record::RequestResultType; + use env_logger; + + + #[test] + fn test_get_ok() { + let d = tempdir().unwrap(); + let url = String::from("deadbeef"); + let data = "foobar"; + + let fp = d.path().join(&url); //.as_path().to_string().unwrap(); + write(&fp, data); + let f = File::open(&fp).unwrap(); + + let method = Method::Get; + + let auth = AuthResult { + identity: vec!(), + error: false, + }; + + let res = process_method(&method, url, f, 6, &d.path(), auth); + assert_eq!(res.typ, RequestResultType::Found); + } + + #[test] + fn test_get_bogus() { + let d = tempdir().unwrap(); + let url = String::from("teadbeef"); + let data = "foobar"; + + let fp = d.path().join(&url); //.as_path().to_string().unwrap(); + write(&fp, data); + let f = File::open(&fp).unwrap(); + + let method = Method::Get; + + let auth = AuthResult { + identity: vec!(), + error: false, + }; + + let res = process_method(&method, url, f, 6, &d.path(), auth); + assert_eq!(res.typ, RequestResultType::InputError); + } + + #[test] + fn test_put_immutable() { + let d = tempdir().unwrap(); + let mut url = String::from("deadbeef"); + let data = "foobar"; + + let fp = d.path().join(&url); //.as_path().to_string().unwrap(); + write(&fp, data); + let f = File::open(&fp).unwrap(); + + let method = Method::Put; + + let auth = AuthResult { + identity: vec!(), + error: false, + }; + + url = String::new(); + let res = process_method(&method, url, f, 6, &d.path(), auth); + assert_eq!(res.typ, RequestResultType::Changed); + + let content_ref = String::from("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"); + assert_eq!(res.v.unwrap(), content_ref); + + } + + #[test] + fn test_put_mutable() { + let d = tempdir().unwrap(); + let url = String::from("deadbeef"); + let data = "foobar"; + + let fp = d.path().join(&url); //.as_path().to_string().unwrap(); + write(&fp, data); + let f = File::open(&fp).unwrap(); + + let method = Method::Put; + + let auth = AuthResult { + identity: vec!(0x66, 0x6f, 0x6f), + error: false, + }; + + let res = process_method(&method, url, f, 6, &d.path(), auth); + assert_eq!(res.typ, RequestResultType::Changed); + + let content_ref = String::from("129208a8eac1bedd060645411baaae4aabc5d9e4c858942defe139b5ba15aba6"); + assert_eq!(res.v.unwrap(), content_ref); + + let fp_immutable = d.path().join(&content_ref); + let r = read(fp_immutable).unwrap(); + + assert_eq!(data.as_bytes(), r); + } + + #[test] + fn test_put_mutable_noauth() { + let d = tempdir().unwrap(); + let url = String::from("deadbeef"); + let data = "foobar"; + + let fp = d.path().join(&url); //.as_path().to_string().unwrap(); + write(&fp, data); + let f = File::open(&fp).unwrap(); + + let method = Method::Put; + + let auth = AuthResult { + identity: vec!(0x2a), + error: true, + }; + + let res = process_method(&method, url, f, 6, &d.path(), auth); + assert_eq!(res.typ, RequestResultType::AuthError); + } +}