2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
import email.utils
|
|
|
|
import sys
|
2018-03-21 23:22:31 +00:00
|
|
|
import threading
|
2017-09-15 06:24:41 +00:00
|
|
|
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
|
2018-04-17 16:40:45 +00:00
|
|
|
from mitogen.core import b
|
2018-02-26 23:34:02 +00:00
|
|
|
|
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
|
|
|
|
2018-03-21 23:22:31 +00:00
|
|
|
# TODO: this is a horrendous hack. Without it, we can't deliver a
|
|
|
|
# response to find_module() via _on_load_module() since find_module()
|
|
|
|
# is still holding the lock. The tests need a nicer abstraction for
|
|
|
|
# soemthing like "fake participant" that lets us have a mock master
|
|
|
|
# that respects the execution model expected by the code -- probably
|
|
|
|
# (grmph) including multiplexer thread and all.
|
|
|
|
self.importer._lock = threading.RLock()
|
|
|
|
|
|
|
|
def set_get_module_response(self, resp):
|
|
|
|
def on_context_send(msg):
|
|
|
|
self.context_send_msg = msg
|
|
|
|
self.importer._on_load_module(
|
|
|
|
mitogen.core.Message.pickled(resp)
|
|
|
|
)
|
|
|
|
self.context.send = on_context_send
|
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
def tearDown(self):
|
|
|
|
sys.modules.pop(self.modname, None)
|
|
|
|
super(ImporterMixin, self).tearDown()
|
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class LoadModuleTest(ImporterMixin, testlib.TestCase):
|
2018-04-17 16:40:45 +00:00
|
|
|
data = zlib.compress(b("data = 1\n\n"))
|
2017-09-15 06:24:41 +00:00
|
|
|
path = 'fake_module.py'
|
|
|
|
modname = 'fake_module'
|
2018-03-21 23:22:31 +00:00
|
|
|
|
|
|
|
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
|
|
|
|
response = (modname, None, path, data, [])
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_no_such_module(self):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(
|
|
|
|
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
|
|
|
|
(self.modname, None, None, None, 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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-04-17 16:40:45 +00:00
|
|
|
data = zlib.compress(b("data = 1\n\n"))
|
2017-09-15 06:24:41 +00:00
|
|
|
path = 'fake_module.py'
|
|
|
|
modname = 'mypkg.fake_module'
|
2018-03-21 23:22:31 +00:00
|
|
|
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
|
|
|
|
response = (modname, None, path, data, [])
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_package_unset(self):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-04-17 16:40:45 +00:00
|
|
|
data = zlib.compress(b("func = lambda: 1\n\n"))
|
2017-09-15 06:24:41 +00:00
|
|
|
path = 'fake_pkg/__init__.py'
|
|
|
|
modname = 'fake_pkg'
|
2018-03-21 23:22:31 +00:00
|
|
|
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
|
|
|
|
response = (modname, [], path, data, [])
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_file_set(self):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(self.response)
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
source = mod.__loader__.get_source(self.modname)
|
2018-06-26 02:19:59 +00:00
|
|
|
self.assertEquals(source,
|
|
|
|
mitogen.core.to_text(zlib.decompress(self.data)))
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_loader_set(self):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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):
|
2018-03-21 23:22:31 +00:00
|
|
|
self.set_get_module_response(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
|
|
|
|
|
|
|
|
2018-03-21 23:22:31 +00:00
|
|
|
class ImporterBlacklistTest(testlib.TestCase):
|
|
|
|
def test_is_blacklisted_import_default(self):
|
|
|
|
importer = mitogen.core.Importer(
|
|
|
|
router=mock.Mock(), context=None, core_src='',
|
|
|
|
)
|
|
|
|
self.assertIsInstance(importer.whitelist, list)
|
|
|
|
self.assertIsInstance(importer.blacklist, list)
|
|
|
|
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',),
|
|
|
|
)
|
|
|
|
self.assertIsInstance(importer.whitelist, list)
|
|
|
|
self.assertIsInstance(importer.blacklist, list)
|
|
|
|
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',),
|
|
|
|
)
|
|
|
|
self.assertIsInstance(importer.whitelist, list)
|
|
|
|
self.assertIsInstance(importer.blacklist, list)
|
|
|
|
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',),
|
|
|
|
)
|
|
|
|
self.assertIsInstance(importer.whitelist, list)
|
|
|
|
self.assertIsInstance(importer.blacklist, list)
|
|
|
|
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'))
|
|
|
|
|
|
|
|
|
2019-01-23 12:44:08 +00:00
|
|
|
class Python24LineCacheTest(testlib.TestCase):
|
|
|
|
# TODO: mitogen.core.Importer._update_linecache()
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
if __name__ == '__main__':
|
2017-11-12 12:42:27 +00:00
|
|
|
unittest2.main()
|