Ejemplo n.º 1
0
  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"])
Ejemplo n.º 2
0
    def testStatExtAttrs(self):
        with test_lib.AutoTempFilePath() as temp_filepath:
            client_test_lib.SetExtAttr(temp_filepath,
                                       name="user.foo",
                                       value="bar")
            client_test_lib.SetExtAttr(temp_filepath,
                                       name="user.quux",
                                       value="norf")

            action = rdf_file_finder.FileFinderAction.Stat()
            results = self._RunFileFinder([temp_filepath], action)
            self.assertEqual(len(results), 1)

            ext_attrs = results[0].stat_entry.ext_attrs
            self.assertEqual(ext_attrs[0].name, "user.foo")
            self.assertEqual(ext_attrs[0].value, "bar")
            self.assertEqual(ext_attrs[1].name, "user.quux")
            self.assertEqual(ext_attrs[1].value, "norf")

            action = rdf_file_finder.FileFinderAction.Stat(
                collect_ext_attrs=False)
            results = self._RunFileFinder([temp_filepath], action)
            self.assertEqual(len(results), 1)

            ext_attrs = results[0].stat_entry.ext_attrs
            self.assertFalse(ext_attrs)
Ejemplo n.º 3
0
  def testMany(self):
    with test_lib.AutoTempFilePath() as temp_filepath:
      client_test_lib.SetExtAttr(temp_filepath, name="user.foo", value="bar")
      client_test_lib.SetExtAttr(temp_filepath, name="user.quux", value="norf")

      attrs = list(client_utils_linux.GetExtAttrs(temp_filepath))

      self.assertEqual(len(attrs), 2)
      self.assertEqual(attrs[0].name, "user.foo")
      self.assertEqual(attrs[0].value, "bar")
      self.assertEqual(attrs[1].name, "user.quux")
      self.assertEqual(attrs[1].value, "norf")
Ejemplo n.º 4
0
  def testFileFinderStatExtAttrs(self):
    with temp.AutoTempFilePath() as temp_filepath:
      client_test_lib.SetExtAttr(temp_filepath, name=b"user.bar", value=b"quux")
      client_test_lib.SetExtAttr(temp_filepath, name=b"user.baz", value=b"norf")

      action = rdf_file_finder.FileFinderAction.Stat()
      results = self.RunFlow(action=action, paths=[temp_filepath])
      self.assertEqual(len(results), 1)

      stat_entry = results[0].stat_entry
      self.assertItemsEqual(stat_entry.ext_attrs, [
          rdf_client_fs.ExtAttr(name=b"user.bar", value=b"quux"),
          rdf_client_fs.ExtAttr(name=b"user.baz", value=b"norf"),
      ])
Ejemplo n.º 5
0
  def testAttrChangeAfterListing(self, listxattr):
    with test_lib.AutoTempFilePath() as temp_filepath:
      client_test_lib.SetExtAttr(temp_filepath, name="user.bar", value="baz")

      attrs = list(client_utils_linux.GetExtAttrs(temp_filepath))

      self.assertTrue(listxattr.called)
      self.assertEqual(len(attrs), 1)
      self.assertEqual(attrs[0].name, "user.bar")
      self.assertEqual(attrs[0].value, "baz")
Ejemplo n.º 6
0
    def testStatExtAttrBytesName(self):
        with temp.AutoTempFilePath() as temp_filepath:
            name = b"user.\xDE\xAD\xBE\xEF"
            value = b"bar"

            client_test_lib.SetExtAttr(temp_filepath, name=name, value=value)

            # This should not explode (`xattr` does not handle non-unicode names).
            action = rdf_file_finder.FileFinderAction.Stat()
            results = self._RunFileFinder([temp_filepath], action)
            self.assertLen(results, 1)
Ejemplo n.º 7
0
  def testStatExtAttrsDisabled(self):
    with test_lib.AutoTempFilePath() as temp_filepath:
      client_test_lib.SetExtAttr(temp_filepath, name="user.foo", value="bar")

      pathspec = rdf_paths.PathSpec(
          path=temp_filepath, pathtype=rdf_paths.PathSpec.PathType.OS)

      request = rdf_client_action.GetFileStatRequest(
          pathspec=pathspec, collect_ext_attrs=False)
      results = self.RunAction(standard.GetFileStat, request)

      self.assertEqual(len(results), 1)
      self.assertEqual(len(results[0].ext_attrs), 0)
Ejemplo n.º 8
0
    def testStatExtAttrBytesValue(self):
        with temp.AutoTempFilePath() as temp_filepath:
            name = b"user.foo"
            value = b"\xDE\xAD\xBE\xEF"

            client_test_lib.SetExtAttr(temp_filepath, name=name, value=value)

            action = rdf_file_finder.FileFinderAction.Stat()
            results = self._RunFileFinder([temp_filepath], action)
            self.assertLen(results, 1)

            ext_attrs = results[0].stat_entry.ext_attrs
            self.assertLen(ext_attrs, 1)
            self.assertEqual(ext_attrs[0].name, name)
            self.assertEqual(ext_attrs[0].value, value)
Ejemplo n.º 9
0
    def testStatExtAttrUnicode(self):
        with temp.AutoTempFilePath() as temp_filepath:
            name_0 = "user.żółć".encode("utf-8")
            value_0 = "jaźń".encode("utf-8")
            client_test_lib.SetExtAttr(temp_filepath,
                                       name=name_0,
                                       value=value_0)

            name_1 = "user.rtęć".encode("utf-8")
            value_1 = "kość".encode("utf-8")
            client_test_lib.SetExtAttr(temp_filepath,
                                       name=name_1,
                                       value=value_1)

            action = rdf_file_finder.FileFinderAction.Stat()
            results = self._RunFileFinder([temp_filepath], action)
            self.assertLen(results, 1)

            ext_attrs = results[0].stat_entry.ext_attrs
            self.assertLen(ext_attrs, 2)
            self.assertEqual(ext_attrs[0].name, name_0)
            self.assertEqual(ext_attrs[0].value, value_0)
            self.assertEqual(ext_attrs[1].name, name_1)
            self.assertEqual(ext_attrs[1].value, value_1)