summaryrefslogtreecommitdiffstats
path: root/net/ftp/ftp_ctrl_response_buffer_unittest.cc
blob: 1477c0989c90aef5d516148db144c39151dab5fd (plain)
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
// 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_ctrl_response_buffer.h"

#include <string.h>

#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class FtpCtrlResponseBufferTest : public testing::Test {
 protected:
  int PushDataToBuffer(const char* data) {
    return buffer_.ConsumeData(data, strlen(data));
  }

  net::FtpCtrlResponseBuffer buffer_;
};

TEST_F(FtpCtrlResponseBufferTest, Basic) {
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("200 Status Text\r\n"));
  EXPECT_TRUE(buffer_.ResponseAvailable());

  net::FtpCtrlResponse response = buffer_.PopResponse();
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(200, response.status_code);
  ASSERT_EQ(1U, response.lines.size());
  EXPECT_EQ("Status Text", response.lines[0]);
}

TEST_F(FtpCtrlResponseBufferTest, Chunks) {
  EXPECT_EQ(net::OK, PushDataToBuffer("20"));
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(net::OK, PushDataToBuffer("0 Status"));
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(net::OK, PushDataToBuffer(" Text"));
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(net::OK, PushDataToBuffer("\r"));
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(net::OK, PushDataToBuffer("\n"));
  EXPECT_TRUE(buffer_.ResponseAvailable());

  net::FtpCtrlResponse response = buffer_.PopResponse();
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(200, response.status_code);
  ASSERT_EQ(1U, response.lines.size());
  EXPECT_EQ("Status Text", response.lines[0]);
}

TEST_F(FtpCtrlResponseBufferTest, Continuation) {
  EXPECT_EQ(net::OK, PushDataToBuffer("230-FirstLine\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("230-SecondLine\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("230 LastLine\r\n"));
  EXPECT_TRUE(buffer_.ResponseAvailable());

  net::FtpCtrlResponse response = buffer_.PopResponse();
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(230, response.status_code);
  ASSERT_EQ(3U, response.lines.size());
  EXPECT_EQ("FirstLine", response.lines[0]);
  EXPECT_EQ("SecondLine", response.lines[1]);
  EXPECT_EQ("LastLine", response.lines[2]);
}

TEST_F(FtpCtrlResponseBufferTest, MultilineContinuation) {
  EXPECT_EQ(net::OK, PushDataToBuffer("230-FirstLine\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("Continued\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("230-SecondLine\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("215 Continued\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("230 LastLine\r\n"));
  EXPECT_TRUE(buffer_.ResponseAvailable());

  net::FtpCtrlResponse response = buffer_.PopResponse();
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(230, response.status_code);
  ASSERT_EQ(3U, response.lines.size());
  EXPECT_EQ("FirstLineContinued", response.lines[0]);
  EXPECT_EQ("SecondLine215 Continued", response.lines[1]);
  EXPECT_EQ("LastLine", response.lines[2]);
}

TEST_F(FtpCtrlResponseBufferTest, MultilineContinuationZeroLength) {
  // For the corner case from bug 29322.
  EXPECT_EQ(net::OK, PushDataToBuffer("230-\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("example.com\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("230 LastLine\r\n"));
  EXPECT_TRUE(buffer_.ResponseAvailable());

  net::FtpCtrlResponse response = buffer_.PopResponse();
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(230, response.status_code);
  ASSERT_EQ(2U, response.lines.size());
  EXPECT_EQ("example.com", response.lines[0]);
  EXPECT_EQ("LastLine", response.lines[1]);
}

TEST_F(FtpCtrlResponseBufferTest, SimilarContinuation) {
  EXPECT_EQ(net::OK, PushDataToBuffer("230-FirstLine\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  // Notice the space at the start of the line. It should be recognized
  // as a continuation, and not the last line.
  EXPECT_EQ(net::OK, PushDataToBuffer(" 230 Continued\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("230 TrueLastLine\r\n"));
  EXPECT_TRUE(buffer_.ResponseAvailable());

  net::FtpCtrlResponse response = buffer_.PopResponse();
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(230, response.status_code);
  ASSERT_EQ(2U, response.lines.size());
  EXPECT_EQ("FirstLine 230 Continued", response.lines[0]);
  EXPECT_EQ("TrueLastLine", response.lines[1]);
}

// The nesting of multi-line responses is not allowed.
TEST_F(FtpCtrlResponseBufferTest, NoNesting) {
  EXPECT_EQ(net::OK, PushDataToBuffer("230-FirstLine\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("300-Continuation\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("300 Still continuation\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());

  EXPECT_EQ(net::OK, PushDataToBuffer("230 Real End\r\n"));
  ASSERT_TRUE(buffer_.ResponseAvailable());

  net::FtpCtrlResponse response = buffer_.PopResponse();
  EXPECT_FALSE(buffer_.ResponseAvailable());
  EXPECT_EQ(230, response.status_code);
  ASSERT_EQ(2U, response.lines.size());
  EXPECT_EQ("FirstLine300-Continuation300 Still continuation",
            response.lines[0]);
  EXPECT_EQ("Real End", response.lines[1]);
}

TEST_F(FtpCtrlResponseBufferTest, NonNumericResponse) {
  EXPECT_EQ(net::ERR_INVALID_RESPONSE, PushDataToBuffer("Non-numeric\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());
}

TEST_F(FtpCtrlResponseBufferTest, OutOfRangeResponse) {
  EXPECT_EQ(net::ERR_INVALID_RESPONSE, PushDataToBuffer("777 OK?\r\n"));
  EXPECT_FALSE(buffer_.ResponseAvailable());
}

}  // namespace