blob: 759ed87205d92ac97cae0c1e4bf19f78eb02c581 [file] [log] [blame]
[email protected]c2972192009-07-28 20:30:091// 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.
4
5#include "net/ftp/ftp_ctrl_response_buffer.h"
6
7#include <string.h>
8
9#include "net/base/net_errors.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
ttuttle859dc7a2015-04-23 19:42:2912namespace net {
13
[email protected]c2972192009-07-28 20:30:0914namespace {
15
16class FtpCtrlResponseBufferTest : public testing::Test {
[email protected]844f32c12012-11-07 12:06:0117 public:
ttuttle859dc7a2015-04-23 19:42:2918 FtpCtrlResponseBufferTest() : buffer_(BoundNetLog()) {}
[email protected]844f32c12012-11-07 12:06:0119
[email protected]c2972192009-07-28 20:30:0920 protected:
21 int PushDataToBuffer(const char* data) {
22 return buffer_.ConsumeData(data, strlen(data));
23 }
24
ttuttle859dc7a2015-04-23 19:42:2925 FtpCtrlResponseBuffer buffer_;
[email protected]c2972192009-07-28 20:30:0926};
27
28TEST_F(FtpCtrlResponseBufferTest, Basic) {
29 EXPECT_FALSE(buffer_.ResponseAvailable());
30
ttuttle859dc7a2015-04-23 19:42:2931 EXPECT_EQ(OK, PushDataToBuffer("200 Status Text\r\n"));
[email protected]c2972192009-07-28 20:30:0932 EXPECT_TRUE(buffer_.ResponseAvailable());
33
ttuttle859dc7a2015-04-23 19:42:2934 FtpCtrlResponse response = buffer_.PopResponse();
[email protected]c2972192009-07-28 20:30:0935 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]);
39}
40
41TEST_F(FtpCtrlResponseBufferTest, Chunks) {
ttuttle859dc7a2015-04-23 19:42:2942 EXPECT_EQ(OK, PushDataToBuffer("20"));
[email protected]c2972192009-07-28 20:30:0943 EXPECT_FALSE(buffer_.ResponseAvailable());
ttuttle859dc7a2015-04-23 19:42:2944 EXPECT_EQ(OK, PushDataToBuffer("0 Status"));
[email protected]c2972192009-07-28 20:30:0945 EXPECT_FALSE(buffer_.ResponseAvailable());
ttuttle859dc7a2015-04-23 19:42:2946 EXPECT_EQ(OK, PushDataToBuffer(" Text"));
[email protected]c2972192009-07-28 20:30:0947 EXPECT_FALSE(buffer_.ResponseAvailable());
ttuttle859dc7a2015-04-23 19:42:2948 EXPECT_EQ(OK, PushDataToBuffer("\r"));
[email protected]c2972192009-07-28 20:30:0949 EXPECT_FALSE(buffer_.ResponseAvailable());
ttuttle859dc7a2015-04-23 19:42:2950 EXPECT_EQ(OK, PushDataToBuffer("\n"));
[email protected]c2972192009-07-28 20:30:0951 EXPECT_TRUE(buffer_.ResponseAvailable());
52
ttuttle859dc7a2015-04-23 19:42:2953 FtpCtrlResponse response = buffer_.PopResponse();
[email protected]c2972192009-07-28 20:30:0954 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]);
58}
59
60TEST_F(FtpCtrlResponseBufferTest, Continuation) {
ttuttle859dc7a2015-04-23 19:42:2961 EXPECT_EQ(OK, PushDataToBuffer("230-FirstLine\r\n"));
[email protected]c2972192009-07-28 20:30:0962 EXPECT_FALSE(buffer_.ResponseAvailable());
63
ttuttle859dc7a2015-04-23 19:42:2964 EXPECT_EQ(OK, PushDataToBuffer("230-SecondLine\r\n"));
[email protected]c2972192009-07-28 20:30:0965 EXPECT_FALSE(buffer_.ResponseAvailable());
66
ttuttle859dc7a2015-04-23 19:42:2967 EXPECT_EQ(OK, PushDataToBuffer("230 LastLine\r\n"));
[email protected]c2972192009-07-28 20:30:0968 EXPECT_TRUE(buffer_.ResponseAvailable());
69
ttuttle859dc7a2015-04-23 19:42:2970 FtpCtrlResponse response = buffer_.PopResponse();
[email protected]c2972192009-07-28 20:30:0971 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]);
77}
78
79TEST_F(FtpCtrlResponseBufferTest, MultilineContinuation) {
ttuttle859dc7a2015-04-23 19:42:2980 EXPECT_EQ(OK, PushDataToBuffer("230-FirstLine\r\n"));
[email protected]c2972192009-07-28 20:30:0981 EXPECT_FALSE(buffer_.ResponseAvailable());
82
ttuttle859dc7a2015-04-23 19:42:2983 EXPECT_EQ(OK, PushDataToBuffer("Continued\r\n"));
[email protected]c2972192009-07-28 20:30:0984 EXPECT_FALSE(buffer_.ResponseAvailable());
85
ttuttle859dc7a2015-04-23 19:42:2986 EXPECT_EQ(OK, PushDataToBuffer("230-SecondLine\r\n"));
[email protected]c2972192009-07-28 20:30:0987 EXPECT_FALSE(buffer_.ResponseAvailable());
88
ttuttle859dc7a2015-04-23 19:42:2989 EXPECT_EQ(OK, PushDataToBuffer("215 Continued\r\n"));
[email protected]c2972192009-07-28 20:30:0990 EXPECT_FALSE(buffer_.ResponseAvailable());
91
ttuttle859dc7a2015-04-23 19:42:2992 EXPECT_EQ(OK, PushDataToBuffer("230 LastLine\r\n"));
[email protected]c2972192009-07-28 20:30:0993 EXPECT_TRUE(buffer_.ResponseAvailable());
94
ttuttle859dc7a2015-04-23 19:42:2995 FtpCtrlResponse response = buffer_.PopResponse();
[email protected]c2972192009-07-28 20:30:0996 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]);
102}
103
[email protected]1c9735a2009-12-07 20:50:48104TEST_F(FtpCtrlResponseBufferTest, MultilineContinuationZeroLength) {
105 // For the corner case from bug 29322.
ttuttle859dc7a2015-04-23 19:42:29106 EXPECT_EQ(OK, PushDataToBuffer("230-\r\n"));
[email protected]1c9735a2009-12-07 20:50:48107 EXPECT_FALSE(buffer_.ResponseAvailable());
108
ttuttle859dc7a2015-04-23 19:42:29109 EXPECT_EQ(OK, PushDataToBuffer("example.com\r\n"));
[email protected]1c9735a2009-12-07 20:50:48110 EXPECT_FALSE(buffer_.ResponseAvailable());
111
ttuttle859dc7a2015-04-23 19:42:29112 EXPECT_EQ(OK, PushDataToBuffer("230 LastLine\r\n"));
[email protected]1c9735a2009-12-07 20:50:48113 EXPECT_TRUE(buffer_.ResponseAvailable());
114
ttuttle859dc7a2015-04-23 19:42:29115 FtpCtrlResponse response = buffer_.PopResponse();
[email protected]1c9735a2009-12-07 20:50:48116 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]);
121}
122
[email protected]c2972192009-07-28 20:30:09123TEST_F(FtpCtrlResponseBufferTest, SimilarContinuation) {
ttuttle859dc7a2015-04-23 19:42:29124 EXPECT_EQ(OK, PushDataToBuffer("230-FirstLine\r\n"));
[email protected]c2972192009-07-28 20:30:09125 EXPECT_FALSE(buffer_.ResponseAvailable());
126
127 // Notice the space at the start of the line. It should be recognized
128 // as a continuation, and not the last line.
ttuttle859dc7a2015-04-23 19:42:29129 EXPECT_EQ(OK, PushDataToBuffer(" 230 Continued\r\n"));
[email protected]c2972192009-07-28 20:30:09130 EXPECT_FALSE(buffer_.ResponseAvailable());
131
ttuttle859dc7a2015-04-23 19:42:29132 EXPECT_EQ(OK, PushDataToBuffer("230 TrueLastLine\r\n"));
[email protected]c2972192009-07-28 20:30:09133 EXPECT_TRUE(buffer_.ResponseAvailable());
134
ttuttle859dc7a2015-04-23 19:42:29135 FtpCtrlResponse response = buffer_.PopResponse();
[email protected]c2972192009-07-28 20:30:09136 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]);
141}
142
143// The nesting of multi-line responses is not allowed.
144TEST_F(FtpCtrlResponseBufferTest, NoNesting) {
ttuttle859dc7a2015-04-23 19:42:29145 EXPECT_EQ(OK, PushDataToBuffer("230-FirstLine\r\n"));
[email protected]c2972192009-07-28 20:30:09146 EXPECT_FALSE(buffer_.ResponseAvailable());
147
ttuttle859dc7a2015-04-23 19:42:29148 EXPECT_EQ(OK, PushDataToBuffer("300-Continuation\r\n"));
[email protected]c2972192009-07-28 20:30:09149 EXPECT_FALSE(buffer_.ResponseAvailable());
150
ttuttle859dc7a2015-04-23 19:42:29151 EXPECT_EQ(OK, PushDataToBuffer("300 Still continuation\r\n"));
[email protected]c2972192009-07-28 20:30:09152 EXPECT_FALSE(buffer_.ResponseAvailable());
153
ttuttle859dc7a2015-04-23 19:42:29154 EXPECT_EQ(OK, PushDataToBuffer("230 Real End\r\n"));
[email protected]c2972192009-07-28 20:30:09155 ASSERT_TRUE(buffer_.ResponseAvailable());
156
ttuttle859dc7a2015-04-23 19:42:29157 FtpCtrlResponse response = buffer_.PopResponse();
[email protected]c2972192009-07-28 20:30:09158 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",
162 response.lines[0]);
163 EXPECT_EQ("Real End", response.lines[1]);
164}
165
166TEST_F(FtpCtrlResponseBufferTest, NonNumericResponse) {
ttuttle859dc7a2015-04-23 19:42:29167 EXPECT_EQ(ERR_INVALID_RESPONSE, PushDataToBuffer("Non-numeric\r\n"));
[email protected]c2972192009-07-28 20:30:09168 EXPECT_FALSE(buffer_.ResponseAvailable());
169}
170
171TEST_F(FtpCtrlResponseBufferTest, OutOfRangeResponse) {
ttuttle859dc7a2015-04-23 19:42:29172 EXPECT_EQ(ERR_INVALID_RESPONSE, PushDataToBuffer("777 OK?\r\n"));
[email protected]c2972192009-07-28 20:30:09173 EXPECT_FALSE(buffer_.ResponseAvailable());
174}
175
176} // namespace
ttuttle859dc7a2015-04-23 19:42:29177
178} // namespace net