Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 1 | """ test framework utilities """ |
| 2 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 3 | import socket |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 4 | import sys |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 5 | from abc import abstractmethod, ABCMeta |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 6 | from cStringIO import StringIO |
| 7 | |
| 8 | |
| 9 | def 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 Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 19 | |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 20 | def 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 Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 29 | tail = "" |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 30 | if limit < len(capture): |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 31 | 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 Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 37 | |
| 38 | |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 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)) |
| 42 | |
| 43 | |
| 44 | def ip4n_range(ip4n, s, e): |
| 45 | ip4 = socket.inet_ntop(socket.AF_INET, ip4n) |
Klement Sekera | 72715ee | 2017-01-17 10:37:05 +0100 | [diff] [blame] | 46 | return (socket.inet_pton(socket.AF_INET, ip) |
| 47 | for ip in ip4_range(ip4, s, e)) |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 48 | |
| 49 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 50 | class 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 Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 71 | class Host(object): |
| 72 | """ Generic test host "connected" to VPPs interface. """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 73 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 74 | @property |
| 75 | def mac(self): |
| 76 | """ MAC address """ |
| 77 | return self._mac |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 78 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 79 | @property |
| 80 | def ip4(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 81 | """ IPv4 address - string """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 82 | return self._ip4 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 83 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 84 | @property |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 85 | def ip4n(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 86 | """ IPv4 address of remote host - raw, suitable as API parameter.""" |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 87 | return socket.inet_pton(socket.AF_INET, self._ip4) |
| 88 | |
| 89 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 90 | def ip6(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 91 | """ IPv6 address - string """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 92 | return self._ip6 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 93 | |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 94 | @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 Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 99 | def __init__(self, mac=None, ip4=None, ip6=None): |
| 100 | self._mac = mac |
| 101 | self._ip4 = ip4 |
| 102 | self._ip6 = ip6 |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 103 | |
| 104 | |
| 105 | class 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) |