def testHandlerRespectsTimestamp(self): archive_path1 = "vfs_C_1000000000000000/fs/os/c/Downloads/a.txt" archive_path2 = "vfs_C_1000000000000000/fs/os/c/b.txt" result = self.handler.Handle( vfs_plugin.ApiGetVfsFilesArchiveArgs( client_id=self.client_id, timestamp=self.time_2), context=self.context) out_fd = io.BytesIO() for chunk in result.GenerateContent(): out_fd.write(chunk) zip_fd = zipfile.ZipFile(out_fd, "r") self.assertCountEqual(zip_fd.namelist(), [archive_path1, archive_path2]) self.assertEqual(zip_fd.read(archive_path1), b"Goodbye World") self.assertEqual(zip_fd.read(archive_path2), b"Goodbye World") result = self.handler.Handle( vfs_plugin.ApiGetVfsFilesArchiveArgs( client_id=self.client_id, timestamp=self.time_1), context=self.context) out_fd = io.BytesIO() for chunk in result.GenerateContent(): out_fd.write(chunk) zip_fd = zipfile.ZipFile(out_fd, "r") self.assertCountEqual(zip_fd.namelist(), [archive_path1, archive_path2]) self.assertEqual(zip_fd.read(archive_path1), b"Hello World") self.assertEqual(zip_fd.read(archive_path2), b"Hello World")
def testNonExistentPathRaises(self): result = self.handler.Handle(vfs_plugin.ApiGetVfsFilesArchiveArgs( client_id=self.client_id, file_path="fs/os/blah/blah"), context=self.context) with self.assertRaises(db.UnknownPathError): out_fd = io.BytesIO() for chunk in result.GenerateContent(): out_fd.write(chunk)
def testVfsMethodsAreAccessChecked(self): args = api_vfs.ApiListFilesArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.ListFiles, "CheckClientAccess", args=args) args = api_vfs.ApiGetVfsFilesArchiveArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.GetVfsFilesArchive, "CheckClientAccess", args=args) args = api_vfs.ApiGetFileDetailsArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.GetFileDetails, "CheckClientAccess", args=args) args = api_vfs.ApiGetFileTextArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.GetFileText, "CheckClientAccess", args=args) args = api_vfs.ApiGetFileBlobArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.GetFileBlob, "CheckClientAccess", args=args) args = api_vfs.ApiGetFileVersionTimesArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.GetFileVersionTimes, "CheckClientAccess", args=args) args = api_vfs.ApiGetFileDownloadCommandArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.GetFileDownloadCommand, "CheckClientAccess", args=args) args = api_vfs.ApiCreateVfsRefreshOperationArgs( client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.CreateVfsRefreshOperation, "CheckClientAccess", args=args) args = api_vfs.ApiGetVfsTimelineArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.GetVfsTimeline, "CheckClientAccess", args=args) args = api_vfs.ApiGetVfsTimelineAsCsvArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.GetVfsTimelineAsCsv, "CheckClientAccess", args=args) args = api_vfs.ApiUpdateVfsFileContentArgs(client_id=self.client_id) self.CheckMethodIsAccessChecked(self.router.UpdateVfsFileContent, "CheckClientAccess", args=args)
def testNonExistentPathGeneratesEmptyArchive(self): result = self.handler.Handle(vfs_plugin.ApiGetVfsFilesArchiveArgs( client_id=self.client_id, file_path="fs/os/blah/blah"), token=self.token) out_fd = io.BytesIO() for chunk in result.GenerateContent(): out_fd.write(chunk) zip_fd = zipfile.ZipFile(out_fd, "r") self.assertEqual(zip_fd.namelist(), [])
def testClickingOnDownloadEverythingButtonStartsDownload(self, mock_method): # Open VFS view for client 1 on a specific location. self.Open("/#c=C.0000000000000001&main=VirtualFileSystemView" "&t=_fs-os-c-proc") self.Click("css=grr-vfs-files-archive-button") self.Click("css=a[name=downloadEverything]:not([disabled])") # Mock method will be called twice: once for HEAD request (to check # permissions) and once for GET request. self.WaitUntil(lambda: mock_method.call_count) mock_method.assert_called_with( api_vfs.ApiGetVfsFilesArchiveArgs(client_id="C.0000000000000001"), token=mock.ANY)
def testFiltersArchivedFilesByPath(self): archive_path = ("vfs_C_1000000000000000_fs_os_c_Downloads/" "fs/os/c/Downloads/a.txt") result = self.handler.Handle(vfs_plugin.ApiGetVfsFilesArchiveArgs( client_id=self.client_id, file_path="fs/os/c/Downloads"), context=self.context) out_fd = io.BytesIO() for chunk in result.GenerateContent(): out_fd.write(chunk) zip_fd = zipfile.ZipFile(out_fd, "r") self.assertEqual(zip_fd.namelist(), [archive_path]) contents = zip_fd.read(archive_path) self.assertEqual(contents, b"Goodbye World")
def testGeneratesZipArchiveWhenPathIsNotPassed(self): archive_path1 = "vfs_C_1000000000000000/fs/os/c/Downloads/a.txt" archive_path2 = "vfs_C_1000000000000000/fs/os/c/b.txt" result = self.handler.Handle( vfs_plugin.ApiGetVfsFilesArchiveArgs(client_id=self.client_id), context=self.context) out_fd = io.BytesIO() for chunk in result.GenerateContent(): out_fd.write(chunk) zip_fd = zipfile.ZipFile(out_fd, "r") self.assertEqual( set(zip_fd.namelist()), set([archive_path1, archive_path2])) for path in [archive_path1, archive_path2]: contents = zip_fd.read(path) self.assertEqual(contents, b"Goodbye World")
def testInvalidPathTriggersException(self): with self.assertRaises(ValueError): self.handler.Handle( vfs_plugin.ApiGetVfsFilesArchiveArgs( client_id=self.client_id, file_path="invalid-prefix/path"), context=self.context)