Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Write tests for parse_and_validate_args #1

@rusty-snake

Description

@rusty-snake

Some tests for parse_and_validate_args would be quite nice.

fdns4users/src/main.rs

Lines 61 to 102 in 2d08c76

/// Parse and validate the arguments
///
/// The first argument must be `--proxy-addr=127.70.74.[0-9]{1,3}` or `--help`.
/// All other arguments are optional. Currently supported are `--blocklist=[A-Za-z0-9._-]+` and
/// `--whitelist=[A-Za-z0-9._-]+` in any order and number.
fn parse_and_validate_args<T: Iterator<Item = String>>(args: &mut T) -> (String, Vec<String>) {
// validate first commandline arg (--proxy-addr)
let proxy_addr = {
let arg_1 = args
.next()
.expect("No command-line arguments given. --proxy-addr must be given.");
if arg_1 == "--help" {
help()
}
if arg_1.starts_with("--proxy-addr=127.70.74.")
&& arg_1[23..].chars().all(|c| c.is_ascii_digit())
&& 24 <= arg_1.len()
&& arg_1.len() <= 26
{
arg_1
} else {
panic!(
"Invalid first argument, must be --help or --proxy-addr with a allowed IP-address."
);
}
};
// parse left over commandline args, keep only '--whitelist=[A-Za-z0-9._-]*'
// and '--blocklist=[A-Za-z0-9._-]*'
let fdns_args = args
.filter(|arg| {
(arg.starts_with("--blocklist=") || arg.starts_with("--whitelist="))
&& arg[12..]
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_')
})
.collect::<Vec<_>>();
(proxy_addr, fdns_args)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions