def test_ingest_input(self):
        with tempfile.NamedTemporaryFile('wb', prefix='dnsviz', delete=False) as example_bad_json:
            example_bad_json.write(b'{')

        with tempfile.NamedTemporaryFile('wb', prefix='dnsviz', delete=False) as example_no_version:
            example_no_version.write(b'{}')

        with tempfile.NamedTemporaryFile('wb', prefix='dnsviz', delete=False) as example_invalid_version_1:
            example_invalid_version_1.write(b'{ "_meta._dnsviz.": { "version": 1.11 } }')

        with tempfile.NamedTemporaryFile('wb', prefix='dnsviz', delete=False) as example_invalid_version_2:
            example_invalid_version_2.write(b'{ "_meta._dnsviz.": { "version": 5.0 } }')

        with gzip.open(EXAMPLE_AUTHORITATIVE, 'rb') as example_auth_in:
            with tempfile.NamedTemporaryFile('wb', prefix='dnsviz', delete=False) as example_auth_out:
                example_auth_out.write(example_auth_in.read())

        try:
            args = ['-r', example_auth_out.name]
            arghelper = ArgHelper(self.logger)
            arghelper.build_parser('probe')
            arghelper.parse_args(args)
            arghelper.ingest_input()

            # Bad json
            args = ['-r', example_bad_json.name]
            arghelper = ArgHelper(self.logger)
            arghelper.build_parser('probe')
            arghelper.parse_args(args)
            with self.assertRaises(AnalysisInputError):
                arghelper.ingest_input()

            # No version
            args = ['-r', example_no_version.name]
            arghelper = ArgHelper(self.logger)
            arghelper.build_parser('probe')
            arghelper.parse_args(args)
            with self.assertRaises(AnalysisInputError):
                arghelper.ingest_input()

            # Invalid version
            args = ['-r', example_invalid_version_1.name]
            arghelper = ArgHelper(self.logger)
            arghelper.build_parser('probe')
            arghelper.parse_args(args)
            with self.assertRaises(AnalysisInputError):
                arghelper.ingest_input()

            # Invalid version
            args = ['-r', example_invalid_version_2.name]
            arghelper = ArgHelper(self.logger)
            arghelper.build_parser('probe')
            arghelper.parse_args(args)
            with self.assertRaises(AnalysisInputError):
                arghelper.ingest_input()

        finally:
            for tmpfile in (example_auth_out, example_bad_json, example_no_version, \
                    example_invalid_version_1, example_invalid_version_2):
                os.remove(tmpfile.name)
    def test_recursive_aggregation(self):
        args1 = ['-s', 'ns1.example.com=192.0.2.1,ns1.example.com=[2001:db8::1]',
                        '-s', 'ns1.example.com=192.0.2.4,a.root-servers.net']

        explicit_delegations1 = {
                (WILDCARD_EXPLICIT_DELEGATION, dns.rdatatype.NS):
                        dns.rrset.from_text_list(WILDCARD_EXPLICIT_DELEGATION, 0, dns.rdataclass.IN, dns.rdatatype.NS,
                            ['ns1.example.com', 'a.root-servers.net']),
                (dns.name.from_text('ns1.example.com'), dns.rdatatype.A):
                        dns.rrset.from_text_list(dns.name.from_text('ns1.example.com'), 0, dns.rdataclass.IN, dns.rdatatype.A,
                            ['192.0.2.1', '192.0.2.4']),
                (dns.name.from_text('ns1.example.com'), dns.rdatatype.AAAA):
                        dns.rrset.from_text_list(dns.name.from_text('ns1.example.com'), 0, dns.rdataclass.IN, dns.rdatatype.AAAA,
                            ['2001:db8::1']),
                (dns.name.from_text('a.root-servers.net'), dns.rdatatype.A):
                        dns.rrset.from_text_list(dns.name.from_text('a.root-servers.net'), 0, dns.rdataclass.IN, dns.rdatatype.A,
                            ['198.41.0.4']),
                (dns.name.from_text('a.root-servers.net'), dns.rdatatype.AAAA):
                        dns.rrset.from_text_list(dns.name.from_text('a.root-servers.net'), 0, dns.rdataclass.IN, dns.rdatatype.AAAA,
                            ['2001:503:ba3e::2:30'])
                }

        odd_ports1 = {}

        arghelper1 = ArgHelper(self.logger)
        arghelper1.build_parser('probe')
        arghelper1.parse_args(args1)
        arghelper1.aggregate_delegation_info()
        self.assertEqual(arghelper1.explicit_delegations, explicit_delegations1)
        self.assertEqual(arghelper1.odd_ports, odd_ports1)
    def test_delegation_authoritative_aggregation_errors(self):
        args1 = ['-A', '-N', 'example.com:ns1.example.com=192.0.2.1,ns1.example.com=[2001:db8::1]',
                '-x', 'com:ns1.foo.com=192.0.2.3']

        arghelper1 = ArgHelper(self.logger)
        arghelper1.build_parser('probe')
        arghelper1.parse_args(args1)

        # com is specified with -x but example.com is specified with -N
        with self.assertRaises(argparse.ArgumentTypeError):
            arghelper1.aggregate_delegation_info()
Beispiel #4
0
    def test_th_factories(self):
        args = ['example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertIsNone(arghelper.th_factories)

        args = ['-u', 'http://example.com/', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertIsInstance(arghelper.th_factories[0],
                              transport.DNSQueryTransportHandlerHTTPFactory)

        args = ['-u', 'ws:///dev/null', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertIsInstance(
            arghelper.th_factories[0],
            transport.DNSQueryTransportHandlerWebSocketServerFactory)

        args = ['-u', 'ssh://example.com/', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertIsInstance(
            arghelper.th_factories[0],
            transport.DNSQueryTransportHandlerRemoteCmdFactory)
Beispiel #5
0
    def test_ip4_ipv6(self):
        args = []
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(arghelper.try_ipv4, True)
        self.assertEqual(arghelper.try_ipv6, True)

        args = ['-4', '-6']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(arghelper.try_ipv4, True)
        self.assertEqual(arghelper.try_ipv6, True)

        args = ['-4']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(arghelper.try_ipv4, True)
        self.assertEqual(arghelper.try_ipv6, False)

        args = ['-6']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(arghelper.try_ipv4, False)
        self.assertEqual(arghelper.try_ipv6, True)
Beispiel #6
0
    def test_delegation_authoritative_aggregation(self):
        args1 = [
            '-A', '-N',
            'example.com:ns1.example.com=192.0.2.1,ns1.example.com=[2001:db8::1]',
            '-x', 'foo.com:ns1.foo.com=192.0.2.3:50503'
        ]

        explicit_delegations1 = {
            (dns.name.from_text('com'), dns.rdatatype.NS):
            dns.rrset.from_text_list(dns.name.from_text('com'), 0,
                                     dns.rdataclass.IN, dns.rdatatype.NS,
                                     ['localhost']),
            (dns.name.from_text('foo.com'), dns.rdatatype.NS):
            dns.rrset.from_text_list(dns.name.from_text('foo.com'), 0,
                                     dns.rdataclass.IN, dns.rdatatype.NS,
                                     ['ns1.foo.com']),
            (dns.name.from_text('ns1.foo.com'), dns.rdatatype.A):
            dns.rrset.from_text_list(dns.name.from_text('ns1.foo.com'), 0,
                                     dns.rdataclass.IN, dns.rdatatype.A,
                                     ['192.0.2.3']),
        }

        for ex in (explicit_delegations1, ):
            if self.use_ipv6:
                ex[(dns.name.from_text('localhost'), dns.rdatatype.AAAA)] = \
                        dns.rrset.from_text_list(dns.name.from_text('localhost'), 0, dns.rdataclass.IN, dns.rdatatype.AAAA,
                            ['::1'])
                loopback_ip = IPAddr('::1')
            else:
                ex[(dns.name.from_text('localhost'), dns.rdatatype.A)] = \
                        dns.rrset.from_text_list(dns.name.from_text('localhost'), 0, dns.rdataclass.IN, dns.rdatatype.A,
                            ['127.0.0.1'])
                loopback_ip = IPAddr('127.0.0.1')

        odd_ports1 = {
            (dns.name.from_text('com'), loopback_ip): self.first_port,
            (dns.name.from_text('foo.com'), IPAddr('192.0.2.3')): 50503,
        }

        ZoneFileToServe._next_free_port = self.first_port

        arghelper1 = ArgHelper(self.resolver, self.logger)
        arghelper1.build_parser('probe', args1)
        arghelper1.aggregate_delegation_info()
        self.assertEqual(arghelper1.explicit_delegations,
                         explicit_delegations1)
        self.assertEqual(arghelper1.odd_ports, odd_ports1)
Beispiel #7
0
    def test_client_ip(self):
        args = []
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertIsNone(arghelper.client_ipv4)
        self.assertIsNone(arghelper.client_ipv6)

        args = ['-b', '127.0.0.1']
        if self.use_ipv6:
            args.extend(['-b', '::1'])
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(arghelper.client_ipv4, IPAddr('127.0.0.1'))
        if self.use_ipv6:
            self.assertEqual(arghelper.client_ipv6, IPAddr('::1'))
Beispiel #8
0
    def test_edns_options(self):
        CustomQueryMixin.edns_options = self.custom_query_mixin_edns_options_orig[:]

        # None
        args = ['-c', '', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(len(CustomQueryMixin.edns_options), 0)

        CustomQueryMixin.edns_options = self.custom_query_mixin_edns_options_orig[:]

        # Only DNS cookie
        args = ['example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(set([o.otype for o in CustomQueryMixin.edns_options]),
                         set([10]))

        CustomQueryMixin.edns_options = self.custom_query_mixin_edns_options_orig[:]

        # All EDNS options
        args = ['-n', '-e', '192.0.2.0/24', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(set([o.otype for o in CustomQueryMixin.edns_options]),
                         set([3, 8, 10]))

        CustomQueryMixin.edns_options = self.custom_query_mixin_edns_options_orig[:]
Beispiel #9
0
    def test_ceiling(self):
        args = ['-a', 'com', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(arghelper.ceiling, dns.name.from_text('com'))

        args = ['example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertEqual(arghelper.ceiling, dns.name.root)

        args = ['-A', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.set_kwargs()
        self.assertIsNone(arghelper.ceiling)
Beispiel #10
0
    def test_option_combination_errors(self):

        # Names, input file, or names file required
        args = []
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        with self.assertRaises(argparse.ArgumentTypeError):
            arghelper.check_args()

        # Names file and command-line domain names are mutually exclusive
        args = ['-f', '/dev/null', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        with self.assertRaises(argparse.ArgumentTypeError):
            arghelper.check_args()
        arghelper.args.names_file.close()

        # Authoritative analysis and recursive servers
        args = ['-A', '-s', '192.0.2.1', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        with self.assertRaises(argparse.ArgumentTypeError):
            arghelper.check_args()

        # Authoritative servers with recursive analysis
        args = ['-x', 'example.com:ns1.example.com=192.0.2.1', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        with self.assertRaises(argparse.ArgumentTypeError):
            arghelper.check_args()

        # Delegation information with recursive analysis
        args = ['-N', 'example.com:ns1.example.com=192.0.2.1', 'example.com']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        with self.assertRaises(argparse.ArgumentTypeError):
            arghelper.check_args()

        # Delegation information with recursive analysis
        args = [
            '-D',
            'example.com:34983 10 1 EC358CFAAEC12266EF5ACFC1FEAF2CAFF083C418',
            'example.com'
        ]
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        with self.assertRaises(argparse.ArgumentTypeError):
            arghelper.check_args()
Beispiel #11
0
    def test_delegation_aggregation(self):
        args1 = [
            '-A', '-N',
            'example.com:ns1.example.com=192.0.2.1,ns1.example.com=[2001:db8::1]',
            '-N', 'example.com:ns1.example.com=192.0.2.4', '-N',
            'example.com:ns2.example.com=192.0.2.2', '-N',
            'example.com:ns3.example.com=192.0.2.3'
        ]
        args2 = [
            '-A', '-N', 'example.com:ns1.example.com=192.0.2.1', '-D',
            'example.com:34983 10 1 EC358CFAAEC12266EF5ACFC1FEAF2CAFF083C418',
            '-D',
            'example.com:34983 10 2 608D3B089D79D554A1947BD10BEC0A5B1BDBE67B4E60E34B1432ED00 33F24B49'
        ]
        args3 = [
            '-A', '-N', 'example.com:ns1.example.com=192.0.2.1', '-N',
            'example1.com:ns1.example1.com=192.0.2.2'
        ]
        args4 = [
            '-A', '-N', 'example.com:ns1.example.com=192.0.2.1', '-N',
            'example.net:ns1.example.net=192.0.2.2'
        ]

        explicit_delegations1 = {
            (dns.name.from_text('com'), dns.rdatatype.NS):
            dns.rrset.from_text_list(dns.name.from_text('com'), 0,
                                     dns.rdataclass.IN, dns.rdatatype.NS,
                                     ['localhost']),
        }
        explicit_delegations2 = {
            (dns.name.from_text('com'), dns.rdatatype.NS):
            dns.rrset.from_text_list(dns.name.from_text('com'), 0,
                                     dns.rdataclass.IN, dns.rdatatype.NS,
                                     ['localhost']),
        }
        explicit_delegations3 = {
            (dns.name.from_text('com'), dns.rdatatype.NS):
            dns.rrset.from_text_list(dns.name.from_text('com'), 0,
                                     dns.rdataclass.IN, dns.rdatatype.NS,
                                     ['localhost']),
        }
        explicit_delegations4 = {
            (dns.name.from_text('com'), dns.rdatatype.NS):
            dns.rrset.from_text_list(dns.name.from_text('com'), 0,
                                     dns.rdataclass.IN, dns.rdatatype.NS,
                                     ['localhost']),
            (dns.name.from_text('net'), dns.rdatatype.NS):
            dns.rrset.from_text_list(dns.name.from_text('net'), 0,
                                     dns.rdataclass.IN, dns.rdatatype.NS,
                                     ['localhost']),
        }

        for ex in (explicit_delegations1, explicit_delegations2,
                   explicit_delegations3, explicit_delegations4):
            if self.use_ipv6:
                ex[(dns.name.from_text('localhost'), dns.rdatatype.AAAA)] = \
                        dns.rrset.from_text_list(dns.name.from_text('localhost'), 0, dns.rdataclass.IN, dns.rdatatype.AAAA,
                            ['::1'])
                loopback_ip = IPAddr('::1')
            else:
                ex[(dns.name.from_text('localhost'), dns.rdatatype.A)] = \
                        dns.rrset.from_text_list(dns.name.from_text('localhost'), 0, dns.rdataclass.IN, dns.rdatatype.A,
                            ['127.0.0.1'])
                loopback_ip = IPAddr('127.0.0.1')

        odd_ports1 = {
            (dns.name.from_text('com'), loopback_ip): self.first_port
        }
        odd_ports2 = {
            (dns.name.from_text('com'), loopback_ip): self.first_port
        }
        odd_ports3 = {
            (dns.name.from_text('com'), loopback_ip): self.first_port
        }
        odd_ports4 = {
            (dns.name.from_text('com'), loopback_ip): self.first_port,
            (dns.name.from_text('net'), loopback_ip): self.first_port + 1,
        }

        if self.use_ipv6:
            rdata = b'AAAA ::1'
        else:
            rdata = b'A 127.0.0.1'

        zone_contents1 = b'''@ 600 IN SOA localhost. root.localhost. 1 1800 900 86400 600
@ 600 IN NS @
@ 600 IN ''' + rdata + \
b'''
example 0 IN NS ns1.example
example 0 IN NS ns2.example
example 0 IN NS ns3.example
ns1.example 0 IN A 192.0.2.1
ns1.example 0 IN A 192.0.2.4
ns1.example 0 IN AAAA 2001:db8::1
ns2.example 0 IN A 192.0.2.2
ns3.example 0 IN A 192.0.2.3
'''
        zone_contents2 = b'''@ 600 IN SOA localhost. root.localhost. 1 1800 900 86400 600
@ 600 IN NS @
@ 600 IN ''' + rdata + \
b'''
example 0 IN DS 34983 10 1 ec358cfaaec12266ef5acfc1feaf2caff083c418
example 0 IN DS 34983 10 2 608d3b089d79d554a1947bd10bec0a5b1bdbe67b4e60e34b1432ed0033f24b49
example 0 IN NS ns1.example
ns1.example 0 IN A 192.0.2.1
'''

        ZoneFileToServe._next_free_port = self.first_port

        arghelper1 = ArgHelper(self.resolver, self.logger)
        arghelper1.build_parser('probe', args1)
        arghelper1.aggregate_delegation_info()
        zone_to_serve = arghelper1._zones_to_serve[0]
        zone_obj = dns.zone.from_file(zone_to_serve.filename,
                                      dns.name.from_text('com'))
        zone_obj_other = dns.zone.from_text(zone_contents1,
                                            dns.name.from_text('com'))
        self.assertEqual(zone_obj, zone_obj_other)
        self.assertEqual(arghelper1.explicit_delegations,
                         explicit_delegations1)
        self.assertEqual(arghelper1.odd_ports, odd_ports1)

        ZoneFileToServe._next_free_port = self.first_port

        arghelper2 = ArgHelper(self.resolver, self.logger)
        arghelper2.build_parser('probe', args2)
        arghelper2.aggregate_delegation_info()
        zone_to_serve = arghelper2._zones_to_serve[0]
        zone_obj = dns.zone.from_file(zone_to_serve.filename,
                                      dns.name.from_text('com'))
        zone_obj_other = dns.zone.from_text(zone_contents2,
                                            dns.name.from_text('com'))
        self.assertEqual(zone_obj, zone_obj_other)
        self.assertEqual(arghelper2.explicit_delegations,
                         explicit_delegations2)
        self.assertEqual(arghelper2.odd_ports, odd_ports2)

        ZoneFileToServe._next_free_port = self.first_port

        arghelper3 = ArgHelper(self.resolver, self.logger)
        arghelper3.build_parser('probe', args3)
        arghelper3.aggregate_delegation_info()
        self.assertEqual(arghelper3.explicit_delegations,
                         explicit_delegations3)
        self.assertEqual(arghelper3.odd_ports, odd_ports3)

        ZoneFileToServe._next_free_port = self.first_port

        arghelper4 = ArgHelper(self.resolver, self.logger)
        arghelper4.build_parser('probe', args4)
        arghelper4.aggregate_delegation_info()
        self.assertEqual(arghelper4.explicit_delegations,
                         explicit_delegations4)
        self.assertEqual(arghelper4.odd_ports, odd_ports4)
Beispiel #12
0
    def test_ingest_names(self):
        args = ['example.com', 'example.net']
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.ingest_names()
        self.assertEqual(list(arghelper.names), [
            dns.name.from_text('example.com'),
            dns.name.from_text('example.net')
        ])

        unicode_name = 'テスト'

        args = [unicode_name]
        arghelper = ArgHelper(self.resolver, self.logger)
        arghelper.build_parser('probe', args)
        arghelper.ingest_names()
        self.assertEqual(list(arghelper.names),
                         [dns.name.from_text('xn--zckzah.')])

        with tempfile.NamedTemporaryFile('wb', prefix='dnsviz',
                                         delete=False) as names_file:
            names_file.write('example.com\nexample.net\n'.encode('utf-8'))

        with tempfile.NamedTemporaryFile('wb', prefix='dnsviz',
                                         delete=False) as names_file_unicode:
            try:
                names_file_unicode.write(
                    ('%s\n' % (unicode_name)).encode('utf-8'))
            # python3/python2 dual compatibility
            except UnicodeDecodeError:
                names_file_unicode.write(('%s\n' % (unicode_name)))

        with tempfile.NamedTemporaryFile('wb', prefix='dnsviz',
                                         delete=False) as example_names_only:
            example_names_only.write(
                b'{ "_meta._dnsviz.": { "version": 1.2, "names": [ "example.com.", "example.net.", "example.org." ] } }'
            )

        try:
            args = ['-f', names_file.name]
            arghelper = ArgHelper(self.resolver, self.logger)
            arghelper.build_parser('probe', args)
            arghelper.ingest_names()
            self.assertEqual(list(arghelper.names), [
                dns.name.from_text('example.com'),
                dns.name.from_text('example.net')
            ])

            args = ['-f', names_file_unicode.name]
            arghelper = ArgHelper(self.resolver, self.logger)
            arghelper.build_parser('probe', args)
            arghelper.ingest_names()
            self.assertEqual(list(arghelper.names),
                             [dns.name.from_text('xn--zckzah.')])

            args = ['-r', example_names_only.name]
            arghelper = ArgHelper(self.resolver, self.logger)
            arghelper.build_parser('probe', args)
            arghelper.ingest_input()
            arghelper.ingest_names()
            self.assertEqual(list(arghelper.names), [
                dns.name.from_text('example.com'),
                dns.name.from_text('example.net'),
                dns.name.from_text('example.org')
            ])

            args = ['-r', example_names_only.name, 'example.com']
            arghelper = ArgHelper(self.resolver, self.logger)
            arghelper.build_parser('probe', args)
            arghelper.ingest_input()
            arghelper.ingest_names()
            self.assertEqual(list(arghelper.names),
                             [dns.name.from_text('example.com')])
        finally:
            for tmpfile in (names_file, names_file_unicode,
                            example_names_only):
                os.remove(tmpfile.name)