aria2/test/NetrcTest.cc

96 lines
3.1 KiB
C++
Raw Normal View History

2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To handle Segment as SegmentHandle: * src/AbstractCommand.cc (execute): Rewritten. * src/SegmentMan.h: Segment -> SegmentHandle Introducded HttpResponse class, HttpRequest class to improve code extensiveness and make it clear: * src/HttpDownloadCommand.cc: transfer encoders are now managed by HttpResponse class. * src/HttpRequest.h, src/HttpRequest.cc: New class. * src/HttpResponse.h, src/HttpResponse.cc: New class. * src/HttpConnection.cc: Contruction of http request were moved to HttpRequest class. * src/HttpResponseCommand.h, src/HttpResponseCommand.cc: Refactored. * src/HttpRequestCommand.cc (executeInternal): Rewritten. * src/HttpAuthConfig.h: New class. * src/Range.h: New class. To make FtpTunnel{Request, Response}Command and HttpProxy{Request, Response}Command derived from AbstractProxy{Request, Response}Command: * src/FtpTunnelResponseCommand.h, src/FtpTunnelResponseCommand.cc: Derived from AbstractProxyRequestCommand class. * src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc: Derived from AbstractProxyResponseCommand class. * src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc: Derived from AbstractProxyRequestCommand class. * src/HttpProxyResponseCommand.h, src/HttpProxyResponseCommand.cc: Derived from AbstractProxyResponseCommand class. * src/AbstractProxyRequestCommand.h, src/AbstractProxyRequestCommand.cc : New class. * src/AbstractProxyResponseCommand.h, src/AbstractProxyResponseCommand.cc: New class. To add netrc support: * src/Netrc.h, src/Netrc.cc: New class. * src/Util.h, src/Util.cc (split): New function. * src/HttpHeader.cc (getRange): Fixed so that it inspects "Content-Range" header instead of "Range" header. * src/HttpHeader.h (getStatus): Removed. (setStatus): Removed. * src/Segment.h (getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
#include "Netrc.h"
#include "Exception.h"
#include <cppunit/extensions/HelperMacros.h>
using namespace std;
class NetrcTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(NetrcTest);
CPPUNIT_TEST(testFindAuthenticatable);
CPPUNIT_TEST(testParse);
CPPUNIT_TEST(testParse_fileNotFound);
CPPUNIT_TEST(testParse_emptyfile);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {
}
void testFindAuthenticatable();
void testParse();
void testParse_fileNotFound();
void testParse_emptyfile();
};
CPPUNIT_TEST_SUITE_REGISTRATION( NetrcTest );
void NetrcTest::testFindAuthenticatable()
{
Netrc netrc;
netrc.addAuthenticatable(new Authenticator("host1", "tujikawa", "tujikawapasswd", "tujikawaaccount"));
netrc.addAuthenticatable(new Authenticator("host2", "aria2", "aria2password", "aria2account"));
netrc.addAuthenticatable(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"));
AuthenticatorHandle aria2auth = netrc.findAuthenticatable("host2");
CPPUNIT_ASSERT(!aria2auth.isNull());
CPPUNIT_ASSERT_EQUAL(string("aria2"), aria2auth->getLogin());
CPPUNIT_ASSERT_EQUAL(string("aria2password"), aria2auth->getPassword());
CPPUNIT_ASSERT_EQUAL(string("aria2account"), aria2auth->getAccount());
AuthenticatorHandle defaultauth = netrc.findAuthenticatable("host3");
CPPUNIT_ASSERT(!defaultauth.isNull());
CPPUNIT_ASSERT_EQUAL(string("default"), defaultauth->getLogin());
CPPUNIT_ASSERT_EQUAL(string("defaultpassword"), defaultauth->getPassword());
CPPUNIT_ASSERT_EQUAL(string("defaultaccount"), defaultauth->getAccount());
}
void NetrcTest::testParse()
{
Netrc netrc;
netrc.parse("sample.netrc");
Authenticatables::const_iterator itr = netrc.getAuthenticatables().begin();
AuthenticatorHandle tujikawaauth = *itr;
CPPUNIT_ASSERT(!tujikawaauth.isNull());
CPPUNIT_ASSERT_EQUAL(string("host1"), tujikawaauth->getMachine());
CPPUNIT_ASSERT_EQUAL(string("tujikawa"), tujikawaauth->getLogin());
CPPUNIT_ASSERT_EQUAL(string("tujikawapassword"), tujikawaauth->getPassword());
CPPUNIT_ASSERT_EQUAL(string("tujikawaaccount"), tujikawaauth->getAccount());
++itr;
AuthenticatorHandle aria2auth = *itr;
CPPUNIT_ASSERT(!aria2auth.isNull());
CPPUNIT_ASSERT_EQUAL(string("host2"), aria2auth->getMachine());
CPPUNIT_ASSERT_EQUAL(string("aria2"), aria2auth->getLogin());
CPPUNIT_ASSERT_EQUAL(string("aria2password"), aria2auth->getPassword());
CPPUNIT_ASSERT_EQUAL(string("aria2account"), aria2auth->getAccount());
++itr;
DefaultAuthenticatorHandle defaultauth = *itr;
CPPUNIT_ASSERT(!defaultauth.isNull());
CPPUNIT_ASSERT_EQUAL(string("anonymous"), defaultauth->getLogin());
CPPUNIT_ASSERT_EQUAL(string("ARIA2@USER"), defaultauth->getPassword());
CPPUNIT_ASSERT_EQUAL(string("ARIA2@ACCT"), defaultauth->getAccount());
}
void NetrcTest::testParse_fileNotFound()
{
Netrc netrc;
try {
netrc.parse("");
CPPUNIT_FAIL("exception must be threw.");
} catch(Exception* e) {
cerr << e->getMsg() << endl;
delete e;
}
}
void NetrcTest::testParse_emptyfile()
{
Netrc netrc;
netrc.parse("emptyfile");
CPPUNIT_ASSERT_EQUAL((size_t)0, netrc.getAuthenticatables().size());
}