]> Frank Brehm's Git Trees - pixelpark/trace-maillog.git/commitdiff
Adding test/test_magic.py and testdata
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 10 May 2017 15:18:34 +0000 (17:18 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 10 May 2017 15:18:34 +0000 (17:18 +0200)
test/test_magic.py [new file with mode: 0755]
test/testdata/keep-going.jpg [new file with mode: 0644]
test/testdata/lambda [new file with mode: 0644]
test/testdata/test.gz [new file with mode: 0644]
test/testdata/test.pdf [new file with mode: 0644]
test/testdata/text-iso8859-1.txt [new file with mode: 0644]
test/testdata/text.txt [new file with mode: 0644]

diff --git a/test/test_magic.py b/test/test_magic.py
new file mode 100755 (executable)
index 0000000..6f4ed4e
--- /dev/null
@@ -0,0 +1,208 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os, sys
+# for output which reports a local time
+os.environ['TZ'] = 'GMT'
+import shutil
+import os.path
+import re
+
+import logging
+import locale
+
+try:
+    import unittest2 as unittest
+except ImportError:
+    import unittest
+
+import six
+
+from pb_base.common import pp
+from pb_base.common import bytes2human
+
+libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib'))
+sys.path.insert(0, libdir)
+
+from trace_maillog import magic
+
+from general import LogtraceTestcase, get_arg_verbose, init_root_logger
+
+locale.setlocale(locale.LC_ALL, '')
+
+APPNAME = 'test_magic'
+
+LOG = logging.getLogger(APPNAME)
+
+
+
+# =============================================================================
+class MagicTest(LogtraceTestcase):
+
+    # -------------------------------------------------------------------------
+    def setUp(self):
+        self.appname = APPNAME
+        self.testdata_dir = os.path.join(os.path.dirname(__file__), 'testdata')
+
+    # -------------------------------------------------------------------------
+    def tearDown(self):
+        pass
+
+    # -------------------------------------------------------------------------
+    def assert_values(self, m, expected_values):
+        for filename, expected_value in expected_values.items():
+            try:
+                filename = os.path.join(self.testdata_dir, filename)
+            except TypeError:
+                filename = os.path.join(self.testdata_dir.encode('utf-8'), filename)
+
+            if type(expected_value) is not tuple:
+                expected_value = (expected_value,)
+
+            LOG.debug("Testing {!r}, expecting {!r}.".format(filename, expected_value))
+            
+            for i in expected_value:
+                with open(filename, 'rb') as f:
+                    buf_value = m.from_buffer(f.read())
+
+                file_value = m.from_file(filename)
+                LOG.debug("Got: buffer value: {!r}, file value: {!r}.".format(buf_value, file_value))
+                if buf_value == i and file_value == i:
+                    break
+            else:
+                self.assertTrue(False, "no match for " + repr(expected_value))
+                
+    # -------------------------------------------------------------------------
+    def test_mime_types(self):
+
+        LOG.info("Testing MIME types ...")
+
+        dest = os.path.join(self.testdata_dir, b'\xce\xbb'.decode('utf-8'))
+        shutil.copyfile(os.path.join(self.testdata_dir, 'lambda'), dest)
+        try:
+            m = magic.Magic(mime=True)
+            self.assert_values(m, {
+                'magic.pyc': 'application/octet-stream',
+                'test.pdf': 'application/pdf',
+                'test.gz': 'application/gzip',
+                'text.txt': 'text/plain',
+                b'\xce\xbb'.decode('utf-8'): 'text/plain',
+                b'\xce\xbb': 'text/plain',
+            })
+        finally:
+            os.unlink(dest)
+
+    # -------------------------------------------------------------------------
+    def test_descriptions(self):
+
+        LOG.info("Testing descriptions ...")
+
+        m = magic.Magic()
+        os.environ['TZ'] = 'UTC'  # To get the last modified date of test.gz in UTC
+        try:
+            self.assert_values(m, {
+                'magic.pyc': 'python 2.4 byte-compiled',
+                'test.pdf': 'PDF document, version 1.2',
+                'test.gz':
+                ('gzip compressed data, was "test", from Unix, last modified: Sun Jun 29 01:32:52 2008',
+                 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix'),
+                'text.txt': 'ASCII text',
+            })
+        finally:
+            del os.environ['TZ']
+
+    # -------------------------------------------------------------------------
+    def test_mime_encodings(self):
+
+        LOG.info("Testing MIME encodings ...")
+
+        m = magic.Magic(mime_encoding=True)
+        self.assert_values(m, {
+            'text-iso8859-1.txt': 'iso-8859-1',
+            'text.txt': 'us-ascii',
+        })
+
+    # -------------------------------------------------------------------------
+    def test_errors(self):
+
+        LOG.info("Testing errors ...")
+
+        m = magic.Magic()
+        self.assertRaises(IOError, m.from_file, 'nonexistent')
+        self.assertRaises(magic.MagicException, magic.Magic,
+                          magic_file='nonexistent')
+        os.environ['MAGIC'] = 'nonexistent'
+        try:
+            self.assertRaises(magic.MagicException, magic.Magic)
+        finally:
+            del os.environ['MAGIC']
+
+    # -------------------------------------------------------------------------
+    def test_keep_going(self):
+
+        LOG.info("Testing keep going ...")
+
+        filename = os.path.join(self.testdata_dir, 'keep-going.jpg')
+
+        m = magic.Magic(mime=True)
+        result = m.from_file(filename)
+        LOG.debug("Testing {!r} - expecting {!r} - got {!r}.".format(
+            filename, 'image/jpeg', result))
+        self.assertEqual(result, 'image/jpeg')
+        
+        m = magic.Magic(mime=True, keep_going=True)
+        expected = ['image/jpeg', 'application/octet-stream']
+        result = m.from_file(filename)
+        result = re.split(r'\\012-\s+', result)
+
+        LOG.debug("Testing {!r} keep_going - expecting {!r} - got {!r}.".format(
+            filename, expected, result))
+        self.assertEqual(result, expected)
+
+
+    # -------------------------------------------------------------------------
+    def test_rethrow(self):
+
+        LOG.info("Testing rethrow ...")
+
+        old = magic.magic_buffer
+        try:
+            def t(x,y):
+                raise magic.MagicException("passthrough")
+            magic.magic_buffer = t
+            
+            self.assertRaises(magic.MagicException, magic.from_buffer, "hello", True)
+        finally:
+            magic.magic_buffer = old
+
+
+# =============================================================================
+
+if __name__ == '__main__':
+
+
+    verbose = get_arg_verbose()
+    if verbose is None:
+        verbose = 0
+    init_root_logger(verbose)
+
+    LOG.info("Starting tests ...")
+
+    loader = unittest.TestLoader()
+    suite = unittest.TestSuite()
+
+    suite.addTest(MagicTest('test_mime_types', verbose))
+    suite.addTest(MagicTest('test_descriptions', verbose))
+    suite.addTest(MagicTest('test_mime_encodings', verbose))
+    suite.addTest(MagicTest('test_errors', verbose))
+    suite.addTest(MagicTest('test_keep_going', verbose))
+    suite.addTest(MagicTest('test_rethrow', verbose))
+
+    runner = unittest.TextTestRunner(verbosity=verbose)
+
+    result = runner.run(suite)
+
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
diff --git a/test/testdata/keep-going.jpg b/test/testdata/keep-going.jpg
new file mode 100644 (file)
index 0000000..c15171d
Binary files /dev/null and b/test/testdata/keep-going.jpg differ
diff --git a/test/testdata/lambda b/test/testdata/lambda
new file mode 100644 (file)
index 0000000..9daeafb
--- /dev/null
@@ -0,0 +1 @@
+test
diff --git a/test/testdata/test.gz b/test/testdata/test.gz
new file mode 100644 (file)
index 0000000..5d847dd
Binary files /dev/null and b/test/testdata/test.gz differ
diff --git a/test/testdata/test.pdf b/test/testdata/test.pdf
new file mode 100644 (file)
index 0000000..b986617
--- /dev/null
@@ -0,0 +1,199 @@
+%PDF-1.2
+7 0 obj
+[5 0 R/XYZ 111.6 757.86]
+endobj
+13 0 obj
+<<
+/Title(About this document)
+/A<<
+/S/GoTo
+/D(subsection.1.1)
+>>
+/Parent 12 0 R
+/Next 14 0 R
+>>
+endobj
+15 0 obj
+<<
+/Title(Compiling with GHC)
+/A<<
+/S/GoTo
+/D(subsubsection.1.2.1)
+>>
+/Parent 14 0 R
+/Next 16 0 R
+>>
+endobj
+16 0 obj
+<<
+/Title(Compiling with Hugs)
+/A<<
+/S/GoTo
+/D(subsubsection.1.2.2)
+>>
+/Parent 14 0 R
+/Prev 15 0 R
+>>
+endobj
+14 0 obj
+<<
+/Title(Compatibility)
+/A<<
+/S/GoTo
+/D(subsection.1.2)
+>>
+/Parent 12 0 R
+/Prev 13 0 R
+/First 15 0 R
+/Last 16 0 R
+/Count -2
+/Next 17 0 R
+>>
+endobj
+17 0 obj
+<<
+/Title(Reporting bugs)
+/A<<
+/S/GoTo
+/D(subsection.1.3)
+>>
+/Parent 12 0 R
+/Prev 14 0 R
+/Next 18 0 R
+>>
+endobj
+18 0 obj
+<<
+/Title(History)
+/A<<
+/S/GoTo
+/D(subsection.1.4)
+>>
+/Parent 12 0 R
+/Prev 17 0 R
+/Next 19 0 R
+>>
+endobj
+19 0 obj
+<<
+/Title(License)
+/A<<
+/S/GoTo
+/D(subsection.1.5)
+>>
+/Parent 12 0 R
+/Prev 18 0 R
+>>
+endobj
+12 0 obj
+<<
+/Title(Introduction)
+/A<<
+/S/GoTo
+/D(section.1)
+>>
+/Parent 11 0 R
+/First 13 0 R
+/Last 19 0 R
+/Count -5
+/Next 20 0 R
+>>
+endobj
+21 0 obj
+<<
+/Title(Running a parser)
+/A<<
+/S/GoTo
+/D(subsection.2.1)
+>>
+/Parent 20 0 R
+/Next 22 0 R
+>>
+endobj
+22 0 obj
+<<
+/Title(Sequence and choice)
+/A<<
+/S/GoTo
+/D(subsection.2.2)
+>>
+/Parent 20 0 R
+/Prev 21 0 R
+/Next 23 0 R
+>>
+endobj
+23 0 obj
+<<
+/Title(Predictive parsers)
+/A<<
+/S/GoTo
+/D(subsection.2.3)
+>>
+/Parent 20 0 R
+/Prev 22 0 R
+/Next 24 0 R
+>>
+endobj
+24 0 obj
+<<
+/Title(Adding semantics)
+/A<<
+/S/GoTo
+/D(subsection.2.4)
+>>
+/Parent 20 0 R
+/Prev 23 0 R
+/Next 25 0 R
+>>
+endobj
+25 0 obj
+<<
+/Title(Sequences and seperators)
+/A<<
+/S/GoTo
+/D(subsection.2.5)
+>>
+/Parent 20 0 R
+/Prev 24 0 R
+/Next 26 0 R
+>>
+endobj
+26 0 obj
+<<
+/Title(Improving error messages)
+/A<<
+/S/GoTo
+/D(subsection.2.6)
+>>
+/Parent 20 0 R
+/Prev 25 0 R
+/Next 27 0 R
+>>
+endobj
+27 0 obj
+<<
+/Title(Expressions)
+/A<<
+/S/GoTo
+/D(subsection.2.7)
+>>
+/Parent 20 0 R
+/Prev 26 0 R
+/Next 28 0 R
+>>
+endobj
+28 0 obj
+<<
+/Title(Lexical analysis)
+/A<<
+/S/GoTo
+/D(subsection.2.8)
+>>
+/Parent 20 0 R
+/Prev 27 0 R
+/Next 29 0 R
+>>
+endobj
+30 0 obj
+<<
+/Title(Lexeme parsers
\ No newline at end of file
diff --git a/test/testdata/text-iso8859-1.txt b/test/testdata/text-iso8859-1.txt
new file mode 100644 (file)
index 0000000..524a1d0
--- /dev/null
@@ -0,0 +1,2 @@
+This is a web page encoded in iso-8859-1
+éèàùôâïî
diff --git a/test/testdata/text.txt b/test/testdata/text.txt
new file mode 100644 (file)
index 0000000..476f506
--- /dev/null
@@ -0,0 +1,2 @@
+Hello, World!
+