Beispiel #1
0
def test_is_wixel_port():
    port = ListPortInfo()
    port.vid = 4321
    port.pid = 5678
    assert wixel.is_wixel_port(port) == False
    port.vid = wixel.VID
    port.pid = wixel.PID
    assert wixel.is_wixel_port(port) == True
def fake_comports():
    """
    Generate a fake list of comports to compare against.
    """
    fake_device = ListPortInfo(device='COM1')
    fake_device.vid = 0
    fake_device.pid = 1000
    fake_device.serial_number = 'a1'

    fake_device2 = ListPortInfo(device='COM2')
    fake_device2.vid = 1
    fake_device2.pid = 1010
    fake_device2.serial_number = 'c0'
    return [fake_device, fake_device2]
def test_instrument_open_serial_by_usb_ids_multiple_matches(_, mock_comports):
    with pytest.raises(serial.SerialException):
        fake_device = ListPortInfo(device='COM1')
        fake_device.vid = 0
        fake_device.pid = 1000
        fake_device.serial_number = 'a1'

        fake_device2 = ListPortInfo(device='COM2')
        fake_device2.vid = 0
        fake_device2.pid = 1000
        fake_device2.serial_number = 'b2'

        mock_comports.return_value = [fake_device, fake_device2]

        _ = ik.Instrument.open_serial(baud=1234, vid=0, pid=1000)
Beispiel #4
0
def test_instrument_open_serial_by_usb_ids_multiple_matches(_, mock_comports):
    fake_device = ListPortInfo()
    fake_device.vid = 0
    fake_device.pid = 1000
    fake_device.serial_number = 'a1'
    fake_device.device = 'COM1'

    fake_device2 = ListPortInfo()
    fake_device2.vid = 0
    fake_device2.pid = 1000
    fake_device2.serial_number = 'b2'
    fake_device2.device = 'COM2'

    mock_comports.return_value = [fake_device, fake_device2]

    _ = ik.Instrument.open_serial(baud=1234, vid=0, pid=1000)
Beispiel #5
0
def fake_comports():
    """
    Generate a fake list of comports to compare against.
    """
    fake_device = ListPortInfo()
    fake_device.vid = 0
    fake_device.pid = 1000
    fake_device.serial_number = 'a1'
    fake_device.device = 'COM1'

    fake_device2 = ListPortInfo()
    fake_device2.vid = 1
    fake_device2.pid = 1010
    fake_device2.serial_number = 'c0'
    fake_device2.device = 'COM2'
    return [fake_device, fake_device2]
Beispiel #6
0
    def test_is_serial_usb_manufacturer_match(self, mock_port_list,
                                              mock_serial):
        """
        Tests the __init__ function where the port manufacturer
        is not empty and doesn't match the supplied port in RADIO_TRANSMITTER_PORT
        """

        port, port2 = ListPortInfo(), ListPortInfo()
        port.vid = "foo"
        port.pid = "bar"
        port.manufacturer = "Microsoft"
        port.serial_number = "456"
        port.interface = "usb"
        port.device = "usb"

        port2.vid = "foo"
        port2.pid = "bar"
        port2.manufacturer = "Apple"
        port2.serial_number = "123"
        port2.interface = "bluetooth"
        port2.device = "usb2"

        mock_port_list.return_value = [port, port2]

        with patch.dict(
                os.environ,
            {
                "LOG_DIRECTORY": self.temp_dir.path,
                "RADIO_TRANSMITTER_PORT": "usb2",
                "LOG_FILE": "logger.txt",
                "TRANSCEIVER_BAUDRATE": "9600",
                "TRANSCEIVER_TIMEOUT": "1",
            },
        ):
            transciever = Transceiver(log_file_name="LOG_FILE")

            self.assertTrue(transciever.logging is not None)
            self.assertTrue(transciever.logging.name == "LOG_FILE")
            self.assertIsInstance(transciever.logging, Logger)

            self.assertTrue(transciever.port == "usb2")
            self.assertTrue(transciever.port_vid == "foo")
            self.assertTrue(transciever.port_pid == "bar")
            self.assertTrue(transciever.port_vendor == "Apple")
            self.assertTrue(transciever.port_intf == "bluetooth")
            self.assertTrue(transciever.port_serial_number == "123")

            mock_serial.assert_called_with(
                port="usb2",
                baudrate=self.baudrate,
                parity=self.parity,
                stopbits=self.stopbits,
                bytesize=self.bytesize,
                timeout=self.timeout,
            )
Beispiel #7
0
def get_mocked_com_port_none_types():
    """Mock of a serial port with NoneTypes."""
    port = ListPortInfo("/dev/ttyUSB1234")
    port.device = "/dev/ttyUSB1234"
    port.serial_number = None
    port.manufacturer = None
    port.description = "crownstone dongle - crownstone dongle"
    port.vid = None
    port.pid = None

    return port
def get_mocked_com_port():
    """Mock of a serial port."""
    port = ListPortInfo("/dev/ttyUSB1234")
    port.device = "/dev/ttyUSB1234"
    port.serial_number = "1234567"
    port.manufacturer = "crownstone"
    port.description = "crownstone dongle - crownstone dongle"
    port.vid = 1234
    port.pid = 5678

    return port
 def test_args_vid_not_empty(self):
     """
     checks to make sure that the is_serial_usb function
     exists correctly when the args["vid"] is not empty
     and port["vid"] doesn't equal args["vid"]
     """
     port = ListPortInfo()
     port.vid = "foo"
     args = Namespace()
     args.vid = "bar"
     response = is_usb_serial(port, args)
     self.assertFalse(response)
    def test_get_port_empty(self, port_mocks):

        port = ListPortInfo()
        port.vid = None
        port.pid = None
        port.manufacturer = None
        port.serial_number = None
        port.interface = None
        port.device = "usb"

        port_mocks.return_value = [port]
        self.assertIsNone(get_port())
    def test_get_port_match(self, port_mocks):

        port = ListPortInfo()
        port.vid = "vid"
        port.pid = None
        port.manufacturer = None
        port.serial_number = None
        port.interface = None
        port.device = "usb"

        port_mocks.return_value = [port]
        self.assertTrue("port found" in get_port())
    def test_port_vid_empty(self):
        """
        insures that the is_serial_usb function retunrs false
        if the port.vid param is empty
        """
        port = ListPortInfo()
        port.vid = None
        args = Namespace()
        args.vid = None

        response = is_usb_serial(port, args)
        self.assertFalse(response)
Beispiel #13
0
def fixture_patch_comports(mocker):
    comport = "COM1"

    dummy_port_info = ListPortInfo("")
    dummy_port_info.vid = STM_VID
    dummy_port_info.name = comport
    dummy_port_info.description = f"Device ({comport})"

    mocked_comports = mocker.patch.object(
        mc_comm.list_ports,
        "comports",
        autospec=True,
        return_value=[dummy_port_info],
    )
    yield comport, dummy_port_info.description, mocked_comports
Beispiel #14
0
    def test_is_serial_usb_no_vid(self, mock_port_list, mock_serial):
        """
        Tests the __init__ function where the port vid
        is empty
        """

        port = ListPortInfo()
        port.vid = None
        port.pid = None
        port.manufacturer = None
        port.serial_number = None
        port.interface = None
        port.device = "usb"

        mock_port_list.return_value = [port]

        with patch.dict(
                os.environ,
            {
                "LOG_DIRECTORY": self.temp_dir.path,
                "RADIO_TRANSMITTER_PORT": "",
                "LOG_FILE": "logger.txt",
                "TRANSCEIVER_BAUDRATE": "9600",
                "TRANSCEIVER_TIMEOUT": "1",
            },
        ):
            transciever = Transceiver(log_file_name="LOG_FILE")

            self.assertTrue(transciever.logging is not None)
            self.assertTrue(transciever.logging.name == "LOG_FILE")
            self.assertIsInstance(transciever.logging, Logger)

            self.assertTrue(transciever.port == "")
            self.assertIsNone(transciever.port_vid)
            self.assertIsNone(transciever.port_pid)
            self.assertIsNone(transciever.port_vendor)
            self.assertIsNone(transciever.port_intf)
            self.assertIsNone(transciever.port_serial_number)

            mock_serial.assert_called_with(
                port="",
                baudrate=self.baudrate,
                parity=self.parity,
                stopbits=self.stopbits,
                bytesize=self.bytesize,
                timeout=self.timeout,
            )
    def test_args_vendor_not_empty(self):
        """
        checks to make sure that the is_serial_usb function
        exists correctly when the args["vendor"] is not empty
        and port["manufacturer"] doesn't start with args["vendor"]
        """
        port, args = ListPortInfo(), Namespace()
        port.vid = "bar"
        port.pid = "foo"
        port.manufacturer = "Apple"

        args.vid = None
        args.pid = None
        args.vendor = "Microsoft"

        response = is_usb_serial(port, args)
        self.assertFalse(response)
Beispiel #16
0
    def ftdiPorts(cls):
        # FIXME: for some reason, I can't get the Sutter URls with the
        # wildcard and I must handcode the following. It appears the wildcard
        # ftdi:///? does not work for some reason.
        vidpids = [(4930, 1)] # Must add custom pairs in the future.

        urls = []        
        for vid, pid in vidpids:
            pyftdidevices = Ftdi.list_devices(url="ftdi://{0}:{1}/1".format(vid, pid))
            for device, address in pyftdidevices:
                thePort = ListPortInfo(device="")
                thePort.vid = device.vid
                thePort.pid = device.pid
                thePort.serial_number = device.sn
                thePort.device = "ftdi://{0}:{1}:{2}/{3}".format(device.vid, device.pid, device.sn, address)
                urls.append(thePort)

        return urls
Beispiel #17
0
    def test_listen_exception(self, mock_port_list, mock_serial, mock_json):
        """
        tests the listen method with invalid input
        """
        port = ListPortInfo()
        port.vid = "vid"
        port.pid = "pid"
        port.manufacturer = "Microsoft"
        port.serial_number = "456"
        port.interface = "usb"
        port.device = "usb"

        mock_json.side_effect = Exception("ex")

        mock_port_list.return_value = [port]

        test_input = "{'value': 'value'}"
        with patch.dict(
                os.environ,
            {
                "LOG_DIRECTORY": self.temp_dir.path,
                "RADIO_TRANSMITTER_PORT": "usb",
                "LOG_FILE": "logger.txt",
                "TRANSCEIVER_BAUDRATE": "9600",
                "TRANSCEIVER_TIMEOUT": "1",
            },
        ):
            with LogCapture() as capture:
                transceiver = Transceiver(log_file_name="LOG_FILE")

                mock_receiver = MagicMock()
                mock_receiver.readline.return_value.decode.return_value = test_input
                transceiver.serial = mock_receiver

                with self.assertRaises(Exception):
                    transceiver.listen()

                capture.check(
                    ("LOG_FILE", "INFO", "Port device found: usb"),
                    ("LOG_FILE", "INFO", "Opening serial on: usb"),
                    ("LOG_FILE", "ERROR", "error occurred: ex"),
                )
Beispiel #18
0
    def test_send(self, mock_port_list, mock_serial):
        """
        tests the send method
        """
        port = ListPortInfo()
        port.vid = "vid"
        port.pid = "pid"
        port.manufacturer = "Microsoft"
        port.serial_number = "456"
        port.interface = "usb"
        port.device = "usb"

        mock_port_list.return_value = [port]

        test_payload = {"value": "value"}

        output = get_serial_stream(test_payload)

        with patch.dict(
                os.environ,
            {
                "LOG_DIRECTORY": self.temp_dir.path,
                "RADIO_TRANSMITTER_PORT": "usb",
                "LOG_FILE": "logger.txt",
                "TRANSCEIVER_BAUDRATE": "9600",
                "TRANSCEIVER_TIMEOUT": "1",
            },
        ):
            with LogCapture() as capture:
                transciever = Transceiver(log_file_name="LOG_FILE")

                mock_serial_sender = MagicMock()
                transciever.serial = mock_serial_sender

                transciever.send(test_payload)
                mock_serial_sender.write.assert_called_with(output)
                capture.check(
                    ("LOG_FILE", "INFO", "Port device found: usb"),
                    ("LOG_FILE", "INFO", "Opening serial on: usb"),
                    ("LOG_FILE", "INFO", "sending"),
                    ("LOG_FILE", "INFO", "{'value': 'value'}"),
                )
    def test_pass(self):
        """
        insure that is_serial_usb returns true if all test cases haven't
        failed
        """
        port, args = ListPortInfo(), Namespace()

        port.vid = "bar"
        port.pid = "foo"
        port.manufacturer = "Apple"
        port.serial_number = "456"
        port.interface = "bar"

        args.vid = None
        args.pid = None
        args.vendor = None
        args.serial = None
        args.intf = None

        response = is_usb_serial(port, args)
        self.assertTrue(response)
    def test_args_intf_not_empty(self):
        """
        checks to make sure that the is_serial_usb function
        exists correctly when the args["serial"] is not empty
        and port["interface"] is none
        """
        port, args = ListPortInfo(), Namespace()

        port.vid = "bar"
        port.pid = "foo"
        port.manufacturer = "Apple"
        port.serial_number = "456"
        port.interface = None

        args.vid = None
        args.pid = None
        args.vendor = None
        args.serial = None
        args.intf = "foo"

        response = is_usb_serial(port, args)
        self.assertFalse(response)
Beispiel #21
0
def make_port_info(vid: int, pid: int) -> ListPortInfo:
    """Make a ListPortInfo object from a USB vendor ID and product ID."""
    list_port_info = ListPortInfo("/dev/null")
    list_port_info.vid, list_port_info.pid = vid, pid
    return list_port_info