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'))
def test_router(self): """Create a complete router""" 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.incoming, ["eth0"]) self.assertEqual(r.clients, {}) self.assertEqual(r.interfaces, {'eth1': i1, 'eth2': i2})
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'))
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")
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)
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}) }))
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"]
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)
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'))
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'))
def test_bind_once(self): """The binder should be bound to only one router""" self.binder.notify("unknown", self.router) self.binder.notify("unknwon", self.router) with self.assertRaises(ValueError): self.binder.notify("unknwon", Router("eth0"))
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_router_with_shared_interface(self): """Try to create router with duplicate interfaces""" i1 = Interface("LAN", "My second interface") i2 = Interface("WAN", "My third interface") with self.assertRaises(ValueError): r = Router("eth0", interfaces={'eth0': i1, 'eth1': i2})
def test_multiple_incoming(self): """Create an empty router with several incoming interfaces""" r = Router(["eth0", "eth2"]) self.assertEqual(r.incoming, ["eth0", "eth2"]) self.assertEqual(r.interfaces, {}) self.assertEqual(r.clients, {})
def test_empty_router(self): """Create an empty router""" r = Router("eth0") self.assertEqual(r.incoming, ["eth0"]) self.assertEqual(r.interfaces, {}) self.assertEqual(r.clients, {})