From 368474eaaa6cce9d219ff1963ff31e521eca62e9 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 27 Mar 2016 19:08:48 +0200 Subject: Move some testfiles around The long-term goal is to use nose2 for testing --- test/clitest.py | 178 ------------------------------------------------ test/libtest.py | 203 ------------------------------------------------------- test/test.py | 8 +-- test/test_cli.py | 178 ++++++++++++++++++++++++++++++++++++++++++++++++ test/test_lib.py | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 385 insertions(+), 385 deletions(-) delete mode 100644 test/clitest.py delete mode 100644 test/libtest.py create mode 100644 test/test_cli.py create mode 100644 test/test_lib.py diff --git a/test/clitest.py b/test/clitest.py deleted file mode 100644 index 884655f..0000000 --- a/test/clitest.py +++ /dev/null @@ -1,178 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -* - -""" - Unit test for the CLI interface -""" - -import os -import unittest -import subprocess -import sys -import tarfile -import stat - -import test -from libmat import mat - - -class TestRemovecli(test.MATTest): - """ - test if cli correctly remove metadatas - """ - - def test_remove(self): - """make sure that the cli remove all compromizing meta""" - for _, dirty in self.file_list: - subprocess.call(['mat', '--add2archive', dirty]) - current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) - self.assertTrue(current_file.is_clean()) - - def test_remove_fileformat_specific_options(self): - """ test metadata removal with fileformat-specific options """ - for _, dirty in self.file_list: # can't be faster than that :/ - if dirty.endswith('pdf'): - subprocess.call(['mat', '--low-pdf-quality', dirty]) - current_file = mat.create_class_file(dirty, False, low_pdf_quality=True) - self.assertTrue(current_file.is_clean()) - - def test_remove_empty(self): - """Test removal with clean files\n""" - for clean, _ in self.file_list: - subprocess.call(['mat', '--add2archive', clean]) - current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True) - self.assertTrue(current_file.is_clean()) - - -class TestListcli(test.MATTest): - """ - test if cli correctly display metadatas - """ - - def test_list_clean(self): - """check if get_meta returns meta""" - for clean, _ in self.file_list: - proc = subprocess.Popen(['mat', '-d', clean], - stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertEqual(str(stdout).strip('\n'), "[+] File %s \ -:\nNo harmful metadata found" % clean) - - def test_list_dirty(self): - """check if get_meta returns all the expected meta""" - for _, dirty in self.file_list: - proc = subprocess.Popen(['mat', '-d', dirty], - stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertNotEqual(str(stdout), "[+] File %s :\n No\ -harmul metadata found" % dirty) - - -class TestisCleancli(test.MATTest): - """ - check if cli correctly check if a file is clean or not - """ - - def test_clean(self): - """test is_clean on clean files""" - for clean, _ in self.file_list: - proc = subprocess.Popen(['mat', '-c', clean], - stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertEqual(str(stdout).strip('\n'), '[+] %s is clean' % clean) - - def test_dirty(self): - """test is_clean on dirty files""" - for _, dirty in self.file_list: - proc = subprocess.Popen(['mat', '-c', dirty], - stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertEqual(str(stdout).strip('\n'), '[+] %s is not clean' % dirty) - - -class TestFileAttributes(unittest.TestCase): - """ - test various stuffs about files (readable, writable, exist, ...) - """ - - def test_not_writtable(self): - """ test MAT's behaviour on non-writable file""" - proc = subprocess.Popen(['mat', 'not_writtable'], - stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process not_writtable') - - def test_not_exist(self): - """ test MAT's behaviour on non-existent file""" - proc = subprocess.Popen(['mat', 'ilikecookies'], - stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process ilikecookies') - - def test_empty(self): - """ test MAT's behaviour on empty file""" - proc = subprocess.Popen(['mat', 'empty_file'], stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process empty_file') - - def test_not_readable(self): - """ test MAT's behaviour on non-writable file""" - open('non_readable', 'a').close() - os.chmod('non_readable', 0 & stat.S_IWRITE) - proc = subprocess.Popen(['mat', 'non_readable'], stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - os.remove('non_readable') - - -class TestUnsupported(test.MATTest): - """ test MAT's behaviour on unsupported files """ - def test_abort_unsupported(self): - """ test if the cli aborts on unsupported files - """ - tarpath = os.path.join(self.tmpdir, "test.tar.bz2") - tar = tarfile.open(tarpath, "w") - for f in ('libtest.py', 'test.py', 'clitest.py'): - tar.add(f, f) - tar.close() - proc = subprocess.Popen(['mat', tarpath], stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertTrue('It contains unsupported filetypes:' - '\n- libtest.py\n- test.py\n- clitest.py\n' - in str(stdout)) - - -class TestHelp(test.MATTest): - """ Test the different ways to trigger help """ - def test_dash_h(self): - """ test help invocation with `-h` and `--help` """ - proc = subprocess.Popen(['mat', '-h'], stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertTrue('show this help message and exit' in stdout) - - proc = subprocess.Popen(['mat', '--help'], stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertTrue('show this help message and exit' in stdout) - - def test_no_argument(self): - """ test help invocation when no argument is provided """ - proc = subprocess.Popen(['mat'], stdout=subprocess.PIPE) - stdout, _ = proc.communicate() - self.assertTrue('show this help message and exit' in stdout) - - def test_wrong_argument(self): - """ Test MAT's behaviour on wrong argument """ - proc = subprocess.Popen(['mat', '--obviously-wrong-argument'], stderr=subprocess.PIPE) - _, stderr = proc.communicate() - self.assertTrue(('usage: mat [-h]' and ' error: unrecognized arguments:') in stderr) - - -def get_tests(): - """ Return every clitests""" - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(TestRemovecli)) - suite.addTest(unittest.makeSuite(TestListcli)) - suite.addTest(unittest.makeSuite(TestisCleancli)) - suite.addTest(unittest.makeSuite(TestUnsupported)) - suite.addTest(unittest.makeSuite(TestFileAttributes)) - suite.addTest(unittest.makeSuite(TestHelp)) - return suite diff --git a/test/libtest.py b/test/libtest.py deleted file mode 100644 index 98cb4ea..0000000 --- a/test/libtest.py +++ /dev/null @@ -1,203 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -* - -""" - Unit test for the library -""" - -import os -import sys -import stat -import shutil -import tarfile -import tempfile -import unittest - -import test -import libmat - - -class TestRemovelib(test.MATTest): - """ test the remove_all() method - """ - - def test_remove(self): - """make sure that the lib remove all compromizing meta""" - for _, dirty in self.file_list: - current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) - current_file.remove_all() - current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) - self.assertTrue(current_file.is_clean()) - - def test_remove_fileformat_specific_options(self): - """ test metadata removal with fileformat-specific options """ - for _, dirty in self.file_list: # can't be faster than that :/ - if dirty.endswith('pdf'): - current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) - current_file.remove_all() - current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) - self.assertTrue(current_file.is_clean()) - - def test_remove_empty(self): - """Test removal with clean files""" - for clean, _ in self.file_list: - current_file = libmat.mat.create_class_file(clean, False, add2archive=True) - current_file.remove_all() - current_file = libmat.mat.create_class_file(clean, False, add2archive=True) - self.assertTrue(current_file.is_clean()) - - -class TestListlib(test.MATTest): - """ test the get_meta() method - """ - - def test_list(self): - """check if get_meta returns metadata""" - for _, dirty in self.file_list: - current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) - self.assertIsNotNone(current_file.get_meta()) - - def testlist_list_empty(self): - """check that a listing of a clean file returns an empty dict""" - for clean, _ in self.file_list: - current_file = libmat.mat.create_class_file(clean, False, add2archive=True) - self.assertEqual(current_file.get_meta(), dict()) - - -class TestisCleanlib(test.MATTest): - """ Test the is_clean() method - """ - - def test_dirty(self): - """test is_clean on dirty files""" - for _, dirty in self.file_list: - current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) - self.assertFalse(current_file.is_clean()) - - def test_clean(self): - """test is_clean on clean files""" - for clean, _ in self.file_list: - current_file = libmat.mat.create_class_file(clean, False, add2archive=True) - self.assertTrue(current_file.is_clean()) - - -class TestFileAttributes(unittest.TestCase): - """ - test various stuffs about files (readable, writable, exist, ...) - """ - - def test_not_exist(self): - """ test MAT's behaviour on non-existent file""" - self.assertFalse(libmat.mat.create_class_file('non_existent_file', False, add2archive=True)) - - def test_empty(self): - """ test MAT's behaviour on empty file""" - open('empty_file', 'a').close() - self.assertFalse(libmat.mat.create_class_file('empty_file', False, add2archive=True)) - os.remove('empty_file') - - def test_not_writtable(self): - """ test MAT's behaviour on non-writable file""" - self.assertFalse(libmat.mat.create_class_file('not_writtable', False, add2archive=True)) - - def test_not_readable(self): - """ test MAT's behaviour on non-readable file""" - open('non_readable', 'a').close() - os.chmod('non_readable', 0 | stat.S_IWRITE) - self.assertFalse(libmat.mat.create_class_file('non_readable', False, add2archive=True)) - os.remove('non_readable') - - -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(libmat.mat.secure_remove(file_to_remove)) - - def test_remove_fail(self): - """ test the secure removal of an non-removable file - """ - self.assertRaises(libmat.exceptions.UnableToWriteFile, libmat.mat.secure_remove, '/NOTREMOVABLE') - - -class TestArchiveProcessing(test.MATTest): - """ Test archives processing - """ - - def test_remove_bz2(self): - """ Test MAT's ability to process .tar.bz2 - """ - tarpath = os.path.join(self.tmpdir, "test.tar.bz2") - tar = tarfile.open(tarpath, "w:bz2") - for clean, dirty in self.file_list: - tar.add(dirty) - tar.add(clean) - tar.close() - current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) - current_file.remove_all() - current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) - self.assertTrue(current_file.is_clean()) - - def test_remove_tar(self): - """ Test MAT on tar files - """ - tarpath = os.path.join(self.tmpdir, "test.tar") - tar = tarfile.open(tarpath, "w") - for clean, dirty in self.file_list: - tar.add(dirty) - tar.add(clean) - tar.close() - current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) - current_file.remove_all() - current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) - self.assertTrue(current_file.is_clean()) - - def test_remove_gz(self): - """ Test MAT on tar.gz files - """ - tarpath = os.path.join(self.tmpdir, "test.tar.gz") - tar = tarfile.open(tarpath, "w") - for clean, dirty in self.file_list: - tar.add(dirty) - tar.add(clean) - tar.close() - current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) - current_file.remove_all() - current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) - self.assertTrue(current_file.is_clean()) - - def test_get_unsupported(self): - """ Test the get_unsupported feature, used by the GUI - """ - tarpath = os.path.join(self.tmpdir, "test.tar.bz2") - tar = tarfile.open(tarpath, "w") - for f in ('libtest.py', 'test.py', 'clitest.py'): - tar.add(f, f) - tar.close() - current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) - unsupported_files = set(current_file.is_clean(list_unsupported=True)) - self.assertEqual(unsupported_files, {'libtest.py', 'test.py', 'clitest.py'}) - - def test_archive_unwritable_content(self): - path = os.path.join(self.tmpdir, './unwritable_content.zip') - shutil.copy2('./unwritable_content.zip', self.tmpdir) - current_file = libmat.mat.create_class_file(path, False, add2archive=False) - current_file.remove_all() - current_file = libmat.mat.create_class_file(path, False, add2archive=False) - self.assertTrue(current_file.is_clean()) - - -def get_tests(): - """ Returns every libtests""" - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(TestRemovelib)) - suite.addTest(unittest.makeSuite(TestListlib)) - suite.addTest(unittest.makeSuite(TestisCleanlib)) - suite.addTest(unittest.makeSuite(TestFileAttributes)) - suite.addTest(unittest.makeSuite(TestSecureRemove)) - suite.addTest(unittest.makeSuite(TestArchiveProcessing)) - return suite diff --git a/test/test.py b/test/test.py index 58e7ec7..818909e 100644 --- a/test/test.py +++ b/test/test.py @@ -85,11 +85,11 @@ def run_all_tests(): the one used to spawn the `mat` process, and the one for Python import) in the main function. """ - import clitest - import libtest + import test_cli + import test_lib suite = unittest.TestSuite() - suite.addTests(clitest.get_tests()) - suite.addTests(libtest.get_tests()) + suite.addTests(test_cli.get_tests()) + suite.addTests(test_lib.get_tests()) return unittest.TextTestRunner(verbosity=VERBOSITY).run(suite).wasSuccessful() diff --git a/test/test_cli.py b/test/test_cli.py new file mode 100644 index 0000000..9f682b9 --- /dev/null +++ b/test/test_cli.py @@ -0,0 +1,178 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +""" + Unit test for the CLI interface +""" + +import os +import unittest +import subprocess +import sys +import tarfile +import stat + +import test +from libmat import mat + + +class TestRemovecli(test.MATTest): + """ + test if cli correctly remove metadatas + """ + + def test_remove(self): + """make sure that the cli remove all compromizing meta""" + for _, dirty in self.file_list: + subprocess.call(['mat', '--add2archive', dirty]) + current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) + self.assertTrue(current_file.is_clean()) + + def test_remove_fileformat_specific_options(self): + """ test metadata removal with fileformat-specific options """ + for _, dirty in self.file_list: # can't be faster than that :/ + if dirty.endswith('pdf'): + subprocess.call(['mat', '--low-pdf-quality', dirty]) + current_file = mat.create_class_file(dirty, False, low_pdf_quality=True) + self.assertTrue(current_file.is_clean()) + + def test_remove_empty(self): + """Test removal with clean files\n""" + for clean, _ in self.file_list: + subprocess.call(['mat', '--add2archive', clean]) + current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True) + self.assertTrue(current_file.is_clean()) + + +class TestListcli(test.MATTest): + """ + test if cli correctly display metadatas + """ + + def test_list_clean(self): + """check if get_meta returns meta""" + for clean, _ in self.file_list: + proc = subprocess.Popen(['mat', '-d', clean], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertEqual(str(stdout).strip('\n'), "[+] File %s \ +:\nNo harmful metadata found" % clean) + + def test_list_dirty(self): + """check if get_meta returns all the expected meta""" + for _, dirty in self.file_list: + proc = subprocess.Popen(['mat', '-d', dirty], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertNotEqual(str(stdout), "[+] File %s :\n No\ +harmul metadata found" % dirty) + + +class TestisCleancli(test.MATTest): + """ + check if cli correctly check if a file is clean or not + """ + + def test_clean(self): + """test is_clean on clean files""" + for clean, _ in self.file_list: + proc = subprocess.Popen(['mat', '-c', clean], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertEqual(str(stdout).strip('\n'), '[+] %s is clean' % clean) + + def test_dirty(self): + """test is_clean on dirty files""" + for _, dirty in self.file_list: + proc = subprocess.Popen(['mat', '-c', dirty], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertEqual(str(stdout).strip('\n'), '[+] %s is not clean' % dirty) + + +class TestFileAttributes(unittest.TestCase): + """ + test various stuffs about files (readable, writable, exist, ...) + """ + + def test_not_writtable(self): + """ test MAT's behaviour on non-writable file""" + proc = subprocess.Popen(['mat', 'not_writtable'], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process not_writtable') + + def test_not_exist(self): + """ test MAT's behaviour on non-existent file""" + proc = subprocess.Popen(['mat', 'ilikecookies'], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process ilikecookies') + + def test_empty(self): + """ test MAT's behaviour on empty file""" + proc = subprocess.Popen(['mat', 'empty_file'], stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process empty_file') + + def test_not_readable(self): + """ test MAT's behaviour on non-writable file""" + open('non_readable', 'a').close() + os.chmod('non_readable', 0 & stat.S_IWRITE) + proc = subprocess.Popen(['mat', 'non_readable'], stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + os.remove('non_readable') + + +class TestUnsupported(test.MATTest): + """ test MAT's behaviour on unsupported files """ + def test_abort_unsupported(self): + """ test if the cli aborts on unsupported files + """ + tarpath = os.path.join(self.tmpdir, "test.tar.bz2") + tar = tarfile.open(tarpath, "w") + for f in ('test_lib.py', 'test.py', 'test_cli.py'): + tar.add(f, f) + tar.close() + proc = subprocess.Popen(['mat', tarpath], stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertTrue('It contains unsupported filetypes:' + '\n- test_lib.py\n- test.py\n- test_cli.py\n' + in str(stdout)) + + +class TestHelp(test.MATTest): + """ Test the different ways to trigger help """ + def test_dash_h(self): + """ test help invocation with `-h` and `--help` """ + proc = subprocess.Popen(['mat', '-h'], stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertTrue('show this help message and exit' in stdout) + + proc = subprocess.Popen(['mat', '--help'], stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertTrue('show this help message and exit' in stdout) + + def test_no_argument(self): + """ test help invocation when no argument is provided """ + proc = subprocess.Popen(['mat'], stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertTrue('show this help message and exit' in stdout) + + def test_wrong_argument(self): + """ Test MAT's behaviour on wrong argument """ + proc = subprocess.Popen(['mat', '--obviously-wrong-argument'], stderr=subprocess.PIPE) + _, stderr = proc.communicate() + self.assertTrue(('usage: mat [-h]' and ' error: unrecognized arguments:') in stderr) + + +def get_tests(): + """ Return every clitests""" + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(TestRemovecli)) + suite.addTest(unittest.makeSuite(TestListcli)) + suite.addTest(unittest.makeSuite(TestisCleancli)) + suite.addTest(unittest.makeSuite(TestUnsupported)) + suite.addTest(unittest.makeSuite(TestFileAttributes)) + suite.addTest(unittest.makeSuite(TestHelp)) + return suite diff --git a/test/test_lib.py b/test/test_lib.py new file mode 100644 index 0000000..33b3a02 --- /dev/null +++ b/test/test_lib.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +""" + Unit test for the library +""" + +import os +import sys +import stat +import shutil +import tarfile +import tempfile +import unittest + +import test +import libmat + + +class TestRemovelib(test.MATTest): + """ test the remove_all() method + """ + + def test_remove(self): + """make sure that the lib remove all compromizing meta""" + for _, dirty in self.file_list: + current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) + current_file.remove_all() + current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) + self.assertTrue(current_file.is_clean()) + + def test_remove_fileformat_specific_options(self): + """ test metadata removal with fileformat-specific options """ + for _, dirty in self.file_list: # can't be faster than that :/ + if dirty.endswith('pdf'): + current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) + current_file.remove_all() + current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) + self.assertTrue(current_file.is_clean()) + + def test_remove_empty(self): + """Test removal with clean files""" + for clean, _ in self.file_list: + current_file = libmat.mat.create_class_file(clean, False, add2archive=True) + current_file.remove_all() + current_file = libmat.mat.create_class_file(clean, False, add2archive=True) + self.assertTrue(current_file.is_clean()) + + +class TestListlib(test.MATTest): + """ test the get_meta() method + """ + + def test_list(self): + """check if get_meta returns metadata""" + for _, dirty in self.file_list: + current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) + self.assertIsNotNone(current_file.get_meta()) + + def testlist_list_empty(self): + """check that a listing of a clean file returns an empty dict""" + for clean, _ in self.file_list: + current_file = libmat.mat.create_class_file(clean, False, add2archive=True) + self.assertEqual(current_file.get_meta(), dict()) + + +class TestisCleanlib(test.MATTest): + """ Test the is_clean() method + """ + + def test_dirty(self): + """test is_clean on dirty files""" + for _, dirty in self.file_list: + current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) + self.assertFalse(current_file.is_clean()) + + def test_clean(self): + """test is_clean on clean files""" + for clean, _ in self.file_list: + current_file = libmat.mat.create_class_file(clean, False, add2archive=True) + self.assertTrue(current_file.is_clean()) + + +class TestFileAttributes(unittest.TestCase): + """ + test various stuffs about files (readable, writable, exist, ...) + """ + + def test_not_exist(self): + """ test MAT's behaviour on non-existent file""" + self.assertFalse(libmat.mat.create_class_file('non_existent_file', False, add2archive=True)) + + def test_empty(self): + """ test MAT's behaviour on empty file""" + open('empty_file', 'a').close() + self.assertFalse(libmat.mat.create_class_file('empty_file', False, add2archive=True)) + os.remove('empty_file') + + def test_not_writtable(self): + """ test MAT's behaviour on non-writable file""" + self.assertFalse(libmat.mat.create_class_file('not_writtable', False, add2archive=True)) + + def test_not_readable(self): + """ test MAT's behaviour on non-readable file""" + open('non_readable', 'a').close() + os.chmod('non_readable', 0 | stat.S_IWRITE) + self.assertFalse(libmat.mat.create_class_file('non_readable', False, add2archive=True)) + os.remove('non_readable') + + +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(libmat.mat.secure_remove(file_to_remove)) + + def test_remove_fail(self): + """ test the secure removal of an non-removable file + """ + self.assertRaises(libmat.exceptions.UnableToWriteFile, libmat.mat.secure_remove, '/NOTREMOVABLE') + + +class TestArchiveProcessing(test.MATTest): + """ Test archives processing + """ + + def test_remove_bz2(self): + """ Test MAT's ability to process .tar.bz2 + """ + tarpath = os.path.join(self.tmpdir, "test.tar.bz2") + tar = tarfile.open(tarpath, "w:bz2") + for clean, dirty in self.file_list: + tar.add(dirty) + tar.add(clean) + tar.close() + current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) + current_file.remove_all() + current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) + self.assertTrue(current_file.is_clean()) + + def test_remove_tar(self): + """ Test MAT on tar files + """ + tarpath = os.path.join(self.tmpdir, "test.tar") + tar = tarfile.open(tarpath, "w") + for clean, dirty in self.file_list: + tar.add(dirty) + tar.add(clean) + tar.close() + current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) + current_file.remove_all() + current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) + self.assertTrue(current_file.is_clean()) + + def test_remove_gz(self): + """ Test MAT on tar.gz files + """ + tarpath = os.path.join(self.tmpdir, "test.tar.gz") + tar = tarfile.open(tarpath, "w") + for clean, dirty in self.file_list: + tar.add(dirty) + tar.add(clean) + tar.close() + current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) + current_file.remove_all() + current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) + self.assertTrue(current_file.is_clean()) + + def test_get_unsupported(self): + """ Test the get_unsupported feature, used by the GUI + """ + tarpath = os.path.join(self.tmpdir, "test.tar.bz2") + tar = tarfile.open(tarpath, "w") + for f in ('test_lib.py', 'test.py', 'test_cli.py'): + tar.add(f, f) + tar.close() + current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) + unsupported_files = set(current_file.is_clean(list_unsupported=True)) + self.assertEqual(unsupported_files, {'test_lib.py', 'test.py', 'test_cli.py'}) + + def test_archive_unwritable_content(self): + path = os.path.join(self.tmpdir, './unwritable_content.zip') + shutil.copy2('./unwritable_content.zip', self.tmpdir) + current_file = libmat.mat.create_class_file(path, False, add2archive=False) + current_file.remove_all() + current_file = libmat.mat.create_class_file(path, False, add2archive=False) + self.assertTrue(current_file.is_clean()) + + +def get_tests(): + """ Returns every libtests""" + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(TestRemovelib)) + suite.addTest(unittest.makeSuite(TestListlib)) + suite.addTest(unittest.makeSuite(TestisCleanlib)) + suite.addTest(unittest.makeSuite(TestFileAttributes)) + suite.addTest(unittest.makeSuite(TestSecureRemove)) + suite.addTest(unittest.makeSuite(TestArchiveProcessing)) + return suite -- cgit v1.3