def _test_default_dfi(self):
     response = self.udsclient.request_file_transfer(moop=1,
                                                     path="my_file.txt",
                                                     filesize=Filesize(
                                                         uncompressed=0x222,
                                                         compressed=0x111,
                                                         width=4))
     self.assertEqual(response.service_data.dfi.compression, 0)
     self.assertEqual(response.service_data.dfi.encryption, 0)
 def _test_replace_file_extra_bytes_response_zero_padding_not_ok(self):
     self.udsclient.config['tolerate_zero_padding'] = False
     with self.assertRaises(InvalidResponseException):
         self.udsclient.replace_file("my_file.txt",
                                     dfi=DataFormatIdentifier(compression=5,
                                                              encryption=2),
                                     filesize=Filesize(uncompressed=0x222,
                                                       compressed=0x111,
                                                       width=4))
예제 #3
0
 def test_create(self):
     #Normal use case
     Filesize(uncompressed=123)
     Filesize(compressed=123)
     Filesize(uncompressed=123, compressed=100)
     Filesize(uncompressed=123, compressed=100, width=2)
     Filesize(uncompressed=123, width=2)
     Filesize(compressed=100, width=2)
 def _test_replace_file_success(self):
     response = self.udsclient.replace_file(
         "my_file.txt",
         dfi=DataFormatIdentifier(compression=5, encryption=2),
         filesize=Filesize(uncompressed=0x222, compressed=0x111, width=4))
     self.assertTrue(response.valid)
     self.assertTrue(response.positive)
     self.assertEqual(response.service_data.moop_echo, 3)
     self.assertEqual(response.service_data.max_length, 0xabcd)
     self.assertEqual(response.service_data.dfi.compression, 5)
     self.assertEqual(response.service_data.dfi.encryption, 2)
     self.assertIsNone(
         response.service_data.filesize
     )  # No filesize info when doing AddFile. Only for ReadFile
     self.assertIsNone(response.service_data.dirinfo_length)
 def _test_add_file_extra_bytes_response_zero_padding_ok(self):
     self.udsclient.config['tolerate_zero_padding'] = True
     response = self.udsclient.add_file(
         "my_file.txt",
         dfi=DataFormatIdentifier(compression=5, encryption=2),
         filesize=Filesize(uncompressed=0x222, compressed=0x111, width=4))
     self.assertTrue(response.valid)
     self.assertTrue(response.positive)
     self.assertEqual(response.service_data.moop_echo, 1)
     self.assertEqual(response.service_data.max_length, 0xabcd)
     self.assertEqual(response.service_data.dfi.compression, 5)
     self.assertEqual(response.service_data.dfi.encryption, 2)
     self.assertIsNone(
         response.service_data.filesize
     )  # No filesize info when doing AddFile. Only for ReadFile
     self.assertIsNone(response.service_data.dirinfo_length)
    def _test_bad_param(self):
        good_filesize = Filesize(uncompressed=0x222, compressed=0x111, width=4)
        good_dfi = DataFormatIdentifier(compression=5, encryption=2)
        with self.assertRaises(ValueError):
            self.udsclient.request_file_transfer(moop=1,
                                                 path="a" * (2**16),
                                                 dfi=good_dfi,
                                                 filesize=good_filesize)

        with self.assertRaises(ValueError):
            self.udsclient.request_file_transfer(moop=1,
                                                 path="",
                                                 dfi=good_dfi,
                                                 filesize=good_filesize)

        with self.assertRaises(ValueError):
            self.udsclient.request_file_transfer(moop=1,
                                                 path="hello.txt",
                                                 dfi=1,
                                                 filesize=good_filesize)

        with self.assertRaises(ValueError):
            self.udsclient.request_file_transfer(moop=1,
                                                 path="hello.txt",
                                                 dfi=good_dfi,
                                                 filesize="asd")

        for moop in [2, 4, 5]:  # Delete File, Read File, Read Dir
            with self.assertRaises(ValueError):
                self.udsclient.request_file_transfer(
                    moop=moop,
                    path="hello.txt",
                    dfi=good_dfi,
                    filesize=good_filesize)  # unexpected filesize

        for moop in [1, 3]:  # Add File, Replace File
            with self.assertRaises(ValueError):
                self.udsclient.request_file_transfer(
                    moop=moop, path="hello.txt", dfi=good_dfi,
                    filesize=None)  # missing filesize

        for moop in [2, 5]:
            with self.assertRaises(ValueError):
                self.udsclient.request_file_transfer(
                    moop=moop, path="hello.txt", dfi=good_dfi,
                    filesize=None)  # dfi not expected
예제 #7
0
    def test_bytes(self):

        self.assertEqual(
            Filesize(uncompressed=0xFF).get_uncompressed_bytes(), '\xFF')
        self.assertEqual(
            Filesize(uncompressed=0x12345678).get_uncompressed_bytes(),
            '\x12\x34\x56\x78')
        self.assertEqual(
            Filesize(uncompressed=0x1234, width=4).get_uncompressed_bytes(),
            '\x00\x00\x12\x34')
        self.assertEqual(
            Filesize(uncompressed=0xFF).get_compressed_bytes(), '')

        self.assertEqual(
            Filesize(compressed=0xFF).get_compressed_bytes(), '\xFF')
        self.assertEqual(
            Filesize(compressed=0x12345678).get_compressed_bytes(),
            '\x12\x34\x56\x78')
        self.assertEqual(
            Filesize(compressed=0x1234, width=4).get_compressed_bytes(),
            '\x00\x00\x12\x34')
        self.assertEqual(
            Filesize(compressed=0x12345678).get_uncompressed_bytes(), '')
    def make_request(cls, moop, path, dfi=None, filesize=None):
        """
        Generates a request for RequestFileTransfer

        :param moop: Mode of operation. Can be AddFile(1), DeleteFile(2), ReplaceFile(3), ReadFile(4), ReadDir(5). See :class:`RequestFileTransfer.ModeOfOperation<udsoncan.services.RequestFileTransfer.ModeOfOperation>`
        :type moop: int

        :param path: String representing the path to the target file or directory.
        :type path: string

        :param dfi: DataFormatIdentifier defining the compression and encryption scheme of the data. 
                If not specified, the default value of 00 will be used, specifying no encryption and no compression. 
                This value is only used when ModeOfOperation is : ``AddFile``, ``ReplaceFile``, ``ReadFile``
        :type dfi: :ref:`DataFormatIdentifier<DataFormatIdentifier>`

        :param filesize: The filesize of the file to write when ModeOfOperation is ``AddFile`` or ``ReplaceFile``. 
            If filesize is an object of type :ref:`Filesize<Filesize>`, the uncompressed size and compressed size will be encoded on
            the minimum amount of bytes necessary, unless a ``width`` is explicitly defined. If no compressed size is given or filesize is an ``int``,
            then the compressed size will be set equal to the uncompressed size or the integer value given as specified by ISO-14229
        :type filesize: :ref:`Filesize<Filesize>` or int

        :raises ValueError: If parameters are out of range, missing or wrong type
        """
        from udsoncan import Request, Filesize
        if not isinstance(moop, int):
            raise ValueError('Mode of operation must be an integer')

        if moop not in [
                cls.ModeOfOperation.AddFile, cls.ModeOfOperation.DeleteFile,
                cls.ModeOfOperation.ReplaceFile, cls.ModeOfOperation.ReadFile,
                cls.ModeOfOperation.ReadDir
        ]:
            raise ValueError("Mode of operation of %d is not a known mode" %
                             moop)

        if not isinstance(path, str):
            raise ValueError('Given path must be a valid string')

        if len(path) <= 0:
            raise ValueError('Path must be a string longer than 0 character')

        path_ascii = path.encode('ascii')
        if len(path_ascii) > 0xFF:
            raise ValueError(
                'Path length must be smaller or equal than 255 bytes when encoded in ASCII'
            )

        use_dfi = moop in [
            cls.ModeOfOperation.AddFile, cls.ModeOfOperation.ReplaceFile,
            cls.ModeOfOperation.ReadFile
        ]
        use_filesize = moop in [
            cls.ModeOfOperation.AddFile, cls.ModeOfOperation.ReplaceFile
        ]

        if use_dfi:
            dfi = cls.normalize_data_format_identifier(dfi)
        else:
            if dfi is not None:
                raise ValueError(
                    'DataFormatIdentifier is not needed with ModeOfOperation=%d'
                    % moop)

        if use_filesize:
            if filesize is None:
                raise ValueError(
                    'A filesize must be given for this mode of operation')

            if isinstance(filesize, int):
                filesize = Filesize(filesize)

            if not isinstance(filesize, Filesize):
                raise ValueError(
                    'Given filesize must be a valid Filesize object or an integer'
                )

            if filesize.uncompressed is None:
                raise ValueError(
                    'Filesize needs at least an Uncompressed file size')

            if filesize.compressed is None:
                filesize = Filesize(uncompressed=filesize.uncompressed,
                                    compressed=filesize.uncompressed,
                                    width=filesize.get_width())
        else:
            if filesize is not None:
                raise ValueError(
                    'Filesize is not needed with ModeOfOperation=%d' % moop)

        data = moop.to_bytes(1, 'big')
        data += len(path_ascii).to_bytes(1, 'big')
        data += path_ascii
        if use_dfi:
            data += dfi.get_byte()
        if use_filesize:
            data += filesize.get_width().to_bytes(1, 'big')
            data += filesize.get_uncompressed_bytes()
            data += filesize.get_compressed_bytes()

        request = Request(cls, data=data)
        return request
    def interpret_response(cls, response, tolerate_zero_padding=True):
        """
        Populates the response ``service_data`` property with an instance of :class:`RequestFileTransfer.ResponseData<udsoncan.services.RequestFileTransfer.ResponseData>`

        :param response: The received response to interpret
        :type response: :ref:`Response<Response>`

        :raises InvalidResponseException: If length of ``response.data`` is too short or payload does not respect ISO-14229 specifications
        :raises NotImplementedError: If the MaxNumberOfBlock or fileSizeUncompressedOrDirInfoLength value is encoded over more than 8 bytes.
        """
        from udsoncan import Filesize, DataFormatIdentifier
        response.service_data = cls.ResponseData()
        if len(response.data) < 1:
            raise InvalidResponseException(
                response, 'Response payload must be at least 1 byte long')
        response.service_data.moop_echo = int(response.data[0])

        has_lfid = response.service_data.moop_echo in [
            cls.ModeOfOperation.AddFile, cls.ModeOfOperation.ReplaceFile,
            cls.ModeOfOperation.ReadFile, cls.ModeOfOperation.ReadDir
        ]
        has_dfi = response.service_data.moop_echo in [
            cls.ModeOfOperation.AddFile, cls.ModeOfOperation.ReplaceFile,
            cls.ModeOfOperation.ReadFile, cls.ModeOfOperation.ReadDir
        ]
        has_filesize_length = response.service_data.moop_echo in [
            cls.ModeOfOperation.ReadFile, cls.ModeOfOperation.ReadDir
        ]
        has_uncompressed_filesize = response.service_data.moop_echo in [
            cls.ModeOfOperation.ReadFile, cls.ModeOfOperation.ReadDir
        ]
        has_compressed_filesize = response.service_data.moop_echo in [
            cls.ModeOfOperation.ReadFile
        ]

        cursor = 1
        if has_lfid:
            if len(response.data) < 2:
                raise InvalidResponseException(
                    response,
                    'Response payload must be at least 2 byte long for Mode of operation %d'
                    % response.service_data.moop_echo)
            lfid = int(response.data[1])
            cursor = 2

            if lfid > 8:
                raise NotImplementedError(
                    'This client does not support number bigger than %d bits, but MaxNumberOfBlock is encoded on %d bits'
                    % ((8 * 8), (lfid * 8)))

            if lfid == 0:
                raise InvalidResponseException(
                    response,
                    'Received a MaxNumberOfBlockLength of 0 which is impossible'
                )

            if len(response.data) < 2 + lfid:
                raise InvalidResponseException(
                    response,
                    'Response payload says that MaxNumberOfBlock is encoded on %d bytes, but only %d bytes are present'
                    % (lfid, (len(response.data) - 2)))

            todecode = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
            for i in range(1, lfid + 1):
                todecode[-i] = response.data[cursor + lfid - i]
            response.service_data.max_length = struct.unpack('>q', todecode)[0]
            cursor += lfid

        if has_dfi:
            if len(response.data) < cursor + 1:
                raise InvalidResponseException(
                    response,
                    'Missing DataFormatIdentifier in received response')

            response.service_data.dfi = DataFormatIdentifier.from_byte(
                response.data[cursor])
            cursor += 1
            dfi = response.service_data.dfi.get_byte_as_int()

            if response.service_data.moop_echo == cls.ModeOfOperation.ReadDir and dfi != 0:
                raise InvalidResponseException(
                    response,
                    'DataFormatIdentifier for ReadDir can only be 0x00 as per ISO-14229, but its value was set to 0x%02x'
                    % (dfi))

        if has_filesize_length:
            if len(response.data) < cursor + 2:
                raise InvalidResponseException(
                    response,
                    'Missing or incomplete FileSizeOrDirInfoParameterLength in received response'
                )
            fsodipl = struct.unpack('>H', response.data[cursor:cursor + 2])[0]
            cursor += 2

            if fsodipl > 8:
                raise NotImplementedError(
                    response,
                    'This client does not support number bigger than %d bits, but FileSizeOrDirInfoLength is encoded on %d bits'
                    % ((8 * 8), (fsodipl * 8)))

            if fsodipl == 0:
                raise InvalidResponseException(
                    response,
                    'Received a FileSizeOrDirInfoParameterLength of 0 which is impossible'
                )

            if has_uncompressed_filesize:
                if len(response.data) < cursor + fsodipl:
                    raise InvalidResponseException(
                        response,
                        'Missing or incomplete fileSizeUncompressedOrDirInfoLength in received response'
                    )

                todecode = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
                for i in range(1, lfid + 1):
                    todecode[-i] = response.data[cursor + fsodipl - i]
                uncompressed_size = struct.unpack('>q', todecode)[0]
                cursor += fsodipl
            else:
                uncompressed_size = None

            if has_compressed_filesize:
                if len(response.data) < cursor + fsodipl:
                    raise InvalidResponseException(
                        response,
                        'Missing or incomplete fileSizeCompressed in received response'
                    )

                todecode = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
                for i in range(1, lfid + 1):
                    todecode[-i] = response.data[cursor + fsodipl - i]
                compressed_size = struct.unpack('>q', todecode)[0]
                cursor += fsodipl
            else:
                compressed_size = None

        if has_uncompressed_filesize and response.service_data.moop_echo == cls.ModeOfOperation.ReadDir:
            response.service_data.dirinfo_length = uncompressed_size
        else:
            if has_uncompressed_filesize or has_compressed_filesize:
                response.service_data.filesize = Filesize(
                    uncompressed=uncompressed_size, compressed=compressed_size)

        if len(response.data) > cursor:
            if response.data[cursor:] == b'\x00' * (
                    len(response.data) - cursor) and tolerate_zero_padding:
                pass
            else:
                raise InvalidResponseException(
                    response,
                    'Response payload has extra data that has no meaning')

        response.service_data = response.service_data
 def _test_filesize_no_compressed_size(self):
     self.udsclient.request_file_transfer(moop=1,
                                          path="my_file.txt",
                                          filesize=Filesize(
                                              uncompressed=0x222, width=4))
 def _test_default_filesize_width(self):
     self.udsclient.request_file_transfer(moop=1,
                                          path="my_file.txt",
                                          filesize=Filesize(
                                              uncompressed=0x222,
                                              compressed=0x111))
예제 #12
0
 def test_str_repr(self):
     fs = Filesize(uncompressed=123)
     unicode(fs)
     fs.__repr__()
예제 #13
0
    def test_width(self):

        self.assertEqual(Filesize(uncompressed=0xFF).get_width(), 1)
        self.assertEqual(Filesize(uncompressed=0x100).get_width(), 2)
        self.assertEqual(Filesize(uncompressed=0xFFFF).get_width(), 2)
        self.assertEqual(Filesize(uncompressed=0x10000).get_width(), 3)
        self.assertEqual(Filesize(uncompressed=0xFFFFFF).get_width(), 3)
        self.assertEqual(Filesize(uncompressed=0x1000000).get_width(), 4)
        self.assertEqual(Filesize(uncompressed=0xFFFFFFFF).get_width(), 4)

        self.assertEqual(Filesize(compressed=0xFF).get_width(), 1)
        self.assertEqual(Filesize(compressed=0x100).get_width(), 2)
        self.assertEqual(Filesize(compressed=0xFFFF).get_width(), 2)
        self.assertEqual(Filesize(compressed=0x10000).get_width(), 3)
        self.assertEqual(Filesize(compressed=0xFFFFFF).get_width(), 3)
        self.assertEqual(Filesize(compressed=0x1000000).get_width(), 4)
        self.assertEqual(Filesize(compressed=0xFFFFFFFF).get_width(), 4)

        self.assertEqual(
            Filesize(uncompressed=0xFF, compressed=0xFF).get_width(), 1)
        self.assertEqual(
            Filesize(uncompressed=0x100, compressed=0xFF).get_width(), 2)
        self.assertEqual(
            Filesize(uncompressed=0xFF, compressed=0x100).get_width(), 2)
        self.assertEqual(
            Filesize(uncompressed=0xFFFFFF, compressed=0x100).get_width(), 3)
        self.assertEqual(
            Filesize(uncompressed=0xFFFFFF, compressed=0xFFFFFFFF).get_width(),
            4)

        self.assertEqual(
            Filesize(uncompressed=0xFF, compressed=0xFF, width=4).get_width(),
            4)
        self.assertEqual(
            Filesize(uncompressed=0xFF, compressed=0xFF, width=8).get_width(),
            8)

        with self.assertRaises(ValueError):
            Filesize(uncompressed=0x100, compressed=0x100, width=1)

        with self.assertRaises(ValueError):
            Filesize(uncompressed=0xFF, compressed=0x100, width=1)

        with self.assertRaises(ValueError):
            Filesize(uncompressed=0x100, compressed=0xFF, width=1)
 def _test_replace_file_extra_bytes_response(self):
     with self.assertRaises(InvalidResponseException):
         self.udsclient.replace_file("my_file.txt", dfi= DataFormatIdentifier(compression=5, encryption=2), filesize = Filesize(uncompressed=0x222, compressed=0x111, width=4))              
 def _test_replace_file_negative_response_no_exception(self):
     self.udsclient.config['exception_on_negative_response'] = False
     response = self.udsclient.replace_file("my_file.txt", dfi= DataFormatIdentifier(compression=5, encryption=2), filesize = Filesize(uncompressed=0x222, compressed=0x111, width=4))
     self.assertTrue(response.valid)
     self.assertFalse(response.positive)