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

    smraw_handle = pysmraw.handle()

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

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

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

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

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

    # Test open_file_object and close and dereferencing file_object.
    smraw_handle.open_file_object(file_object)
    del file_object
    smraw_handle.close()
Exemplo n.º 2
0
def pysmraw_test_multi_open_close_file_object(filename, mode):
  print(("Testing multi open close of file-like object of: {0:s} "
         "with access: {1:s}\t").format(filename, get_mode_string(mode)))

  result = True
  try:
    filenames = pysmraw.glob(filename)
    file_objects = []
    for filename in filenames:
      file_object = open(filename, "rb")
      file_objects.append(file_object)

    smraw_handle = pysmraw.handle()

    smraw_handle.open_file_object(file_objects, mode)
    smraw_handle.close()
    smraw_handle.open_file_object(file_objects, mode)
    smraw_handle.close()

  except Exception as exception:
    print(str(exception))
    result = False

  if not result:
    print("(FAIL)")
  else:
    print("(PASS)")
  return result
Exemplo n.º 3
0
  def test_read_buffer_at_offset(self):
    """Tests the read_buffer_at_offset function."""
    if not unittest.source:
      return

    smraw_handle = pysmraw.handle()

    smraw_handle.open(unittest.source)

    file_size = smraw_handle.get_size()

    # Test normal read.
    data = smraw_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 = smraw_handle.read_buffer_at_offset(4096, file_size - 16)

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

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

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

    smraw_handle.close()

    # Test the read without open.
    with self.assertRaises(IOError):
      smraw_handle.read_buffer_at_offset(4096, 0)
Exemplo n.º 4
0
def pysmraw_test_single_open_close_file_object(filename, mode):
    """Tests a single file-like object open and close."""
    description = (
        "Testing single open close of file-like object of: {0:s} with access: "
        "{1:s}\t").format(get_filename_string(filename), get_mode_string(mode))
    print(description, end="")

    error_string = None
    result = True

    try:
        file_objects = []
        if filename:
            filenames = pysmraw.glob(filename)
            for filename in filenames:
                file_object = open(filename, "rb")
                file_objects.append(file_object)

        smraw_handle = pysmraw.handle()

        smraw_handle.open_file_objects(file_objects, mode)
        smraw_handle.close()

    except Exception as exception:
        error_string = str(exception)
        result = False

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")

    if error_string:
        print(error_string)
    return result
Exemplo n.º 5
0
def pysmraw_test_multi_open_close_file(filename, mode):
    """Tests multiple open and close."""
    description = ("Testing multi open close of: {0:s} with access: {1:s}"
                   "\t").format(get_filename_string(filename),
                                get_mode_string(mode))
    print(description, end="")

    error_string = None
    result = True

    try:
        smraw_handle = pysmraw.handle()

        filenames = None
        if filename:
            filenames = pysmraw.glob(filename)

        smraw_handle.open(filenames, mode)
        smraw_handle.close()
        smraw_handle.open(filenames, mode)
        smraw_handle.close()

    except Exception as exception:
        error_string = str(exception)
        result = False

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")

    if error_string:
        print(error_string)
    return result
Exemplo n.º 6
0
    def test_read_buffer_at_offset(self):
        """Tests the read_buffer_at_offset function."""
        if not unittest.source:
            return

        smraw_handle = pysmraw.handle()

        smraw_handle.open(unittest.source)

        file_size = smraw_handle.get_size()

        # Test normal read.
        data = smraw_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 = smraw_handle.read_buffer_at_offset(4096, file_size - 16)

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

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

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

        smraw_handle.close()

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

        smraw_handle = pysmraw.handle()

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

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

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

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

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

        # Test open_file_object and close and dereferencing file_object.
        smraw_handle.open_file_object(file_object)
        del file_object
        smraw_handle.close()
Exemplo n.º 8
0
    def test_seek_offset(self):
        """Tests the seek_offset function."""
        if not unittest.source:
            return

        smraw_handle = pysmraw.handle()

        smraw_handle.open(unittest.source)

        file_size = smraw_handle.get_size()

        smraw_handle.seek_offset(16, os.SEEK_SET)

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

        smraw_handle.seek_offset(16, os.SEEK_CUR)

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

        smraw_handle.seek_offset(-16, os.SEEK_CUR)

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

        smraw_handle.seek_offset(-16, os.SEEK_END)

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

        smraw_handle.seek_offset(16, os.SEEK_END)

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

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

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

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

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

        smraw_handle.close()

        # Test the seek without open.
        with self.assertRaises(IOError):
            smraw_handle.seek_offset(16, os.SEEK_SET)
Exemplo n.º 9
0
def pysmraw_test_seek_file(filename):
    filenames = pysmraw.glob(filename)
    smraw_handle = pysmraw.handle()

    smraw_handle.open(filenames, "r")
    result = pysmraw_test_seek(smraw_handle)
    smraw_handle.close()

    return result
Exemplo n.º 10
0
    def test_close(self):
        """Tests the close function."""
        if not unittest.source:
            return

        smraw_handle = pysmraw.handle()

        with self.assertRaises(IOError):
            smraw_handle.close()
Exemplo n.º 11
0
  def test_close(self):
    """Tests the close function."""
    if not unittest.source:
      return

    smraw_handle = pysmraw.handle()

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

    smraw_handle = pysmraw.handle()

    smraw_handle.open(unittest.source)

    file_size = smraw_handle.get_size()

    smraw_handle.seek_offset(16, os.SEEK_SET)

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

    smraw_handle.seek_offset(16, os.SEEK_CUR)

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

    smraw_handle.seek_offset(-16, os.SEEK_CUR)

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

    smraw_handle.seek_offset(-16, os.SEEK_END)

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

    smraw_handle.seek_offset(16, os.SEEK_END)

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

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

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

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

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

    smraw_handle.close()

    # Test the seek without open.
    with self.assertRaises(IOError):
      smraw_handle.seek_offset(16, os.SEEK_SET)
Exemplo n.º 13
0
def pysmraw_test_read_file(filename):
    """Tests the read function with a file."""
    smraw_handle = pysmraw.handle()

    filenames = pysmraw.glob(filename)
    smraw_handle.open(filenames, "r")
    result = pysmraw_test_read(smraw_handle)
    smraw_handle.close()

    return result
Exemplo n.º 14
0
def pysmraw_test_single_open_close_file(filename, mode):
    """Tests a single open and close."""
    description = ("Testing single open close of: {0:s} with access: {1:s}"
                   "\t").format(get_filename_string(filename),
                                get_mode_string(mode))
    print(description, end="")

    error_string = None
    result = True

    try:
        smraw_handle = pysmraw.handle()

        filenames = None
        if filename:
            filenames = pysmraw.glob(filename)

        smraw_handle.open(filenames, mode)
        smraw_handle.close()

    except TypeError as exception:
        expected_message = ("{0:s}: argument: files must be a sequence object."
                            ).format("pysmraw_handle_open")

        if not filename and str(exception) == expected_message:
            pass

        else:
            error_string = str(exception)
            result = False

    except ValueError as exception:
        expected_message = (
            "{0:s}: unsupported mode: w.").format("pysmraw_handle_open")

        if mode != "w" or str(exception) != expected_message:
            error_string = str(exception)
            result = False

    except Exception as exception:
        error_string = str(exception)
        result = False

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")

    if error_string:
        print(error_string)
    return result
Exemplo n.º 15
0
def pysmraw_test_seek_file_object(filename):
    filenames = pysmraw.glob(filename)
    file_objects = []
    for filename in filenames:
        file_object = open(filename, "rb")
        file_objects.append(file_object)

    smraw_handle = pysmraw.handle()

    smraw_handle.open_file_object(file_objects, "r")
    result = pysmraw_test_seek(smraw_handle)
    smraw_handle.close()

    return result
Exemplo n.º 16
0
def pysmraw_test_read_file_object(filename):
    """Tests the read function with a file-like object."""
    smraw_handle = pysmraw.handle()

    filenames = pysmraw.glob(filename)
    file_objects = []
    for filename in filenames:
        file_object = open(filename, "rb")
        file_objects.append(file_object)

    smraw_handle.open_file_objects(file_objects, "r")
    result = pysmraw_test_read(smraw_handle)
    smraw_handle.close()

    return result
Exemplo n.º 17
0
def pysmraw_test_single_open_close_file(filename, mode):
  if not filename:
    filename_string = "None"
  else:
    filename_string = filename

  print("Testing single open close of: {0:s} with access: {1:s}\t".format(
      filename_string, get_mode_string(mode)))

  result = True
  try:
    filenames = pysmraw.glob(filename)
    smraw_handle = pysmraw.handle()

    smraw_handle.open(filenames, mode)
    smraw_handle.close()

  except TypeError as exception:
    expected_message = (
        "{0:s}: unsupported string object type.").format(
            "pysmraw_handle_open")

    if not filename and str(exception) == expected_message:
      pass

    else:
      print(str(exception))
      result = False

  except ValueError as exception:
    expected_message = (
        "{0:s}: unsupported mode: w.").format(
            "pysmraw_handle_open")

    if mode != "w" or str(exception) != expected_message:
      print(str(exception))
      result = False

  except Exception as exception:
    print(str(exception))
    result = False

  if not result:
    print("(FAIL)")
  else:
    print("(PASS)")
  return result
Exemplo n.º 18
0
def pysmraw_test_seek_file_no_open(filename):
    print("Testing seek of offset without open:\n")

    smraw_handle = pysmraw.handle()

    result = False
    try:
        smraw_handle.seek(0, os.SEEK_SET)
    except Exception as exception:
        print(str(exception))
        result = True

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")
    return result
Exemplo n.º 19
0
    def test_open(self):
        """Tests the open function."""
        if not unittest.source:
            return

        smraw_handle = pysmraw.handle()

        smraw_handle.open(unittest.source)

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

        smraw_handle.close()

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

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

    smraw_handle = pysmraw.handle()

    smraw_handle.open(unittest.source)

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

    smraw_handle.close()

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

    with self.assertRaises(ValueError):
      smraw_handle.open(unittest.source, mode="w")
Exemplo n.º 21
0
    def _OpenFileObject(self, path_spec):
        """Opens the file-like object defined by path specification.

    Args:
      path_spec: the path specification (instance of path.PathSpec).

    Returns:
      A file-like object or None.

    Raises:
      PathSpecError: if the path specification is invalid.
    """
        if not path_spec.HasParent():
            raise errors.PathSpecError(
                u'Unsupported path specification without parent.')

        parent_path_spec = path_spec.parent

        file_system = resolver.Resolver.OpenFileSystem(
            parent_path_spec, resolver_context=self._resolver_context)

        # Note that we cannot use pysmraw's glob function since it does not
        # handle the file system abstraction dfvfs provides.
        segment_file_path_specs = raw.RawGlobPathSpec(file_system, path_spec)
        if not segment_file_path_specs:
            return

        if parent_path_spec.IsSystemLevel():
            # Typically the file-like object cache should have room for 127 items.
            self._resolver_context.SetMaximumNumberOfFileObjects(
                len(segment_file_path_specs) + 127)

        file_objects = []
        for segment_file_path_spec in segment_file_path_specs:
            file_object = resolver.Resolver.OpenFileObject(
                segment_file_path_spec,
                resolver_context=self._resolver_context)
            file_objects.append(file_object)

        raw_handle = pysmraw.handle()
        raw_handle.open_file_objects(file_objects)
        return raw_handle
Exemplo n.º 22
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")

        smraw_handle = pysmraw.handle()

        smraw_handle.open_file_object(file_object)

        file_size = smraw_handle.get_size()

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

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

        smraw_handle.close()
Exemplo n.º 23
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")

    smraw_handle = pysmraw.handle()

    smraw_handle.open_file_object(file_object)

    file_size = smraw_handle.get_size()

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

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

    smraw_handle.close()
Exemplo n.º 24
0
  def _OpenFileObject(self, path_spec):
    """Opens the file-like object defined by path specification.

    Args:
      path_spec (PathSpec): path specification.

    Returns:
      pysmraw.handle: a file-like object or None.

    Raises:
      PathSpecError: if the path specification is invalid.
    """
    if not path_spec.HasParent():
      raise errors.PathSpecError(
          'Unsupported path specification without parent.')

    parent_path_spec = path_spec.parent

    file_system = resolver.Resolver.OpenFileSystem(
        parent_path_spec, resolver_context=self._resolver_context)

    # Note that we cannot use pysmraw's glob function since it does not
    # handle the file system abstraction dfvfs provides.
    segment_file_path_specs = raw.RawGlobPathSpec(file_system, path_spec)
    if not segment_file_path_specs:
      return None

    if parent_path_spec.IsSystemLevel():
      # Typically the file-like object cache should have room for 127 items.
      self._resolver_context.SetMaximumNumberOfFileObjects(
          len(segment_file_path_specs) + 127)

    file_objects = []
    for segment_file_path_spec in segment_file_path_specs:
      file_object = resolver.Resolver.OpenFileObject(
          segment_file_path_spec, resolver_context=self._resolver_context)
      file_objects.append(file_object)

    raw_handle = pysmraw.handle()
    raw_handle.open_file_objects(file_objects)
    return raw_handle
Exemplo n.º 25
0
    def test_open_file_object(self):
        """Tests the open_file_object function."""
        if not unittest.source:
            return

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

        smraw_handle = pysmraw.handle()

        smraw_handle.open_file_object(file_object)

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

        smraw_handle.close()

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

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

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

    smraw_handle = pysmraw.handle()

    smraw_handle.open_file_object(file_object)

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

    smraw_handle.close()

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

    with self.assertRaises(ValueError):
      smraw_handle.open_file_object(file_object, mode="w")
Exemplo n.º 27
0
def pysmraw_test_read_file_no_open(filename):
    """Tests the read function with a file without open."""
    description = "Testing read of without open:\t"
    print(description, end="")

    smraw_handle = pysmraw.handle()

    error_string = None
    result = False
    try:
        smraw_handle.read(size=4096)
    except Exception as exception:
        error_string = str(exception)
        result = True

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")

    if error_string:
        print(error_string)
    return result
Exemplo n.º 28
0
def pysmraw_test_multi_open_close_file(filename, mode):
  print("Testing multi open close of: {0:s} with access: {1:s}\t".format(
      filename, get_mode_string(mode)))

  result = True
  try:
    filenames = pysmraw.glob(filename)
    smraw_handle = pysmraw.handle()

    smraw_handle.open(filenames, mode)
    smraw_handle.close()
    smraw_handle.open(filenames, mode)
    smraw_handle.close()

  except Exception as exception:
    print(str(exception))
    result = False

  if not result:
    print("(FAIL)")
  else:
    print("(PASS)")
  return result
Exemplo n.º 29
0
def pysmraw_test_seek_file_no_open(filename):
    """Tests the seek function with a file without open."""
    description = "Testing seek of offset without open:\t"
    print(description, end="")

    smraw_handle = pysmraw.handle()

    error_string = None
    result = False
    try:
        smraw_handle.seek(0, os.SEEK_SET)
    except Exception as exception:
        error_string = str(exception)
        result = True

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")

    if error_string:
        print(error_string)
    return result
Exemplo n.º 30
0
    def test_signal_abort(self):
        """Tests the signal_abort function."""
        smraw_handle = pysmraw.handle()

        smraw_handle.signal_abort()
Exemplo n.º 31
0
  def test_signal_abort(self):
    """Tests the signal_abort function."""
    smraw_handle = pysmraw.handle()

    smraw_handle.signal_abort()