1 """ test framework utilities """
5 from abc import abstractmethod, ABCMeta
6 from cStringIO import StringIO
9 def ppp(headline, packet):
10 """ Return string containing the output of scapy packet.show() call. """
12 old_stdout = sys.stdout
16 sys.stdout = old_stdout
20 def ppc(headline, capture, limit=10):
21 """ Return string containing ppp() printout for a capture.
23 :param headline: printed as first line of output
24 :param capture: packets to print
25 :param limit: limit the print to # of packets
30 if limit < len(capture):
31 tail = "\nPrint limit reached, %s out of %s packets printed" % (
34 body = "".join([ppp("Packet #%s:" % count, p)
35 for count, p in zip(range(0, limit), capture)])
36 return "%s\n%s%s" % (headline, body, tail)
39 def ip4_range(ip4, s, e):
40 tmp = ip4.rsplit('.', 1)[0]
41 return ("%s.%d" % (tmp, i) for i in range(s, e))
44 def ip4n_range(ip4n, s, e):
45 ip4 = socket.inet_ntop(socket.AF_INET, ip4n)
46 return (socket.inet_pton(socket.AF_INET, ip)
47 for ip in ip4_range(ip4, s, e))
51 """ Convert the : separated format into binary packet data for the API """
52 return mac.replace(':', '').decode('hex')
55 class NumericConstant(object):
56 __metaclass__ = ABCMeta
61 def __init__(self, value):
71 if self._value in self.desc_dict:
72 return self.desc_dict[self._value]
77 """ Generic test host "connected" to VPPs interface. """
86 """ IPv4 address - string """
91 """ IPv4 address of remote host - raw, suitable as API parameter."""
92 return socket.inet_pton(socket.AF_INET, self._ip4)
96 """ IPv6 address - string """
101 """ IPv6 address of remote host - raw, suitable as API parameter."""
102 return socket.inet_pton(socket.AF_INET6, self._ip6)
104 def __init__(self, mac=None, ip4=None, ip6=None):
110 class ForeignAddressFactory(object):
113 net_template = '10.10.10.{}'
114 net = net_template.format(0) + '/' + str(prefix_len)
118 raise Exception("Network host address exhaustion")
120 return self.net_template.format(self.count)