blob: 6c6fb3da200209e48220c612bc5fcf2088b35201 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
3from abc import abstractmethod
4
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 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020011
Klement Sekeraf62ae122016-10-11 11:47:09 +020012 @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 Marionf56b77a2016-10-03 19:44:57 +020019
Klement Sekeraf62ae122016-10-11 11:47:09 +020020 @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 Marionf56b77a2016-10-03 19:44:57 +020028 @abstractmethod
29 def encapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020030 """ Encapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020031 pass
32
Damjan Marionf56b77a2016-10-03 19:44:57 +020033 @abstractmethod
34 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020035 """ Decapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020036 pass
37
Damjan Marionf56b77a2016-10-03 19:44:57 +020038 @abstractmethod
39 def check_encapsulation(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020040 """ Verify the encapsulation """
Damjan Marionf56b77a2016-10-03 19:44:57 +020041 pass
42
Damjan Marionf56b77a2016-10-03 19:44:57 +020043 def test_decap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020044 """ Decapsulation test
45 Send encapsulated frames from pg0
46 Verify receipt of decapsulated frames on pg1
47 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020048
Klement Sekeraf62ae122016-10-11 11:47:09 +020049 encapsulated_pkt = self.encapsulate(self.frame_pg0_to_pg1)
Damjan Marionf56b77a2016-10-03 19:44:57 +020050
Klement Sekeraf62ae122016-10-11 11:47:09 +020051 self.pg0.add_stream([encapsulated_pkt, ])
Damjan Marionf56b77a2016-10-03 19:44:57 +020052
Klement Sekeraf62ae122016-10-11 11:47:09 +020053 self.pg1.enable_capture()
54
Damjan Marionf56b77a2016-10-03 19:44:57 +020055 self.pg_start()
56
Klement Sekeraf62ae122016-10-11 11:47:09 +020057 # Pick first received frame and check if it's the non-encapsulated frame
58 out = self.pg1.get_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020059 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 Sekeraf62ae122016-10-11 11:47:09 +020065 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 Marionf56b77a2016-10-03 19:44:57 +020072
Damjan Marionf56b77a2016-10-03 19:44:57 +020073 def test_encap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020074 """ 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 Marionf56b77a2016-10-03 19:44:57 +020079
Klement Sekeraf62ae122016-10-11 11:47:09 +020080 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020081
Damjan Marionf56b77a2016-10-03 19:44:57 +020082 self.pg_start()
83
Klement Sekeraf62ae122016-10-11 11:47:09 +020084 # Pick first received frame and check if it's corectly encapsulated.
85 out = self.pg0.get_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020086 self.assertEqual(len(out), 1,
87 'Invalid number of packets on '
88 'output: {}'.format(len(out)))
Klement Sekeraf62ae122016-10-11 11:47:09 +020089 pkt = out[0]
90 self.check_encapsulation(pkt)
Damjan Marionf56b77a2016-10-03 19:44:57 +020091
Klement Sekeraf62ae122016-10-11 11:47:09 +020092 payload = self.decapsulate(pkt)
Damjan Marionf56b77a2016-10-03 19:44:57 +020093 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020094 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])