def test_write_response_hostname(self): event = Response(RESP_STATUS["SUCCESS"], ADDR_TYPE["DOMAINNAME"], u"google.com", 8080) data = write_response(event) expected_data = struct.pack("!BBxBB10sH", 0x5, 0x0, 0x3, 10, b"google.com", 8080) self.assertEqual(data, expected_data)
def test_write_response_ipv6(self): event = Response(RESP_STATUS["SUCCESS"], ADDR_TYPE["IPV6"], u"::1", 8080) data = write_response(event) expected_data = struct.pack("!BBxB8HH", 0x5, 0x0, 0x4, 0, 0, 0, 0, 0, 0, 0, 1, 8080) self.assertEqual(data, expected_data)
def test_write_response_ipv4(self): event = Response(RESP_STATUS["SUCCESS"], ADDR_TYPE["IPV4"], u"127.0.0.1", 8080) data = write_response(event) expected_data = struct.pack("!BBxB4BH", 0x5, 0x0, 0x1, 127, 0, 0, 1, 8080) self.assertEqual(data, expected_data)
def test_send_response_with_incorrect_event_detail_port(self): conn = Connection(our_role="server") conn._conn.machine.set_state("response") conn._conn._version = 5 conn._conn._addr_type = ADDR_TYPE["IPV4"] conn._conn._addr = ipaddress.IPv4Address("127.0.0.1") conn._conn._port = 8080 event = Response(RESP_STATUS["SUCCESS"], ADDR_TYPE["IPV4"], "127.0.0.1", 5580) with self.assertRaises(ProtocolError): conn.send(event)
def read_response(data): try: parsed_data = dict(data_structure.Response.parse(data)) except (construct.FieldError, construct.RangeError): return NeedMoreData() except construct.ValidationError: raise ParserError("read_response: Incorrect version.") parsed_data.pop("version") if parsed_data["atyp"] == ADDR_TYPE["DOMAINNAME"]: parsed_data["addr"] = string_func(parsed_data["addr"], encoding="ascii") return Response(**parsed_data)
def test_send_response(self): conn = Connection(our_role="server") conn._conn.machine.set_state("response") conn._conn._version = 5 conn._conn._addr_type = ADDR_TYPE["IPV4"] conn._conn._addr = ipaddress.IPv4Address("127.0.0.1") conn._conn._port = 8080 event = Response(RESP_STATUS["SUCCESS"], ADDR_TYPE["IPV4"], "127.0.0.1", 8080) data = conn.send(event) expected_data = struct.pack("!BBxB4BH", 0x5, 0x0, 0x1, 127, 0, 0, 1, 8080) self.assertEqual(conn._conn.state, "end") self.assertEqual(data, expected_data)