2007-03-27 16:16:44 +00:00
|
|
|
#include "NetrcAuthResolver.h"
|
2013-06-26 12:45:29 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2007-03-27 16:16:44 +00:00
|
|
|
#include "prefs.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Netrc.h"
|
|
|
|
#include "AuthConfig.h"
|
2013-06-26 12:45:29 +00:00
|
|
|
#include "a2functional.h"
|
2007-03-27 16:16:44 +00:00
|
|
|
|
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-26 12:45:29 +00:00
|
|
|
std::unique_ptr<Netrc> netrc_;
|
|
|
|
std::unique_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());
|
2013-06-26 12:45:29 +00:00
|
|
|
netrc_->addAuthenticator(make_unique<Authenticator>("localhost",
|
|
|
|
"name",
|
|
|
|
"passwd",
|
|
|
|
"account"));
|
|
|
|
netrc_->addAuthenticator(make_unique<DefaultAuthenticator>
|
|
|
|
("default",
|
|
|
|
"defaultpasswd",
|
|
|
|
"defaultaccount"));
|
2007-03-27 16:16:44 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
resolver_.reset(new NetrcAuthResolver());
|
2013-06-26 12:45:29 +00:00
|
|
|
resolver_->setNetrc(netrc_.get());
|
|
|
|
resolver_->setDefaultCred("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-26 12:45:29 +00:00
|
|
|
auto 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");
|
2013-06-26 12:45:29 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"),
|
|
|
|
authConfig->getAuthText());
|
2007-03-27 16:16:44 +00:00
|
|
|
|
2013-06-26 12:45:29 +00:00
|
|
|
resolver_->setNetrc(nullptr);
|
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()
|
|
|
|
{
|
2013-06-26 12:45:29 +00:00
|
|
|
resolver_->setUserDefinedCred("myname", "mypasswd");
|
|
|
|
auto authConfig = resolver_->resolveAuthConfig("localhost");
|
|
|
|
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");
|
2013-06-26 12:45:29 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
|
|
|
|
authConfig->getAuthText());
|
2007-03-27 16:16:44 +00:00
|
|
|
|
2013-06-26 12:45:29 +00:00
|
|
|
resolver_->setNetrc(nullptr);
|
2010-06-21 13:51:56 +00:00
|
|
|
authConfig = resolver_->resolveAuthConfig("mymachine");
|
2013-06-26 12:45:29 +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-26 12:45:29 +00:00
|
|
|
auto 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-26 12:45:29 +00:00
|
|
|
auto defAuthConfig = 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
|