00001
00011 #include <cppunit/ui/text/TestRunner.h>
00012 #include <cppunit/TextOutputter.h>
00013 #include <cppunit/extensions/TestFactoryRegistry.h>
00014 #include <cppunit/extensions/HelperMacros.h>
00015 #include <cppunit/TestAssert.h>
00016
00017 #include "../ModuleManager.h"
00018 #include "../Properties.h"
00019
00020 using namespace std;
00021
00022
00023 class ModuleManagerTests
00024 : public CppUnit::TestFixture
00025 {
00026 CPPUNIT_TEST_SUITE(ModuleManagerTests);
00027 CPPUNIT_TEST(test_load);
00028 CPPUNIT_TEST(test_unload);
00029 CPPUNIT_TEST(test_unloadAll);
00030 CPPUNIT_TEST(test_symbol);
00031 CPPUNIT_TEST(test_setLoadpath);
00032 CPPUNIT_TEST(test_getLoadPath);
00033 CPPUNIT_TEST(test_addLoadpath);
00034 CPPUNIT_TEST(test_getLoadedModules);
00035 CPPUNIT_TEST(test_getLoadableModules);
00036 CPPUNIT_TEST(test_allowAbsolutePath);
00037 CPPUNIT_TEST(test_disallowAbsolutePath);
00038 CPPUNIT_TEST(test_allowModuleDownload);
00039 CPPUNIT_TEST(test_disallowModuleDownload);
00040 CPPUNIT_TEST(test_findFile);
00041 CPPUNIT_TEST(test_fileExist);
00042 CPPUNIT_TEST(test_getInitFuncName);
00043 CPPUNIT_TEST_SUITE_END();
00044
00045 private:
00046
00047 RTC::ModuleManager* m_pModMgr;
00048
00049 public:
00050
00051
00052
00053
00054 ModuleManagerTests()
00055 {
00056
00057 }
00058
00059 ~ModuleManagerTests()
00060 {
00061 }
00062
00063
00064
00065
00066
00067 virtual void setUp()
00068 {
00069
00070 const char* default_properties[] = {
00071 "manager.modules.config_ext", "so",
00072 "manager.modules.config_path", "/etc/rtc",
00073 "manager.modules.detect_loadable", "Yes",
00074 "manager.modules.load_path", "/usr/lib, /usr/local/lib, /usr/local/lib/rtc",
00075 "manager.modules.init_func_suffix", "Init",
00076 "manager.modules.init_func_prefix", "",
00077 "manager.modules.abs_path_allowed", "Yes",
00078 "manager.modules.download_allowed", "Yes",
00079 "manager.modules.download_dir", "/tmp/rtc",
00080 "manager.modules.download_cleanup", "Yes",
00081 "manager.modules.preload", "",
00082 ""
00083 };
00084
00085 RTC::Properties prop(default_properties);
00086 m_pModMgr = new RTC::ModuleManager(prop);
00087 m_pModMgr->load("libRTC.so");
00088 m_pModMgr->load("libm.so");
00089 }
00090
00091
00092 virtual void tearDown()
00093 {
00094 m_pModMgr->unloadAll();
00095 delete m_pModMgr;
00096 }
00097
00098
00099
00100 void test_load() {
00101 string libname;
00102
00103 try {
00104
00105
00106
00107 libname = m_pModMgr->load("libRTC.so");
00108 CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
00109
00110 libname = m_pModMgr->load("libRTC.so");
00111 CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
00112
00113 libname = m_pModMgr->load("libRTC.so");
00114 CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
00115
00116
00117 libname = m_pModMgr->load("/usr/lib/libRTC.so");
00118 CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
00119
00120
00121 libname = m_pModMgr->load("/usr//users/kurihara/../kurihara/Components/test/test.so");
00122 CPPUNIT_ASSERT(libname == "/usr//users/kurihara/../kurihara/Components/test/test.so");
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 }
00145 catch (RTC::ModuleManager::Error& e)
00146 {
00147 std::cout << "Error " << e.reason << std::endl;
00148 }
00149 catch (RTC::ModuleManager::NotFound& e)
00150 {
00151 std::cout << "NotFound " << e.name << std::endl;
00152 }
00153 catch (...)
00154 {
00155 std::cout << "other exception" << std::endl;
00156 }
00157
00158 }
00159
00160
00161
00162 void test_unload() {
00163 try {
00164 string libname;
00165 libname = m_pModMgr->load("libRTC.so");
00166 CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
00167
00168
00169 m_pModMgr->unload("/usr/lib/libRTC.so");
00170 m_pModMgr->unload("/usr/lib/libm.so");
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 }
00185 catch (RTC::ModuleManager::Error& e)
00186 {
00187 std::cout << "Error " << e.reason << std::endl;
00188 }
00189 catch (RTC::ModuleManager::NotFound& e)
00190 {
00191 std::cout << "NotFound " << e.name << std::endl;
00192 }
00193 catch (...)
00194 {
00195 std::cout << "other exception" << std::endl;
00196 }
00197 }
00198
00199
00200
00201 void test_unloadAll() {
00202 try {
00203 m_pModMgr->unloadAll();
00204 m_pModMgr->unloadAll();
00205 m_pModMgr->unloadAll();
00206 }
00207 catch (RTC::ModuleManager::Error& e)
00208 {
00209 std::cout << "Error " << e.reason << std::endl;
00210 }
00211 catch (RTC::ModuleManager::NotFound& e)
00212 {
00213 std::cout << "NotFound " << e.name << std::endl;
00214 }
00215 catch (...)
00216 {
00217 std::cout << "other exception" << std::endl;
00218 }
00219 }
00220
00221
00222
00223 void test_symbol() {
00224
00225
00226 string libname;
00227
00228 try
00229 {
00230 libname = m_pModMgr->load("libm.so");
00231 }
00232 catch (RTC::ModuleManager::Error& e)
00233 {
00234 std::cout << "Error " << e.reason << std::endl;
00235 }
00236 catch (RTC::ModuleManager::NotFound& e)
00237 {
00238 std::cout << "NotFound " << e.name << std::endl;
00239 }
00240 catch (...)
00241 {
00242 std::cout << "other exception" << std::endl;
00243 }
00244
00245 typedef double (*cosine)(double);
00246 typedef double (*sine)(double);
00247
00248 cosine _cos;
00249 sine _sin;
00250
00251 _cos = (cosine) m_pModMgr->symbol(libname, "cos");
00252
00253
00254 CPPUNIT_ASSERT_MESSAGE("load error: cos(0.0)",
00255 (*_cos)(0.0) == 1.0);
00256 CPPUNIT_ASSERT_MESSAGE("load error: cos(pi/2)",
00257 (*_cos)(3.141592653589793238462643383279/2) < 0.01);
00258
00259 _sin = (sine) m_pModMgr->symbol(libname, "sin");
00260
00261
00262 CPPUNIT_ASSERT_MESSAGE("load error: sin(0.0)",
00263 (*_sin)(0.0) == 0.0);
00264 CPPUNIT_ASSERT_MESSAGE("load error: sin(pi/2)",
00265 (*_sin)(3.141592653589793238462643383279/2) == 1.0);
00266
00267
00268
00269 libname = m_pModMgr->load("/usr/users/kurihara/RTM/Components/test/test.so");
00270 typedef void (*testHello)();
00271
00272 testHello _th;
00273
00274 _th = (testHello) m_pModMgr->symbol(libname, "_ZN4test5helloEv");
00275 (*_th)();
00276 }
00277
00278
00279
00280 void test_setLoadpath() {
00281 vector<string> set_loadpath, get_loadpath;
00282 set_loadpath.push_back("/usr");
00283 set_loadpath.push_back("/usr/lib");
00284 set_loadpath.push_back("/usr/local/lib");
00285 set_loadpath.push_back("/tmp");
00286 set_loadpath.push_back("/usr/users/aaaaaaaaaaa/bbbbbbbbbbbb/ccccccccccccc/ddddddddddddd/eeeeeeeeeeeee/ffffffffffffffff/ggggggggggggg/hhhhhhhhhhhhh/iiiiiiiiiii");
00287
00288 m_pModMgr->setLoadpath(set_loadpath);
00289 get_loadpath = m_pModMgr->getLoadPath();
00290
00291 unsigned int size = get_loadpath.size();
00292
00293 for (unsigned int i = 0; i < size; i++) {
00294 CPPUNIT_ASSERT(set_loadpath[i] == get_loadpath[i]);
00295 }
00296 }
00297
00298
00299
00300 void test_getLoadPath() {
00301 vector<string> getlist, expectation;
00302 expectation.push_back("/usr/lib");
00303 expectation.push_back("/usr/local/lib");
00304 expectation.push_back("/usr/local/lib/rtc");
00305 getlist = m_pModMgr->getLoadPath();
00306
00307 for (unsigned int i = 0; i < getlist.size(); i++) {
00308 CPPUNIT_ASSERT(getlist[i] == expectation[i]);
00309 }
00310 }
00311
00312
00313
00314 void test_addLoadpath() {
00315 vector<string> getlist, expectation, add_path;
00316 expectation.push_back("/usr/lib");
00317 expectation.push_back("/usr/local/lib");
00318 expectation.push_back("/usr/local/lib/rtc");
00319 expectation.push_back("/tmp");
00320 expectation.push_back("/hoge");
00321 expectation.push_back("/hoge/hoge");
00322
00323 add_path.push_back("/tmp");
00324 add_path.push_back("/hoge");
00325 add_path.push_back("/hoge/hoge");
00326 m_pModMgr->addLoadpath(add_path);
00327
00328 getlist = m_pModMgr->getLoadPath();
00329 for (unsigned int i = 0; i < getlist.size(); i++) {
00330 CPPUNIT_ASSERT(getlist[i] == expectation[i]);
00331 }
00332 }
00333
00334
00335
00336 void test_getLoadedModules() {
00337 vector<string> get_modlist;
00338
00339 get_modlist = m_pModMgr->getLoadedModules();
00340 CPPUNIT_ASSERT(get_modlist[0] == "/usr/lib/libRTC.so");
00341 CPPUNIT_ASSERT(get_modlist[1] == "/usr/lib/libm.so");
00342 }
00343
00344
00345
00346 void test_getLoadableModules() {
00347
00348
00349 vector<string> get_modlist;
00350 get_modlist = m_pModMgr->getLoadableModules();
00351 for (unsigned int i = 0; i < get_modlist.size(); i++) {
00352 cout << get_modlist[i] << endl;
00353 }
00354
00355 }
00356
00357
00358
00359 void test_allowAbsolutePath() {
00360 string libname;
00361 try {
00362
00363
00364 m_pModMgr->allowAbsolutePath();
00365 libname = m_pModMgr->load("libRTC.so");
00366 m_pModMgr->unload(libname);
00367
00368 libname = m_pModMgr->load("/usr/lib/libm.so");
00369 m_pModMgr->unload(libname);
00370
00371 libname = m_pModMgr->load("../lib/libm.so");
00372 m_pModMgr->unload(libname);
00373
00374
00375 m_pModMgr->disallowAbsolutePath();
00376 libname = m_pModMgr->load("../lib/libRTC.so");
00377 m_pModMgr->unload(libname);
00378
00379
00380
00381
00382 }
00383 catch (RTC::ModuleManager::Error& e)
00384 {
00385 std::cout << "Error " << e.reason << std::endl;
00386 }
00387 catch (RTC::ModuleManager::NotFound& e)
00388 {
00389 std::cout << "NotFound " << e.name << std::endl;
00390 }
00391 catch (...)
00392 {
00393 std::cout << "other exception" << std::endl;
00394 }
00395 }
00396
00397
00398
00399 void test_disallowAbsolutePath() {
00400
00401 }
00402
00403
00404
00405 void test_allowModuleDownload() {
00406
00407 }
00408
00409
00410
00411 void test_disallowModuleDownload() {
00412
00413 }
00414
00415
00416
00417 void test_findFile() {
00418
00419 std::string result;
00420 std::vector<std::string> path;
00421 path.push_back("/lib");
00422 path.push_back("/usr/lib");
00423 path.push_back("/usr/local/lib");
00424
00425 result = m_pModMgr->findFile("libm.so", path);
00426 CPPUNIT_ASSERT_MESSAGE("fileFile error: libm.so",
00427 result == "/usr/lib/libm.so");
00428
00429 result = m_pModMgr->findFile("libc.so", path);
00430 CPPUNIT_ASSERT_MESSAGE("fileFile error: libc.so",
00431 result == "/usr/lib/libc.so");
00432
00433 result = m_pModMgr->findFile("libACE.so", path);
00434 CPPUNIT_ASSERT_MESSAGE("fileFile error: libACE.so",
00435 result == "/usr/lib/libACE.so");
00436
00437 result = m_pModMgr->findFile("hosts", path);
00438 CPPUNIT_ASSERT_MESSAGE("fileFile error: hosts",
00439 result == "");
00440
00441 result = m_pModMgr->findFile("munyamunya", path);
00442 CPPUNIT_ASSERT_MESSAGE("fileFile error: munyamunya",
00443 result == "");
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 }
00454
00455
00456
00457 void test_fileExist() {
00458
00459 CPPUNIT_ASSERT(m_pModMgr->fileExist("../../../../../../../../usr/lib/libm.so"));
00460 CPPUNIT_ASSERT(m_pModMgr->fileExist("/usr/lib/libm.so"));
00461
00462
00463
00464 }
00465
00466
00467
00468 void test_getInitFuncName() {
00469 std::string result;
00470
00471
00472 result = m_pModMgr->getInitFuncName("Manipulator");
00473 CPPUNIT_ASSERT_MESSAGE("getInitFuncName error: Manipulator",
00474 result == "ManipulatorInit");
00475
00476 result = m_pModMgr->getInitFuncName("PHANToM");
00477 CPPUNIT_ASSERT_MESSAGE("getInitFuncName error: PHANToM",
00478 result == "PHANToMInit");
00479
00480 }
00481 };
00482
00483
00484
00485
00486
00487 CPPUNIT_TEST_SUITE_REGISTRATION(ModuleManagerTests);
00488
00489
00490
00491 int main(int argc, char* argv[])
00492 {
00493 CppUnit::TextUi::TestRunner runner;
00494
00495 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00496 CppUnit::Outputter* outputter =
00497 new CppUnit::TextOutputter(&runner.result(), std::cout);
00498 runner.setOutputter(outputter);
00499
00500 return runner.run();
00501 }