test_libmat2.py 32.5 KB
Newer Older
1
#!/usr/bin/env python3
jvoisin's avatar
jvoisin committed
2
3
4
5

import unittest
import shutil
import os
jvoisin's avatar
jvoisin committed
6
import re
jvoisin's avatar
jvoisin committed
7
8
import tarfile
import tempfile
9
import zipfile
jvoisin's avatar
jvoisin committed
10

jvoisin's avatar
jvoisin committed
11
from libmat2 import pdf, images, audio, office, parser_factory, torrent, harmless
jvoisin's avatar
jvoisin committed
12
from libmat2 import check_dependencies, video, archive, web, epub
13
14
15
16


class TestCheckDependencies(unittest.TestCase):
    def test_deps(self):
17
        ret = check_dependencies()
18
19
        for key, value in ret.items():
            self.assertTrue(value, "The value for %s is False" % key)
jvoisin's avatar
jvoisin committed
20

21

22
23
24
25
26
27
28
class TestParserFactory(unittest.TestCase):
    def test_subsubcalss(self):
        """ Test that our module auto-detection is handling sub-sub-classes """
        parser, mimetype = parser_factory.get_parser('./tests/data/dirty.mp3')
        self.assertEqual(mimetype, 'audio/mpeg')
        self.assertEqual(parser.__class__, audio.MP3Parser)

jvoisin's avatar
jvoisin committed
29
30
31
32
33
34
35
36
    def test_tarfile_double_extension_handling(self):
        """ Test that our module auto-detection is handling sub-sub-classes """
        with tarfile.TarFile.open('./tests/data/dirty.tar.bz2', 'w:bz2') as zout:
            zout.add('./tests/data/dirty.jpg')
        parser, mimetype = parser_factory.get_parser('./tests/data/dirty.tar.bz2')
        self.assertEqual(mimetype, 'application/x-tar+bz2')
        os.remove('./tests/data/dirty.tar.bz2')

jvoisin's avatar
jvoisin committed
37

38
39
40
41
42
43
44
45
46
class TestParameterInjection(unittest.TestCase):
    def test_ver_injection(self):
        shutil.copy('./tests/data/dirty.png', './-ver')
        p = images.PNGParser('-ver')
        meta = p.get_meta()
        self.assertEqual(meta['Comment'], 'This is a comment, be careful!')
        self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25")
        os.remove('-ver')

47
48
49
50
51
52
53
54
55
56
57
58
    def test_ffmpeg_injection(self):
        try:
            video._get_ffmpeg_path()
        except RuntimeError:
            raise unittest.SkipTest

        shutil.copy('./tests/data/dirty.avi', './--output')
        p = video.AVIParser('--output')
        meta = p.get_meta()
        self.assertEqual(meta['Software'], 'MEncoder SVN-r33148-4.0.1')
        os.remove('--output')

59
60
61
62
63
64
65
66
67
68
69
70
71
72
    def test_ffmpeg_injection_complete_path(self):
        try:
            video._get_ffmpeg_path()
        except RuntimeError:
            raise unittest.SkipTest

        shutil.copy('./tests/data/dirty.avi', './tests/data/ --output.avi')
        p = video.AVIParser('./tests/data/ --output.avi')
        meta = p.get_meta()
        self.assertEqual(meta['Software'], 'MEncoder SVN-r33148-4.0.1')
        self.assertTrue(p.remove_all())
        os.remove('./tests/data/ --output.avi')
        os.remove('./tests/data/ --output.cleaned.avi')

73

74
75
76
77
78
79
80
81
82
83
84
85
86
class TestUnsupportedEmbeddedFiles(unittest.TestCase):
    def test_odt_with_svg(self):
        shutil.copy('./tests/data/embedded.odt', './tests/data/clean.odt')
        p = office.LibreOfficeParser('./tests/data/clean.odt')
        self.assertFalse(p.remove_all())
        os.remove('./tests/data/clean.odt')

    def test_docx_with_svg(self):
        shutil.copy('./tests/data/embedded.docx', './tests/data/clean.docx')
        p = office.MSOfficeParser('./tests/data/clean.docx')
        self.assertFalse(p.remove_all())
        os.remove('./tests/data/clean.docx')

87

jvoisin's avatar
jvoisin committed
88
89
90
class TestGetMeta(unittest.TestCase):
    def test_pdf(self):
        p = pdf.PDFParser('./tests/data/dirty.pdf')
jvoisin's avatar
jvoisin committed
91
92
93
        meta = p.get_meta()
        self.assertEqual(meta['producer'], 'pdfTeX-1.40.14')
        self.assertEqual(meta['creator'], "'Certified by IEEE PDFeXpress at 03/19/2016 2:56:07 AM'")
94
        self.assertEqual(meta['DocumentID'], "uuid:4a1a79c8-404e-4d38-9580-5bc081036e61")
jvoisin's avatar
jvoisin committed
95
96
        self.assertEqual(meta['PTEX.Fullbanner'], "This is pdfTeX, Version "
                "3.1415926-2.5-1.40.14 (TeX Live 2013/Debian) kpathsea "
97
                "version 6.1.1")
jvoisin's avatar
jvoisin committed
98

99
100
101
102
103
    def test_torrent(self):
        p = torrent.TorrentParser('./tests/data/dirty.torrent')
        meta = p.get_meta()
        self.assertEqual(meta['created by'], b'mktorrent 1.0')

jvoisin's avatar
jvoisin committed
104
    def test_png(self):
105
        p = images.PNGParser('./tests/data/dirty.png')
jvoisin's avatar
jvoisin committed
106
107
108
        meta = p.get_meta()
        self.assertEqual(meta['Comment'], 'This is a comment, be careful!')
        self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25")
jvoisin's avatar
jvoisin committed
109

jvoisin's avatar
jvoisin committed
110
    def test_jpg(self):
111
        p = images.JPGParser('./tests/data/dirty.jpg')
jvoisin's avatar
jvoisin committed
112
113
114
        meta = p.get_meta()
        self.assertEqual(meta['Comment'], 'Created with GIMP')

jvoisin's avatar
jvoisin committed
115
    def test_tiff(self):
116
        p = images.TiffParser('./tests/data/dirty.tiff')
jvoisin's avatar
jvoisin committed
117
118
119
120
121
        meta = p.get_meta()
        self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.')
        self.assertEqual(meta['Model'], 'C7070WZ')
        self.assertEqual(meta['ModifyDate'], '2005:12:26 17:09:35')

jvoisin's avatar
jvoisin committed
122
123
124
    def test_mp3(self):
        p = audio.MP3Parser('./tests/data/dirty.mp3')
        meta = p.get_meta()
jvoisin's avatar
jvoisin committed
125
        self.assertEqual(meta['TXXX:I am a'], 'various comment')
jvoisin's avatar
jvoisin committed
126
127
128
129

    def test_ogg(self):
        p = audio.OGGParser('./tests/data/dirty.ogg')
        meta = p.get_meta()
jvoisin's avatar
jvoisin committed
130
        self.assertEqual(meta['title'], 'I am so')
jvoisin's avatar
jvoisin committed
131

jvoisin's avatar
jvoisin committed
132
133
134
    def test_flac(self):
        p = audio.FLACParser('./tests/data/dirty.flac')
        meta = p.get_meta()
jvoisin's avatar
jvoisin committed
135
        self.assertEqual(meta['title'], 'I am so')
136
        self.assertEqual(meta['Cover 0'], {'Comment': 'Created with GIMP'})
jvoisin's avatar
jvoisin committed
137

jvoisin's avatar
jvoisin committed
138
    def test_docx(self):
jvoisin's avatar
jvoisin committed
139
        p = office.MSOfficeParser('./tests/data/dirty.docx')
jvoisin's avatar
jvoisin committed
140
        meta = p.get_meta()
141
142
143
        self.assertEqual(meta['docProps/core.xml']['cp:lastModifiedBy'], 'Julien Voisin')
        self.assertEqual(meta['docProps/core.xml']['dc:creator'], 'julien voisin')
        self.assertEqual(meta['docProps/app.xml']['Application'], 'LibreOffice/5.4.5.1$Linux_X86_64 LibreOffice_project/40m0$Build-1')
jvoisin's avatar
jvoisin committed
144

jvoisin's avatar
jvoisin committed
145
    def test_libreoffice(self):
jvoisin's avatar
jvoisin committed
146
        p = office.LibreOfficeParser('./tests/data/dirty.odt')
jvoisin's avatar
jvoisin committed
147
        meta = p.get_meta()
148
149
150
        self.assertEqual(meta['meta.xml']['meta:initial-creator'], 'jvoisin ')
        self.assertEqual(meta['meta.xml']['meta:creation-date'], '2011-07-26T03:27:48')
        self.assertEqual(meta['meta.xml']['meta:generator'], 'LibreOffice/3.3$Unix LibreOffice_project/330m19$Build-202')
jvoisin's avatar
jvoisin committed
151

jvoisin's avatar
jvoisin committed
152
153
        p = office.LibreOfficeParser('./tests/data/weird_producer.odt')
        meta = p.get_meta()
154
155
        self.assertEqual(meta['mimetype']['create_system'], 'Windows')
        self.assertEqual(meta['mimetype']['comment'], b'YAY FOR COMMENTS')
jvoisin's avatar
jvoisin committed
156

jvoisin's avatar
jvoisin committed
157
158
159
160
161
162
    def test_txt(self):
        p, mimetype = parser_factory.get_parser('./tests/data/dirty.txt')
        self.assertEqual(mimetype, 'text/plain')
        meta = p.get_meta()
        self.assertEqual(meta, {})

jvoisin's avatar
jvoisin committed
163
164
165
166
167
168
169
170
171
172
173
174
    def test_zip(self):
        with zipfile.ZipFile('./tests/data/dirty.zip', 'w') as zout:
            zout.write('./tests/data/dirty.flac')
            zout.write('./tests/data/dirty.docx')
            zout.write('./tests/data/dirty.jpg')
        p, mimetype = parser_factory.get_parser('./tests/data/dirty.zip')
        self.assertEqual(mimetype, 'application/zip')
        meta = p.get_meta()
        self.assertEqual(meta['tests/data/dirty.flac']['comments'], 'Thank you for using MAT !')
        self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')
        os.remove('./tests/data/dirty.zip')

jvoisin's avatar
jvoisin committed
175
176
177
178
179
    def test_wmv(self):
        p, mimetype = parser_factory.get_parser('./tests/data/dirty.wmv')
        self.assertEqual(mimetype, 'video/x-ms-wmv')
        meta = p.get_meta()
        self.assertEqual(meta['EncodingSettings'], 'Lavf52.103.0')
jvoisin's avatar
jvoisin committed
180

jvoisin's avatar
jvoisin committed
181
182
183
184
185
186
    def test_gif(self):
        p, mimetype = parser_factory.get_parser('./tests/data/dirty.gif')
        self.assertEqual(mimetype, 'image/gif')
        meta = p.get_meta()
        self.assertEqual(meta['Comment'], 'this is a test comment')

jvoisin's avatar
jvoisin committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    def test_epub(self):
        p, mimetype = parser_factory.get_parser('./tests/data/dirty.epub')
        self.assertEqual(mimetype, 'application/epub+zip')
        meta = p.get_meta()
        self.assertEqual(meta['OEBPS/content.opf']['dc:creator'], 'Dorothy L. Sayers')
        self.assertEqual(meta['OEBPS/toc.ncx']['dtb:generator'], 'Ebookmaker 0.4.0a5 by Marcello Perathoner <webmaster@gutenberg.org>')
        self.assertEqual(meta['OEBPS/@public@vhost@g@gutenberg@html@files@58820@58820-h@images@shield25.jpg']['CreatorTool'], 'Adobe Photoshop CS5 Macintosh')
        self.assertEqual(meta['OEBPS/@public@vhost@g@gutenberg@html@files@58820@58820-h@58820-h-2.htm.html']['generator'], 'Ebookmaker 0.4.0a5 by Marcello Perathoner <webmaster@gutenberg.org>')

    def test_css(self):
        p, mimetype = parser_factory.get_parser('./tests/data/dirty.css')
        self.assertEqual(mimetype, 'text/css')
        meta = p.get_meta()
        self.assertEqual(meta['author'], 'jvoisin')
        self.assertEqual(meta['version'], '1.0')
        self.assertEqual(meta['harmful data'], 'underline is cool')

jvoisin's avatar
jvoisin committed
204
205
206
207
208
209
210
211
212
213
214
215
216
    def test_tar(self):
        with tarfile.TarFile('./tests/data/dirty.tar', 'w') as tout:
            tout.add('./tests/data/dirty.flac')
            tout.add('./tests/data/dirty.docx')
            tout.add('./tests/data/dirty.jpg')
        p, mimetype = parser_factory.get_parser('./tests/data/dirty.tar')
        self.assertEqual(mimetype, 'application/x-tar')
        meta = p.get_meta()
        self.assertEqual(meta['./tests/data/dirty.flac']['comments'], 'Thank you for using MAT !')
        self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')
        os.remove('./tests/data/dirty.tar')


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
class TestRemovingThumbnails(unittest.TestCase):
    def test_odt(self):
        shutil.copy('./tests/data/revision.odt', './tests/data/clean.odt')

        zipin = zipfile.ZipFile(os.path.abspath('./tests/data/clean.odt'))
        self.assertIn('Thumbnails/thumbnail.png', zipin.namelist())
        zipin.close()

        p = office.LibreOfficeParser('./tests/data/clean.odt')
        self.assertTrue(p.remove_all())

        zipin = zipfile.ZipFile(os.path.abspath('./tests/data/clean.cleaned.odt'))
        self.assertNotIn('Thumbnails/thumbnail.png', zipin.namelist())
        zipin.close()

        os.remove('./tests/data/clean.cleaned.odt')
jvoisin's avatar
jvoisin committed
233
        os.remove('./tests/data/clean.odt')
234

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

class TestRevisionsCleaning(unittest.TestCase):
    def test_libreoffice(self):
        with zipfile.ZipFile('./tests/data/revision.odt') as zipin:
            c = zipin.open('content.xml')
            r = c.read()
            self.assertIn(b'tracked-changes', r)

        shutil.copy('./tests/data/revision.odt', './tests/data/clean.odt')
        p = office.LibreOfficeParser('./tests/data/clean.odt')
        self.assertTrue(p.remove_all())

        with zipfile.ZipFile('./tests/data/clean.cleaned.odt') as zipin:
            c = zipin.open('content.xml')
            r = c.read()
            self.assertNotIn(b'tracked-changes', r)

        os.remove('./tests/data/clean.odt')
        os.remove('./tests/data/clean.cleaned.odt')

jvoisin's avatar
jvoisin committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    def test_msoffice(self):
        with zipfile.ZipFile('./tests/data/revision.docx') as zipin:
            c = zipin.open('word/document.xml')
            content = c.read()
            r = b'<w:ins w:id="1" w:author="Unknown Author" w:date="2018-06-28T23:48:00Z">'
            self.assertIn(r, content)

        shutil.copy('./tests/data/revision.docx', './tests/data/revision_clean.docx')
        p = office.MSOfficeParser('./tests/data/revision_clean.docx')
        self.assertTrue(p.remove_all())

        with zipfile.ZipFile('./tests/data/revision_clean.cleaned.docx') as zipin:
            c = zipin.open('word/document.xml')
            content = c.read()
            r = b'<w:ins w:id="1" w:author="Unknown Author" w:date="2018-06-28T23:48:00Z">'
            self.assertNotIn(r, content)

        os.remove('./tests/data/revision_clean.docx')
        os.remove('./tests/data/revision_clean.cleaned.docx')

jvoisin's avatar
jvoisin committed
275
class TestCleaning(unittest.TestCase):
jvoisin's avatar
jvoisin committed
276
    def test_pdf(self):
jvoisin's avatar
jvoisin committed
277
        shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf')
jvoisin's avatar
jvoisin committed
278
        p = pdf.PDFParser('./tests/data/clean.pdf')
jvoisin's avatar
jvoisin committed
279
280
281
282
283
284
285

        meta = p.get_meta()
        self.assertEqual(meta['producer'], 'pdfTeX-1.40.14')

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
286
        p = pdf.PDFParser('./tests/data/clean.cleaned.pdf')
jvoisin's avatar
jvoisin committed
287
288
        expected_meta = {'creation-date': -1, 'format': 'PDF-1.5', 'mod-date': -1}
        self.assertEqual(p.get_meta(), expected_meta)
289
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
290
291

        os.remove('./tests/data/clean.pdf')
jvoisin's avatar
jvoisin committed
292
        os.remove('./tests/data/clean.cleaned.pdf')
293
        os.remove('./tests/data/clean.cleaned.cleaned.pdf')
jvoisin's avatar
jvoisin committed
294
295
296

    def test_png(self):
        shutil.copy('./tests/data/dirty.png', './tests/data/clean.png')
297
        p = images.PNGParser('./tests/data/clean.png')
jvoisin's avatar
jvoisin committed
298
299
300
301
302
303
304

        meta = p.get_meta()
        self.assertEqual(meta['Comment'], 'This is a comment, be careful!')

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
305
        p = images.PNGParser('./tests/data/clean.cleaned.png')
jvoisin's avatar
jvoisin committed
306
        self.assertEqual(p.get_meta(), {})
307
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
308
309

        os.remove('./tests/data/clean.png')
jvoisin's avatar
jvoisin committed
310
        os.remove('./tests/data/clean.cleaned.png')
311
        os.remove('./tests/data/clean.cleaned.cleaned.png')
jvoisin's avatar
jvoisin committed
312
313
314

    def test_jpg(self):
        shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
315
        p = images.JPGParser('./tests/data/clean.jpg')
jvoisin's avatar
jvoisin committed
316
317
318
319
320
321
322

        meta = p.get_meta()
        self.assertEqual(meta['Comment'], 'Created with GIMP')

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
323
        p = images.JPGParser('./tests/data/clean.cleaned.jpg')
jvoisin's avatar
jvoisin committed
324
        self.assertEqual(p.get_meta(), {})
325
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
326
327

        os.remove('./tests/data/clean.jpg')
jvoisin's avatar
jvoisin committed
328
        os.remove('./tests/data/clean.cleaned.jpg')
329
        os.remove('./tests/data/clean.cleaned.cleaned.jpg')
jvoisin's avatar
jvoisin committed
330
331
332
333
334
335

    def test_mp3(self):
        shutil.copy('./tests/data/dirty.mp3', './tests/data/clean.mp3')
        p = audio.MP3Parser('./tests/data/clean.mp3')

        meta = p.get_meta()
jvoisin's avatar
jvoisin committed
336
        self.assertEqual(meta['TXXX:I am a'], 'various comment')
jvoisin's avatar
jvoisin committed
337
338
339
340

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
341
        p = audio.MP3Parser('./tests/data/clean.cleaned.mp3')
jvoisin's avatar
jvoisin committed
342
        self.assertEqual(p.get_meta(), {})
343
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
344
345

        os.remove('./tests/data/clean.mp3')
jvoisin's avatar
jvoisin committed
346
        os.remove('./tests/data/clean.cleaned.mp3')
347
        os.remove('./tests/data/clean.cleaned.cleaned.mp3')
jvoisin's avatar
jvoisin committed
348
349
350
351
352
353

    def test_ogg(self):
        shutil.copy('./tests/data/dirty.ogg', './tests/data/clean.ogg')
        p = audio.OGGParser('./tests/data/clean.ogg')

        meta = p.get_meta()
jvoisin's avatar
jvoisin committed
354
        self.assertEqual(meta['title'], 'I am so')
jvoisin's avatar
jvoisin committed
355
356
357
358

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
359
        p = audio.OGGParser('./tests/data/clean.cleaned.ogg')
jvoisin's avatar
jvoisin committed
360
        self.assertEqual(p.get_meta(), {})
361
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
362
363

        os.remove('./tests/data/clean.ogg')
jvoisin's avatar
jvoisin committed
364
        os.remove('./tests/data/clean.cleaned.ogg')
365
        os.remove('./tests/data/clean.cleaned.cleaned.ogg')
jvoisin's avatar
jvoisin committed
366
367
368
369
370
371

    def test_flac(self):
        shutil.copy('./tests/data/dirty.flac', './tests/data/clean.flac')
        p = audio.FLACParser('./tests/data/clean.flac')

        meta = p.get_meta()
jvoisin's avatar
jvoisin committed
372
        self.assertEqual(meta['title'], 'I am so')
jvoisin's avatar
jvoisin committed
373
374
375
376

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
377
        p = audio.FLACParser('./tests/data/clean.cleaned.flac')
jvoisin's avatar
jvoisin committed
378
        self.assertEqual(p.get_meta(), {})
379
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
380
381

        os.remove('./tests/data/clean.flac')
jvoisin's avatar
jvoisin committed
382
        os.remove('./tests/data/clean.cleaned.flac')
383
        os.remove('./tests/data/clean.cleaned.cleaned.flac')
jvoisin's avatar
jvoisin committed
384
385
386

    def test_office(self):
        shutil.copy('./tests/data/dirty.docx', './tests/data/clean.docx')
jvoisin's avatar
jvoisin committed
387
        p = office.MSOfficeParser('./tests/data/clean.docx')
jvoisin's avatar
jvoisin committed
388
389
390
391
392
393
394

        meta = p.get_meta()
        self.assertIsNotNone(meta)

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
395
        p = office.MSOfficeParser('./tests/data/clean.cleaned.docx')
jvoisin's avatar
jvoisin committed
396
        self.assertEqual(p.get_meta(), {})
397
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
398
399

        os.remove('./tests/data/clean.docx')
jvoisin's avatar
jvoisin committed
400
        os.remove('./tests/data/clean.cleaned.docx')
401
        os.remove('./tests/data/clean.cleaned.cleaned.docx')
jvoisin's avatar
jvoisin committed
402
403
404

    def test_libreoffice(self):
        shutil.copy('./tests/data/dirty.odt', './tests/data/clean.odt')
jvoisin's avatar
jvoisin committed
405
        p = office.LibreOfficeParser('./tests/data/clean.odt')
jvoisin's avatar
jvoisin committed
406
407
408
409
410
411
412

        meta = p.get_meta()
        self.assertIsNotNone(meta)

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
413
        p = office.LibreOfficeParser('./tests/data/clean.cleaned.odt')
jvoisin's avatar
jvoisin committed
414
        self.assertEqual(p.get_meta(), {})
415
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
416
417

        os.remove('./tests/data/clean.odt')
jvoisin's avatar
jvoisin committed
418
        os.remove('./tests/data/clean.cleaned.odt')
419
        os.remove('./tests/data/clean.cleaned.cleaned.odt')
jvoisin's avatar
jvoisin committed
420
421
422

    def test_tiff(self):
        shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff')
423
        p = images.TiffParser('./tests/data/clean.tiff')
jvoisin's avatar
jvoisin committed
424
425
426
427
428
429
430

        meta = p.get_meta()
        self.assertEqual(meta['Model'], 'C7070WZ')

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
431
        p = images.TiffParser('./tests/data/clean.cleaned.tiff')
jvoisin's avatar
jvoisin committed
432
        self.assertEqual(p.get_meta(), {})
433
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
434
435

        os.remove('./tests/data/clean.tiff')
jvoisin's avatar
jvoisin committed
436
        os.remove('./tests/data/clean.cleaned.tiff')
437
        os.remove('./tests/data/clean.cleaned.cleaned.tiff')
jvoisin's avatar
jvoisin committed
438
439
440

    def test_bmp(self):
        shutil.copy('./tests/data/dirty.bmp', './tests/data/clean.bmp')
jvoisin's avatar
jvoisin committed
441
        p = harmless.HarmlessParser('./tests/data/clean.bmp')
jvoisin's avatar
jvoisin committed
442
443
444
445
446
447
448

        meta = p.get_meta()
        self.assertEqual(meta, {})  # bmp has no meta :)

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
449
        p = harmless.HarmlessParser('./tests/data/clean.cleaned.bmp')
jvoisin's avatar
jvoisin committed
450
        self.assertEqual(p.get_meta(), {})
451
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
452
453

        os.remove('./tests/data/clean.bmp')
jvoisin's avatar
jvoisin committed
454
        os.remove('./tests/data/clean.cleaned.bmp')
455
        os.remove('./tests/data/clean.cleaned.cleaned.bmp')
456
457
458
459
460
461
462
463
464
465
466

    def test_torrent(self):
        shutil.copy('./tests/data/dirty.torrent', './tests/data/clean.torrent')
        p = torrent.TorrentParser('./tests/data/clean.torrent')

        meta = p.get_meta()
        self.assertEqual(meta, {'created by': b'mktorrent 1.0', 'creation date': 1522397702})

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
467
        p = torrent.TorrentParser('./tests/data/clean.cleaned.torrent')
468
        self.assertEqual(p.get_meta(), {})
469
        self.assertTrue(p.remove_all())
470
471

        os.remove('./tests/data/clean.torrent')
jvoisin's avatar
jvoisin committed
472
        os.remove('./tests/data/clean.cleaned.torrent')
473
        os.remove('./tests/data/clean.cleaned.cleaned.torrent')
jvoisin's avatar
jvoisin committed
474
475
476
477
478
479

    def test_odf(self):
        shutil.copy('./tests/data/dirty.odf', './tests/data/clean.odf')
        p = office.LibreOfficeParser('./tests/data/clean.odf')

        meta = p.get_meta()
480
        self.assertEqual(meta['meta.xml']['meta:creation-date'], '2018-04-23T00:18:59.438231281')
jvoisin's avatar
jvoisin committed
481
482
483
484

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
485
        p = office.LibreOfficeParser('./tests/data/clean.cleaned.odf')
jvoisin's avatar
jvoisin committed
486
        self.assertEqual(p.get_meta(), {})
487
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
488
489

        os.remove('./tests/data/clean.odf')
jvoisin's avatar
jvoisin committed
490
        os.remove('./tests/data/clean.cleaned.odf')
491
        os.remove('./tests/data/clean.cleaned.cleaned.odf')
jvoisin's avatar
jvoisin committed
492
493
494
495
496
497

    def test_odg(self):
        shutil.copy('./tests/data/dirty.odg', './tests/data/clean.odg')
        p = office.LibreOfficeParser('./tests/data/clean.odg')

        meta = p.get_meta()
498
        self.assertEqual(meta['meta.xml']['dc:date'], '2018-04-23T00:26:59.385838550')
jvoisin's avatar
jvoisin committed
499
500
501
502

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
503
        p = office.LibreOfficeParser('./tests/data/clean.cleaned.odg')
jvoisin's avatar
jvoisin committed
504
        self.assertEqual(p.get_meta(), {})
505
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
506
507

        os.remove('./tests/data/clean.odg')
jvoisin's avatar
jvoisin committed
508
        os.remove('./tests/data/clean.cleaned.odg')
509
        os.remove('./tests/data/clean.cleaned.cleaned.odg')
jvoisin's avatar
jvoisin committed
510
511
512
513
514
515
516
517
518
519
520
521
522

    def test_txt(self):
        shutil.copy('./tests/data/dirty.txt', './tests/data/clean.txt')
        p = harmless.HarmlessParser('./tests/data/clean.txt')

        meta = p.get_meta()
        self.assertEqual(meta, {})

        ret = p.remove_all()
        self.assertTrue(ret)

        p = harmless.HarmlessParser('./tests/data/clean.cleaned.txt')
        self.assertEqual(p.get_meta(), {})
523
        self.assertTrue(p.remove_all())
jvoisin's avatar
jvoisin committed
524
525
526

        os.remove('./tests/data/clean.txt')
        os.remove('./tests/data/clean.cleaned.txt')
527
        os.remove('./tests/data/clean.cleaned.cleaned.txt')
528
529

    def test_avi(self):
530
531
532
533
534
        try:
            video._get_ffmpeg_path()
        except RuntimeError:
            raise unittest.SkipTest

535
536
537
538
539
540
        shutil.copy('./tests/data/dirty.avi', './tests/data/clean.avi')
        p = video.AVIParser('./tests/data/clean.avi')

        meta = p.get_meta()
        self.assertEqual(meta['Software'], 'MEncoder SVN-r33148-4.0.1')

541
        ret = p.remove_all()
542
543
544
545
546
547
548
549
550
        self.assertTrue(ret)

        p = video.AVIParser('./tests/data/clean.cleaned.avi')
        self.assertEqual(p.get_meta(), {})
        self.assertTrue(p.remove_all())

        os.remove('./tests/data/clean.avi')
        os.remove('./tests/data/clean.cleaned.avi')
        os.remove('./tests/data/clean.cleaned.cleaned.avi')
jvoisin's avatar
jvoisin committed
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571

    def test_zip(self):
        with zipfile.ZipFile('./tests/data/dirty.zip', 'w') as zout:
            zout.write('./tests/data/dirty.flac')
            zout.write('./tests/data/dirty.docx')
            zout.write('./tests/data/dirty.jpg')
        p = archive.ZipParser('./tests/data/dirty.zip')
        meta = p.get_meta()
        self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = archive.ZipParser('./tests/data/dirty.cleaned.zip')
        self.assertEqual(p.get_meta(), {})
        self.assertTrue(p.remove_all())

        os.remove('./tests/data/dirty.zip')
        os.remove('./tests/data/dirty.cleaned.zip')
        os.remove('./tests/data/dirty.cleaned.cleaned.zip')

jvoisin's avatar
jvoisin committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594

    def test_mp4(self):
        try:
            video._get_ffmpeg_path()
        except RuntimeError:
            raise unittest.SkipTest

        shutil.copy('./tests/data/dirty.mp4', './tests/data/clean.mp4')
        p = video.MP4Parser('./tests/data/clean.mp4')

        meta = p.get_meta()
        self.assertEqual(meta['Encoder'], 'HandBrake 0.9.4 2009112300')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = video.MP4Parser('./tests/data/clean.cleaned.mp4')
        self.assertNotIn('Encoder', p.get_meta())
        self.assertTrue(p.remove_all())

        os.remove('./tests/data/clean.mp4')
        os.remove('./tests/data/clean.cleaned.mp4')
        os.remove('./tests/data/clean.cleaned.cleaned.mp4')
jvoisin's avatar
jvoisin committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617

    def test_wmv(self):
        try:
            video._get_ffmpeg_path()
        except RuntimeError:
            raise unittest.SkipTest

        shutil.copy('./tests/data/dirty.wmv', './tests/data/clean.wmv')
        p = video.WMVParser('./tests/data/clean.wmv')

        meta = p.get_meta()
        self.assertEqual(meta['EncodingSettings'], 'Lavf52.103.0')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = video.WMVParser('./tests/data/clean.cleaned.wmv')
        self.assertNotIn('EncodingSettings', p.get_meta())
        self.assertTrue(p.remove_all())

        os.remove('./tests/data/clean.wmv')
        os.remove('./tests/data/clean.cleaned.wmv')
        os.remove('./tests/data/clean.cleaned.cleaned.wmv')
jvoisin's avatar
jvoisin committed
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635

    def test_gif(self):
        shutil.copy('./tests/data/dirty.gif', './tests/data/clean.gif')
        p = images.GIFParser('./tests/data/clean.gif')

        meta = p.get_meta()
        self.assertEqual(meta['Comment'], 'this is a test comment')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = images.GIFParser('./tests/data/clean.cleaned.gif')
        self.assertNotIn('EncodingSettings', p.get_meta())
        self.assertTrue(p.remove_all())

        os.remove('./tests/data/clean.gif')
        os.remove('./tests/data/clean.cleaned.gif')
        os.remove('./tests/data/clean.cleaned.cleaned.gif')
jvoisin's avatar
jvoisin committed
636
637
638

    def test_html(self):
        shutil.copy('./tests/data/dirty.html', './tests/data/clean.html')
jvoisin's avatar
jvoisin committed
639
        p = web.HTMLParser('./tests/data/clean.html')
jvoisin's avatar
jvoisin committed
640
641
642
643
644
645
646

        meta = p.get_meta()
        self.assertEqual(meta['author'], 'jvoisin')

        ret = p.remove_all()
        self.assertTrue(ret)

jvoisin's avatar
jvoisin committed
647
        p = web.HTMLParser('./tests/data/clean.cleaned.html')
jvoisin's avatar
jvoisin committed
648
649
650
651
652
653
        self.assertEqual(p.get_meta(), {})
        self.assertTrue(p.remove_all())

        os.remove('./tests/data/clean.html')
        os.remove('./tests/data/clean.cleaned.html')
        os.remove('./tests/data/clean.cleaned.cleaned.html')
jvoisin's avatar
jvoisin committed
654

jvoisin's avatar
jvoisin committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
        with open('./tests/data/clean.html', 'w') as f:
            f.write('<title><title><pouet/><meta/></title></title><test/>')
        p = web.HTMLParser('./tests/data/clean.html')
        self.assertTrue(p.remove_all())
        with open('./tests/data/clean.cleaned.html', 'r') as f:
            self.assertEqual(f.read(), '<title></title><test/>')
        os.remove('./tests/data/clean.html')
        os.remove('./tests/data/clean.cleaned.html')

        with open('./tests/data/clean.html', 'w') as f:
            f.write('<test><title>Some<b>metadata</b><br/></title></test>')
        p = web.HTMLParser('./tests/data/clean.html')
        self.assertTrue(p.remove_all())
        with open('./tests/data/clean.cleaned.html', 'r') as f:
            self.assertEqual(f.read(), '<test><title></title></test>')
        os.remove('./tests/data/clean.html')
        os.remove('./tests/data/clean.cleaned.html')

        with open('./tests/data/clean.html', 'w') as f:
jvoisin's avatar
jvoisin committed
674
            f.write('<meta><meta/><!----><!-- test--></meta>')
jvoisin's avatar
jvoisin committed
675
676
677
678
679
680
681
        p = web.HTMLParser('./tests/data/clean.html')
        self.assertTrue(p.remove_all())
        with open('./tests/data/clean.cleaned.html', 'r') as f:
            self.assertEqual(f.read(), '')
        os.remove('./tests/data/clean.html')
        os.remove('./tests/data/clean.cleaned.html')

jvoisin's avatar
jvoisin committed
682
683
684
685
686
687
688
689
690
691
692
693

    def test_epub(self):
        shutil.copy('./tests/data/dirty.epub', './tests/data/clean.epub')
        p = epub.EPUBParser('./tests/data/clean.epub')

        meta = p.get_meta()
        self.assertEqual(meta['OEBPS/content.opf']['dc:source'], 'http://www.gutenberg.org/files/58820/58820-h/58820-h.htm')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = epub.EPUBParser('./tests/data/clean.cleaned.epub')
jvoisin's avatar
jvoisin committed
694
695
696
697
        meta = p.get_meta()
        res = re.match(meta['OEBPS/content.opf']['metadata'], '^<dc:identifier>[0-9a-f-]+</dc:identifier><dc:title /><dc:language />$')
        self.assertNotEqual(res, False)

jvoisin's avatar
jvoisin committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
        self.assertTrue(p.remove_all())

        os.remove('./tests/data/clean.epub')
        os.remove('./tests/data/clean.cleaned.epub')
        os.remove('./tests/data/clean.cleaned.cleaned.epub')


    def test_css(self):
        shutil.copy('./tests/data/dirty.css', './tests/data/clean.css')
        p = web.CSSParser('./tests/data/clean.css')

        self.assertEqual(p.get_meta(), {
            'harmful data': 'underline is cool',
            'version': '1.0',
            'author': 'jvoisin'})

        ret = p.remove_all()
        self.assertTrue(ret)

        p = web.CSSParser('./tests/data/clean.cleaned.css')
        self.assertEqual(p.get_meta(), {})
        self.assertTrue(p.remove_all())

        os.remove('./tests/data/clean.css')
        os.remove('./tests/data/clean.cleaned.css')
        os.remove('./tests/data/clean.cleaned.cleaned.css')
jvoisin's avatar
jvoisin committed
724
725

    def test_tar(self):
jvoisin's avatar
jvoisin committed
726
        with tarfile.TarFile.open('./tests/data/dirty.tar', 'w') as zout:
jvoisin's avatar
jvoisin committed
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
            zout.add('./tests/data/dirty.flac')
            zout.add('./tests/data/dirty.docx')
            zout.add('./tests/data/dirty.jpg')
        p = archive.TarParser('./tests/data/dirty.tar')
        meta = p.get_meta()
        self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = archive.TarParser('./tests/data/dirty.cleaned.tar')
        self.assertEqual(p.get_meta(), {})
        self.assertTrue(p.remove_all())

        tmp_dir = tempfile.mkdtemp()
        with tarfile.open('./tests/data/dirty.cleaned.tar') as zout:
            zout.extractall(path=tmp_dir)
            zout.close()

        number_of_files = 0
        for root, _, fnames in os.walk(tmp_dir):
            for f in fnames:
                complete_path = os.path.join(root, f)
                p, _ = parser_factory.get_parser(complete_path)
                self.assertIsNotNone(p)
                self.assertEqual(p.get_meta(), {})
                number_of_files += 1
        self.assertEqual(number_of_files, 3)

        os.remove('./tests/data/dirty.tar')
        os.remove('./tests/data/dirty.cleaned.tar')
        os.remove('./tests/data/dirty.cleaned.cleaned.tar')
jvoisin's avatar
jvoisin committed
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863

    def test_targz(self):
        with tarfile.TarFile.open('./tests/data/dirty.tar.gz', 'w:gz') as zout:
            zout.add('./tests/data/dirty.flac')
            zout.add('./tests/data/dirty.docx')
            zout.add('./tests/data/dirty.jpg')
        p = archive.TarParser('./tests/data/dirty.tar.gz')
        meta = p.get_meta()
        self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = archive.TarParser('./tests/data/dirty.cleaned.tar.gz')
        self.assertEqual(p.get_meta(), {})
        self.assertTrue(p.remove_all())

        tmp_dir = tempfile.mkdtemp()
        with tarfile.open('./tests/data/dirty.cleaned.tar.gz') as zout:
            zout.extractall(path=tmp_dir)
            zout.close()

        number_of_files = 0
        for root, _, fnames in os.walk(tmp_dir):
            for f in fnames:
                complete_path = os.path.join(root, f)
                p, _ = parser_factory.get_parser(complete_path)
                self.assertIsNotNone(p)
                self.assertEqual(p.get_meta(), {})
                number_of_files += 1
        self.assertEqual(number_of_files, 3)

        os.remove('./tests/data/dirty.tar.gz')
        os.remove('./tests/data/dirty.cleaned.tar.gz')
        os.remove('./tests/data/dirty.cleaned.cleaned.tar.gz')

    def test_tarbz2(self):
        with tarfile.TarFile.open('./tests/data/dirty.tar.bz2', 'w:bz2') as zout:
            zout.add('./tests/data/dirty.flac')
            zout.add('./tests/data/dirty.docx')
            zout.add('./tests/data/dirty.jpg')
        p = archive.TarParser('./tests/data/dirty.tar.bz2')
        meta = p.get_meta()
        self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = archive.TarParser('./tests/data/dirty.cleaned.tar.bz2')
        self.assertEqual(p.get_meta(), {})
        self.assertTrue(p.remove_all())

        tmp_dir = tempfile.mkdtemp()
        with tarfile.open('./tests/data/dirty.cleaned.tar.bz2') as zout:
            zout.extractall(path=tmp_dir)
            zout.close()

        number_of_files = 0
        for root, _, fnames in os.walk(tmp_dir):
            for f in fnames:
                complete_path = os.path.join(root, f)
                p, _ = parser_factory.get_parser(complete_path)
                self.assertIsNotNone(p)
                self.assertEqual(p.get_meta(), {})
                number_of_files += 1
        self.assertEqual(number_of_files, 3)

        os.remove('./tests/data/dirty.tar.bz2')
        os.remove('./tests/data/dirty.cleaned.tar.bz2')
        os.remove('./tests/data/dirty.cleaned.cleaned.tar.bz2')

    def test_tarxz(self):
        with tarfile.TarFile.open('./tests/data/dirty.tar.xz', 'w:xz') as zout:
            zout.add('./tests/data/dirty.flac')
            zout.add('./tests/data/dirty.docx')
            zout.add('./tests/data/dirty.jpg')
        p = archive.TarParser('./tests/data/dirty.tar.xz')
        meta = p.get_meta()
        self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')

        ret = p.remove_all()
        self.assertTrue(ret)

        p = archive.TarParser('./tests/data/dirty.cleaned.tar.xz')
        self.assertEqual(p.get_meta(), {})
        self.assertTrue(p.remove_all())

        tmp_dir = tempfile.mkdtemp()
        with tarfile.open('./tests/data/dirty.cleaned.tar.xz') as zout:
            zout.extractall(path=tmp_dir)
            zout.close()

        number_of_files = 0
        for root, _, fnames in os.walk(tmp_dir):
            for f in fnames:
                complete_path = os.path.join(root, f)
                p, _ = parser_factory.get_parser(complete_path)
                self.assertIsNotNone(p)
                self.assertEqual(p.get_meta(), {})
                number_of_files += 1
        self.assertEqual(number_of_files, 3)

        os.remove('./tests/data/dirty.tar.xz')
        os.remove('./tests/data/dirty.cleaned.tar.xz')
        os.remove('./tests/data/dirty.cleaned.cleaned.tar.xz')