AuthConfigFactoryTest.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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->setUrl("http://localhost/download/aria2-1.0.0.tar.bz2");
  27. Option option;
  28. option.put(PREF_NO_NETRC, V_FALSE);
  29. option.put(PREF_HTTP_AUTH_CHALLENGE, V_TRUE);
  30. AuthConfigFactory factory;
  31. // without auth info
  32. CPPUNIT_ASSERT(factory.createAuthConfig(req, &option).isNull());
  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).isNull());
  45. CPPUNIT_ASSERT(factory.activateBasicCred("localhost", "/", &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->setUrl("http://mirror/");
  50. CPPUNIT_ASSERT(!factory.activateBasicCred("mirror", "/", &option));
  51. CPPUNIT_ASSERT(factory.createAuthConfig(req, &option).isNull());
  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).isNull());
  56. CPPUNIT_ASSERT(factory.activateBasicCred("mirror", "/", &option));
  57. CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
  58. factory.createAuthConfig(req, &option)->getAuthText());
  59. // username and password in URI
  60. req->setUrl("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->setUrl("http://localhost/download/aria2-1.0.0.tar.bz2");
  68. Option option;
  69. option.put(PREF_NO_NETRC, V_FALSE);
  70. AuthConfigFactory factory;
  71. // without auth info
  72. CPPUNIT_ASSERT(factory.createAuthConfig(req, &option).isNull());
  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->setUrl("http://mirror/");
  88. CPPUNIT_ASSERT(factory.createAuthConfig(req, &option).isNull());
  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->setUrl("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->setUrl("ftp://localhost/download/aria2-1.0.0.tar.bz2");
  103. Option option;
  104. option.put(PREF_NO_NETRC, 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, 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, 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->setUrl("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. }
  131. void AuthConfigFactoryTest::testUpdateBasicCred()
  132. {
  133. Option option;
  134. option.put(PREF_NO_NETRC, V_FALSE);
  135. option.put(PREF_HTTP_AUTH_CHALLENGE, V_TRUE);
  136. AuthConfigFactory factory;
  137. factory.updateBasicCred
  138. (AuthConfigFactory::BasicCred("myname", "mypass", "localhost", "/", true));
  139. factory.updateBasicCred
  140. (AuthConfigFactory::BasicCred("price","j38jdc","localhost","/download", true));
  141. factory.updateBasicCred
  142. (AuthConfigFactory::BasicCred("alice","ium8","localhost","/documents", true));
  143. factory.updateBasicCred
  144. (AuthConfigFactory::BasicCred("jack", "jackx","mirror", "/doc", true));
  145. SharedHandle<Request> req(new Request());
  146. req->setUrl("http://localhost/download/v2.6/Changelog");
  147. CPPUNIT_ASSERT_EQUAL(std::string("price:j38jdc"),
  148. factory.createAuthConfig(req, &option)->getAuthText());
  149. req->setUrl("http://localhost/documents/reference.html");
  150. CPPUNIT_ASSERT_EQUAL(std::string("alice:ium8"),
  151. factory.createAuthConfig(req, &option)->getAuthText());
  152. req->setUrl("http://localhost/documents2/manual.html");
  153. CPPUNIT_ASSERT_EQUAL(std::string("myname:mypass"),
  154. factory.createAuthConfig(req, &option)->getAuthText());
  155. req->setUrl("http://localhost/doc/readme.txt");
  156. CPPUNIT_ASSERT_EQUAL(std::string("myname:mypass"),
  157. factory.createAuthConfig(req, &option)->getAuthText());
  158. req->setUrl("http://local/");
  159. CPPUNIT_ASSERT(factory.createAuthConfig(req, &option).isNull());
  160. req->setUrl("http://mirror/");
  161. CPPUNIT_ASSERT(factory.createAuthConfig(req, &option).isNull());
  162. }
  163. } // namespace aria2