1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/ftp/ftp_ctrl_response_buffer.h"
9 #include "net/base/net_errors.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 class FtpCtrlResponseBufferTest
: public testing::Test
{
18 FtpCtrlResponseBufferTest() : buffer_(BoundNetLog()) {}
21 int PushDataToBuffer(const char* data
) {
22 return buffer_
.ConsumeData(data
, strlen(data
));
25 FtpCtrlResponseBuffer buffer_
;
28 TEST_F(FtpCtrlResponseBufferTest
, Basic
) {
29 EXPECT_FALSE(buffer_
.ResponseAvailable());
31 EXPECT_EQ(OK
, PushDataToBuffer("200 Status Text\r\n"));
32 EXPECT_TRUE(buffer_
.ResponseAvailable());
34 FtpCtrlResponse response
= buffer_
.PopResponse();
35 EXPECT_FALSE(buffer_
.ResponseAvailable());
36 EXPECT_EQ(200, response
.status_code
);
37 ASSERT_EQ(1U, response
.lines
.size());
38 EXPECT_EQ("Status Text", response
.lines
[0]);
41 TEST_F(FtpCtrlResponseBufferTest
, Chunks
) {
42 EXPECT_EQ(OK
, PushDataToBuffer("20"));
43 EXPECT_FALSE(buffer_
.ResponseAvailable());
44 EXPECT_EQ(OK
, PushDataToBuffer("0 Status"));
45 EXPECT_FALSE(buffer_
.ResponseAvailable());
46 EXPECT_EQ(OK
, PushDataToBuffer(" Text"));
47 EXPECT_FALSE(buffer_
.ResponseAvailable());
48 EXPECT_EQ(OK
, PushDataToBuffer("\r"));
49 EXPECT_FALSE(buffer_
.ResponseAvailable());
50 EXPECT_EQ(OK
, PushDataToBuffer("\n"));
51 EXPECT_TRUE(buffer_
.ResponseAvailable());
53 FtpCtrlResponse response
= buffer_
.PopResponse();
54 EXPECT_FALSE(buffer_
.ResponseAvailable());
55 EXPECT_EQ(200, response
.status_code
);
56 ASSERT_EQ(1U, response
.lines
.size());
57 EXPECT_EQ("Status Text", response
.lines
[0]);
60 TEST_F(FtpCtrlResponseBufferTest
, Continuation
) {
61 EXPECT_EQ(OK
, PushDataToBuffer("230-FirstLine\r\n"));
62 EXPECT_FALSE(buffer_
.ResponseAvailable());
64 EXPECT_EQ(OK
, PushDataToBuffer("230-SecondLine\r\n"));
65 EXPECT_FALSE(buffer_
.ResponseAvailable());
67 EXPECT_EQ(OK
, PushDataToBuffer("230 LastLine\r\n"));
68 EXPECT_TRUE(buffer_
.ResponseAvailable());
70 FtpCtrlResponse response
= buffer_
.PopResponse();
71 EXPECT_FALSE(buffer_
.ResponseAvailable());
72 EXPECT_EQ(230, response
.status_code
);
73 ASSERT_EQ(3U, response
.lines
.size());
74 EXPECT_EQ("FirstLine", response
.lines
[0]);
75 EXPECT_EQ("SecondLine", response
.lines
[1]);
76 EXPECT_EQ("LastLine", response
.lines
[2]);
79 TEST_F(FtpCtrlResponseBufferTest
, MultilineContinuation
) {
80 EXPECT_EQ(OK
, PushDataToBuffer("230-FirstLine\r\n"));
81 EXPECT_FALSE(buffer_
.ResponseAvailable());
83 EXPECT_EQ(OK
, PushDataToBuffer("Continued\r\n"));
84 EXPECT_FALSE(buffer_
.ResponseAvailable());
86 EXPECT_EQ(OK
, PushDataToBuffer("230-SecondLine\r\n"));
87 EXPECT_FALSE(buffer_
.ResponseAvailable());
89 EXPECT_EQ(OK
, PushDataToBuffer("215 Continued\r\n"));
90 EXPECT_FALSE(buffer_
.ResponseAvailable());
92 EXPECT_EQ(OK
, PushDataToBuffer("230 LastLine\r\n"));
93 EXPECT_TRUE(buffer_
.ResponseAvailable());
95 FtpCtrlResponse response
= buffer_
.PopResponse();
96 EXPECT_FALSE(buffer_
.ResponseAvailable());
97 EXPECT_EQ(230, response
.status_code
);
98 ASSERT_EQ(3U, response
.lines
.size());
99 EXPECT_EQ("FirstLineContinued", response
.lines
[0]);
100 EXPECT_EQ("SecondLine215 Continued", response
.lines
[1]);
101 EXPECT_EQ("LastLine", response
.lines
[2]);
104 TEST_F(FtpCtrlResponseBufferTest
, MultilineContinuationZeroLength
) {
105 // For the corner case from bug 29322.
106 EXPECT_EQ(OK
, PushDataToBuffer("230-\r\n"));
107 EXPECT_FALSE(buffer_
.ResponseAvailable());
109 EXPECT_EQ(OK
, PushDataToBuffer("example.com\r\n"));
110 EXPECT_FALSE(buffer_
.ResponseAvailable());
112 EXPECT_EQ(OK
, PushDataToBuffer("230 LastLine\r\n"));
113 EXPECT_TRUE(buffer_
.ResponseAvailable());
115 FtpCtrlResponse response
= buffer_
.PopResponse();
116 EXPECT_FALSE(buffer_
.ResponseAvailable());
117 EXPECT_EQ(230, response
.status_code
);
118 ASSERT_EQ(2U, response
.lines
.size());
119 EXPECT_EQ("example.com", response
.lines
[0]);
120 EXPECT_EQ("LastLine", response
.lines
[1]);
123 TEST_F(FtpCtrlResponseBufferTest
, SimilarContinuation
) {
124 EXPECT_EQ(OK
, PushDataToBuffer("230-FirstLine\r\n"));
125 EXPECT_FALSE(buffer_
.ResponseAvailable());
127 // Notice the space at the start of the line. It should be recognized
128 // as a continuation, and not the last line.
129 EXPECT_EQ(OK
, PushDataToBuffer(" 230 Continued\r\n"));
130 EXPECT_FALSE(buffer_
.ResponseAvailable());
132 EXPECT_EQ(OK
, PushDataToBuffer("230 TrueLastLine\r\n"));
133 EXPECT_TRUE(buffer_
.ResponseAvailable());
135 FtpCtrlResponse response
= buffer_
.PopResponse();
136 EXPECT_FALSE(buffer_
.ResponseAvailable());
137 EXPECT_EQ(230, response
.status_code
);
138 ASSERT_EQ(2U, response
.lines
.size());
139 EXPECT_EQ("FirstLine 230 Continued", response
.lines
[0]);
140 EXPECT_EQ("TrueLastLine", response
.lines
[1]);
143 // The nesting of multi-line responses is not allowed.
144 TEST_F(FtpCtrlResponseBufferTest
, NoNesting
) {
145 EXPECT_EQ(OK
, PushDataToBuffer("230-FirstLine\r\n"));
146 EXPECT_FALSE(buffer_
.ResponseAvailable());
148 EXPECT_EQ(OK
, PushDataToBuffer("300-Continuation\r\n"));
149 EXPECT_FALSE(buffer_
.ResponseAvailable());
151 EXPECT_EQ(OK
, PushDataToBuffer("300 Still continuation\r\n"));
152 EXPECT_FALSE(buffer_
.ResponseAvailable());
154 EXPECT_EQ(OK
, PushDataToBuffer("230 Real End\r\n"));
155 ASSERT_TRUE(buffer_
.ResponseAvailable());
157 FtpCtrlResponse response
= buffer_
.PopResponse();
158 EXPECT_FALSE(buffer_
.ResponseAvailable());
159 EXPECT_EQ(230, response
.status_code
);
160 ASSERT_EQ(2U, response
.lines
.size());
161 EXPECT_EQ("FirstLine300-Continuation300 Still continuation",
163 EXPECT_EQ("Real End", response
.lines
[1]);
166 TEST_F(FtpCtrlResponseBufferTest
, NonNumericResponse
) {
167 EXPECT_EQ(ERR_INVALID_RESPONSE
, PushDataToBuffer("Non-numeric\r\n"));
168 EXPECT_FALSE(buffer_
.ResponseAvailable());
171 TEST_F(FtpCtrlResponseBufferTest
, OutOfRangeResponse
) {
172 EXPECT_EQ(ERR_INVALID_RESPONSE
, PushDataToBuffer("777 OK?\r\n"));
173 EXPECT_FALSE(buffer_
.ResponseAvailable());