1 """ test framework utilities """
5 from abc import abstractmethod, ABCMeta
6 from cStringIO import StringIO
7 from scapy.layers.inet6 import in6_mactoifaceid
10 def ppp(headline, packet):
11 """ Return string containing the output of scapy packet.show() call. """
13 old_stdout = sys.stdout
17 sys.stdout = old_stdout
21 def ppc(headline, capture, limit=10):
22 """ Return string containing ppp() printout for a capture.
24 :param headline: printed as first line of output
25 :param capture: packets to print
26 :param limit: limit the print to # of packets
31 if limit < len(capture):
32 tail = "\nPrint limit reached, %s out of %s packets printed" % (
35 body = "".join([ppp("Packet #%s:" % count, p)
36 for count, p in zip(range(0, limit), capture)])
37 return "%s\n%s%s" % (headline, body, tail)
40 def ip4_range(ip4, s, e):
41 tmp = ip4.rsplit('.', 1)[0]
42 return ("%s.%d" % (tmp, i) for i in range(s, e))
45 def ip4n_range(ip4n, s, e):
46 ip4 = socket.inet_ntop(socket.AF_INET, ip4n)
47 return (socket.inet_pton(socket.AF_INET, ip)
48 for ip in ip4_range(ip4, s, e))
52 """ Convert the : separated format into binary packet data for the API """
53 return mac.replace(':', '').decode('hex')
57 euid = in6_mactoifaceid(mac)
58 addr = "fe80::" + euid
62 class NumericConstant(object):
63 __metaclass__ = ABCMeta
68 def __init__(self, value):
78 if self._value in self.desc_dict:
79 return self.desc_dict[self._value]
84 """ Generic test host "connected" to VPPs interface. """
94 return mactobinary(self._mac)
98 """ IPv4 address - string """
103 """ IPv4 address of remote host - raw, suitable as API parameter."""
104 return socket.inet_pton(socket.AF_INET, self._ip4)
108 """ IPv6 address - string """
113 """ IPv6 address of remote host - raw, suitable as API parameter."""
114 return socket.inet_pton(socket.AF_INET6, self._ip6)
118 """ IPv6 link-local address - string """
123 """ IPv6 link-local address of remote host -
124 raw, suitable as API parameter."""
125 return socket.inet_pton(socket.AF_INET6, self._ip6_ll)
127 def __eq__(self, other):
128 if isinstance(other, Host):
129 return (self.mac == other.mac and
130 self.ip4 == other.ip4 and
131 self.ip6 == other.ip6 and
132 self.ip6_ll == other.ip6_ll)
136 def __ne__(self, other):
137 return not self.__eq__(other)
140 return "Host { mac:%s ip4:%s ip6:%s ip6_ll:%s }" % (self.mac,
146 return hash(self.__repr__())
148 def __init__(self, mac=None, ip4=None, ip6=None, ip6_ll=None):
152 self._ip6_ll = ip6_ll
155 class ForeignAddressFactory(object):
158 net_template = '10.10.10.{}'
159 net = net_template.format(0) + '/' + str(prefix_len)
163 raise Exception("Network host address exhaustion")
165 return self.net_template.format(self.count)