NetrcTest.cc 3.4 KB

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