def test_popup_message(mocked_call): submission = export.SDExport("testfile", TEST_CONFIG) submission.popup_message("hello!") mocked_call.assert_called_once_with([ "notify-send", "--expire-time", "3000", "--icon", "/usr/share/securedrop/icons/sd-logo.png", "SecureDrop: hello!" ])
def test_empty_config(capsys): export.SDExport("testfile", TEST_CONFIG) temp_folder = tempfile.mkdtemp() metadata = os.path.join(temp_folder, export.Metadata.METADATA_FILE) with open(metadata, "w") as f: f.write("{}") config = export.Metadata(temp_folder) assert not config.is_valid()
def test_luks_precheck_encrypted_single_part(mocked_call, capsys): submission = export.SDExport("testfile", TEST_CONFIG) expected_message = export.ExportStatus.USB_ENCRYPTED.value mocked_exit = mock.patch("export.exit_gracefully", return_value=0) with pytest.raises(SystemExit) as sysexit: submission.check_luks_volume() mocked_exit.assert_called_once_with(expected_message) assert sysexit.value.code == 0
def test_valid_printer_config(capsys): export.SDExport("", TEST_CONFIG) temp_folder = tempfile.mkdtemp() metadata = os.path.join(temp_folder, export.Metadata.METADATA_FILE) with open(metadata, "w") as f: f.write('{"device": "printer"}') config = export.Metadata(temp_folder) assert config.is_valid() assert config.encryption_key is None assert config.encryption_method is None
def test_luks_precheck_encrypted_multi_part(mocked_call, capsys): submission = export.SDExport("testfile", TEST_CONFIG) expected_message = export.ExportStatus.USB_ENCRYPTION_NOT_SUPPORTED.value mocked_exit = mock.patch("export.exit_gracefully", return_value=0) with pytest.raises(SystemExit) as sysexit: submission.check_luks_volume() mocked_exit.assert_called_once_with(expected_message) assert sysexit.value.code == 0 captured = capsys.readouterr() assert captured.err == "{}\n".format(expected_message)
def test_safe_check_call(capsys): submission = export.SDExport("testfile", TEST_CONFIG) submission.safe_check_call(['ls'], "this will work") mocked_exit = mock.patch("export.exit_gracefully", return_value=0) expected_message = "uh oh!!!!" with pytest.raises(SystemExit) as sysexit: submission.safe_check_call(['ls', 'kjdsfhkdjfh'], expected_message) mocked_exit.assert_called_once_with(expected_message) assert sysexit.value.code == 0 captured = capsys.readouterr() assert captured.err == "{}\n".format(expected_message)
def test_bad_sd_export_config_invalid_value(capsys): expected_message = "ERROR_CONFIG" with pytest.raises(SystemExit) as sysexit: submission = export.SDExport("", ANOTHER_BAD_TEST_CONFIG) # A graceful exit means a return code of 0 assert sysexit.value.code == 0 captured = capsys.readouterr() assert captured.err == "{}\n".format(expected_message) assert captured.out == ""
def test_usb_precheck_error_2(mocked_call, capsys): submission = export.SDExport("testfile", TEST_CONFIG) expected_message = "ERROR_USB_CHECK" mocked_exit = mock.patch("export.exit_gracefully", return_value=0) with pytest.raises(SystemExit) as sysexit: result = submission.check_usb_connected() mocked_exit.assert_called_once_with(expected_message) assert sysexit.value.code == 0 captured = capsys.readouterr() assert captured.err == "{}\n".format(expected_message)
def test_valid_encryption_config(capsys): export.SDExport("testfile", TEST_CONFIG) temp_folder = tempfile.mkdtemp() metadata = os.path.join(temp_folder, export.Metadata.METADATA_FILE) with open(metadata, "w") as f: f.write( '{"device": "disk", "encryption_method": "luks", "encryption_key": "hunter1"}' ) config = export.Metadata(temp_folder) assert config.encryption_key == "hunter1" assert config.encryption_method == "luks" assert config.is_valid()
def test_exit_gracefully_exception(capsys): submission = export.SDExport("testfile", TEST_CONFIG) test_msg = 'test' with pytest.raises(SystemExit) as sysexit: submission.exit_gracefully(test_msg, e=Exception('BANG!')) # A graceful exit means a return code of 0 assert sysexit.value.code == 0 captured = capsys.readouterr() assert captured.err == "{}\n<unknown exception>\n".format(test_msg) assert captured.out == ""
def test_get_bad_printer_uri(mocked_call, capsys): submission = export.SDExport("testfile", TEST_CONFIG) expected_message = "ERROR_PRINTER_NOT_FOUND" mocked_exit = mock.patch("export.exit_gracefully", return_value=0) with pytest.raises(SystemExit) as sysexit: result = submission.get_printer_uri() assert result == "" mocked_exit.assert_called_once_with(expected_message) assert sysexit.value.code == 0 captured = capsys.readouterr() assert captured.err == "{}\n".format(expected_message) assert captured.out == ""
def test_usb_precheck_disconnected(capsys): submission = export.SDExport("testfile", TEST_CONFIG) expected_message = "USB_NOT_CONNECTED" assert export.ExportStatus.USB_NOT_CONNECTED.value == expected_message mocked_exit = mock.patch("export.exit_gracefully", return_value=0) mock.patch("subprocess.check_output", return_value=CalledProcessError(1, 'check_output')) with pytest.raises(SystemExit) as sysexit: submission.check_usb_connected() mocked_exit.assert_called_once_with(expected_message) assert sysexit.value.code == 0 captured = capsys.readouterr() assert captured.err == "{}\n".format(expected_message)
def start(): my_sub = export.SDExport(sys.argv[1], CONFIG_PATH) try: # Halt immediately if target file is absent if not os.path.exists(my_sub.archive): msg = "ERROR_FILE_NOT_FOUND" my_sub.exit_gracefully(msg) main.__main__(my_sub) # Delete extracted achive from tempfile shutil.rmtree(my_sub.tmpdir) except Exception: # exit with 0 return code otherwise the os will attempt to open # the file with another application msg = "ERROR_GENERIC" my_sub.exit_gracefully(msg)
def start(): try: configure_logging() except Exception: msg = "ERROR_LOGGING" export.SDExport.exit_gracefully(msg) logging.info('Starting SecureDrop Export {}'.format(__version__)) my_sub = export.SDExport(sys.argv[1], CONFIG_PATH) try: # Halt immediately if target file is absent if not os.path.exists(my_sub.archive): logging.info('Archive is not found {}.'.format(my_sub.archive)) msg = "ERROR_FILE_NOT_FOUND" my_sub.exit_gracefully(msg) main.__main__(my_sub) # Delete extracted achive from tempfile shutil.rmtree(my_sub.tmpdir) except Exception: # exit with 0 return code otherwise the os will attempt to open # the file with another application msg = "ERROR_GENERIC" my_sub.exit_gracefully(msg)
def test_get_good_printer_uri(mocked_call): submission = export.SDExport("testfile", TEST_CONFIG) result = submission.get_printer_uri() assert result == "usb://Brother/HL-L2320D%20series?serial=A00000A000000"
def test_good_sd_export_config(capsys): submission = export.SDExport("", TEST_CONFIG) assert submission.pci_bus_id == "2"
def test_is_not_open_office_file(capsys, open_office_paths): submission = export.SDExport("", TEST_CONFIG) assert not submission.is_open_office_file(open_office_paths)
def test_extract_device_name_single_part(mocked_call, capsys): submission = export.SDExport("testfile", TEST_CONFIG) submission.set_extracted_device_name() assert submission.device == "/dev/sda1"