2007-10-30 12:48:01 +00:00
|
|
|
#include "AuthConfigFactory.h"
|
2008-11-04 14:08:26 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2007-10-30 12:48:01 +00:00
|
|
|
#include "Netrc.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "Request.h"
|
|
|
|
#include "AuthConfig.h"
|
|
|
|
#include "Option.h"
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2007-10-30 12:48:01 +00:00
|
|
|
class AuthConfigFactoryTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(AuthConfigFactoryTest);
|
|
|
|
CPPUNIT_TEST(testCreateAuthConfig_http);
|
2009-02-19 12:02:22 +00:00
|
|
|
CPPUNIT_TEST(testCreateAuthConfig_httpNoChallenge);
|
2007-10-30 12:48:01 +00:00
|
|
|
CPPUNIT_TEST(testCreateAuthConfig_ftp);
|
2009-01-30 16:12:41 +00:00
|
|
|
CPPUNIT_TEST(testUpdateBasicCred);
|
2007-10-30 12:48:01 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void testCreateAuthConfig_http();
|
2009-02-19 12:02:22 +00:00
|
|
|
void testCreateAuthConfig_httpNoChallenge();
|
2007-10-30 12:48:01 +00:00
|
|
|
void testCreateAuthConfig_ftp();
|
2009-01-30 16:12:41 +00:00
|
|
|
void testUpdateBasicCred();
|
2007-10-30 12:48:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( AuthConfigFactoryTest );
|
|
|
|
|
|
|
|
void AuthConfigFactoryTest::testCreateAuthConfig_http()
|
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Request> req(new Request());
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://localhost/download/aria2-1.0.0.tar.bz2");
|
2007-10-30 12:48:01 +00:00
|
|
|
|
|
|
|
Option option;
|
2010-09-11 12:48:03 +00:00
|
|
|
option.put(PREF_NO_NETRC, A2_V_FALSE);
|
|
|
|
option.put(PREF_HTTP_AUTH_CHALLENGE, A2_V_TRUE);
|
2007-10-30 12:48:01 +00:00
|
|
|
|
2009-07-14 12:37:34 +00:00
|
|
|
AuthConfigFactory factory;
|
2007-10-30 12:48:01 +00:00
|
|
|
|
|
|
|
// without auth info
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
2007-10-30 12:48:01 +00:00
|
|
|
|
2008-09-25 14:37:28 +00:00
|
|
|
// with Netrc
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Netrc> netrc(new Netrc());
|
2008-09-25 14:37:28 +00:00
|
|
|
netrc->addAuthenticator
|
|
|
|
(SharedHandle<Authenticator>(new Authenticator("localhost",
|
2010-01-05 16:01:46 +00:00
|
|
|
"localhostuser",
|
|
|
|
"localhostpass",
|
|
|
|
"localhostacct")));
|
2008-04-20 00:50:22 +00:00
|
|
|
netrc->addAuthenticator
|
|
|
|
(SharedHandle<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
|
2007-10-30 12:48:01 +00:00
|
|
|
factory.setNetrc(netrc);
|
2008-09-25 14:37:28 +00:00
|
|
|
|
2009-01-30 16:12:41 +00:00
|
|
|
// not activated
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2009-07-14 12:37:34 +00:00
|
|
|
CPPUNIT_ASSERT(factory.activateBasicCred("localhost", "/", &option));
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2008-09-25 14:37:28 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2007-10-30 12:48:01 +00:00
|
|
|
|
2008-09-25 14:37:28 +00:00
|
|
|
// See default token in netrc is ignored.
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://mirror/");
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2009-07-14 12:37:34 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.activateBasicCred("mirror", "/", &option));
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
2008-09-25 14:37:28 +00:00
|
|
|
|
2007-10-30 12:48:01 +00:00
|
|
|
// with Netrc + user defined
|
|
|
|
option.put(PREF_HTTP_USER, "userDefinedUser");
|
|
|
|
option.put(PREF_HTTP_PASSWD, "userDefinedPassword");
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2009-07-14 12:37:34 +00:00
|
|
|
CPPUNIT_ASSERT(factory.activateBasicCred("mirror", "/", &option));
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2007-10-30 12:48:01 +00:00
|
|
|
|
2009-01-30 16:12:41 +00:00
|
|
|
// username and password in URI
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
|
2009-01-30 16:12:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2007-10-30 12:48:01 +00:00
|
|
|
}
|
|
|
|
|
2009-02-19 12:02:22 +00:00
|
|
|
void AuthConfigFactoryTest::testCreateAuthConfig_httpNoChallenge()
|
|
|
|
{
|
|
|
|
SharedHandle<Request> req(new Request());
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://localhost/download/aria2-1.0.0.tar.bz2");
|
2009-02-19 12:02:22 +00:00
|
|
|
|
|
|
|
Option option;
|
2010-09-11 12:48:03 +00:00
|
|
|
option.put(PREF_NO_NETRC, A2_V_FALSE);
|
2009-02-19 12:02:22 +00:00
|
|
|
|
2009-07-14 12:37:34 +00:00
|
|
|
AuthConfigFactory factory;
|
2009-02-19 12:02:22 +00:00
|
|
|
|
|
|
|
// without auth info
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
2009-02-19 12:02:22 +00:00
|
|
|
|
|
|
|
// with Netrc
|
|
|
|
SharedHandle<Netrc> netrc(new Netrc());
|
|
|
|
netrc->addAuthenticator
|
|
|
|
(SharedHandle<Authenticator>(new Authenticator("localhost",
|
2010-01-05 16:01:46 +00:00
|
|
|
"localhostuser",
|
|
|
|
"localhostpass",
|
|
|
|
"localhostacct")));
|
2009-02-19 12:02:22 +00:00
|
|
|
netrc->addAuthenticator
|
|
|
|
(SharedHandle<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
|
|
|
|
factory.setNetrc(netrc);
|
|
|
|
|
|
|
|
// not activated
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-02-19 12:02:22 +00:00
|
|
|
|
|
|
|
// See default token in netrc is ignored.
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://mirror/");
|
2009-02-19 12:02:22 +00:00
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
2009-02-19 12:02:22 +00:00
|
|
|
|
|
|
|
// with Netrc + user defined
|
|
|
|
option.put(PREF_HTTP_USER, "userDefinedUser");
|
|
|
|
option.put(PREF_HTTP_PASSWD, "userDefinedPassword");
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-02-19 12:02:22 +00:00
|
|
|
|
|
|
|
// username and password in URI
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
|
2009-02-19 12:02:22 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-02-19 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 12:48:01 +00:00
|
|
|
void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
|
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Request> req(new Request());
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("ftp://localhost/download/aria2-1.0.0.tar.bz2");
|
2007-10-30 12:48:01 +00:00
|
|
|
|
|
|
|
Option option;
|
2010-09-11 12:48:03 +00:00
|
|
|
option.put(PREF_NO_NETRC, A2_V_FALSE);
|
2007-10-30 12:48:01 +00:00
|
|
|
|
2009-07-14 12:37:34 +00:00
|
|
|
AuthConfigFactory factory;
|
2007-10-30 12:48:01 +00:00
|
|
|
|
|
|
|
// without auth info
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("anonymous:ARIA2USER@"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2007-10-30 12:48:01 +00:00
|
|
|
|
|
|
|
// with Netrc
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Netrc> netrc(new Netrc());
|
|
|
|
netrc->addAuthenticator
|
|
|
|
(SharedHandle<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
|
2007-10-30 12:48:01 +00:00
|
|
|
factory.setNetrc(netrc);
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpassword"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2007-10-30 12:48:01 +00:00
|
|
|
|
|
|
|
// disable Netrc
|
2010-09-11 12:48:03 +00:00
|
|
|
option.put(PREF_NO_NETRC, A2_V_TRUE);
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("anonymous:ARIA2USER@"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2007-10-30 12:48:01 +00:00
|
|
|
|
|
|
|
// with Netrc + user defined
|
2010-09-11 12:48:03 +00:00
|
|
|
option.put(PREF_NO_NETRC, A2_V_FALSE);
|
2007-10-30 12:48:01 +00:00
|
|
|
option.put(PREF_FTP_USER, "userDefinedUser");
|
|
|
|
option.put(PREF_FTP_PASSWD, "userDefinedPassword");
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2007-10-30 12:48:01 +00:00
|
|
|
|
2007-12-08 10:49:20 +00:00
|
|
|
// username and password in URI
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("ftp://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-10-27 12:58:46 +00:00
|
|
|
|
|
|
|
// username in URI, but no password. We have DefaultAuthenticator
|
|
|
|
// but username is not aria2user
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("ftp://aria2user@localhost/download/aria2-1.0.0.tar.bz2");
|
2009-10-27 12:58:46 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:userDefinedPassword"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-10-27 12:58:46 +00:00
|
|
|
|
|
|
|
// Recreate netrc with entry for user aria2user
|
|
|
|
netrc.reset(new Netrc());
|
|
|
|
netrc->addAuthenticator
|
|
|
|
(SharedHandle<Authenticator>(new Authenticator("localhost",
|
2010-01-05 16:01:46 +00:00
|
|
|
"aria2user",
|
|
|
|
"netrcpass",
|
|
|
|
"netrcacct")));
|
2009-10-27 12:58:46 +00:00
|
|
|
factory.setNetrc(netrc);
|
|
|
|
// This time, we can find same username "aria2user" in netrc, so the
|
|
|
|
// password "netrcpass" is used, instead of "userDefinedPassword"
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:netrcpass"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-10-27 12:58:46 +00:00
|
|
|
// No netrc entry for host mirror, so "userDefinedPassword" is used.
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("ftp://aria2user@mirror/download/aria2-1.0.0.tar.bz2");
|
2009-10-27 12:58:46 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:userDefinedPassword"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2007-10-30 12:48:01 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-01-30 16:12:41 +00:00
|
|
|
void AuthConfigFactoryTest::testUpdateBasicCred()
|
|
|
|
{
|
|
|
|
Option option;
|
2010-09-11 12:48:03 +00:00
|
|
|
option.put(PREF_NO_NETRC, A2_V_FALSE);
|
|
|
|
option.put(PREF_HTTP_AUTH_CHALLENGE, A2_V_TRUE);
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2009-07-14 12:37:34 +00:00
|
|
|
AuthConfigFactory factory;
|
2009-01-30 16:12:41 +00:00
|
|
|
|
|
|
|
factory.updateBasicCred
|
|
|
|
(AuthConfigFactory::BasicCred("myname", "mypass", "localhost", "/", true));
|
|
|
|
factory.updateBasicCred
|
|
|
|
(AuthConfigFactory::BasicCred("price","j38jdc","localhost","/download", true));
|
|
|
|
factory.updateBasicCred
|
|
|
|
(AuthConfigFactory::BasicCred("alice","ium8","localhost","/documents", true));
|
|
|
|
factory.updateBasicCred
|
|
|
|
(AuthConfigFactory::BasicCred("jack", "jackx","mirror", "/doc", true));
|
|
|
|
|
|
|
|
SharedHandle<Request> req(new Request());
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://localhost/download/v2.6/Changelog");
|
2009-01-30 16:12:41 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("price:j38jdc"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://localhost/documents/reference.html");
|
2009-01-30 16:12:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("alice:ium8"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://localhost/documents2/manual.html");
|
2009-01-30 16:12:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypass"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://localhost/doc/readme.txt");
|
2009-01-30 16:12:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypass"),
|
2010-01-05 16:01:46 +00:00
|
|
|
factory.createAuthConfig(req, &option)->getAuthText());
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://local/");
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
2009-01-30 16:12:41 +00:00
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
req->setUri("http://mirror/");
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
2009-01-30 16:12:41 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|