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 <core/plugin.h>
25#include <plugin/loader.h>
26#include <utils/system/dynamic_module/module_dl.h>
27#include <utils/system/dynamic_module/module_manager_template.h>
28
29#include <iostream>
30
31using namespace std;
32using 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 */
38bool
39test_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 */
50bool
51test_module(Module *m)
52{
53 bool success = true;
54 try {
55 if (!m->has_symbol("plugin_factory")) { // "plugin_factory"
56 cout << "Doh, symbol not found" << endl;
57 success = false;
58 } else {
59 cout << "Yeah, we got the symbol" << endl;
60
61 PluginFactoryFunc pff = (PluginFactoryFunc)m->get_symbol("plugin_factory");
62 PluginDestroyFunc pdf = (PluginDestroyFunc)m->get_symbol("plugin_destroy");
63
64 if ((pff != NULL) && (pdf != NULL)) {
65 Plugin *p = pff(NULL);
66
67 success = test_plugin(p);
68
69 pdf(p);
70 p = NULL;
71
72 } else {
73 success = false;
74 if (pff == NULL) {
75 cout << "pff == NULL" << endl;
76 }
77 if (pdf == NULL) {
78 cout << "pdf == NULL" << endl;
79 }
80 }
81 }
82 } catch (Exception &e) {
83 cout << "Could not open module" << endl;
84 e.print_trace();
85 success = false;
86 }
87
88 return success;
89}
90
91/** The main test program.
92 * @param argc the number of arguments
93 * @param argv the arguments
94 * @return 0 on success
95 */
96int
97main(int argc, char **argv)
98{
99 // Load just the test module
100
101 bool success = true;
102
103 cout << "Running plain module tests" << endl;
104 ModuleDL *m = new ModuleDL(PLUGINDIR "/test_splugin.so");
105 try {
106 m->open();
107 } catch (Exception &e) {
108 e.print_trace();
109 throw;
110 }
111 success = test_module(m);
112 m->close();
113 delete m;
114 if (success) {
115 cout << "SUCCESSFULLY tested plain module" << endl;
116 } else {
117 cout << "FAILED plain module tests, aborting further tests" << endl;
118 return -1;
119 }
120
121 success = true;
122 cout << endl << endl << "Running ModuleManagerTemplate tests" << endl;
123 ModuleManagerTemplate<ModuleDL> mm(PLUGINDIR);
124 Module * mod = mm.open_module("test_plugin.so");
125 if (mod == NULL) {
126 cout << "Failed to retrieve module from manager" << endl;
127 success = false;
128 } else {
129 cout << "Retrieved module from module manager" << endl;
130 }
131
132 success = test_module(mod);
133
134 cout << "Testing ref count" << endl;
135 cout << "RefCount (should be 1): " << mod->get_ref_count() << endl;
136 cout << "Retrieving module twice, again" << endl;
137 mm.open_module("test_plugin.so");
138 mm.open_module("test_plugin.so");
139 cout << "RefCount (should be 3): " << mod->get_ref_count() << endl;
140 cout << "Closing module twice" << endl;
141 mm.close_module(mod);
142 mm.close_module(mod);
143 cout << "RefCount (should be 1): " << mod->get_ref_count() << endl;
144 cout << "Finally closing module" << endl;
145 mm.close_module(mod);
146 if (mm.module_opened("test_plugin.so")) {
147 cout << "Plugin still opened, bug!" << endl;
148 success = false;
149 } else {
150 cout << "Plugin has been unloaded from module manager" << endl;
151 }
152
153 if (success) {
154 cout << "SUCCESSFULLY tested module manager" << endl;
155 } else {
156 cout << "FAILED module manager tests, aborting further tests" << endl;
157 return 2;
158 }
159
160 success = true;
161 cout << endl << endl << "Running PluginLoader tests" << endl;
162 PluginLoader *pl = new PluginLoader(PLUGINDIR, NULL);
163
164 Plugin *p;
165 try {
166 p = pl->load("test_plugin");
167 success = test_plugin(p);
168 pl->unload(p);
169 success = true;
170 } catch (PluginLoadException &e) {
171 cout << "Could not load plugin" << endl;
172 e.print_trace();
173 success = false;
174 }
175
176 delete pl;
177 if (success) {
178 cout << "SUCCESSFULLY tested PluginLoader" << endl;
179 } else {
180 cout << "FAILED module manager tests, aborting further tests" << endl;
181 return 3;
182 }
183
184 return 0;
185}
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace() noexcept
Prints trace to stderr.
Definition: exception.cpp:601
Dynamic module loader for Linux, FreeBSD, and MacOS X.
Definition: module.h:41
virtual void * get_symbol(const char *symbol_name)
Get a symbol from the module.
Definition: module.cpp:244
virtual unsigned int get_ref_count()
Get the reference count of this module.
Definition: module.cpp:196
virtual bool has_symbol(const char *symbol_name)
Check if the module has the given symbol.
Definition: module.cpp:222
This exception is thrown if the requested plugin could not be loaded.
Definition: loader.h:39
This class manages plugins.
Definition: loader.h:58
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:200
void unload(Plugin *plugin)
Unload the given plugin This will unload the given plugin.
Definition: loader.cpp:365
Plugin interface class.
Definition: plugin.h:34
void(* PluginDestroyFunc)(Plugin *plugin)
Plugin destructor function for the shared library.
Definition: plugin.h:76
Plugin *(* PluginFactoryFunc)(fawkes::Configuration *)
Plugin loader function for the shared library Do not use directly, rather use the EXPORT_PLUGIN macro...
Definition: plugin.h:69
const char * name() const
Get the name of the plugin.
Definition: plugin.cpp:142
Fawkes library namespace.