AuthConfigFactoryTest.cc 9.7 KB

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