def testAttrChangeAfterListing(self, listxattr): with temp.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.assertLen(attrs, 1) self.assertEqual(attrs[0].name, "user.bar") self.assertEqual(attrs[0].value, "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 testGetOsxFlags(self): with temp.AutoTempFilePath() as temp_filepath: client_test_lib.Chflags(temp_filepath, flags=["nodump", "hidden"]) stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertTrue(stat.IsRegular()) self.assertTrue(stat.GetOsxFlags() & self.UF_NODUMP) self.assertTrue(stat.GetOsxFlags() & self.UF_HIDDEN) self.assertFalse(stat.GetOsxFlags() & self.UF_IMMUTABLE) self.assertEqual(stat.GetLinuxFlags(), 0)
def testGetLinuxFlags(self): with temp.AutoTempFilePath() as temp_filepath: client_test_lib.Chattr(temp_filepath, attrs=["+c", "+d"]) stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertTrue(stat.IsRegular()) self.assertTrue(stat.GetLinuxFlags() & self.FS_COMPR_FL) self.assertTrue(stat.GetLinuxFlags() & self.FS_NODUMP_FL) self.assertFalse(stat.GetLinuxFlags() & self.FS_IMMUTABLE_FL) self.assertEqual(stat.GetOsxFlags(), 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)
def testFileFinderStatExtFlags(self): with temp.AutoTempFilePath() as temp_filepath: client_test_lib.Chattr(temp_filepath, attrs=["+d"]) 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.assertTrue(stat_entry.st_flags_linux & self.FS_NODUMP_FL) self.assertFalse(stat_entry.st_flags_linux & self.FS_UNRM_FL)
def testStatExtFlags(self): with temp.AutoTempFilePath() as temp_filepath: client_test_lib.Chattr(temp_filepath, attrs=["+c"]) action = rdf_file_finder.FileFinderAction.Stat() results = self._RunFileFinder([temp_filepath], action) self.assertLen(results, 1) stat_entry = results[0].stat_entry self.assertTrue(stat_entry.st_flags_linux & self.EXT2_COMPR_FL) self.assertFalse(stat_entry.st_flags_linux & self.EXT2_IMMUTABLE_FL)
def testGetTime(self): adate = datetime.datetime(2017, 10, 2, 8, 45) mdate = datetime.datetime(2001, 5, 3, 10, 30) with temp.AutoTempFilePath() as temp_filepath: self._Touch(temp_filepath, "-a", adate) self._Touch(temp_filepath, "-m", mdate) stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertEqual(stat.GetAccessTime(), self._EpochMillis(adate)) self.assertEqual(stat.GetModificationTime(), self._EpochMillis(mdate))
def testEmpty(self): action = FakeAction() uploader = uploading.TransferStoreUploader(action, chunk_size=3) with temp.AutoTempFilePath() as temp_filepath: blobdesc = uploader.UploadFilePath(temp_filepath) self.assertEqual(action.charged_bytes, 0) self.assertEqual(len(action.messages), 0) self.assertEqual(len(blobdesc.chunks), 0) self.assertEqual(blobdesc.chunk_size, 3)
def testMany(self): with temp.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.assertLen(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")
def testStatSize(self): with temp.AutoTempFilePath() as temp_filepath: with open(temp_filepath, "wb") as temp_file: temp_file.write("123456") pathspec = rdf_paths.PathSpec( path=temp_filepath, pathtype=rdf_paths.PathSpec.PathType.OS) request = rdf_client_action.GetFileStatRequest(pathspec=pathspec) results = self.RunAction(standard.GetFileStat, request) self.assertLen(results, 1) self.assertEqual(results[0].st_size, 6)
def testStatExtAttrsDisabled(self): with temp.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)
def testHashFilePart(self): with temp.AutoTempFilePath() as tmp_path: with open(tmp_path, "wb") as tmp_file: tmp_file.write("foobar") hasher = client_utils_common.MultiHasher(["md5", "sha1"]) hasher.HashFilePath(tmp_path, len("foo")) hash_object = hasher.GetHashObject() self.assertEqual(hash_object.num_bytes, len("foo")) self.assertEqual(hash_object.md5, self._GetHash(hashlib.md5, "foo")) self.assertEqual(hash_object.sha1, self._GetHash(hashlib.sha1, "foo")) self.assertFalse(hash_object.sha256)
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"), ])
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)
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)
def testSingleChunk(self): action = FakeAction() uploader = uploading.TransferStoreUploader(action, chunk_size=6) with temp.AutoTempFilePath() as temp_filepath: with open(temp_filepath, "w") as temp_file: temp_file.write("foobar") blobdesc = uploader.UploadFilePath(temp_filepath) self.assertEqual(action.charged_bytes, 6) self.assertEqual(len(action.messages), 1) self.assertEqual(action.messages[0].item.data, zlib.compress("foobar")) self.assertEqual(len(blobdesc.chunks), 1) self.assertEqual(blobdesc.chunk_size, 6) self.assertEqual(blobdesc.chunks[0].offset, 0) self.assertEqual(blobdesc.chunks[0].length, 6) self.assertEqual(blobdesc.chunks[0].digest, Sha256("foobar"))
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 testLimitedAmount(self): action = FakeAction() uploader = uploading.TransferStoreUploader(action, chunk_size=3) with temp.AutoTempFilePath() as temp_filepath: with open(temp_filepath, "w") as temp_file: temp_file.write("1234567890") blobdesc = uploader.UploadFilePath(temp_filepath, amount=5) self.assertEqual(action.charged_bytes, 5) self.assertEqual(len(action.messages), 2) self.assertEqual(action.messages[0].item.data, zlib.compress("123")) self.assertEqual(action.messages[1].item.data, zlib.compress("45")) self.assertEqual(len(blobdesc.chunks), 2) self.assertEqual(blobdesc.chunk_size, 3) self.assertEqual(blobdesc.chunks[0].offset, 0) self.assertEqual(blobdesc.chunks[0].length, 3) self.assertEqual(blobdesc.chunks[0].digest, Sha256("123")) self.assertEqual(blobdesc.chunks[1].offset, 3) self.assertEqual(blobdesc.chunks[1].length, 2) self.assertEqual(blobdesc.chunks[1].digest, Sha256("45"))
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)
def testGetPath(self): with temp.AutoTempFilePath() as temp_filepath: stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertEqual(stat.GetPath(), temp_filepath)
def testEmpty(self): with temp.AutoTempFilePath() as temp_filepath: attrs = list(client_utils_linux.GetExtAttrs(temp_filepath)) self.assertEqual(len(attrs), 0)