[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" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10 | #include "net/test/gtest_util.h" |
| 11 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14 | using net::test::IsError; |
| 15 | using net::test::IsOk; |
| 16 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 17 | namespace net { |
| 18 | |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | class FtpCtrlResponseBufferTest : public testing::Test { |
[email protected] | 844f32c1 | 2012-11-07 12:06:01 | [diff] [blame] | 22 | public: |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 23 | FtpCtrlResponseBufferTest() : buffer_(NetLogWithSource()) {} |
[email protected] | 844f32c1 | 2012-11-07 12:06:01 | [diff] [blame] | 24 | |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 25 | protected: |
| 26 | int PushDataToBuffer(const char* data) { |
| 27 | return buffer_.ConsumeData(data, strlen(data)); |
| 28 | } |
| 29 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 30 | FtpCtrlResponseBuffer buffer_; |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | TEST_F(FtpCtrlResponseBufferTest, Basic) { |
| 34 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 35 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 36 | EXPECT_THAT(PushDataToBuffer("200 Status Text\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 37 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 38 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 39 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 40 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 41 | EXPECT_EQ(200, response.status_code); |
| 42 | ASSERT_EQ(1U, response.lines.size()); |
| 43 | EXPECT_EQ("Status Text", response.lines[0]); |
| 44 | } |
| 45 | |
| 46 | TEST_F(FtpCtrlResponseBufferTest, Chunks) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 47 | EXPECT_THAT(PushDataToBuffer("20"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 48 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 49 | EXPECT_THAT(PushDataToBuffer("0 Status"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 50 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 51 | EXPECT_THAT(PushDataToBuffer(" Text"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 52 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 53 | EXPECT_THAT(PushDataToBuffer("\r"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 54 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 55 | EXPECT_THAT(PushDataToBuffer("\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 56 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 57 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 58 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 59 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 60 | EXPECT_EQ(200, response.status_code); |
| 61 | ASSERT_EQ(1U, response.lines.size()); |
| 62 | EXPECT_EQ("Status Text", response.lines[0]); |
| 63 | } |
| 64 | |
| 65 | TEST_F(FtpCtrlResponseBufferTest, Continuation) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 66 | EXPECT_THAT(PushDataToBuffer("230-FirstLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 67 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 68 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 69 | EXPECT_THAT(PushDataToBuffer("230-SecondLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 70 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 71 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 72 | EXPECT_THAT(PushDataToBuffer("230 LastLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 73 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 74 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 75 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 76 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 77 | EXPECT_EQ(230, response.status_code); |
| 78 | ASSERT_EQ(3U, response.lines.size()); |
| 79 | EXPECT_EQ("FirstLine", response.lines[0]); |
| 80 | EXPECT_EQ("SecondLine", response.lines[1]); |
| 81 | EXPECT_EQ("LastLine", response.lines[2]); |
| 82 | } |
| 83 | |
| 84 | TEST_F(FtpCtrlResponseBufferTest, MultilineContinuation) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 85 | EXPECT_THAT(PushDataToBuffer("230-FirstLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 86 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 87 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 88 | EXPECT_THAT(PushDataToBuffer("Continued\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 89 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 90 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 91 | EXPECT_THAT(PushDataToBuffer("230-SecondLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 92 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 93 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 94 | EXPECT_THAT(PushDataToBuffer("215 Continued\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 95 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 96 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 97 | EXPECT_THAT(PushDataToBuffer("230 LastLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 98 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 99 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 100 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 101 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 102 | EXPECT_EQ(230, response.status_code); |
| 103 | ASSERT_EQ(3U, response.lines.size()); |
| 104 | EXPECT_EQ("FirstLineContinued", response.lines[0]); |
| 105 | EXPECT_EQ("SecondLine215 Continued", response.lines[1]); |
| 106 | EXPECT_EQ("LastLine", response.lines[2]); |
| 107 | } |
| 108 | |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 109 | TEST_F(FtpCtrlResponseBufferTest, MultilineContinuationZeroLength) { |
| 110 | // For the corner case from bug 29322. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 111 | EXPECT_THAT(PushDataToBuffer("230-\r\n"), IsOk()); |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 112 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 113 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 114 | EXPECT_THAT(PushDataToBuffer("example.com\r\n"), IsOk()); |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 115 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 116 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 117 | EXPECT_THAT(PushDataToBuffer("230 LastLine\r\n"), IsOk()); |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 118 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 119 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 120 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | 1c9735a | 2009-12-07 20:50:48 | [diff] [blame] | 121 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 122 | EXPECT_EQ(230, response.status_code); |
| 123 | ASSERT_EQ(2U, response.lines.size()); |
| 124 | EXPECT_EQ("example.com", response.lines[0]); |
| 125 | EXPECT_EQ("LastLine", response.lines[1]); |
| 126 | } |
| 127 | |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 128 | TEST_F(FtpCtrlResponseBufferTest, SimilarContinuation) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 129 | EXPECT_THAT(PushDataToBuffer("230-FirstLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 130 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 131 | |
| 132 | // Notice the space at the start of the line. It should be recognized |
| 133 | // as a continuation, and not the last line. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 134 | EXPECT_THAT(PushDataToBuffer(" 230 Continued\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 135 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 136 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 137 | EXPECT_THAT(PushDataToBuffer("230 TrueLastLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 138 | EXPECT_TRUE(buffer_.ResponseAvailable()); |
| 139 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 140 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 141 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 142 | EXPECT_EQ(230, response.status_code); |
| 143 | ASSERT_EQ(2U, response.lines.size()); |
| 144 | EXPECT_EQ("FirstLine 230 Continued", response.lines[0]); |
| 145 | EXPECT_EQ("TrueLastLine", response.lines[1]); |
| 146 | } |
| 147 | |
| 148 | // The nesting of multi-line responses is not allowed. |
| 149 | TEST_F(FtpCtrlResponseBufferTest, NoNesting) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 150 | EXPECT_THAT(PushDataToBuffer("230-FirstLine\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 151 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 152 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 153 | EXPECT_THAT(PushDataToBuffer("300-Continuation\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 154 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 155 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 156 | EXPECT_THAT(PushDataToBuffer("300 Still continuation\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 157 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 158 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 159 | EXPECT_THAT(PushDataToBuffer("230 Real End\r\n"), IsOk()); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 160 | ASSERT_TRUE(buffer_.ResponseAvailable()); |
| 161 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 162 | FtpCtrlResponse response = buffer_.PopResponse(); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 163 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 164 | EXPECT_EQ(230, response.status_code); |
| 165 | ASSERT_EQ(2U, response.lines.size()); |
| 166 | EXPECT_EQ("FirstLine300-Continuation300 Still continuation", |
| 167 | response.lines[0]); |
| 168 | EXPECT_EQ("Real End", response.lines[1]); |
| 169 | } |
| 170 | |
| 171 | TEST_F(FtpCtrlResponseBufferTest, NonNumericResponse) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 172 | EXPECT_THAT(PushDataToBuffer("Non-numeric\r\n"), |
| 173 | IsError(ERR_INVALID_RESPONSE)); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 174 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 175 | } |
| 176 | |
| 177 | TEST_F(FtpCtrlResponseBufferTest, OutOfRangeResponse) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 178 | EXPECT_THAT(PushDataToBuffer("777 OK?\r\n"), IsError(ERR_INVALID_RESPONSE)); |
[email protected] | c297219 | 2009-07-28 20:30:09 | [diff] [blame] | 179 | EXPECT_FALSE(buffer_.ResponseAvailable()); |
| 180 | } |
| 181 | |
| 182 | } // namespace |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 183 | |
| 184 | } // namespace net |