Exemplo n.º 1
0
 def test_bytes_to_string(self):
     """Test conversion of byte arrays."""
     self.assertEqual('0A0B',
                      GatttoolBackend.bytes_to_string(bytes([0x0A, 0x0B])))
     self.assertEqual(
         '0x0C0D', GatttoolBackend.bytes_to_string(bytes([0x0C, 0x0D]),
                                                   True))
Exemplo n.º 2
0
 def test_read_handle_empty_output(self, _, popen_mock):
     """Test reading handle where no result is returned."""
     _configure_popenmock(popen_mock, '')
     backend = GatttoolBackend()
     backend.connect(TEST_MAC)
     result = backend.read_handle(0xFF)
     self.assertIsNone(result)
Exemplo n.º 3
0
 def test_write_handle_ok(self, time_mock, popen_mock):
     """Test writing to a handle successfully."""
     _configure_popenmock(popen_mock,
                          'Characteristic value was written successfully')
     backend = GatttoolBackend()
     backend.connect(TEST_MAC)
     self.assertTrue(backend.write_handle(0xFF, b'\x00\x10\xFF'))
Exemplo n.º 4
0
 def test_read_handle_ok(self, popen_mock):
     """Test reading handle successfully."""
     gattoutput = bytes([0x00, 0x11, 0xAA, 0xFF])
     _configure_popenmock(popen_mock, 'Characteristic value/descriptor: 00 11 AA FF')
     be = GatttoolBackend()
     be.connect(TEST_MAC)
     result = be.read_handle(0xFF)
     self.assertEqual(gattoutput, result)
Exemplo n.º 5
0
 def test_run_connect_disconnect(self):
     """Just run connect and disconnect"""
     backend = GatttoolBackend()
     backend.connect(TEST_MAC)
     self.assertEqual(TEST_MAC, backend._mac)
     backend.disconnect()
     self.assertEqual(None, backend._mac)
Exemplo n.º 6
0
 def test_write_handle_wrong_handle(self, time_mock, popen_mock):
     """Test writing to a non-writable handle."""
     _configure_popenmock(popen_mock, "Characteristic Write Request failed: Attribute can't be written")
     be = GatttoolBackend()
     be.connect(TEST_MAC)
     with self.assertRaises(ValueError):
         be.write_handle(0xFF, b'\X00\X10\XFF')
Exemplo n.º 7
0
 def test_read_handle_wrong_handle(self, popen_mock):
     """Test reading invalid handle."""
     _configure_popenmock(popen_mock, 'Characteristic value/descriptor read failed: Invalid handle')
     be = GatttoolBackend()
     be.connect(TEST_MAC)
     with self.assertRaises(ValueError):
         be.read_handle(0xFF)
Exemplo n.º 8
0
 def test_check_backend_fail(self, call_mock):
     """Test check_backend with IOError being risen."""
     self.assertFalse(GatttoolBackend().check_backend())
Exemplo n.º 9
0
 def test_check_backend_ok(self, call_mock):
     """Test check_backend successfully."""
     self.assertTrue(GatttoolBackend().check_backend())
Exemplo n.º 10
0
 def setUp(self):
     """Setup of the test case."""
     self.backend = GatttoolBackend(retries=0, timeout=20)
Exemplo n.º 11
0
class TestGatttoolBackend(unittest.TestCase):
    def setUp(self):
        self.backend = GatttoolBackend(retries=0, timeout=5)

    @pytest.mark.usefixtures("mac")
    def test_read(self):
        self.backend.connect(self.mac)
        result = self.backend.read_handle(TEST_READ_HANDLE)
        self.assertIsNotNone(result)
        self.backend.disconnect()

    @pytest.mark.usefixtures("mac")
    def test_write(self):
        self.backend.connect(self.mac)
        result = self.backend.write_handle(TEST_WRITE_HANDLE, TEST_WRITE_DATA)
        self.assertIsNotNone(result)
        self.backend.disconnect()

    def test_read_not_connected(self):
        try:
            self.backend.read_handle(TEST_READ_HANDLE)
            self.fail('should have thrown an exception')
        except ValueError:
            pass

    def test_check_backend(self):
        self.backend.check_backend()
Exemplo n.º 12
0
 def test_byte_to_handle(self):
     """Test conversion of handles."""
     self.assertEqual('0x0B', GatttoolBackend.byte_to_handle(0x0B))
     self.assertEqual('0xAF', GatttoolBackend.byte_to_handle(0xAF))
     self.assertEqual('0xAABB', GatttoolBackend.byte_to_handle(0xAABB))
Exemplo n.º 13
0
 def test_check_backend_ok(self, call_mock):
     """Test check_backend successfully."""
     be = GatttoolBackend()
     be.check_backend()
Exemplo n.º 14
0
 def test_read_not_connected(self):
     """Test reading data when not connected."""
     backend = GatttoolBackend()
     with self.assertRaises(ValueError):
         backend.read_handle(0xFF)
Exemplo n.º 15
0
 def setUp(self):
     self.backend = GatttoolBackend(retries=0, timeout=5)
Exemplo n.º 16
0
 def test_write_not_connected(self):
     """Test writing data when not connected."""
     backend = GatttoolBackend()
     with self.assertRaises(ValueError):
         backend.write_handle(0xFF, [0x00])
Exemplo n.º 17
0
 def test_bytes_to_string(self):
     self.assertEqual('0A0B',
                      GatttoolBackend.bytes_to_string(bytes([0x0A, 0x0B])))
     self.assertEqual(
         '0x0C0D', GatttoolBackend.bytes_to_string(bytes([0x0C, 0x0D]),
                                                   True))
Exemplo n.º 18
0
 def test_write_handle_no_answer(self, time_mock, popen_mock):
     """Test writing to a handle when no result is returned."""
     _configure_popenmock(popen_mock, '')
     backend = GatttoolBackend()
     backend.connect(TEST_MAC)
     self.assertFalse(backend.write_handle(0xFF, b'\x00\x10\xFF'))
Exemplo n.º 19
0
 def test_byte_to_handle(self):
     self.assertEqual('0x0B', GatttoolBackend.byte_to_handle(0x0B))
     self.assertEqual('0xAF', GatttoolBackend.byte_to_handle(0xAF))
     self.assertEqual('0xAABB', GatttoolBackend.byte_to_handle(0xAABB))
Exemplo n.º 20
0
class TestGatttoolBackend(unittest.TestCase):
    """Test GatttoolBackend with real sensor."""

    # pylint does not understand pytest fixtures, so we have to disable the warning
    # pylint: disable=no-member

    def setUp(self):
        """Setup of the test case."""
        self.backend = GatttoolBackend(retries=0, timeout=20)

    @pytest.mark.usefixtures("mac")
    def test_read(self):
        """Test reading a handle from the sensor."""
        self.backend.connect(self.mac)
        result = self.backend.read_handle(HANDLE_READ_NAME)
        self.assertIsNotNone(result)
        self.backend.disconnect()

    @pytest.mark.usefixtures("mac")
    def test_write(self):
        """Test writing data to handle of the sensor."""
        self.backend.connect(self.mac)
        result = self.backend.write_handle(HANDLE_WRITE_MODE_CHANGE,
                                           DATA_MODE_CHANGE)
        self.assertIsNotNone(result)
        self.backend.disconnect()

    def test_read_not_connected(self):
        """Test error handling if not connected."""
        with self.assertRaises(BluetoothBackendException):
            self.backend.read_handle(HANDLE_READ_NAME)

    def test_check_backend(self):
        """Test check_backend function."""
        self.assertTrue(self.backend.check_backend())

    def test_invalid_mac_exception(self):
        """Test writing data to handle of the sensor."""
        with self.assertRaises(BluetoothBackendException):
            self.backend.connect(TEST_MAC)
            self.backend.read_handle(HANDLE_READ_NAME)
Exemplo n.º 21
0
 def test_check_backend_fail(self, call_mock):
     """Test check_backend with IOError being risen."""
     be = GatttoolBackend()
     with self.assertRaises(BluetoothBackendException):
         be.check_backend()