ModuleManagerTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: ModuleManagerTests.cpp,v $
00023  * Revision 1.2  2008/05/01 03:42:41  arafune
00024  * Modified some tests.
00025  *
00026  * Revision 1.1  2007/12/20 07:50:17  arafune
00027  * *** empty log message ***
00028  *
00029  * Revision 1.1  2006/11/27 08:33:02  n-ando
00030  * TestSuites are devided into each directory.
00031  *
00032  * Revision 1.2  2006/10/26 01:34:57  kurihara
00033  * test program of class ModuelManager.
00034  *
00035  * Revision 1.1  2006/09/20 08:52:19  n-ando
00036  * The first commit.
00037  *
00038  */
00039 
00040 #ifndef ModuleManager_cpp
00041 #define ModuleManager_cpp
00042 
00043 #include <cppunit/ui/text/TestRunner.h>
00044 #include <cppunit/TextOutputter.h>
00045 #include <cppunit/extensions/TestFactoryRegistry.h>
00046 #include <cppunit/extensions/HelperMacros.h>
00047 #include <cppunit/TestAssert.h>
00048 #include <fstream>
00049 #include <rtm/ModuleManager.h>
00050 #include <coil/Properties.h>
00051 
00056 namespace ModuleManager
00057 {
00058   class ModuleManagerTests
00059     : public CppUnit::TestFixture
00060   {
00061     CPPUNIT_TEST_SUITE(ModuleManagerTests);
00062     CPPUNIT_TEST(test_load);
00063     CPPUNIT_TEST(test_load_inexistent_on_load_path);
00064     CPPUNIT_TEST(test_unload);
00065     CPPUNIT_TEST(test_unloadAll);
00066     CPPUNIT_TEST(test_symbol);
00067     CPPUNIT_TEST(test_setLoadpath_and_getLoadPath);
00068     CPPUNIT_TEST(test_addLoadpath);
00069     //CPPUNIT_TEST(test_getLoadedModules);
00070     CPPUNIT_TEST(test_getLoadableModules);
00071     CPPUNIT_TEST(test_allowAbsolutePath_and_disallowAbsolutePath);
00072     //CPPUNIT_TEST(test_allowModuleDownload);
00073     //CPPUNIT_TEST(test_disallowModuleDownload);
00074     CPPUNIT_TEST(test_findFile);
00075     CPPUNIT_TEST(test_fileExist);
00076     CPPUNIT_TEST(test_getInitFuncName);
00077     CPPUNIT_TEST_SUITE_END();
00078                 
00079   private:
00080     RTC::ModuleManager* m_pModMgr;
00081                 
00082   private:
00083     bool isFound(const std::vector<coil::Properties>& path, const std::string& mod)
00084     {
00085       for (int i(0), len(path.size()); i < len; ++i)
00086         {
00087           if (mod == path[i]["file_path"]) return true;
00088         }
00089       return false;
00090     }
00091         
00092   public:
00096     ModuleManagerTests()
00097     {
00098     }
00099                 
00103     ~ModuleManagerTests()
00104     {
00105     }
00106                 
00110     virtual void setUp()
00111     {
00112       const char* default_properties[] =
00113         {
00114           "manager.modules.config_ext", "so",
00115           "manager.modules.config_path", "/etc/rtc",
00116           "manager.modules.detect_loadable", "Yes",
00117           "manager.modules.load_path",
00118           "../../.libs, .libs, /usr/lib, /usr/local/lib, /usr/local/lib/rtc",
00119           "manager.modules.init_func_suffix", "Init",
00120           "manager.modules.init_func_prefix", "",
00121           "manager.modules.abs_path_allowed", "Yes",
00122           "manager.modules.download_allowed", "Yes",
00123           "manager.modules.download_dir", "/tmp/rtc",
00124           "manager.modules.download_cleanup", "Yes",
00125           "manager.modules.preload", "",
00126           ""
00127         };
00128                         
00129       coil::Properties prop(default_properties);
00130       m_pModMgr = new RTC::ModuleManager(prop);
00131                         
00132       // �����ɥѥ����libRTC.so��¸�ߤ��뤳�Ȥ��ǧ���Ƥ���
00133       m_pModMgr->load("libRTC.so");
00134                         
00135       // �����ɥѥ����libm.so��¸�ߤ��뤳�Ȥ��ǧ���Ƥ���
00136       m_pModMgr->load("libm.so");
00137     }
00138                 
00142     virtual void tearDown()
00143     {
00144       m_pModMgr->unloadAll();
00145       delete m_pModMgr;
00146     }
00147                 
00148     /* tests for string load(const string& file_name) */
00158     void test_load()
00159     {
00160       try
00161         {
00162           // Success case
00163           // �ե�����̾�������ꤷ�����ˡ������ɤǤ��뤫��
00164           //std::string str = m_pModMgr->load("libRTC.so");
00165           //std::cout << "\nstr=(" << str << std::endl;
00166           // ���󥹥ȡ���Ķ��ˤ�äơ��ե�����ѥ����Ѳ�����Τǥ����Ȥˤ���
00167 //        CPPUNIT_ASSERT_EQUAL(
00168 //                             std::string("/usr/local/lib/libRTC.so"),
00169 //                             std::string("../../.libs/libRTC.so"),
00170 //                             std::string("/usr/lib/libRTC.so"),
00171 //                             m_pModMgr->load("libRTC.so"));
00172                                 
00173 /*
00174           // �ե�����̾�����Хѥ��ǻ��ꤷ�����ˡ�����˥����ɤǤ��뤫��
00175           CPPUNIT_ASSERT_EQUAL(
00176 //                             std::string("/usr/local/lib/libRTC.so"),
00177                                std::string("../../.libs/libRTC.so"),
00178                                m_pModMgr->load("/usr/local/lib/libRTC.so"));
00179                                 
00180           // �ǥ��쥯�ȥ�ζ��ڤ�ʸ����"//"��"../"��������
00181           CPPUNIT_ASSERT_EQUAL(
00182 //                             std::string("/usr/local//lib/../lib/libRTC.so"),
00183 //                             m_pModMgr->load("/usr/local//lib/../lib/libRTC.so"));
00184                                std::string("../..//.libs/../libs/libRTC.so"),
00185                                m_pModMgr->load("../..//.libs/../libs/libRTC.so"));
00186 */
00187         }
00188       catch (RTC::ModuleManager::Error& e)
00189         {
00190           CPPUNIT_FAIL("Error" + e.reason);
00191         }
00192       catch (RTC::ModuleManager::NotFound& e)
00193         {
00194           CPPUNIT_FAIL("NotFound" + e.name);
00195         }
00196       catch (RTC::ModuleManager::NotAllowedOperation& e)
00197         {
00198           CPPUNIT_FAIL("NotAllowedOperation");
00199         }
00200       catch (RTC::ModuleManager::InvalidArguments& e)
00201         {
00202           CPPUNIT_FAIL("InvalidArguments");
00203         }
00204       catch (RTC::ModuleManager::FileNotFound& e)
00205         {
00206           CPPUNIT_FAIL("FileNotFound" + e.name);
00207         }
00208       catch (...)
00209         {
00210           CPPUNIT_FAIL("Other Exception");
00211         }
00212                         
00213       // Failure case
00214       bool notThrown;
00215 
00216       // �����ɥѥ���˥ե������¸�ߤ��뤬����ĥ�Ҥ�Ŭ�ѳ��Ǥ�����ˡ��տޤɤ�������ɼ��Ԥ��뤫��
00217       notThrown = false;
00218       try
00219         {
00220           m_pModMgr->load("libm.a");
00221           notThrown = true;
00222         }
00223       catch (...) {}
00224       if (notThrown) CPPUNIT_FAIL("Exception not thrown.");
00225 
00226       // ¸�ߤ��ʤ��ե�����ξ��
00227       notThrown = false;
00228       try
00229         {
00230           m_pModMgr->load("inexist-file");
00231           notThrown = true;
00232         }
00233       catch (...) {}
00234       if (notThrown) CPPUNIT_FAIL("Exception not thrown.");
00235     }
00236                 
00242     void test_load_inexistent_on_load_path()
00243     {
00244 
00245       std::vector<std::string> loadPath = m_pModMgr->getLoadPath();
00246                         
00247       // setLoadPath()�����Ѥ��ơ������ɥѥ��򥯥ꥢ����
00248       std::vector<std::string> emptyPath;
00249       m_pModMgr->setLoadpath(emptyPath);
00250                         
00251       // �����ɥѥ����¸�ߤ��ʤ��⥸�塼��Υ����ɤ��ߤ���硢�տޤɤ�������ɼ��Ԥ��뤫��
00252       // �ʥ����ɥѥ��򥯥ꥢ�������֤ʤΤǡ��⥸�塼������ɤ˼��Ԥ���Ϥ���
00253       bool notThrown = false;
00254       try
00255         {
00256           m_pModMgr->load("libm.so");
00257           notThrown = true;
00258         }
00259       catch (...) {}
00260       if (notThrown) CPPUNIT_FAIL("Exception not thrown.");
00261                         
00262       // ���Υ����ɥѥ������ꤷ�ʤ���
00263       m_pModMgr->setLoadpath(loadPath);
00264                         
00265       // �⥸�塼��Υ����ɤ���������Ϥ�
00266       std::string modName = m_pModMgr->load("libm.so");
00267       CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName));
00268     }
00269                 
00279     void test_unload()
00280     {
00281 
00282       // �⥸�塼�������ɤ��Ƥ���
00283       std::string modName1 = m_pModMgr->load("libRTC.so");
00284       CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName1));
00285                         
00286       std::string modName2 = m_pModMgr->load("libm.so");
00287       CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName2));
00288                         
00289       // Success case
00290       // �����ɤ��Ƥ������⥸�塼�����������������ɤǤ��뤫��
00291       m_pModMgr->unload(modName1);
00292       CPPUNIT_ASSERT(! isFound(m_pModMgr->getLoadedModules(), modName1));
00293                         
00294       // ��������ɤ��Ƥ��ʤ��⥸�塼��ϡ��ʤ���������ɤ��줺�˻ĤäƤ��뤫��
00295       CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName2));
00296                         
00297                         
00298       // Failure case
00299       // ���Хѥ�����ꤻ�����ե�����̾�������ꤷ�����ˡ��տޤɤ���˥�������ɼ��Ԥ��뤫��
00300       try
00301         {
00302           m_pModMgr->unload("libm.so");
00303           CPPUNIT_FAIL("Exception not thrown.");
00304         }
00305       catch (RTC::ModuleManager::NotFound expected) {}
00306                         
00307       // �����ɤ��Ƥ��ʤ��⥸�塼��Υ�������ɤ��ߤ���硢�տޤɤ���˼��Ԥ��뤫��
00308       try
00309         {
00310           m_pModMgr->unload("non-loaded-module.so");
00311           CPPUNIT_FAIL("Exception not thrown.");
00312         }
00313       catch (RTC::ModuleManager::NotFound expected) {}
00314                         
00315       // ��������ɺѤߤΥ⥸�塼��򡢤���˥�������ɤ��褦�Ȼ�ߤ���硢�տޤɤ���˼��Ԥ��뤫��
00316       try
00317         {
00318           m_pModMgr->unload(modName1);
00319           CPPUNIT_FAIL("Exception not thrown.");
00320         }
00321       catch (RTC::ModuleManager::NotFound expected) {}
00322     }
00323                 
00329     void test_unloadAll()
00330     {
00331       // �⥸�塼�������ɤ��Ƥ���
00332       std::string modName1 = m_pModMgr->load("libRTC.so");
00333       CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName1));
00334                         
00335       std::string modName2 = m_pModMgr->load("libm.so");
00336       CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName2));
00337                         
00338       // unloadAll()�ˤ�äơ������ɺѤߤΥ⥸�塼�뤬���٤ƥ�������ɤ���뤫��
00339       m_pModMgr->unloadAll();
00340       CPPUNIT_ASSERT(! isFound(m_pModMgr->getLoadedModules(), modName1));
00341       CPPUNIT_ASSERT(! isFound(m_pModMgr->getLoadedModules(), modName2));
00342     }
00343                 
00350     void test_symbol()
00351     {
00352       typedef double (*FUNCTION_COS)(double);
00353       typedef double (*FUNCTION_SIN)(double);
00354 
00355       // �⥸�塼�������ɤ��Ƥ���
00356       std::string modName = m_pModMgr->load("libm.so");
00357       CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName));
00358                         
00359       // "cos"�ؿ��ؤΥ���ܥ���������
00360       FUNCTION_COS func_cos = (FUNCTION_COS) m_pModMgr->symbol(modName, "cos");
00361       CPPUNIT_ASSERT(func_cos != NULL);
00362                         
00363       // "sin"�ؿ��ؤΥ���ܥ���������
00364       FUNCTION_SIN func_sin = (FUNCTION_SIN) m_pModMgr->symbol(modName, "sin");
00365       CPPUNIT_ASSERT(func_sin != NULL);
00366                         
00367       // ������������ܥ�ؤθƽФ�����˹Ԥ��뤫��
00368       CPPUNIT_ASSERT_EQUAL(1.0, (*func_cos)(0.0));
00369       CPPUNIT_ASSERT_EQUAL(0.0, (*func_sin)(0.0));
00370     }
00371                 
00378     void test_setLoadpath_and_getLoadPath()
00379     {
00380 
00381       std::vector<std::string> loadPath;
00382       loadPath.push_back("/usr");
00383       loadPath.push_back("/usr/lib");
00384                         
00385       // setLoadpath()�����ꤷ���ѥ���getLoadPath()�Ǽ����Ǥ��뤫��
00386       m_pModMgr->setLoadpath(loadPath);
00387       std::vector<std::string> loadPathRet = m_pModMgr->getLoadPath();
00388       CPPUNIT_ASSERT_EQUAL(2, (int) loadPathRet.size());
00389       CPPUNIT_ASSERT_EQUAL(std::string("/usr"), loadPathRet[0]);
00390       CPPUNIT_ASSERT_EQUAL(std::string("/usr/lib"), loadPathRet[1]);
00391 
00392     }
00393                 
00399     void test_addLoadpath()
00400     {
00401       std::vector<std::string> loadPath1;
00402       loadPath1.push_back("/usr");
00403       loadPath1.push_back("/usr/lib");
00404       std::sort(loadPath1.begin(), loadPath1.end());
00405 
00406       std::vector<std::string> loadPath2;
00407       loadPath2.push_back("/usr/local/lib");
00408       loadPath2.push_back("/usr/local/lib/rtc");
00409       std::sort(loadPath2.begin(), loadPath2.end());
00410                         
00411       std::vector<std::string> expectedPath;
00412       expectedPath.push_back("/usr");
00413       expectedPath.push_back("/usr/lib");
00414       expectedPath.push_back("/usr/local/lib");
00415       expectedPath.push_back("/usr/local/lib/rtc");
00416       std::sort(expectedPath.begin(), expectedPath.end());
00417                         
00418       // �ޤ�������֤Υ����ɥѥ������ꤷ�Ƥ���
00419       std::vector<std::string> loadPathRet;
00420       m_pModMgr->setLoadpath(loadPath1);
00421       loadPathRet = m_pModMgr->getLoadPath();
00422       CPPUNIT_ASSERT_EQUAL(2, (int) loadPathRet.size());
00423       for (int i = 0; i < 2; ++i) {
00424         CPPUNIT_ASSERT_EQUAL(loadPath1[i], loadPathRet[i]);
00425       }
00426                         
00427       // �����������ɥѥ����ɲäǤ��뤫��
00428       m_pModMgr->addLoadpath(loadPath2);
00429       loadPathRet = m_pModMgr->getLoadPath();
00430       CPPUNIT_ASSERT_EQUAL(4, (int) loadPathRet.size());
00431       for (int i = 0; i < 4; ++i) {
00432         CPPUNIT_ASSERT_EQUAL(expectedPath[i], loadPathRet[i]);
00433       }
00434     }
00435                 
00439     void test_getLoadedModules()
00440     {
00441       // ¾�ƥ�����ǻ��Ѥ���Ƥ��ꡢ�����Ƿ�ͤ��ΤȤ���
00442     }
00443                 
00447     void test_getLoadableModules()
00448     {
00449       std::vector<coil::Properties> props = m_pModMgr->getLoadableModules();
00450       int len = props.size();
00451       CPPUNIT_ASSERT(len > 0);
00452 
00453       bool bret = false;
00454       for(int i(0); i<len; ++i)
00455       {
00456         if(props[i].getProperty("module_file_name") == "DummyModule1.so")
00457         {
00458           bret = true;
00459           break;
00460         }
00461       }
00462       CPPUNIT_ASSERT(bret);
00463     }
00464                 
00471     void test_allowAbsolutePath_and_disallowAbsolutePath()
00472     {
00473       // ���Хѥ��������Ĥ������֤ǡ����Хѥ�����ǥ⥸�塼������ɤǤ��뤫��
00474       m_pModMgr->allowAbsolutePath();
00475       std::string modName = m_pModMgr->load("/usr/lib/libm.so");
00476       CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName));
00477                         
00478       // ���Хѥ������ػߤ������֤ǡ����Хѥ�����ǥ⥸�塼������ɤ��ߤơ��տޤɤ��꼺�Ԥ��뤫��
00479       m_pModMgr->unloadAll(); // ���ä��󥢥�����ɤ��Ƥ���
00480       m_pModMgr->disallowAbsolutePath();
00481       try
00482         {
00483           m_pModMgr->load("/usr/lib/libm.so");
00484           CPPUNIT_FAIL("Exception not thrown.");
00485         }
00486       catch (RTC::ModuleManager::NotAllowedOperation expected) {}
00487     }
00488                 
00492     void test_allowModuleDownload()
00493     {
00494       // ����������ɤˤ��⥸�塼������ɽ�����̤�����Τ��ᡢ�ƥ��Ȥ�̤����
00495     }
00496                 
00500     void test_disallowModuleDownload()
00501     {
00502       // ����������ɤˤ��⥸�塼������ɽ�����̤�����Τ��ᡢ�ƥ��Ȥ�̤����
00503     }
00504                 
00511     void test_findFile()
00512     {
00513 
00514       std::vector<std::string> path;
00515       path.push_back("/usr/lib");
00516                         
00517       // ���ꤷ���ѥ���˥ե����뤬¸�ߤ����硢������̵ͭȽ�ꤷ���ѥ�������Ǥ��뤫��
00518       CPPUNIT_ASSERT_EQUAL(std::string("/usr/lib/libm.so"),
00519                            m_pModMgr->findFile("libm.so", path));
00520                         
00521       // ���ꤷ���ѥ���˥ե����뤬¸�ߤ��ʤ���硢������̵ͭȽ�ꤷ����ʸ���������Ǥ��뤫��
00522       CPPUNIT_ASSERT_EQUAL(std::string(""),
00523                            m_pModMgr->findFile("inexist-file.so", path));
00524     }
00525                 
00533     void test_fileExist()
00534     {
00535       // ¸�ߤ���ե��������ꤷ�����ˡ�������Ƚ�ꤵ��뤫��
00536       CPPUNIT_ASSERT(m_pModMgr->fileExist("/usr/lib/libm.so"));
00537 
00538       // ¸�ߤ��ʤ��ե��������ꤷ�����ˡ�������Ƚ�ꤵ��뤫��
00539       CPPUNIT_ASSERT(! m_pModMgr->fileExist("/usr/lib/inexistent-file.so"));
00540                         
00541       // ¸�ߤ���ե�������//�ס�../�פ������ä��ѥ�ɽ���ǻ��ꤷ����硢������Ƚ�ꤵ��뤫��
00542       CPPUNIT_ASSERT(m_pModMgr->fileExist("/usr//lib/../lib/libm.so"));
00543     }
00544                 
00550     void test_getInitFuncName()
00551     {
00552       // ������ؿ�̾�������������Ǥ��뤫��
00553       CPPUNIT_ASSERT_EQUAL(std::string("ManipulatorInit"),
00554                            m_pModMgr->getInitFuncName("Manipulator"));
00555                         
00556       CPPUNIT_ASSERT_EQUAL(std::string("PHANToMInit"),
00557                            m_pModMgr->getInitFuncName("PHANToM"));
00558     }
00559                 
00560   };
00561 }; // namespace ModuleManager
00562 
00563 /*
00564  * Register test suite
00565  */
00566 CPPUNIT_TEST_SUITE_REGISTRATION(ModuleManager::ModuleManagerTests);
00567 
00568 #ifdef LOCAL_MAIN
00569 int main(int argc, char* argv[])
00570 {
00571 
00572   FORMAT format = TEXT_OUT;
00573   int target = 0;
00574   std::string xsl;
00575   std::string ns;
00576   std::string fname;
00577   std::ofstream ofs;
00578 
00579   int i(1);
00580   while (i < argc)
00581     {
00582       std::string arg(argv[i]);
00583       std::string next_arg;
00584       if (i + 1 < argc) next_arg = argv[i + 1];
00585       else              next_arg = "";
00586 
00587       if (arg == "--text") { format = TEXT_OUT; break; }
00588       if (arg == "--xml")
00589         {
00590           if (next_arg == "")
00591             {
00592               fname = argv[0];
00593               fname += ".xml";
00594             }
00595           else
00596             {
00597               fname = next_arg;
00598             }
00599           format = XML_OUT;
00600           ofs.open(fname.c_str());
00601         }
00602       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00603       if ( arg == "--cerr"      ) { target = 1; break; }
00604       if ( arg == "--xsl"       )
00605         {
00606           if (next_arg == "") xsl = "default.xsl"; 
00607           else                xsl = next_arg;
00608         }
00609       if ( arg == "--namespace" )
00610         {
00611           if (next_arg == "")
00612             {
00613               std::cerr << "no namespace specified" << std::endl;
00614               exit(1); 
00615             }
00616           else
00617             {
00618               xsl = next_arg;
00619             }
00620         }
00621       ++i;
00622     }
00623   CppUnit::TextUi::TestRunner runner;
00624   if ( ns.empty() )
00625     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00626   else
00627     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00628   CppUnit::Outputter* outputter = 0;
00629   std::ostream* stream = target ? &std::cerr : &std::cout;
00630   switch ( format )
00631     {
00632     case TEXT_OUT :
00633       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00634       break;
00635     case XML_OUT :
00636       std::cout << "XML_OUT" << std::endl;
00637       outputter = new CppUnit::XmlOutputter(&runner.result(),
00638                                             ofs, "shift_jis");
00639       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00640       break;
00641     case COMPILER_OUT :
00642       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00643       break;
00644     }
00645   runner.setOutputter(outputter);
00646   runner.run();
00647   return 0; // runner.run() ? 0 : 1;
00648 }
00649 #endif // MAIN
00650 #endif // ModuleManager_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:05