blob: 01e8b855004b81eeb5a0e83aec9f397e078e8cf2 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Klement Sekera0e3c0de2016-09-29 14:43:44 +02003from abc import abstractmethod, ABCMeta
Damjan Marionf56b77a2016-10-03 19:44:57 +02004
5from scapy.layers.l2 import Ether, Raw
6from scapy.layers.inet import IP, UDP
7
8
9class BridgeDomain(object):
Klement Sekeraf62ae122016-10-11 11:47:09 +020010 """ Bridge domain abstraction """
Klement Sekera0e3c0de2016-09-29 14:43:44 +020011 __metaclass__ = ABCMeta
Damjan Marionf56b77a2016-10-03 19:44:57 +020012
Klement Sekeraf62ae122016-10-11 11:47:09 +020013 @property
14 def frame_pg0_to_pg1(self):
15 """ Ethernet frame sent from pg0 and expected to arrive at pg1 """
16 return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') /
17 IP(src='1.2.3.4', dst='4.3.2.1') /
18 UDP(sport=10000, dport=20000) /
19 Raw('\xa5' * 100))
Damjan Marionf56b77a2016-10-03 19:44:57 +020020
Klement Sekeraf62ae122016-10-11 11:47:09 +020021 @property
22 def frame_pg1_to_pg0(self):
23 """ Ethernet frame sent from pg1 and expected to arrive at pg0 """
24 return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
25 IP(src='4.3.2.1', dst='1.2.3.4') /
26 UDP(sport=20000, dport=10000) /
27 Raw('\xa5' * 100))
28
Damjan Marionf56b77a2016-10-03 19:44:57 +020029 @abstractmethod
30 def encapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020031 """ Encapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020032 pass
33
Damjan Marionf56b77a2016-10-03 19:44:57 +020034 @abstractmethod
35 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020036 """ Decapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020037 pass
38
Damjan Marionf56b77a2016-10-03 19:44:57 +020039 @abstractmethod
40 def check_encapsulation(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020041 """ Verify the encapsulation """
Damjan Marionf56b77a2016-10-03 19:44:57 +020042 pass
43
Damjan Marionf56b77a2016-10-03 19:44:57 +020044 def test_decap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020045 """ Decapsulation test
46 Send encapsulated frames from pg0
47 Verify receipt of decapsulated frames on pg1
48 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020049
Klement Sekeraf62ae122016-10-11 11:47:09 +020050 encapsulated_pkt = self.encapsulate(self.frame_pg0_to_pg1)
Damjan Marionf56b77a2016-10-03 19:44:57 +020051
Klement Sekeraf62ae122016-10-11 11:47:09 +020052 self.pg0.add_stream([encapsulated_pkt, ])
Damjan Marionf56b77a2016-10-03 19:44:57 +020053
Klement Sekeraf62ae122016-10-11 11:47:09 +020054 self.pg1.enable_capture()
55
Damjan Marionf56b77a2016-10-03 19:44:57 +020056 self.pg_start()
57
Klement Sekeraf62ae122016-10-11 11:47:09 +020058 # Pick first received frame and check if it's the non-encapsulated frame
59 out = self.pg1.get_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020060 self.assertEqual(len(out), 1,
61 'Invalid number of packets on '
62 'output: {}'.format(len(out)))
63 pkt = out[0]
64
65 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020066 self.assertEqual(pkt[Ether].src, self.frame_pg0_to_pg1[Ether].src)
67 self.assertEqual(pkt[Ether].dst, self.frame_pg0_to_pg1[Ether].dst)
68 self.assertEqual(pkt[IP].src, self.frame_pg0_to_pg1[IP].src)
69 self.assertEqual(pkt[IP].dst, self.frame_pg0_to_pg1[IP].dst)
70 self.assertEqual(pkt[UDP].sport, self.frame_pg0_to_pg1[UDP].sport)
71 self.assertEqual(pkt[UDP].dport, self.frame_pg0_to_pg1[UDP].dport)
72 self.assertEqual(pkt[Raw], self.frame_pg0_to_pg1[Raw])
Damjan Marionf56b77a2016-10-03 19:44:57 +020073
Damjan Marionf56b77a2016-10-03 19:44:57 +020074 def test_encap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020075 """ Encapsulation test
76 Send frames from pg1
77 Verify receipt of encapsulated frames on pg0
78 """
79 self.pg1.add_stream([self.frame_pg1_to_pg0])
Damjan Marionf56b77a2016-10-03 19:44:57 +020080
Klement Sekeraf62ae122016-10-11 11:47:09 +020081 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020082
Damjan Marionf56b77a2016-10-03 19:44:57 +020083 self.pg_start()
84
Klement Sekeraf62ae122016-10-11 11:47:09 +020085 # Pick first received frame and check if it's corectly encapsulated.
86 out = self.pg0.get_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020087 self.assertEqual(len(out), 1,
88 'Invalid number of packets on '
89 'output: {}'.format(len(out)))
Klement Sekeraf62ae122016-10-11 11:47:09 +020090 pkt = out[0]
91 self.check_encapsulation(pkt)
Damjan Marionf56b77a2016-10-03 19:44:57 +020092
Klement Sekeraf62ae122016-10-11 11:47:09 +020093 payload = self.decapsulate(pkt)
Damjan Marionf56b77a2016-10-03 19:44:57 +020094 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020095 self.assertEqual(payload[Ether].src, self.frame_pg1_to_pg0[Ether].src)
96 self.assertEqual(payload[Ether].dst, self.frame_pg1_to_pg0[Ether].dst)
97 self.assertEqual(payload[IP].src, self.frame_pg1_to_pg0[IP].src)
98 self.assertEqual(payload[IP].dst, self.frame_pg1_to_pg0[IP].dst)
99 self.assertEqual(payload[UDP].sport, self.frame_pg1_to_pg0[UDP].sport)
100 self.assertEqual(payload[UDP].dport, self.frame_pg1_to_pg0[UDP].dport)
101 self.assertEqual(payload[Raw], self.frame_pg1_to_pg0[Raw])