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 | |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 50 | def mactobinary(mac): |
| 51 | """ Convert the : separated format into binary packet data for the API """ |
| 52 | return mac.replace(':', '').decode('hex') |
| 53 | |
| 54 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 55 | class 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 Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 76 | class Host(object): |
| 77 | """ Generic test host "connected" to VPPs interface. """ |
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 mac(self): |
| 81 | """ MAC address """ |
| 82 | return self._mac |
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 |
| 85 | def ip4(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 86 | """ IPv4 address - string """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 87 | return self._ip4 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 88 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 89 | @property |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 90 | def ip4n(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 91 | """ IPv4 address of remote host - raw, suitable as API parameter.""" |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 92 | return socket.inet_pton(socket.AF_INET, self._ip4) |
| 93 | |
| 94 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 95 | def ip6(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 96 | """ IPv6 address - string """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 97 | return self._ip6 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 98 | |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 99 | @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 Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 104 | def __init__(self, mac=None, ip4=None, ip6=None): |
| 105 | self._mac = mac |
| 106 | self._ip4 = ip4 |
| 107 | self._ip6 = ip6 |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 108 | |
| 109 | |
| 110 | class 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) |