NetrcTest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "Netrc.h"
  2. #include "Exception.h"
  3. #include <iostream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. namespace aria2 {
  6. class NetrcTest : public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(NetrcTest);
  8. CPPUNIT_TEST(testFindAuthenticator);
  9. CPPUNIT_TEST(testParse);
  10. CPPUNIT_TEST(testParse_fileNotFound);
  11. CPPUNIT_TEST(testParse_emptyfile);
  12. CPPUNIT_TEST(testParse_malformedNetrc);
  13. CPPUNIT_TEST_SUITE_END();
  14. private:
  15. public:
  16. void setUp() {
  17. }
  18. void testFindAuthenticator();
  19. void testParse();
  20. void testParse_fileNotFound();
  21. void testParse_emptyfile();
  22. void testParse_malformedNetrc();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION( NetrcTest );
  25. void NetrcTest::testFindAuthenticator()
  26. {
  27. Netrc netrc;
  28. netrc.addAuthenticator(new Authenticator("host1", "tujikawa", "tujikawapasswd", "tujikawaaccount"));
  29. netrc.addAuthenticator(new Authenticator("host2", "aria2", "aria2password", "aria2account"));
  30. netrc.addAuthenticator(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"));
  31. SharedHandle<Authenticator> aria2auth = netrc.findAuthenticator("host2");
  32. CPPUNIT_ASSERT(!aria2auth.isNull());
  33. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
  34. CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
  35. CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
  36. SharedHandle<Authenticator> defaultauth = netrc.findAuthenticator("host3");
  37. CPPUNIT_ASSERT(!defaultauth.isNull());
  38. CPPUNIT_ASSERT_EQUAL(std::string("default"), defaultauth->getLogin());
  39. CPPUNIT_ASSERT_EQUAL(std::string("defaultpassword"), defaultauth->getPassword());
  40. CPPUNIT_ASSERT_EQUAL(std::string("defaultaccount"), defaultauth->getAccount());
  41. }
  42. void NetrcTest::testParse()
  43. {
  44. Netrc netrc;
  45. netrc.parse("sample.netrc");
  46. std::deque<SharedHandle<Authenticator> >::const_iterator itr = netrc.getAuthenticators().begin();
  47. SharedHandle<Authenticator> tujikawaauth = *itr;
  48. CPPUNIT_ASSERT(!tujikawaauth.isNull());
  49. CPPUNIT_ASSERT_EQUAL(std::string("host1"), tujikawaauth->getMachine());
  50. CPPUNIT_ASSERT_EQUAL(std::string("tujikawa"), tujikawaauth->getLogin());
  51. CPPUNIT_ASSERT_EQUAL(std::string("tujikawapassword"), tujikawaauth->getPassword());
  52. CPPUNIT_ASSERT_EQUAL(std::string("tujikawaaccount"), tujikawaauth->getAccount());
  53. ++itr;
  54. SharedHandle<Authenticator> aria2auth = *itr;
  55. CPPUNIT_ASSERT(!aria2auth.isNull());
  56. CPPUNIT_ASSERT_EQUAL(std::string("host2"), aria2auth->getMachine());
  57. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
  58. CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
  59. CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
  60. ++itr;
  61. SharedHandle<Authenticator> defaultauth = *itr;
  62. CPPUNIT_ASSERT(!defaultauth.isNull());
  63. CPPUNIT_ASSERT_EQUAL(std::string("anonymous"), defaultauth->getLogin());
  64. CPPUNIT_ASSERT_EQUAL(std::string("ARIA2@USER"), defaultauth->getPassword());
  65. CPPUNIT_ASSERT_EQUAL(std::string("ARIA2@ACCT"), defaultauth->getAccount());
  66. }
  67. void NetrcTest::testParse_fileNotFound()
  68. {
  69. Netrc netrc;
  70. try {
  71. netrc.parse("");
  72. CPPUNIT_FAIL("exception must be thrown.");
  73. } catch(Exception* e) {
  74. std::cerr << e->getMsg() << std::endl;
  75. delete e;
  76. }
  77. }
  78. void NetrcTest::testParse_emptyfile()
  79. {
  80. Netrc netrc;
  81. netrc.parse("emptyfile");
  82. CPPUNIT_ASSERT_EQUAL((size_t)0, netrc.getAuthenticators().size());
  83. }
  84. void NetrcTest::testParse_malformedNetrc()
  85. {
  86. Netrc netrc;
  87. try {
  88. netrc.parse("malformed.netrc");
  89. CPPUNIT_FAIL("exception must be thrown.");
  90. } catch(Exception* e) {
  91. std::cerr << e->getMsg() << std::endl;
  92. delete e;
  93. }
  94. }
  95. } // namespace aria2