blob: d6b77f9d3c3e90756b087f419cadab49e6b0ba07 [file] [log] [blame]
Klement Sekeraacb9b8e2017-02-14 02:55:31 +01001""" test framework utilities """
2
Matej Klotton0178d522016-11-04 11:11:44 +01003import socket
Klement Sekera7bb873a2016-11-18 07:38:42 +01004import sys
Klement Sekera0e3c0de2016-09-29 14:43:44 +02005from abc import abstractmethod, ABCMeta
Klement Sekera7bb873a2016-11-18 07:38:42 +01006from cStringIO import StringIO
7
8
9def ppp(headline, packet):
10 """ Return string containing the output of scapy packet.show() call. """
11 o = StringIO()
12 old_stdout = sys.stdout
13 sys.stdout = o
14 print(headline)
15 packet.show()
16 sys.stdout = old_stdout
17 return o.getvalue()
18
Damjan Marionf56b77a2016-10-03 19:44:57 +020019
Klement Sekera9225dee2016-12-12 08:36:58 +010020def ppc(headline, capture, limit=10):
21 """ Return string containing ppp() printout for a capture.
22
23 :param headline: printed as first line of output
24 :param capture: packets to print
25 :param limit: limit the print to # of packets
26 """
27 if not capture:
28 return headline
Klement Sekeradab231a2016-12-21 08:50:14 +010029 tail = ""
Klement Sekera9225dee2016-12-12 08:36:58 +010030 if limit < len(capture):
Klement Sekeradab231a2016-12-21 08:50:14 +010031 tail = "\nPrint limit reached, %s out of %s packets printed" % (
32 len(capture), limit)
33 limit = len(capture)
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)
Klement Sekera9225dee2016-12-12 08:36:58 +010037
38
Eyal Barid81da8c2017-01-11 13:39:54 +020039def ip4_range(ip4, s, e):
40 tmp = ip4.rsplit('.', 1)[0]
41 return ("%s.%d" % (tmp, i) for i in range(s, e))
42
43
44def ip4n_range(ip4n, s, e):
45 ip4 = socket.inet_ntop(socket.AF_INET, ip4n)
Klement Sekera72715ee2017-01-17 10:37:05 +010046 return (socket.inet_pton(socket.AF_INET, ip)
47 for ip in ip4_range(ip4, s, e))
Eyal Barid81da8c2017-01-11 13:39:54 +020048
49
Neale Ranns39f9d8b2017-02-16 21:57:05 -080050def mactobinary(mac):
51 """ Convert the : separated format into binary packet data for the API """
52 return mac.replace(':', '').decode('hex')
53
54
Klement Sekera0e3c0de2016-09-29 14:43:44 +020055class NumericConstant(object):
56 __metaclass__ = ABCMeta
57
58 desc_dict = {}
59
60 @abstractmethod
61 def __init__(self, value):
62 self._value = value
63
64 def __int__(self):
65 return self._value
66
67 def __long__(self):
68 return self._value
69
70 def __str__(self):
71 if self._value in self.desc_dict:
72 return self.desc_dict[self._value]
73 return ""
74
75
Matej Klotton0178d522016-11-04 11:11:44 +010076class Host(object):
77 """ Generic test host "connected" to VPPs interface. """
Damjan Marionf56b77a2016-10-03 19:44:57 +020078
Klement Sekeraf62ae122016-10-11 11:47:09 +020079 @property
80 def mac(self):
81 """ MAC address """
82 return self._mac
Damjan Marionf56b77a2016-10-03 19:44:57 +020083
Klement Sekeraf62ae122016-10-11 11:47:09 +020084 @property
85 def ip4(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +010086 """ IPv4 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +020087 return self._ip4
Damjan Marionf56b77a2016-10-03 19:44:57 +020088
Klement Sekeraf62ae122016-10-11 11:47:09 +020089 @property
Matej Klotton0178d522016-11-04 11:11:44 +010090 def ip4n(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +010091 """ IPv4 address of remote host - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010092 return socket.inet_pton(socket.AF_INET, self._ip4)
93
94 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020095 def ip6(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +010096 """ IPv6 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +020097 return self._ip6
Damjan Marionf56b77a2016-10-03 19:44:57 +020098
Klement Sekera46a87ad2017-01-02 08:22:23 +010099 @property
100 def ip6n(self):
101 """ IPv6 address of remote host - raw, suitable as API parameter."""
102 return socket.inet_pton(socket.AF_INET6, self._ip6)
103
Klement Sekeraf62ae122016-10-11 11:47:09 +0200104 def __init__(self, mac=None, ip4=None, ip6=None):
105 self._mac = mac
106 self._ip4 = ip4
107 self._ip6 = ip6
Filip Tehlar770e89e2017-01-31 10:39:16 +0100108
109
110class ForeignAddressFactory(object):
111 count = 0
112 prefix_len = 24
113 net_template = '10.10.10.{}'
114 net = net_template.format(0) + '/' + str(prefix_len)
115
116 def get_ip4(self):
117 if self.count > 255:
118 raise Exception("Network host address exhaustion")
119 self.count += 1
120 return self.net_template.format(self.count)