From dc3d3e5f0a8c4de734187c39888af5fbdb63d8a0 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 25 Jun 2014 20:31:10 +0200 Subject: add inet_ntop/inet_pton functions --- test/test_utils.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_utils.py b/test/test_utils.py index 61820a81..a9a48cd0 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,5 +1,5 @@ from netlib import utils - +import socket def test_hexdump(): assert utils.hexdump("one\0"*10) @@ -11,3 +11,20 @@ def test_cleanBin(): assert utils.cleanBin("\nne") == "\nne" assert utils.cleanBin("\nne", True) == ".ne" +def test_ntop_pton(): + for family, ip_string, packed_ip in ( + (socket.AF_INET, + "127.0.0.1", + "\x7f\x00\x00\x01"), + (socket.AF_INET6, + "2001:0db8:85a3:08d3:1319:8a2e:0370:7344", + " \x01\r\xb8\x85\xa3\x08\xd3\x13\x19\x8a.\x03psD")): + assert ip_string == utils.inet_ntop(family, packed_ip) + assert packed_ip == utils.inet_pton(family, ip_string) + if hasattr(socket, "inet_ntop"): + ntop, pton = socket.inet_ntop, socket.inet_pton + delattr(socket,"inet_ntop") + delattr(socket,"inet_pton") + assert ip_string == utils.inet_ntop(family, packed_ip) + assert packed_ip == utils.inet_pton(family, ip_string) + socket.inet_ntop, socket.inet_pton = ntop, pton \ No newline at end of file -- cgit v1.2.3