2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
import email.utils
|
|
|
|
import sys
|
|
|
|
import types
|
|
|
|
import zlib
|
|
|
|
|
|
|
|
import mock
|
|
|
|
import pytest
|
2017-11-12 12:42:27 +00:00
|
|
|
import unittest2
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
import mitogen.core
|
2018-02-26 23:34:02 +00:00
|
|
|
import mitogen.utils
|
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
import testlib
|
|
|
|
|
|
|
|
|
2017-09-24 23:50:50 +00:00
|
|
|
class ImporterMixin(testlib.RouterMixin):
|
2017-09-15 06:24:41 +00:00
|
|
|
modname = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(ImporterMixin, self).setUp()
|
|
|
|
self.context = mock.Mock()
|
2018-02-14 23:04:10 +00:00
|
|
|
self.importer = mitogen.core.Importer(self.router, self.context, '')
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.modules.pop(self.modname, None)
|
|
|
|
super(ImporterMixin, self).tearDown()
|
|
|
|
|
|
|
|
|
2018-02-26 23:34:02 +00:00
|
|
|
class ImporterBlacklist(testlib.TestCase):
|
|
|
|
def test_is_blacklisted_import_default(self):
|
|
|
|
importer = mitogen.core.Importer(
|
|
|
|
router=mock.Mock(), context=None, core_src='',
|
|
|
|
)
|
2018-02-26 23:38:05 +00:00
|
|
|
self.assertIsInstance(importer.whitelist, list)
|
|
|
|
self.assertIsInstance(importer.blacklist, list)
|
2018-02-26 23:34:02 +00:00
|
|
|
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg'))
|
|
|
|
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod'))
|
|
|
|
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg'))
|
|
|
|
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins'))
|
|
|
|
|
|
|
|
def test_is_blacklisted_import_just_whitelist(self):
|
|
|
|
importer = mitogen.core.Importer(
|
|
|
|
router=mock.Mock(), context=None, core_src='',
|
|
|
|
whitelist=('mypkg',),
|
|
|
|
)
|
2018-02-26 23:38:05 +00:00
|
|
|
self.assertIsInstance(importer.whitelist, list)
|
|
|
|
self.assertIsInstance(importer.blacklist, list)
|
2018-02-26 23:34:02 +00:00
|
|
|
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg'))
|
|
|
|
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins'))
|
|
|
|
|
|
|
|
def test_is_blacklisted_import_just_blacklist(self):
|
|
|
|
importer = mitogen.core.Importer(
|
|
|
|
router=mock.Mock(), context=None, core_src='',
|
|
|
|
blacklist=('mypkg',),
|
|
|
|
)
|
2018-02-26 23:38:05 +00:00
|
|
|
self.assertIsInstance(importer.whitelist, list)
|
|
|
|
self.assertIsInstance(importer.blacklist, list)
|
2018-02-26 23:34:02 +00:00
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod'))
|
|
|
|
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg'))
|
|
|
|
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins'))
|
|
|
|
|
|
|
|
def test_is_blacklisted_import_whitelist_and_blacklist(self):
|
|
|
|
importer = mitogen.core.Importer(
|
|
|
|
router=mock.Mock(), context=None, core_src='',
|
|
|
|
whitelist=('mypkg',), blacklist=('mypkg',),
|
|
|
|
)
|
2018-02-26 23:38:05 +00:00
|
|
|
self.assertIsInstance(importer.whitelist, list)
|
|
|
|
self.assertIsInstance(importer.blacklist, list)
|
2018-02-26 23:34:02 +00:00
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__'))
|
|
|
|
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins'))
|
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class LoadModuleTest(ImporterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
data = zlib.compress("data = 1\n\n")
|
|
|
|
path = 'fake_module.py'
|
|
|
|
modname = 'fake_module'
|
|
|
|
response = (None, path, data)
|
|
|
|
|
|
|
|
def test_no_such_module(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = None
|
2017-09-15 06:24:41 +00:00
|
|
|
self.assertRaises(ImportError,
|
|
|
|
lambda: self.importer.load_module(self.modname))
|
|
|
|
|
|
|
|
def test_module_added_to_sys_modules(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIs(sys.modules[self.modname], mod)
|
|
|
|
self.assertIsInstance(mod, types.ModuleType)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_file_set(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__file__, 'master:' + self.path)
|
|
|
|
|
|
|
|
def test_module_loader_set(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIs(mod.__loader__, self.importer)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_package_unset(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIsNone(mod.__package__)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class LoadSubmoduleTest(ImporterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
data = zlib.compress("data = 1\n\n")
|
|
|
|
path = 'fake_module.py'
|
|
|
|
modname = 'mypkg.fake_module'
|
|
|
|
response = (None, path, data)
|
|
|
|
|
|
|
|
def test_module_package_unset(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__package__, 'mypkg')
|
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class LoadModulePackageTest(ImporterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
data = zlib.compress("func = lambda: 1\n\n")
|
|
|
|
path = 'fake_pkg/__init__.py'
|
|
|
|
modname = 'fake_pkg'
|
|
|
|
response = ([], path, data)
|
|
|
|
|
|
|
|
def test_module_file_set(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__file__, 'master:' + self.path)
|
|
|
|
|
|
|
|
def test_get_filename(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
filename = mod.__loader__.get_filename(self.modname)
|
|
|
|
self.assertEquals('master:fake_pkg/__init__.py', filename)
|
|
|
|
|
|
|
|
def test_get_source(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
source = mod.__loader__.get_source(self.modname)
|
|
|
|
self.assertEquals(source, zlib.decompress(self.data))
|
|
|
|
|
|
|
|
def test_module_loader_set(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIs(mod.__loader__, self.importer)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_path_present(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__path__, [])
|
|
|
|
|
|
|
|
def test_module_package_set(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__package__, self.modname)
|
|
|
|
|
|
|
|
def test_module_data(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIsInstance(mod.func, types.FunctionType)
|
2017-09-15 06:24:41 +00:00
|
|
|
self.assertEquals(mod.func.__module__, self.modname)
|
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class EmailParseAddrSysTest(testlib.RouterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def initdir(self, caplog):
|
|
|
|
self.caplog = caplog
|
|
|
|
|
|
|
|
def test_sys_module_not_fetched(self):
|
|
|
|
# An old version of core.Importer would request the email.sys module
|
|
|
|
# while executing email.utils.parseaddr(). Ensure this needless
|
|
|
|
# roundtrip has not reappeared.
|
|
|
|
pass
|
2017-10-01 10:00:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-11-12 12:42:27 +00:00
|
|
|
unittest2.main()
|