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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/ftp/ftp_network_transaction.h"
#include "base/ref_counted.h"
#include "net/base/host_resolver.h"
#include "net/base/host_resolver_unittest.h"
#include "net/base/io_buffer.h"
#include "net/base/test_completion_callback.h"
#include "net/ftp/ftp_network_session.h"
#include "net/ftp/ftp_request_info.h"
#include "net/socket/socket_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace {
// Size we use for IOBuffers used to receive data from the test data socket.
const int kBufferSize = 128;
} // namespace
namespace net {
class FtpMockControlSocket : public DynamicMockSocket {
public:
enum State {
NONE,
PRE_USER,
PRE_PASSWD,
PRE_SYST,
PRE_PWD,
PRE_TYPE,
PRE_PASV,
PRE_SIZE,
PRE_MDTM,
PRE_LIST,
PRE_RETR,
PRE_PASV2,
PRE_CWD,
PRE_QUIT,
QUIT
};
FtpMockControlSocket() : failure_injection_state_(NONE) {
Init();
}
virtual MockWriteResult OnWrite(const std::string& data) {
if (InjectFault())
return MockWriteResult(true, OK);
switch (state()) {
case PRE_USER:
return Verify("USER anonymous\r\n", data, PRE_PASSWD,
"331 Password needed\r\n");
case PRE_PASSWD:
return Verify("PASS chrome@example.com\r\n", data, PRE_SYST,
"230 Welcome\r\n");
case PRE_SYST:
return Verify("SYST\r\n", data, PRE_PWD, "215 UNIX\r\n");
case PRE_PWD:
return Verify("PWD\r\n", data, PRE_TYPE,
"257 \"/\" is your current location\r\n");
case PRE_TYPE:
return Verify("TYPE I\r\n", data, PRE_PASV,
"200 TYPE is now 8-bit binary\r\n");
case PRE_PASV:
return Verify("PASV\r\n", data, PRE_SIZE,
"227 Entering Passive Mode (127,0,0,1,123,456)\r\n");
case PRE_QUIT:
return Verify("QUIT\r\n", data, QUIT, "221 Goodbye.\r\n");
default:
return MockWriteResult(true, ERR_UNEXPECTED);
}
}
void InjectFailure(State state, State next_state, const char* response) {
DCHECK_EQ(NONE, failure_injection_state_);
DCHECK_NE(NONE, state);
DCHECK_NE(NONE, next_state);
DCHECK_NE(state, next_state);
failure_injection_state_ = state;
failure_injection_next_state_ = next_state;
fault_response_ = response;
}
State state() const {
return state_;
}
virtual void Reset() {
DynamicMockSocket::Reset();
Init();
}
protected:
void Init() {
state_ = PRE_USER;
SimulateRead("220 host TestFTPd\r\n");
}
// If protocol fault injection has been requested, adjusts state and mocked
// read and returns true.
bool InjectFault() {
if (state_ != failure_injection_state_)
return false;
SimulateRead(fault_response_);
state_ = failure_injection_next_state_;
return true;
}
MockWriteResult Verify(const std::string& expected,
const std::string& data,
State next_state,
const char* next_read) {
EXPECT_EQ(expected, data);
if (expected == data) {
state_ = next_state;
SimulateRead(next_read);
return MockWriteResult(true, OK);
}
return MockWriteResult(true, ERR_UNEXPECTED);
}
private:
State state_;
State failure_injection_state_;
State failure_injection_next_state_;
const char* fault_response_;
DISALLOW_COPY_AND_ASSIGN(FtpMockControlSocket);
};
class FtpMockControlSocketDirectoryListing : public FtpMockControlSocket {
public:
FtpMockControlSocketDirectoryListing() {
}
virtual MockWriteResult OnWrite(const std::string& data) {
if (InjectFault())
return MockWriteResult(true, OK);
switch (state()) {
case PRE_SIZE:
return Verify("SIZE /\r\n", data, PRE_MDTM,
"550 I can only retrieve regular files\r\n");
case PRE_MDTM:
return Verify("MDTM /\r\n", data, PRE_RETR,
"213 20070221112533\r\n");
case PRE_RETR:
return Verify("RETR /\r\n", data, PRE_PASV2,
"550 Can't download directory\r\n");
case PRE_PASV2:
// Parser should also accept format without parentheses.
return Verify("PASV\r\n", data, PRE_CWD,
"227 Entering Passive Mode 127,0,0,1,123,456\r\n");
case PRE_CWD:
return Verify("CWD /\r\n", data, PRE_LIST, "200 OK\r\n");
case PRE_LIST:
// TODO(phajdan.jr): Also test with "150 Accepted Data Connection".
return Verify("LIST\r\n", data, PRE_QUIT, "200 OK\r\n");
default:
return FtpMockControlSocket::OnWrite(data);
}
}
private:
DISALLOW_COPY_AND_ASSIGN(FtpMockControlSocketDirectoryListing);
};
class FtpMockControlSocketFileDownload : public FtpMockControlSocket {
public:
FtpMockControlSocketFileDownload() {
}
virtual MockWriteResult OnWrite(const std::string& data) {
if (InjectFault())
return MockWriteResult(true, OK);
switch (state()) {
case PRE_SIZE:
return Verify("SIZE /file\r\n", data, PRE_MDTM,
"213 18\r\n");
case PRE_MDTM:
return Verify("MDTM /file\r\n", data, PRE_RETR,
"213 20070221112533\r\n");
case PRE_RETR:
// TODO(phajdan.jr): Also test with "150 Accepted Data Connection".
return Verify("RETR /file\r\n", data, PRE_QUIT, "200 OK\r\n");
default:
return FtpMockControlSocket::OnWrite(data);
}
}
private:
DISALLOW_COPY_AND_ASSIGN(FtpMockControlSocketFileDownload);
};
class FtpMockControlSocketFileDownloadRetrFail
: public FtpMockControlSocketFileDownload {
public:
FtpMockControlSocketFileDownloadRetrFail() {
}
virtual MockWriteResult OnWrite(const std::string& data) {
if (InjectFault())
return MockWriteResult(true, OK);
switch (state()) {
case PRE_PASV2:
return Verify("PASV\r\n", data, PRE_CWD,
"227 Entering Passive Mode (127,0,0,1,123,456)\r\n");
case PRE_CWD:
return Verify("CWD /file\r\n", data, PRE_QUIT,
"500 file is a directory\r\n");
default:
return FtpMockControlSocketFileDownload::OnWrite(data);
}
}
private:
DISALLOW_COPY_AND_ASSIGN(FtpMockControlSocketFileDownloadRetrFail);
};
class FtpNetworkTransactionTest : public PlatformTest {
public:
FtpNetworkTransactionTest()
: session_(new FtpNetworkSession(new HostResolver)),
transaction_(session_.get(), &mock_socket_factory_) {
}
protected:
FtpRequestInfo GetRequestInfo(const std::string& url) {
FtpRequestInfo info;
info.url = GURL(url);
return info;
}
void TransactionFailHelper(FtpMockControlSocket* ctrl_socket,
const char* request,
FtpMockControlSocket::State state,
FtpMockControlSocket::State next_state,
const char* response,
int expected_result) {
ctrl_socket->InjectFailure(state, next_state, response);
StaticMockSocket data_socket1;
StaticMockSocket data_socket2;
mock_socket_factory_.AddMockSocket(ctrl_socket);
mock_socket_factory_.AddMockSocket(&data_socket1);
mock_socket_factory_.AddMockSocket(&data_socket2);
FtpRequestInfo request_info = GetRequestInfo(request);
ASSERT_EQ(ERR_IO_PENDING, transaction_.Start(&request_info, &callback_));
EXPECT_EQ(expected_result, callback_.WaitForResult());
EXPECT_EQ(FtpMockControlSocket::QUIT, ctrl_socket->state());
}
scoped_refptr<FtpNetworkSession> session_;
MockClientSocketFactory mock_socket_factory_;
FtpNetworkTransaction transaction_;
TestCompletionCallback callback_;
};
TEST_F(FtpNetworkTransactionTest, FailedLookup) {
FtpRequestInfo request_info = GetRequestInfo("ftp://badhost");
scoped_refptr<RuleBasedHostMapper> mapper(new RuleBasedHostMapper());
mapper->AddSimulatedFailure("badhost");
ScopedHostMapper scoped_mapper(mapper.get());
ASSERT_EQ(ERR_IO_PENDING, transaction_.Start(&request_info, &callback_));
EXPECT_EQ(ERR_FAILED, callback_.WaitForResult());
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransaction) {
FtpMockControlSocketDirectoryListing ctrl_socket;
std::string test_string("mock-directory-listing");
MockRead data_reads[] = {
MockRead(test_string.c_str()),
};
// TODO(phajdan.jr): FTP transaction should not open two data sockets.
StaticMockSocket data_socket1;
StaticMockSocket data_socket2(data_reads, NULL);
mock_socket_factory_.AddMockSocket(&ctrl_socket);
mock_socket_factory_.AddMockSocket(&data_socket1);
mock_socket_factory_.AddMockSocket(&data_socket2);
FtpRequestInfo request_info = GetRequestInfo("ftp://host");
ASSERT_EQ(ERR_IO_PENDING, transaction_.Start(&request_info, &callback_));
EXPECT_EQ(OK, callback_.WaitForResult());
EXPECT_EQ(FtpMockControlSocket::QUIT, ctrl_socket.state());
scoped_refptr<IOBuffer> io_buffer(new IOBuffer(kBufferSize));
memset(io_buffer->data(), 0, kBufferSize);
EXPECT_EQ(ERR_IO_PENDING,
transaction_.Read(io_buffer.get(), kBufferSize, &callback_));
EXPECT_EQ(static_cast<int>(test_string.length()), callback_.WaitForResult());
EXPECT_EQ(test_string, std::string(io_buffer->data(), test_string.length()));
}
TEST_F(FtpNetworkTransactionTest, DownloadTransaction) {
FtpMockControlSocketFileDownload ctrl_socket;
std::string test_string("mock-file-contents");
MockRead data_reads[] = {
MockRead(test_string.c_str()),
};
StaticMockSocket data_socket(data_reads, NULL);
mock_socket_factory_.AddMockSocket(&ctrl_socket);
mock_socket_factory_.AddMockSocket(&data_socket);
FtpRequestInfo request_info = GetRequestInfo("ftp://host/file");
ASSERT_EQ(ERR_IO_PENDING, transaction_.Start(&request_info, &callback_));
EXPECT_EQ(OK, callback_.WaitForResult());
EXPECT_EQ(FtpMockControlSocket::QUIT, ctrl_socket.state());
scoped_refptr<IOBuffer> io_buffer(new IOBuffer(kBufferSize));
memset(io_buffer->data(), 0, kBufferSize);
EXPECT_EQ(ERR_IO_PENDING,
transaction_.Read(io_buffer.get(), kBufferSize, &callback_));
EXPECT_EQ(static_cast<int>(test_string.length()), callback_.WaitForResult());
EXPECT_EQ(test_string, std::string(io_buffer->data(), test_string.length()));
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailUser) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_USER,
FtpMockControlSocket::PRE_QUIT,
"500 no such user\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailPass) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_PASSWD,
FtpMockControlSocket::PRE_QUIT,
"530 Login authentication failed\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailSyst) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_SYST,
FtpMockControlSocket::PRE_PWD,
"500 failed syst\r\n",
OK);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailPwd) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_PWD,
FtpMockControlSocket::PRE_QUIT,
"500 failed pwd\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailType) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_TYPE,
FtpMockControlSocket::PRE_QUIT,
"500 failed type\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailPasv) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_PASV,
FtpMockControlSocket::PRE_QUIT,
"500 failed pasv\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionMalformedMdtm) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_MDTM,
FtpMockControlSocket::PRE_RETR,
"213 foobar\r\n",
OK);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailMdtm) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_MDTM,
FtpMockControlSocket::PRE_RETR,
"500 failed mdtm\r\n",
OK);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailPasv2) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_PASV2,
FtpMockControlSocket::PRE_QUIT,
"500 failed pasv\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailCwd) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_CWD,
FtpMockControlSocket::PRE_QUIT,
"500 failed cwd\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailList) {
FtpMockControlSocketDirectoryListing ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host",
FtpMockControlSocket::PRE_LIST,
FtpMockControlSocket::PRE_QUIT,
"500 failed list\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailUser) {
FtpMockControlSocketFileDownload ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host/file",
FtpMockControlSocket::PRE_USER,
FtpMockControlSocket::PRE_QUIT,
"500 no such user\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailPass) {
FtpMockControlSocketFileDownload ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host/file",
FtpMockControlSocket::PRE_PASSWD,
FtpMockControlSocket::PRE_QUIT,
"530 Login authentication failed\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailSyst) {
FtpMockControlSocketFileDownload ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host/file",
FtpMockControlSocket::PRE_SYST,
FtpMockControlSocket::PRE_PWD,
"500 failed syst\r\n",
OK);
}
TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailPwd) {
FtpMockControlSocketFileDownload ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host/file",
FtpMockControlSocket::PRE_PWD,
FtpMockControlSocket::PRE_QUIT,
"500 failed pwd\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailType) {
FtpMockControlSocketFileDownload ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host/file",
FtpMockControlSocket::PRE_TYPE,
FtpMockControlSocket::PRE_QUIT,
"500 failed type\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailPasv) {
FtpMockControlSocketFileDownload ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host/file",
FtpMockControlSocket::PRE_PASV,
FtpMockControlSocket::PRE_QUIT,
"500 failed pasv\r\n",
ERR_FAILED);
}
TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailMdtm) {
FtpMockControlSocketFileDownload ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host/file",
FtpMockControlSocket::PRE_MDTM,
FtpMockControlSocket::PRE_RETR,
"500 failed mdtm\r\n",
OK);
}
TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailRetr) {
FtpMockControlSocketFileDownloadRetrFail ctrl_socket;
TransactionFailHelper(&ctrl_socket,
"ftp://host/file",
FtpMockControlSocket::PRE_RETR,
FtpMockControlSocket::PRE_PASV2,
"500 failed retr\r\n",
ERR_FAILED);
}
} // namespace net
|