def testGetAllFilesErrors(self, warn): with temp.AutoTempDirPath() as foo_dirpath,\ temp.AutoTempDirPath() as bar_dirpath: self.assertTrue(self.sources.AddDir(foo_dirpath)) self.assertTrue(self.sources.AddDir("/baz/quux/norf")) self.assertTrue(self.sources.AddDir(bar_dirpath)) self.assertTrue(self.sources.AddDir("/thud")) self.assertTrue(self.sources.AddDir("/foo/bar")) files = list(self.sources.GetAllFiles()) self.assertFalse(files) self.assertTrue(warn.called) self.assertEqual(warn.call_count, 3)
def testDirectory(self): with temp.AutoTempDirPath() as temp_dirpath: stat = utils.Stat(temp_dirpath, follow_symlink=False) self.assertTrue(stat.IsDirectory()) self.assertFalse(stat.IsRegular()) self.assertFalse(stat.IsSocket()) self.assertFalse(stat.IsSymlink())
def testPathInterpolation(self): self.client_id = self.SetupClient(0) bar = rdf_client.User(username="******") baz = rdf_client.User(username="******") kb = rdf_client.KnowledgeBase(os="foo", domain="norf", users=[bar, baz]) client = aff4.FACTORY.Open(self.client_id, token=self.token, mode="rw") with client: client.Set(client.Schema.KNOWLEDGE_BASE, kb) with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: self._Touch(os.path.join(temp_dirpath, "foo", "bar")) self._Touch(os.path.join(temp_dirpath, "foo", "baz")) self._Touch(os.path.join(temp_dirpath, "foo", "quux")) self._Touch(os.path.join(temp_dirpath, "thud", "norf", "plugh")) self._Touch(os.path.join(temp_dirpath, "thud", "norf", "blargh")) paths = [ os.path.join(temp_dirpath, "%%os%%", "%%users.username%%"), os.path.join(temp_dirpath, "thud", "%%domain%%", "plugh"), ] action = rdf_file_finder.FileFinderAction.Action.STAT results = self._RunCFF(paths, action) paths = [result.stat_entry.pathspec.path for result in results] self.assertItemsEqual(paths, [ os.path.join(temp_dirpath, "foo", "bar"), os.path.join(temp_dirpath, "foo", "baz"), os.path.join(temp_dirpath, "thud", "norf", "plugh") ])
def testPathInterpolation(self): bar = rdf_client.User(username="******") baz = rdf_client.User(username="******") self.client_id = self.SetupClient(0, system="foo", fqdn="norf", users=[bar, baz]) with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: self._Touch(os.path.join(temp_dirpath, "foo", "bar")) self._Touch(os.path.join(temp_dirpath, "foo", "baz")) self._Touch(os.path.join(temp_dirpath, "foo", "quux")) self._Touch(os.path.join(temp_dirpath, "thud", "norf", "plugh")) self._Touch(os.path.join(temp_dirpath, "thud", "norf", "blargh")) paths = [ os.path.join(temp_dirpath, "%%os%%", "%%users.username%%"), os.path.join(temp_dirpath, "thud", "%%fqdn%%", "plugh"), ] action = rdf_file_finder.FileFinderAction.Action.STAT results = self._RunCFF(paths, action) paths = [result.stat_entry.pathspec.path for result in results] self.assertCountEqual(paths, [ os.path.join(temp_dirpath, "foo", "bar"), os.path.join(temp_dirpath, "foo", "baz"), os.path.join(temp_dirpath, "thud", "norf", "plugh") ])
def testExtAttrsCollection(self): with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: foo_filepath = temp.TempFilePath(dir=temp_dirpath) client_test_lib.SetExtAttr(foo_filepath, name="user.quux", value="foo") bar_filepath = temp.TempFilePath(dir=temp_dirpath) client_test_lib.SetExtAttr(bar_filepath, name="user.quux", value="bar") baz_filepath = temp.TempFilePath(dir=temp_dirpath) client_test_lib.SetExtAttr(baz_filepath, name="user.quux", value="baz") request = rdf_client_fs.FindSpec( pathspec=rdf_paths.PathSpec( path=temp_dirpath, pathtype=rdf_paths.PathSpec.PathType.OS), path_glob="*", collect_ext_attrs=True) request.iterator.number = 100 hits = [] for response in self.RunAction(searching.Find, request): if isinstance(response, rdf_client_fs.FindSpec): hits.append(response.hit) self.assertEqual(len(hits), 3) values = [] for hit in hits: self.assertEqual(len(hit.ext_attrs), 1) values.append(hit.ext_attrs[0].value) self.assertItemsEqual(values, ["foo", "bar", "baz"])
def testGetFlagsSymlink(self): with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath,\ temp.AutoTempFilePath() as temp_filepath: temp_linkpath = os.path.join(temp_dirpath, "foo") os.symlink(temp_filepath, temp_linkpath) stat = utils.Stat(temp_linkpath, follow_symlink=False) self.assertTrue(stat.IsSymlink()) self.assertEqual(stat.GetLinuxFlags(), 0) self.assertEqual(stat.GetOsxFlags(), 0)
def testGetFlagsSocket(self): with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: temp_socketpath = os.path.join(temp_dirpath, "foo") sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.bind(temp_socketpath) stat = utils.Stat(temp_socketpath, follow_symlink=False) self.assertTrue(stat.IsSocket()) self.assertEqual(stat.GetLinuxFlags(), 0) self.assertEqual(stat.GetOsxFlags(), 0) finally: sock.close()
def testSocket(self): with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: temp_socketpath = os.path.join(temp_dirpath, "foo") sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.bind(temp_socketpath) stat = utils.Stat(temp_socketpath, follow_symlink=False) self.assertFalse(stat.IsDirectory()) self.assertFalse(stat.IsRegular()) self.assertTrue(stat.IsSocket()) self.assertFalse(stat.IsSymlink()) finally: sock.close()
def testGetAllFiles(self): with temp.AutoTempDirPath(remove_non_empty=True) as tmpdir_path: foo_path = temp.TempFilePath(suffix="foo.yaml") bar_path = temp.TempFilePath(suffix="bar.json") baz_path = temp.TempFilePath(suffix="baz.yaml") quux_path = temp.TempFilePath(dir=tmpdir_path, suffix="quux.yaml") norf_path = temp.TempFilePath(dir=tmpdir_path, suffix="norf.json") thud_path = temp.TempFilePath(dir=tmpdir_path, suffix="thud.xml") self.sources.AddFile(foo_path) self.sources.AddFile(bar_path) self.sources.AddDir(tmpdir_path) files = list(self.sources.GetAllFiles()) self.assertIn(foo_path, files) self.assertIn(bar_path, files) self.assertIn(quux_path, files) self.assertIn(norf_path, files) self.assertNotIn(baz_path, files) self.assertNotIn(thud_path, files)
def testSymlink(self): with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath,\ temp.AutoTempFilePath() as temp_filepath: with open(temp_filepath, "wb") as fd: fd.write("foobar") temp_linkpath = os.path.join(temp_dirpath, "foo") os.symlink(temp_filepath, temp_linkpath) stat = utils.Stat(temp_linkpath, follow_symlink=False) self.assertFalse(stat.IsDirectory()) self.assertFalse(stat.IsRegular()) self.assertFalse(stat.IsSocket()) self.assertTrue(stat.IsSymlink()) stat = utils.Stat(temp_linkpath, follow_symlink=True) self.assertFalse(stat.IsDirectory()) self.assertTrue(stat.IsRegular()) self.assertFalse(stat.IsSocket()) self.assertFalse(stat.IsSymlink()) self.assertEqual(stat.GetSize(), 6)
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.assertEqual(len(filedescs), 3) self.assertEqual(filedescs[0].Read(), b"FOO") self.assertEqual(filedescs[1].Read(), b"BAR") self.assertEqual(filedescs[2].Read(), b"BAZ")