AuthConfigFactoryTest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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(testUpdateBasicCred);
  14. CPPUNIT_TEST_SUITE_END();
  15. public:
  16. void testCreateAuthConfig_http();
  17. void testCreateAuthConfig_ftp();
  18. void testUpdateBasicCred();
  19. };
  20. CPPUNIT_TEST_SUITE_REGISTRATION( AuthConfigFactoryTest );
  21. void AuthConfigFactoryTest::testCreateAuthConfig_http()
  22. {
  23. SharedHandle<Request> req(new Request());
  24. req->setUrl("http://localhost/download/aria2-1.0.0.tar.bz2");
  25. Option option;
  26. option.put(PREF_NO_NETRC, V_FALSE);
  27. AuthConfigFactory factory(&option);
  28. // without auth info
  29. CPPUNIT_ASSERT(factory.createAuthConfig(req).isNull());
  30. // with Netrc
  31. SharedHandle<Netrc> netrc(new Netrc());
  32. netrc->addAuthenticator
  33. (SharedHandle<Authenticator>(new Authenticator("localhost",
  34. "localhostuser",
  35. "localhostpass",
  36. "localhostacct")));
  37. netrc->addAuthenticator
  38. (SharedHandle<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
  39. factory.setNetrc(netrc);
  40. // not activated
  41. CPPUNIT_ASSERT(factory.createAuthConfig(req).isNull());
  42. CPPUNIT_ASSERT(factory.activateBasicCred("localhost", "/"));
  43. CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
  44. factory.createAuthConfig(req)->getAuthText());
  45. // See default token in netrc is ignored.
  46. SharedHandle<Request> mirrorReq(new Request());
  47. req->setUrl("http://mirror/");
  48. CPPUNIT_ASSERT(!factory.activateBasicCred("mirror", "/"));
  49. CPPUNIT_ASSERT(factory.createAuthConfig(mirrorReq).isNull());
  50. // with Netrc + user defined
  51. option.put(PREF_HTTP_USER, "userDefinedUser");
  52. option.put(PREF_HTTP_PASSWD, "userDefinedPassword");
  53. CPPUNIT_ASSERT(factory.createAuthConfig(req).isNull());
  54. CPPUNIT_ASSERT(factory.activateBasicCred("mirror", "/"));
  55. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  56. factory.createAuthConfig(req)->getAuthText());
  57. // username and password in URI
  58. req->setUrl("http://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
  59. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
  60. factory.createAuthConfig(req)->getAuthText());
  61. }
  62. void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
  63. {
  64. SharedHandle<Request> req(new Request());
  65. req->setUrl("ftp://localhost/download/aria2-1.0.0.tar.bz2");
  66. Option option;
  67. option.put(PREF_NO_NETRC, V_FALSE);
  68. AuthConfigFactory factory(&option);
  69. // without auth info
  70. CPPUNIT_ASSERT_EQUAL(std::string("anonymous:ARIA2USER@"),
  71. factory.createAuthConfig(req)->getAuthText());
  72. // with Netrc
  73. SharedHandle<Netrc> netrc(new Netrc());
  74. netrc->addAuthenticator
  75. (SharedHandle<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
  76. factory.setNetrc(netrc);
  77. CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpassword"),
  78. factory.createAuthConfig(req)->getAuthText());
  79. // disable Netrc
  80. option.put(PREF_NO_NETRC, V_TRUE);
  81. CPPUNIT_ASSERT_EQUAL(std::string("anonymous:ARIA2USER@"),
  82. factory.createAuthConfig(req)->getAuthText());
  83. // with Netrc + user defined
  84. option.put(PREF_NO_NETRC, V_FALSE);
  85. option.put(PREF_FTP_USER, "userDefinedUser");
  86. option.put(PREF_FTP_PASSWD, "userDefinedPassword");
  87. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  88. factory.createAuthConfig(req)->getAuthText());
  89. // username and password in URI
  90. req->setUrl("ftp://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
  91. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
  92. factory.createAuthConfig(req)->getAuthText());
  93. }
  94. void AuthConfigFactoryTest::testUpdateBasicCred()
  95. {
  96. Option option;
  97. option.put(PREF_NO_NETRC, V_FALSE);
  98. AuthConfigFactory factory(&option);
  99. factory.updateBasicCred
  100. (AuthConfigFactory::BasicCred("myname", "mypass", "localhost", "/", true));
  101. factory.updateBasicCred
  102. (AuthConfigFactory::BasicCred("price","j38jdc","localhost","/download", true));
  103. factory.updateBasicCred
  104. (AuthConfigFactory::BasicCred("alice","ium8","localhost","/documents", true));
  105. factory.updateBasicCred
  106. (AuthConfigFactory::BasicCred("jack", "jackx","mirror", "/doc", true));
  107. SharedHandle<Request> req(new Request());
  108. req->setUrl("http://localhost/download/v2.6/Changelog");
  109. CPPUNIT_ASSERT_EQUAL(std::string("price:j38jdc"),
  110. factory.createAuthConfig(req)->getAuthText());
  111. req->setUrl("http://localhost/documents/reference.html");
  112. CPPUNIT_ASSERT_EQUAL(std::string("alice:ium8"),
  113. factory.createAuthConfig(req)->getAuthText());
  114. req->setUrl("http://localhost/documents2/manual.html");
  115. CPPUNIT_ASSERT_EQUAL(std::string("myname:mypass"),
  116. factory.createAuthConfig(req)->getAuthText());
  117. req->setUrl("http://localhost/doc/readme.txt");
  118. CPPUNIT_ASSERT_EQUAL(std::string("myname:mypass"),
  119. factory.createAuthConfig(req)->getAuthText());
  120. req->setUrl("http://local/");
  121. CPPUNIT_ASSERT(factory.createAuthConfig(req).isNull());
  122. req->setUrl("http://mirror/");
  123. CPPUNIT_ASSERT(factory.createAuthConfig(req).isNull());
  124. }
  125. } // namespace aria2