def test_get_exception_message(self): self.assertEqual('oops', get_exception_message(ValueError('oops'))) try: raise ValueError('oops') except ValueError as error: self.assertEqual('oops', get_exception_message(error)) self.assertEqual('ValueError', get_exception_message(ValueError())) try: raise ValueError except ValueError as error: self.assertEqual('ValueError', get_exception_message(error)) try: raise ValueError() except ValueError as error: self.assertEqual('ValueError', get_exception_message(error)) self.assertEqual('NoNameservers', get_exception_message(NoNameservers())) try: raise NoNameservers except NoNameservers as error: self.assertEqual('NoNameservers', get_exception_message(error)) try: raise NoNameservers() except NoNameservers as error: self.assertEqual('NoNameservers', get_exception_message(error))
def test_record_NoNameservers(self): """ Tests the method which let us get the PTR record for the case that we get a NoNameservers exception. """ self.dns_lookup.resolver.query = Mock(side_effect=NoNameservers()) expected = None actual = self.dns_lookup.ptr_record(self.subject) self.assertEqual(expected, actual)
def ip(self): # Determine the domain's IP address try: req = query(self.dom) except NXDOMAIN: raise NXDOMAIN("Check if %r exists." % self.dom) except NoNameservers: raise NoNameservers('No Nameservers found for domain: %r' % self.dom) except NoAnswer: raise NoAnswer('No IP available for domain: %s' % self.dom) self.addr = [str(ip.address) for ip in req]
def lookup(domain): 'Lookup if domain is using CDN. This is "bad" CDN lookup coz it use known CDN list to find if the domain use CDN.' print('Looking up via DNS..') CDNs = [] print('Checking Multiple IP...') try: asw = query(domain) except NXDOMAIN: raise NXDOMAIN("Are you sure %r exist?" % domain) except NoNameservers: raise NoNameservers('No nameserver found for %r' % domain) except NoAnswer: raise NoAnswer('No IP for %s!' % domain) print('Found IP: %s' % [str(a.address) for a in asw]) #print('Probably %s is using CDN (maybe :P)!'%domain) # check whether domain is using CNAME (but doesn't mean it's use CDN..) print('Checking CNAME...') try: asw = query(domain, 'cname') domains = [a.to_text() for a in asw] print('Found that %s have another domains: %s' % (domain, ','.join(domains))) for d in domains: cdn_found = find_cdn(d) if cdn_found: print('Got CDN from CNAME!') if cdn_found not in CDNs: CDNs.append(cdn_found) except NoAnswer as err: print('No CNAME found!') print('Checking NS...') try: asw = query(domain, 'ns') print('Found Nameservers: %s' % ','.join([a.to_text() for a in asw])) except NoAnswer: print('No Nameserver? Perhaps you can try SOA query instead') else: print('Checking CDN from NS..') for a in asw: ns = a.to_text() cdn_found = find_cdn(ns) if cdn_found: print('Got CDN from NS!') if cdn_found not in CDNs: CDNs.append(cdn_found) if CDNs: print('CDN used by %s : %s' % (domain, ','.join(CDNs))) else: print('No CDN used by %s' % domain)
def A(domain): 'this func sometime helpful :)' print('Checking Multiple IP...') try: asw = query(domain) except NXDOMAIN: raise NXDOMAIN("Are you sure %r exist?" % domain) except NoNameservers: raise NoNameservers('No nameserver found for %r' % domain) except NoAnswer: raise NoAnswer('No IP for %s!' % domain) print('Found IP: %s' % [str(a.address) for a in asw]) print('Checking CNAME...') try: asw = query(domain, 'cname') domains = [a.to_text() for a in asw] print('Found that %s have another domains: %s' % (domain, ','.join(domains))) except Exception as e: print(e) print()
def test_validate_dns_no_name_servers(self, mocker: MockFixture) -> None: layer = Layer.load_from_yaml( os.path.join(os.getcwd(), "tests", "fixtures", "dummy_data", "dummy_config_parent.yaml"), None, ) dns_module = layer.get_module("awsdns", 6) if dns_module is None: raise Exception("did not find dns module") del dns_module.data["upload_cert"] dns_module.data["delegated"] = True processor = DNSModuleProcessor(dns_module, layer) mocked_get_terraform_outputs = mocker.patch( "modules.base.get_terraform_outputs") mocked_get_terraform_outputs.return_value = { "name_servers": ["blah.com", "baloney.com"] } mocked_query = mocker.patch("modules.base.query") mocked_query.side_effect = NoNameservers( "No name servers found") # type: ignore with pytest.raises(UserErrors): processor.validate_dns() mocked_query.assert_called_once()
def test_rbl(): """ handles failure ok, and handles lookups ok with dns module. """ import mock from dns.resolver import NoAnswer, NXDOMAIN, NoNameservers from pygameweb.user.rbl import rbl assert rbl('127.0.0.1') is False assert rbl('') is False with mock.patch('dns.resolver.query') as query: query.side_effect = NoAnswer() assert rbl('192.168.0.1') is False query.side_effect = NXDOMAIN() assert rbl('192.168.0.1') is False query.side_effect = NoNameservers() assert rbl('192.168.0.1') is False with mock.patch('dns.resolver.query') as query: query.side_effect = '127.0.0.2' assert rbl('192.168.0.1') is True assert query.called
Name("does-not-exist.example.com".split("."))) _MX_RECORDS = [_MX_RECORD_1] _IP_SET_RECORDS = [RRset()] _IPV4_RECORD_1 = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.0.2.1", 25)) _IPV4_RECORD_2 = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.0.2.2", 25)) _IPV6_RECORD = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("2001:db8::1", 25)) _BAD_IP_RECORD = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("BAD", 25)) _IP_RECORDS = [_IPV4_RECORD_1, _IPV6_RECORD] _POSSIBLE_DNS_RESULTS = { "test3.com": [_MX_RECORD_2], "no-mx.example.com": NoAnswer(), "does-not-exist.example.com": NXDOMAIN(), "timeout.example.com": Timeout(), "no-ns-servers.example.com": NoNameservers(), "bad-response.example.com": [_BAD_MX_RECORD], "no-lookup.example.com": [_DNE_MX_RECORD], "no-answer.example.com": [_DNE_MX_RECORD], "dns-checks.com": [_MX_RECORD_2], "invalid-mx.com": [_MX_RECORD_3] } _POSSIBLE_DNS_RESULTS_NO_MX = { "does-not-exist.example.com": NXDOMAIN(), "mx2.example.com": NXDOMAIN(), "no-lookup.example.com": NXDOMAIN(), "no-answer.example.com": NoAnswer(), "bad-response.example.com": [RRsetInvalid()], "dns-checks.com": [ _SPF_RECORD,