def test_correct_file_argument(mocker):
    mocker.patch(get_dynamic_open_module_name())
    parser = TargetArgumentParser()
    args = parser.parse_args(["-targets-file", "dummy-file.txt"])
    assert not args.fast
    assert args.targets_file
    assert not args.targets
def test_incorrect_no_argument(mocker):
    m = mocker.patch.object(TargetArgumentParser, "error")
    parser = TargetArgumentParser()
    parser.parse_args([])
    m.assert_called_once()
    assert re.match(r"^one of the arguments .+ is required$",
                    m.call_args[0][0])
def test_incorrect_both_argument(mocker):
    mocker.patch(get_dynamic_open_module_name())
    m = mocker.patch.object(TargetArgumentParser,
                            "error",
                            side_effect=ValueError)
    parser = TargetArgumentParser()
    with pytest.raises(ValueError):
        parser.parse_args(
            ["test.hostname-02.com", "-targets-file", "dummy-file.txt"])
    m.assert_any_call(
        "argument -targets-file: not allowed with argument targets")
def test_correct_several_arguments():
    parser = TargetArgumentParser()
    args = parser.parse_args([
        "2001:DB8::1", "192.0.2.1", "192.0.2.1/24", "test.hostname-02.com",
        "--fast"
    ])
    assert args.fast
    assert args.targets_file is None
    assert args.targets == [
        "2001:DB8::1",
        "192.0.2.1",
        "192.0.2.1/24",
        "test.hostname-02.com",
    ]
def test_incorrect_bad_argument(mocker):
    m = mocker.patch.object(TargetArgumentParser, "error")
    parser = TargetArgumentParser()
    parser.parse_args(["-incorrect"])
    m.assert_any_call("unrecognized arguments: -incorrect")
def test_correct_one_argument():
    parser = TargetArgumentParser()
    args = parser.parse_args(["2001:DB8::1"])
    assert not args.fast
    assert args.targets_file is None
    assert args.targets == ["2001:DB8::1"]
def test_incorrect_file_argument():
    parser = TargetArgumentParser()
    with pytest.raises(SystemExit):
        parser.parse_args(["-targets-file", "dummy-file.txt"])
Exemple #8
0
import sys

from port_scanner import PortScanner
from port_scanner.args_validators import TargetArgumentParser


def convert_args_to_unicode_if_needed():
    """
    In Python 2.7, sys.argv are str and not unicode,
    ipaddress package only works on unicode,
    hence we need to decode given arguments
    """
    if sys.version_info.major == 2:
        sys.argv = map(lambda arg: arg.decode(sys.stdout.encoding), sys.argv)


if __name__ == "__main__":
    convert_args_to_unicode_if_needed()
    parser = TargetArgumentParser()
    args = parser.parse_args()
    ps = PortScanner(**vars(args))
    ps()