Ejemplo n.º 1
0
    def testSubFileEntries(self):
        """Test the sub_file_entries property."""
        path_spec = zip_path_spec.ZipPathSpec(location='/',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
        self.assertEqual(file_entry.number_of_sub_file_entries, 2)

        self._assertSubFileEntries(file_entry, ['syslog', 'wtmp.1'])

        # Test on a zip file that has missing directory entries.
        test_file = self._GetTestFilePath(['missing_directory_entries.zip'])
        self._SkipIfPathNotExists(test_file)

        path_spec = os_path_spec.OSPathSpec(location=test_file)
        path_spec = zip_path_spec.ZipPathSpec(location='/', parent=path_spec)

        file_system = zip_file_system.ZipFileSystem(self._resolver_context)
        self.assertIsNotNone(file_system)
        file_system.Open(path_spec)

        file_entry = file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self._assertSubFileEntries(file_entry, ['folder'])

        # The "folder" folder is a missing directory entry but should still
        # be found due to the files found inside the directory.
        sub_file_entry = next(file_entry.sub_file_entries)
        self.assertTrue(sub_file_entry.IsVirtual())
        self._assertSubFileEntries(sub_file_entry, ['syslog', 'wtmp.1'])

        file_system.Close()
Ejemplo n.º 2
0
    def testIsFunctions(self):
        """Test the Is? functionality."""
        path_spec = zip_path_spec.ZipPathSpec(location=u'/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertFalse(file_entry.IsRoot())
        self.assertFalse(file_entry.IsVirtual())
        self.assertTrue(file_entry.IsAllocated())

        self.assertFalse(file_entry.IsDevice())
        self.assertFalse(file_entry.IsDirectory())
        self.assertTrue(file_entry.IsFile())
        self.assertFalse(file_entry.IsLink())
        self.assertFalse(file_entry.IsPipe())
        self.assertFalse(file_entry.IsSocket())

        path_spec = zip_path_spec.ZipPathSpec(location=u'/',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertTrue(file_entry.IsRoot())
        self.assertTrue(file_entry.IsVirtual())
        self.assertTrue(file_entry.IsAllocated())

        self.assertFalse(file_entry.IsDevice())
        self.assertTrue(file_entry.IsDirectory())
        self.assertFalse(file_entry.IsFile())
        self.assertFalse(file_entry.IsLink())
        self.assertFalse(file_entry.IsPipe())
        self.assertFalse(file_entry.IsSocket())
Ejemplo n.º 3
0
    def testDataStreams(self):
        """Test the data_streams property."""
        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertEqual(file_entry.number_of_data_streams, 1)

        data_stream_names = []
        for data_stream in file_entry.data_streams:
            data_stream_names.append(data_stream.name)

        self.assertEqual(data_stream_names, [''])

        path_spec = zip_path_spec.ZipPathSpec(location='/',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertEqual(file_entry.number_of_data_streams, 0)

        data_stream_names = []
        for data_stream in file_entry.data_streams:
            data_stream_names.append(data_stream.name)

        self.assertEqual(data_stream_names, [])
Ejemplo n.º 4
0
    def testIsVirtual(self):
        """Test the IsVirtual function."""
        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
        self.assertFalse(file_entry.IsVirtual())

        path_spec = zip_path_spec.ZipPathSpec(location='/',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
        self.assertTrue(file_entry.IsVirtual())
Ejemplo n.º 5
0
    def testGetFileEntryByPathSpec(self):
        """Tests the GetFileEntryByPathSpec function."""
        file_system = zip_file_system.ZipFileSystem(self._resolver_context)
        self.assertIsNotNone(file_system)

        file_system.Open(self._zip_path_spec)

        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
        self.assertEqual(file_entry.name, 'syslog')

        path_spec = zip_path_spec.ZipPathSpec(location='/bogus',
                                              parent=self._os_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNone(file_entry)

        file_system.Close()

        # Test on a tar file that has missing directory entries.
        test_file = self._GetTestFilePath(['missing_directory_entries.zip'])
        self._SkipIfPathNotExists(test_file)

        test_file_path_spec = os_path_spec.OSPathSpec(location=test_file)
        path_spec = zip_path_spec.ZipPathSpec(location='/',
                                              parent=test_file_path_spec)

        file_system = zip_file_system.ZipFileSystem(self._resolver_context)
        self.assertIsNotNone(file_system)
        file_system.Open(path_spec)

        path_spec = zip_path_spec.ZipPathSpec(location='/folder',
                                              parent=test_file_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)
        self.assertTrue(file_entry.IsVirtual())
        self.assertEqual(file_entry.name, 'folder')

        path_spec = zip_path_spec.ZipPathSpec(location='/folder/syslog',
                                              parent=test_file_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)
        self.assertEqual(file_entry.name, 'syslog')

        file_system.Close()
Ejemplo n.º 6
0
    def _EntriesGenerator(self):
        """Retrieves directory entries.

       Since a directory can contain a vast number of entries using
       a generator is more memory efficient.

    Yields:
      A path specification (instance of path.ZipPathSpec).
    """
        location = getattr(self.path_spec, u'location', None)

        if (location is None
                or not location.startswith(self._file_system.PATH_SEPARATOR)):
            return

        zip_file = self._file_system.GetZipFile()
        for zip_info in zip_file.infolist():
            path = zip_info.filename

            if not path or not path.startswith(location[1:]):
                continue

            _, suffix = self._file_system.GetPathSegmentAndSuffix(
                location[1:], path)

            # Ignore anything that is part of a sub directory or the directory itself.
            if suffix or path == location:
                continue

            path_spec_location = self._file_system.JoinPath([path])
            yield zip_path_spec.ZipPathSpec(location=path_spec_location,
                                            parent=self.path_spec.parent)
Ejemplo n.º 7
0
    def testFileEntryExistsByPathSpec(self):
        """Test the file entry exists by path specification functionality."""
        file_system = zip_file_system.ZipFileSystem(self._resolver_context)
        self.assertNotEqual(file_system, None)

        file_system.Open(path_spec=self._zip_path_spec)

        path_spec = zip_path_spec.ZipPathSpec(location=u'/syslog',
                                              parent=self._os_path_spec)
        self.assertTrue(file_system.FileEntryExistsByPathSpec(path_spec))

        path_spec = zip_path_spec.ZipPathSpec(location=u'/bogus',
                                              parent=self._os_path_spec)
        self.assertFalse(file_system.FileEntryExistsByPathSpec(path_spec))

        file_system.Close()
Ejemplo n.º 8
0
 def setUp(self):
     """Sets up the needed objects used throughout the test."""
     self._resolver_context = context.Context()
     test_file = os.path.join(u'test_data', u'syslog.zip')
     self._os_path_spec = os_path_spec.OSPathSpec(location=test_file)
     self._zip_path_spec = zip_path_spec.ZipPathSpec(
         location=u'/', parent=self._os_path_spec)
Ejemplo n.º 9
0
 def setUp(self):
     """Sets up the needed objects used throughout the test."""
     self._resolver_context = context.Context()
     test_file = self._GetTestFilePath(['syslog.zip'])
     self._os_path_spec = os_path_spec.OSPathSpec(location=test_file)
     self._zip_path_spec = zip_path_spec.ZipPathSpec(
         location='/', parent=self._os_path_spec)
Ejemplo n.º 10
0
    def _EntriesGenerator(self):
        """Retrieves directory entries.

    Since a directory can contain a vast number of entries using
    a generator is more memory efficient.

    Yields:
      ZipPathSpec: a path specification.
    """
        location = getattr(self.path_spec, 'location', None)

        if location and location.startswith(self._file_system.PATH_SEPARATOR):
            # The zip_info filename does not have the leading path separator
            # as the location string does.
            zip_path = location[1:]

            # Set of top level sub directories that have been yielded.
            processed_directories = set()

            zip_file = self._file_system.GetZipFile()
            for zip_info in zip_file.infolist():
                path = getattr(zip_info, 'filename', None)
                if path is not None and not isinstance(path,
                                                       py2to3.UNICODE_TYPE):
                    try:
                        path = path.decode(self._file_system.encoding)
                    except UnicodeDecodeError:
                        path = None

                if not path or not path.startswith(zip_path):
                    continue

                # Ignore the directory itself.
                if path == zip_path:
                    continue

                path_segment, suffix = self._file_system.GetPathSegmentAndSuffix(
                    zip_path, path)
                if not path_segment:
                    continue

                # Some times the ZIP file lacks directories, therefore we will
                # provide virtual ones.
                if suffix:
                    path_spec_location = self._file_system.JoinPath(
                        [location, path_segment])
                    is_directory = True
                else:
                    path_spec_location = self._file_system.JoinPath([path])
                    is_directory = path.endswith('/')

                if is_directory:
                    if path_spec_location in processed_directories:
                        continue
                    processed_directories.add(path_spec_location)
                    # Restore / at end path to indicate a directory.
                    path_spec_location += self._file_system.PATH_SEPARATOR

                yield zip_path_spec.ZipPathSpec(location=path_spec_location,
                                                parent=self.path_spec.parent)
Ejemplo n.º 11
0
  def testInitialize(self):
    """Tests the path specification initialization."""
    path_spec = zip_path_spec.ZipPathSpec(
        location='/test', parent=self._path_spec)

    self.assertIsNotNone(path_spec)

    with self.assertRaises(ValueError):
      zip_path_spec.ZipPathSpec(location='/test', parent=None)

    with self.assertRaises(ValueError):
      zip_path_spec.ZipPathSpec(location=None, parent=self._path_spec)

    with self.assertRaises(ValueError):
      zip_path_spec.ZipPathSpec(
          location='/test', parent=self._path_spec, bogus='BOGUS')
Ejemplo n.º 12
0
  def GetParentFileEntry(self):
    """Retrieves the parent file entry.

    Returns:
      ZipFileEntry: parent file entry or None if not available.
    """
    location = getattr(self.path_spec, 'location', None)
    if location is None:
      return None

    parent_location = self._file_system.DirnamePath(location)
    if parent_location is None:
      return None

    parent_path_spec = getattr(self.path_spec, 'parent', None)

    if parent_location == '':
      parent_location = self._file_system.PATH_SEPARATOR
      is_root = True
      is_virtual = True
    else:
      is_root = False
      is_virtual = False

    path_spec = zip_path_spec.ZipPathSpec(
        location=parent_location, parent=parent_path_spec)
    return ZipFileEntry(
        self._resolver_context, self._file_system, path_spec, is_root=is_root,
        is_virtual=is_virtual)
Ejemplo n.º 13
0
    def testModificationTime(self):
        """Test the modification_time property."""
        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry.modification_time)
Ejemplo n.º 14
0
    def testSize(self):
        """Test the size property."""
        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
        self.assertEqual(file_entry.size, 1247)
Ejemplo n.º 15
0
  def GetRootFileEntry(self):
    """Retrieves the root file entry.

    Returns:
      ZipFileEntry: a file entry or None.
    """
    path_spec = zip_path_spec.ZipPathSpec(
        location=self.LOCATION_ROOT, parent=self._path_spec.parent)
    return self.GetFileEntryByPathSpec(path_spec)
Ejemplo n.º 16
0
    def GetRootFileEntry(self):
        """Retrieves the root file entry.

    Returns:
      A file entry (instance of vfs.FileEntry).
    """
        path_spec = zip_path_spec.ZipPathSpec(location=self.LOCATION_ROOT,
                                              parent=self._path_spec.parent)
        return self.GetFileEntryByPathSpec(path_spec)
Ejemplo n.º 17
0
    def testGetStat(self):
        """Test the get stat functionality."""
        path_spec = zip_path_spec.ZipPathSpec(location=u'/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        stat_object = file_entry.GetStat()

        self.assertNotEqual(stat_object, None)
        self.assertEqual(stat_object.type, stat_object.TYPE_FILE)
Ejemplo n.º 18
0
    def setUp(self):
        """Sets up the needed objects used throughout the test."""
        super(ZipFileTest, self).setUp()
        self._resolver_context = context.Context()
        test_file = self._GetTestFilePath(['syslog.zip'])
        self._SkipIfPathNotExists(test_file)

        path_spec = os_path_spec.OSPathSpec(location=test_file)
        self._zip_path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                                        parent=path_spec)
Ejemplo n.º 19
0
    def testComparable(self):
        """Tests the path specification comparable property."""
        path_spec = zip_path_spec.ZipPathSpec(location='/test',
                                              parent=self._path_spec)

        self.assertIsNotNone(path_spec)

        expected_comparable = '\n'.join(
            ['type: TEST', 'type: ZIP, location: /test', ''])

        self.assertEqual(path_spec.comparable, expected_comparable)
Ejemplo n.º 20
0
    def testGetFileEntryByPathSpec(self):
        """Test the get entry by path specification functionality."""
        file_system = zip_file_system.ZipFileSystem(self._resolver_context)
        self.assertNotEqual(file_system, None)

        file_system.Open(path_spec=self._zip_path_spec)

        path_spec = zip_path_spec.ZipPathSpec(location=u'/syslog',
                                              parent=self._os_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)

        self.assertNotEqual(file_entry, None)
        self.assertEqual(file_entry.name, u'syslog')

        path_spec = zip_path_spec.ZipPathSpec(location=u'/bogus',
                                              parent=self._os_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)

        self.assertEqual(file_entry, None)

        file_system.Close()
Ejemplo n.º 21
0
    def testGetFileEntryByPathSpec(self):
        """Tests the GetFileEntryByPathSpec function."""
        file_system = zip_file_system.ZipFileSystem(self._resolver_context)
        self.assertIsNotNone(file_system)

        file_system.Open(self._zip_path_spec)

        path_spec = zip_path_spec.ZipPathSpec(location=u'/syslog',
                                              parent=self._os_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
        self.assertEqual(file_entry.name, u'syslog')

        path_spec = zip_path_spec.ZipPathSpec(location=u'/bogus',
                                              parent=self._os_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNone(file_entry)

        file_system.Close()
Ejemplo n.º 22
0
    def testGetParentFileEntry(self):
        """Tests the GetParentFileEntry function."""
        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        parent_file_entry = file_entry.GetParentFileEntry()

        self.assertIsNotNone(parent_file_entry)

        self.assertEqual(parent_file_entry.name, '')
Ejemplo n.º 23
0
    def testGetParentFileEntry(self):
        """Test the get parent file entry functionality."""
        path_spec = zip_path_spec.ZipPathSpec(location=u'/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertNotEqual(file_entry, None)

        parent_file_entry = file_entry.GetParentFileEntry()

        self.assertNotEqual(parent_file_entry, None)

        self.assertEqual(parent_file_entry.name, u'')
Ejemplo n.º 24
0
    def setUp(self):
        """Sets up the needed objects used throughout the test."""
        self._resolver_context = context.Context()
        test_file = self._GetTestFilePath(['syslog.zip'])
        self._SkipIfPathNotExists(test_file)

        self._os_path_spec = os_path_spec.OSPathSpec(location=test_file)
        self._zip_path_spec = zip_path_spec.ZipPathSpec(
            location='/', parent=self._os_path_spec)

        self._file_system = zip_file_system.ZipFileSystem(
            self._resolver_context)
        self._file_system.Open(self._zip_path_spec)
Ejemplo n.º 25
0
    def testFileEntryExistsByPathSpec(self):
        """Test the file entry exists by path specification functionality."""
        file_system = zip_file_system.ZipFileSystem(self._resolver_context)
        self.assertIsNotNone(file_system)

        file_system.Open(self._zip_path_spec)

        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        self.assertTrue(file_system.FileEntryExistsByPathSpec(path_spec))

        path_spec = zip_path_spec.ZipPathSpec(location='/bogus',
                                              parent=self._os_path_spec)
        self.assertFalse(file_system.FileEntryExistsByPathSpec(path_spec))

        file_system.Close()

        # Test on a zip file that has missing directory entries.
        test_file = self._GetTestFilePath(['missing_directory_entries.zip'])
        self._SkipIfPathNotExists(test_file)

        test_file_path_spec = os_path_spec.OSPathSpec(location=test_file)
        path_spec = zip_path_spec.ZipPathSpec(location='/',
                                              parent=test_file_path_spec)

        file_system = zip_file_system.ZipFileSystem(self._resolver_context)
        self.assertIsNotNone(file_system)
        file_system.Open(path_spec)

        path_spec = zip_path_spec.ZipPathSpec(location='/folder',
                                              parent=test_file_path_spec)
        self.assertTrue(file_system.FileEntryExistsByPathSpec(path_spec))

        path_spec = zip_path_spec.ZipPathSpec(location='/folder/syslog',
                                              parent=test_file_path_spec)
        self.assertTrue(file_system.FileEntryExistsByPathSpec(path_spec))

        file_system.Close()
Ejemplo n.º 26
0
    def testGetDataStream(self):
        """Tests the GetDataStream function."""
        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        data_stream_name = ''
        data_stream = file_entry.GetDataStream(data_stream_name)
        self.assertIsNotNone(data_stream)
        self.assertEqual(data_stream.name, data_stream_name)

        data_stream = file_entry.GetDataStream('bogus')
        self.assertIsNone(data_stream)
Ejemplo n.º 27
0
    def testGetStat(self):
        """Tests the _GetStat function."""
        path_spec = zip_path_spec.ZipPathSpec(location='/syslog',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        stat_object = file_entry._GetStat()

        self.assertIsNotNone(stat_object)
        self.assertEqual(stat_object.type, stat_object.TYPE_FILE)
        self.assertEqual(stat_object.size, 1247)

        self.assertEqual(stat_object.mode, 256)

        self.assertEqual(stat_object.mtime, 1343141124)
Ejemplo n.º 28
0
    def GetParentFileEntry(self):
        """Retrieves the parent file entry."""
        location = getattr(self.path_spec, u'location', None)
        if location is None:
            return

        parent_location = self._file_system.DirnamePath(location)
        if parent_location is None:
            return
        if parent_location == u'':
            parent_location = self._file_system.PATH_SEPARATOR

        parent_path_spec = getattr(self.path_spec, u'parent', None)
        path_spec = zip_path_spec.ZipPathSpec(location=parent_location,
                                              parent=parent_path_spec)
        return ZipFileEntry(self._resolver_context, self._file_system,
                            path_spec)
Ejemplo n.º 29
0
    def testSubFileEntries(self):
        """Test the sub file entries iteration functionality."""
        path_spec = zip_path_spec.ZipPathSpec(location=u'/',
                                              parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertEqual(file_entry.number_of_sub_file_entries, 2)

        expected_sub_file_entry_names = [u'syslog', u'wtmp.1']

        sub_file_entry_names = []
        for sub_file_entry in file_entry.sub_file_entries:
            sub_file_entry_names.append(sub_file_entry.name)

        self.assertEqual(len(sub_file_entry_names),
                         len(expected_sub_file_entry_names))
        self.assertEqual(sorted(sub_file_entry_names),
                         sorted(expected_sub_file_entry_names))
Ejemplo n.º 30
0
    def _EntriesGenerator(self):
        """Retrieves directory entries.

    Since a directory can contain a vast number of entries using
    a generator is more memory efficient.

    Yields:
      A path specification (instance of path.ZipPathSpec).
    """
        location = getattr(self.path_spec, u'location', None)

        if (location is None
                or not location.startswith(self._file_system.PATH_SEPARATOR)):
            return

        zip_file = self._file_system.GetZipFile()
        for zip_info in zip_file.infolist():
            path = getattr(zip_info, u'filename', None)
            if path is not None and not isinstance(path, py2to3.UNICODE_TYPE):
                try:
                    path = path.decode(self._file_system.encoding)
                except UnicodeDecodeError:
                    path = None

            if not path or not path.startswith(location[1:]):
                continue

            _, suffix = self._file_system.GetPathSegmentAndSuffix(
                location[1:], path)

            # Ignore anything that is part of a sub directory or the directory itself.
            if suffix or path == location[1:]:
                continue

            path_spec_location = self._file_system.JoinPath([path])

            # Restore / at end path to indicate a directory.
            if path.endswith(self._file_system.PATH_SEPARATOR):
                path_spec_location += self._file_system.PATH_SEPARATOR

            yield zip_path_spec.ZipPathSpec(location=path_spec_location,
                                            parent=self.path_spec.parent)