def test_get_zone_dump(mock_find_name_servers, mock_get_answer): mock_find_name_servers.return_value = ['ns1.vk.ru', 'ns4.vk.ru'] f_header = _Header(2932, MessageType.RESPONSE, 1, response_type=ResponseType.REFUSED) s_header = _Header(2928, MessageType.RESPONSE, 1, answer_count=1) questions = [_Question('vk.com', RRType.AXFR)] mock_get_answer.side_effect = [ Answer(f_header, questions, [], [], []), Answer(s_header, questions, [], [], []) ] assert not resolver.get_zone_dump('vk.com', port=53, timeout=10).answers
def test_resolve_ip4_recursive(mock_get_answer): args = Namespace(hostname='vk.com', protocol='udp', server='8.8.8.8', port=53, timeout=10, inverse=False, dump=False, debug=False, ipv6=False) header = _Header(1823, MessageType.RESPONSE, question_count=1, answer_count=1) questions = [_Question('vk.com')] answers = [ _ResourceRecord('vk.com', type_=RRType.A, length=4, data=_AResourceData(b'\x57\xf0\xb6\xe0')) ] mock_get_answer.return_value = Answer(header, questions, answers, [], []) assert resolver.resolve(args).answers[0].data.ip == '87.240.182.224'
def test_get_primary_name_server_1(mock_find_name_servers, mock_get_answer): mock_find_name_servers.return_value = [ 'ns4.vkontakte.ru', 'ns2.vkontakte.ru' ] header = _Header(9633, MessageType.RESPONSE, question_count=1, answer_count=4) questions = [_Question('vk.com', type_=RRType.NS)] answers = [ _ResourceRecord( 'vk.com', type_=RRType.NS, length=18, data=_SOAResourceData( b'\xc0\x0c\x00\x06\x00\x01\x00\x00\x02\x0b\x00\x44' b'\x03ns1\x09vkontakte\x02ru\x00' b'\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', offset=0)) ] mock_get_answer.return_value = Answer(header, questions, answers, [], []) assert resolver.get_primary_name_server('vk.com', protocol='udp', port=53, timeout=10) == 'ns1.vkontakte.ru'
def test_one_A(self): in_bytes = b'\x00\x00\x80\x00\x00\x01\x00\x01\x00\x00\x00\x00' \ b'\x06google\x03com\x00\x00\x01\x00\x01' \ b'\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x00' \ b'\x00\x04\xac\xd9\x0en' a_resource_data = _AResourceData(b'\xac\xd9\x0en') actual = Answer.from_bytes(in_bytes) self.assertEqual(0, actual.header.identifier) self.assertEqual(MessageType.RESPONSE, actual.header.message_type) self.assertEqual(QueryType.STANDARD, actual.header.query_type) self.assertEqual(False, actual.header.is_authority_answer) self.assertEqual(False, actual.header.is_truncated) self.assertEqual(False, actual.header.is_recursion_desired) self.assertEqual(False, actual.header.is_recursion_available) self.assertEqual(ResponseType.NO_ERROR, actual.header.response_type) self.assertEqual('google.com', actual.questions[0].name) self.assertEqual(RRType.A, actual.questions[0].type_) self.assertEqual(RRClass.IN, actual.questions[0].class_) self.assertEqual('google.com', actual.answers[0].name) self.assertEqual(RRType.A, actual.answers[0].type_) self.assertEqual(RRClass.IN, actual.answers[0].class_) self.assertEqual(0, actual.answers[0].ttl) self.assertEqual(4, actual.answers[0].length) self.assertEqual(a_resource_data.ip, actual.answers[0].data.ip)
def test_one_A(self): header = _Header(0, MessageType.RESPONSE, 1) question = [_Question('google.com')] a_resource_data = _AResourceData(b'\xac\xd9\x0en') answers = [ _ResourceRecord('google.com', type_=RRType.A, length=4, data=a_resource_data, ttl=0) ] authorities = [] additions = [] actual = Answer(header, question, answers, authorities, additions) self.assertEqual(0, actual.header.identifier) self.assertEqual(MessageType.RESPONSE, actual.header.message_type) self.assertEqual(QueryType.STANDARD, actual.header.query_type) self.assertEqual(False, actual.header.is_authority_answer) self.assertEqual(False, actual.header.is_truncated) self.assertEqual(False, actual.header.is_recursion_desired) self.assertEqual(False, actual.header.is_recursion_available) self.assertEqual(ResponseType.NO_ERROR, actual.header.response_type) self.assertEqual('google.com', actual.questions[0].name) self.assertEqual(RRType.A, actual.questions[0].type_) self.assertEqual(RRClass.IN, actual.questions[0].class_) self.assertEqual('google.com', actual.answers[0].name) self.assertEqual(RRType.A, actual.answers[0].type_) self.assertEqual(RRClass.IN, actual.answers[0].class_) self.assertEqual(0, actual.answers[0].ttl) self.assertEqual(4, actual.answers[0].length) self.assertEqual(a_resource_data.ip, actual.answers[0].data.ip)
def test_resolve_reverse_lookup(mock_get_answer): args = Namespace(hostname='87.240.182.224', protocol='udp', server='8.8.8.8', port=53, timeout=10, inverse=True, dump=False, debug=False, ipv6=False) f_header = _Header(1823, MessageType.RESPONSE, question_count=1, answer_count=0, authority_count=1) s_header = _Header(2938, MessageType.RESPONSE, question_count=1, answer_count=1) questions = [_Question('vk.com')] authorities = [ _ResourceRecord('vk.com', type_=RRType.NS, length=18, data=_NSResourceData( b'\xc0\x0c\x00\x02\x00\x01\x00\x00\x02\x0b\x00\x12' b'\x03ns4\x09vkontakte\x02ru\x00', offset=0)) ] answers = [ _ResourceRecord('vk.com', type_=RRType.PTR, length=8, data=_PTRResourceData(b'\x02vk\x03com\x00')) ] first = Answer(f_header, questions, [], authorities, []) second = Answer(s_header, questions, answers, [], []) mock_get_answer.side_effect = [first, second] assert ( resolver.resolve_reverse_lookup(args).answers[0].data.name == 'vk.com')
def test_get_primary_name_server_2(mock_find_name_servers, mock_get_answer): mock_find_name_servers.return_value = [ 'ns4.vkontakte.ru', 'ns2.vkontakte.ru' ] header = _Header(9633, MessageType.RESPONSE, question_count=1, answer_count=0) questions = [_Question('vk.com', type_=RRType.NS)] mock_get_answer.return_value = Answer(header, questions, [], [], []) assert resolver.get_primary_name_server( 'vk.com', protocol='udp', port=53, timeout=10) is None
def get_answer(hostname, record_type, *, inverse=False, ipv6=False, protocol, server, port, timeout): if inverse: hostname = get_ip_reverse_notation(hostname, ipv6=ipv6) response = send_query(hostname=hostname, record_type=record_type, protocol=protocol, server=server, port=port, timeout=timeout) try: answer = Answer.from_bytes(response) except InvalidAnswer as e: raise InvalidServerResponse from e return answer
def test_find_name_servers(mock_get_answer, mock_get_root_servers): mock_get_root_servers.return_value = {'198.41.0.4'} header = _Header(9633, MessageType.RESPONSE, question_count=1, answer_count=4) questions = [_Question('vk.com', type_=RRType.NS)] answers = [ _ResourceRecord('vk.com', type_=RRType.NS, length=18, data=_NSResourceData( b'\xc0\x0c\x00\x02\x00\x01\x00\x00\x02\x0b\x00\x12' b'\x03ns4\x09vkontakte\x02ru\x00', offset=0)) ] mock_get_answer.return_value = Answer(header, questions, answers, [], []) assert resolver.find_name_servers('vk.com', protocol='udp', port=53, timeout=10) == ['ns4.vkontakte.ru']
def test_A(self, mock_socket_recv): response = resolver.tcp_query(self.bytes_, **self.argv) answer = Answer.from_bytes(response) self.assertEqual(answer.questions[0].name, 'google.com') self.assertEqual(answer.answers[0].data.ip, '74.125.232.231')
def test_A(self, mock_socket_recv): response = resolver.udp_query(self.bytes_, **self.argv) answer = Answer.from_bytes(response) self.assertEqual(answer.answers[0].name, 'yandex.com') self.assertEqual(answer.answers[0].data.ip, '213.180.204.62')