File: FtpConnectionTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (349 lines) | stat: -rw-r--r-- 10,263 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "FtpConnection.h"

#include <iostream>
#include <cstring>

#include <cppunit/extensions/HelperMacros.h>

#include "Exception.h"
#include "util.h"
#include "SocketCore.h"
#include "Request.h"
#include "Option.h"
#include "DlRetryEx.h"
#include "DlAbortEx.h"
#include "AuthConfigFactory.h"
#include "AuthConfig.h"

namespace aria2 {

class FtpConnectionTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(FtpConnectionTest);
  CPPUNIT_TEST(testReceiveResponse);
  CPPUNIT_TEST(testReceiveResponse_overflow);
  CPPUNIT_TEST(testSendMdtm);
  CPPUNIT_TEST(testReceiveMdtmResponse);
  CPPUNIT_TEST(testSendPwd);
  CPPUNIT_TEST(testReceivePwdResponse);
  CPPUNIT_TEST(testReceivePwdResponse_unquotedResponse);
  CPPUNIT_TEST(testReceivePwdResponse_badStatus);
  CPPUNIT_TEST(testSendCwd);
  CPPUNIT_TEST(testSendSize);
  CPPUNIT_TEST(testReceiveSizeResponse);
  CPPUNIT_TEST(testSendRetr);
  CPPUNIT_TEST(testReceiveEpsvResponse);
  CPPUNIT_TEST_SUITE_END();

private:
  std::shared_ptr<SocketCore> serverSocket_;
  uint16_t listenPort_;
  std::shared_ptr<SocketCore> clientSocket_;
  std::shared_ptr<FtpConnection> ftp_;
  std::shared_ptr<Option> option_;
  std::shared_ptr<AuthConfigFactory> authConfigFactory_;
  std::shared_ptr<Request> req_;

public:
  void setUp()
  {
    option_.reset(new Option());
    authConfigFactory_.reset(new AuthConfigFactory());

    //_ftpServerSocket.reset(new SocketCore());
    std::shared_ptr<SocketCore> listenSocket(new SocketCore());
    listenSocket->bind(0);
    listenSocket->beginListen();
    listenSocket->setBlockingMode();
    listenPort_ = listenSocket->getAddrInfo().port;

    req_.reset(new Request());
    req_->setUri("ftp://localhost/dir%20sp/hello%20world.img");

    clientSocket_.reset(new SocketCore());
    clientSocket_->establishConnection("localhost", listenPort_);

    while (!clientSocket_->isWritable(0))
      ;

    serverSocket_ = listenSocket->acceptConnection();
    serverSocket_->setBlockingMode();
    ftp_.reset(new FtpConnection(
        1, clientSocket_, req_,
        authConfigFactory_->createAuthConfig(req_, option_.get()),
        option_.get()));
  }

  void tearDown() {}

  void testSendMdtm();
  void testReceiveMdtmResponse();
  void testReceiveResponse();
  void testReceiveResponse_overflow();
  void testSendPwd();
  void testReceivePwdResponse();
  void testReceivePwdResponse_unquotedResponse();
  void testReceivePwdResponse_badStatus();
  void testSendCwd();
  void testSendSize();
  void testReceiveSizeResponse();
  void testSendRetr();
  void testReceiveEpsvResponse();
};

CPPUNIT_TEST_SUITE_REGISTRATION(FtpConnectionTest);

namespace {
void waitRead(const std::shared_ptr<SocketCore>& socket)
{
  while (!socket->isReadable(0))
    ;
}
} // namespace

void FtpConnectionTest::testReceiveResponse()
{
  serverSocket_->writeData("100");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(0, ftp_->receiveResponse());
  serverSocket_->writeData(" single line response");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(0, ftp_->receiveResponse());
  serverSocket_->writeData("\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(100, ftp_->receiveResponse());
  // 2 responses in the buffer
  serverSocket_->writeData("101 single1\r\n"
                           "102 single2\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(101, ftp_->receiveResponse());
  CPPUNIT_ASSERT_EQUAL(102, ftp_->receiveResponse());

  serverSocket_->writeData("103-multi line response\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(0, ftp_->receiveResponse());
  serverSocket_->writeData("103-line2\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(0, ftp_->receiveResponse());
  serverSocket_->writeData("103");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(0, ftp_->receiveResponse());
  serverSocket_->writeData(" ");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(0, ftp_->receiveResponse());
  serverSocket_->writeData("last\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(103, ftp_->receiveResponse());

  serverSocket_->writeData("104-multi\r\n"
                           "104 \r\n"
                           "105-multi\r\n"
                           "105 \r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(104, ftp_->receiveResponse());
  CPPUNIT_ASSERT_EQUAL(105, ftp_->receiveResponse());
}

void FtpConnectionTest::testSendMdtm()
{
  ftp_->sendMdtm();
  char data[32];
  size_t len = sizeof(data);
  serverSocket_->readData(data, len);
  data[len] = '\0';
  CPPUNIT_ASSERT_EQUAL(std::string("MDTM hello world.img\r\n"),
                       std::string(data));
}

void FtpConnectionTest::testReceiveMdtmResponse()
{
  {
    Time t;
    serverSocket_->writeData("213 20080908124312");
    waitRead(clientSocket_);
    CPPUNIT_ASSERT_EQUAL(0, ftp_->receiveMdtmResponse(t));
    serverSocket_->writeData("\r\n");
    waitRead(clientSocket_);
    CPPUNIT_ASSERT_EQUAL(213, ftp_->receiveMdtmResponse(t));
    CPPUNIT_ASSERT_EQUAL((time_t)1220877792, t.getTimeFromEpoch());
  }
  {
    // see milli second part is ignored
    Time t;
    serverSocket_->writeData("213 20080908124312.014\r\n");
    waitRead(clientSocket_);
    CPPUNIT_ASSERT_EQUAL(213, ftp_->receiveMdtmResponse(t));
    CPPUNIT_ASSERT_EQUAL((time_t)1220877792, t.getTimeFromEpoch());
  }
  {
    // hhmmss part is missing
    Time t;
    serverSocket_->writeData("213 20080908\r\n");
    waitRead(clientSocket_);
    CPPUNIT_ASSERT_EQUAL(213, ftp_->receiveMdtmResponse(t));
    CPPUNIT_ASSERT(t.bad());
  }
  {
    // invalid month: 19
    Time t;
    serverSocket_->writeData("213 20081908124312\r\n");
    waitRead(clientSocket_);
    CPPUNIT_ASSERT_EQUAL(213, ftp_->receiveMdtmResponse(t));
#ifdef HAVE_TIMEGM
    // Time will be normalized. Wed Jul 8 12:43:12 2009
    CPPUNIT_ASSERT_EQUAL((time_t)1247056992, t.getTimeFromEpoch());
#else  // !HAVE_TIMEGM
    // The replacement timegm does not normalize.
    CPPUNIT_ASSERT_EQUAL((time_t)-1, t.getTimeFromEpoch());
#endif // !HAVE_TIMEGM
  }
  {
    Time t;
    serverSocket_->writeData("550 File Not Found\r\n");
    waitRead(clientSocket_);
    CPPUNIT_ASSERT_EQUAL(550, ftp_->receiveMdtmResponse(t));
  }
}

void FtpConnectionTest::testReceiveResponse_overflow()
{
  char data[1_k];
  memset(data, 0, sizeof(data));
  memcpy(data, "213 ", 4);
  for (int i = 0; i < 64; ++i) {
    serverSocket_->writeData(data, sizeof(data));
    waitRead(clientSocket_);
    CPPUNIT_ASSERT_EQUAL(0, ftp_->receiveResponse());
  }
  serverSocket_->writeData(data, sizeof(data));
  waitRead(clientSocket_);
  try {
    ftp_->receiveResponse();
    CPPUNIT_FAIL("exception must be thrown.");
  }
  catch (DlRetryEx& e) {
    // success
  }
}

void FtpConnectionTest::testSendPwd()
{
  ftp_->sendPwd();
  char data[32];
  size_t len = sizeof(data);
  serverSocket_->readData(data, len);
  CPPUNIT_ASSERT_EQUAL((size_t)5, len);
  data[len] = '\0';
  CPPUNIT_ASSERT_EQUAL(std::string("PWD\r\n"), std::string(data));
}

void FtpConnectionTest::testReceivePwdResponse()
{
  std::string pwd;
  serverSocket_->writeData("257 ");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(0, ftp_->receivePwdResponse(pwd));
  CPPUNIT_ASSERT(pwd.empty());
  serverSocket_->writeData("\"/dir/to\" is your directory.\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(257, ftp_->receivePwdResponse(pwd));
  CPPUNIT_ASSERT_EQUAL(std::string("/dir/to"), pwd);
}

void FtpConnectionTest::testReceivePwdResponse_unquotedResponse()
{
  std::string pwd;
  serverSocket_->writeData("257 /dir/to\r\n");
  waitRead(clientSocket_);
  try {
    ftp_->receivePwdResponse(pwd);
    CPPUNIT_FAIL("exception must be thrown.");
  }
  catch (DlAbortEx& e) {
    // success
  }
}

void FtpConnectionTest::testReceivePwdResponse_badStatus()
{
  std::string pwd;
  serverSocket_->writeData("500 failed\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(500, ftp_->receivePwdResponse(pwd));
  CPPUNIT_ASSERT(pwd.empty());
}

void FtpConnectionTest::testSendCwd()
{
  ftp_->sendCwd("%2Fdir%20sp");
  char data[32];
  size_t len = sizeof(data);
  serverSocket_->readData(data, len);
  data[len] = '\0';
  CPPUNIT_ASSERT_EQUAL(std::string("CWD /dir sp\r\n"), std::string(data));
}

void FtpConnectionTest::testSendSize()
{
  ftp_->sendSize();
  char data[32];
  size_t len = sizeof(data);
  serverSocket_->readData(data, len);
  CPPUNIT_ASSERT_EQUAL(std::string("SIZE hello world.img\r\n"),
                       std::string(&data[0], &data[len]));
}

void FtpConnectionTest::testReceiveSizeResponse()
{
  serverSocket_->writeData("213 4294967296\r\n");
  waitRead(clientSocket_);
  int64_t size;
  CPPUNIT_ASSERT_EQUAL(213, ftp_->receiveSizeResponse(size));
  CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, size);
}

void FtpConnectionTest::testSendRetr()
{
  ftp_->sendRetr();
  char data[32];
  size_t len = sizeof(data);
  serverSocket_->readData(data, len);
  CPPUNIT_ASSERT_EQUAL(std::string("RETR hello world.img\r\n"),
                       std::string(&data[0], &data[len]));
}

void FtpConnectionTest::testReceiveEpsvResponse()
{
  serverSocket_->writeData("229 Success (|||12000|)\r\n");
  waitRead(clientSocket_);
  uint16_t port = 0;
  CPPUNIT_ASSERT_EQUAL(229, ftp_->receiveEpsvResponse(port));
  CPPUNIT_ASSERT_EQUAL((uint16_t)12000, port);

  serverSocket_->writeData("229 Success |||12000|)\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(229, ftp_->receiveEpsvResponse(port));
  CPPUNIT_ASSERT_EQUAL((uint16_t)0, port);

  serverSocket_->writeData("229 Success (|||12000|\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(229, ftp_->receiveEpsvResponse(port));
  CPPUNIT_ASSERT_EQUAL((uint16_t)0, port);

  serverSocket_->writeData("229 Success ()|||12000|\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(229, ftp_->receiveEpsvResponse(port));
  CPPUNIT_ASSERT_EQUAL((uint16_t)0, port);

  serverSocket_->writeData("229 Success )(|||12000|)\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(229, ftp_->receiveEpsvResponse(port));
  CPPUNIT_ASSERT_EQUAL((uint16_t)0, port);

  serverSocket_->writeData("229 Success )(||12000|)\r\n");
  waitRead(clientSocket_);
  CPPUNIT_ASSERT_EQUAL(229, ftp_->receiveEpsvResponse(port));
  CPPUNIT_ASSERT_EQUAL((uint16_t)0, port);
}

} // namespace aria2