NetrcTest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "Netrc.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "Exception.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
  29. (SharedHandle<Authenticator>(new Authenticator("host1", "tujikawa", "tujikawapasswd", "tujikawaaccount")));
  30. netrc.addAuthenticator
  31. (SharedHandle<Authenticator>(new Authenticator("host2", "aria2", "aria2password", "aria2account")));
  32. netrc.addAuthenticator
  33. (SharedHandle<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
  34. SharedHandle<Authenticator> aria2auth = netrc.findAuthenticator("host2");
  35. CPPUNIT_ASSERT(!aria2auth.isNull());
  36. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
  37. CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
  38. CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
  39. SharedHandle<Authenticator> defaultauth = netrc.findAuthenticator("host3");
  40. CPPUNIT_ASSERT(!defaultauth.isNull());
  41. CPPUNIT_ASSERT_EQUAL(std::string("default"), defaultauth->getLogin());
  42. CPPUNIT_ASSERT_EQUAL(std::string("defaultpassword"), defaultauth->getPassword());
  43. CPPUNIT_ASSERT_EQUAL(std::string("defaultaccount"), defaultauth->getAccount());
  44. }
  45. void NetrcTest::testParse()
  46. {
  47. Netrc netrc;
  48. netrc.parse("sample.netrc");
  49. std::vector<SharedHandle<Authenticator> >::const_iterator itr =
  50. netrc.getAuthenticators().begin();
  51. SharedHandle<Authenticator> tujikawaauth = *itr;
  52. CPPUNIT_ASSERT(!tujikawaauth.isNull());
  53. CPPUNIT_ASSERT_EQUAL(std::string("host1"), tujikawaauth->getMachine());
  54. CPPUNIT_ASSERT_EQUAL(std::string("tujikawa"), tujikawaauth->getLogin());
  55. CPPUNIT_ASSERT_EQUAL(std::string("tujikawapassword"), tujikawaauth->getPassword());
  56. CPPUNIT_ASSERT_EQUAL(std::string("tujikawaaccount"), tujikawaauth->getAccount());
  57. ++itr;
  58. SharedHandle<Authenticator> aria2auth = *itr;
  59. CPPUNIT_ASSERT(!aria2auth.isNull());
  60. CPPUNIT_ASSERT_EQUAL(std::string("host2"), aria2auth->getMachine());
  61. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
  62. CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
  63. CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
  64. ++itr;
  65. SharedHandle<Authenticator> defaultauth = *itr;
  66. CPPUNIT_ASSERT(!defaultauth.isNull());
  67. CPPUNIT_ASSERT_EQUAL(std::string("anonymous"), defaultauth->getLogin());
  68. CPPUNIT_ASSERT_EQUAL(std::string("ARIA2@USER"), defaultauth->getPassword());
  69. CPPUNIT_ASSERT_EQUAL(std::string("ARIA2@ACCT"), defaultauth->getAccount());
  70. }
  71. void NetrcTest::testParse_fileNotFound()
  72. {
  73. Netrc netrc;
  74. try {
  75. netrc.parse("");
  76. CPPUNIT_FAIL("exception must be thrown.");
  77. } catch(Exception& e) {
  78. std::cerr << e.stackTrace() << std::endl;
  79. }
  80. }
  81. void NetrcTest::testParse_emptyfile()
  82. {
  83. Netrc netrc;
  84. netrc.parse("emptyfile");
  85. CPPUNIT_ASSERT_EQUAL((size_t)0, netrc.getAuthenticators().size());
  86. }
  87. void NetrcTest::testParse_malformedNetrc()
  88. {
  89. Netrc netrc;
  90. try {
  91. netrc.parse("malformed.netrc");
  92. CPPUNIT_FAIL("exception must be thrown.");
  93. } catch(Exception& e) {
  94. std::cerr << e.stackTrace() << std::endl;
  95. }
  96. }
  97. } // namespace aria2