Commit 3d2429a4 authored by jvoisin's avatar jvoisin
Browse files

Fiabilise secure_remove + associated tests

parent cc1664d0
......@@ -13,6 +13,8 @@ import xml.sax
import hachoir_core.cmd_line
import hachoir_parser
import MAT.exceptions
__version__ = '0.4'
__author__ = 'jvoisin'
......@@ -107,18 +109,20 @@ def secure_remove(filename):
'''
securely remove the file
'''
removed = False
try:
subprocess.call(['shred', '--remove', filename])
removed = True
if subprocess.call(['shred', '--remove', filename]) == 0:
return True
else:
raise OSError
except OSError:
logging.error('Unable to securely remove %s' % filename)
if not removed:
try:
os.remove(filename)
except OSError:
logging.error('Unable to remove %s' % filename)
try:
os.remove(filename)
return True
except OSError:
logging.error('Unable to remove %s' % filename)
raise MAT.exceptions.UnableToRemoveFile
def create_class_file(name, backup, **kwargs):
......
......@@ -8,8 +8,9 @@
import unittest
import test
import sys
import tempfile
sys.path.append('..')
from MAT import mat
import MAT
class TestRemovelib(test.MATTest):
......@@ -19,15 +20,15 @@ class TestRemovelib(test.MATTest):
def test_remove(self):
'''make sure that the lib remove all compromizing meta'''
for _, dirty in self.file_list:
current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
current_file = MAT.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
current_file.remove_all()
current_file2 = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
current_file2 = MAT.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
self.assertTrue(current_file2.is_clean())
def test_remove_empty(self):
'''Test removal with clean files'''
for clean, _ in self.file_list:
current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
current_file = MAT.mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
current_file.remove_all()
self.assertTrue(current_file.is_clean())
......@@ -39,14 +40,14 @@ class TestListlib(test.MATTest):
def test_list(self):
'''check if get_meta returns all the expected meta'''
for _, dirty in self.file_list:
current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
current_file = MAT.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
#FIXME assertisNotNone() : python 2.7
self.assertTrue(current_file.get_meta())
def testlist_list_empty(self):
'''check that a listing of a clean file return an empty dict'''
for clean, _ in self.file_list:
current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
current_file = MAT.mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
self.assertEqual(current_file.get_meta(), dict())
......@@ -57,13 +58,13 @@ class TestisCleanlib(test.MATTest):
def test_dirty(self):
'''test is_clean on clean files'''
for _, dirty in self.file_list:
current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
current_file = MAT.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
self.assertFalse(current_file.is_clean())
def test_clean(self):
'''test is_clean on dirty files'''
for clean, _ in self.file_list:
current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
current_file = MAT.mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
self.assertTrue(current_file.is_clean())
......@@ -73,15 +74,30 @@ class TestFileAttributes(unittest.TestCase):
'''
def test_not_writtable(self):
''' test MAT's behaviour on non-writable file'''
self.assertFalse(mat.create_class_file('not_writtable', False, add2archive=True, low_pdf_quality=True))
self.assertFalse(MAT.mat.create_class_file('not_writtable', False, add2archive=True, low_pdf_quality=True))
def test_not_exist(self):
''' test MAT's behaviour on non-existent file'''
self.assertFalse(mat.create_class_file('ilikecookies', False, add2archive=True, low_pdf_quality=True))
self.assertFalse(MAT.mat.create_class_file('ilikecookies', False, add2archive=True, low_pdf_quality=True))
def test_empty(self):
''' test MAT's behaviour on empty file'''
self.assertFalse(mat.create_class_file('empty_file', False, add2archive=True, low_pdf_quality=True))
self.assertFalse(MAT.mat.create_class_file('empty_file', False, add2archive=True, low_pdf_quality=True))
class TestSecureRemove(unittest.TestCase):
''' Test the secure_remove function
'''
def test_remove_existing(self):
''' test the secure removal of an existing file
'''
_, file_to_remove = tempfile.mkstemp()
self.assertTrue(MAT.mat.secure_remove(file_to_remove))
def test_remove_fail(self):
''' test the secure removal of an non-removable file
'''
self.assertRaises(MAT.exceptions.UnableToRemoveFile, MAT.mat.secure_remove, '/OMAGAD')
def get_tests():
''' Return every libtests'''
......@@ -90,6 +106,7 @@ def get_tests():
suite.addTest(unittest.makeSuite(TestListlib))
suite.addTest(unittest.makeSuite(TestisCleanlib))
suite.addTest(unittest.makeSuite(TestFileAttributes))
suite.addTest(unittest.makeSuite(TestSecureRemove))
return 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