2007-03-27 16:16:44 +00:00
|
|
|
#include "NetrcAuthResolver.h"
|
|
|
|
#include "prefs.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Netrc.h"
|
|
|
|
#include "AuthConfig.h"
|
2007-03-27 16:16:44 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2007-03-27 16:16:44 +00:00
|
|
|
|
|
|
|
class NetrcAuthResolverTest : public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(NetrcAuthResolverTest);
|
|
|
|
CPPUNIT_TEST(testResolveAuthConfig_without_userDefined);
|
|
|
|
CPPUNIT_TEST(testResolveAuthConfig_with_userDefined);
|
2008-09-25 14:37:28 +00:00
|
|
|
CPPUNIT_TEST(testResolveAuthConfig_ignoreDefault);
|
2007-03-27 16:16:44 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Netrc> netrc_;
|
|
|
|
//std::shared_ptr<Option> _option;
|
|
|
|
std::shared_ptr<NetrcAuthResolver> resolver_;
|
2007-03-27 16:16:44 +00:00
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
netrc_.reset(new Netrc());
|
|
|
|
netrc_->addAuthenticator
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::shared_ptr<Authenticator>(new Authenticator("localhost", "name", "passwd", "account")));
|
2010-06-21 13:51:56 +00:00
|
|
|
netrc_->addAuthenticator
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpasswd", "defaultaccount")));
|
2007-03-27 16:16:44 +00:00
|
|
|
|
|
|
|
//_option = new Option();
|
2010-06-21 13:51:56 +00:00
|
|
|
resolver_.reset(new NetrcAuthResolver());
|
|
|
|
resolver_->setNetrc(netrc_);
|
|
|
|
resolver_->setDefaultAuthConfig
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::shared_ptr<AuthConfig>(new AuthConfig("foo", "bar")));
|
2007-03-27 16:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void testResolveAuthConfig_without_userDefined();
|
|
|
|
void testResolveAuthConfig_with_userDefined();
|
2008-09-25 14:37:28 +00:00
|
|
|
void testResolveAuthConfig_ignoreDefault();
|
2007-03-27 16:16:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( NetrcAuthResolverTest );
|
|
|
|
|
|
|
|
void NetrcAuthResolverTest::testResolveAuthConfig_without_userDefined()
|
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name:passwd"), authConfig->getAuthText());
|
2007-03-27 16:16:44 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
authConfig = resolver_->resolveAuthConfig("mymachine");
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"), authConfig->getAuthText());
|
2007-03-27 16:16:44 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
resolver_->setNetrc(std::shared_ptr<Netrc>());
|
2010-06-21 13:51:56 +00:00
|
|
|
authConfig = resolver_->resolveAuthConfig("localhost");
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
|
2007-03-27 16:16:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetrcAuthResolverTest::testResolveAuthConfig_with_userDefined()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
resolver_->setUserDefinedAuthConfig
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::shared_ptr<AuthConfig>(new AuthConfig("myname", "mypasswd")));
|
|
|
|
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
|
2007-03-27 16:16:44 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
authConfig = resolver_->resolveAuthConfig("mymachine");
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
|
2007-03-27 16:16:44 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
resolver_->setNetrc(std::shared_ptr<Netrc>());
|
2010-06-21 13:51:56 +00:00
|
|
|
authConfig = resolver_->resolveAuthConfig("mymachine");
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
|
2007-03-27 16:16:44 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-09-25 14:37:28 +00:00
|
|
|
void NetrcAuthResolverTest::testResolveAuthConfig_ignoreDefault()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
resolver_->ignoreDefault();
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("mirror");
|
2008-09-25 14:37:28 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
|
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
resolver_->useDefault();
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<AuthConfig> defAuthConfig =
|
2010-06-21 13:51:56 +00:00
|
|
|
resolver_->resolveAuthConfig("mirror");
|
2008-09-25 14:37:28 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"),
|
2012-10-01 14:52:22 +00:00
|
|
|
defAuthConfig->getAuthText());
|
2008-09-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|