Exemple #1
0
 def testEmptyPluginConfig(self):
     mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
     pcfg = PluginConfig()
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     self.pmgr._instantiate_all()
     self.assertEqual(0, len(mockplugin.instances))
Exemple #2
0
    def testMessageHandlerFiltering(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr.filter(self.__class__.handshake_msg1, 'client')
        self.pmgr.filter(self.__class__.handshake_msg2, 'server')
        p1 = mockplugin.instances[0]
        p2 = mockplugin.instances[1]
        msg = {'msgtype': 0x03, 'chat_msg': 'foo!'}

        self.assertTrue(self.pmgr.filter(msg, 'client'))
        self.assertEquals(msg, p1.last_msg)
        self.assertEquals(msg, p2.last_msg)

        p1.drop_next_msg = True
        self.assertFalse(
            self.pmgr.filter({
                'msgtype': 0x03,
                'chat_msg': 'bar!'
            }, 'server'))
        self.assertEquals(msg, p2.last_msg)

        p1.drop_next_msg = True
        self.assertTrue(
            self.pmgr.filter({
                'msgtype': 0x04,
                'time': 42
            }, 'client'))
Exemple #3
0
 def testMissingPluginClass(self):
     self._write_and_load('empty', 'from mc3p.plugins import MC3Plugin\n')
     pcfg = PluginConfig().add('empty', 'p')
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     # Should log an error, but not raise an exception.
     self.pmgr._instantiate_all()
Exemple #4
0
 def testMultiplePluginClasses(self):
     code = MOCK_PLUGIN_CODE + "class AnotherPlugin(MC3Plugin): pass"
     mockplugin = self._write_and_load('mockplugin', code)
     pcfg = PluginConfig().add('mockplugin', 'p')
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     self.pmgr._instantiate_all()
     self.assertEqual(0, len(mockplugin.instances))
Exemple #5
0
    def testInstantiationAfterSuccessfulHandshake(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.assertEqual(0, len(mockplugin.instances))

        self.pmgr.filter(self.__class__.handshake_msg1, 'client')
        self.pmgr.filter(self.__class__.handshake_msg2, 'server')
        self.assertEqual(2, len(mockplugin.instances))
Exemple #6
0
 def testInstantiation(self):
     mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
     pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     self.pmgr._instantiate_all()
     self.assertEqual(2, len(mockplugin.instances))
     self.assertTrue(mockplugin.instances[0].initialized)
     self.assertTrue(mockplugin.instances[1].initialized)
Exemple #7
0
 def testLoadingPluginInPackage(self):
     pkgname = os.path.basename(self.__class__.pkgdir)
     mockplugin = self._write_and_load(pkgname + '.mockplugin',
                                       MOCK_PLUGIN_CODE)
     pcfg = PluginConfig().add('%s.mockplugin' % pkgname, 'p1')
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     self.pmgr._instantiate_all()
     self.assertEqual(1, len(mockplugin.instances))
     self.assertTrue(mockplugin.instances[0].initialized)
Exemple #8
0
 def testMissingPluginClass(self):
     self._write_and_load('empty', 'from mc3p.plugins import MC3Plugin\n')
     pcfg = PluginConfig().add('empty', 'p')
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     # Should log an error, but not raise an exception.
     self.pmgr._instantiate_all()
Exemple #9
0
 def testEmptyPluginConfig(self):
     mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
     pcfg = PluginConfig()
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     self.pmgr._instantiate_all()
     self.assertEqual(0, len(mockplugin.instances))
Exemple #10
0
 def testMultiplePluginClasses(self):
     code = MOCK_PLUGIN_CODE + "class AnotherPlugin(MC3Plugin): pass"
     mockplugin = self._write_and_load('mockplugin', code)
     pcfg = PluginConfig().add('mockplugin', 'p')
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     self.pmgr._instantiate_all()
     self.assertEqual(0, len(mockplugin.instances))
Exemple #11
0
    def testInstantiationAfterSuccessfulHandshake(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.assertEqual(0, len(mockplugin.instances))

        self.pmgr.filter(self.__class__.handshake_msg1, 'client')
        self.pmgr.filter(self.__class__.handshake_msg2, 'server')
        self.assertEqual(2, len(mockplugin.instances))
Exemple #12
0
 def testLoadingPluginInPackage(self):
     pkgname = os.path.basename(self.__class__.pkgdir)
     mockplugin = self._write_and_load(pkgname+'.mockplugin', MOCK_PLUGIN_CODE)
     pcfg = PluginConfig().add('%s.mockplugin' % pkgname, 'p1')
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     self.pmgr._instantiate_all()
     self.assertEqual(1, len(mockplugin.instances))
     self.assertTrue(mockplugin.instances[0].initialized)
Exemple #13
0
 def testInstantiation(self):
     mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
     pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
     self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
     self.pmgr._load_plugins()
     self.pmgr._instantiate_all()
     self.assertEqual(2, len(mockplugin.instances))
     self.assertTrue(mockplugin.instances[0].initialized)
     self.assertTrue(mockplugin.instances[1].initialized)
Exemple #14
0
    def testMessageHandlerFiltering(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr.filter(self.__class__.handshake_msg1, 'client')
        self.pmgr.filter(self.__class__.handshake_msg2, 'server')
        p1 = mockplugin.instances[0]
        p2 = mockplugin.instances[1]
        msg = {'msgtype': 0x03, 'chat_msg': 'foo!'}

        self.assertTrue(self.pmgr.filter(msg, 'client'))
        self.assertEquals(msg, p1.last_msg)
        self.assertEquals(msg, p2.last_msg)

        p1.drop_next_msg = True
        self.assertFalse(self.pmgr.filter({'msgtype': 0x03, 'chat_msg': 'bar!'}, 'server'))
        self.assertEquals(msg, p2.last_msg)

        p1.drop_next_msg = True
        self.assertTrue(self.pmgr.filter({'msgtype': 0x04, 'time': 42}, 'client'))
Exemple #15
0
class TestPluginManager(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.pdir = tempfile.mkdtemp()
        cls.pkgdir = tempfile.mkdtemp(dir=cls.pdir)
        with open(os.path.join(cls.pkgdir, '__init__.py'), 'w') as f:
            f.write('')
        sys.path.append(cls.pdir)

    @classmethod
    def tearDownClass(cls):
        sys.path.pop()
        shutil.rmtree(cls.pdir)

    def _write_and_load(self, name, content):
        pfile = os.path.join(*name.split('.'))
        pfile = os.path.join(self.pdir, pfile + '.py')
        print 'loading %s as %s' % (pfile, name)
        with open(pfile, 'w') as f:
            f.write(content)
        mod = load_source(name, pfile)
        return mod

    def setUp(self):
        self.pdir = self.__class__.pdir
        self.cli_proxy, self.srv_proxy = MockMinecraftProxy(
        ), MockMinecraftProxy()

    def tearDown(self):
        if hasattr(self, 'pmgr') and self.pmgr != None:
            try:
                self.pmgr.destroy()
            except Exception as e:
                import traceback
                traceback.print_exc()

    def testInstantiation(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        self.pmgr._instantiate_all()
        self.assertEqual(2, len(mockplugin.instances))
        self.assertTrue(mockplugin.instances[0].initialized)
        self.assertTrue(mockplugin.instances[1].initialized)

    def testDefaultPluginIds(self):
        pcfg = PluginConfig().add('mockplugin')
        self.assertEqual('mockplugin', pcfg.ids[0])

        pcfg.add('mockplugin')
        pcfg.add('mockplugin')
        self.assertEqual('mockplugin1', pcfg.ids[1])
        self.assertEqual('mockplugin2', pcfg.ids[2])

    def testEmptyPluginConfig(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig()
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        self.pmgr._instantiate_all()
        self.assertEqual(0, len(mockplugin.instances))

    def testMissingPluginClass(self):
        self._write_and_load('empty', 'from mc3p.plugins import MC3Plugin\n')
        pcfg = PluginConfig().add('empty', 'p')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        # Should log an error, but not raise an exception.
        self.pmgr._instantiate_all()

    def testMultiplePluginClasses(self):
        code = MOCK_PLUGIN_CODE + "class AnotherPlugin(MC3Plugin): pass"
        mockplugin = self._write_and_load('mockplugin', code)
        pcfg = PluginConfig().add('mockplugin', 'p')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        self.pmgr._instantiate_all()
        self.assertEqual(0, len(mockplugin.instances))

    handshake_msg1 = {
        'msgtype': 0x01,
        'proto_version': 21,
        'username': '******',
        'nu1': 0,
        'nu2': 0,
        'nu3': 0,
        'nu4': 0,
        'nu5': 0,
        'nu6': 0
    }
    handshake_msg2 = {
        'msgtype': 0x01,
        'eid': 1,
        'reserved': '',
        'map_seed': 42,
        'server_mode': 0,
        'dimension': 0,
        'difficulty': 2,
        'world_height': 128,
        'max_players': 16
    }

    def testLoadingPluginInPackage(self):
        pkgname = os.path.basename(self.__class__.pkgdir)
        mockplugin = self._write_and_load(pkgname + '.mockplugin',
                                          MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('%s.mockplugin' % pkgname, 'p1')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        self.pmgr._instantiate_all()
        self.assertEqual(1, len(mockplugin.instances))
        self.assertTrue(mockplugin.instances[0].initialized)

    def testInstantiationAfterSuccessfulHandshake(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.assertEqual(0, len(mockplugin.instances))

        self.pmgr.filter(self.__class__.handshake_msg1, 'client')
        self.pmgr.filter(self.__class__.handshake_msg2, 'server')
        self.assertEqual(2, len(mockplugin.instances))

    def testMessageHandlerRegistration(self):
        class A(MC3Plugin):
            @msghdlr(0x01, 0x02, 0x03)
            def hdlr1(self, msg, dir):
                pass

            @msghdlr(0x04)
            def hdlr2(self, msg, dir):
                pass

        a = A(21, None, None)
        hdlrs = getattr(a, '_MC3Plugin__hdlrs')
        for msgtype in (0x01, 0x02, 0x03, 0x04):
            self.assertTrue(msgtype in hdlrs)
        self.assertEquals('hdlr1', hdlrs[0x01].__name__)
        self.assertEquals('hdlr2', hdlrs[0x04].__name__)

    def testMessageHandlerFiltering(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr.filter(self.__class__.handshake_msg1, 'client')
        self.pmgr.filter(self.__class__.handshake_msg2, 'server')
        p1 = mockplugin.instances[0]
        p2 = mockplugin.instances[1]
        msg = {'msgtype': 0x03, 'chat_msg': 'foo!'}

        self.assertTrue(self.pmgr.filter(msg, 'client'))
        self.assertEquals(msg, p1.last_msg)
        self.assertEquals(msg, p2.last_msg)

        p1.drop_next_msg = True
        self.assertFalse(
            self.pmgr.filter({
                'msgtype': 0x03,
                'chat_msg': 'bar!'
            }, 'server'))
        self.assertEquals(msg, p2.last_msg)

        p1.drop_next_msg = True
        self.assertTrue(
            self.pmgr.filter({
                'msgtype': 0x04,
                'time': 42
            }, 'client'))
Exemple #16
0
class TestPluginManager(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.pdir = tempfile.mkdtemp()
        cls.pkgdir = tempfile.mkdtemp(dir=cls.pdir)
        with open(os.path.join(cls.pkgdir, '__init__.py'), 'w') as f:
            f.write('')
        sys.path.append(cls.pdir)

    @classmethod
    def tearDownClass(cls):
        sys.path.pop()
        shutil.rmtree(cls.pdir)

    def _write_and_load(self, name, content):
        pfile = os.path.join(*name.split('.'))
        pfile = os.path.join(self.pdir, pfile+'.py')
        print 'loading %s as %s' % (pfile, name)
        with open(pfile, 'w') as f:
            f.write(content)
        mod = load_source(name, pfile)
        return mod

    def setUp(self):
        self.pdir = self.__class__.pdir
        self.cli_proxy, self.srv_proxy = MockMinecraftProxy(), MockMinecraftProxy()

    def tearDown(self):
        if hasattr(self,'pmgr') and self.pmgr != None:
            try: self.pmgr.destroy()
            except Exception as e:
                import traceback
                traceback.print_exc()

    def testInstantiation(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        self.pmgr._instantiate_all()
        self.assertEqual(2, len(mockplugin.instances))
        self.assertTrue(mockplugin.instances[0].initialized)
        self.assertTrue(mockplugin.instances[1].initialized)

    def testDefaultPluginIds(self):
        pcfg = PluginConfig().add('mockplugin')
        self.assertEqual('mockplugin', pcfg.ids[0])

        pcfg.add('mockplugin')
        pcfg.add('mockplugin')
        self.assertEqual('mockplugin1', pcfg.ids[1])
        self.assertEqual('mockplugin2', pcfg.ids[2])

    def testEmptyPluginConfig(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig()
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        self.pmgr._instantiate_all()
        self.assertEqual(0, len(mockplugin.instances))
        
    def testMissingPluginClass(self):
        self._write_and_load('empty', 'from mc3p.plugins import MC3Plugin\n')
        pcfg = PluginConfig().add('empty', 'p')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        # Should log an error, but not raise an exception.
        self.pmgr._instantiate_all()

    def testMultiplePluginClasses(self):
        code = MOCK_PLUGIN_CODE + "class AnotherPlugin(MC3Plugin): pass"
        mockplugin = self._write_and_load('mockplugin', code)
        pcfg = PluginConfig().add('mockplugin', 'p')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        self.pmgr._instantiate_all()
        self.assertEqual(0, len(mockplugin.instances))

    handshake_msg1 = {'msgtype':0x01, 'proto_version': 21, 'username': '******',
                      'nu1': 0, 'nu2': 0, 'nu3': 0, 'nu4': 0, 'nu5': 0, 'nu6': 0}
    handshake_msg2 = {'msgtype':0x01, 'eid': 1, 'reserved': '',
                      'map_seed': 42, 'server_mode': 0, 'dimension': 0,
                      'difficulty': 2, 'world_height': 128, 'max_players': 16}

    def testLoadingPluginInPackage(self):
        pkgname = os.path.basename(self.__class__.pkgdir)
        mockplugin = self._write_and_load(pkgname+'.mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('%s.mockplugin' % pkgname, 'p1')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr._load_plugins()
        self.pmgr._instantiate_all()
        self.assertEqual(1, len(mockplugin.instances))
        self.assertTrue(mockplugin.instances[0].initialized)

    def testInstantiationAfterSuccessfulHandshake(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.assertEqual(0, len(mockplugin.instances))

        self.pmgr.filter(self.__class__.handshake_msg1, 'client')
        self.pmgr.filter(self.__class__.handshake_msg2, 'server')
        self.assertEqual(2, len(mockplugin.instances))

    def testMessageHandlerRegistration(self):
        class A(MC3Plugin):
            @msghdlr(0x01, 0x02, 0x03)
            def hdlr1(self, msg, dir): pass
            @msghdlr(0x04)
            def hdlr2(self, msg, dir): pass
        a = A(21, None, None)
        hdlrs = getattr(a, '_MC3Plugin__hdlrs')
        for msgtype in (0x01, 0x02, 0x03, 0x04):
            self.assertTrue(msgtype in hdlrs)
        self.assertEquals('hdlr1', hdlrs[0x01].__name__)
        self.assertEquals('hdlr2', hdlrs[0x04].__name__)

    def testMessageHandlerFiltering(self):
        mockplugin = self._write_and_load('mockplugin', MOCK_PLUGIN_CODE)
        pcfg = PluginConfig().add('mockplugin', 'p1').add('mockplugin', 'p2')
        self.pmgr = PluginManager(pcfg, self.cli_proxy, self.srv_proxy)
        self.pmgr.filter(self.__class__.handshake_msg1, 'client')
        self.pmgr.filter(self.__class__.handshake_msg2, 'server')
        p1 = mockplugin.instances[0]
        p2 = mockplugin.instances[1]
        msg = {'msgtype': 0x03, 'chat_msg': 'foo!'}

        self.assertTrue(self.pmgr.filter(msg, 'client'))
        self.assertEquals(msg, p1.last_msg)
        self.assertEquals(msg, p2.last_msg)

        p1.drop_next_msg = True
        self.assertFalse(self.pmgr.filter({'msgtype': 0x03, 'chat_msg': 'bar!'}, 'server'))
        self.assertEquals(msg, p2.last_msg)

        p1.drop_next_msg = True
        self.assertTrue(self.pmgr.filter({'msgtype': 0x04, 'time': 42}, 'client'))