コード例 #1
0
 def test_to_byte_array_list_fail_neg(self, list_val):
     """
     to_byte_array() with incompatible param:
     :param list_val: random list of negative integers. -ValueError
     """
     with pytest.raises(ValueError):
         pointer, leng = to_byte_array(list_val)
コード例 #2
0
    def test_to_byte_array_int(self, int_val):
        """
        to_byte_array() with param:
        :param int_val: random positive integer
        """
        pointer, leng = to_byte_array(int_val)
        self.verify_c_type(pointer, leng)

        py_bytes = self.reverse_case(pointer, leng, to_byte_array)
        assert int(py_bytes, 16) == int_val
コード例 #3
0
    def test_to_byte_array_from_hex(self, test_val):
        """
        to_byte_array() with param:
        :param list_val: list of ints in range (0-255), convert to bytearray
        """
        pointer, leng = to_byte_array(test_val)
        self.verify_c_type(pointer, leng)

        py_bytes = self.reverse_case(pointer, leng, to_byte_array)
        assert py_bytes == b"deadbeef"
コード例 #4
0
    def test_to_byte_array_hexstring(self, int_val):
        """
        to_byte_array() with param:
        :param int_val: random integer to be converted to hex string.
        """
        hex_string = b(hex(int_val).replace("0x", "").replace("L", ""))
        pointer, leng = to_byte_array(hex_string)
        self.verify_c_type(pointer, leng)

        py_bytes = self.reverse_case(pointer, leng, to_byte_array)
        assert int(py_bytes, 16) == int(hex_string, 16)
コード例 #5
0
    def test_to_byte_array_int_neg_overflow(self, int_val):
        """
        to_byte_array() with param:
        :param int_val: random int value. Will result in data loss
        """
        pointer, leng = to_byte_array(int_val)
        self.verify_c_type(pointer, leng)

        py_bytes = self.reverse_case(pointer, leng, to_byte_array)
        LOG.debug("to_byte_array() data loss: %s => %s", b(hex(int_val)),
                  py_bytes)
        assert int(py_bytes, 16) != int_val
コード例 #6
0
    def test_to_byte_array(self, list_val):
        """
        to_byte_array() with param:
        :param list_val: list of ints in range (0-255), convert to bytearray
        """
        b_array = bytearray(list_val)

        pointer, leng = to_byte_array(b_array)
        self.verify_c_type(pointer, leng)

        py_bytes = self.reverse_case(pointer, leng, to_byte_array)
        assert py_bytes == hexlify(b_array)
コード例 #7
0
    def test_to_byte_array_list(self, list_val):
        """
        to_byte_array() with param:
        :param list_val: randomly list of postive integers (within byte range).
        """
        pointer, leng = to_byte_array(list_val)
        self.verify_c_type(pointer, leng)

        py_bytes = self.reverse_case(pointer, leng, to_byte_array)

        # Create list from returned byte-string
        py_list = []
        for i in range(0, len(py_bytes), 2):
            py_list.append(int(py_bytes[i:i + 2], 16))

        assert py_list == list_val