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 |
Neale Ranns | 2a3ea49 | 2017-04-19 05:24:40 -0700 | [diff] [blame] | 7 | from scapy.layers.inet6 import in6_mactoifaceid |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 8 | |
| 9 | |
| 10 | def ppp(headline, packet): |
| 11 | """ Return string containing the output of scapy packet.show() call. """ |
| 12 | o = StringIO() |
| 13 | old_stdout = sys.stdout |
| 14 | sys.stdout = o |
| 15 | print(headline) |
| 16 | packet.show() |
| 17 | sys.stdout = old_stdout |
| 18 | return o.getvalue() |
| 19 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 20 | |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 21 | def ppc(headline, capture, limit=10): |
| 22 | """ Return string containing ppp() printout for a capture. |
| 23 | |
| 24 | :param headline: printed as first line of output |
| 25 | :param capture: packets to print |
| 26 | :param limit: limit the print to # of packets |
| 27 | """ |
| 28 | if not capture: |
| 29 | return headline |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 30 | tail = "" |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 31 | if limit < len(capture): |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 32 | tail = "\nPrint limit reached, %s out of %s packets printed" % ( |
| 33 | len(capture), limit) |
| 34 | limit = len(capture) |
| 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) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 38 | |
| 39 | |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 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)) |
| 43 | |
| 44 | |
| 45 | def ip4n_range(ip4n, s, e): |
| 46 | ip4 = socket.inet_ntop(socket.AF_INET, ip4n) |
Klement Sekera | 72715ee | 2017-01-17 10:37:05 +0100 | [diff] [blame] | 47 | return (socket.inet_pton(socket.AF_INET, ip) |
| 48 | for ip in ip4_range(ip4, s, e)) |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 49 | |
| 50 | |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 51 | def mactobinary(mac): |
| 52 | """ Convert the : separated format into binary packet data for the API """ |
| 53 | return mac.replace(':', '').decode('hex') |
| 54 | |
| 55 | |
Neale Ranns | 2a3ea49 | 2017-04-19 05:24:40 -0700 | [diff] [blame] | 56 | def mk_ll_addr(mac): |
| 57 | euid = in6_mactoifaceid(mac) |
| 58 | addr = "fe80::" + euid |
| 59 | return addr |
| 60 | |
| 61 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 62 | class NumericConstant(object): |
| 63 | __metaclass__ = ABCMeta |
| 64 | |
| 65 | desc_dict = {} |
| 66 | |
| 67 | @abstractmethod |
| 68 | def __init__(self, value): |
| 69 | self._value = value |
| 70 | |
| 71 | def __int__(self): |
| 72 | return self._value |
| 73 | |
| 74 | def __long__(self): |
| 75 | return self._value |
| 76 | |
| 77 | def __str__(self): |
| 78 | if self._value in self.desc_dict: |
| 79 | return self.desc_dict[self._value] |
| 80 | return "" |
| 81 | |
| 82 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 83 | class Host(object): |
| 84 | """ Generic test host "connected" to VPPs interface. """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 85 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 86 | @property |
| 87 | def mac(self): |
| 88 | """ MAC address """ |
| 89 | return self._mac |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 90 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 91 | @property |
Eyal Bari | c86e592 | 2017-07-02 18:33:16 +0300 | [diff] [blame] | 92 | def bin_mac(self): |
| 93 | """ MAC address """ |
| 94 | return mactobinary(self._mac) |
| 95 | |
| 96 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 97 | def ip4(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 98 | """ IPv4 address - string """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 99 | return self._ip4 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 100 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 101 | @property |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 102 | def ip4n(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 103 | """ IPv4 address of remote host - raw, suitable as API parameter.""" |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 104 | return socket.inet_pton(socket.AF_INET, self._ip4) |
| 105 | |
| 106 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 107 | def ip6(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 108 | """ IPv6 address - string """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 109 | return self._ip6 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 110 | |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 111 | @property |
| 112 | def ip6n(self): |
| 113 | """ IPv6 address of remote host - raw, suitable as API parameter.""" |
| 114 | return socket.inet_pton(socket.AF_INET6, self._ip6) |
| 115 | |
Neale Ranns | 2a3ea49 | 2017-04-19 05:24:40 -0700 | [diff] [blame] | 116 | @property |
| 117 | def ip6_ll(self): |
| 118 | """ IPv6 link-local address - string """ |
| 119 | return self._ip6_ll |
| 120 | |
| 121 | @property |
| 122 | def ip6n_ll(self): |
| 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) |
| 126 | |
Eyal Bari | c86e592 | 2017-07-02 18:33:16 +0300 | [diff] [blame] | 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) |
| 133 | else: |
| 134 | return False |
| 135 | |
| 136 | def __ne__(self, other): |
| 137 | return not self.__eq__(other) |
| 138 | |
| 139 | def __repr__(self): |
| 140 | return "Host { mac:%s ip4:%s ip6:%s ip6_ll:%s }" % (self.mac, |
| 141 | self.ip4, |
| 142 | self.ip6, |
| 143 | self.ip6_ll) |
| 144 | |
| 145 | def __hash__(self): |
| 146 | return hash(self.__repr__()) |
| 147 | |
Neale Ranns | 2a3ea49 | 2017-04-19 05:24:40 -0700 | [diff] [blame] | 148 | def __init__(self, mac=None, ip4=None, ip6=None, ip6_ll=None): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 149 | self._mac = mac |
| 150 | self._ip4 = ip4 |
| 151 | self._ip6 = ip6 |
Neale Ranns | 2a3ea49 | 2017-04-19 05:24:40 -0700 | [diff] [blame] | 152 | self._ip6_ll = ip6_ll |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 153 | |
| 154 | |
| 155 | class ForeignAddressFactory(object): |
| 156 | count = 0 |
| 157 | prefix_len = 24 |
| 158 | net_template = '10.10.10.{}' |
| 159 | net = net_template.format(0) + '/' + str(prefix_len) |
| 160 | |
| 161 | def get_ip4(self): |
| 162 | if self.count > 255: |
| 163 | raise Exception("Network host address exhaustion") |
| 164 | self.count += 1 |
| 165 | return self.net_template.format(self.count) |