1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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 ('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
|