ModuleManagerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
11 #include <cppunit/ui/text/TestRunner.h>
12 #include <cppunit/TextOutputter.h>
13 #include <cppunit/extensions/TestFactoryRegistry.h>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <cppunit/TestAssert.h>
16 
17 #include "../ModuleManager.h"
18 #include "../Properties.h"
19 
20 using namespace std;
21 
22 // ModuleManagerTests をテストする [3]
24  : public CppUnit::TestFixture
25 {
26  CPPUNIT_TEST_SUITE(ModuleManagerTests);
27  CPPUNIT_TEST(test_load);
28  CPPUNIT_TEST(test_unload);
29  CPPUNIT_TEST(test_unloadAll);
30  CPPUNIT_TEST(test_symbol);
31  CPPUNIT_TEST(test_setLoadpath);
32  CPPUNIT_TEST(test_getLoadPath);
33  CPPUNIT_TEST(test_addLoadpath);
34  CPPUNIT_TEST(test_getLoadedModules);
35  CPPUNIT_TEST(test_getLoadableModules);
36  CPPUNIT_TEST(test_allowAbsolutePath);
37  CPPUNIT_TEST(test_disallowAbsolutePath);
38  CPPUNIT_TEST(test_allowModuleDownload);
39  CPPUNIT_TEST(test_disallowModuleDownload);
40  CPPUNIT_TEST(test_findFile);
41  CPPUNIT_TEST(test_fileExist);
42  CPPUNIT_TEST(test_getInitFuncName);
43  CPPUNIT_TEST_SUITE_END();
44 
45 private:
46 
48 
49 public:
50 
51  /*
52  * コンストラクタ/デストラクタ [7]
53  */
55  {
56 
57  }
58 
60  {
61  }
62 
63 
64  /*
65  * 初期化/後始末 [8]
66  */
67  virtual void setUp()
68  {
69 
70  const char* default_properties[] = {
71  "manager.modules.config_ext", "so",
72  "manager.modules.config_path", "/etc/rtc",
73  "manager.modules.detect_loadable", "Yes",
74  "manager.modules.load_path", "/usr/lib, /usr/local/lib, /usr/local/lib/rtc",
75  "manager.modules.init_func_suffix", "Init",
76  "manager.modules.init_func_prefix", "",
77  "manager.modules.abs_path_allowed", "Yes",
78  "manager.modules.download_allowed", "Yes",
79  "manager.modules.download_dir", "/tmp/rtc",
80  "manager.modules.download_cleanup", "Yes",
81  "manager.modules.preload", "",
82  ""
83  };
84 
85  RTC::Properties prop(default_properties);
86  m_pModMgr = new RTC::ModuleManager(prop);
87  m_pModMgr->load("libRTC.so");
88  m_pModMgr->load("libm.so");
89  }
90 
91 
92  virtual void tearDown()
93  {
94  m_pModMgr->unloadAll();
95  delete m_pModMgr;
96  }
97 
98 
99  /* tests for string load(const string& file_name) */
100  void test_load() {
101  string libname;
102 
103  try {
104 
105  // Success case
106  // ファイル名だけ与える。
107  libname = m_pModMgr->load("libRTC.so");
108  CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
109 
110  libname = m_pModMgr->load("libRTC.so");
111  CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
112 
113  libname = m_pModMgr->load("libRTC.so");
114  CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
115 
116  // ファイル名を絶対パスで与える。
117  libname = m_pModMgr->load("/usr/lib/libRTC.so");
118  CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
119 
120  // ディレクトリの区切り文字に"//"や"../"がある場合。 -> OK.
121  libname = m_pModMgr->load("/usr//users/kurihara/../kurihara/Components/test/test.so");
122  CPPUNIT_ASSERT(libname == "/usr//users/kurihara/../kurihara/Components/test/test.so");
123 
124 
125 
126  // Failure case
127  //
128  // ファイル名だけ与える。 -> Error DLL open failed.
129  // libname = m_pModMgr->load("libm.a");
130  // CPPUNIT_ASSERT(libname == "/usr/lib/libm.a");
131 
132  // ディレクトリ名を与える。 -> Error DLL open failed.
133  // libname = m_pModMgr->load("OpenRTM");
134  // CPPUNIT_ASSERT(libname == "/usr/lib/OpenRTM");
135 
136  // 実行ファイルを与える。 -> Error Invalid file name.
137  // libname = m_pModMgr->load("rtm-naming");
138  // CPPUNIT_ASSERT(libname == "/usr/bin/rtm-naming");
139 
140  // 存在しないファイル名を与える。-> Error Invalid file name.
141  // libname = m_pModMgr->load("test.test");
142  // cout << "libname: " << libname << endl;
143  // CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
144  }
145  catch (RTC::ModuleManager::Error& e)
146  {
147  std::cout << "Error " << e.reason << std::endl;
148  }
150  {
151  std::cout << "NotFound " << e.name << std::endl;
152  }
153  catch (...)
154  {
155  std::cout << "other exception" << std::endl;
156  }
157 
158  }
159 
160 
161  /* tests for void unload(const string& file_name) */
162  void test_unload() {
163  try {
164  string libname;
165  libname = m_pModMgr->load("libRTC.so");
166  CPPUNIT_ASSERT(libname == "/usr/lib/libRTC.so");
167 
168  // Success case
169  m_pModMgr->unload("/usr/lib/libRTC.so");
170  m_pModMgr->unload("/usr/lib/libm.so");
171 
172 
173 
174  // Failure case
175  // ファイル名だけを与える。 -> NotFound. 絶対パスで与える必要がある。
176  // m_pModMgr->unload("libRTC.so");
177 
178  // loadしていないファイルをunloadする。 -> NotFound.
179  // m_pModMgr->unload("usr/users/kurihara/Components/test/test.so");
180 
181  // 一度unloadしたファイルをunloadする。 -> NotFound.
182  // m_pModMgr->unload("/usr/lib/libRTC.so");
183 
184  }
185  catch (RTC::ModuleManager::Error& e)
186  {
187  std::cout << "Error " << e.reason << std::endl;
188  }
190  {
191  std::cout << "NotFound " << e.name << std::endl;
192  }
193  catch (...)
194  {
195  std::cout << "other exception" << std::endl;
196  }
197  }
198 
199 
200  /* tests for void unloadAll() */
201  void test_unloadAll() {
202  try {
203  m_pModMgr->unloadAll();
204  m_pModMgr->unloadAll();
205  m_pModMgr->unloadAll();
206  }
207  catch (RTC::ModuleManager::Error& e)
208  {
209  std::cout << "Error " << e.reason << std::endl;
210  }
212  {
213  std::cout << "NotFound " << e.name << std::endl;
214  }
215  catch (...)
216  {
217  std::cout << "other exception" << std::endl;
218  }
219  }
220 
221 
222  /* tests for void* symbol(const string& file_name, const string& func_name) */
223  void test_symbol() {
224 
225  //============ 安藤氏作成のテストプログラムより抜粋 ==============================
226  string libname;
227 
228  try
229  {
230  libname = m_pModMgr->load("libm.so");
231  }
232  catch (RTC::ModuleManager::Error& e)
233  {
234  std::cout << "Error " << e.reason << std::endl;
235  }
237  {
238  std::cout << "NotFound " << e.name << std::endl;
239  }
240  catch (...)
241  {
242  std::cout << "other exception" << std::endl;
243  }
244 
245  typedef double (*cosine)(double);
246  typedef double (*sine)(double);
247 
248  cosine _cos;
249  sine _sin;
250 
251  _cos = (cosine) m_pModMgr->symbol(libname, "cos");
252  // std::cout << (*_cos)(0.0) << std::endl;
253  // std::cout << (*_cos)(3.141592653589793238462643383279/2) << std::endl;
254  CPPUNIT_ASSERT_MESSAGE("load error: cos(0.0)",
255  (*_cos)(0.0) == 1.0);
256  CPPUNIT_ASSERT_MESSAGE("load error: cos(pi/2)",
257  (*_cos)(3.141592653589793238462643383279/2) < 0.01);
258 
259  _sin = (sine) m_pModMgr->symbol(libname, "sin");
260  // std::cout << (*_sin)(0.0) << std::endl;
261  // std::cout << (*_sin)(3.141592653589793238462643383279/2) << std::endl;
262  CPPUNIT_ASSERT_MESSAGE("load error: sin(0.0)",
263  (*_sin)(0.0) == 0.0);
264  CPPUNIT_ASSERT_MESSAGE("load error: sin(pi/2)",
265  (*_sin)(3.141592653589793238462643383279/2) == 1.0);
266 
267  //===============================================================================
268 
269  libname = m_pModMgr->load("/usr/users/kurihara/RTM/Components/test/test.so");
270  typedef void (*testHello)();
271 
272  testHello _th;
273  // nm --dynamic test.soで"hello"が含まれる文字列(シンボル)を探して使用した。
274  _th = (testHello) m_pModMgr->symbol(libname, "_ZN4test5helloEv");
275  (*_th)();
276  }
277 
278 
279  /* tests for void setLoadpath(const vector<string>& load_path) */
281  vector<string> set_loadpath, get_loadpath;
282  set_loadpath.push_back("/usr");
283  set_loadpath.push_back("/usr/lib");
284  set_loadpath.push_back("/usr/local/lib");
285  set_loadpath.push_back("/tmp");
286  set_loadpath.push_back("/usr/users/aaaaaaaaaaa/bbbbbbbbbbbb/ccccccccccccc/ddddddddddddd/eeeeeeeeeeeee/ffffffffffffffff/ggggggggggggg/hhhhhhhhhhhhh/iiiiiiiiiii");
287 
288  m_pModMgr->setLoadpath(set_loadpath);
289  get_loadpath = m_pModMgr->getLoadPath();
290 
291  unsigned int size = get_loadpath.size();
292 
293  for (unsigned int i = 0; i < size; i++) {
294  CPPUNIT_ASSERT(set_loadpath[i] == get_loadpath[i]);
295  }
296  }
297 
298 
299  /* tests for vector<string> getLoadPath() */
301  vector<string> getlist, expectation;
302  expectation.push_back("/usr/lib");
303  expectation.push_back("/usr/local/lib");
304  expectation.push_back("/usr/local/lib/rtc");
305  getlist = m_pModMgr->getLoadPath();
306 
307  for (unsigned int i = 0; i < getlist.size(); i++) {
308  CPPUNIT_ASSERT(getlist[i] == expectation[i]);
309  }
310  }
311 
312 
313  /* tests for void addLoadpath(const vector<string>& load_path) */
315  vector<string> getlist, expectation, add_path;
316  expectation.push_back("/usr/lib");
317  expectation.push_back("/usr/local/lib");
318  expectation.push_back("/usr/local/lib/rtc");
319  expectation.push_back("/tmp");
320  expectation.push_back("/hoge");
321  expectation.push_back("/hoge/hoge");
322 
323  add_path.push_back("/tmp");
324  add_path.push_back("/hoge");
325  add_path.push_back("/hoge/hoge");
326  m_pModMgr->addLoadpath(add_path);
327 
328  getlist = m_pModMgr->getLoadPath();
329  for (unsigned int i = 0; i < getlist.size(); i++) {
330  CPPUNIT_ASSERT(getlist[i] == expectation[i]);
331  }
332  }
333 
334 
335  /* tests for vector<string> getLoadedModules() */
337  vector<string> get_modlist;
338 
339  get_modlist = m_pModMgr->getLoadedModules();
340  CPPUNIT_ASSERT(get_modlist[0] == "/usr/lib/libRTC.so");
341  CPPUNIT_ASSERT(get_modlist[1] == "/usr/lib/libm.so");
342  }
343 
344 
345  /* tests for vector<string> getLoadableModules() */
347  // ModuelManager.cppで実装されていないl。
348 
349  vector<string> get_modlist;
350  get_modlist = m_pModMgr->getLoadableModules();
351  for (unsigned int i = 0; i < get_modlist.size(); i++) {
352  cout << get_modlist[i] << endl;
353  }
354 
355  }
356 
357 
358  /* tests for void allowAbsolutePath() */
360  string libname;
361  try {
362 
363  // Success case
364  m_pModMgr->allowAbsolutePath();
365  libname = m_pModMgr->load("libRTC.so");
366  m_pModMgr->unload(libname);
367 
368  libname = m_pModMgr->load("/usr/lib/libm.so");
369  m_pModMgr->unload(libname);
370 
371  libname = m_pModMgr->load("../lib/libm.so");
372  m_pModMgr->unload(libname);
373 
374 
375  m_pModMgr->disallowAbsolutePath();
376  libname = m_pModMgr->load("../lib/libRTC.so");
377  m_pModMgr->unload(libname);
378 
379  // Failure case
380  // libname = m_pModMgr->load("/usr/lib/libRTC.so");
381  // m_pModMgr->unload(libname);
382  }
383  catch (RTC::ModuleManager::Error& e)
384  {
385  std::cout << "Error " << e.reason << std::endl;
386  }
388  {
389  std::cout << "NotFound " << e.name << std::endl;
390  }
391  catch (...)
392  {
393  std::cout << "other exception" << std::endl;
394  }
395  }
396 
397 
398  /* tests for void disallowAbsolutePath() */
400  // test_allowAbsolutePath()にてテスト済み。
401  }
402 
403 
404  /* tests for void allowModuleDownload() */
406  // テストまだ } /* tests for void disallowModuleDownload() */ void test_disallowModuleDownload() { // テストまだ } /* tests for string findFile(const string& fname, const vector<string>& load_path) */ void test_findFile() { //============ 安藤氏作成のテストプログラムより抜粋 ============================== std::string result; std::vector<std::string> path; path.push_back("/lib"); path.push_back("/usr/lib"); path.push_back("/usr/local/lib"); result = m_pModMgr->findFile("libm.so", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: libm.so", result == "/usr/lib/libm.so"); result = m_pModMgr->findFile("libc.so", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: libc.so", result == "/usr/lib/libc.so"); result = m_pModMgr->findFile("libACE.so", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: libACE.so", result == "/usr/lib/libACE.so"); result = m_pModMgr->findFile("hosts", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: hosts", result == ""); result = m_pModMgr->findFile("munyamunya", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: munyamunya", result == ""); //================================================================================ // Failure case // path.clear(); // result = m_pModMgr->findFile("libm.so", path); // CPPUNIT_ASSERT_MESSAGE("fileFile error: libm.so", // result == "/usr/lib/libm.so"); } /* tests for bool fileExist(const string& filename) */ void test_fileExist() { // Success case CPPUNIT_ASSERT(m_pModMgr->fileExist("../../../../../../../../usr/lib/libm.so")); CPPUNIT_ASSERT(m_pModMgr->fileExist("/usr/lib/libm.so")); // Failure case // CPPUNIT_ASSERT(m_pModMgr->fileExist("libm.so")); } /* tests for string getInitFuncName(const string& file_path) */ void test_getInitFuncName() { std::string result; //============ 安藤氏作成のテストプログラムより抜粋 ============================== result = m_pModMgr->getInitFuncName("Manipulator"); CPPUNIT_ASSERT_MESSAGE("getInitFuncName error: Manipulator", result == "ManipulatorInit"); result = m_pModMgr->getInitFuncName("PHANToM"); CPPUNIT_ASSERT_MESSAGE("getInitFuncName error: PHANToM", result == "PHANToMInit"); //================================================================================ } }; /* * register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(ModuleManagerTests); int main(int argc, char* argv[]) { CppUnit::TextUi::TestRunner runner; runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); CppUnit::Outputter* outputter = new CppUnit::TextOutputter(&runner.result(), std::cout); runner.setOutputter(outputter); return runner.run(); }
407  }
408 
409 
410  /* tests for void disallowModuleDownload() */
412  // テストまだ } /* tests for string findFile(const string& fname, const vector<string>& load_path) */ void test_findFile() { //============ 安藤氏作成のテストプログラムより抜粋 ============================== std::string result; std::vector<std::string> path; path.push_back("/lib"); path.push_back("/usr/lib"); path.push_back("/usr/local/lib"); result = m_pModMgr->findFile("libm.so", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: libm.so", result == "/usr/lib/libm.so"); result = m_pModMgr->findFile("libc.so", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: libc.so", result == "/usr/lib/libc.so"); result = m_pModMgr->findFile("libACE.so", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: libACE.so", result == "/usr/lib/libACE.so"); result = m_pModMgr->findFile("hosts", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: hosts", result == ""); result = m_pModMgr->findFile("munyamunya", path); CPPUNIT_ASSERT_MESSAGE("fileFile error: munyamunya", result == ""); //================================================================================ // Failure case // path.clear(); // result = m_pModMgr->findFile("libm.so", path); // CPPUNIT_ASSERT_MESSAGE("fileFile error: libm.so", // result == "/usr/lib/libm.so"); } /* tests for bool fileExist(const string& filename) */ void test_fileExist() { // Success case CPPUNIT_ASSERT(m_pModMgr->fileExist("../../../../../../../../usr/lib/libm.so")); CPPUNIT_ASSERT(m_pModMgr->fileExist("/usr/lib/libm.so")); // Failure case // CPPUNIT_ASSERT(m_pModMgr->fileExist("libm.so")); } /* tests for string getInitFuncName(const string& file_path) */ void test_getInitFuncName() { std::string result; //============ 安藤氏作成のテストプログラムより抜粋 ============================== result = m_pModMgr->getInitFuncName("Manipulator"); CPPUNIT_ASSERT_MESSAGE("getInitFuncName error: Manipulator", result == "ManipulatorInit"); result = m_pModMgr->getInitFuncName("PHANToM"); CPPUNIT_ASSERT_MESSAGE("getInitFuncName error: PHANToM", result == "PHANToMInit"); //================================================================================ } }; /* * register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(ModuleManagerTests); int main(int argc, char* argv[]) { CppUnit::TextUi::TestRunner runner; runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); CppUnit::Outputter* outputter = new CppUnit::TextOutputter(&runner.result(), std::cout); runner.setOutputter(outputter); return runner.run(); }
413  }
414 
415 
416  /* tests for string findFile(const string& fname, const vector<string>& load_path) */
417  void test_findFile() {
418  //============ 安藤氏作成のテストプログラムより抜粋 ==============================
419  std::string result;
420  std::vector<std::string> path;
421  path.push_back("/lib");
422  path.push_back("/usr/lib");
423  path.push_back("/usr/local/lib");
424 
425  result = m_pModMgr->findFile("libm.so", path);
426  CPPUNIT_ASSERT_MESSAGE("fileFile error: libm.so",
427  result == "/usr/lib/libm.so");
428 
429  result = m_pModMgr->findFile("libc.so", path);
430  CPPUNIT_ASSERT_MESSAGE("fileFile error: libc.so",
431  result == "/usr/lib/libc.so");
432 
433  result = m_pModMgr->findFile("libACE.so", path);
434  CPPUNIT_ASSERT_MESSAGE("fileFile error: libACE.so",
435  result == "/usr/lib/libACE.so");
436 
437  result = m_pModMgr->findFile("hosts", path);
438  CPPUNIT_ASSERT_MESSAGE("fileFile error: hosts",
439  result == "");
440 
441  result = m_pModMgr->findFile("munyamunya", path);
442  CPPUNIT_ASSERT_MESSAGE("fileFile error: munyamunya",
443  result == "");
444 
445  //================================================================================
446 
447  // Failure case
448  // path.clear();
449  // result = m_pModMgr->findFile("libm.so", path);
450  // CPPUNIT_ASSERT_MESSAGE("fileFile error: libm.so",
451  // result == "/usr/lib/libm.so");
452 
453  }
454 
455 
456  /* tests for bool fileExist(const string& filename) */
457  void test_fileExist() {
458  // Success case
459  CPPUNIT_ASSERT(m_pModMgr->fileExist("../../../../../../../../usr/lib/libm.so"));
460  CPPUNIT_ASSERT(m_pModMgr->fileExist("/usr/lib/libm.so"));
461 
462  // Failure case
463  // CPPUNIT_ASSERT(m_pModMgr->fileExist("libm.so"));
464  }
465 
466 
467  /* tests for string getInitFuncName(const string& file_path) */
469  std::string result;
470 
471  //============ 安藤氏作成のテストプログラムより抜粋 ==============================
472  result = m_pModMgr->getInitFuncName("Manipulator");
473  CPPUNIT_ASSERT_MESSAGE("getInitFuncName error: Manipulator",
474  result == "ManipulatorInit");
475 
476  result = m_pModMgr->getInitFuncName("PHANToM");
477  CPPUNIT_ASSERT_MESSAGE("getInitFuncName error: PHANToM",
478  result == "PHANToMInit");
479  //================================================================================
480  }
481 };
482 
483 
484 /*
485  * register test suite
486  */
488 
489 
490 
491 int main(int argc, char* argv[])
492 {
493  CppUnit::TextUi::TestRunner runner;
494 
495  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
496  CppUnit::Outputter* outputter =
497  new CppUnit::TextOutputter(&runner.result(), std::cout);
498  runner.setOutputter(outputter);
499 
500  return runner.run();
501 }
void * symbol(const std::string &file_name, const std::string &func_name)
Refer to the symbol of the module.
Structure for exception handling of unimplemented part and specified module missing.
std::string getInitFuncName(const std::string &file_path)
Create initialization function symbol.
Structure for exception handling when file open is failed.
void addLoadpath(const std::vector< std::string > &load_path)
Add the module load path.
std::vector< coil::Properties > getLoadableModules()
Get the loadable module list.
void setLoadpath(const std::vector< std::string > &load_path)
Set the module load path.
void disallowAbsolutePath()
Disallow absolute path when specify module path.
ModuleManager test.
std::string load(const std::string &file_name)
Load the module.
void allowAbsolutePath()
Allow absolute path when specify module path.
std::vector< coil::Properties > getLoadedModules()
Get the module list that has been loaded.
bool fileExist(const std::string &filename)
Check whether the file exists.
CPPUNIT_TEST_SUITE_REGISTRATION(ModuleManagerTests)
prop
Organization::get_organization_property ();.
int main(int argc, char *argv[])
ModuleManager class.
Definition: ModuleManager.h:74
std::vector< std::string > getLoadPath()
Get the module load path.
RTC::ModuleManager * m_pModMgr
Class represents a set of properties.
Definition: Properties.h:101
void unloadAll()
Unload all modules.
void unload(const std::string &file_name)
Unload the module.
std::string findFile(const std::string &fname, const std::vector< std::string > &load_path)
Search the file from the LoadPath.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:43