wala-rust

Content-adressed HTTP file server
Info | Log | Files | Refs | README | LICENSE

trace.rs (4286B)


      1 use std::path::Path;
      2 use std::fs::{
      3     File,
      4     OpenOptions,
      5 };
      6 use std::io::Write;
      7     
      8 use hex;
      9 
     10 use log::{debug};
     11 
     12 use crate::record::{
     13     RequestResult,
     14     RequestResultType,
     15 };
     16 
     17 
     18 pub fn trace_request(p: &Path, res: &RequestResult, store_identity: bool) {
     19     if res.typ != RequestResultType::Changed {
     20         return;
     21     }
     22     let mut rf = String::new();
     23     match &res.v {
     24         Some(v) => {
     25             rf.push_str(v);
     26         },
     27         None => {
     28         },
     29     };
     30     if rf.len() == 0 {
     31         return;
     32     }
     33     //let mut content = String::new();
     34     let mut content: Vec<u8> = vec!();
     35     let mut identity = String::new();
     36     match &res.a {
     37         Some(auth) => {
     38             if auth.active() && store_identity {
     39                 content = auth.identity.clone();
     40                 identity = hex::encode(&content);
     41             }
     42         },
     43         None => {
     44         },
     45     }
     46     let fp = p.join(&rf);
     47     let mut f = File::create(fp).unwrap();
     48     f.write(content.as_ref());
     49 
     50     // useless update because we can always resolve mutable to immutable from data dir
     51 //    if content.len() != 0 {
     52 //        let rf_content_hex = res.s.as_ref().unwrap();
     53 //        let rf_content = hex::decode(rf_content_hex).unwrap();
     54 //        //let rf_content = s.clone();
     55 //        let fp = p.join(&identity);
     56 //    
     57 //        let mut f = OpenOptions::new()
     58 //            .write(true)
     59 //            .append(true)
     60 //            .create(true)
     61 //            .open(fp)
     62 //            .unwrap();
     63 //
     64 //        let rf_bin = hex::decode(rf).unwrap();
     65 //        f.write(rf_bin.as_ref());
     66 //        f.write(rf_content.as_ref());
     67 //    }
     68 }
     69 
     70 #[cfg(test)]
     71 mod tests {
     72     use std::path::Path;
     73     use std::fs::File;
     74 
     75     use tempfile::tempdir;
     76 
     77     use log::{debug};
     78 
     79     use crate::record::{
     80         RequestResult,
     81         RequestResultType,
     82     };
     83     use crate::auth::{
     84         AuthResult,
     85     };
     86 
     87     use super::trace_request;
     88 
     89     #[test]
     90     fn test_trace_immutable() {
     91         let d = tempdir().unwrap();
     92         let p = d.path();
     93         let url = String::from("deadbeef");
     94         let mut r = RequestResult::new(RequestResultType::Changed);
     95         r = r.with_content(url);
     96         trace_request(&p, &r, false);
     97         let fp = p.join(&r.v.unwrap());
     98         let f = File::open(fp).unwrap();
     99         let meta = f.metadata().unwrap();
    100         assert_eq!(meta.len(), 0);
    101     }
    102 
    103     #[test]
    104     fn test_trace_typchk() {
    105         let d = tempdir().unwrap();
    106         let p = d.path();
    107         let url = String::from("deadbeef");
    108         let mut r = RequestResult::new(RequestResultType::Found);
    109         r = r.with_content(url);
    110         trace_request(&p, &r, false);
    111         let fp = p.join(&r.v.unwrap());
    112         let f = File::open(fp);
    113         match f {
    114             Ok(v) => {
    115                 panic!("should not have file {:?}", v);
    116             },
    117             Err(e) => {
    118             },
    119         }
    120     }
    121 
    122     #[test]
    123     #[ignore]
    124     fn test_trace_auth_inactive() {
    125         let d = tempdir().unwrap();
    126         let p = d.path();
    127         let url = String::from("deadbeef");
    128         let a = AuthResult{
    129             identity: vec!(),
    130             error: false,
    131         };
    132         let mut r = RequestResult::new(RequestResultType::Changed);
    133         r = r.with_content(url);
    134         r = r.with_auth(a);
    135         trace_request(&p, &r, false);
    136         let fp = p.join(&r.v.unwrap());
    137         let f = File::open(fp);
    138         match f {
    139             Ok(v) => {
    140                 panic!("should not have file {:?}", v);
    141             },
    142             Err(e) => {
    143             },
    144         }
    145     }
    146 
    147     #[test]
    148     fn test_trace_mutable() {
    149         let d = tempdir().unwrap();
    150         let p = d.path();
    151         let url = String::from("deadbeef");
    152         let id_b = b"moradi";
    153         let a = AuthResult{
    154             identity: id_b.to_vec(),
    155             error: false,
    156         };
    157         let mut r = RequestResult::new(RequestResultType::Changed);
    158         r = r.with_content(url);
    159         r = r.with_auth(a);
    160         trace_request(&p, &r, true);
    161         let fp = p.join(r.v.unwrap());
    162         let f = File::open(fp).unwrap();
    163         let meta = f.metadata().unwrap();
    164         //let id_l = (id_b.len() * 2) as u64;
    165         let id_l = id_b.len() as u64;
    166         let r_l = meta.len();
    167         assert_eq!(r_l, id_l);
    168     }
    169 }