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

Write tests for parse_and_validate_args #1

Open
rusty-snake opened this issue Jan 6, 2021 · 0 comments
Open

Write tests for parse_and_validate_args #1

rusty-snake opened this issue Jan 6, 2021 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@rusty-snake
Copy link
Owner

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)
}

@rusty-snake rusty-snake added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jan 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant