def test_config_dnsmasq(): """ Test configuration of dnsmasq where we specify an interface """ write_file(CONFIG_FILE, DNSMASQ_CONFIG) manager = ConfigManager(WRITE_DIR) manager.loadConfig(search=CONFIG_FILE, execute=False) commands = manager.previousCommands assert len(commands) > 0 # Should have generated a dnsmasq config file. dnsmasq_conf = os.path.join(WRITE_DIR, "dnsmasq-lan.conf") assert os.path.exists(dnsmasq_conf) os.remove(dnsmasq_conf) # Write a fake pid file, so we can test the kill command. pidFile = os.path.join(WRITE_DIR, "dnsmasq-lan.pid") with open(pidFile, 'w') as output: output.write("12345") manager.unload(execute=False) commands = manager.previousCommands for cmd in commands: print(cmd) # Unload should generate a kill command for the fake pid. assert in_commands("kill", commands) os.remove(pidFile)
def test_config_network_bridge(): """ Test loading a bridge interface config """ write_file(CONFIG_FILE, NETWORK_BRIDGE_CONFIG) manager = ConfigManager(WRITE_DIR) manager.loadConfig(search=CONFIG_FILE, execute=False) commands = manager.previousCommands for cmd in commands: print(cmd) assert len(commands) == 10 # Should generate a command to create bridge interface. assert in_commands("ip link add name br-lan type bridge", commands) # Should assign eth1 and eth2 to bridge. assert in_commands("ip link set dev eth1 master br-lan", commands) assert in_commands("ip link set dev eth2 master br-lan", commands) manager.unload(execute=False) commands = manager.previousCommands for cmd in commands: print(cmd) assert len(commands) == 8 # Should delete bridge interface. assert in_commands("ip link delete br-lan", commands)
def test_bad_config(): """ Test how pdconf manager handles a bad configuration section """ from paradrop.backend.pdconfd.config.manager import ConfigManager manager = ConfigManager(writeDir="/tmp") source = os.path.join(CONFIG_DIR, "bad_config") manager.loadConfig(search=source, execute=False)
def test_config_wireless_sta(): """ Test loading a wireless sta config TODO: Flesh this out after implementing sta mode. """ write_file(CONFIG_FILE, WIRELESS_STA_CONFIG) manager = ConfigManager(WRITE_DIR) manager.loadConfig(search=CONFIG_FILE, execute=False) commands = manager.previousCommands for cmd in commands: print(cmd)
def test_config_network_execute(): """ Test loading a network configuration with mock execution """ write_file(CONFIG_FILE, NETWORK_WAN_CONFIG) manager = ConfigManager(WRITE_DIR) manager.execute = MagicMock() manager.loadConfig(search=CONFIG_FILE, execute=True) assert manager.execute.called manager.execute.reset_mock() manager.unload(execute=True) assert manager.execute.called
def test_manager_execute(execute): """ Test the manager execute method """ from paradrop.backend.pdconfd.config.manager import ConfigManager manager = ConfigManager(writeDir="/tmp") source = os.path.join(CONFIG_DIR, "multi_ap") manager.loadConfig(search=source, execute=True) assert execute.call_count > 1 # Verify the order of commands: # "interface add" < "addr add" < "hostapd" ifaceAdd = None addrAdd = None hostapd = None i = 0 for cmd in manager.previousCommands.commands(): print(cmd) if "interface add" in cmd: ifaceAdd = i elif "addr add" in cmd: addrAdd = i elif "hostapd" in cmd: hostapd = i i += 1 assert ifaceAdd < addrAdd and addrAdd < hostapd execute.reset_mock() manager.unload(execute=True) assert execute.call_count > 1 # Verify the order of commands: # "kill" < "addr del" < "iw dev ... del" print("---") kill = None addrDel = None iwDev = None i = 0 for cmd in manager.previousCommands.commands(): print(cmd) if "kill" in cmd: kill = i elif "addr del" in cmd: addrDel = i elif "iw dev" in cmd: iwDev = i i += 1 assert kill < addrDel and addrDel < iwDev
def test_config_default_dnsmasq(): """ Test configuration of dnsmasq using default (all interfaces) """ write_file(CONFIG_FILE, DEFAULT_DNSMASQ_CONFIG) manager = ConfigManager(WRITE_DIR) manager.loadConfig(search=CONFIG_FILE, execute=False) commands = manager.previousCommands assert len(commands) > 0 # Should have generated a dnsmasq config file. dnsmasq_conf = os.path.join(WRITE_DIR, "dnsmasq-lan.conf") assert os.path.exists(dnsmasq_conf) os.remove(dnsmasq_conf)
def test_config_firewall_zone(): """ Test loading a firewall config with WAN zone """ write_file(CONFIG_FILE, FIREWALL_ZONE_CONFIG) manager = ConfigManager(WRITE_DIR) manager.loadConfig(search=CONFIG_FILE, execute=False) commands = manager.previousCommands assert len(commands) == 2 # Should generate a masquerade rule. assert in_commands("MASQUERADE", commands) manager.unload(execute=False) commands = manager.previousCommands assert len(commands) == 2
def test_config_firewall_redirect(): """ Test loading a firewall config with redirect rules """ write_file(CONFIG_FILE, FIREWALL_REDIRECT_CONFIG) manager = ConfigManager(WRITE_DIR) manager.loadConfig(search=CONFIG_FILE, execute=False) commands = manager.previousCommands assert len(commands) == 5 # Should generate a DNAT rule. assert in_commands("DNAT", commands) # TODO Check for SNAT rule when implemented. assert not in_commands("SNAT", commands) manager.unload(execute=False) commands = manager.previousCommands assert len(commands) == 5
def test_config_wireless_ap(): """ Test loading a wireless AP config """ write_file(CONFIG_FILE, WIRELESS_AP_CONFIG) manager = ConfigManager(WRITE_DIR) manager.loadConfig(search=CONFIG_FILE, execute=False) commands = manager.previousCommands for cmd in commands: print(cmd) assert len(commands) == 9 # Check for command to add ap mode interface. assert in_commands("add wlan0 type __ap", commands) # Check that one command starts hostapd. assert in_commands("hostapd", commands) # Should have generated a hostapd config file. hostapd_conf = os.path.join(WRITE_DIR, "hostapd-ap1.conf") assert os.path.exists(hostapd_conf) os.remove(hostapd_conf) # Write a fake pid file, so we can test the kill command. pidFile = os.path.join(WRITE_DIR, "hostapd-ap1.pid") with open(pidFile, 'w') as output: output.write("12345") manager.unload(execute=False) commands = manager.previousCommands print("---") for cmd in commands: print(cmd) assert len(commands) == 7 # Unload should generate a kill command for the fake pid. assert in_commands("kill /tmp/hostapd-ap1.pid", commands) os.remove(pidFile)
def test_config_network_wan(): """ Test loading a configuration file that specifies an WAN interface """ write_file(CONFIG_FILE, NETWORK_WAN_CONFIG) manager = ConfigManager(WRITE_DIR) manager.loadConfig(search=CONFIG_FILE, execute=False) commands = manager.previousCommands for cmd in commands: print(cmd) assert len(commands) == 4 # One of the commands should assign the given IP address. assert in_commands("192.168.33.66", commands) # Should add a default route via the gateway. assert in_commands("default via 192.168.33.1", commands) manager.unload(execute=False) commands = manager.previousCommands for cmd in commands: print(cmd) assert len(commands) == 2
def test_change_channel(): """ Test how pdconf manager handles changing a WiFi card's channel """ from paradrop.backend.pdconfd.config.manager import ConfigManager # We want to use the same path for the config file at every step of the # test case, as this allows the manager to detect when a section is # removed. temp = tempfile.mkdtemp() confFile = os.path.join(temp, "config") manager = ConfigManager(writeDir="/tmp") source = os.path.join(CONFIG_DIR, "change_channel_1") shutil.copyfile(source, confFile) manager.loadConfig(search=confFile, execute=False) for cmd in manager.previousCommands: print(cmd) source = os.path.join(CONFIG_DIR, "change_channel_2") shutil.copyfile(source, confFile) manager.loadConfig(search=confFile, execute=False) print("---") for cmd in manager.previousCommands: print(cmd) # Changing the channel should restart hostapd but not delete and recreate # the wireless interface. assert "del" not in manager.previousCommands source = os.path.join(CONFIG_DIR, "change_channel_3") shutil.copyfile(source, confFile) manager.loadConfig(search=confFile, execute=False) print("---") for cmd in manager.previousCommands: print(cmd) # The third config should bring down hostapd, not start it. assert "kill" in manager.previousCommands assert "bin/hostapd" not in manager.previousCommands # Cleanup shutil.rmtree(temp)