# Path: elem/exploit/exploit_source.py
# class ExploitSource(Resource):
#     """
#     ExploitSource is the source of an exploit or exploits.
#     """
#     def __init__(self, source_name, location):
#         super(ExploitSource, self).__init__(location)
#         self.token = 0
# 
#         self.source_name = source_name
# 
# 
#     def exploits(self):
#         exploits = []
#         if isinstance(self.data, list):
#             for location in self.data:
#                 data = Resource(location).read()
#                 exploit = ExploitSource.generate_exploit(data, location, self.source_name)
#                 exploits.append(exploit)
#         elif isinstance(self.data, str):
#             exploits.append(ExploitSource.generate_exploit(self.data, self.location, self.source_name))
#         return exploits
# 
#     @classmethod
#     def generate_exploit(cls, data, location, source_name):
#         matches = re.findall(CVE_STRING, data, re.MULTILINE)
#         if ResourceConnectorFactory.location_is_url(location):
#             location_path = urlparse.urlparse(location).path
#         elif os.path.isfile(location):
#             location_path = location
#         location_base = os.path.basename(location_path)
#         eid = os.path.splitext(location_base)[0]
# 
#         exploit = dict(source=source_name,
#                        cves=list(set(matches)),
#                        id=eid,
#                        exploit=base64.b64encode(data))
#         return exploit


import unittest
import os
import mock
from elem.exploit import ExploitSource

class TestExploitSourceDir(unittest.TestCase):
    def setUp(self):
        self.test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                           '..',
                                           'test_data')

        self.exploit_dir = os.path.join(self.test_data_path,
                                        'exploit-source')

        self.exploit_source = ExploitSource('exploit-source', self.exploit_dir)

    def test_exploits(self):
        exploits = self.exploit_source.exploits()
        self.assertEqual(len(exploits), 7)

class TestExploitSourceFile(unittest.TestCase):
    def setUp(self):
        self.test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                           '..',
                                           'test_data')

        self.exploit_file = os.path.join(self.test_data_path,
                                         'exploit-source',
                                         'platforms',
                                         'linux',
                                         'local',
                                         'one_cve.py')


# Path: elem/exploit/exploit_source.py
# class ExploitSource(Resource):
#     """
#     ExploitSource is the source of an exploit or exploits.
#     """
#     def __init__(self, source_name, location):
#         super(ExploitSource, self).__init__(location)
#         self.token = 0
# 
#         self.source_name = source_name
# 
# 
#     def exploits(self):
#         exploits = []
#         if isinstance(self.data, list):
#             for location in self.data:
#                 data = Resource(location).read()
#                 exploit = ExploitSource.generate_exploit(data, location, self.source_name)
#                 exploits.append(exploit)
#         elif isinstance(self.data, str):
#             exploits.append(ExploitSource.generate_exploit(self.data, self.location, self.source_name))
#         return exploits
# 
#     @classmethod
#     def generate_exploit(cls, data, location, source_name):
#         matches = re.findall(CVE_STRING, data, re.MULTILINE)
#         if ResourceConnectorFactory.location_is_url(location):
#             location_path = urlparse.urlparse(location).path
#         elif os.path.isfile(location):
#             location_path = location
#         location_base = os.path.basename(location_path)
#         eid = os.path.splitext(location_base)[0]
# 
#         exploit = dict(source=source_name,
#                        cves=list(set(matches)),
#                        id=eid,
#                        exploit=base64.b64encode(data))
#         return exploit


# Path: tests/exploit/test_exploit_source.py
import unittest
import os
import mock
from elem.exploit import ExploitSource


class TestExploitSourceDir(unittest.TestCase):
    def setUp(self):
        self.test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                           '..',
                                           'test_data')

        self.exploit_dir = os.path.join(self.test_data_path,
                                        'exploit-source')

        self.exploit_source = ExploitSource('exploit-source', self.exploit_dir)

    def test_exploits(self):
        exploits = self.exploit_source.exploits()
        self.assertEqual(len(exploits), 7)

class TestExploitSourceFile(unittest.TestCase):
    def setUp(self):
        self.test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                           '..',
                                           'test_data')

        self.exploit_file = os.path.join(self.test_data_path,
                                         'exploit-source',
                                         'platforms',
                                         'linux',
                                         'local',
                                         'one_cve.py')


        self.exploit_source = ExploitSource('exploit-source', self.exploit_file)