def test_zigator_debug_long(self): """Test the --debug argument.""" tmp_stdout = io.StringIO() with contextlib.redirect_stdout(tmp_stdout): zigator.main(["zigator", "--debug"]) captured_output = tmp_stdout.getvalue().rstrip() self.assertGreater(len(captured_output), 14) self.assertEqual(captured_output[:14], "usage: zigator")
def test_zigator(self): """Test the default message.""" tmp_stdout = io.StringIO() with contextlib.redirect_stdout(tmp_stdout): zigator.main(["zigator"]) captured_output = tmp_stdout.getvalue().rstrip() self.assertGreater(len(captured_output), 14) self.assertEqual(captured_output[:14], "usage: zigator")
def test_none_arguments(self): """Test the usage of None for the list of arguments.""" tmp_stdout = io.StringIO() with contextlib.redirect_stdout(tmp_stdout): zigator.main(None) captured_output = tmp_stdout.getvalue().rstrip() self.assertGreater(len(captured_output), 14) self.assertEqual(captured_output[:14], "usage: zigator")
def test_zigator_help_long(self): """Test the --help argument.""" tmp_stdout = io.StringIO() with contextlib.redirect_stdout(tmp_stdout): with self.assertRaises(SystemExit) as cm: zigator.main(["zigator", "--help"]) self.assertEqual(str(cm.exception), "0") captured_output = tmp_stdout.getvalue().rstrip() self.assertGreater(len(captured_output), 14) self.assertEqual(captured_output[:14], "usage: zigator")
def test_unrecognized_argument_long(self): """Test the usage of a long unrecognized argument.""" tmp_stdout = io.StringIO() with contextlib.redirect_stderr(tmp_stdout): with self.assertRaises(SystemExit) as cm: zigator.main(["zigator", "--output"]) self.assertEqual(str(cm.exception), "2") captured_error = tmp_stdout.getvalue().rstrip() self.assertGreater(len(captured_error), 14) self.assertEqual(captured_error[:14], "usage: zigator")
def test_zigator_version_long(self): """Test the --version argument.""" tmp_stdout = io.StringIO() with contextlib.redirect_stdout(tmp_stdout): with self.assertRaises(SystemExit) as cm: zigator.main(["zigator", "--version"]) self.assertEqual(str(cm.exception), "0") captured_output = tmp_stdout.getvalue().rstrip() match = re.search( r"^(0\+[0-9a-f]{7}|[0-9]+\.[0-9]+(\+[0-9a-f]{7})?)$", captured_output) self.assertIsNotNone(match)
def entry_point(): zigator.main(sys.argv)
def test_integration_info(self): """Test integration with INFO logging.""" tmp_stdout = io.StringIO() with contextlib.redirect_stdout(tmp_stdout): with self.assertLogs(level="INFO") as cm: zigator.main([ "zigator", "print-config", ]) captured_output = tmp_stdout.getvalue().rstrip() self.assertEqual(len(cm.output), 2) self.assertTrue( re.search( r"^INFO:root:Started Zigator version " r"(0\+[0-9a-f]{7}|[0-9]+\.[0-9]+(\+[0-9a-f]{7})?)$", cm.output[0]) is not None) self.assertTrue( re.search(r"^INFO:root:Printing the current configuration...$", cm.output[1]) is not None) config_list = captured_output.split("\n\n") self.assertEqual(len(config_list), 4) network_keys = config_list[0].split("\n") self.assertGreater(len(network_keys), 0) self.assertEqual(network_keys[0], "Network keys:") for i in range(1, len(network_keys)): self.assertGreater(len(network_keys[i]), 33) self.assertNotEqual(network_keys[i][:33], "11111111111111111111111111111111\t") self.assertNotEqual(network_keys[i][33:], "test_11111111111111111111111111111111") link_keys = config_list[1].split("\n") self.assertGreater(len(link_keys), 0) self.assertEqual(link_keys[0], "Link keys:") install_codes = config_list[2].split("\n") self.assertGreater(len(install_codes), 0) self.assertEqual(install_codes[0], "Install codes:") self.assertTrue( re.search(r"^Configuration directory: \".+zigator\"$", config_list[3]) is not None) with self.assertLogs(level="INFO") as cm: zigator.main([ "zigator", "add-config-entry", "network-key", "11111111111111111111111111111111", "test_11111111111111111111111111111111", ]) self.assertEqual(len(cm.output), 2) self.assertTrue( re.search( r"^INFO:root:Started Zigator version " r"(0\+[0-9a-f]{7}|[0-9]+\.[0-9]+(\+[0-9a-f]{7})?)$", cm.output[0]) is not None) self.assertTrue( re.search( r"^INFO:root:Saved the network key " r"11111111111111111111111111111111 in the " r"\".+network-keys.tsv\" configuration file$", cm.output[1]) is not None) tmp_stdout = io.StringIO() with contextlib.redirect_stdout(tmp_stdout): with self.assertLogs(level="INFO") as cm: zigator.main([ "zigator", "print-config", ]) captured_output = tmp_stdout.getvalue().rstrip() self.assertEqual(len(cm.output), 2) self.assertTrue( re.search( r"^INFO:root:Started Zigator version " r"(0\+[0-9a-f]{7}|[0-9]+\.[0-9]+(\+[0-9a-f]{7})?)$", cm.output[0]) is not None) self.assertTrue( re.search(r"^INFO:root:Printing the current configuration...$", cm.output[1]) is not None) config_list = captured_output.split("\n\n") self.assertEqual(len(config_list), 4) network_keys = config_list[0].split("\n") self.assertGreater(len(network_keys), 0) self.assertEqual(network_keys[0], "Network keys:") self.assertTrue( "11111111111111111111111111111111\t" "test_11111111111111111111111111111111" in network_keys) link_keys = config_list[1].split("\n") self.assertGreater(len(link_keys), 0) self.assertEqual(link_keys[0], "Link keys:") install_codes = config_list[2].split("\n") self.assertGreater(len(install_codes), 0) self.assertEqual(install_codes[0], "Install codes:") self.assertTrue( re.search(r"^Configuration directory: \".+zigator\"$", config_list[3]) is not None) pcap_directory = os.path.join(DIR_PATH, "data") db_filepath = os.path.join(DIR_PATH, "info-logging.db") with self.assertLogs(level="INFO") as cm: zigator.main([ "zigator", "parse", pcap_directory, db_filepath, ]) self.assertLoggingOutput(cm) connection = sqlite3.connect(db_filepath) connection.text_factory = str cursor = connection.cursor() cursor.execute("SELECT name FROM sqlite_master " "WHERE type=\"table\" " "ORDER BY name") self.assertEqual(cursor.fetchall(), [("addresses", ), ("devices", ), ("networks", ), ("packets", ), ("pairs", )]) self.assertAddressesTable(cursor) self.assertDevicesTable(cursor) self.assertNetworksTable(cursor) self.assertPacketsTable(cursor) self.assertPairsTable(cursor) cursor.close() connection.close() with self.assertLogs(level="INFO") as cm: zigator.main([ "zigator", "rm-config-entry", "network-key", "test_11111111111111111111111111111111", ]) self.assertEqual(len(cm.output), 2) self.assertTrue( re.search( r"^INFO:root:Started Zigator version " r"(0\+[0-9a-f]{7}|[0-9]+\.[0-9]+(\+[0-9a-f]{7})?)$", cm.output[0]) is not None) self.assertTrue( re.search( r"^INFO:root:Removed the " r"\"test_11111111111111111111111111111111\" network key " r"from the \".+network-keys.tsv\" configuration file$", cm.output[1]) is not None) tmp_stdout = io.StringIO() with contextlib.redirect_stdout(tmp_stdout): with self.assertLogs(level="INFO") as cm: zigator.main([ "zigator", "print-config", ]) captured_output = tmp_stdout.getvalue().rstrip() self.assertEqual(len(cm.output), 2) self.assertTrue( re.search( r"^INFO:root:Started Zigator version " r"(0\+[0-9a-f]{7}|[0-9]+\.[0-9]+(\+[0-9a-f]{7})?)$", cm.output[0]) is not None) self.assertTrue( re.search(r"^INFO:root:Printing the current configuration...$", cm.output[1]) is not None) config_list = captured_output.split("\n\n") self.assertEqual(len(config_list), 4) network_keys = config_list[0].split("\n") self.assertGreater(len(network_keys), 0) self.assertEqual(network_keys[0], "Network keys:") for i in range(1, len(network_keys)): self.assertGreater(len(network_keys[i]), 33) self.assertNotEqual(network_keys[i][:33], "11111111111111111111111111111111\t") self.assertNotEqual(network_keys[i][33:], "test_11111111111111111111111111111111") link_keys = config_list[1].split("\n") self.assertGreater(len(link_keys), 0) self.assertEqual(link_keys[0], "Link keys:") install_codes = config_list[2].split("\n") self.assertGreater(len(install_codes), 0) self.assertEqual(install_codes[0], "Install codes:") self.assertTrue( re.search(r"^Configuration directory: \".+zigator\"$", config_list[3]) is not None)