class TestDrvinfo(TestCaseBase): @ValidateRunningAsRoot @requires_brctl def setUp(self): self._bridge = Bridge() self._bridge.addDevice() def tearDown(self): self._bridge.delDevice() def testBridgeEthtoolDrvinfo(self): self.assertEqual(ipwrapper.drv_name(self._bridge.devName), ipwrapper.LinkType.BRIDGE) def testEnablePromisc(self): ipwrapper.getLink(self._bridge.devName).promisc = True self.assertTrue(ipwrapper.getLink(self._bridge.devName).promisc, "Could not enable promiscuous mode.") def testDisablePromisc(self): ipwrapper.getLink(self._bridge.devName).promisc = True ipwrapper.getLink(self._bridge.devName).promisc = False self.assertFalse(ipwrapper.getLink(self._bridge.devName).promisc, "Could not disable promiscuous mode.")
def setUp(self): self._tap0 = Tap() self._tap1 = Tap() self._tap2 = Tap() self._bridge0 = Bridge('src-') self._bridge1 = Bridge('target-') self._bridge2 = Bridge('target2-') self._devices = [self._tap0, self._tap1, self._tap2, self._bridge0, self._bridge1, self._bridge2] # If setUp raise, teardown is not called, so we should either succeed, # or fail without leaving junk around. cleanup = [] try: for iface in self._devices: iface.addDevice() cleanup.append(iface) self._bridge0.addIf(self._tap0.devName) self._bridge1.addIf(self._tap1.devName) self._bridge2.addIf(self._tap2.devName) except: t, v, tb = sys.exc_info() for iface in cleanup: try: iface.delDevice() except Exception: self.log.exception("Error removing device %s" % iface) six.reraise(t, v, tb)
class TestDrvinfo(TestCaseBase): @ValidateRunningAsRoot @requires_brctl def setUp(self): self._bridge = Bridge() self._bridge.addDevice() def tearDown(self): self._bridge.delDevice() def testBridgeEthtoolDrvinfo(self): self.assertEqual(ipwrapper.drv_name(self._bridge.devName), ipwrapper.LinkType.BRIDGE) def testEnablePromisc(self): ipwrapper.getLink(self._bridge.devName).promisc = True self.assertTrue( ipwrapper.getLink(self._bridge.devName).promisc, "Could not enable promiscuous mode.") def testDisablePromisc(self): ipwrapper.getLink(self._bridge.devName).promisc = True ipwrapper.getLink(self._bridge.devName).promisc = False self.assertFalse( ipwrapper.getLink(self._bridge.devName).promisc, "Could not disable promiscuous mode.")
class TestLinks(TestCaseBase): @ValidateRunningAsRoot @requires_brctl def setUp(self): self._bridge = Bridge() self._bridge.addDevice() def tearDown(self): self._bridge.delDevice() def testGetLink(self): link = ipwrapper.getLink(self._bridge.devName) self.assertTrue(link.isBRIDGE) self.assertEqual(link.master, None) self.assertEqual(link.name, self._bridge.devName)
class TestUnicodeDrvinfo(TestCaseBase): @ValidateRunningAsRoot @requires_brctl def setUp(self): # First 3 Hebrew letters # See http://unicode.org/charts/PDF/U0590.pdf self._bridge = Bridge("\xd7\x90\xd7\x91\xd7\x92") self._bridge.addDevice() def tearDown(self): self._bridge.delDevice() def testUtf8BridgeEthtoolDrvinfo(self): self.assertEqual( ipwrapper.drv_name(self._bridge.devName.decode('utf8')), ipwrapper.LinkType.BRIDGE)
def setUp(self): self._tap0 = Tap() self._tap1 = Tap() self._tap2 = Tap() self._bridge0 = Bridge('src-') self._bridge1 = Bridge('target-') self._bridge2 = Bridge('target2-') self._devices = [ self._tap0, self._tap1, self._tap2, self._bridge0, self._bridge1, self._bridge2 ] # If setUp raise, teardown is not called, so we should either succeed, # or fail without leaving junk around. cleanup = [] try: for iface in self._devices: iface.addDevice() cleanup.append(iface) self._bridge0.addIf(self._tap0.devName) self._bridge1.addIf(self._tap1.devName) self._bridge2.addIf(self._tap2.devName) except: t, v, tb = sys.exc_info() for iface in cleanup: try: iface.delDevice() except Exception: self.log.exception("Error removing device %s" % iface) six.reraise(t, v, tb)
class TestQdisc(TestCaseBase): @ValidateRunningAsRoot @requires_tc def setUp(self): self._bridge = Bridge() self._bridge.addDevice() def tearDown(self): self._bridge.delDevice() def _showQdisc(self): popen = Popen([EXT_TC, "qdisc", "show", "dev", self._bridge.devName], stdout=PIPE) return popen.stdout.read() def _addIngress(self): tc._qdisc_replace_ingress(self._bridge.devName) self.assertIn("qdisc ingress", self._showQdisc()) def testToggleIngress(self): self._addIngress() tc._qdisc_del(self._bridge.devName, 'ingress') self.assertNotIn("qdisc ingress", self._showQdisc()) def testQdiscsOfDevice(self): self._addIngress() self.assertEquals(("ffff:", ), tuple(tc._qdiscs_of_device(self._bridge.devName))) def testReplacePrio(self): self._addIngress() tc.qdisc.replace(self._bridge.devName, 'prio', parent=None) self.assertIn("root", self._showQdisc()) def testException(self): self.assertRaises(tc.TrafficControlException, tc._qdisc_del, "__nosuchiface__", 'ingress')
class TestBridgeOverwrite(TestCaseBase): _bridge = Bridge() @ValidateRunningAsRoot @requires_brctl def setUp(self): self._bridge.addDevice() def tearDown(self): self._bridge.delDevice() def testBridgeOverwriteErr(self): """iproute2 configurator uses hardcoded error output, test it's still the same""" expected_err = _BRCTL_DEV_EXISTS % self._bridge.devName popen = Popen([EXT_BRCTL, 'addbr', self._bridge.devName], stderr=PIPE) err = popen.stderr.read().strip() self.assertEquals(expected_err, err)
class TestPortMirror(TestCaseBase): """ Tests port mirroring of IP traffic between two bridges. This test brings up two tap devices and attaches every device to a separate bridge. Then mirroring of IP packets between the two bridges is enabled. If sent through _tap0 the packet _ICMP arrives on _tap1 the test succeeds. The tap devices are needed because the tc filter rules only become active when the bridge is ready, and the bridge only becomes ready when it is attached to an active device. """ # [ Ethernet ] # dst = 00:1c:c0:d0:44:dc # src = 00:21:5c:4d:42:75 # type = IPv4 # [ IP ] # version = 4L # ihl = 5L # tos = 0x0 # len = 33 # id = 1 # flags = # frag = 0L # ttl = 64 # proto = icmp # chksum = 0xf953 # src = 192.168.0.52 # dst = 192.168.0.3 # \options \ # [ ICMP ] # type = echo-request # code = 0 # chksum = 0x2875 # id = 0x0 # seq = 0x0 # [ Raw ] # load = '\x01#Eg\x89' _ICMP = unhexlify( '001cc0d044dc''00215c4d4275''0800' # Ethernet '45000021000100004001f953''c0a80034''c0a80003' # IP '080028750000000' # ICMP '00123456789') # Payload @ValidateRunningAsRoot @requires_tc @requires_tun def setUp(self): self._tap0 = Tap() self._tap1 = Tap() self._tap2 = Tap() self._bridge0 = Bridge('src-') self._bridge1 = Bridge('target-') self._bridge2 = Bridge('target2-') self._devices = [self._tap0, self._tap1, self._tap2, self._bridge0, self._bridge1, self._bridge2] # If setUp raise, teardown is not called, so we should either succeed, # or fail without leaving junk around. cleanup = [] try: for iface in self._devices: iface.addDevice() cleanup.append(iface) self._bridge0.addIf(self._tap0.devName) self._bridge1.addIf(self._tap1.devName) self._bridge2.addIf(self._tap2.devName) except: t, v, tb = sys.exc_info() for iface in cleanup: try: iface.delDevice() except Exception: self.log.exception("Error removing device %s" % iface) six.reraise(t, v, tb) def tearDown(self): failed = False for iface in self._devices: try: iface.delDevice() except Exception: self.log.exception("Error removing device %s" % iface) failed = True if failed: raise RuntimeError("Error tearing down interfaces") def _sendPing(self): self._tap1.startListener(self._ICMP) self._tap0.writeToDevice(self._ICMP) # Attention: sleep is bad programming practice! Never use it for # synchronization in productive code! time.sleep(0.1) if self._tap1.isListenerAlive(): self._tap1.stopListener() return False else: return True def testMirroring(self): tc.setPortMirroring(self._bridge0.devName, self._bridge1.devName) self.assertTrue(self._sendPing(), "Bridge received no mirrored ping " "requests.") tc.unsetPortMirroring(self._bridge0.devName, self._bridge1.devName) self.assertFalse(self._sendPing(), "Bridge received mirrored ping " "requests, but mirroring is unset.") def testMirroringWithDistraction(self): "setting another mirror action should not obstract the first one" tc.setPortMirroring(self._bridge0.devName, self._bridge2.devName) self.testMirroring() tc.unsetPortMirroring(self._bridge0.devName, self._bridge2.devName)
def setUp(self): # First 3 Hebrew letters # See http://unicode.org/charts/PDF/U0590.pdf self._bridge = Bridge("\xd7\x90\xd7\x91\xd7\x92") self._bridge.addDevice()
class TestPortMirror(TestCaseBase): """ Tests port mirroring of IP traffic between two bridges. This test brings up two tap devices and attaches every device to a separate bridge. Then mirroring of IP packets between the two bridges is enabled. If sent through _tap0 the packet _ICMP arrives on _tap1 the test succeeds. The tap devices are needed because the tc filter rules only become active when the bridge is ready, and the bridge only becomes ready when it is attached to an active device. """ # [ Ethernet ] # dst = 00:1c:c0:d0:44:dc # src = 00:21:5c:4d:42:75 # type = IPv4 # [ IP ] # version = 4L # ihl = 5L # tos = 0x0 # len = 33 # id = 1 # flags = # frag = 0L # ttl = 64 # proto = icmp # chksum = 0xf953 # src = 192.168.0.52 # dst = 192.168.0.3 # \options \ # [ ICMP ] # type = echo-request # code = 0 # chksum = 0x2875 # id = 0x0 # seq = 0x0 # [ Raw ] # load = '\x01#Eg\x89' _ICMP = unhexlify('001cc0d044dc' '00215c4d4275' '0800' # Ethernet '45000021000100004001f953' 'c0a80034' 'c0a80003' # IP '080028750000000' # ICMP '00123456789') # Payload @ValidateRunningAsRoot @requires_tc @requires_tun def setUp(self): self._tap0 = Tap() self._tap1 = Tap() self._tap2 = Tap() self._bridge0 = Bridge('src-') self._bridge1 = Bridge('target-') self._bridge2 = Bridge('target2-') self._devices = [ self._tap0, self._tap1, self._tap2, self._bridge0, self._bridge1, self._bridge2 ] # If setUp raise, teardown is not called, so we should either succeed, # or fail without leaving junk around. cleanup = [] try: for iface in self._devices: iface.addDevice() cleanup.append(iface) self._bridge0.addIf(self._tap0.devName) self._bridge1.addIf(self._tap1.devName) self._bridge2.addIf(self._tap2.devName) except: t, v, tb = sys.exc_info() for iface in cleanup: try: iface.delDevice() except Exception: self.log.exception("Error removing device %s" % iface) six.reraise(t, v, tb) def tearDown(self): failed = False for iface in self._devices: try: iface.delDevice() except Exception: self.log.exception("Error removing device %s" % iface) failed = True if failed: raise RuntimeError("Error tearing down interfaces") def _sendPing(self): self._tap1.startListener(self._ICMP) self._tap0.writeToDevice(self._ICMP) # Attention: sleep is bad programming practice! Never use it for # synchronization in productive code! time.sleep(0.1) if self._tap1.isListenerAlive(): self._tap1.stopListener() return False else: return True def testMirroring(self): tc.setPortMirroring(self._bridge0.devName, self._bridge1.devName) self.assertTrue(self._sendPing(), "Bridge received no mirrored ping " "requests.") tc.unsetPortMirroring(self._bridge0.devName, self._bridge1.devName) self.assertFalse( self._sendPing(), "Bridge received mirrored ping " "requests, but mirroring is unset.") def testMirroringWithDistraction(self): "setting another mirror action should not obstract the first one" tc.setPortMirroring(self._bridge0.devName, self._bridge2.devName) self.testMirroring() tc.unsetPortMirroring(self._bridge0.devName, self._bridge2.devName)
def setUp(self): self._bridge = Bridge() self._bridge.addDevice()