#!/usr/bin/env python from abc import abstractmethod from scapy.layers.l2 import Ether, Raw from scapy.layers.inet import IP, UDP class BridgeDomain(object): """ Bridge domain abstraction """ @property def frame_pg0_to_pg1(self): """ Ethernet frame sent from pg0 and expected to arrive at pg1 """ return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') / IP(src='1.2.3.4', dst='4.3.2.1') / UDP(sport=10000, dport=20000) / Raw('\xa5' * 100)) @property def frame_pg1_to_pg0(self): """ Ethernet frame sent from pg1 and expected to arrive at pg0 """ return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / IP(src='4.3.2.1', dst='1.2.3.4') / UDP(sport=20000, dport=10000) / Raw('\xa5' * 100)) @abstractmethod def encapsulate(self, pkt): """ Encapsulate packet """ pass @abstractmethod def decapsulate(self, pkt): """ Decapsulate packet """ pass @abstractmethod def check_encapsulation(self, pkt): """ Verify the encapsulation """ pass def test_decap(self): """ Decapsulation test Send encapsulated frames from pg0 Verify receipt of decapsulated frames on pg1 """ encapsulated_pkt = self.encapsulate(self.frame_pg0_to_pg1) self.pg0.add_stream([encapsulated_pkt, ]) self.pg1.enable_capture() self.pg_start() # Pick first received frame and check if it's the non-encapsulated frame out = self.pg1.get_capture() self.assertEqual(len(out), 1, 'Invalid number of packets on ' 'output: {}'.format(len(out))) pkt = out[0] # TODO: add error messages self.assertEqual(pkt[Ether].src, self.frame_pg0_to_pg1[Ether].src) self.assertEqual(pkt[Ether].dst, self.frame_pg0_to_pg1[Ether].dst) self.assertEqual(pkt[IP].src, self.frame_pg0_to_pg1[IP].src) self.assertEqual(pkt[IP].dst, self.frame_pg0_to_pg1[IP].dst) self.assertEqual(pkt[UDP].sport, self.frame_pg0_to_pg1[UDP].sport) self.assertEqual(pkt[UDP].dport, self.frame_pg0_to_pg1[UDP].dport) self.assertEqual(pkt[Raw], self.frame_pg0_to_pg1[Raw]) def test_encap(self): """ Encapsulation test Send frames from pg1 Verify receipt of encapsulated frames on pg0 """ self.pg1.add_stream([self.frame_pg1_to_pg0]) self.pg0.enable_capture() self.pg_start() # Pick first received frame and check if it's corectly encapsulated. out = self.pg0.get_capture() self.assertEqual(len(out), 1, 'Invalid number of packets on ' 'output: {}'.format(len(out))) pkt = out[0] self.check_encapsulation(pkt) payload = self.decapsulate(pkt) # TODO: add error messages self.assertEqual(payload[Ether].src, self.frame_pg1_to_pg0[Ether].src) self.assertEqual(payload[Ether].dst, self.frame_pg1_to_pg0[Ether].dst) self.assertEqual(payload[IP].src, self.frame_pg1_to_pg0[IP].src) self.assertEqual(payload[IP].dst, self.frame_pg1_to_pg0[IP].dst) self.assertEqual(payload[UDP].sport, self.frame_pg1_to_pg0[UDP].sport) self.assertEqual(payload[UDP].dport, self.frame_pg1_to_pg0[UDP].dport) self.assertEqual(payload[Raw], self.frame_pg1_to_pg0[Raw])