plugin.cpp

00001 
00002 /***************************************************************************
00003  *  plugin.cpp - QA Application for dynamic modules and plugins
00004  *
00005  *  Generated: Wed Aug 23 17:00:00 2006
00006  *  Copyright  2006  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <utils/system/dynamic_module/module_dl.h>
00025 #include <utils/system/dynamic_module/module_manager_template.h>
00026 #include <core/plugin.h>
00027 #include <plugin/loader.h>
00028 
00029 #include <iostream>
00030 
00031 using namespace std;
00032 using namespace fawkes;
00033 
00034 /** Method for testing a plugin.
00035  * @param p The plugin to be tested
00036  * @return true if the plugin was tested successfully, false otherwise
00037  */
00038 bool
00039 test_plugin(Plugin *p)
00040 {       
00041   cout << "Plugin name: " << p->name() << endl;
00042 
00043   return true;
00044 }
00045 
00046 /** Test a module.
00047  * @param m the module to be tested
00048  * @return true if the module was tested successfully, false otherwise
00049  */
00050 bool
00051 test_module(Module *m)
00052 {
00053   bool success = true;
00054   try {
00055  
00056     if ( ! m->has_symbol("plugin_factory") ) { // "plugin_factory"
00057       cout << "Doh, symbol not found" << endl;
00058       success = false;
00059     } else {
00060       cout << "Yeah, we got the symbol" << endl;
00061 
00062       PluginFactoryFunc pff = (PluginFactoryFunc)m->get_symbol("plugin_factory");
00063       PluginDestroyFunc pdf = (PluginDestroyFunc)m->get_symbol("plugin_destroy");
00064 
00065       if ( (pff != NULL) && (pdf != NULL) ) {
00066         Plugin *p = pff(NULL);
00067 
00068         success = test_plugin(p);
00069 
00070         pdf(p);
00071         p = NULL;
00072 
00073       } else {
00074         success = false;
00075         if ( pff == NULL ) {
00076           cout << "pff == NULL" << endl;
00077         }
00078         if ( pdf == NULL ) {
00079           cout << "pdf == NULL" << endl;
00080         }
00081       }
00082     }
00083   } catch (Exception &e) {
00084     cout << "Could not open module" << endl;
00085     e.print_trace();
00086     success = false;
00087   }
00088 
00089   return success;
00090 }
00091 
00092 
00093 /** The main test program.
00094  * @param argc the number of arguments
00095  * @param argv the arguments
00096  * @return 0 on success
00097  */
00098 int
00099 main(int argc, char **argv)
00100 {
00101   // Load just the test module
00102 
00103   bool success = true;
00104 
00105   cout << "Running plain module tests" << endl;
00106   ModuleDL *m = new ModuleDL(PLUGINDIR"/test_splugin.so");
00107   try {
00108     m->open();
00109   } catch (Exception &e) {
00110     e.print_trace();
00111     throw;
00112   }
00113   success = test_module(m);
00114   m->close();
00115   delete m;
00116   if ( success ) {
00117     cout << "SUCCESSFULLY tested plain module" << endl;
00118   } else {
00119     cout << "FAILED plain module tests, aborting further tests" << endl;
00120     return -1;
00121   }
00122 
00123   success = true;
00124   cout << endl << endl << "Running ModuleManagerTemplate tests" << endl;
00125   ModuleManagerTemplate<ModuleDL> mm(PLUGINDIR);
00126   Module *mod = mm.open_module("test_plugin.so");
00127   if ( mod == NULL ) {
00128     cout << "Failed to retrieve module from manager" << endl;
00129     success = false;
00130   } else {
00131     cout << "Retrieved module from module manager" << endl;
00132   }
00133 
00134   success = test_module(mod);
00135 
00136   cout << "Testing ref count" << endl;
00137   cout << "RefCount (should be 1): " << mod->get_ref_count() << endl;
00138   cout << "Retrieving module twice, again" << endl;
00139   mm.open_module("test_plugin.so");
00140   mm.open_module("test_plugin.so");
00141   cout << "RefCount (should be 3): " << mod->get_ref_count() << endl;
00142   cout << "Closing module twice" << endl;
00143   mm.close_module(mod);
00144   mm.close_module(mod);
00145   cout << "RefCount (should be 1): " << mod->get_ref_count() << endl;
00146   cout << "Finally closing module" << endl;
00147   mm.close_module(mod);
00148   if ( mm.module_opened("test_plugin.so") ) {
00149     cout << "Plugin still opened, bug!" << endl;
00150     success = false;
00151   } else {
00152     cout << "Plugin has been unloaded from module manager" << endl;
00153   }
00154 
00155 
00156   if ( success ) {
00157     cout << "SUCCESSFULLY tested module manager" << endl;
00158   } else {
00159     cout << "FAILED module manager tests, aborting further tests" << endl;
00160     return 2;
00161   }
00162 
00163 
00164   success = true;
00165   cout << endl << endl << "Running PluginLoader tests" << endl;
00166   PluginLoader *pl = new PluginLoader(PLUGINDIR, NULL);
00167 
00168   Plugin *p;
00169   try {
00170     p = pl->load("test_plugin");
00171     success = test_plugin(p);
00172     pl->unload(p);
00173     success = true;
00174   } catch (PluginLoadException &e) {
00175     cout << "Could not load plugin" << endl;
00176     e.print_trace();
00177     success = false;
00178   }
00179 
00180   delete pl;
00181   if ( success ) {
00182     cout << "SUCCESSFULLY tested PluginLoader" << endl;
00183   } else {
00184     cout << "FAILED module manager tests, aborting further tests" << endl;
00185     return 3;
00186   }
00187 
00188   return 0;
00189 }

Generated on Tue Feb 22 13:31:25 2011 for Fawkes API by  doxygen 1.4.7