rpz

Response policy zone (RPZ) file generator.
git clone https://git.philomathiclife.com/repos/rpz
Log | Files | Refs | README

tests.rs (5067B)


      1 use crate::E;
      2 use std::{
      3     fs,
      4     io::Write as _,
      5     process::{Command, Stdio},
      6     thread,
      7 };
      8 /// The path to the program dir with no block subdirectories.
      9 pub(crate) const PROG_DIR_NO_SUB: &str = "/home/zack/projects/rpz/target/";
     10 /// The path to the program dir with block subdirectories.
     11 pub(crate) const PROG_DIR: &str = "/home/zack/projects/rpz/target/release/";
     12 /// The path to the program.
     13 const PROG: &str = "/home/zack/projects/rpz/target/release/rpz";
     14 /// Message to append to the appropriate variable above if an issue occurs.
     15 pub(crate) const ERR_MSG: &str = " does not exist, so program testing cannot occur";
     16 /// Verify the correct directories and files exist.
     17 #[expect(clippy::panic, reason = "OK in tests")]
     18 pub(crate) fn verify_files() {
     19     assert!(
     20         fs::metadata(PROG)
     21             .unwrap_or_else(|_| panic!("{PROG}{ERR_MSG}"))
     22             .is_file(),
     23         "{PROG} is not an executable file"
     24     );
     25     assert!(
     26         fs::metadata(PROG_DIR)
     27             .unwrap_or_else(|_| panic!("{PROG_DIR}{ERR_MSG}"))
     28             .is_dir(),
     29         "{PROG_DIR} is not a directory"
     30     );
     31     assert!(
     32         fs::metadata(PROG_DIR_NO_SUB)
     33             .unwrap_or_else(|_| panic!("{PROG_DIR_NO_SUB}{ERR_MSG}"))
     34             .is_dir(),
     35         "{PROG_DIR_NO_SUB} is not a directory"
     36     );
     37     let mut files = String::from(PROG_DIR);
     38     let len = files.len();
     39     files.push_str("block/adblock/foo");
     40     assert!(
     41         fs::metadata(files.as_str())
     42             .unwrap_or_else(|_| panic!("{files}{ERR_MSG}. it must only contain '||bar.com'"))
     43             .is_file(),
     44         "{files} is not a file"
     45     );
     46     files.truncate(len);
     47     files.push_str("block/domain/foo");
     48     assert!(
     49         fs::metadata(files.as_str())
     50             .unwrap_or_else(|_| panic!("{files}{ERR_MSG}. it must only contain 'www.example.com'"))
     51             .is_file(),
     52         "{files} is not a file"
     53     );
     54     files.truncate(len);
     55     files.push_str("unblock/hosts/foo");
     56     assert!(
     57         fs::metadata(files.as_str())
     58             .unwrap_or_else(|_| panic!(
     59                 "{files}{ERR_MSG}. it must only contain '0.0.0.0 www.bar.com'"
     60             ))
     61             .is_file(),
     62         "{files} is not a file"
     63     );
     64     files.truncate(len);
     65     files.push_str("unblock/wildcard/foo");
     66     assert!(
     67         fs::metadata(files.as_str())
     68             .unwrap_or_else(|_| panic!("{files}{ERR_MSG}. it must only contain '*.foo.com'"))
     69             .is_file(),
     70         "{files} is not a file"
     71     );
     72 }
     73 pub(crate) fn get_command() -> Command {
     74     Command::new(PROG)
     75 }
     76 #[test]
     77 #[ignore = "performs I/O"]
     78 fn app() {
     79     verify_files();
     80     assert!(
     81         get_command()
     82             .args(["-f", "-"])
     83             .stderr(Stdio::piped())
     84             .stdin(Stdio::piped())
     85             .stdout(Stdio::null())
     86             .spawn()
     87             .is_ok_and(|mut cmd| {
     88                 cmd.stdin.take().is_some_and(|mut stdin| {
     89                     thread::spawn(move || {
     90                         stdin
     91                             .write_all(format!("local_dir=\"{PROG_DIR_NO_SUB}\"").as_bytes())
     92                             .is_ok()
     93                     })
     94                     .join()
     95                     .is_ok_and(|v| v)
     96                 }) && cmd.wait_with_output().is_ok_and(|output| {
     97                     !output.status.success()
     98                         && output.stderr == format!("Error: {:?}\n", E::NoBlockEntries).into_bytes()
     99                 })
    100             })
    101     );
    102     assert!(get_command()
    103             .args(["-vf", "-"])
    104             .stderr(Stdio::null())
    105             .stdin(Stdio::piped())
    106             .stdout(Stdio::piped())
    107             .spawn()
    108             .is_ok_and(|mut cmd| {
    109                 cmd.stdin.take().is_some_and(|mut stdin| {
    110                     thread::spawn(move || {
    111                         stdin
    112                             .write_all(format!("local_dir=\"{PROG_DIR}\"").as_bytes())
    113                             .is_ok()
    114                     })
    115                     .join()
    116                     .is_ok_and(|v| v)
    117                 }) && cmd.wait_with_output().is_ok_and(|output| {
    118                     output.status.success()
    119                         && output.stdout
    120                             == b"www.bar.com CNAME rpz-passthru.
    121 bar.com CNAME .
    122 *.bar.com CNAME .
    123 www.example.com CNAME .
    124 (Hosts) /home/zack/projects/rpz/target/release/unblock/hosts/foo - domains parsed: 1, comments parsed: 0, blanks parsed: 0, parsing errors: 0
    125 (Wildcard) /home/zack/projects/rpz/target/release/unblock/wildcard/foo - domains parsed: 1, comments parsed: 0, blanks parsed: 0, parsing errors: 0
    126 (Adblock) /home/zack/projects/rpz/target/release/block/adblock/foo - domains parsed: 1, comments parsed: 0, blanks parsed: 0, parsing errors: 0
    127 (Domain-only) /home/zack/projects/rpz/target/release/block/domain/foo - domains parsed: 1, comments parsed: 0, blanks parsed: 0, parsing errors: 0
    128 unblock count written: 1
    129 block count written: 3
    130 total lines written: 4
    131 domains parsed: 4
    132 comments parsed: 0
    133 blanks parsed: 0
    134 parsing errors: 0\n"
    135                 })
    136             }));
    137 }