def setUp(self): self.server = DHCPServerFactory() self.dhcp_dp = DHCPDiscoverPacketFactory(hwaddr=HWADDR, relay_ip=RELAY_IP) self.dhcp_dp_wrong_relay = DHCPDiscoverPacketFactory(hwaddr=HWADDR, relay_ip="176.98.33.76") self.dhcp_dp_wrong_mac = DHCPDiscoverPacketFactory(hwaddr="00:aa:bb:cc:dd:ee", relay_ip=RELAY_IP) self.dhcp_dp_vlan = DHCPDiscoverPacketFactory(hwaddr=HWADDR, relay_ip=RELAY_IP, relay_vlan=RELAY_VLAN) self.dhcp_dp_wrong_vlan = DHCPDiscoverPacketFactory(hwaddr=HWADDR, relay_ip=RELAY_IP, relay_vlan=99)
class DHCPServerTestCase(unittest.TestCase): def setUp(self): self.server = DHCPServerFactory() self.dhcp_dp = DHCPDiscoverPacketFactory(hwaddr=HWADDR, relay_ip=RELAY_IP) self.dhcp_dp_wrong_relay = DHCPDiscoverPacketFactory(hwaddr=HWADDR, relay_ip="176.98.33.76") self.dhcp_dp_wrong_mac = DHCPDiscoverPacketFactory(hwaddr="00:aa:bb:cc:dd:ee", relay_ip=RELAY_IP) self.dhcp_dp_vlan = DHCPDiscoverPacketFactory(hwaddr=HWADDR, relay_ip=RELAY_IP, relay_vlan=RELAY_VLAN) self.dhcp_dp_wrong_vlan = DHCPDiscoverPacketFactory(hwaddr=HWADDR, relay_ip=RELAY_IP, relay_vlan=99) def test_evaluate_relay(self): """Check if packet has correctly relay set and relay properly readed from config""" self.assertEqual(self.server._evaluateRelay(self.dhcp_dp), True) self.assertEqual(self.server._evaluateRelay(self.dhcp_dp_wrong_relay), False) def test_handle_dhcp_discover(self): """Check if dhcp server """ self.server._sendDHCPPacket = MagicMock() self.server._handleDHCPDiscover(self.dhcp_dp, SERVER_IP, False) self.assertEqual(self.server._sendDHCPPacket.called, True) def test_handle_dhcp_discover_wrong_mac(self): """Check if dhcp server """ self.server._sendDHCPPacket = MagicMock() self.server._handleDHCPDiscover(self.dhcp_dp_wrong_mac, SERVER_IP, False) self.assertEqual(self.server._sendDHCPPacket.called, False) def test_handle_dhcp_discover_vlan(self): """Check if dhcp server """ self.server._sendDHCPPacket = MagicMock() self.server._handleDHCPDiscover(self.dhcp_dp_vlan, SERVER_IP, False) self.assertEqual(self.server._sendDHCPPacket.called, True) def test_handle_dhcp_discover_wrong_vlan(self): """Check if dhcp server """ self.server._sendDHCPPacket = MagicMock() self.server._handleDHCPDiscover(self.dhcp_dp_wrong_vlan, SERVER_IP, False) self.assertEqual(self.server._sendDHCPPacket.called, False)