Exemple #1
0
    def test_get_measurement_kwargs_http(self):

        spec = Configuration.DEFAULT["specification"]["types"]["http"]

        cmd = HttpMeasureCommand()
        cmd.init_args([
            "http", "--target", "ripe.net"
        ])
        self.assertEqual(
            cmd._get_measurement_kwargs(),
            {
                "af": Configuration.DEFAULT["specification"]["af"],
                "description": "Http measurement to ripe.net",
                "target": "ripe.net",
                "header_bytes": spec["header-bytes"],
                "version": spec["version"],
                "method": spec["method"],
                "port": spec["port"],
                "path": spec["path"],
                "query_string": spec["query-string"],
                "user_agent": spec["user-agent"],
                "max_bytes_read": spec["body-bytes"],
            }
        )

        cmd = HttpMeasureCommand()
        cmd.init_args([
            "http",
            "--target", "ripe.net",
            "--af", "6",
            "--description", "This is my description",
            "--header-bytes", "100",
            "--version", "99",
            "--method", "a-method",
            "--port", "7",
            "--path", "/path/to/something",
            "--query-string", "x=7",
            "--user-agent", "This is my custom user agent",
            "--body-bytes", "200",
            "--timing-verbosity", "2"
        ])
        self.assertEqual(
            cmd._get_measurement_kwargs(),
            {
                "af": 6,
                "description": "This is my description",
                "target": "ripe.net",
                "header_bytes": 100,
                "version": "99",
                "method": "a-method",
                "port": 7,
                "path": "/path/to/something",
                "query_string": "x=7",
                "user_agent": "This is my custom user agent",
                "max_bytes_read": 200,
                "extended_timing": True,
                "more_extended_timing": True
            }
        )
Exemple #2
0
    def test_add_arguments(self):

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--renderer", "not-a-renderer"])
            self.assertTrue(stderr.getvalue().split("\n")[-2].startswith(
                "ripe-atlas measure: error: argument --renderer: invalid "
                "choice: 'not-a-renderer' (choose from"))

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(["ping", "--af", "5"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --af: invalid choice: 5 "
                "(choose from 4, 6)")

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--target", "not a target"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --target: "
                "\"not a target\" does not appear to be an IP address or host "
                "name")

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--from-area", "not an area"])
            self.assertTrue(stderr.getvalue().split("\n")[-2].startswith(
                "ripe-atlas measure: error: argument --from-area: invalid "
                "choice:"))

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--from-probes", "0,50000"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --from-probes: 0 "
                "is lower than the minimum permitted value of 1.")

        for clude in ("in", "ex"):
            with capture_sys_output() as (stdout, stderr):
                for tag in ("NotATag", "tag!", "not a tag", "νοτ α ταγ"):
                    with self.assertRaises(SystemExit):
                        PingMeasureCommand().init_args(
                            ["ping", "--{}clude-tag".format(clude), tag])
                    self.assertEqual(
                        stderr.getvalue().split("\n")[-2],
                        'ripe-atlas measure: error: argument --{}clude-tag: '
                        '"{}" does not appear to be valid.'.format(clude, tag))

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                TracerouteMeasureCommand().init_args(
                    ["traceroute", "--protocol", "invalid"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --protocol: invalid "
                "choice: 'invalid' (choose from 'ICMP', 'UDP', 'TCP')")

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                DnsMeasureCommand().init_args(["dns", "--protocol", "invalid"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --protocol: invalid "
                "choice: 'invalid' (choose from 'UDP', 'TCP')")

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                HttpMeasureCommand().init_args(
                    ["http", "--timing-verbosity", "3"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --timing-verbosity: "
                "invalid choice: 3 (choose from 0, 1, 2)")

        min_options = {
            "from-measurement": ((PingMeasureCommand, ), 1),
            "probes": ((PingMeasureCommand, ), 1),
            "packets": ((PingMeasureCommand, TracerouteMeasureCommand,
                         NtpMeasureCommand), 1),
            "size": ((PingMeasureCommand, TracerouteMeasureCommand), 1),
            "packet-interval": ((PingMeasureCommand, ), 1),
            "timeout": ((
                TracerouteMeasureCommand,
                NtpMeasureCommand,
            ), 1),
            "destination-option-size": ((TracerouteMeasureCommand, ), 1),
            "hop-by-hop-option-size": ((TracerouteMeasureCommand, ), 1),
            "retry": ((DnsMeasureCommand, ), 1),
            "udp-payload-size": ((DnsMeasureCommand, ), 1),
        }
        for option, (klasses, minimum) in min_options.items():
            for klass in klasses:
                with capture_sys_output() as (stdout, stderr):
                    test_value = minimum - 1
                    with self.assertRaises(SystemExit):
                        klass().init_args([
                            klass.__name__.replace("MeasureCommand",
                                                   "").lower(),
                            "--{}".format(option),
                            str(test_value)
                        ])
                    self.assertEqual(
                        stderr.getvalue().split("\n")[-2],
                        "ripe-atlas measure: error: argument --{}: "
                        "The integer must be greater than {}.".format(
                            option, minimum))

        min_max_options = {
            "from-asn": ((PingMeasureCommand, ), (0, 2**32 - 2 + 1)),
            "paris": ((TracerouteMeasureCommand, ), (-1, 65)),
            "first-hop": ((TracerouteMeasureCommand, ), (0, 256)),
            "max-hops": ((TracerouteMeasureCommand, ), (0, 256)),
            "port": ((TracerouteMeasureCommand, SslcertMeasureCommand,
                      HttpMeasureCommand), (0, 2**16 + 1)),
            "header-bytes": ((HttpMeasureCommand, ), (-1, 2049)),
            "body-bytes": ((HttpMeasureCommand, ), (0, 1020049)),
        }
        for option, (klasses, extremes) in min_max_options.items():
            for klass in klasses:
                for val in extremes:
                    with capture_sys_output() as (stdout, stderr):
                        with self.assertRaises(SystemExit):
                            klass().init_args([
                                klass.__name__.replace("MeasureCommand",
                                                       "").lower(),
                                "--{}".format(option),
                                str(val)
                            ])
                        self.assertEqual(
                            stderr.getvalue().split("\n")[-2],
                            "ripe-atlas measure: error: argument --{}: The "
                            "integer must be between {} and {}.".format(
                                option, extremes[0] + 1, extremes[1] - 1))
Exemple #3
0
    def test_add_arguments(self):

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--set-alias", "\\invalid"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                'ripe-atlas measure: error: argument --set-alias: '
                '"\\invalid" does not appear to be a valid alias.')

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--renderer", "not-a-renderer"])
            self.assertTrue(stderr.getvalue().split("\n")[-2].startswith(
                "ripe-atlas measure: error: argument --renderer: invalid "
                "choice: 'not-a-renderer' (choose from"))

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(["ping", "--af", "5"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --af: invalid choice: 5 "
                "(choose from 4, 6)")

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--target", "not a target"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --target: "
                "\"not a target\" does not appear to be an IP address or host "
                "name")

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--from-area", "not an area"])
            self.assertTrue(stderr.getvalue().split("\n")[-2].startswith(
                "ripe-atlas measure: error: argument --from-area: invalid "
                "choice:"))

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                PingMeasureCommand().init_args(
                    ["ping", "--from-probes", "0,50000"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --from-probes: 0 "
                "is lower than the minimum permitted value of 1.")

        for clude in ("in", "ex"):
            with capture_sys_output() as (stdout, stderr):
                for tag in ("NotATag", "tag!", "not a tag", "νοτ α ταγ"):
                    with self.assertRaises(SystemExit):
                        PingMeasureCommand().init_args(
                            ["ping", "--{}clude-tag".format(clude), tag])
                    self.assertEqual(
                        stderr.getvalue().split("\n")[-2],
                        'ripe-atlas measure: error: argument --{}clude-tag: '
                        '"{}" does not appear to be a valid tag.'.format(
                            clude, tag))

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                TracerouteMeasureCommand().init_args(
                    ["traceroute", "--protocol", "invalid"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --protocol: invalid "
                "choice: 'INVALID' (choose from 'ICMP', 'UDP', 'TCP')")

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                DnsMeasureCommand().init_args(["dns", "--protocol", "invalid"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --protocol: invalid "
                "choice: 'INVALID' (choose from 'UDP', 'TCP')")

        with capture_sys_output() as (stdout, stderr):
            with self.assertRaises(SystemExit):
                HttpMeasureCommand().init_args(
                    ["http", "--timing-verbosity", "3"])
            self.assertEqual(
                stderr.getvalue().split("\n")[-2],
                "ripe-atlas measure: error: argument --timing-verbosity: "
                "invalid choice: 3 (choose from 0, 1, 2)")

        min_options = [
            ("from-measurement", ((PingMeasureCommand, ), 1)),
            ("probes", ((PingMeasureCommand, ), 1)),
            ("timeout", ((TracerouteMeasureCommand, ), 1)),
        ]
        for option, (klasses, minimum) in min_options:
            for klass in klasses:
                with capture_sys_output() as (stdout, stderr):
                    test_value = minimum - 1
                    with self.assertRaises(SystemExit):
                        klass().init_args([
                            klass.__name__.replace("MeasureCommand",
                                                   "").lower(),
                            "--{}".format(option),
                            str(test_value)
                        ])
                    self.assertEqual(
                        stderr.getvalue().split("\n")[-2],
                        "ripe-atlas measure: error: argument --{}: "
                        "The integer must be greater than {}.".format(
                            option, minimum))

        min_max_options = [
            ("from-asn", ((PingMeasureCommand, ), (1, 2**32 - 2))),
            ("paris", ((TracerouteMeasureCommand, ), (0, 64))),
            ("first-hop", ((TracerouteMeasureCommand, ), (1, 255))),
            ("max-hops", ((TracerouteMeasureCommand, ), (1, 255))),
            ("port", ((TracerouteMeasureCommand, SslcertMeasureCommand,
                       HttpMeasureCommand), (1, 65535))),
            ("header-bytes", ((HttpMeasureCommand, ), (0, 2048))),
            ("body-bytes", ((HttpMeasureCommand, ), (1, 1020048))),
            ("size", ((PingMeasureCommand, ), (1, 2048))),
            ("size", ((TracerouteMeasureCommand, ), (0, 2048))),
            ("packets", ((
                PingMeasureCommand,
                TracerouteMeasureCommand,
                NtpMeasureCommand,
            ), (1, 16))),
            ("packet-interval", ((PingMeasureCommand, ), (2, 30000))),
            ("timeout", ((NtpMeasureCommand, ), (1, 60000))),
            ("destination-option-size", ((TracerouteMeasureCommand, ),
                                         (0, 1024))),
            ("hop-by-hop-option-size", ((TracerouteMeasureCommand, ), (0,
                                                                       2048))),
            ("retry", ((DnsMeasureCommand, ), (0, 10))),
            ("udp-payload-size", ((DnsMeasureCommand, ), (512, 4096))),
        ]
        for option, (klasses, extremes) in min_max_options:
            for klass in klasses:
                lower = extremes[0] - 1
                upper = extremes[1] + 1
                for invalid in lower, upper:
                    with capture_sys_output() as (stdout, stderr):
                        try:
                            klass().init_args([
                                klass.__name__.replace("MeasureCommand",
                                                       "").lower(),
                                "--{}".format(option),
                                str(invalid)
                            ])
                        except SystemExit:
                            pass
                        else:
                            self.fail(
                                "--{} for {} should fail for value {}".format(
                                    option, klass.__name__, invalid))
                        self.assertEqual(
                            stderr.getvalue().split("\n")[-2],
                            "ripe-atlas measure: error: argument --{}: The "
                            "integer must be between {} and {}.".format(
                                option, extremes[0], extremes[1]))