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_load(self): """Load router from YAML representation""" doc = """ clients: eth0 interfaces: eth1: name: LAN description: "My first interface" qos: - qos1 - qos2 eth2: name: WAN description: "My second interface" qos: - qos1 - qos3 password: 1234 qos: qos1: name: "100M" description: "My first QoS" bandwidth: 100mbps netem: delay 100ms 10ms distribution experimental qos2: name: "10M" description: "My second QoS" bandwidth: 10mbps netem: delay 200ms 10ms qos3: name: "1M" description: "My third QoS" bandwidth: 1mbps netem: delay 500ms 30ms """ r = Router.load(yaml.load(doc)) self.assertEqual(r.incoming, ["eth0"]) self.assertEqual(r.clients, {}) self.assertEqual(r.interfaces["eth1"], Interface("LAN", "My first interface", qos={'qos1': QoS("100M", "My first QoS", {"bandwidth": "100mbps", "netem": "delay 100ms 10ms distribution experimental"}), 'qos2': QoS("10M", "My second QoS", {"bandwidth": "10mbps", "netem": "delay 200ms 10ms"})})) self.assertEqual(r.interfaces["eth2"], Interface("WAN", "My second interface", qos={'qos1': QoS("100M", "My first QoS", {"bandwidth": "100mbps", "netem": "delay 100ms 10ms distribution experimental"}), 'qos3': QoS("1M", "My third QoS", {"bandwidth": "1mbps", "netem": "delay 500ms 30ms"})})) self.assertEqual(len(r.interfaces), 2) self.assertEqual(r.interfaces["eth2"].check_password("1234"), True)
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_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_password(self): """Password protection""" i1 = Interface("eth0", "My first interface", {}, "12457") i2 = Interface("eth0", "My first interface", {}) self.assertEqual(i1.check_password("12457"), True) self.assertEqual(i1.check_password(12457), True) self.assertEqual(i1.check_password(None), False) self.assertEqual(i1.check_password("1245711"), False) self.assertEqual(i2.check_password(None), True) self.assertEqual(i2.check_password("1245711"), True)
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_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_build_interface(self): """Build a regular interface""" q1 = QoS("100M", "My first QoS") q2 = QoS("10M", "My second QoS") i = Interface("eth1", "My second interface", {'qos1': q1, 'qos2': q2}) self.assertEqual(i.name, "eth1") self.assertEqual(i.description, "My second interface") self.assertEqual(i.qos, {'qos1': q1, 'qos2': q2}) str(i)
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_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_load_almost_minimal(self): """Load router with almost minimal information""" doc = """ clients: eth0 interfaces: eth1: name: LAN description: "My first interface" """ r = Router.load(yaml.load(doc)) self.assertEqual(r.interfaces, {'eth1': Interface("LAN", "My first interface")}) self.assertEqual(r.incoming, ["eth0"]) self.assertEqual(r.clients, {})
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_pickle(self): """Pickling""" i = Interface("eth0", "My first interface", {'qos1': QoS("100M", ""), 'qos2': QoS("10M", "")}) self.assertEqual(i, pickle.loads(pickle.dumps(i)))
def test_interface_equality(self): """Interface equality""" self.assertEqual(Interface("eth0", "My first interface"), Interface("eth0", "My first interface")) self.assertNotEqual(Interface("eth0", "My first interface"), Interface("eth0", "My second interface")) self.assertNotEqual(Interface("eth0", "My first interface"), Interface("eth1", "My first interface")) self.assertEqual(Interface("eth0", "My first interface", {'qos1': QoS("100M", ""), 'qos2': QoS("10M", "")}), Interface("eth0", "My first interface", {'qos1': QoS("100M", ""), 'qos2': QoS("10M", "")})) self.assertNotEqual(Interface("eth0", "My first interface", {'qos1': QoS("100M", ""), 'qos2': QoS("10M", "")}), Interface("eth0", "My first interface", {'qos1': QoS("100M", ""), 'qos2': QoS("10M", "Different")})) self.assertNotEqual(Interface("eth0", "My first interface", {'qos1': QoS("100M", ""), 'qos2':QoS("10M", "")}), "eth0")
def test_build_empty_interface(self): """Build an empty interface (no QoS)""" i = Interface("eth0", "My first interface") self.assertEqual(i.name, "eth0") self.assertEqual(i.description, "My first interface") self.assertEqual(i.qos, {})
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})