def DirHierarchy(filepaths): """A context manager that setups a fake directory hierarchy. Args: filepaths: A list of paths that should exist in the hierarchy. Yields: A context within which the fake hierarchy exists. """ with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: hierarchy = DirHierarchyContext(temp_dirpath) for filepath in map(hierarchy, filepaths): dirpath = os.path.dirname(filepath) try: if not os.path.exists(dirpath): os.makedirs(dirpath) with io.open(filepath, "a"): pass except UnicodeEncodeError: raise unittest.SkipTest( "Unicode not supported by the filesystem") try: yield hierarchy finally: # TODO: Required to clean-up the temp directory. files.FlushHandleCache()
def testListRootDirectory(self): with mock.patch.object(client_utils, "GetRawDevice", new=self._MockGetRawDevice): results = _RunFileFinder( rdf_file_finder.FileFinderArgs( paths=[self._paths_expr], pathtype=self.pathtype, action=rdf_file_finder.FileFinderAction.Stat())) names = [ result.stat_entry.pathspec.nested_path.path for result in results ] self.assertIn("/numbers.txt", names) files.FlushHandleCache()
def testProgressCallback(self): with temp.AutoTempFilePath() as temppath: self._Touch(temppath, b"QUUX") pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=temppath) func = mock.MagicMock() with vfs.VFSMultiOpen([pathspec], progress_callback=func) as filedescs: self.assertLen(filedescs, 1) self.assertEqual(filedescs[0].Read(), b"QUUX") self.assertTrue(func.called) files.FlushHandleCache()
def testRecursiveRegexMatch(self) -> None: with temp.AutoTempDirPath(remove_non_empty=True) as temp_dir: nested_dir = os.path.join(temp_dir, "a", "b", "c") os.makedirs(nested_dir) with open(os.path.join(nested_dir, "foo.txt"), "w") as f: f.write("bar123") results = _RunFileFinder( rdf_file_finder.FileFinderArgs( paths=[os.path.join(temp_dir, "**", "*")], pathtype=rdf_paths.PathSpec.PathType.OS, conditions=[ rdf_file_finder.FileFinderCondition.ContentsRegexMatch( regex=b"bar[0-9]+"), ], action=rdf_file_finder.FileFinderAction.Stat())) self.assertLen(results, 1) self.assertEqual(results[0].matches[0].data, b"bar123") files.FlushHandleCache()
def testFollowSymlinkEnabled(self): data = b"quux" * 1024 with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: target_filepath = os.path.join(temp_dirpath, "target") symlink_filepath = os.path.join(temp_dirpath, "symlink") filesystem_test_lib.CreateFile(target_filepath, data) os.symlink(target_filepath, symlink_filepath) request = rdf_client_action.GetFileStatRequest() request.pathspec = rdf_paths.PathSpec.OS(path=symlink_filepath) request.follow_symlink = True results = self.RunAction(standard.GetFileStat, request) self.assertLen(results, 1) self.assertFalse(stat.S_ISLNK(int(results[0].st_mode))) self.assertEqual(results[0].st_size, len(data)) # TODO: Required to clean-up the temp directory. files.FlushHandleCache()
def testImplementationType(self) -> None: orig_vfs_open = vfs.VFSOpen def MockVfsOpen(pathspec, *args, **kwargs): self.assertEqual(pathspec.implementation_type, rdf_paths.PathSpec.ImplementationType.DIRECT) return orig_vfs_open(pathspec, *args, **kwargs) with contextlib.ExitStack() as stack: stack.enter_context( mock.patch.object(vfs, "VFSOpen", new=MockVfsOpen)) stack.enter_context( mock.patch.object(client_utils, "GetRawDevice", new=self._MockGetRawDevice)) _RunFileFinder( rdf_file_finder.FileFinderArgs( paths=[self._paths_expr], pathtype=self.pathtype, implementation_type=rdf_paths.PathSpec.ImplementationType. DIRECT, action=rdf_file_finder.FileFinderAction.Stat())) files.FlushHandleCache()
def testMultipleFiles(self): with temp.AutoTempDirPath(remove_non_empty=True) as tempdir: foo_path = os.path.join(tempdir, "foo") bar_path = os.path.join(tempdir, "bar") baz_path = os.path.join(tempdir, "baz") self._Touch(foo_path, b"FOO") self._Touch(bar_path, b"BAR") self._Touch(baz_path, b"BAZ") foo_pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=foo_path) bar_pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=bar_path) baz_pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=baz_path) pathspecs = [foo_pathspec, bar_pathspec, baz_pathspec] with vfs.VFSMultiOpen(pathspecs) as filedescs: self.assertLen(filedescs, 3) self.assertEqual(filedescs[0].Read(), b"FOO") self.assertEqual(filedescs[1].Read(), b"BAR") self.assertEqual(filedescs[2].Read(), b"BAZ") files.FlushHandleCache()