Beispiel #1
0
    def testDataStreams(self):
        """Tests the data streams functionality."""
        test_location = '/a_directory/another_file'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=21,
            location=test_location,
            parent=self._apfs_container_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, [''])

        test_location = '/a_directory'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=18,
            location=test_location,
            parent=self._apfs_container_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, [])
Beispiel #2
0
    def testComparable(self):
        """Tests the path specification comparable property."""
        path_spec = apfs_path_spec.APFSPathSpec(location='/test',
                                                parent=self._path_spec)

        self.assertIsNotNone(path_spec)

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

        self.assertEqual(path_spec.comparable, expected_comparable)

        path_spec = apfs_path_spec.APFSPathSpec(identifier=1,
                                                parent=self._path_spec)

        self.assertIsNotNone(path_spec)

        expected_comparable = '\n'.join(
            ['type: TEST', 'type: APFS, identifier: 1', ''])

        self.assertEqual(path_spec.comparable, expected_comparable)

        path_spec = apfs_path_spec.APFSPathSpec(location='/test',
                                                identifier=1,
                                                parent=self._path_spec)

        self.assertIsNotNone(path_spec)

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

        self.assertEqual(path_spec.comparable, expected_comparable)
Beispiel #3
0
    def testIsFunctions(self):
        """Tests the Is? functions."""
        test_location = '/a_directory/another_file'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=21,
            location=test_location,
            parent=self._apfs_container_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        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())

        test_location = '/a_directory'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=18,
            location=test_location,
            parent=self._apfs_container_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertFalse(file_entry.IsRoot())
        self.assertFalse(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())

        path_spec = apfs_path_spec.APFSPathSpec(
            location='/', parent=self._apfs_container_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertTrue(file_entry.IsRoot())
        self.assertFalse(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())
Beispiel #4
0
    def GetLinkedFileEntry(self):
        """Retrieves the linked file entry, e.g. for a symbolic link.

    Returns:
      APFSFileEntry: linked file entry or None if not available.
    """
        link = self._GetLink()
        if not link:
            return None

        # TODO: is there a way to determine the identifier here?
        link_identifier = None

        parent_path_spec = getattr(self.path_spec, 'parent', None)
        path_spec = apfs_path_spec.APFSPathSpec(location=link,
                                                parent=parent_path_spec)

        is_root = bool(
            link == self._file_system.LOCATION_ROOT
            or link_identifier == self._file_system.ROOT_DIRECTORY_IDENTIFIER)

        return APFSFileEntry(self._resolver_context,
                             self._file_system,
                             path_spec,
                             is_root=is_root)
Beispiel #5
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:
      APFSPathSpec: APFS path specification.
    """
        try:
            fsapfs_file_entry = self._file_system.GetAPFSFileEntryByPathSpec(
                self.path_spec)
        except errors.PathSpecError:
            return

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

        for fsapfs_sub_file_entry in fsapfs_file_entry.sub_file_entries:
            directory_entry = fsapfs_sub_file_entry.name

            if location == self._file_system.PATH_SEPARATOR:
                directory_entry = self._file_system.JoinPath([directory_entry])
            else:
                directory_entry = self._file_system.JoinPath(
                    [location, directory_entry])

            yield apfs_path_spec.APFSPathSpec(
                identifier=fsapfs_sub_file_entry.identifier,
                location=directory_entry,
                parent=self.path_spec.parent)
Beispiel #6
0
    def testGetStat(self):
        """Tests the GetStat function."""
        test_location = '/a_directory/another_file'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=21,
            location=test_location,
            parent=self._apfs_container_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, 22)

        self.assertEqual(stat_object.mode, 420)
        self.assertEqual(stat_object.uid, 99)
        self.assertEqual(stat_object.gid, 99)

        self.assertEqual(stat_object.atime, 1539321508)
        self.assertEqual(stat_object.atime_nano, 9478457)

        self.assertEqual(stat_object.ctime, 1539321508)
        self.assertEqual(stat_object.ctime_nano, 9495127)

        self.assertEqual(stat_object.crtime, 1539321508)
        self.assertEqual(stat_object.crtime_nano, 9495127)

        self.assertEqual(stat_object.mtime, 1539321508)
        self.assertEqual(stat_object.mtime_nano, 9478457)
Beispiel #7
0
    def testGetFileEntryByPathSpec(self):
        """Tests the GetFileEntryByPathSpec function."""
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=19, parent=self._apfs_container_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
Beispiel #8
0
    def GetParentFileEntry(self):
        """Retrieves the parent file entry.

    Returns:
      APFSFileEntry: parent file entry or None if not available.
    """
        parent_location = None

        location = getattr(self.path_spec, 'location', None)
        if location is not None:
            parent_location = self._file_system.DirnamePath(location)
            if parent_location == '':
                parent_location = self._file_system.PATH_SEPARATOR

        parent_identifier = self._fsapfs_file_entry.parent_identifier
        if parent_identifier is None:
            return None

        parent_path_spec = getattr(self.path_spec, 'parent', None)
        path_spec = apfs_path_spec.APFSPathSpec(location=parent_location,
                                                identifier=parent_identifier,
                                                parent=parent_path_spec)

        is_root = bool(parent_location == self._file_system.LOCATION_ROOT
                       or parent_identifier
                       == self._file_system.ROOT_DIRECTORY_IDENTIFIER)

        return APFSFileEntry(self._resolver_context,
                             self._file_system,
                             path_spec,
                             is_root=is_root)
Beispiel #9
0
    def testInitialize(self):
        """Tests the path specification initialization."""
        path_spec = apfs_path_spec.APFSPathSpec(location='/test',
                                                parent=self._path_spec)

        self.assertIsNotNone(path_spec)

        path_spec = apfs_path_spec.APFSPathSpec(identifier=1,
                                                parent=self._path_spec)

        self.assertIsNotNone(path_spec)

        path_spec = apfs_path_spec.APFSPathSpec(location='/test',
                                                identifier=1,
                                                parent=self._path_spec)

        self.assertIsNotNone(path_spec)

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

        with self.assertRaises(ValueError):
            apfs_path_spec.APFSPathSpec(location=None, parent=self._path_spec)

        with self.assertRaises(ValueError):
            apfs_path_spec.APFSPathSpec(identifier=None,
                                        parent=self._path_spec)

        with self.assertRaises(ValueError):
            apfs_path_spec.APFSPathSpec(location='/test',
                                        parent=self._path_spec,
                                        bogus='BOGUS')
Beispiel #10
0
  def GetRootFileEntry(self):
    """Retrieves the root file entry.

    Returns:
      APFSFileEntry: file entry.
    """
    path_spec = apfs_path_spec.APFSPathSpec(
        location=self.LOCATION_ROOT, identifier=self.ROOT_DIRECTORY_IDENTIFIER,
        parent=self._path_spec.parent)
    return self.GetFileEntryByPathSpec(path_spec)
Beispiel #11
0
    def testModificationTime(self):
        """Test the modification_time property."""
        test_location = '/a_directory/another_file'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=21,
            location=test_location,
            parent=self._apfs_container_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
        self.assertIsNotNone(file_entry.modification_time)
Beispiel #12
0
    def testGetDataStream(self):
        """Tests the GetDataStream function."""
        test_location = '/a_directory/another_file'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=21,
            location=test_location,
            parent=self._apfs_container_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)
Beispiel #13
0
    def testGetParentFileEntry(self):
        """Tests the GetParentFileEntry function."""
        test_location = '/a_directory/another_file'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=21,
            location=test_location,
            parent=self._apfs_container_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, 'a_directory')
Beispiel #14
0
    def testGetLinkedFileEntry(self):
        """Tests the GetLinkedFileEntry function."""
        test_location = '/a_link'
        path_spec = apfs_path_spec.APFSPathSpec(
            identifier=22,
            location=test_location,
            parent=self._apfs_container_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        linked_file_entry = file_entry.GetLinkedFileEntry()

        self.assertIsNotNone(linked_file_entry)

        self.assertEqual(linked_file_entry.name, 'a_file')
Beispiel #15
0
    def setUp(self):
        """Sets up the needed objects used throughout the test."""
        self._resolver_context = context.Context()
        test_file = self._GetTestFilePath(['apfs.dmg'])
        path_spec = os_path_spec.OSPathSpec(location=test_file)
        path_spec = raw_path_spec.RawPathSpec(parent=path_spec)
        partition_path_spec = tsk_partition_path_spec.TSKPartitionPathSpec(
            location='/p1', parent=path_spec)
        self._apfs_container_path_spec = (
            apfs_container_path_spec.APFSContainerPathSpec(
                location='/apfs1', parent=partition_path_spec))
        self._apfs_path_spec = apfs_path_spec.APFSPathSpec(
            location='/', parent=self._apfs_container_path_spec)

        self._file_system = apfs_file_system.APFSFileSystem(
            self._resolver_context)
        self._file_system.Open(self._apfs_path_spec)
Beispiel #16
0
    def testSubFileEntries(self):
        """Tests the number_of_sub_file_entries and sub_file_entries properties."""
        path_spec = apfs_path_spec.APFSPathSpec(
            location='/', parent=self._apfs_container_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertEqual(file_entry.number_of_sub_file_entries, 4)

        expected_sub_file_entry_names = [
            '.fseventsd', 'a_directory', 'a_link', 'passwords.txt'
        ]

        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))
Beispiel #17
0
    def setUp(self):
        """Sets up the needed objects used throughout the test."""
        self._resolver_context = context.Context()
        test_file = self._GetTestFilePath(['apfs_encrypted.dmg'])
        self._SkipIfPathNotExists(test_file)

        path_spec = os_path_spec.OSPathSpec(location=test_file)
        path_spec = raw_path_spec.RawPathSpec(parent=path_spec)
        partition_path_spec = tsk_partition_path_spec.TSKPartitionPathSpec(
            location='/p1', parent=path_spec)
        self._apfs_container_path_spec = (
            apfs_container_path_spec.APFSContainerPathSpec(
                location='/apfs1', parent=partition_path_spec))
        self._apfs_path_spec = apfs_path_spec.APFSPathSpec(
            location='/', parent=self._apfs_container_path_spec)

        resolver.Resolver.key_chain.SetCredential(
            self._apfs_container_path_spec, 'password', self._APFS_PASSWORD)

        self._file_system = apfs_file_system.APFSFileSystem(
            self._resolver_context)
        self._file_system.Open(self._apfs_path_spec)