AuthConfigFactoryTest.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_httpNoChallenge);
  13. CPPUNIT_TEST(testCreateAuthConfig_ftp);
  14. CPPUNIT_TEST(testUpdateBasicCred);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void testCreateAuthConfig_http();
  18. void testCreateAuthConfig_httpNoChallenge();
  19. void testCreateAuthConfig_ftp();
  20. void testUpdateBasicCred();
  21. };
  22. CPPUNIT_TEST_SUITE_REGISTRATION(AuthConfigFactoryTest);
  23. void AuthConfigFactoryTest::testCreateAuthConfig_http()
  24. {
  25. std::shared_ptr<Request> req(new Request());
  26. req->setUri("http://localhost/download/aria2-1.0.0.tar.bz2");
  27. Option option;
  28. option.put(PREF_NO_NETRC, A2_V_FALSE);
  29. option.put(PREF_HTTP_AUTH_CHALLENGE, A2_V_TRUE);
  30. AuthConfigFactory factory;
  31. // without auth info
  32. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  33. // with Netrc
  34. auto netrc = make_unique<Netrc>();
  35. netrc->addAuthenticator(make_unique<Authenticator>(
  36. "localhost", "localhostuser", "localhostpass", "localhostacct"));
  37. netrc->addAuthenticator(make_unique<DefaultAuthenticator>(
  38. "default", "defaultpassword", "defaultaccount"));
  39. factory.setNetrc(std::move(netrc));
  40. // not activated
  41. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  42. CPPUNIT_ASSERT(factory.activateBasicCred("localhost", 80, "/", &option));
  43. CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
  44. factory.createAuthConfig(req, &option)->getAuthText());
  45. // See default token in netrc is ignored.
  46. req->setUri("http://mirror/");
  47. CPPUNIT_ASSERT(!factory.activateBasicCred("mirror", 80, "/", &option));
  48. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  49. // with Netrc + user defined
  50. option.put(PREF_HTTP_USER, "userDefinedUser");
  51. option.put(PREF_HTTP_PASSWD, "userDefinedPassword");
  52. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  53. CPPUNIT_ASSERT(factory.activateBasicCred("mirror", 80, "/", &option));
  54. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  55. factory.createAuthConfig(req, &option)->getAuthText());
  56. // username and password in URI
  57. req->setUri(
  58. "http://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
  59. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
  60. factory.createAuthConfig(req, &option)->getAuthText());
  61. }
  62. void AuthConfigFactoryTest::testCreateAuthConfig_httpNoChallenge()
  63. {
  64. std::shared_ptr<Request> req(new Request());
  65. req->setUri("http://localhost/download/aria2-1.0.0.tar.bz2");
  66. Option option;
  67. option.put(PREF_NO_NETRC, A2_V_FALSE);
  68. AuthConfigFactory factory;
  69. // without auth info
  70. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  71. // with Netrc
  72. auto netrc = make_unique<Netrc>();
  73. netrc->addAuthenticator(make_unique<Authenticator>(
  74. "localhost", "localhostuser", "localhostpass", "localhostacct"));
  75. netrc->addAuthenticator(make_unique<DefaultAuthenticator>(
  76. "default", "defaultpassword", "defaultaccount"));
  77. factory.setNetrc(std::move(netrc));
  78. // not activated
  79. CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
  80. factory.createAuthConfig(req, &option)->getAuthText());
  81. // See default token in netrc is ignored.
  82. req->setUri("http://mirror/");
  83. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  84. // with Netrc + user defined
  85. option.put(PREF_HTTP_USER, "userDefinedUser");
  86. option.put(PREF_HTTP_PASSWD, "userDefinedPassword");
  87. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  88. factory.createAuthConfig(req, &option)->getAuthText());
  89. // username and password in URI
  90. req->setUri(
  91. "http://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
  92. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
  93. factory.createAuthConfig(req, &option)->getAuthText());
  94. }
  95. void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
  96. {
  97. std::shared_ptr<Request> req(new Request());
  98. req->setUri("ftp://localhost/download/aria2-1.0.0.tar.bz2");
  99. Option option;
  100. option.put(PREF_NO_NETRC, A2_V_FALSE);
  101. AuthConfigFactory factory;
  102. // without auth info
  103. CPPUNIT_ASSERT_EQUAL(std::string("anonymous:ARIA2USER@"),
  104. factory.createAuthConfig(req, &option)->getAuthText());
  105. // with Netrc
  106. auto netrc = make_unique<Netrc>();
  107. netrc->addAuthenticator(make_unique<DefaultAuthenticator>(
  108. "default", "defaultpassword", "defaultaccount"));
  109. factory.setNetrc(std::move(netrc));
  110. CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpassword"),
  111. factory.createAuthConfig(req, &option)->getAuthText());
  112. // disable Netrc
  113. option.put(PREF_NO_NETRC, A2_V_TRUE);
  114. CPPUNIT_ASSERT_EQUAL(std::string("anonymous:ARIA2USER@"),
  115. factory.createAuthConfig(req, &option)->getAuthText());
  116. // with Netrc + user defined
  117. option.put(PREF_NO_NETRC, A2_V_FALSE);
  118. option.put(PREF_FTP_USER, "userDefinedUser");
  119. option.put(PREF_FTP_PASSWD, "userDefinedPassword");
  120. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  121. factory.createAuthConfig(req, &option)->getAuthText());
  122. // username and password in URI
  123. req->setUri(
  124. "ftp://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
  125. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
  126. factory.createAuthConfig(req, &option)->getAuthText());
  127. // username in URI, but no password. We have DefaultAuthenticator
  128. // but username is not aria2user
  129. req->setUri("ftp://aria2user@localhost/download/aria2-1.0.0.tar.bz2");
  130. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:userDefinedPassword"),
  131. factory.createAuthConfig(req, &option)->getAuthText());
  132. // Recreate netrc with entry for user aria2user
  133. netrc.reset(new Netrc());
  134. netrc->addAuthenticator(make_unique<Authenticator>("localhost", "aria2user",
  135. "netrcpass", "netrcacct"));
  136. factory.setNetrc(std::move(netrc));
  137. // This time, we can find same username "aria2user" in netrc, so the
  138. // password "netrcpass" is used, instead of "userDefinedPassword"
  139. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:netrcpass"),
  140. factory.createAuthConfig(req, &option)->getAuthText());
  141. // No netrc entry for host mirror, so "userDefinedPassword" is used.
  142. req->setUri("ftp://aria2user@mirror/download/aria2-1.0.0.tar.bz2");
  143. CPPUNIT_ASSERT_EQUAL(std::string("aria2user:userDefinedPassword"),
  144. factory.createAuthConfig(req, &option)->getAuthText());
  145. }
  146. namespace {
  147. std::unique_ptr<BasicCred>
  148. createBasicCred(const std::string& user, const std::string& password,
  149. const std::string& host, uint16_t port, const std::string& path,
  150. bool activated = false)
  151. {
  152. return make_unique<BasicCred>(user, password, host, port, path, activated);
  153. }
  154. } // namespace
  155. void AuthConfigFactoryTest::testUpdateBasicCred()
  156. {
  157. Option option;
  158. option.put(PREF_NO_NETRC, A2_V_FALSE);
  159. option.put(PREF_HTTP_AUTH_CHALLENGE, A2_V_TRUE);
  160. AuthConfigFactory factory;
  161. factory.updateBasicCred(
  162. createBasicCred("myname", "mypass", "localhost", 80, "/", true));
  163. factory.updateBasicCred(
  164. createBasicCred("price", "j38jdc", "localhost", 80, "/download", true));
  165. factory.updateBasicCred(createBasicCred("soap", "planB", "localhost", 80,
  166. "/download/beta", true));
  167. factory.updateBasicCred(
  168. createBasicCred("alice", "ium8", "localhost", 80, "/documents", true));
  169. factory.updateBasicCred(
  170. createBasicCred("jack", "jackx", "mirror", 80, "/doc", true));
  171. std::shared_ptr<Request> req(new Request());
  172. req->setUri("http://localhost/download/v2.6/Changelog");
  173. CPPUNIT_ASSERT_EQUAL(std::string("price:j38jdc"),
  174. factory.createAuthConfig(req, &option)->getAuthText());
  175. req->setUri("http://localhost/download/beta/v2.7/Changelog");
  176. CPPUNIT_ASSERT_EQUAL(std::string("soap:planB"),
  177. factory.createAuthConfig(req, &option)->getAuthText());
  178. req->setUri("http://localhost/documents/reference.html");
  179. CPPUNIT_ASSERT_EQUAL(std::string("alice:ium8"),
  180. factory.createAuthConfig(req, &option)->getAuthText());
  181. req->setUri("http://localhost/documents2/manual.html");
  182. CPPUNIT_ASSERT_EQUAL(std::string("myname:mypass"),
  183. factory.createAuthConfig(req, &option)->getAuthText());
  184. req->setUri("http://localhost/doc/readme.txt");
  185. CPPUNIT_ASSERT_EQUAL(std::string("myname:mypass"),
  186. factory.createAuthConfig(req, &option)->getAuthText());
  187. req->setUri("http://localhost:8080/doc/readme.txt");
  188. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  189. req->setUri("http://local/");
  190. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  191. req->setUri("http://mirror/");
  192. CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
  193. }
  194. } // namespace aria2