Commit 1bb8abef authored by jvoisin's avatar jvoisin
Browse files

Handle files with stupid rights

Now, files that are not readable, or writatble, or non-existent
are correctly handled.
parent 52fef0b1
......@@ -152,8 +152,10 @@ def main():
for filename in filenames:
class_file = mat.create_class_file(filename, args.backup,
args.add2archive)
if class_file is not None:
if class_file is not False:
func(class_file, filename, args.force)
else:
print('Unable to process %s' % filename)
if __name__ == '__main__':
main()
......@@ -108,7 +108,17 @@ def create_class_file(name, backup, add2archive):
if not os.path.isfile(name):
# check if the file exists
logging.error('%s is not a valid file' % name)
return
return False
if not os.access(name, os.R_OK):
#check read permissions
logging.error('%s is is not readable' % name)
return False
if not os.access(name, os.W_OK):
#check write permission
logging.error('%s is not writtable' % name)
return False
filename = ''
try:
......@@ -119,7 +129,7 @@ def create_class_file(name, backup, add2archive):
parser = hachoir_parser.createParser(filename)
if not parser:
logging.info('Unable to parse %s' % filename)
return
return False
mime = parser.mime_type
......@@ -135,6 +145,6 @@ def create_class_file(name, backup, add2archive):
stripper_class = strippers.STRIPPERS[mime]
except KeyError:
logging.info('Don\'t have stripper for %s format' % mime)
return
return False
return stripper_class(filename, parser, mime, backup, add2archive)
......@@ -72,6 +72,27 @@ class TestisCleancli(test.MATTest):
stdout, _ = proc.communicate()
self.assertEqual(stdout.strip('\n'), '[+] %s is not clean' % dirty)
class TestFileAttributes(unittest.TestCase):
'''
test various stuffs about files (readable, writable, exist, ...)
'''
def test_not_readable(self):
proc = subprocess.Popen(['../mat-cli', 'not_readable'],
stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
self.assertEqual(stdout.strip('\n'), 'Unable to pocess %s' % 'not_readable')
def test_not_writtable(self):
proc = subprocess.Popen(['../mat-cli', 'not_writtable'],
stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
self.assertEqual(stdout.strip('\n'), 'Unable to pocess %s' % 'not_writtable')
def test_not_exist(self):
proc = subprocess.Popen(['../mat-cli', 'ilikecookies'],
stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
self.assertEqual(stdout.strip('\n'), 'Unable to pocess %s' % 'ilikecookies')
if __name__ == '__main__':
suite = unittest.TestSuite()
......
......@@ -64,11 +64,24 @@ class TestisCleanlib(test.MATTest):
current_file = mat.create_class_file(clean, False, True)
self.assertTrue(current_file.is_clean())
class TestFileAttributes(unittest.TestCase):
'''
test various stuffs about files (readable, writable, exist, ...)
'''
def test_not_readable(self):
self.assertFalse(mat.create_class_file('not_readable', False, True))
def test_not_writtable(self):
self.assertFalse(mat.create_class_file('not_writtable', False, True))
def test_not_exist(self):
self.assertFalse(mat.create_class_file('ilikecookies', False, True))
if __name__ == '__main__':
Suite = unittest.TestSuite()
Suite.addTest(unittest.makeSuite(TestRemovelib))
Suite.addTest(unittest.makeSuite(TestListlib))
Suite.addTest(unittest.makeSuite(TestisCleanlib))
Suite.addTest(unittest.makeSuite(TestFileAttributes))
unittest.TextTestRunner(verbosity=test.VERBOSITY).run(Suite)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment