#!/usr/bin/env python
###############################################################################
# #
# Peekaboo Extended Email Attachment Behavior Observation Owl #
# #
# test.py #
###############################################################################
# #
# Copyright (C) 2016-2019 science + computing ag #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or (at #
# your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
###############################################################################
""" The testsuite. """
from future.builtins import super # pylint: disable=wrong-import-order
import gettext
import sys
import os
import tempfile
import logging
import shutil
import unittest
from datetime import datetime, timedelta
# Add Peekaboo to PYTHONPATH
# pylint: disable=wrong-import-position
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from peekaboo.exceptions import PeekabooConfigException, \
PeekabooRulesetConfigError
from peekaboo.config import PeekabooConfig, PeekabooConfigParser
from peekaboo.sample import SampleFactory
from peekaboo.ruleset import RuleResult, Result
from peekaboo.ruleset.engine import RulesetEngine
from peekaboo.ruleset.rules import FileTypeOnWhitelistRule, \
FileTypeOnGreylistRule, CuckooAnalysisFailedRule, \
KnownRule, FileLargerThanRule, CuckooEvilSigRule, \
CuckooScoreRule, RequestsEvilDomainRule, FinalRule, \
OfficeMacroRule, OfficeMacroWithSuspiciousKeyword, \
ExpressionRule
from peekaboo.toolbox.cuckoo import CuckooReport
from peekaboo.db import PeekabooDatabase, PeekabooDatabaseError
# pylint: enable=wrong-import-position
""" Since Python 3.2 assertRegexpMatches and assertRaisesRegexp
have been renamed to assertRegex() and assertRaisesRegex(). """
if sys.version_info[0] < 3:
class CompatibleTestCase(unittest.TestCase):
def assertRaisesRegex(self, exc, r, callable=None, *args, **kwds):
return self.assertRaisesRegexp(exc, r, callable, *args, **kwds)
def assertRegex(self, text, regex, msg=None):
return self.assertRegexpMatches(text, regex, msg)
else:
class CompatibleTestCase(unittest.TestCase):
pass
class CreatingConfigMixIn(object):
""" A class for adding config file creation logic to any other class. """
def create_config(self, content):
""" Create a configuration file with defined content and pass it to the
parent constructor for parsing. """
_, self.created_config_file = tempfile.mkstemp()
with open(self.created_config_file, 'w') as file_desc:
file_desc.write(content)
def remove_config(self):
""" Remove the configuration file we've created. """
os.unlink(self.created_config_file)
class CreatingConfigParser(PeekabooConfigParser, CreatingConfigMixIn):
""" A special kind of config parser that creates the configuration file
with defined content. """
def __init__(self, content=''):
self.created_config_file = None
self.create_config(content)
PeekabooConfigParser.__init__(self, self.created_config_file)
def __del__(self):
self.remove_config()
class CreatingPeekabooConfig(PeekabooConfig, CreatingConfigMixIn):
""" A special kind of Peekaboo config that creates the configuration file
with defined content. """
def __init__(self, content=''):
self.created_config_file = None
self.create_config(content)
PeekabooConfig.__init__(self, self.created_config_file)
def __del__(self):
self.remove_config()
class TestConfigParser(CompatibleTestCase):
""" Test a configuration with all values different from the defaults. """
@classmethod
def setUpClass(cls):
""" Set up common test case resources. """
cls.config <