mirror of https://github.com/aria2/aria2
Rewrite AuthConfig objects using std::unique_ptr
parent
a4cf50914d
commit
d485c8e767
|
@ -34,6 +34,7 @@
|
||||||
/* copyright --> */
|
/* copyright --> */
|
||||||
#include "AbstractAuthResolver.h"
|
#include "AbstractAuthResolver.h"
|
||||||
#include "AuthConfig.h"
|
#include "AuthConfig.h"
|
||||||
|
#include "a2functional.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -41,16 +42,29 @@ AbstractAuthResolver::AbstractAuthResolver() {}
|
||||||
|
|
||||||
AbstractAuthResolver::~AbstractAuthResolver() {}
|
AbstractAuthResolver::~AbstractAuthResolver() {}
|
||||||
|
|
||||||
void AbstractAuthResolver::setUserDefinedAuthConfig
|
void AbstractAuthResolver::setUserDefinedCred
|
||||||
(const std::shared_ptr<AuthConfig>& authConfig)
|
(std::string user, std::string password)
|
||||||
{
|
{
|
||||||
userDefinedAuthConfig_ = authConfig;
|
userDefinedUser_ = std::move(user);
|
||||||
|
userDefinedPassword_ = std::move(password);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractAuthResolver::setDefaultAuthConfig
|
std::unique_ptr<AuthConfig>
|
||||||
(const std::shared_ptr<AuthConfig>& authConfig)
|
AbstractAuthResolver::getUserDefinedAuthConfig() const
|
||||||
{
|
{
|
||||||
defaultAuthConfig_ = authConfig;
|
return AuthConfig::create(userDefinedUser_, userDefinedPassword_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractAuthResolver::setDefaultCred
|
||||||
|
(std::string user, std::string password)
|
||||||
|
{
|
||||||
|
defaultUser_ = std::move(user);
|
||||||
|
defaultPassword_ = std::move(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<AuthConfig> AbstractAuthResolver::getDefaultAuthConfig() const
|
||||||
|
{
|
||||||
|
return AuthConfig::create(defaultUser_, defaultPassword_);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -40,29 +40,24 @@
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class AbstractAuthResolver : public AuthResolver {
|
class AbstractAuthResolver : public AuthResolver {
|
||||||
private:
|
|
||||||
std::shared_ptr<AuthConfig> userDefinedAuthConfig_;
|
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig> defaultAuthConfig_;
|
|
||||||
public:
|
public:
|
||||||
AbstractAuthResolver();
|
AbstractAuthResolver();
|
||||||
|
|
||||||
virtual ~AbstractAuthResolver();
|
virtual ~AbstractAuthResolver();
|
||||||
|
|
||||||
void setUserDefinedAuthConfig(const std::shared_ptr<AuthConfig>& authConfig);
|
void setUserDefinedCred(std::string user, std::string password);
|
||||||
|
|
||||||
const std::shared_ptr<AuthConfig>& getUserDefinedAuthConfig() const
|
std::unique_ptr<AuthConfig> getUserDefinedAuthConfig() const;
|
||||||
{
|
|
||||||
return userDefinedAuthConfig_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setDefaultAuthConfig(const std::shared_ptr<AuthConfig>& authConfig);
|
void setDefaultCred(std::string user, std::string password);
|
||||||
|
|
||||||
const std::shared_ptr<AuthConfig>& getDefaultAuthConfig() const
|
std::unique_ptr<AuthConfig> getDefaultAuthConfig() const;
|
||||||
{
|
private:
|
||||||
return defaultAuthConfig_;
|
std::string userDefinedUser_;
|
||||||
}
|
std::string userDefinedPassword_;
|
||||||
|
|
||||||
|
std::string defaultUser_;
|
||||||
|
std::string defaultPassword_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -42,9 +42,9 @@ namespace aria2 {
|
||||||
|
|
||||||
AuthConfig::AuthConfig() {}
|
AuthConfig::AuthConfig() {}
|
||||||
|
|
||||||
AuthConfig::AuthConfig(const std::string& user, const std::string& password)
|
AuthConfig::AuthConfig(std::string user, std::string password)
|
||||||
: user_(user),
|
: user_(std::move(user)),
|
||||||
password_(password)
|
password_(std::move(password))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
AuthConfig::~AuthConfig() {}
|
AuthConfig::~AuthConfig() {}
|
||||||
|
@ -57,6 +57,16 @@ std::string AuthConfig::getAuthText() const
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<AuthConfig> AuthConfig::create
|
||||||
|
(std::string user, std::string password)
|
||||||
|
{
|
||||||
|
if(user.empty()) {
|
||||||
|
return std::unique_ptr<AuthConfig>();
|
||||||
|
} else {
|
||||||
|
return make_unique<AuthConfig>(std::move(user), std::move(password));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& o,
|
std::ostream& operator<<(std::ostream& o,
|
||||||
const std::shared_ptr<AuthConfig>& authConfig)
|
const std::shared_ptr<AuthConfig>& authConfig)
|
||||||
{
|
{
|
||||||
|
|
|
@ -50,7 +50,7 @@ private:
|
||||||
std::string password_;
|
std::string password_;
|
||||||
public:
|
public:
|
||||||
AuthConfig();
|
AuthConfig();
|
||||||
AuthConfig(const std::string& user, const std::string& password);
|
AuthConfig(std::string user, std::string password);
|
||||||
~AuthConfig();
|
~AuthConfig();
|
||||||
|
|
||||||
// Don't allow copying
|
// Don't allow copying
|
||||||
|
@ -68,6 +68,9 @@ public:
|
||||||
{
|
{
|
||||||
return password_;
|
return password_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::unique_ptr<AuthConfig> create
|
||||||
|
(std::string user, std::string password);
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& o,
|
std::ostream& operator<<(std::ostream& o,
|
||||||
|
|
|
@ -56,33 +56,32 @@ AuthConfigFactory::AuthConfigFactory() {}
|
||||||
|
|
||||||
AuthConfigFactory::~AuthConfigFactory() {}
|
AuthConfigFactory::~AuthConfigFactory() {}
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig>
|
std::unique_ptr<AuthConfig>
|
||||||
AuthConfigFactory::createAuthConfig
|
AuthConfigFactory::createAuthConfig
|
||||||
(const std::shared_ptr<Request>& request, const Option* op)
|
(const std::shared_ptr<Request>& request, const Option* op)
|
||||||
{
|
{
|
||||||
if(request->getProtocol() == "http" || request->getProtocol() == "https") {
|
if(request->getProtocol() == "http" || request->getProtocol() == "https") {
|
||||||
|
|
||||||
if(op->getAsBool(PREF_HTTP_AUTH_CHALLENGE)) {
|
if(op->getAsBool(PREF_HTTP_AUTH_CHALLENGE)) {
|
||||||
if(!request->getUsername().empty()) {
|
if(!request->getUsername().empty()) {
|
||||||
std::shared_ptr<BasicCred> bc(new BasicCred(request->getUsername(),
|
updateBasicCred(make_unique<BasicCred>(request->getUsername(),
|
||||||
request->getPassword(),
|
request->getPassword(),
|
||||||
request->getHost(),
|
request->getHost(),
|
||||||
request->getPort(),
|
request->getPort(),
|
||||||
request->getDir(), true));
|
request->getDir(), true));
|
||||||
updateBasicCred(bc);
|
return AuthConfig::create(request->getUsername(),
|
||||||
return createAuthConfig(request->getUsername(), request->getPassword());
|
request->getPassword());
|
||||||
}
|
}
|
||||||
BasicCredSet::iterator i =
|
auto i = findBasicCred(request->getHost(), request->getPort(),
|
||||||
findBasicCred(request->getHost(), request->getPort(),
|
|
||||||
request->getDir());
|
request->getDir());
|
||||||
if(i == basicCreds_.end()) {
|
if(i == std::end(basicCreds_)) {
|
||||||
return std::shared_ptr<AuthConfig>();
|
return std::unique_ptr<AuthConfig>();
|
||||||
} else {
|
} else {
|
||||||
return createAuthConfig((*i)->user_, (*i)->password_);
|
return AuthConfig::create((*i)->user_, (*i)->password_);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(!request->getUsername().empty()) {
|
if(!request->getUsername().empty()) {
|
||||||
return createAuthConfig(request->getUsername(), request->getPassword());
|
return AuthConfig::create(request->getUsername(),
|
||||||
|
request->getPassword());
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
createHttpAuthResolver(op)->resolveAuthConfig(request->getHost());
|
createHttpAuthResolver(op)->resolveAuthConfig(request->getHost());
|
||||||
|
@ -91,93 +90,80 @@ AuthConfigFactory::createAuthConfig
|
||||||
} else if(request->getProtocol() == "ftp") {
|
} else if(request->getProtocol() == "ftp") {
|
||||||
if(!request->getUsername().empty()) {
|
if(!request->getUsername().empty()) {
|
||||||
if(request->hasPassword()) {
|
if(request->hasPassword()) {
|
||||||
return createAuthConfig(request->getUsername(), request->getPassword());
|
return AuthConfig::create(request->getUsername(),
|
||||||
|
request->getPassword());
|
||||||
} else {
|
} else {
|
||||||
if(!op->getAsBool(PREF_NO_NETRC)) {
|
if(!op->getAsBool(PREF_NO_NETRC)) {
|
||||||
// First, check we have password corresponding to host and
|
// First, check we have password corresponding to host and
|
||||||
// username
|
// username
|
||||||
NetrcAuthResolver authResolver;
|
NetrcAuthResolver authResolver;
|
||||||
authResolver.setNetrc(netrc_);
|
authResolver.setNetrc(netrc_.get());
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig> ac =
|
auto ac = authResolver.resolveAuthConfig(request->getHost());
|
||||||
authResolver.resolveAuthConfig(request->getHost());
|
|
||||||
if(ac && ac->getUser() == request->getUsername()) {
|
if(ac && ac->getUser() == request->getUsername()) {
|
||||||
return ac;
|
return ac;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// We don't have password for host and username. Return
|
// We don't have password for host and username. Return
|
||||||
// password specified by --ftp-passwd
|
// password specified by --ftp-passwd
|
||||||
return
|
return AuthConfig::create(request->getUsername(),
|
||||||
createAuthConfig(request->getUsername(), op->get(PREF_FTP_PASSWD));
|
op->get(PREF_FTP_PASSWD));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
createFtpAuthResolver(op)->resolveAuthConfig(request->getHost());
|
createFtpAuthResolver(op)->resolveAuthConfig(request->getHost());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return std::shared_ptr<AuthConfig>();
|
return std::unique_ptr<AuthConfig>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig>
|
std::unique_ptr<AuthResolver> AuthConfigFactory::createHttpAuthResolver
|
||||||
AuthConfigFactory::createAuthConfig(const std::string& user, const std::string& password) const
|
|
||||||
{
|
|
||||||
std::shared_ptr<AuthConfig> ac;
|
|
||||||
if(!user.empty()) {
|
|
||||||
ac.reset(new AuthConfig(user, password));
|
|
||||||
}
|
|
||||||
return ac;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<AuthResolver> AuthConfigFactory::createHttpAuthResolver
|
|
||||||
(const Option* op) const
|
(const Option* op) const
|
||||||
{
|
{
|
||||||
AbstractAuthResolver* resolver;
|
std::unique_ptr<AbstractAuthResolver> resolver;
|
||||||
if(op->getAsBool(PREF_NO_NETRC)) {
|
if(op->getAsBool(PREF_NO_NETRC)) {
|
||||||
resolver = new DefaultAuthResolver();
|
resolver.reset(new DefaultAuthResolver());
|
||||||
} else {
|
} else {
|
||||||
NetrcAuthResolver* authResolver(new NetrcAuthResolver());
|
auto authResolver = make_unique<NetrcAuthResolver>();
|
||||||
authResolver->setNetrc(netrc_);
|
authResolver->setNetrc(netrc_.get());
|
||||||
authResolver->ignoreDefault();
|
authResolver->ignoreDefault();
|
||||||
resolver = authResolver;
|
resolver = std::move(authResolver);
|
||||||
}
|
}
|
||||||
resolver->setUserDefinedAuthConfig
|
resolver->setUserDefinedCred(op->get(PREF_HTTP_USER),
|
||||||
(createAuthConfig(op->get(PREF_HTTP_USER), op->get(PREF_HTTP_PASSWD)));
|
op->get(PREF_HTTP_PASSWD));
|
||||||
return std::shared_ptr<AuthResolver>(resolver);
|
return std::move(resolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<AuthResolver> AuthConfigFactory::createFtpAuthResolver
|
std::unique_ptr<AuthResolver> AuthConfigFactory::createFtpAuthResolver
|
||||||
(const Option* op) const
|
(const Option* op) const
|
||||||
{
|
{
|
||||||
AbstractAuthResolver* resolver;
|
std::unique_ptr<AbstractAuthResolver> resolver;
|
||||||
if(op->getAsBool(PREF_NO_NETRC)) {
|
if(op->getAsBool(PREF_NO_NETRC)) {
|
||||||
resolver = new DefaultAuthResolver();
|
resolver.reset(new DefaultAuthResolver());
|
||||||
} else {
|
} else {
|
||||||
NetrcAuthResolver* authResolver(new NetrcAuthResolver());
|
auto authResolver = make_unique<NetrcAuthResolver>();
|
||||||
authResolver->setNetrc(netrc_);
|
authResolver->setNetrc(netrc_.get());
|
||||||
resolver = authResolver;
|
resolver = std::move(authResolver);
|
||||||
}
|
}
|
||||||
resolver->setUserDefinedAuthConfig
|
resolver->setUserDefinedCred(op->get(PREF_FTP_USER),
|
||||||
(createAuthConfig(op->get(PREF_FTP_USER), op->get(PREF_FTP_PASSWD)));
|
op->get(PREF_FTP_PASSWD));
|
||||||
std::shared_ptr<AuthConfig> defaultAuthConfig
|
resolver->setDefaultCred(AUTH_DEFAULT_USER, AUTH_DEFAULT_PASSWD);
|
||||||
(new AuthConfig(AUTH_DEFAULT_USER, AUTH_DEFAULT_PASSWD));
|
return std::move(resolver);
|
||||||
resolver->setDefaultAuthConfig(defaultAuthConfig);
|
|
||||||
return std::shared_ptr<AuthResolver>(resolver);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AuthConfigFactory::setNetrc(const std::shared_ptr<Netrc>& netrc)
|
void AuthConfigFactory::setNetrc(std::unique_ptr<Netrc> netrc)
|
||||||
{
|
{
|
||||||
netrc_ = netrc;
|
netrc_ = std::move(netrc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AuthConfigFactory::updateBasicCred
|
void AuthConfigFactory::updateBasicCred(std::unique_ptr<BasicCred> basicCred)
|
||||||
(const std::shared_ptr<BasicCred>& basicCred)
|
|
||||||
{
|
{
|
||||||
BasicCredSet::iterator i = basicCreds_.lower_bound(basicCred);
|
auto i = basicCreds_.lower_bound(basicCred);
|
||||||
if(i != basicCreds_.end() && *(*i) == *basicCred) {
|
if(i != std::end(basicCreds_) && *i == basicCred) {
|
||||||
*(*i) = *basicCred;
|
*(*i) = std::move(*basicCred);
|
||||||
} else {
|
} else {
|
||||||
basicCreds_.insert(i, basicCred);
|
basicCreds_.insert(i, std::move(basicCred));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,17 +173,15 @@ bool AuthConfigFactory::activateBasicCred
|
||||||
const std::string& path,
|
const std::string& path,
|
||||||
const Option* op)
|
const Option* op)
|
||||||
{
|
{
|
||||||
BasicCredSet::iterator i = findBasicCred(host, port, path);
|
auto i = findBasicCred(host, port, path);
|
||||||
if(i == basicCreds_.end()) {
|
if(i == std::end(basicCreds_)) {
|
||||||
std::shared_ptr<AuthConfig> authConfig =
|
auto authConfig = createHttpAuthResolver(op)->resolveAuthConfig(host);
|
||||||
createHttpAuthResolver(op)->resolveAuthConfig(host);
|
|
||||||
if(!authConfig) {
|
if(!authConfig) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
std::shared_ptr<BasicCred> bc
|
basicCreds_.insert(make_unique<BasicCred>(authConfig->getUser(),
|
||||||
(new BasicCred(authConfig->getUser(), authConfig->getPassword(),
|
authConfig->getPassword(),
|
||||||
host, port, path, true));
|
host, port, path, true));
|
||||||
basicCreds_.insert(bc);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -206,55 +190,59 @@ bool AuthConfigFactory::activateBasicCred
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthConfigFactory::BasicCred::BasicCred
|
BasicCred::BasicCred
|
||||||
(const std::string& user, const std::string& password,
|
(std::string user, std::string password,
|
||||||
const std::string& host, uint16_t port, const std::string& path,
|
std::string host, uint16_t port, std::string path,
|
||||||
bool activated):
|
bool activated)
|
||||||
user_(user), password_(password),
|
: user_(std::move(user)),
|
||||||
host_(host), port_(port), path_(path), activated_(activated)
|
password_(std::move(password)),
|
||||||
|
host_(std::move(host)),
|
||||||
|
port_(port),
|
||||||
|
path_(std::move(path)),
|
||||||
|
activated_(activated)
|
||||||
{
|
{
|
||||||
if(path_.empty() || path_[path_.size()-1] != '/') {
|
if(path_.empty() || path_[path_.size()-1] != '/') {
|
||||||
path_ += "/";
|
path_ += "/";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AuthConfigFactory::BasicCred::activate()
|
void BasicCred::activate()
|
||||||
{
|
{
|
||||||
activated_ = true;
|
activated_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AuthConfigFactory::BasicCred::isActivated() const
|
bool BasicCred::isActivated() const
|
||||||
{
|
{
|
||||||
return activated_;
|
return activated_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AuthConfigFactory::BasicCred::operator==(const BasicCred& cred) const
|
bool BasicCred::operator==(const BasicCred& cred) const
|
||||||
{
|
{
|
||||||
return host_ == cred.host_ && port_ == cred.port_ && path_ == cred.path_;
|
return host_ == cred.host_ && port_ == cred.port_ && path_ == cred.path_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AuthConfigFactory::BasicCred::operator<(const BasicCred& cred) const
|
bool BasicCred::operator<(const BasicCred& cred) const
|
||||||
{
|
{
|
||||||
return host_ < cred.host_ ||
|
return host_ < cred.host_ ||
|
||||||
(!(cred.host_ < host_) && (port_ < cred.port_ ||
|
(!(cred.host_ < host_) && (port_ < cred.port_ ||
|
||||||
(!(cred.port_ < port_) && path_ > cred.path_)));
|
(!(cred.port_ < port_) && path_ > cred.path_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthConfigFactory::BasicCredSet::iterator
|
AuthConfigFactory::BasicCredSet::iterator AuthConfigFactory::findBasicCred
|
||||||
AuthConfigFactory::findBasicCred
|
|
||||||
(const std::string& host,
|
(const std::string& host,
|
||||||
uint16_t port,
|
uint16_t port,
|
||||||
const std::string& path)
|
const std::string& path)
|
||||||
{
|
{
|
||||||
std::shared_ptr<BasicCred> bc(new BasicCred("", "", host, port, path));
|
auto bc = make_unique<BasicCred>("", "", host, port, path);
|
||||||
BasicCredSet::iterator i = basicCreds_.lower_bound(bc);
|
auto i = basicCreds_.lower_bound(bc);
|
||||||
for(; i != basicCreds_.end() && (*i)->host_ == host && (*i)->port_ == port;
|
for(; i != std::end(basicCreds_) &&
|
||||||
++i) {
|
(*i)->host_ == host &&
|
||||||
|
(*i)->port_ == port; ++i) {
|
||||||
if(util::startsWith(bc->path_, (*i)->path_)) {
|
if(util::startsWith(bc->path_, (*i)->path_)) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return basicCreds_.end();
|
return std::end(basicCreds_);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -52,17 +52,6 @@ class AuthConfig;
|
||||||
class Request;
|
class Request;
|
||||||
class AuthResolver;
|
class AuthResolver;
|
||||||
|
|
||||||
class AuthConfigFactory {
|
|
||||||
private:
|
|
||||||
std::shared_ptr<Netrc> netrc_;
|
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig> createAuthConfig(const std::string& user,
|
|
||||||
const std::string& password) const;
|
|
||||||
|
|
||||||
std::shared_ptr<AuthResolver> createHttpAuthResolver(const Option* op) const;
|
|
||||||
|
|
||||||
std::shared_ptr<AuthResolver> createFtpAuthResolver(const Option* op) const;
|
|
||||||
public:
|
|
||||||
class BasicCred {
|
class BasicCred {
|
||||||
public:
|
public:
|
||||||
std::string user_;
|
std::string user_;
|
||||||
|
@ -72,8 +61,8 @@ public:
|
||||||
std::string path_;
|
std::string path_;
|
||||||
bool activated_;
|
bool activated_;
|
||||||
|
|
||||||
BasicCred(const std::string& user, const std::string& password,
|
BasicCred(std::string user, std::string password,
|
||||||
const std::string& host, uint16_t port, const std::string& path,
|
std::string host, uint16_t port, std::string path,
|
||||||
bool activated = false);
|
bool activated = false);
|
||||||
|
|
||||||
void activate();
|
void activate();
|
||||||
|
@ -85,9 +74,17 @@ public:
|
||||||
bool operator<(const BasicCred& cred) const;
|
bool operator<(const BasicCred& cred) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::set<std::shared_ptr<BasicCred>,
|
class AuthConfigFactory {
|
||||||
DerefLess<std::shared_ptr<BasicCred> > > BasicCredSet;
|
public:
|
||||||
|
typedef std::set<std::unique_ptr<BasicCred>,
|
||||||
|
DerefLess<std::unique_ptr<BasicCred>>> BasicCredSet;
|
||||||
private:
|
private:
|
||||||
|
std::unique_ptr<Netrc> netrc_;
|
||||||
|
|
||||||
|
std::unique_ptr<AuthResolver> createHttpAuthResolver(const Option* op) const;
|
||||||
|
|
||||||
|
std::unique_ptr<AuthResolver> createFtpAuthResolver(const Option* op) const;
|
||||||
|
|
||||||
BasicCredSet basicCreds_;
|
BasicCredSet basicCreds_;
|
||||||
public:
|
public:
|
||||||
AuthConfigFactory();
|
AuthConfigFactory();
|
||||||
|
@ -98,10 +95,10 @@ public:
|
||||||
// are used in this method: PREF_HTTP_USER, PREF_HTTP_PASSWD,
|
// are used in this method: PREF_HTTP_USER, PREF_HTTP_PASSWD,
|
||||||
// PREF_FTP_USER, PREF_FTP_PASSWD, PREF_NO_NETRC and
|
// PREF_FTP_USER, PREF_FTP_PASSWD, PREF_NO_NETRC and
|
||||||
// PREF_HTTP_AUTH_CHALLENGE.
|
// PREF_HTTP_AUTH_CHALLENGE.
|
||||||
std::shared_ptr<AuthConfig> createAuthConfig
|
std::unique_ptr<AuthConfig> createAuthConfig
|
||||||
(const std::shared_ptr<Request>& request, const Option* op);
|
(const std::shared_ptr<Request>& request, const Option* op);
|
||||||
|
|
||||||
void setNetrc(const std::shared_ptr<Netrc>& netrc);
|
void setNetrc(std::unique_ptr<Netrc> netrc);
|
||||||
|
|
||||||
// Find a BasicCred using findBasicCred() and activate it then
|
// Find a BasicCred using findBasicCred() and activate it then
|
||||||
// return true. If matching BasicCred is not found, AuthConfig
|
// return true. If matching BasicCred is not found, AuthConfig
|
||||||
|
@ -127,7 +124,9 @@ public:
|
||||||
// If the same BasicCred is already added, then it is replaced with
|
// If the same BasicCred is already added, then it is replaced with
|
||||||
// given basicCred. Otherwise, insert given basicCred to
|
// given basicCred. Otherwise, insert given basicCred to
|
||||||
// basicCreds_.
|
// basicCreds_.
|
||||||
void updateBasicCred(const std::shared_ptr<BasicCred>& basicCred);
|
//
|
||||||
|
// Made public for unit test.
|
||||||
|
void updateBasicCred(std::unique_ptr<BasicCred> basicCred);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -48,7 +48,8 @@ class AuthResolver {
|
||||||
public:
|
public:
|
||||||
virtual ~AuthResolver() {}
|
virtual ~AuthResolver() {}
|
||||||
|
|
||||||
virtual std::shared_ptr<AuthConfig> resolveAuthConfig(const std::string& hostname) = 0;
|
virtual std::unique_ptr<AuthConfig> resolveAuthConfig
|
||||||
|
(const std::string& hostname) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -37,13 +37,14 @@
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig> DefaultAuthResolver::resolveAuthConfig
|
std::unique_ptr<AuthConfig> DefaultAuthResolver::resolveAuthConfig
|
||||||
(const std::string& hostname)
|
(const std::string& hostname)
|
||||||
{
|
{
|
||||||
if(!getUserDefinedAuthConfig()) {
|
auto authConfig = getUserDefinedAuthConfig();
|
||||||
return getDefaultAuthConfig();
|
if(authConfig) {
|
||||||
|
return authConfig;
|
||||||
} else {
|
} else {
|
||||||
return getUserDefinedAuthConfig();
|
return getDefaultAuthConfig();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ namespace aria2 {
|
||||||
|
|
||||||
class DefaultAuthResolver : public AbstractAuthResolver {
|
class DefaultAuthResolver : public AbstractAuthResolver {
|
||||||
public:
|
public:
|
||||||
virtual std::shared_ptr<AuthConfig> resolveAuthConfig
|
virtual std::unique_ptr<AuthConfig> resolveAuthConfig
|
||||||
(const std::string& hostname);
|
(const std::string& hostname);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -200,9 +200,9 @@ int MultiUrlRequestInfo::prepare()
|
||||||
A2_LOG_NOTICE(fmt(MSG_INCORRECT_NETRC_PERMISSION,
|
A2_LOG_NOTICE(fmt(MSG_INCORRECT_NETRC_PERMISSION,
|
||||||
option_->get(PREF_NETRC_PATH).c_str()));
|
option_->get(PREF_NETRC_PATH).c_str()));
|
||||||
} else {
|
} else {
|
||||||
std::shared_ptr<Netrc> netrc(new Netrc());
|
auto netrc = make_unique<Netrc>();
|
||||||
netrc->parse(option_->get(PREF_NETRC_PATH));
|
netrc->parse(option_->get(PREF_NETRC_PATH));
|
||||||
authConfigFactory->setNetrc(netrc);
|
authConfigFactory->setNetrc(std::move(netrc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
e_->setAuthConfigFactory(std::move(authConfigFactory));
|
e_->setAuthConfigFactory(std::move(authConfigFactory));
|
||||||
|
|
88
src/Netrc.cc
88
src/Netrc.cc
|
@ -50,14 +50,14 @@ namespace aria2 {
|
||||||
Authenticator::Authenticator() {}
|
Authenticator::Authenticator() {}
|
||||||
|
|
||||||
Authenticator::Authenticator
|
Authenticator::Authenticator
|
||||||
(const std::string& machine,
|
(std::string machine,
|
||||||
const std::string& login,
|
std::string login,
|
||||||
const std::string& password,
|
std::string password,
|
||||||
const std::string& account)
|
std::string account)
|
||||||
: machine_(machine),
|
: machine_(std::move(machine)),
|
||||||
login_(login),
|
login_(std::move(login)),
|
||||||
password_(password),
|
password_(std::move(password)),
|
||||||
account_(account)
|
account_(std::move(account))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Authenticator::~Authenticator() {}
|
Authenticator::~Authenticator() {}
|
||||||
|
@ -67,33 +67,34 @@ bool Authenticator::match(const std::string& hostname) const
|
||||||
return util::noProxyDomainMatch(hostname, machine_);
|
return util::noProxyDomainMatch(hostname, machine_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Authenticator::setMachine(const std::string& machine)
|
void Authenticator::setMachine(std::string machine)
|
||||||
{
|
{
|
||||||
machine_ = machine;
|
machine_ = std::move(machine);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Authenticator::setLogin(const std::string& login)
|
void Authenticator::setLogin(std::string login)
|
||||||
{
|
{
|
||||||
login_ = login;
|
login_ = std::move(login);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Authenticator::setPassword(const std::string& password)
|
void Authenticator::setPassword(std::string password)
|
||||||
{
|
{
|
||||||
password_ = password;
|
password_ = std::move(password);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Authenticator::setAccount(const std::string& account)
|
void Authenticator::setAccount(std::string account)
|
||||||
{
|
{
|
||||||
account_ = account;
|
account_ = std::move(account);
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultAuthenticator::DefaultAuthenticator() {}
|
DefaultAuthenticator::DefaultAuthenticator() {}
|
||||||
|
|
||||||
DefaultAuthenticator::DefaultAuthenticator
|
DefaultAuthenticator::DefaultAuthenticator
|
||||||
(const std::string& login,
|
(std::string login,
|
||||||
const std::string& password,
|
std::string password,
|
||||||
const std::string& account)
|
std::string account)
|
||||||
: Authenticator(A2STR::NIL, login, password, account)
|
: Authenticator("", std::move(login), std::move(password),
|
||||||
|
std::move(account))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
DefaultAuthenticator::~DefaultAuthenticator() {}
|
DefaultAuthenticator::~DefaultAuthenticator() {}
|
||||||
|
@ -107,9 +108,9 @@ Netrc::Netrc() {}
|
||||||
|
|
||||||
Netrc::~Netrc() {}
|
Netrc::~Netrc() {}
|
||||||
|
|
||||||
void Netrc::addAuthenticator(const std::shared_ptr<Authenticator>& authenticator)
|
void Netrc::addAuthenticator(std::unique_ptr<Authenticator> authenticator)
|
||||||
{
|
{
|
||||||
authenticators_.push_back(authenticator);
|
authenticators_.push_back(std::move(authenticator));
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -146,7 +147,7 @@ void Netrc::parse(const std::string& path)
|
||||||
SET_ACCOUNT,
|
SET_ACCOUNT,
|
||||||
SET_MACDEF
|
SET_MACDEF
|
||||||
};
|
};
|
||||||
std::shared_ptr<Authenticator> authenticator;
|
std::unique_ptr<Authenticator> authenticator;
|
||||||
STATE state = GET_TOKEN;
|
STATE state = GET_TOKEN;
|
||||||
while(1) {
|
while(1) {
|
||||||
std::string line = fp.getLine();
|
std::string line = fp.getLine();
|
||||||
|
@ -169,11 +170,11 @@ void Netrc::parse(const std::string& path)
|
||||||
eoi = tokens.end(); iter != eoi; ++iter) {
|
eoi = tokens.end(); iter != eoi; ++iter) {
|
||||||
if(state == GET_TOKEN) {
|
if(state == GET_TOKEN) {
|
||||||
if(util::streq((*iter).first, (*iter).second, "machine")) {
|
if(util::streq((*iter).first, (*iter).second, "machine")) {
|
||||||
storeAuthenticator(authenticator);
|
storeAuthenticator(std::move(authenticator));
|
||||||
authenticator.reset(new Authenticator());
|
authenticator.reset(new Authenticator());
|
||||||
state = SET_MACHINE;
|
state = SET_MACHINE;
|
||||||
} else if(util::streq((*iter).first, (*iter).second, "default")) {
|
} else if(util::streq((*iter).first, (*iter).second, "default")) {
|
||||||
storeAuthenticator(authenticator);
|
storeAuthenticator(std::move(authenticator));
|
||||||
authenticator.reset(new DefaultAuthenticator());
|
authenticator.reset(new DefaultAuthenticator());
|
||||||
} else {
|
} else {
|
||||||
if(!authenticator) {
|
if(!authenticator) {
|
||||||
|
@ -194,13 +195,13 @@ void Netrc::parse(const std::string& path)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(state == SET_MACHINE) {
|
if(state == SET_MACHINE) {
|
||||||
authenticator->setMachine((*iter).first, (*iter).second);
|
authenticator->setMachine({(*iter).first, (*iter).second});
|
||||||
} else if(state == SET_LOGIN) {
|
} else if(state == SET_LOGIN) {
|
||||||
authenticator->setLogin((*iter).first, (*iter).second);
|
authenticator->setLogin({(*iter).first, (*iter).second});
|
||||||
} else if(state == SET_PASSWORD) {
|
} else if(state == SET_PASSWORD) {
|
||||||
authenticator->setPassword((*iter).first, (*iter).second);
|
authenticator->setPassword({(*iter).first, (*iter).second});
|
||||||
} else if(state == SET_ACCOUNT) {
|
} else if(state == SET_ACCOUNT) {
|
||||||
authenticator->setAccount((*iter).first, (*iter).second);
|
authenticator->setAccount({(*iter).first, (*iter).second});
|
||||||
} else if(state == SET_MACDEF) {
|
} else if(state == SET_MACDEF) {
|
||||||
skipMacdef(fp);
|
skipMacdef(fp);
|
||||||
}
|
}
|
||||||
|
@ -212,13 +213,13 @@ void Netrc::parse(const std::string& path)
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
("Netrc:parse error. EOF reached where a token expected.");
|
("Netrc:parse error. EOF reached where a token expected.");
|
||||||
}
|
}
|
||||||
storeAuthenticator(authenticator);
|
storeAuthenticator(std::move(authenticator));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Netrc::storeAuthenticator(const std::shared_ptr<Authenticator>& authenticator)
|
void Netrc::storeAuthenticator(std::unique_ptr<Authenticator> authenticator)
|
||||||
{
|
{
|
||||||
if(authenticator) {
|
if(authenticator) {
|
||||||
authenticators_.push_back(authenticator);
|
authenticators_.push_back(std::move(authenticator));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,24 +230,31 @@ private:
|
||||||
public:
|
public:
|
||||||
AuthHostMatch(const std::string& hostname):hostname(hostname) {}
|
AuthHostMatch(const std::string& hostname):hostname(hostname) {}
|
||||||
|
|
||||||
bool operator()(const std::shared_ptr<Authenticator>& authenticator)
|
bool operator()(const std::unique_ptr<Authenticator>& authenticator)
|
||||||
{
|
{
|
||||||
return authenticator->match(hostname);
|
return authenticator->match(hostname);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::shared_ptr<Authenticator>
|
const Authenticator*
|
||||||
Netrc::findAuthenticator(const std::string& hostname) const
|
Netrc::findAuthenticator(const std::string& hostname) const
|
||||||
{
|
{
|
||||||
std::shared_ptr<Authenticator> res;
|
std::unique_ptr<Authenticator> res;
|
||||||
std::vector<std::shared_ptr<Authenticator> >::const_iterator itr =
|
auto itr = std::find_if(std::begin(authenticators_),
|
||||||
std::find_if(authenticators_.begin(), authenticators_.end(),
|
std::end(authenticators_),
|
||||||
AuthHostMatch(hostname));
|
AuthHostMatch(hostname));
|
||||||
if(itr != authenticators_.end()) {
|
if(itr == std::end(authenticators_)) {
|
||||||
res = *itr;
|
return nullptr;
|
||||||
|
} else {
|
||||||
|
return (*itr).get();
|
||||||
}
|
}
|
||||||
return res;
|
}
|
||||||
|
|
||||||
|
const std::vector<std::unique_ptr<Authenticator>>&
|
||||||
|
Netrc::getAuthenticators() const
|
||||||
|
{
|
||||||
|
return authenticators_;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
60
src/Netrc.h
60
src/Netrc.h
|
@ -59,10 +59,10 @@ private:
|
||||||
public:
|
public:
|
||||||
Authenticator();
|
Authenticator();
|
||||||
|
|
||||||
Authenticator(const std::string& machine,
|
Authenticator(std::string machine,
|
||||||
const std::string& login,
|
std::string login,
|
||||||
const std::string& password,
|
std::string password,
|
||||||
const std::string& account);
|
std::string account);
|
||||||
|
|
||||||
virtual ~Authenticator();
|
virtual ~Authenticator();
|
||||||
|
|
||||||
|
@ -73,61 +73,37 @@ public:
|
||||||
return machine_;
|
return machine_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMachine(const std::string& machine);
|
void setMachine(std::string machine);
|
||||||
|
|
||||||
template<typename InputIterator>
|
|
||||||
void setMachine(InputIterator first, InputIterator last)
|
|
||||||
{
|
|
||||||
machine_.assign(first, last);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& getLogin() const
|
const std::string& getLogin() const
|
||||||
{
|
{
|
||||||
return login_;
|
return login_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLogin(const std::string& login);
|
void setLogin(std::string login);
|
||||||
|
|
||||||
template<typename InputIterator>
|
|
||||||
void setLogin(InputIterator first, InputIterator last)
|
|
||||||
{
|
|
||||||
login_.assign(first, last);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& getPassword() const
|
const std::string& getPassword() const
|
||||||
{
|
{
|
||||||
return password_;
|
return password_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPassword(const std::string& password);
|
void setPassword(std::string password);
|
||||||
|
|
||||||
template<typename InputIterator>
|
|
||||||
void setPassword(InputIterator first, InputIterator last)
|
|
||||||
{
|
|
||||||
password_.assign(first, last);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& getAccount() const
|
const std::string& getAccount() const
|
||||||
{
|
{
|
||||||
return account_;
|
return account_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setAccount(const std::string& account);
|
void setAccount(std::string account);
|
||||||
|
|
||||||
template<typename InputIterator>
|
|
||||||
void setAccount(InputIterator first, InputIterator last)
|
|
||||||
{
|
|
||||||
account_.assign(first, last);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class DefaultAuthenticator : public Authenticator {
|
class DefaultAuthenticator : public Authenticator {
|
||||||
public:
|
public:
|
||||||
DefaultAuthenticator();
|
DefaultAuthenticator();
|
||||||
|
|
||||||
DefaultAuthenticator(const std::string& login,
|
DefaultAuthenticator(std::string login,
|
||||||
const std::string& password,
|
std::string password,
|
||||||
const std::string& account);
|
std::string account);
|
||||||
|
|
||||||
virtual ~DefaultAuthenticator();
|
virtual ~DefaultAuthenticator();
|
||||||
|
|
||||||
|
@ -136,9 +112,9 @@ public:
|
||||||
|
|
||||||
class Netrc {
|
class Netrc {
|
||||||
private:
|
private:
|
||||||
std::vector<std::shared_ptr<Authenticator> > authenticators_;
|
std::vector<std::unique_ptr<Authenticator>> authenticators_;
|
||||||
|
|
||||||
void storeAuthenticator(const std::shared_ptr<Authenticator>& authenticator);
|
void storeAuthenticator(std::unique_ptr<Authenticator> authenticator);
|
||||||
public:
|
public:
|
||||||
Netrc();
|
Netrc();
|
||||||
|
|
||||||
|
@ -146,15 +122,11 @@ public:
|
||||||
|
|
||||||
void parse(const std::string& path);
|
void parse(const std::string& path);
|
||||||
|
|
||||||
std::shared_ptr<Authenticator> findAuthenticator
|
const Authenticator* findAuthenticator(const std::string& hostname) const;
|
||||||
(const std::string& hostname) const;
|
|
||||||
|
|
||||||
const std::vector<std::shared_ptr<Authenticator> >& getAuthenticators() const
|
const std::vector<std::unique_ptr<Authenticator>>& getAuthenticators() const;
|
||||||
{
|
|
||||||
return authenticators_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addAuthenticator(const std::shared_ptr<Authenticator>& authenticator);
|
void addAuthenticator(std::unique_ptr<Authenticator> authenticator);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -35,42 +35,44 @@
|
||||||
#include "NetrcAuthResolver.h"
|
#include "NetrcAuthResolver.h"
|
||||||
#include "AuthConfig.h"
|
#include "AuthConfig.h"
|
||||||
#include "Netrc.h"
|
#include "Netrc.h"
|
||||||
|
#include "a2functional.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
NetrcAuthResolver::NetrcAuthResolver():ignoreDefault_(false) {}
|
NetrcAuthResolver::NetrcAuthResolver()
|
||||||
|
: netrc_(nullptr), ignoreDefault_(false) {}
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig>
|
std::unique_ptr<AuthConfig>
|
||||||
NetrcAuthResolver::resolveAuthConfig(const std::string& hostname)
|
NetrcAuthResolver::resolveAuthConfig(const std::string& hostname)
|
||||||
{
|
{
|
||||||
if(!getUserDefinedAuthConfig()) {
|
auto authConfig = getUserDefinedAuthConfig();
|
||||||
return findNetrcAuthenticator(hostname);
|
if(authConfig) {
|
||||||
|
return authConfig;
|
||||||
} else {
|
} else {
|
||||||
return getUserDefinedAuthConfig();
|
return findNetrcAuthenticator(hostname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig>
|
std::unique_ptr<AuthConfig>
|
||||||
NetrcAuthResolver::findNetrcAuthenticator(const std::string& hostname) const
|
NetrcAuthResolver::findNetrcAuthenticator(const std::string& hostname) const
|
||||||
{
|
{
|
||||||
if(!netrc_) {
|
if(!netrc_) {
|
||||||
return getDefaultAuthConfig();
|
return getDefaultAuthConfig();
|
||||||
} else {
|
} else {
|
||||||
std::shared_ptr<Authenticator> auth = netrc_->findAuthenticator(hostname);
|
auto auth = netrc_->findAuthenticator(hostname);
|
||||||
if(!auth) {
|
if(!auth) {
|
||||||
return getDefaultAuthConfig();
|
return getDefaultAuthConfig();
|
||||||
} else {
|
} else {
|
||||||
if(ignoreDefault_ && auth->getMachine().empty()) {
|
if(ignoreDefault_ && auth->getMachine().empty()) {
|
||||||
return getDefaultAuthConfig();
|
return getDefaultAuthConfig();
|
||||||
} else {
|
} else {
|
||||||
return std::shared_ptr<AuthConfig>
|
return make_unique<AuthConfig>(auth->getLogin(), auth->getPassword());
|
||||||
(new AuthConfig(auth->getLogin(), auth->getPassword()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetrcAuthResolver::setNetrc(const std::shared_ptr<Netrc>& netrc)
|
void NetrcAuthResolver::setNetrc(Netrc* netrc)
|
||||||
{
|
{
|
||||||
netrc_ = netrc;
|
netrc_ = netrc;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,24 +43,19 @@ class Netrc;
|
||||||
|
|
||||||
class NetrcAuthResolver : public AbstractAuthResolver {
|
class NetrcAuthResolver : public AbstractAuthResolver {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Netrc> netrc_;
|
Netrc* netrc_;
|
||||||
|
|
||||||
bool ignoreDefault_;
|
bool ignoreDefault_;
|
||||||
|
|
||||||
std::shared_ptr<AuthConfig> findNetrcAuthenticator
|
std::unique_ptr<AuthConfig> findNetrcAuthenticator
|
||||||
(const std::string& hostname) const;
|
(const std::string& hostname) const;
|
||||||
public:
|
public:
|
||||||
NetrcAuthResolver();
|
NetrcAuthResolver();
|
||||||
|
|
||||||
virtual std::shared_ptr<AuthConfig> resolveAuthConfig
|
virtual std::unique_ptr<AuthConfig> resolveAuthConfig
|
||||||
(const std::string& hostname);
|
(const std::string& hostname);
|
||||||
|
|
||||||
void setNetrc(const std::shared_ptr<Netrc>& netrc);
|
void setNetrc(Netrc* netrc);
|
||||||
|
|
||||||
const std::shared_ptr<Netrc>& getNetrc() const
|
|
||||||
{
|
|
||||||
return netrc_;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignores default token of netrc
|
// Ignores default token of netrc
|
||||||
void ignoreDefault();
|
void ignoreDefault();
|
||||||
|
|
|
@ -44,15 +44,15 @@ void AuthConfigFactoryTest::testCreateAuthConfig_http()
|
||||||
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
||||||
|
|
||||||
// with Netrc
|
// with Netrc
|
||||||
std::shared_ptr<Netrc> netrc(new Netrc());
|
auto netrc = make_unique<Netrc>();
|
||||||
netrc->addAuthenticator
|
netrc->addAuthenticator(make_unique<Authenticator>("localhost",
|
||||||
(std::shared_ptr<Authenticator>(new Authenticator("localhost",
|
|
||||||
"localhostuser",
|
"localhostuser",
|
||||||
"localhostpass",
|
"localhostpass",
|
||||||
"localhostacct")));
|
"localhostacct"));
|
||||||
netrc->addAuthenticator
|
netrc->addAuthenticator(make_unique<DefaultAuthenticator>("default",
|
||||||
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
|
"defaultpassword",
|
||||||
factory.setNetrc(netrc);
|
"defaultaccount"));
|
||||||
|
factory.setNetrc(std::move(netrc));
|
||||||
|
|
||||||
// not activated
|
// not activated
|
||||||
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
||||||
|
@ -100,15 +100,15 @@ void AuthConfigFactoryTest::testCreateAuthConfig_httpNoChallenge()
|
||||||
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
|
||||||
|
|
||||||
// with Netrc
|
// with Netrc
|
||||||
std::shared_ptr<Netrc> netrc(new Netrc());
|
auto netrc = make_unique<Netrc>();
|
||||||
netrc->addAuthenticator
|
netrc->addAuthenticator(make_unique<Authenticator>("localhost",
|
||||||
(std::shared_ptr<Authenticator>(new Authenticator("localhost",
|
|
||||||
"localhostuser",
|
"localhostuser",
|
||||||
"localhostpass",
|
"localhostpass",
|
||||||
"localhostacct")));
|
"localhostacct"));
|
||||||
netrc->addAuthenticator
|
netrc->addAuthenticator(make_unique<DefaultAuthenticator>("default",
|
||||||
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
|
"defaultpassword",
|
||||||
factory.setNetrc(netrc);
|
"defaultaccount"));
|
||||||
|
factory.setNetrc(std::move(netrc));
|
||||||
|
|
||||||
// not activated
|
// not activated
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
|
CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
|
||||||
|
@ -147,10 +147,11 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
|
||||||
factory.createAuthConfig(req, &option)->getAuthText());
|
factory.createAuthConfig(req, &option)->getAuthText());
|
||||||
|
|
||||||
// with Netrc
|
// with Netrc
|
||||||
std::shared_ptr<Netrc> netrc(new Netrc());
|
auto netrc = make_unique<Netrc>();
|
||||||
netrc->addAuthenticator
|
netrc->addAuthenticator(make_unique<DefaultAuthenticator>("default",
|
||||||
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
|
"defaultpassword",
|
||||||
factory.setNetrc(netrc);
|
"defaultaccount"));
|
||||||
|
factory.setNetrc(std::move(netrc));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpassword"),
|
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpassword"),
|
||||||
factory.createAuthConfig(req, &option)->getAuthText());
|
factory.createAuthConfig(req, &option)->getAuthText());
|
||||||
|
|
||||||
|
@ -179,12 +180,11 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
|
||||||
|
|
||||||
// Recreate netrc with entry for user aria2user
|
// Recreate netrc with entry for user aria2user
|
||||||
netrc.reset(new Netrc());
|
netrc.reset(new Netrc());
|
||||||
netrc->addAuthenticator
|
netrc->addAuthenticator(make_unique<Authenticator>("localhost",
|
||||||
(std::shared_ptr<Authenticator>(new Authenticator("localhost",
|
|
||||||
"aria2user",
|
"aria2user",
|
||||||
"netrcpass",
|
"netrcpass",
|
||||||
"netrcacct")));
|
"netrcacct"));
|
||||||
factory.setNetrc(netrc);
|
factory.setNetrc(std::move(netrc));
|
||||||
// This time, we can find same username "aria2user" in netrc, so the
|
// This time, we can find same username "aria2user" in netrc, so the
|
||||||
// password "netrcpass" is used, instead of "userDefinedPassword"
|
// password "netrcpass" is used, instead of "userDefinedPassword"
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:netrcpass"),
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:netrcpass"),
|
||||||
|
@ -196,17 +196,14 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
std::shared_ptr<AuthConfigFactory::BasicCred>
|
std::unique_ptr<BasicCred>
|
||||||
createBasicCred(const std::string& user,
|
createBasicCred(const std::string& user,
|
||||||
const std::string& password,
|
const std::string& password,
|
||||||
const std::string& host, uint16_t port,
|
const std::string& host, uint16_t port,
|
||||||
const std::string& path,
|
const std::string& path,
|
||||||
bool activated = false)
|
bool activated = false)
|
||||||
{
|
{
|
||||||
std::shared_ptr<AuthConfigFactory::BasicCred> bc
|
return make_unique<BasicCred>(user, password, host, port, path, activated);
|
||||||
(new AuthConfigFactory::BasicCred(user, password, host, port, path,
|
|
||||||
activated));
|
|
||||||
return bc;
|
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,7 @@ class DefaultAuthResolverTest : public CppUnit::TestFixture {
|
||||||
CPPUNIT_TEST(testResolveAuthConfig_with_userDefined);
|
CPPUNIT_TEST(testResolveAuthConfig_with_userDefined);
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
private:
|
private:
|
||||||
//NetrcHandle _netrc;
|
std::unique_ptr<DefaultAuthResolver> resolver_;
|
||||||
//std::shared_ptr<Option> _option;
|
|
||||||
std::shared_ptr<DefaultAuthResolver> resolver_;
|
|
||||||
public:
|
public:
|
||||||
void setUp()
|
void setUp()
|
||||||
{
|
{
|
||||||
|
@ -22,8 +20,7 @@ public:
|
||||||
//_option = new Option();
|
//_option = new Option();
|
||||||
resolver_.reset(new DefaultAuthResolver());
|
resolver_.reset(new DefaultAuthResolver());
|
||||||
//_factory->setOption(_option.get());
|
//_factory->setOption(_option.get());
|
||||||
resolver_->setDefaultAuthConfig
|
resolver_->setDefaultCred("foo", "bar");
|
||||||
(std::shared_ptr<AuthConfig>(new AuthConfig("foo", "bar")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void testResolveAuthConfig_without_userDefined();
|
void testResolveAuthConfig_without_userDefined();
|
||||||
|
@ -35,16 +32,16 @@ CPPUNIT_TEST_SUITE_REGISTRATION( DefaultAuthResolverTest );
|
||||||
|
|
||||||
void DefaultAuthResolverTest::testResolveAuthConfig_without_userDefined()
|
void DefaultAuthResolverTest::testResolveAuthConfig_without_userDefined()
|
||||||
{
|
{
|
||||||
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
|
auto authConfig = resolver_->resolveAuthConfig("localhost");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
|
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultAuthResolverTest::testResolveAuthConfig_with_userDefined()
|
void DefaultAuthResolverTest::testResolveAuthConfig_with_userDefined()
|
||||||
{
|
{
|
||||||
resolver_->setUserDefinedAuthConfig
|
resolver_->setUserDefinedCred("myname", "mypasswd");
|
||||||
(std::shared_ptr<AuthConfig>(new AuthConfig("myname", "mypasswd")));
|
auto authConfig = resolver_->resolveAuthConfig("localhost");
|
||||||
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
|
authConfig->getAuthText());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
#include "NetrcAuthResolver.h"
|
#include "NetrcAuthResolver.h"
|
||||||
|
|
||||||
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
#include "prefs.h"
|
#include "prefs.h"
|
||||||
#include "Netrc.h"
|
#include "Netrc.h"
|
||||||
#include "AuthConfig.h"
|
#include "AuthConfig.h"
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include "a2functional.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -14,23 +17,24 @@ class NetrcAuthResolverTest : public CppUnit::TestFixture {
|
||||||
CPPUNIT_TEST(testResolveAuthConfig_ignoreDefault);
|
CPPUNIT_TEST(testResolveAuthConfig_ignoreDefault);
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Netrc> netrc_;
|
std::unique_ptr<Netrc> netrc_;
|
||||||
//std::shared_ptr<Option> _option;
|
std::unique_ptr<NetrcAuthResolver> resolver_;
|
||||||
std::shared_ptr<NetrcAuthResolver> resolver_;
|
|
||||||
public:
|
public:
|
||||||
void setUp()
|
void setUp()
|
||||||
{
|
{
|
||||||
netrc_.reset(new Netrc());
|
netrc_.reset(new Netrc());
|
||||||
netrc_->addAuthenticator
|
netrc_->addAuthenticator(make_unique<Authenticator>("localhost",
|
||||||
(std::shared_ptr<Authenticator>(new Authenticator("localhost", "name", "passwd", "account")));
|
"name",
|
||||||
netrc_->addAuthenticator
|
"passwd",
|
||||||
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpasswd", "defaultaccount")));
|
"account"));
|
||||||
|
netrc_->addAuthenticator(make_unique<DefaultAuthenticator>
|
||||||
|
("default",
|
||||||
|
"defaultpasswd",
|
||||||
|
"defaultaccount"));
|
||||||
|
|
||||||
//_option = new Option();
|
|
||||||
resolver_.reset(new NetrcAuthResolver());
|
resolver_.reset(new NetrcAuthResolver());
|
||||||
resolver_->setNetrc(netrc_);
|
resolver_->setNetrc(netrc_.get());
|
||||||
resolver_->setDefaultAuthConfig
|
resolver_->setDefaultCred("foo", "bar");
|
||||||
(std::shared_ptr<AuthConfig>(new AuthConfig("foo", "bar")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void testResolveAuthConfig_without_userDefined();
|
void testResolveAuthConfig_without_userDefined();
|
||||||
|
@ -43,13 +47,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION( NetrcAuthResolverTest );
|
||||||
|
|
||||||
void NetrcAuthResolverTest::testResolveAuthConfig_without_userDefined()
|
void NetrcAuthResolverTest::testResolveAuthConfig_without_userDefined()
|
||||||
{
|
{
|
||||||
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
|
auto authConfig = resolver_->resolveAuthConfig("localhost");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name:passwd"), authConfig->getAuthText());
|
CPPUNIT_ASSERT_EQUAL(std::string("name:passwd"), authConfig->getAuthText());
|
||||||
|
|
||||||
authConfig = resolver_->resolveAuthConfig("mymachine");
|
authConfig = resolver_->resolveAuthConfig("mymachine");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"), authConfig->getAuthText());
|
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"),
|
||||||
|
authConfig->getAuthText());
|
||||||
|
|
||||||
resolver_->setNetrc(std::shared_ptr<Netrc>());
|
resolver_->setNetrc(nullptr);
|
||||||
authConfig = resolver_->resolveAuthConfig("localhost");
|
authConfig = resolver_->resolveAuthConfig("localhost");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
|
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
|
||||||
|
|
||||||
|
@ -57,28 +62,29 @@ void NetrcAuthResolverTest::testResolveAuthConfig_without_userDefined()
|
||||||
|
|
||||||
void NetrcAuthResolverTest::testResolveAuthConfig_with_userDefined()
|
void NetrcAuthResolverTest::testResolveAuthConfig_with_userDefined()
|
||||||
{
|
{
|
||||||
resolver_->setUserDefinedAuthConfig
|
resolver_->setUserDefinedCred("myname", "mypasswd");
|
||||||
(std::shared_ptr<AuthConfig>(new AuthConfig("myname", "mypasswd")));
|
auto authConfig = resolver_->resolveAuthConfig("localhost");
|
||||||
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
|
authConfig->getAuthText());
|
||||||
|
|
||||||
authConfig = resolver_->resolveAuthConfig("mymachine");
|
authConfig = resolver_->resolveAuthConfig("mymachine");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
|
||||||
|
authConfig->getAuthText());
|
||||||
|
|
||||||
resolver_->setNetrc(std::shared_ptr<Netrc>());
|
resolver_->setNetrc(nullptr);
|
||||||
authConfig = resolver_->resolveAuthConfig("mymachine");
|
authConfig = resolver_->resolveAuthConfig("mymachine");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
|
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
|
||||||
|
authConfig->getAuthText());
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetrcAuthResolverTest::testResolveAuthConfig_ignoreDefault()
|
void NetrcAuthResolverTest::testResolveAuthConfig_ignoreDefault()
|
||||||
{
|
{
|
||||||
resolver_->ignoreDefault();
|
resolver_->ignoreDefault();
|
||||||
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("mirror");
|
auto authConfig = resolver_->resolveAuthConfig("mirror");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
|
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
|
||||||
|
|
||||||
resolver_->useDefault();
|
resolver_->useDefault();
|
||||||
std::shared_ptr<AuthConfig> defAuthConfig =
|
auto defAuthConfig = resolver_->resolveAuthConfig("mirror");
|
||||||
resolver_->resolveAuthConfig("mirror");
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"),
|
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"),
|
||||||
defAuthConfig->getAuthText());
|
defAuthConfig->getAuthText());
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
|
#include "a2functional.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -35,34 +36,41 @@ CPPUNIT_TEST_SUITE_REGISTRATION( NetrcTest );
|
||||||
void NetrcTest::testFindAuthenticator()
|
void NetrcTest::testFindAuthenticator()
|
||||||
{
|
{
|
||||||
Netrc netrc;
|
Netrc netrc;
|
||||||
netrc.addAuthenticator
|
netrc.addAuthenticator(make_unique<Authenticator>("host1",
|
||||||
(std::shared_ptr<Authenticator>(new Authenticator("host1", "tujikawa", "tujikawapasswd", "tujikawaaccount")));
|
"tujikawa",
|
||||||
netrc.addAuthenticator
|
"tujikawapasswd",
|
||||||
(std::shared_ptr<Authenticator>(new Authenticator("host2", "aria2", "aria2password", "aria2account")));
|
"tujikawaaccount"));
|
||||||
netrc.addAuthenticator
|
netrc.addAuthenticator(make_unique<Authenticator>("host2",
|
||||||
(std::shared_ptr<Authenticator>(new Authenticator(".my.domain", "dmname", "dmpass", "dmaccount")));
|
"aria2",
|
||||||
netrc.addAuthenticator
|
"aria2password",
|
||||||
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
|
"aria2account"));
|
||||||
|
netrc.addAuthenticator(make_unique<Authenticator>(".my.domain",
|
||||||
|
"dmname",
|
||||||
|
"dmpass",
|
||||||
|
"dmaccount"));
|
||||||
|
netrc.addAuthenticator(make_unique<DefaultAuthenticator>("default",
|
||||||
|
"defaultpassword",
|
||||||
|
"defaultaccount"));
|
||||||
|
|
||||||
std::shared_ptr<Authenticator> aria2auth = netrc.findAuthenticator("host2");
|
auto aria2auth = netrc.findAuthenticator("host2");
|
||||||
CPPUNIT_ASSERT(aria2auth);
|
CPPUNIT_ASSERT(aria2auth);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
|
||||||
|
|
||||||
std::shared_ptr<Authenticator> defaultauth = netrc.findAuthenticator("host3");
|
auto defaultauth = netrc.findAuthenticator("host3");
|
||||||
CPPUNIT_ASSERT(defaultauth);
|
CPPUNIT_ASSERT(defaultauth);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("default"), defaultauth->getLogin());
|
CPPUNIT_ASSERT_EQUAL(std::string("default"), defaultauth->getLogin());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("defaultpassword"), defaultauth->getPassword());
|
CPPUNIT_ASSERT_EQUAL(std::string("defaultpassword"),
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("defaultaccount"), defaultauth->getAccount());
|
defaultauth->getPassword());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("defaultaccount"),
|
||||||
|
defaultauth->getAccount());
|
||||||
|
|
||||||
std::shared_ptr<Authenticator> domainMatchAuth =
|
auto domainMatchAuth = netrc.findAuthenticator("host3.my.domain");
|
||||||
netrc.findAuthenticator("host3.my.domain");
|
|
||||||
CPPUNIT_ASSERT(domainMatchAuth);
|
CPPUNIT_ASSERT(domainMatchAuth);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("dmname"), domainMatchAuth->getLogin());
|
CPPUNIT_ASSERT_EQUAL(std::string("dmname"), domainMatchAuth->getLogin());
|
||||||
|
|
||||||
std::shared_ptr<Authenticator> domainMatchAuth2 =
|
auto domainMatchAuth2 = netrc.findAuthenticator("my.domain");
|
||||||
netrc.findAuthenticator("my.domain");
|
|
||||||
CPPUNIT_ASSERT(domainMatchAuth2);
|
CPPUNIT_ASSERT(domainMatchAuth2);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("default"), domainMatchAuth2->getLogin());
|
CPPUNIT_ASSERT_EQUAL(std::string("default"), domainMatchAuth2->getLogin());
|
||||||
}
|
}
|
||||||
|
@ -71,24 +79,25 @@ void NetrcTest::testParse()
|
||||||
{
|
{
|
||||||
Netrc netrc;
|
Netrc netrc;
|
||||||
netrc.parse(A2_TEST_DIR"/sample.netrc");
|
netrc.parse(A2_TEST_DIR"/sample.netrc");
|
||||||
std::vector<std::shared_ptr<Authenticator> >::const_iterator itr =
|
auto itr = std::begin(netrc.getAuthenticators());
|
||||||
netrc.getAuthenticators().begin();
|
|
||||||
|
|
||||||
std::shared_ptr<Authenticator> tujikawaauth = *itr;
|
const auto& tujikawaauth = *itr;
|
||||||
CPPUNIT_ASSERT(tujikawaauth);
|
CPPUNIT_ASSERT(tujikawaauth);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("host1"), tujikawaauth->getMachine());
|
CPPUNIT_ASSERT_EQUAL(std::string("host1"), tujikawaauth->getMachine());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("tujikawa"), tujikawaauth->getLogin());
|
CPPUNIT_ASSERT_EQUAL(std::string("tujikawa"), tujikawaauth->getLogin());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("tujikawapassword"), tujikawaauth->getPassword());
|
CPPUNIT_ASSERT_EQUAL(std::string("tujikawapassword"),
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("tujikawaaccount"), tujikawaauth->getAccount());
|
tujikawaauth->getPassword());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("tujikawaaccount"),
|
||||||
|
tujikawaauth->getAccount());
|
||||||
++itr;
|
++itr;
|
||||||
std::shared_ptr<Authenticator> aria2auth = *itr;
|
const auto& aria2auth = *itr;
|
||||||
CPPUNIT_ASSERT(aria2auth);
|
CPPUNIT_ASSERT(aria2auth);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("host2"), aria2auth->getMachine());
|
CPPUNIT_ASSERT_EQUAL(std::string("host2"), aria2auth->getMachine());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
|
||||||
++itr;
|
++itr;
|
||||||
std::shared_ptr<Authenticator> defaultauth = *itr;
|
const auto& defaultauth = *itr;
|
||||||
CPPUNIT_ASSERT(defaultauth);
|
CPPUNIT_ASSERT(defaultauth);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("anonymous"), defaultauth->getLogin());
|
CPPUNIT_ASSERT_EQUAL(std::string("anonymous"), defaultauth->getLogin());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("ARIA2@USER"), defaultauth->getPassword());
|
CPPUNIT_ASSERT_EQUAL(std::string("ARIA2@USER"), defaultauth->getPassword());
|
||||||
|
|
Loading…
Reference in New Issue