Ejemplo n.º 1
0
  def test_open_close(self):
    """Tests the open and close functions."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    # Test open and close.
    smdev_handle.open(unittest.source)
    smdev_handle.close()

    # Test open and close a second time to validate clean up on close.
    smdev_handle.open(unittest.source)
    smdev_handle.close()

    file_object = open(unittest.source, "rb")

    # Test open_file_object and close.
    smdev_handle.open_file_object(file_object)
    smdev_handle.close()

    # Test open_file_object and close a second time to validate clean up on close.
    smdev_handle.open_file_object(file_object)
    smdev_handle.close()

    # Test open_file_object and close and dereferencing file_object.
    smdev_handle.open_file_object(file_object)
    del file_object
    smdev_handle.close()
Ejemplo n.º 2
0
  def _Open(self, path_spec=None, mode='rb'):
    """Opens the file-like object defined by path specification.

    Args:
      path_spec: optional path specification (instance of path.PathSpec).
                 The default is None.
      mode: optional file access mode. The default is 'rb' read-only binary.

    Raises:
      AccessError: if the access to open the file was denied.
      IOError: if the file-like object could not be opened.
      PathSpecError: if the path specification is incorrect.
      ValueError: if the path specification is invalid.
    """
    if not path_spec:
      raise ValueError(u'Missing path specfication.')

    if path_spec.HasParent():
      raise errors.PathSpecError(u'Unsupported path specification with parent.')

    location = getattr(path_spec, 'location', None)

    if location is None:
      raise errors.PathSpecError(u'Path specification missing location.')

    # Windows does not support running os.stat on device files so we use
    # libsmdev to do an initial check.
    try:
      is_device = pysmdev.check_device(location)
    except IOError as exception:
      # Since os.stat() will not recognize Windows device file names and
      # will return '[Error 87] The parameter is incorrect' we check here
      # if pysmdev exception message contains ' access denied ' and raise
      # AccessError instead.
      if u' access denied ' in exception.message:
        raise errors.AccessError(
            u'Access denied to file: {0:s} with error: {1:s}'.format(
                location, exception))
      is_device = False

    if not is_device:
      try:
        stat_info = os.stat(location)
      except OSError as exception:
        raise IOError(u'Unable to open file with error: {0:s}.'.format(
            exception))

      # In case the libsmdev check is not able to detect the device also use
      # the stat information.
      if stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode):
        is_device = True

    if is_device:
      self._file_object = pysmdev.handle()
      self._file_object.open(location, mode=mode)
      self._size = self._file_object.media_size

    else:
      self._file_object = open(location, mode=mode)
      self._size = stat_info.st_size
Ejemplo n.º 3
0
  def test_read_buffer_at_offset(self):
    """Tests the read_buffer_at_offset function."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    smdev_handle.open(unittest.source)

    file_size = smdev_handle.get_size()

    # Test normal read.
    data = smdev_handle.read_buffer_at_offset(4096, 0)

    self.assertIsNotNone(data)
    self.assertEqual(len(data), min(file_size, 4096))

    # Test read beyond file size.
    if file_size > 16:
      data = smdev_handle.read_buffer_at_offset(4096, file_size - 16)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), 16)

    with self.assertRaises(ValueError):
      smdev_handle.read_buffer_at_offset(-1, 0)

    with self.assertRaises(ValueError):
      smdev_handle.read_buffer_at_offset(4096, -1)

    smdev_handle.close()

    # Test the read without open.
    with self.assertRaises(IOError):
      smdev_handle.read_buffer_at_offset(4096, 0)
Ejemplo n.º 4
0
  def test_open_close(self):
    """Tests the open and close functions."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    # Test open and close.
    smdev_handle.open(unittest.source)
    smdev_handle.close()

    # Test open and close a second time to validate clean up on close.
    smdev_handle.open(unittest.source)
    smdev_handle.close()

    file_object = open(unittest.source, "rb")

    # Test open_file_object and close.
    smdev_handle.open_file_object(file_object)
    smdev_handle.close()

    # Test open_file_object and close a second time to validate clean up on close.
    smdev_handle.open_file_object(file_object)
    smdev_handle.close()

    # Test open_file_object and close and dereferencing file_object.
    smdev_handle.open_file_object(file_object)
    del file_object
    smdev_handle.close()
Ejemplo n.º 5
0
  def test_read_buffer_at_offset(self):
    """Tests the read_buffer_at_offset function."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    smdev_handle.open(unittest.source)

    file_size = smdev_handle.get_size()

    # Test normal read.
    data = smdev_handle.read_buffer_at_offset(4096, 0)

    self.assertIsNotNone(data)
    self.assertEqual(len(data), min(file_size, 4096))

    # Test read beyond file size.
    if file_size > 16:
      data = smdev_handle.read_buffer_at_offset(4096, file_size - 16)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), 16)

    with self.assertRaises(ValueError):
      smdev_handle.read_buffer_at_offset(-1, 0)

    with self.assertRaises(ValueError):
      smdev_handle.read_buffer_at_offset(4096, -1)

    smdev_handle.close()

    # Test the read without open.
    with self.assertRaises(IOError):
      smdev_handle.read_buffer_at_offset(4096, 0)
Ejemplo n.º 6
0
  def _Open(self, mode='rb'):
    """Opens the file-like object defined by path specification.

    Args:
      mode (Optional[str]): file access mode.

    Raises:
      AccessError: if the access to open the file was denied.
      IOError: if the file-like object could not be opened.
      OSError: if the file-like object could not be opened.
      PathSpecError: if the path specification is incorrect.
    """
    if self._path_spec.HasParent():
      raise errors.PathSpecError('Unsupported path specification with parent.')

    location = getattr(self._path_spec, 'location', None)

    if location is None:
      raise errors.PathSpecError('Path specification missing location.')

    # Windows does not support running os.stat on device files so we use
    # libsmdev to do an initial check.
    try:
      is_device = pysmdev.check_device(location)
    except IOError as exception:
      # Since os.stat() will not recognize Windows device file names and
      # will return '[Error 87] The parameter is incorrect' we check here
      # if pysmdev exception message contains ' access denied ' and raise
      # AccessError instead.
      if ' access denied ' in str(exception):
        raise errors.AccessError(
            'Access denied to file: {0:s} with error: {1!s}'.format(
                location, exception))

      is_device = False

    if not is_device:
      try:
        stat_info = os.stat(location)
      except OSError as exception:
        raise IOError('Unable to open file with error: {0!s}.'.format(
            exception))

      # In case the libsmdev check is not able to detect the device also use
      # the stat information.
      if stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode):
        is_device = True

    if is_device:
      smdev_handle = pysmdev.handle()
      smdev_handle.open(location, mode=mode)

      self._file_object = smdev_handle
      self._size = smdev_handle.media_size

    else:
      self._file_object = open(location, mode=mode)
      self._size = stat_info.st_size
Ejemplo n.º 7
0
  def test_seek_offset(self):
    """Tests the seek_offset function."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    smdev_handle.open(unittest.source)

    file_size = smdev_handle.get_size()

    smdev_handle.seek_offset(16, os.SEEK_SET)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, 16)

    smdev_handle.seek_offset(16, os.SEEK_CUR)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, 32)

    smdev_handle.seek_offset(-16, os.SEEK_CUR)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, 16)

    smdev_handle.seek_offset(-16, os.SEEK_END)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, file_size - 16)

    smdev_handle.seek_offset(16, os.SEEK_END)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, file_size + 16)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(-1, os.SEEK_SET)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(-32 - file_size, os.SEEK_CUR)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(-32 - file_size, os.SEEK_END)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(0, -1)

    smdev_handle.close()

    # Test the seek without open.
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(16, os.SEEK_SET)
Ejemplo n.º 8
0
  def test_close(self):
    """Tests the close function."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    with self.assertRaises(IOError):
      smdev_handle.close()
Ejemplo n.º 9
0
  def test_close(self):
    """Tests the close function."""
    if not unittest.source:
      raise unittest.SkipTest("missing source")

    smdev_handle = pysmdev.handle()

    with self.assertRaises(IOError):
      smdev_handle.close()
Ejemplo n.º 10
0
  def test_seek_offset(self):
    """Tests the seek_offset function."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    smdev_handle.open(unittest.source)

    file_size = smdev_handle.get_size()

    smdev_handle.seek_offset(16, os.SEEK_SET)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, 16)

    smdev_handle.seek_offset(16, os.SEEK_CUR)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, 32)

    smdev_handle.seek_offset(-16, os.SEEK_CUR)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, 16)

    smdev_handle.seek_offset(-16, os.SEEK_END)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, file_size - 16)

    smdev_handle.seek_offset(16, os.SEEK_END)

    offset = smdev_handle.get_offset()
    self.assertEqual(offset, file_size + 16)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(-1, os.SEEK_SET)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(-32 - file_size, os.SEEK_CUR)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(-32 - file_size, os.SEEK_END)

    # TODO: change IOError into ValueError
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(0, -1)

    smdev_handle.close()

    # Test the seek without open.
    with self.assertRaises(IOError):
      smdev_handle.seek_offset(16, os.SEEK_SET)
Ejemplo n.º 11
0
  def test_close(self):
    """Tests the close function."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    with self.assertRaises(IOError):
      smdev_handle.close()
Ejemplo n.º 12
0
  def test_open_close(self):
    """Tests the open and close functions."""
    if not unittest.source:
      raise unittest.SkipTest("missing source")

    smdev_handle = pysmdev.handle()

    # Test open and close.
    smdev_handle.open(unittest.source)
    smdev_handle.close()

    # Test open and close a second time to validate clean up on close.
    smdev_handle.open(unittest.source)
    smdev_handle.close()
Ejemplo n.º 13
0
  def test_open(self):
    """Tests the open function."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    smdev_handle.open(unittest.source)

    with self.assertRaises(IOError):
      smdev_handle.open(unittest.source)

    smdev_handle.close()

    with self.assertRaises(TypeError):
      smdev_handle.open(None)

    with self.assertRaises(ValueError):
      smdev_handle.open(unittest.source, mode="w")
Ejemplo n.º 14
0
  def test_open(self):
    """Tests the open function."""
    if not unittest.source:
      return

    smdev_handle = pysmdev.handle()

    smdev_handle.open(unittest.source)

    with self.assertRaises(IOError):
      smdev_handle.open(unittest.source)

    smdev_handle.close()

    with self.assertRaises(TypeError):
      smdev_handle.open(None)

    with self.assertRaises(ValueError):
      smdev_handle.open(unittest.source, mode="w")
Ejemplo n.º 15
0
  def test_read_buffer_file_object(self):
    """Tests the read_buffer function on a file-like object."""
    if not unittest.source:
      return

    file_object = open(unittest.source, "rb")

    smdev_handle = pysmdev.handle()

    smdev_handle.open_file_object(file_object)

    file_size = smdev_handle.get_size()

    # Test normal read.
    data = smdev_handle.read_buffer(size=4096)

    self.assertIsNotNone(data)
    self.assertEqual(len(data), min(file_size, 4096))

    smdev_handle.close()
Ejemplo n.º 16
0
  def test_read_buffer_file_object(self):
    """Tests the read_buffer function on a file-like object."""
    if not unittest.source:
      return

    file_object = open(unittest.source, "rb")

    smdev_handle = pysmdev.handle()

    smdev_handle.open_file_object(file_object)

    file_size = smdev_handle.get_size()

    # Test normal read.
    data = smdev_handle.read_buffer(size=4096)

    self.assertIsNotNone(data)
    self.assertEqual(len(data), min(file_size, 4096))

    smdev_handle.close()
Ejemplo n.º 17
0
  def test_read_buffer(self):
    """Tests the read_buffer function."""
    if not unittest.source:
      raise unittest.SkipTest("missing source")

    smdev_handle = pysmdev.handle()

    smdev_handle.open(unittest.source)

    file_size = smdev_handle.get_size()

    # Test normal read.
    data = smdev_handle.read_buffer(size=4096)

    self.assertIsNotNone(data)
    self.assertEqual(len(data), min(file_size, 4096))

    if file_size < 4096:
      data = smdev_handle.read_buffer()

      self.assertIsNotNone(data)
      self.assertEqual(len(data), file_size)

    # Test read beyond file size.
    if file_size > 16:
      smdev_handle.seek_offset(-16, os.SEEK_END)

      data = smdev_handle.read_buffer(size=4096)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), 16)

    with self.assertRaises(ValueError):
      smdev_handle.read_buffer(size=-1)

    smdev_handle.close()

    # Test the read without open.
    with self.assertRaises(IOError):
      smdev_handle.read_buffer(size=4096)
Ejemplo n.º 18
0
  def test_open_file_object(self):
    """Tests the open_file_object function."""
    if not unittest.source:
      return

    file_object = open(unittest.source, "rb")

    smdev_handle = pysmdev.handle()

    smdev_handle.open_file_object(file_object)

    with self.assertRaises(IOError):
      smdev_handle.open_file_object(file_object)

    smdev_handle.close()

    # TODO: change IOError into TypeError
    with self.assertRaises(IOError):
      smdev_handle.open_file_object(None)

    with self.assertRaises(ValueError):
      smdev_handle.open_file_object(file_object, mode="w")
Ejemplo n.º 19
0
  def test_open_file_object(self):
    """Tests the open_file_object function."""
    if not unittest.source:
      return

    file_object = open(unittest.source, "rb")

    smdev_handle = pysmdev.handle()

    smdev_handle.open_file_object(file_object)

    with self.assertRaises(IOError):
      smdev_handle.open_file_object(file_object)

    smdev_handle.close()

    # TODO: change IOError into TypeError
    with self.assertRaises(IOError):
      smdev_handle.open_file_object(None)

    with self.assertRaises(ValueError):
      smdev_handle.open_file_object(file_object, mode="w")
Ejemplo n.º 20
0
  def test_signal_abort(self):
    """Tests the signal_abort function."""
    smdev_handle = pysmdev.handle()

    smdev_handle.signal_abort()
Ejemplo n.º 21
0
    def _Open(self, path_spec=None, mode='rb'):
        """Opens the file-like object defined by path specification.

    Args:
      path_spec: optional path specification (instance of path.PathSpec).
                 The default is None.
      mode: optional file access mode. The default is 'rb' read-only binary.

    Raises:
      AccessError: if the access to open the file was denied.
      IOError: if the file-like object could not be opened.
      PathSpecError: if the path specification is incorrect.
      ValueError: if the path specification is invalid.
    """
        if not path_spec:
            raise ValueError(u'Missing path specfication.')

        if path_spec.HasParent():
            raise errors.PathSpecError(
                u'Unsupported path specification with parent.')

        location = getattr(path_spec, u'location', None)

        if location is None:
            raise errors.PathSpecError(u'Path specification missing location.')

        # Windows does not support running os.stat on device files so we use
        # libsmdev to do an initial check.
        try:
            is_device = pysmdev.check_device(location)
        except IOError as exception:
            # Since os.stat() will not recognize Windows device file names and
            # will return '[Error 87] The parameter is incorrect' we check here
            # if pysmdev exception message contains ' access denied ' and raise
            # AccessError instead.

            # Note that exception.message no longer works in Python 3.
            exception_string = str(exception)
            if not isinstance(exception_string, py2to3.UNICODE_TYPE):
                exception_string = py2to3.UNICODE_TYPE(exception_string,
                                                       errors=u'replace')

            if u' access denied ' in exception_string:
                raise errors.AccessError(
                    u'Access denied to file: {0:s} with error: {1:s}'.format(
                        location, exception_string))
            is_device = False

        if not is_device:
            try:
                stat_info = os.stat(location)
            except OSError as exception:
                raise IOError(u'Unable to open file with error: {0:s}.'.format(
                    exception))

            # In case the libsmdev check is not able to detect the device also use
            # the stat information.
            if stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(
                    stat_info.st_mode):
                is_device = True

        if is_device:
            self._file_object = pysmdev.handle()
            self._file_object.open(location, mode=mode)
            self._size = self._file_object.media_size

        else:
            self._file_object = open(location, mode=mode)
            self._size = stat_info.st_size
Ejemplo n.º 22
0
  def _Open(self, path_spec=None, mode='rb'):
    """Opens the file-like object defined by path specification.

    Args:
      path_spec (PathSpec): path specification.
      mode (Optional[str]): file access mode.

    Raises:
      AccessError: if the access to open the file was denied.
      IOError: if the file-like object could not be opened.
      OSError: if the file-like object could not be opened.
      PathSpecError: if the path specification is incorrect.
      ValueError: if the path specification is invalid.
    """
    if not path_spec:
      raise ValueError('Missing path specification.')

    if path_spec.HasParent():
      raise errors.PathSpecError('Unsupported path specification with parent.')

    location = getattr(path_spec, 'location', None)

    if location is None:
      raise errors.PathSpecError('Path specification missing location.')

    # Windows does not support running os.stat on device files so we use
    # libsmdev to do an initial check.
    try:
      is_device = pysmdev.check_device(location)
    except IOError as exception:
      # Since os.stat() will not recognize Windows device file names and
      # will return '[Error 87] The parameter is incorrect' we check here
      # if pysmdev exception message contains ' access denied ' and raise
      # AccessError instead.

      # Note that exception.message no longer works in Python 3.
      exception_string = str(exception)
      if not isinstance(exception_string, py2to3.UNICODE_TYPE):
        exception_string = py2to3.UNICODE_TYPE(
            exception_string, errors='replace')

      if ' access denied ' in exception_string:
        raise errors.AccessError(
            'Access denied to file: {0:s} with error: {1!s}'.format(
                location, exception_string))
      is_device = False

    if not is_device:
      try:
        stat_info = os.stat(location)
      except OSError as exception:
        raise IOError('Unable to open file with error: {0!s}.'.format(
            exception))

      # In case the libsmdev check is not able to detect the device also use
      # the stat information.
      if stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode):
        is_device = True

    if is_device:
      self._file_object = pysmdev.handle()
      self._file_object.open(location, mode=mode)
      self._size = self._file_object.media_size

    else:
      self._file_object = open(location, mode=mode)
      self._size = stat_info.st_size
Ejemplo n.º 23
0
  def test_signal_abort(self):
    """Tests the signal_abort function."""
    smdev_handle = pysmdev.handle()

    smdev_handle.signal_abort()