[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 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. |
| 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 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 12 | namespace net { |
| 13 | |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | class FtpCtrlResponseBufferTest : public testing::Test { |
[email protected] | 844f32c1 | 2012-11-07 12:06:01 | [diff] [blame] | 17 | public: |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 18 | FtpCtrlResponseBufferTest() : buffer_(BoundNetLog()) {} |
[email protected] | 844f32c1 | 2012-11-07 12:06:01 | [diff] [blame] | 19 | |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 20 | protected: |
| 21 | int PushDataToBuffer(const char* data) { |
| 22 | return buffer_.ConsumeData(data, strlen(data)); |
| 23 | } |
| 24 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 25 | FtpCtrlResponseBuffer buffer_; |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | TEST_F(FtpCtrlResponseBufferTest, Basic) { |
| 29 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 30 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 31 | EXPECT_EQ(OK, PushDataToBuffer("200 Status Text\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 32 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 33 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 34 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 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]); |
| 39 | } |
| 40 | |
| 41 | TEST_F(FtpCtrlResponseBufferTest, Chunks) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 42 | EXPECT_EQ(OK, PushDataToBuffer("20")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 43 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 44 | EXPECT_EQ(OK, PushDataToBuffer("0 Status")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 45 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 46 | EXPECT_EQ(OK, PushDataToBuffer(" Text")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 47 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 48 | EXPECT_EQ(OK, PushDataToBuffer("\r")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 49 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 50 | EXPECT_EQ(OK, PushDataToBuffer("\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 51 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 52 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 53 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 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]); |
| 58 | } |
| 59 | |
| 60 | TEST_F(FtpCtrlResponseBufferTest, Continuation) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 61 | EXPECT_EQ(OK, PushDataToBuffer("230-FirstLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 62 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 63 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 64 | EXPECT_EQ(OK, PushDataToBuffer("230-SecondLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 65 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 66 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 67 | EXPECT_EQ(OK, PushDataToBuffer("230 LastLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 68 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 69 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 70 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 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]); |
| 77 | } |
| 78 | |
| 79 | TEST_F(FtpCtrlResponseBufferTest, MultilineContinuation) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 80 | EXPECT_EQ(OK, PushDataToBuffer("230-FirstLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 81 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 82 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 83 | EXPECT_EQ(OK, PushDataToBuffer("Continued\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 84 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 85 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 86 | EXPECT_EQ(OK, PushDataToBuffer("230-SecondLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 87 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 88 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 89 | EXPECT_EQ(OK, PushDataToBuffer("215 Continued\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 90 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 91 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 92 | EXPECT_EQ(OK, PushDataToBuffer("230 LastLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 93 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 94 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 95 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 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]); |
| 102 | } |
| 103 | |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 104 | TEST_F(FtpCtrlResponseBufferTest, MultilineContinuationZeroLength) { |
| 105 | // For the corner case from bug 29322. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 106 | EXPECT_EQ(OK, PushDataToBuffer("230-\r\n")); |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 107 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 108 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 109 | EXPECT_EQ(OK, PushDataToBuffer("example.com\r\n")); |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 110 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 111 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 112 | EXPECT_EQ(OK, PushDataToBuffer("230 LastLine\r\n")); |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 113 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 114 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 115 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 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]); |
| 121 | } |
| 122 | |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 123 | TEST_F(FtpCtrlResponseBufferTest, SimilarContinuation) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 124 | EXPECT_EQ(OK, PushDataToBuffer("230-FirstLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 125 | 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. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 129 | EXPECT_EQ(OK, PushDataToBuffer(" 230 Continued\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 130 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 131 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 132 | EXPECT_EQ(OK, PushDataToBuffer("230 TrueLastLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 133 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 134 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 135 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 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]); |
| 141 | } |
| 142 | |
| 143 | // The nesting of multi-line responses is not allowed. |
| 144 | TEST_F(FtpCtrlResponseBufferTest, NoNesting) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 145 | EXPECT_EQ(OK, PushDataToBuffer("230-FirstLine\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 146 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 147 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 148 | EXPECT_EQ(OK, PushDataToBuffer("300-Continuation\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 149 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 150 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 151 | EXPECT_EQ(OK, PushDataToBuffer("300 Still continuation\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 152 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 153 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 154 | EXPECT_EQ(OK, PushDataToBuffer("230 Real End\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 155 | ASSERT_TRUE(buffer_.ResponseAvailable()); |
| 156 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 157 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 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", |
| 162 | response.lines[0]); |
| 163 | EXPECT_EQ("Real End", response.lines[1]); |
| 164 | } |
| 165 | |
| 166 | TEST_F(FtpCtrlResponseBufferTest, NonNumericResponse) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 167 | EXPECT_EQ(ERR_INVALID_RESPONSE, PushDataToBuffer("Non-numeric\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 168 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 169 | } |
| 170 | |
| 171 | TEST_F(FtpCtrlResponseBufferTest, OutOfRangeResponse) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 172 | EXPECT_EQ(ERR_INVALID_RESPONSE, PushDataToBuffer("777 OK?\r\n")); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 173 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 174 | } |
| 175 | |
| 176 | } // namespace |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 177 | |
| 178 | } // namespace net |