void FTPClientSessionTest::testDownloadPORT() { DialogServer server; server.addResponse("220 localhost FTP ready"); server.addResponse("331 Password required"); server.addResponse("230 Welcome"); server.addResponse("200 Type set to I"); FTPClientSession session("localhost", server.port()); session.setPassive(false); session.login("user", "password"); server.clearCommands(); server.addResponse("500 EPRT not understood"); server.addResponse("200 PORT OK"); server.addResponse("150 Sending data\r\n226 Transfer complete"); ActiveDownloader dl(session); ActiveResult<std::string> result = dl.download("test.txt"); std::string cmd = server.popCommandWait(); assert (cmd.substr(0, 4) == "EPRT"); cmd = server.popCommandWait(); assert (cmd.substr(0, 4) == "PORT"); std::string dummy; int x, lo, hi; for (std::string::iterator it = cmd.begin(); it != cmd.end(); ++it) { if (*it == ',') *it = ' '; } std::istringstream istr(cmd); istr >> dummy >> x >> x >> x >> x >> hi >> lo; int port = hi*256 + lo; cmd = server.popCommandWait(); assert (cmd == "RETR test.txt"); SocketAddress sa("localhost", (Poco::UInt16) port); DialogSocket dataSock; dataSock.connect(sa); std::string data("This is some data"); dataSock.sendString(data); dataSock.close(); result.wait(); std::string received = result.data(); assert (received == data); server.addResponse("221 Good Bye"); session.close(); }
void FTPClientSessionTest::testDownloadEPRT() { DialogServer server; server.addResponse("220 localhost FTP ready"); server.addResponse("331 Password required"); server.addResponse("230 Welcome"); server.addResponse("200 Type set to I"); FTPClientSession session("localhost", server.port()); session.setPassive(false); session.login("user", "password"); server.clearCommands(); server.addResponse("200 EPRT OK"); server.addResponse("150 Sending data\r\n226 Transfer complete"); ActiveDownloader dl(session); ActiveResult<std::string> result = dl.download("test.txt"); std::string cmd = server.popCommandWait(); assert (cmd.substr(0, 4) == "EPRT"); std::string dummy; char c; int d; int port; std::istringstream istr(cmd); istr >> dummy >> c >> d >> c >> d >> c >> d >> c >> d >> c >> d >> c >> port >> c; cmd = server.popCommandWait(); assert (cmd == "RETR test.txt"); SocketAddress sa("localhost", (Poco::UInt16) port); DialogSocket dataSock; dataSock.connect(sa); std::string data("This is some data"); dataSock.sendString(data); dataSock.close(); result.wait(); std::string received = result.data(); assert (received == data); server.addResponse("221 Good Bye"); session.close(); }
void DialogServer::run() { _ready.set(); Poco::Timespan span(250000); while (!_stop) { if (_socket.poll(span, Socket::SELECT_READ)) { DialogSocket ds = _socket.acceptConnection(); { FastMutex::ScopedLock lock(_mutex); if (!_nextResponses.empty()) { ds.sendMessage(_nextResponses.front()); _nextResponses.erase(_nextResponses.begin()); } } if (_acceptCommands) { try { std::string command; while (ds.receiveMessage(command)) { if (_log) std::cout << ">> " << command << std::endl; { FastMutex::ScopedLock lock(_mutex); _lastCommands.push_back(command); if (!_nextResponses.empty()) { if (_log) std::cout << "<< " << _nextResponses.front() << std::endl; ds.sendMessage(_nextResponses.front()); _nextResponses.erase(_nextResponses.begin()); } } } } catch (Poco::Exception& exc) { std::cerr << "DialogServer: " << exc.displayText() << std::endl; } } } } }
void DialogSocketTest::testDialogSocket() { EchoServer echoServer; DialogSocket ds; ds.connect(SocketAddress("localhost", echoServer.port())); ds.sendMessage("Hello, world!"); std::string str; ds.receiveMessage(str); assert (str == "Hello, world!"); ds.sendString("Hello, World!\n"); ds.receiveMessage(str); assert (str == "Hello, World!"); ds.sendMessage("EHLO", "appinf.com"); ds.receiveMessage(str); assert (str == "EHLO appinf.com"); ds.sendMessage("PUT", "local.txt", "remote.txt"); ds.receiveMessage(str); assert (str == "PUT local.txt remote.txt"); ds.sendMessage("220 Hello, world!"); int status = ds.receiveStatusMessage(str); assert (status == 220); assert (str == "220 Hello, world!"); ds.sendString("220-line1\r\n220 line2\r\n"); status = ds.receiveStatusMessage(str); assert (status == 220); assert (str == "220-line1\n220 line2"); ds.sendString("220-line1\r\nline2\r\n220 line3\r\n"); status = ds.receiveStatusMessage(str); assert (status == 220); assert (str == "220-line1\nline2\n220 line3"); ds.sendMessage("Hello, world!"); status = ds.receiveStatusMessage(str); assert (status == 0); assert (str == "Hello, world!"); }