def testNestedHierarchy(self): with temp.AutoTempDirPath(remove_non_empty=True) as dirpath: thud_filepath = os.path.join(dirpath, "foo", "bar", "baz", "quux", "thud") filesystem_test_lib.CreateFile(thud_filepath, content=b"thud") blargh_filepath = os.path.join(dirpath, "foo", "bar", "blargh") filesystem_test_lib.CreateFile(blargh_filepath, content=b"blargh") entries = list(self._Collect(dirpath.encode("utf-8"))) self.assertLen(entries, 7) paths = [_.path.decode("utf-8") for _ in entries] self.assertCountEqual(paths, [ os.path.join(dirpath), os.path.join(dirpath, "foo"), os.path.join(dirpath, "foo", "bar"), os.path.join(dirpath, "foo", "bar", "baz"), os.path.join(dirpath, "foo", "bar", "baz", "quux"), os.path.join(dirpath, "foo", "bar", "baz", "quux", "thud"), os.path.join(dirpath, "foo", "bar", "blargh"), ]) entries_by_path = { entry.path.decode("utf-8"): entry for entry in entries } self.assertEqual(entries_by_path[thud_filepath].size, 4) self.assertEqual(entries_by_path[blargh_filepath].size, 6)
def testProgress(self): client_id = self.client_id with temp.AutoTempDirPath(remove_non_empty=True) as tempdir: filesystem_test_lib.CreateFile(os.path.join(tempdir, "foo")) filesystem_test_lib.CreateFile(os.path.join(tempdir, "bar")) filesystem_test_lib.CreateFile(os.path.join(tempdir, "baz")) args = rdf_timeline.TimelineArgs() args.root = tempdir.encode("utf-8") flow_id = flow_test_lib.StartFlow(timeline_flow.TimelineFlow, client_id=client_id, flow_args=args) progress = flow_test_lib.GetFlowProgress(client_id=client_id, flow_id=flow_id) self.assertEqual(progress.total_entry_count, 0) flow_test_lib.RunFlow(client_id=client_id, flow_id=flow_id, client_mock=action_mocks.ActionMock( timeline_action.Timeline)) progress = flow_test_lib.GetFlowProgress(client_id=client_id, flow_id=flow_id) self.assertEqual(progress.total_entry_count, 4)
def testOldClientSnapshotFallbackUsesLatestApplicable(self): rel_db = data_store.REL_DB client_id = "C.0123456789abcdef" rel_db.WriteClientMetadata(client_id, first_seen=rdfvalue.RDFDatetime.Now()) # Write some fake snapshot history. kb_0 = rdf_client.KnowledgeBase(os="Linux", os_release="rel0") snapshot_0 = rdf_objects.ClientSnapshot( client_id=client_id, knowledge_base=kb_0) rel_db.WriteClientSnapshot(snapshot_0) kb_1 = rdf_client.KnowledgeBase(os="Linux", os_release="rel1") snapshot_1 = rdf_objects.ClientSnapshot( client_id=client_id, knowledge_base=kb_1) rel_db.WriteClientSnapshot(snapshot_1) kb_2 = rdf_client.KnowledgeBase(os="Linux") snapshot_2 = rdf_objects.ClientSnapshot( client_id=client_id, knowledge_base=kb_2) rel_db.WriteClientSnapshot(snapshot_2) with temp.AutoTempDirPath(remove_non_empty=True) as dirpath: filesystem_test_lib.CreateFile(os.path.join(dirpath, "rel0", "quux")) filesystem_test_lib.CreateFile(os.path.join(dirpath, "rel1", "norf")) # Write a fake artifact. art = rdf_artifacts.Artifact( name="Quux", doc="Lorem ipsum.", sources=[ rdf_artifacts.ArtifactSource( type=rdf_artifacts.ArtifactSource.SourceType.DIRECTORY, attributes={ "paths": [os.path.join(dirpath, "%%os_release%%", "*")], }), ]) rel_db.WriteArtifact(art) artifact_registry.REGISTRY.ReloadDatastoreArtifacts() flow_id = flow_test_lib.TestFlowHelper( compatibility.GetName(collectors.ArtifactCollectorFlow), client_mock=action_mocks.GlobClientMock(), client_id=client_id, artifact_list=["Quux"], old_client_snapshot_fallback=True, token=self.token) results = flow_test_lib.GetFlowResults(client_id=client_id, flow_id=flow_id) self.assertNotEmpty(results) basenames = [os.path.basename(result.pathspec.path) for result in results] self.assertNotIn("quux", basenames) self.assertIn("norf", basenames)
def testContents(self): with temp.AutoTempDirPath(remove_non_empty=True) as dirpath: filepath = os.path.join(dirpath, "foo") filesystem_test_lib.CreateFile(filepath, content=b"foobarbaz") with io.open(filepath, "rb") as filedesc: content = filedesc.read() self.assertEqual(content, b"foobarbaz")
def testSingleFile(self) -> None: with temp.AutoTempDirPath(remove_non_empty=True) as dirpath: filepath = os.path.join(dirpath, "foo") filesystem_test_lib.CreateFile(filepath, content=b"foobar") entries = list(self._Collect(dirpath.encode("utf-8"))) self.assertLen(entries, 2) self.assertTrue(stat_mode.S_ISDIR(entries[0].mode)) self.assertEqual(entries[0].path, dirpath.encode("utf-8")) self.assertTrue(stat_mode.S_ISREG(entries[1].mode)) self.assertEqual(entries[1].path, filepath.encode("utf-8")) self.assertEqual(entries[1].size, 6)
def testMultipleFiles(self) -> None: with temp.AutoTempDirPath(remove_non_empty=True) as dirpath: foo_filepath = os.path.join(dirpath, "foo") filesystem_test_lib.CreateFile(foo_filepath) bar_filepath = os.path.join(dirpath, "bar") filesystem_test_lib.CreateFile(bar_filepath) baz_filepath = os.path.join(dirpath, "baz") filesystem_test_lib.CreateFile(baz_filepath) entries = list(self._Collect(dirpath.encode("utf-8"))) self.assertLen(entries, 4) self.assertTrue(stat_mode.S_ISDIR(entries[0].mode)) self.assertEqual(entries[0].path, dirpath.encode("utf-8")) paths = [_.path for _ in entries[1:]] self.assertIn(foo_filepath.encode("utf-8"), paths) self.assertIn(bar_filepath.encode("utf-8"), paths) self.assertIn(baz_filepath.encode("utf-8"), paths) for entry in entries[1:]: self.assertTrue(stat_mode.S_ISREG(entry.mode))
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 testPathCreation(self): with temp.AutoTempDirPath(remove_non_empty=True) as dirpath: filepath = os.path.join(dirpath, "foo", "bar", "baz") filesystem_test_lib.CreateFile(filepath) self.assertTrue(os.path.exists(filepath))