def testParseAcquireAll(self): recipes = {'test1': None, 'test2': None} af = auto_acquire.AutoForensicate(recipes=recipes) test_args = ['--acquire', 'test1', '--acquire', 'all', 'gs://bucket'] options = af.ParseArguments(test_args) expected_recipes = ['disk', 'firmware', 'sysinfo'] self.assertEqual(options.acquire, expected_recipes)
def testMakeUploader(self): af = auto_acquire.AutoForensicate(recipes={'test': None}) options = af.ParseArguments(['--acquire', 'all', 'destination']) uploader_object = af._MakeUploader(options) self.assertIsNone(uploader_object) options = af.ParseArguments(['--acquire', 'all', 'gs://destination']) with self.assertRaises(errors.BadConfigOption): # We need a --gs_keyfile option for gs:// URLs uploader_object = af._MakeUploader(options) af._ParseGCSJSON = self.FakeBadParseGCSJSON options = af.ParseArguments([ '--acquire', 'all', '--gs_keyfile', 'keyfile', 'gs://destination' ]) with self.assertRaises(errors.BadConfigOption): # Invalid gs_keyfile uploader_object = af._MakeUploader(options) af._ParseGCSJSON = self.FakeParseGCSJSON options = af.ParseArguments([ '--acquire', 'all', '--gs_keyfile', 'keyfile', 'gs://destination' ]) uploader_object = af._MakeUploader(options) self.assertIsInstance(uploader_object, uploader.GCSUploader)
def testParseArgsHelp(self): """Test for help message option.""" recipes = {'test1': None, 'test2': None} self.maxDiff = None af = auto_acquire.AutoForensicate(recipes=recipes) parser = af._CreateParser() expected_help = ( 'usage: run_tests.py [-h] --acquire {all,test1,test2} [--gs_keyfile ' 'GS_KEYFILE]\n' ' [--logging {stackdriver,stdout}] [--select_disks]' '\n [--disk DISK]' '\n' ' destination\n\n' 'Autopush forensics evidence to Cloud Storage\n\n' 'positional arguments:\n' ' destination Sets the destination for uploads. For example' '\n gs://bucket_name/path will upload to GCS in' ' bucket\n <bucket_name> in the folder </path/>' '\n\n' 'optional arguments:\n' ' -h, --help show this help message and exit\n' ' --acquire {all,test1,test2}\n' ' Evidence to acquire\n' ' --gs_keyfile GS_KEYFILE\n' ' Path to the service account private key JSON ' 'file for\n Google Cloud\n' ' --logging {stackdriver,stdout}\n' ' Selects logging methods.\n' ' --select_disks Asks the user to select which disk to acquire' '\n --disk DISK Specify a disk to acquire (eg: sda)' '\n') self.assertEqual(parser.format_help(), expected_help)
def testFailDo(self): af = auto_acquire.AutoForensicate(recipes={}) recipe = FailingRecipe('fail') with tempfile.TemporaryFile() as destination: uploader_object = FileCopyUploader(destination) af._uploader = uploader_object with self.assertRaises(errors.RecipeException): af.Do(recipe)
def testParseAcquireOneRecipe(self): recipes = {'test1': None, 'test2': None} test_args = ['--acquire', 'test1', 'nfs://destination'] af = auto_acquire.AutoForensicate(recipes=recipes) parser = af._CreateParser() options = parser.parse_args(test_args) expected_recipes = ['test1'] self.assertEqual(options.acquire, expected_recipes)
def testParseArgsRequiredURL(self): recipes = {'test1': None, 'test2': None} af = auto_acquire.AutoForensicate(recipes=recipes) test_args = ['--acquire', 'test1', '--gs_keyfile=null'] prev_stderr = sys.stderr sys.stderr = StringIO() with self.assertRaises(SystemExit): af.ParseArguments(test_args) sys.stderr = prev_stderr
def testParseArgsRequiredJson(self): recipes = {'test1': None, 'test2': None} af = auto_acquire.AutoForensicate(recipes=recipes) test_args = ['--acquire', 'test1', '--logging', 'stackdriver'] with self.assertRaises(SystemExit): prev_stderr = sys.stderr sys.stderr = StringIO() af.ParseArguments(test_args) sys.stderr = prev_stderr
def testParseDestination(self): recipes = { 'test1': None, 'test2': None } af = auto_acquire.AutoForensicate(recipes=recipes) test_args = ['--acquire', 'all', 'destination_url'] options = af.ParseArguments(test_args) self.assertEqual(options.destination, 'destination_url')
def testDo(self): af = auto_acquire.AutoForensicate(recipes={}) parser = argparse.ArgumentParser() parser.add_argument('--fake', action='store_true') options = parser.parse_args(['--fake']) af._logger = logging.getLogger(self.__class__.__name__) af._MakeProgressBar = self.FakeMakeProgressBar recipe = BytesIORecipe('stringio', options=options) self.assertTrue(recipe._options.fake) with tempfile.TemporaryFile() as destination: uploader_object = FileCopyUploader(destination) af._uploader = uploader_object af.Do(recipe) destination.seek(0) copied_data = destination.read() self.assertEqual(copied_data, DEFAULT_ARTIFACT_CONTENT)