Exemplo n.º 1
0
    def testGetAllFilesErrors(self, warn):
        with test_lib.AutoTempDirPath() as foo_dirpath,\
             test_lib.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)
Exemplo n.º 2
0
    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 test_lib.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")
            ])
Exemplo n.º 3
0
 def testDirectory(self):
     with test_lib.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())
Exemplo n.º 4
0
    def testExtAttrsCollection(self):
        with test_lib.AutoTempDirPath(remove_non_empty=True) as temp_dirpath:
            foo_filepath = test_lib.TempFilePath(dir=temp_dirpath)
            self._Setfattr(foo_filepath, name="user.quux", value="foo")

            bar_filepath = test_lib.TempFilePath(dir=temp_dirpath)
            self._Setfattr(bar_filepath, name="user.quux", value="bar")

            baz_filepath = test_lib.TempFilePath(dir=temp_dirpath)
            self._Setfattr(baz_filepath, name="user.quux", value="baz")

            request = rdf_client.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.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"])
Exemplo n.º 5
0
    def testGetFlagsSymlink(self):
        with test_lib.AutoTempDirPath(remove_non_empty=True) as temp_dirpath,\
             test_lib.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)
Exemplo n.º 6
0
    def testGetFlagsSocket(self):
        with test_lib.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()
Exemplo n.º 7
0
    def testSocket(self):
        with test_lib.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()
Exemplo n.º 8
0
  def testGetAllFiles(self):
    with test_lib.AutoTempDirPath(remove_non_empty=True) as tmpdir_path:
      foo_path = test_lib.TempFilePath(suffix="foo.yaml")
      bar_path = test_lib.TempFilePath(suffix="bar.json")
      baz_path = test_lib.TempFilePath(suffix="baz.yaml")
      quux_path = test_lib.TempFilePath(dir=tmpdir_path, suffix="quux.yaml")
      norf_path = test_lib.TempFilePath(dir=tmpdir_path, suffix="norf.json")
      thud_path = test_lib.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)
Exemplo n.º 9
0
    def testSymlink(self):
        with test_lib.AutoTempDirPath(remove_non_empty=True) as temp_dirpath,\
             test_lib.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)
Exemplo n.º 10
0
    def testMultipleFiles(self):
        with test_lib.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")