AuthConfigFactoryTest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "AuthConfigFactory.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Netrc.h"
  4. #include "prefs.h"
  5. #include "Request.h"
  6. #include "AuthConfig.h"
  7. #include "Option.h"
  8. namespace aria2 {
  9. class AuthConfigFactoryTest:public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(AuthConfigFactoryTest);
  11. CPPUNIT_TEST(testCreateAuthConfig_http);
  12. CPPUNIT_TEST(testCreateAuthConfig_ftp);
  13. CPPUNIT_TEST_SUITE_END();
  14. public:
  15. void testCreateAuthConfig_http();
  16. void testCreateAuthConfig_ftp();
  17. };
  18. CPPUNIT_TEST_SUITE_REGISTRATION( AuthConfigFactoryTest );
  19. void AuthConfigFactoryTest::testCreateAuthConfig_http()
  20. {
  21. SharedHandle<Request> req(new Request());
  22. req->setUrl("http://localhost/download/aria2-1.0.0.tar.bz2");
  23. Option option;
  24. option.put(PREF_NO_NETRC, V_FALSE);
  25. AuthConfigFactory factory(&option);
  26. // without auth info
  27. CPPUNIT_ASSERT_EQUAL(std::string(":"),
  28. factory.createAuthConfig(req)->getAuthText());
  29. // with Netrc
  30. SharedHandle<Netrc> netrc(new Netrc());
  31. netrc->addAuthenticator
  32. (SharedHandle<Authenticator>(new Authenticator("localhost",
  33. "localhostuser",
  34. "localhostpass",
  35. "localhostacct")));
  36. netrc->addAuthenticator
  37. (SharedHandle<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
  38. factory.setNetrc(netrc);
  39. CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
  40. factory.createAuthConfig(req)->getAuthText());
  41. // See default token in netrc is ignored.
  42. SharedHandle<Request> mirrorReq(new Request());
  43. req->setUrl("http://mirror/");
  44. CPPUNIT_ASSERT_EQUAL(std::string(":"),
  45. factory.createAuthConfig(mirrorReq)->getAuthText());
  46. // with Netrc + user defined
  47. option.put(PREF_HTTP_USER, "userDefinedUser");
  48. option.put(PREF_HTTP_PASSWD, "userDefinedPassword");
  49. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  50. factory.createAuthConfig(req)->getAuthText());
  51. // username and password in URI: disabled by default.
  52. req->setUrl("http://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
  53. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  54. factory.createAuthConfig(req)->getAuthText());
  55. }
  56. void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
  57. {
  58. SharedHandle<Request> req(new Request());
  59. req->setUrl("ftp://localhost/download/aria2-1.0.0.tar.bz2");
  60. Option option;
  61. option.put(PREF_NO_NETRC, V_FALSE);
  62. AuthConfigFactory factory(&option);
  63. // without auth info
  64. CPPUNIT_ASSERT_EQUAL(std::string("anonymous:ARIA2USER@"),
  65. factory.createAuthConfig(req)->getAuthText());
  66. // with Netrc
  67. SharedHandle<Netrc> netrc(new Netrc());
  68. netrc->addAuthenticator
  69. (SharedHandle<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
  70. factory.setNetrc(netrc);
  71. CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpassword"),
  72. factory.createAuthConfig(req)->getAuthText());
  73. // disable Netrc
  74. option.put(PREF_NO_NETRC, V_TRUE);
  75. CPPUNIT_ASSERT_EQUAL(std::string("anonymous:ARIA2USER@"),
  76. factory.createAuthConfig(req)->getAuthText());
  77. // with Netrc + user defined
  78. option.put(PREF_NO_NETRC, V_FALSE);
  79. option.put(PREF_FTP_USER, "userDefinedUser");
  80. option.put(PREF_FTP_PASSWD, "userDefinedPassword");
  81. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  82. factory.createAuthConfig(req)->getAuthText());
  83. // username and password in URI
  84. req->setUrl("ftp://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
  85. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
  86. factory.createAuthConfig(req)->getAuthText());
  87. }
  88. } // namespace aria2