Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from abc import abstractmethod |
| 4 | |
| 5 | from scapy.layers.l2 import Ether, Raw |
| 6 | from scapy.layers.inet import IP, UDP |
| 7 | |
| 8 | |
| 9 | class BridgeDomain(object): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 10 | """ Bridge domain abstraction """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 11 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 12 | @property |
| 13 | def frame_pg0_to_pg1(self): |
| 14 | """ Ethernet frame sent from pg0 and expected to arrive at pg1 """ |
| 15 | return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') / |
| 16 | IP(src='1.2.3.4', dst='4.3.2.1') / |
| 17 | UDP(sport=10000, dport=20000) / |
| 18 | Raw('\xa5' * 100)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 19 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 20 | @property |
| 21 | def frame_pg1_to_pg0(self): |
| 22 | """ Ethernet frame sent from pg1 and expected to arrive at pg0 """ |
| 23 | return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / |
| 24 | IP(src='4.3.2.1', dst='1.2.3.4') / |
| 25 | UDP(sport=20000, dport=10000) / |
| 26 | Raw('\xa5' * 100)) |
| 27 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 28 | @abstractmethod |
| 29 | def encapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 30 | """ Encapsulate packet """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 31 | pass |
| 32 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 33 | @abstractmethod |
| 34 | def decapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 35 | """ Decapsulate packet """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 36 | pass |
| 37 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 38 | @abstractmethod |
| 39 | def check_encapsulation(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 40 | """ Verify the encapsulation """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 41 | pass |
| 42 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 43 | def test_decap(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 44 | """ Decapsulation test |
| 45 | Send encapsulated frames from pg0 |
| 46 | Verify receipt of decapsulated frames on pg1 |
| 47 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 48 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 49 | encapsulated_pkt = self.encapsulate(self.frame_pg0_to_pg1) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 50 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 51 | self.pg0.add_stream([encapsulated_pkt, ]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 52 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 53 | self.pg1.enable_capture() |
| 54 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 55 | self.pg_start() |
| 56 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 57 | # Pick first received frame and check if it's the non-encapsulated frame |
| 58 | out = self.pg1.get_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 59 | self.assertEqual(len(out), 1, |
| 60 | 'Invalid number of packets on ' |
| 61 | 'output: {}'.format(len(out))) |
| 62 | pkt = out[0] |
| 63 | |
| 64 | # TODO: add error messages |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 65 | self.assertEqual(pkt[Ether].src, self.frame_pg0_to_pg1[Ether].src) |
| 66 | self.assertEqual(pkt[Ether].dst, self.frame_pg0_to_pg1[Ether].dst) |
| 67 | self.assertEqual(pkt[IP].src, self.frame_pg0_to_pg1[IP].src) |
| 68 | self.assertEqual(pkt[IP].dst, self.frame_pg0_to_pg1[IP].dst) |
| 69 | self.assertEqual(pkt[UDP].sport, self.frame_pg0_to_pg1[UDP].sport) |
| 70 | self.assertEqual(pkt[UDP].dport, self.frame_pg0_to_pg1[UDP].dport) |
| 71 | self.assertEqual(pkt[Raw], self.frame_pg0_to_pg1[Raw]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 72 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 73 | def test_encap(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 74 | """ Encapsulation test |
| 75 | Send frames from pg1 |
| 76 | Verify receipt of encapsulated frames on pg0 |
| 77 | """ |
| 78 | self.pg1.add_stream([self.frame_pg1_to_pg0]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 79 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 80 | self.pg0.enable_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 81 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 82 | self.pg_start() |
| 83 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 84 | # Pick first received frame and check if it's corectly encapsulated. |
| 85 | out = self.pg0.get_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 86 | self.assertEqual(len(out), 1, |
| 87 | 'Invalid number of packets on ' |
| 88 | 'output: {}'.format(len(out))) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 89 | pkt = out[0] |
| 90 | self.check_encapsulation(pkt) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 91 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 92 | payload = self.decapsulate(pkt) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 93 | # TODO: add error messages |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 94 | self.assertEqual(payload[Ether].src, self.frame_pg1_to_pg0[Ether].src) |
| 95 | self.assertEqual(payload[Ether].dst, self.frame_pg1_to_pg0[Ether].dst) |
| 96 | self.assertEqual(payload[IP].src, self.frame_pg1_to_pg0[IP].src) |
| 97 | self.assertEqual(payload[IP].dst, self.frame_pg1_to_pg0[IP].dst) |
| 98 | self.assertEqual(payload[UDP].sport, self.frame_pg1_to_pg0[UDP].sport) |
| 99 | self.assertEqual(payload[UDP].dport, self.frame_pg1_to_pg0[UDP].dport) |
| 100 | self.assertEqual(payload[Raw], self.frame_pg1_to_pg0[Raw]) |