Пример #1
0
    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()
        self.path = os.path.join(self.tmpdir, 'mimemagic')
        with open(self.path, 'wb') as f:
            f.write(resources.mime_magic_db)

        self.path2 = os.path.join(self.tmpdir, 'mimemagic2')
        with open(self.path2, 'wb') as f:
            f.write(resources.mime_magic_db2)

        # Read the files
        self.magic = Mime.MagicDB()
        self.magic.merge_file(self.path)
        self.magic.merge_file(self.path2)
        self.magic.finalise()
Пример #2
0
def TestOneInput(input_bytes):
    # We need to make the file an absolute path
    testfile_path = os.path.join(os.getcwd(), "testfile.tmp")
    with open(testfile_path, "wb") as f:
        f.write(input_bytes)

    # Test basic Mime API
    Mime.get_type2(testfile_path)
    Mime.get_type_by_contents(testfile_path)
    Mime.get_type_by_data(input_bytes)

    # Test GlobDB
    globs = Mime.GlobDB()
    try:
        globs.merge_file(testfile_path)
        globs.merge_file(testfile_path)
    except UnicodeError as e:
        pass
    except ValueError as e:
        if ("not enough values to unpack" in str(e)
                or "invalid literal for int" in str(e)):
            pass
        else:
            raise e

    # Test MagicDB
    magic = Mime.MagicDB()
    try:
        magic.merge_file(testfile_path)
        magic.finalise()
    except UnicodeDecodeError:
        pass
    except (OSError, ValueError) as e:
        msg = str(e)
        if ("Not a MIME magic file" in msg
                or "Malformed section heading" in msg):
            pass
        else:
            raise e

    # Cleanup
    os.remove(testfile_path)