NetrcTest.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(testFindAuthenticatable);
  8. CPPUNIT_TEST(testParse);
  9. CPPUNIT_TEST(testParse_fileNotFound);
  10. CPPUNIT_TEST(testParse_emptyfile);
  11. CPPUNIT_TEST_SUITE_END();
  12. private:
  13. public:
  14. void setUp() {
  15. }
  16. void testFindAuthenticatable();
  17. void testParse();
  18. void testParse_fileNotFound();
  19. void testParse_emptyfile();
  20. };
  21. CPPUNIT_TEST_SUITE_REGISTRATION( NetrcTest );
  22. void NetrcTest::testFindAuthenticatable()
  23. {
  24. Netrc netrc;
  25. netrc.addAuthenticatable(new Authenticator("host1", "tujikawa", "tujikawapasswd", "tujikawaaccount"));
  26. netrc.addAuthenticatable(new Authenticator("host2", "aria2", "aria2password", "aria2account"));
  27. netrc.addAuthenticatable(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"));
  28. AuthenticatorHandle aria2auth = netrc.findAuthenticatable("host2");
  29. CPPUNIT_ASSERT(!aria2auth.isNull());
  30. CPPUNIT_ASSERT_EQUAL(string("aria2"), aria2auth->getLogin());
  31. CPPUNIT_ASSERT_EQUAL(string("aria2password"), aria2auth->getPassword());
  32. CPPUNIT_ASSERT_EQUAL(string("aria2account"), aria2auth->getAccount());
  33. AuthenticatorHandle defaultauth = netrc.findAuthenticatable("host3");
  34. CPPUNIT_ASSERT(!defaultauth.isNull());
  35. CPPUNIT_ASSERT_EQUAL(string("default"), defaultauth->getLogin());
  36. CPPUNIT_ASSERT_EQUAL(string("defaultpassword"), defaultauth->getPassword());
  37. CPPUNIT_ASSERT_EQUAL(string("defaultaccount"), defaultauth->getAccount());
  38. }
  39. void NetrcTest::testParse()
  40. {
  41. Netrc netrc;
  42. netrc.parse("sample.netrc");
  43. Authenticatables::const_iterator itr = netrc.getAuthenticatables().begin();
  44. AuthenticatorHandle tujikawaauth = *itr;
  45. CPPUNIT_ASSERT(!tujikawaauth.isNull());
  46. CPPUNIT_ASSERT_EQUAL(string("host1"), tujikawaauth->getMachine());
  47. CPPUNIT_ASSERT_EQUAL(string("tujikawa"), tujikawaauth->getLogin());
  48. CPPUNIT_ASSERT_EQUAL(string("tujikawapassword"), tujikawaauth->getPassword());
  49. CPPUNIT_ASSERT_EQUAL(string("tujikawaaccount"), tujikawaauth->getAccount());
  50. ++itr;
  51. AuthenticatorHandle aria2auth = *itr;
  52. CPPUNIT_ASSERT(!aria2auth.isNull());
  53. CPPUNIT_ASSERT_EQUAL(string("host2"), aria2auth->getMachine());
  54. CPPUNIT_ASSERT_EQUAL(string("aria2"), aria2auth->getLogin());
  55. CPPUNIT_ASSERT_EQUAL(string("aria2password"), aria2auth->getPassword());
  56. CPPUNIT_ASSERT_EQUAL(string("aria2account"), aria2auth->getAccount());
  57. ++itr;
  58. DefaultAuthenticatorHandle defaultauth = *itr;
  59. CPPUNIT_ASSERT(!defaultauth.isNull());
  60. CPPUNIT_ASSERT_EQUAL(string("anonymous"), defaultauth->getLogin());
  61. CPPUNIT_ASSERT_EQUAL(string("ARIA2@USER"), defaultauth->getPassword());
  62. CPPUNIT_ASSERT_EQUAL(string("ARIA2@ACCT"), defaultauth->getAccount());
  63. }
  64. void NetrcTest::testParse_fileNotFound()
  65. {
  66. Netrc netrc;
  67. try {
  68. netrc.parse("");
  69. CPPUNIT_FAIL("exception must be threw.");
  70. } catch(Exception* e) {
  71. cerr << e->getMsg() << endl;
  72. delete e;
  73. }
  74. }
  75. void NetrcTest::testParse_emptyfile()
  76. {
  77. Netrc netrc;
  78. netrc.parse("emptyfile");
  79. CPPUNIT_ASSERT_EQUAL((size_t)0, netrc.getAuthenticatables().size());
  80. }