Exemple #1
0
 def test_equality(self):
     """Test equality of two routers"""
     q1 = QoS("100M", "My first QoS")
     q2 = QoS("10M", "My second QoS")
     i1 = Interface("LAN", "My second interface", {'qos1': q1, 'qos2': q2})
     i2 = Interface("WAN", "My third interface", {'qos1': q1})
     r = Router("eth0", interfaces={'eth1': i1, 'eth2': i2})
     self.assertEqual(r, Router("eth0", interfaces={
                 'eth1': Interface("LAN", "My second interface",
                                   {'qos1': QoS("100M", "My first QoS"),
                                    'qos2': q2}),
                 'eth2': Interface("WAN", "My third interface", {'qos1': q1})
                 }))
     self.assertNotEqual(r, Router("eth0",
                                   interfaces={
                 'eth1': Interface("LAN", "My second interface",
                                   {'qos1': QoS("100M", "My first QoS"),
                                    'qos2': q2}),
                 'eth3': Interface("WAN", "My third interface", {'qos1': q1})
                 }))
     self.assertNotEqual(r, Router("eth0", interfaces={
                 'eth1': Interface("LAN", "My second interface",
                                   {'qos3': QoS("3G", "My first QoS"), 'qos2': q2}),
                 'eth2': Interface("WAN", "My third interface", {'qos1': q1})
                 }))
     self.assertNotEqual(r, Router("eth0"))
     self.assertNotEqual(r, "eth0")
     # With equality, clients are not considered
     r.bind("192.168.15.3", "eth2", "qos1")
     self.assertEqual(r, Router("eth0", interfaces={
                 'eth1': Interface("LAN", "My second interface",
                                   {'qos1': QoS("100M", "My first QoS"),
                                    'qos2': q2}),
                 'eth2': Interface("WAN", "My third interface", {'qos1': q1})
                 }))
Exemple #2
0
 def test_bind_client_ipv6(self):
     """IPv6 client binding"""
     q1 = QoS("100M", "My first QoS")
     i1 = Interface("LAN", "My second interface", {'qos1': q1})
     r = Router("eth0", interfaces={'eth1': i1 })
     r.bind("2001:db8::1", "eth1", "qos1")
     r.bind("2001:db8::2", "eth1", "qos1")
     self.assertEqual(r.clients["2001:db8::1"], ('eth1', 'qos1'))
     self.assertEqual(r.clients["2001:db8::2"], ('eth1', 'qos1'))
Exemple #3
0
 def test_double_bind_client(self):
     """Try to bind a client twice"""
     q1 = QoS("100M", "My first QoS")
     i1 = Interface("LAN", "My third interface", {'qos1': q1})
     r = Router("eth0", interfaces={'eth2': i1})
     r.bind("192.168.15.2", "eth2", "qos1")
     r.bind("192.168.15.3", "eth2", "qos1")
     with self.assertRaises(ValueError):
         r.bind("192.168.15.3", "eth2", "qos1")
     self.assertEqual(r.clients["192.168.15.2"], ('eth2', 'qos1'))
     self.assertEqual(r.clients["192.168.15.3"], ('eth2', 'qos1'))
Exemple #4
0
 def test_bind_inexistant(self):
     """Client binding to inexistant interface or QoS"""
     q1 = QoS("100M", "My first QoS")
     q2 = QoS("10M", "My second QoS")
     i1 = Interface("LAN", "My second interface", {'qos1': q1, 'qos2': q2})
     i2 = Interface("WAN", "My third interface", {'qos1': q1})
     r = Router("eth0", interfaces={'eth1': i1, 'eth2': i2})
     with self.assertRaises(KeyError):
         r.bind("192.168.15.2", "eth3", "qos2")
     with self.assertRaises(KeyError):
         r.bind("192.168.15.2", "eth2", "qos2")
Exemple #5
0
 def test_pickle(self):
     """Pickling"""
     q1 = QoS("100M", "My first QoS")
     q2 = QoS("10M", "My second QoS")
     i1 = Interface("LAN", "My second interface", {'qos1': q1, 'qos2': q2})
     i2 = Interface("WAN", "My third interface", {'qos1': q1})
     r = Router("eth0", interfaces={'eth1': i1, 'eth2': i2})
     self.assertEqual(r, pickle.loads(pickle.dumps(r)))
     self.assertEqual(r.clients, pickle.loads(pickle.dumps(r)).clients)
     r.bind("192.168.15.2", "eth2", "qos1")
     self.assertEqual(r, pickle.loads(pickle.dumps(r)))
     self.assertEqual(r.clients, pickle.loads(pickle.dumps(r)).clients)
Exemple #6
0
 def test_bind_incorrect_password(self):
     """Bind with an incorrect password"""
     q1 = QoS("100M", "My first QoS")
     q2 = QoS("10M", "My second QoS")
     i1 = Interface("LAN", "My second interface", {'qos1': q1, 'qos2': q2},
                    password="******")
     r = Router("eth0", interfaces={'eth1': i1})
     with self.assertRaises(AssertionError):
         r.bind("192.168.15.2", "eth1", "qos2")
     with self.assertRaises(AssertionError):
         r.bind("192.168.15.2", "eth1", "qos2", "4512")
     with self.assertRaises(KeyError):
         r.clients["192.168.15.2"]
Exemple #7
0
 def test_unbind_client(self):
     """Unbind a client"""""
     q1 = QoS("100M", "My first QoS")
     i1 = Interface("WAN", "My third interface", {'qos1': q1})
     r = Router("eth0", interfaces={'eth2': i1})
     r.bind("192.168.15.2", "eth2", "qos1")
     r.bind("192.168.15.3", "eth2", "qos1")
     r.unbind("192.168.15.2")
     with self.assertRaises(KeyError):
         r.clients["192.168.15.2"]
     self.assertEqual(len(r.clients), 1)
     r.unbind("192.168.15.2")
     self.assertEqual(len(r.clients), 1)
Exemple #8
0
 def test_bind_client(self):
     """Client binding"""
     q1 = QoS("100M", "My first QoS")
     q2 = QoS("10M", "My second QoS")
     i1 = Interface("LAN", "My second interface", {'qos1': q1, 'qos2': q2})
     i2 = Interface("WAN", "My third interface", {'qos1': q1})
     r = Router("eth0", interfaces={'eth1': i1, 'eth2': i2})
     r.bind("192.168.15.2", "eth1", "qos2")
     r.bind("192.168.15.3", "eth2", "qos1")
     r.bind("192.168.15.4", "eth1", "qos2")
     self.assertEqual(r.clients["192.168.15.2"], ('eth1', 'qos2'))
     self.assertEqual(r.clients["192.168.15.3"], ('eth2', 'qos1'))
     self.assertEqual(r.clients["192.168.15.4"], ('eth1', 'qos2'))
Exemple #9
0
 def test_bind_with_password(self):
     """Bind a password protected interface"""
     q1 = QoS("100M", "My first QoS")
     q2 = QoS("10M", "My second QoS")
     i1 = Interface("LAN", "My second interface", {'qos1': q1, 'qos2': q2},
                    password="******")
     i2 = Interface("WAN", "My third interface", {'qos1': q1})
     r = Router("eth0", interfaces={'eth1': i1, 'eth2': i2})
     r.bind("192.168.15.2", "eth1", "qos2", password="******")
     r.bind("192.168.15.3", "eth2", "qos1")
     r.bind("192.168.15.4", "eth1", "qos2", password=1234)
     r.bind("192.168.15.5", "eth2", "qos1", password=4574)
     self.assertEqual(r.clients["192.168.15.2"], ('eth1', 'qos2'))
     self.assertEqual(r.clients["192.168.15.3"], ('eth2', 'qos1'))
     self.assertEqual(r.clients["192.168.15.4"], ('eth1', 'qos2'))
     self.assertEqual(r.clients["192.168.15.5"], ('eth2', 'qos1'))
Exemple #10
0
class TestRouterObserver(unittest.TestCase):
    def setUp(self):
        q1 = QoS("100M", "My first QoS")
        q2 = QoS("10M", "My second QoS")
        i1 = Interface("LAN", "My second interface", {'qos1': q1, 'qos2': q2})
        i2 = Interface("WAN", "My third interface", {'qos1': q1})
        self.router = Router("eth0", interfaces={'eth1': i1, 'eth2': i2})

    def test_register_observer(self):
        """Register an observer and receive events"""
        last = {}
        class Observer(object):
            zope.interface.implements(IBinder)
            def notify(self, event, source, **kwargs):
                last['event'] = event
                last['source'] = source
                last['args'] = kwargs
        self.router.register(Observer())
        self.router.bind("192.168.15.2", "eth2", "qos1")
        self.assertEqual(last['event'], 'bind')
        self.assertEqual(last['source'], self.router)
        self.assertEqual(last['args']['client'], '192.168.15.2')
        self.assertEqual(last['args']['interface'], 'eth2')
        self.assertEqual(last['args']['qos'], 'qos1')
        self.router.bind("192.168.15.3", "eth2", "qos1")
        self.assertEqual(last['event'], 'bind')
        self.assertEqual(last['source'], self.router)
        self.assertEqual(last['args']['client'], '192.168.15.3')
        self.assertEqual(last['args']['interface'], 'eth2')
        self.assertEqual(last['args']['qos'], 'qos1')
        self.router.unbind("192.168.15.3")
        self.assertEqual(last['event'], 'unbind')
        self.assertEqual(last['source'], self.router)
        self.assertEqual(last['args']['client'], '192.168.15.3')

    def test_register_not_observer(self):
        """Register something which is not an observer"""
        last = {}
        class Observer(object):
            def notify(self, event, source, **kwargs):
                """Will not be called"""
        with self.assertRaises(ValueError):
            self.router.register(Observer())

    def test_register_several_observers(self):
        """Register several observers"""
        events = {}
        class Observer(object):
            zope.interface.implements(IBinder)
            def notify(self, event, source, **kwargs):
                events[self] = event
        obs1 = Observer()
        obs2 = Observer()
        obs3 = Observer()
        self.router.register(obs1)
        self.router.register(obs2)
        self.router.register(obs3)
        self.router.bind("192.168.15.2", "eth2", "qos1")
        self.assertEqual(events, {obs1: 'bind',
                                  obs2: 'bind',
                                  obs3: 'bind'})

    def test_observer_pickling(self):
        """Check if observers are notified on unpickling"""
        temp = tempfile.mkdtemp()
        try:
            testfile = os.path.join(temp, "testfile.txt")
            self.router.register(PickableObserver(testfile))
            self.router.bind("192.168.15.2", "eth2", "qos1")
            self.assertEqual(file(testfile).read(), "bind\n")
            r = pickle.loads(pickle.dumps(self.router))
            self.assertEqual(self.router, r)
            self.assertEqual(self.router.clients, r.clients)
            self.assertEqual(file(testfile).read(), "bind\nbind\n")
        finally:
            shutil.rmtree(temp)

    def test_stats(self):
        """Register an observer that also implements IStatsProvider"""
        class Observer(object):
            zope.interface.implements(IBinder, IStatsProvider)
            def notify(self, event, source, **kwargs):
                """Do nothing"""
            def stats(self):
                return {'eth1': {'up': 47, 'down': 255},
                        'eth2': {'clients': 2, 'details': {'172.14.15.16': {'up': 1, 'down': 2}}}}
        self.assertEqual(self.router.stats,
                         {'eth1': {'clients': 0, 'details': {}},
                          'eth2': {'clients': 0, 'details': {}}})
        self.router.register(Observer())
        self.assertEqual(self.router.stats,
                         {'eth1': {'clients': 0, 'up': 47, 'down': 255, 'details': {}},
                          'eth2': {'clients': 0, 'details': {}}})