diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-09-08 13:06:44 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-09-08 13:06:44 +0000 |
| commit | eb652b570e765bcf12120fd831bf42b3e7f19652 (patch) | |
| tree | 22922bda5cae6325d69f003f33f620efc8f0b6e4 /test/FtpConnectionTest.cc | |
| parent | dbc8f5b737607db38399385e3b94f8dec9b3576c (diff) | |
2008-09-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Implemented the ability to get timestamp from remote FTP server
using
MDTM command described in RFC3659.
* src/FtpConnection.cc
* src/FtpConnection.h
* src/FtpNegotiationCommand.cc
* src/FtpNegotiationCommand.h
* test/FtpConnectionTest.cc
* test/Makefile.am
Diffstat (limited to 'test/FtpConnectionTest.cc')
| -rw-r--r-- | test/FtpConnectionTest.cc | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/test/FtpConnectionTest.cc b/test/FtpConnectionTest.cc new file mode 100644 index 00000000..7095819c --- /dev/null +++ b/test/FtpConnectionTest.cc @@ -0,0 +1,105 @@ +#include "FtpConnection.h" +#include "Exception.h" +#include "Util.h" +#include "SocketCore.h" +#include "Request.h" +#include "Option.h" +#include <iostream> +#include <cppunit/extensions/HelperMacros.h> + +namespace aria2 { + +class FtpConnectionTest:public CppUnit::TestFixture { + + CPPUNIT_TEST_SUITE(FtpConnectionTest); + CPPUNIT_TEST(testSendMdtm); + CPPUNIT_TEST(testReceiveMdtmResponse); + CPPUNIT_TEST_SUITE_END(); +private: + SharedHandle<SocketCore> _serverSocket; + uint16_t _listenPort; + SharedHandle<FtpConnection> _ftp; + Option _option; +public: + void setUp() + { + //_ftpServerSocket.reset(new SocketCore()); + SharedHandle<SocketCore> listenSocket(new SocketCore()); + listenSocket->bind(0); + listenSocket->beginListen(); + std::pair<std::string, uint16_t> addrinfo; + listenSocket->getAddrInfo(addrinfo); + _listenPort = addrinfo.second; + + SharedHandle<Request> req(new Request()); + req->setUrl("ftp://localhost/dir/file.img"); + + SharedHandle<SocketCore> clientSocket(new SocketCore()); + clientSocket->establishConnection("127.0.0.1", _listenPort); + + while(!clientSocket->isWritable(0)); + clientSocket->setBlockingMode(); + + _serverSocket.reset(listenSocket->acceptConnection()); + _ftp.reset(new FtpConnection(1, clientSocket, req, &_option)); + } + + void tearDown() {} + + void testSendMdtm(); + void testReceiveMdtmResponse(); +}; + + +CPPUNIT_TEST_SUITE_REGISTRATION(FtpConnectionTest); + +void FtpConnectionTest::testSendMdtm() +{ + _ftp->sendMdtm(); + char data[32]; + size_t len = sizeof(data); + _serverSocket->readData(data, len); + CPPUNIT_ASSERT_EQUAL((size_t)15, len); + data[len] = '\0'; + CPPUNIT_ASSERT_EQUAL(std::string("MDTM file.img\r\n"), std::string(data)); +} + +void FtpConnectionTest::testReceiveMdtmResponse() +{ + { + Time t; + _serverSocket->writeData("213 20080908124312"); + CPPUNIT_ASSERT_EQUAL((unsigned int)0, _ftp->receiveMdtmResponse(t)); + _serverSocket->writeData("\r\n"); + CPPUNIT_ASSERT_EQUAL((unsigned int)213, _ftp->receiveMdtmResponse(t)); + CPPUNIT_ASSERT_EQUAL((time_t)1220877792, t.getTime()); + } + { + // see milli second part is ignored + Time t; + _serverSocket->writeData("213 20080908124312.014\r\n"); + CPPUNIT_ASSERT_EQUAL((unsigned int)213, _ftp->receiveMdtmResponse(t)); + CPPUNIT_ASSERT_EQUAL((time_t)1220877792, t.getTime()); + } + { + // hhmmss part is missing + Time t; + _serverSocket->writeData("213 20080908\r\n"); + CPPUNIT_ASSERT_EQUAL((unsigned int)213, _ftp->receiveMdtmResponse(t)); + CPPUNIT_ASSERT_EQUAL((time_t)-1, t.getTime()); + } + { + // invalid month: 19 + Time t; + _serverSocket->writeData("213 20081908124312\r\n"); + CPPUNIT_ASSERT_EQUAL((unsigned int)213, _ftp->receiveMdtmResponse(t)); + CPPUNIT_ASSERT_EQUAL((time_t)-1, t.getTime()); + } + { + Time t; + _serverSocket->writeData("550 File Not Found\r\n"); + CPPUNIT_ASSERT_EQUAL((unsigned int)550, _ftp->receiveMdtmResponse(t)); + } +} + +} // namespace aria2 |
