diff --git a/securedrop_export/export.py b/securedrop_export/export.py index 7e20728..8dcff76 100755 --- a/securedrop_export/export.py +++ b/securedrop_export/export.py @@ -80,7 +80,9 @@ def __init__(self, archive, config_path): try: with open(config_path) as f: json_config = json.loads(f.read()) - self.pci_bus_id = int(json_config.get("pci_bus_id", 2)) + self.pci_bus_id = json_config.get("pci_bus_id", None) + if self.pci_bus_id is None: + raise except Exception: self.exit_gracefully("ERROR_CONFIG") @@ -132,12 +134,15 @@ def extract_tarball(self): self.exit_gracefully(msg) def check_usb_connected(self): - p = subprocess.check_output(["lsusb", "-s", self.pci_bus_id]) - # Empty string means a likely wrong pci_bus_id - if p == "": - msg = "ERROR_USB_CHECK" + + # If the USB is not attached via qvm-usb attach, lsusb will return empty string and a + # return code of 1 + try: + p = subprocess.check_output(["lsusb", "-s", "{}:".format(self.pci_bus_id)]) + except subprocess.CalledProcessError: + msg = "ERROR_USB_CONFIGURATION" self.exit_gracefully(msg) - n_usb = len(p.rstrip().split("\n")) + n_usb = len(p.decode("utf-8").rstrip().split("\n")) # If there is one device, it is the root hub. if n_usb == 1: msg = "USB_NOT_CONNECTED" diff --git a/tests/sd-export-config-bad-2.json b/tests/sd-export-config-bad-2.json index 879fb83..f69e25b 100644 --- a/tests/sd-export-config-bad-2.json +++ b/tests/sd-export-config-bad-2.json @@ -1,3 +1,3 @@ { - "pci_bus_id": "two" + "no_pci_bus_id": "nope" } diff --git a/tests/test_export.py b/tests/test_export.py index 19be561..d0a2946 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -2,6 +2,7 @@ import os import pytest +import subprocess import tempfile from securedrop_export import export @@ -9,10 +10,10 @@ SAMPLE_OUTPUT_NO_PRINTER = b"network beh\nnetwork https\nnetwork ipp\nnetwork ipps\nnetwork http\nnetwork\nnetwork ipp14\nnetwork lpd" # noqa SAMPLE_OUTPUT_BOTHER_PRINTER = b"network beh\nnetwork https\nnetwork ipp\nnetwork ipps\nnetwork http\nnetwork\nnetwork ipp14\ndirect usb://Brother/HL-L2320D%20series?serial=A00000A000000\nnetwork lpd" # noqa -SAMPLE_OUTPUT_NO_USB = "Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub" # noqa -SAMPLE_OUTPUT_USB = "Bus 001 Device 002: ID 0781:5575 SanDisk Corp.\nBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub" # noqa -SAMPLE_OUTPUT_USB_ERROR = "" -SAMPLE_OUTPUT_USB_ERROR2 = "h\ne\nl\nl\no" +SAMPLE_OUTPUT_NO_USB = b"Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub" # noqa +SAMPLE_OUTPUT_USB = b"Bus 001 Device 002: ID 0781:5575 SanDisk Corp.\nBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub" # noqa +SAMPLE_OUTPUT_USB_ERROR = b"" +SAMPLE_OUTPUT_USB_ERROR2 = b"h\ne\nl\nl\no" TEST_CONFIG = os.path.join(os.path.dirname(__file__), "sd-export-config.json") BAD_TEST_CONFIG = os.path.join(os.path.dirname(__file__), "sd-export-config-bad.json") ANOTHER_BAD_TEST_CONFIG = os.path.join(os.path.dirname(__file__), "sd-export-config-bad-2.json") @@ -46,7 +47,7 @@ def test_bad_sd_export_config_invalid_value(capsys): def test_good_sd_export_config(capsys): submission = export.SDExport("", TEST_CONFIG) - assert submission.pci_bus_id == 2 + assert submission.pci_bus_id == "2" def test_exit_gracefully_no_exception(capsys): @@ -230,7 +231,7 @@ def test_usb_precheck_disconnected(mocked_call, capsys): assert captured.err == "{}\n".format(expected_message) -@mock.patch("subprocess.check_output", return_value=SAMPLE_OUTPUT_USB_ERROR) +@mock.patch("subprocess.check_output", return_code=1) def test_usb_precheck_error(mocked_call, capsys): submission = export.SDExport("testfile", TEST_CONFIG) expected_message = "ERROR_USB_CHECK"