Package rekall :: Package plugins :: Package response :: Module files_test
[frames] | no frames]

Source Code for Module rekall.plugins.response.files_test

 1  import os 
 2  import mock 
 3   
 4  from rekall import testlib 
 5  from rekall import utils 
 6  from rekall.plugins.response import common 
 7  from rekall.plugins.response import files 
 8   
 9   
10 -class TestGlob(testlib.RekallBaseUnitTestCase):
11 """Test file operations.""" 12
13 - def testGlobComponents(self):
14 """Check to ensure the components are created properly.""" 15 glob_plugin = files.IRGlob(session=self.session) 16 path_components = glob_plugin.convert_glob_into_path_components( 17 "/usr/*/ls") 18 self.assertListEqual([str(x) for x in path_components], 19 ["LiteralComponent:usr", 20 "RegexComponent:.*\Z(?ms)", 21 "LiteralComponent:ls"]) 22 23 glob_plugin = files.IRGlob(session=self.session, path_sep="\\") 24 path_components = glob_plugin.convert_glob_into_path_components( 25 "c:\\windows\\**\*.exe") 26 self.assertListEqual([str(x) for x in path_components], 27 ["LiteralComponent:c:", 28 "LiteralComponent:windows", 29 "RecursiveComponent:.*\Z(?ms)", 30 "RegexComponent:.*\\.exe\\Z(?ms)"])
31
32 - def testComponents(self):
33 self.component_cache = utils.FastStore(50) 34 literal = files.LiteralComponent(session=self.session, 35 cache=self.component_cache, 36 component="passwd") 37 38 path_spec = common.FileSpec("/etc") 39 result = list(literal.filter(path_spec)) 40 self.assertTrue("/etc/passwd" in [unicode(x) for x in result]) 41 42 regex = files.RegexComponent(session=self.session, 43 cache=self.component_cache, 44 component="pass.+") 45 46 result = list(regex.filter(path_spec)) 47 self.assertTrue("/etc/passwd" in [unicode(x) for x in result]) 48 49 recursive = files.RecursiveComponent(session=self.session, 50 cache=self.component_cache, 51 component=".+") 52 53 result = list(recursive.filter(path_spec)) 54 self.assertTrue("/etc/ssh/ssh_config" in [unicode(x) for x in result])
55
56 - def _touch(self, path):
57 with open(path, "wb") as fd: 58 fd.write("")
59
60 - def _make_temp_directory(self):
61 self._touch(os.path.join(self.temp_directory, "boo.txt")) 62 os.makedirs(os.path.join(self.temp_directory, "foo")) 63 self._touch(os.path.join(self.temp_directory, "foo/boo2.txt")) 64 65 # Drop a symlink to / - if we follow links this will crash. 66 os.symlink("/", os.path.join(self.temp_directory, "link"))
67
68 - def testGlob(self):
69 self._make_temp_directory() 70 glob_plugin = files.IRGlob(session=self.session, globs=[ 71 self.temp_directory + "/*.txt"]) 72 result = list(glob_plugin.collect()) 73 self.assertTrue("boo.txt" in [os.path.basename(unicode(x["path"].filename)) 74 for x in result]) 75 self.assertEqual(len(result), 1) 76 77 glob_plugin = files.IRGlob(session=self.session, globs=[ 78 self.temp_directory + "/**/*.txt"]) 79 result = list(glob_plugin.collect()) 80 paths = [os.path.basename(unicode(x["path"].filename)) 81 for x in result] 82 self.assertEqual(["boo.txt", "boo2.txt"], paths)
83 84 85 if __name__ == "__main__": 86 testlib.main() 87