blob: 3a8bd838b0037a4e7dbe3858cf89825abce848b7 [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
Klement Sekera0e3c0de2016-09-29 14:43:44 +020050class NumericConstant(object):
51 __metaclass__ = ABCMeta
52
53 desc_dict = {}
54
55 @abstractmethod
56 def __init__(self, value):
57 self._value = value
58
59 def __int__(self):
60 return self._value
61
62 def __long__(self):
63 return self._value
64
65 def __str__(self):
66 if self._value in self.desc_dict:
67 return self.desc_dict[self._value]
68 return ""
69
70
Matej Klotton0178d522016-11-04 11:11:44 +010071class Host(object):
72 """ Generic test host "connected" to VPPs interface. """
Damjan Marionf56b77a2016-10-03 19:44:57 +020073
Klement Sekeraf62ae122016-10-11 11:47:09 +020074 @property
75 def mac(self):
76 """ MAC address """
77 return self._mac
Damjan Marionf56b77a2016-10-03 19:44:57 +020078
Klement Sekeraf62ae122016-10-11 11:47:09 +020079 @property
80 def ip4(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +010081 """ IPv4 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +020082 return self._ip4
Damjan Marionf56b77a2016-10-03 19:44:57 +020083
Klement Sekeraf62ae122016-10-11 11:47:09 +020084 @property
Matej Klotton0178d522016-11-04 11:11:44 +010085 def ip4n(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +010086 """ IPv4 address of remote host - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010087 return socket.inet_pton(socket.AF_INET, self._ip4)
88
89 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020090 def ip6(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +010091 """ IPv6 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +020092 return self._ip6
Damjan Marionf56b77a2016-10-03 19:44:57 +020093
Klement Sekera46a87ad2017-01-02 08:22:23 +010094 @property
95 def ip6n(self):
96 """ IPv6 address of remote host - raw, suitable as API parameter."""
97 return socket.inet_pton(socket.AF_INET6, self._ip6)
98
Klement Sekeraf62ae122016-10-11 11:47:09 +020099 def __init__(self, mac=None, ip4=None, ip6=None):
100 self._mac = mac
101 self._ip4 = ip4
102 self._ip6 = ip6
Filip Tehlar770e89e2017-01-31 10:39:16 +0100103
104
105class ForeignAddressFactory(object):
106 count = 0
107 prefix_len = 24
108 net_template = '10.10.10.{}'
109 net = net_template.format(0) + '/' + str(prefix_len)
110
111 def get_ip4(self):
112 if self.count > 255:
113 raise Exception("Network host address exhaustion")
114 self.count += 1
115 return self.net_template.format(self.count)