Fawkes API  Fawkes Development Version
plugin.cpp
1 
2 /***************************************************************************
3  * plugin.cpp - QA Application for dynamic modules and plugins
4  *
5  * Generated: Wed Aug 23 17:00:00 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/system/dynamic_module/module_dl.h>
25 #include <utils/system/dynamic_module/module_manager_template.h>
26 #include <core/plugin.h>
27 #include <plugin/loader.h>
28 
29 #include <iostream>
30 
31 using namespace std;
32 using namespace fawkes;
33 
34 /** Method for testing a plugin.
35  * @param p The plugin to be tested
36  * @return true if the plugin was tested successfully, false otherwise
37  */
38 bool
39 test_plugin(Plugin *p)
40 {
41  cout << "Plugin name: " << p->name() << endl;
42 
43  return true;
44 }
45 
46 /** Test a module.
47  * @param m the module to be tested
48  * @return true if the module was tested successfully, false otherwise
49  */
50 bool
51 test_module(Module *m)
52 {
53  bool success = true;
54  try {
55 
56  if ( ! m->has_symbol("plugin_factory") ) { // "plugin_factory"
57  cout << "Doh, symbol not found" << endl;
58  success = false;
59  } else {
60  cout << "Yeah, we got the symbol" << endl;
61 
62  PluginFactoryFunc pff = (PluginFactoryFunc)m->get_symbol("plugin_factory");
63  PluginDestroyFunc pdf = (PluginDestroyFunc)m->get_symbol("plugin_destroy");
64 
65  if ( (pff != NULL) && (pdf != NULL) ) {
66  Plugin *p = pff(NULL);
67 
68  success = test_plugin(p);
69 
70  pdf(p);
71  p = NULL;
72 
73  } else {
74  success = false;
75  if ( pff == NULL ) {
76  cout << "pff == NULL" << endl;
77  }
78  if ( pdf == NULL ) {
79  cout << "pdf == NULL" << endl;
80  }
81  }
82  }
83  } catch (Exception &e) {
84  cout << "Could not open module" << endl;
85  e.print_trace();
86  success = false;
87  }
88 
89  return success;
90 }
91 
92 
93 /** The main test program.
94  * @param argc the number of arguments
95  * @param argv the arguments
96  * @return 0 on success
97  */
98 int
99 main(int argc, char **argv)
100 {
101  // Load just the test module
102 
103  bool success = true;
104 
105  cout << "Running plain module tests" << endl;
106  ModuleDL *m = new ModuleDL(PLUGINDIR"/test_splugin.so");
107  try {
108  m->open();
109  } catch (Exception &e) {
110  e.print_trace();
111  throw;
112  }
113  success = test_module(m);
114  m->close();
115  delete m;
116  if ( success ) {
117  cout << "SUCCESSFULLY tested plain module" << endl;
118  } else {
119  cout << "FAILED plain module tests, aborting further tests" << endl;
120  return -1;
121  }
122 
123  success = true;
124  cout << endl << endl << "Running ModuleManagerTemplate tests" << endl;
125  ModuleManagerTemplate<ModuleDL> mm(PLUGINDIR);
126  Module *mod = mm.open_module("test_plugin.so");
127  if ( mod == NULL ) {
128  cout << "Failed to retrieve module from manager" << endl;
129  success = false;
130  } else {
131  cout << "Retrieved module from module manager" << endl;
132  }
133 
134  success = test_module(mod);
135 
136  cout << "Testing ref count" << endl;
137  cout << "RefCount (should be 1): " << mod->get_ref_count() << endl;
138  cout << "Retrieving module twice, again" << endl;
139  mm.open_module("test_plugin.so");
140  mm.open_module("test_plugin.so");
141  cout << "RefCount (should be 3): " << mod->get_ref_count() << endl;
142  cout << "Closing module twice" << endl;
143  mm.close_module(mod);
144  mm.close_module(mod);
145  cout << "RefCount (should be 1): " << mod->get_ref_count() << endl;
146  cout << "Finally closing module" << endl;
147  mm.close_module(mod);
148  if ( mm.module_opened("test_plugin.so") ) {
149  cout << "Plugin still opened, bug!" << endl;
150  success = false;
151  } else {
152  cout << "Plugin has been unloaded from module manager" << endl;
153  }
154 
155 
156  if ( success ) {
157  cout << "SUCCESSFULLY tested module manager" << endl;
158  } else {
159  cout << "FAILED module manager tests, aborting further tests" << endl;
160  return 2;
161  }
162 
163 
164  success = true;
165  cout << endl << endl << "Running PluginLoader tests" << endl;
166  PluginLoader *pl = new PluginLoader(PLUGINDIR, NULL);
167 
168  Plugin *p;
169  try {
170  p = pl->load("test_plugin");
171  success = test_plugin(p);
172  pl->unload(p);
173  success = true;
174  } catch (PluginLoadException &e) {
175  cout << "Could not load plugin" << endl;
176  e.print_trace();
177  success = false;
178  }
179 
180  delete pl;
181  if ( success ) {
182  cout << "SUCCESSFULLY tested PluginLoader" << endl;
183  } else {
184  cout << "FAILED module manager tests, aborting further tests" << endl;
185  return 3;
186  }
187 
188  return 0;
189 }
Plugin interface class.
Definition: plugin.h:33
Fawkes library namespace.
This exception is thrown if the requested plugin could not be loaded.
Definition: loader.h:41
STL namespace.
virtual unsigned int get_ref_count()
Get the reference count of this module.
Definition: module.cpp:202
const char * name() const
Get the name of the plugin.
Definition: plugin.cpp:142
This class manages plugins.
Definition: loader.h:61
void unload(Plugin *plugin)
Unload the given plugin This will unload the given plugin.
Definition: loader.cpp:376
Dynamic module loader for Linux, FreeBSD, and MacOS X.
Definition: module.h:40
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
virtual void * get_symbol(const char *symbol_name)
Get a symbol from the module.
Definition: module.cpp:253
virtual bool has_symbol(const char *symbol_name)
Check if the module has the given symbol.
Definition: module.cpp:230
Plugin * load(const char *plugin_name)
Load a specific plugin The plugin loader is clever and guarantees that every plugin is only loaded on...
Definition: loader.cpp:211