Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(862)

Side by Side Diff: net/ftp/ftp_directory_listing_parsers_unittest.cc

Issue 465035: Split FTP LIST parsing code into individual files for each listing style. (Closed)
Patch Set: fix Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/ftp/ftp_directory_listing_parsers.cc ('k') | net/ftp/ftp_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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_directory_listing_parsers.h"
6
7 #include "base/format_macros.h"
8 #include "base/string_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 struct SingleLineTestData {
14 const char* input;
15 net::FtpDirectoryListingEntry::Type type;
16 const char* filename;
17 int64 size;
18 int year;
19 int month;
20 int day_of_month;
21 int hour;
22 int minute;
23 };
24
25 class FtpDirectoryListingParsersTest : public testing::Test {
26 protected:
27 FtpDirectoryListingParsersTest() {
28 }
29
30 void RunSingleLineTestCase(net::FtpDirectoryListingParser* parser,
31 const SingleLineTestData& test_case) {
32 ASSERT_TRUE(parser->ConsumeLine(UTF8ToUTF16(test_case.input)));
33 ASSERT_TRUE(parser->EntryAvailable());
34 net::FtpDirectoryListingEntry entry = parser->PopEntry();
35 EXPECT_EQ(test_case.type, entry.type);
36 EXPECT_EQ(UTF8ToUTF16(test_case.filename), entry.name);
37 EXPECT_EQ(test_case.size, entry.size);
38
39 base::Time::Exploded time_exploded;
40 entry.last_modified.LocalExplode(&time_exploded);
41 EXPECT_EQ(test_case.year, time_exploded.year);
42 EXPECT_EQ(test_case.month, time_exploded.month);
43 EXPECT_EQ(test_case.day_of_month, time_exploded.day_of_month);
44 EXPECT_EQ(test_case.hour, time_exploded.hour);
45 EXPECT_EQ(test_case.minute, time_exploded.minute);
46 EXPECT_EQ(0, time_exploded.second);
47 EXPECT_EQ(0, time_exploded.millisecond);
48 }
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(FtpDirectoryListingParsersTest);
52 };
53
54 TEST_F(FtpDirectoryListingParsersTest, Ls) {
55 base::Time::Exploded now_exploded;
56 base::Time::Now().LocalExplode(&now_exploded);
57
58 const struct SingleLineTestData good_cases[] = {
59 { "-rw-r--r-- 1 ftp ftp 528 Nov 01 2007 README",
60 net::FtpDirectoryListingEntry::FILE, "README", 528,
61 2007, 11, 1, 0, 0 },
62 { "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 directory",
63 net::FtpDirectoryListingEntry::DIRECTORY, "directory", -1,
64 now_exploded.year, 5, 15, 18, 11 },
65 { "lrwxrwxrwx 1 0 0 26 Sep 18 2008 pub -> vol/1/.CLUSTER/var_ftp/pub",
66 net::FtpDirectoryListingEntry::SYMLINK, "pub", -1,
67 2008, 9, 18, 0, 0 },
68 { "lrwxrwxrwx 1 0 0 3 Oct 12 13:37 mirror -> pub",
69 net::FtpDirectoryListingEntry::SYMLINK, "mirror", -1,
70 now_exploded.year, 10, 12, 13, 37 },
71 { "drwxrwsr-x 4 501 501 4096 Feb 20 2007 pub",
72 net::FtpDirectoryListingEntry::DIRECTORY, "pub", -1,
73 2007, 2, 20, 0, 0 },
74 { "drwxr-xr-x 4 (?) (?) 4096 Apr 8 2007 jigdo",
75 net::FtpDirectoryListingEntry::DIRECTORY, "jigdo", -1,
76 2007, 4, 8, 0, 0 },
77 { "drwx-wx-wt 2 root wheel 512 Jul 1 02:15 incoming",
78 net::FtpDirectoryListingEntry::DIRECTORY, "incoming", -1,
79 now_exploded.year, 7, 1, 2, 15 },
80 { "-rw-r--r-- 1 2 3 3447432 May 18 2009 Foo - Manual.pdf",
81 net::FtpDirectoryListingEntry::FILE, "Foo - Manual.pdf", 3447432,
82 2009, 5, 18, 0, 0 },
83 };
84 for (size_t i = 0; i < arraysize(good_cases); i++) {
85 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, good_cases[i].input));
86
87 net::FtpLsDirectoryListingParser parser;
88 RunSingleLineTestCase(&parser, good_cases[i]);
89 }
90
91 const char* bad_cases[] = {
92 "garbage",
93 "-rw-r--r-- 1 ftp ftp",
94 "-rw-r--rgb 1 ftp ftp 528 Nov 01 2007 README",
95 "-rw-rgbr-- 1 ftp ftp 528 Nov 01 2007 README",
96 "qrwwr--r-- 1 ftp ftp 528 Nov 01 2007 README",
97 "-rw-r--r-- -1 ftp ftp 528 Nov 01 2007 README",
98 "-rw-r--r-- 1 ftp ftp -528 Nov 01 2007 README",
99 "-rw-r--r-- 1 ftp ftp 528 Foo 01 2007 README",
100 };
101 for (size_t i = 0; i < arraysize(bad_cases); i++) {
102 net::FtpLsDirectoryListingParser parser;
103 EXPECT_FALSE(parser.ConsumeLine(UTF8ToUTF16(bad_cases[i]))) << bad_cases[i];
104 }
105 }
106
107 TEST_F(FtpDirectoryListingParsersTest, Windows) {
108 base::Time::Exploded now_exploded;
109 base::Time::Now().LocalExplode(&now_exploded);
110
111 const struct SingleLineTestData good_cases[] = {
112 { "11-02-09 05:32PM <DIR> NT",
113 net::FtpDirectoryListingEntry::DIRECTORY, "NT", -1,
114 2009, 11, 2, 17, 32 },
115 { "01-06-09 02:42PM 458 Readme.txt",
116 net::FtpDirectoryListingEntry::FILE, "Readme.txt", 458,
117 2009, 1, 6, 14, 42 },
118 { "01-06-09 02:42AM 1 Readme.txt",
119 net::FtpDirectoryListingEntry::FILE, "Readme.txt", 1,
120 2009, 1, 6, 2, 42 },
121 { "01-06-01 02:42AM 458 Readme.txt",
122 net::FtpDirectoryListingEntry::FILE, "Readme.txt", 458,
123 2001, 1, 6, 2, 42 },
124 { "01-06-00 02:42AM 458 Corner1.txt",
125 net::FtpDirectoryListingEntry::FILE, "Corner1.txt", 458,
126 2000, 1, 6, 2, 42 },
127 { "01-06-99 02:42AM 458 Corner2.txt",
128 net::FtpDirectoryListingEntry::FILE, "Corner2.txt", 458,
129 1999, 1, 6, 2, 42 },
130 { "01-06-80 02:42AM 458 Corner3.txt",
131 net::FtpDirectoryListingEntry::FILE, "Corner3.txt", 458,
132 1980, 1, 6, 2, 42 },
133 #if !defined(OS_LINUX)
134 // TODO(phajdan.jr): Re-enable when 2038-year problem is fixed on Linux.
135 { "01-06-79 02:42AM 458 Corner4",
136 net::FtpDirectoryListingEntry::FILE, "Corner4", 458,
137 2079, 1, 6, 2, 42 },
138 #endif // !defined (OS_LINUX)
139 { "01-06-1979 02:42AM 458 Readme.txt",
140 net::FtpDirectoryListingEntry::FILE, "Readme.txt", 458,
141 1979, 1, 6, 2, 42 },
142 };
143 for (size_t i = 0; i < arraysize(good_cases); i++) {
144 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, good_cases[i].input));
145
146 net::FtpWindowsDirectoryListingParser parser;
147 RunSingleLineTestCase(&parser, good_cases[i]);
148 }
149
150 const char* bad_cases[] = {
151 "",
152 "garbage",
153 "11-02-09 05:32PM <GARBAGE> NT",
154 "11-02-09 05:32 <DIR> NT",
155 "11-FEB-09 05:32PM <DIR> NT",
156 "11-02 05:32PM <DIR> NT",
157 "11-02-09 05:32PM -1 NT",
158 };
159 for (size_t i = 0; i < arraysize(bad_cases); i++) {
160 net::FtpWindowsDirectoryListingParser parser;
161 EXPECT_FALSE(parser.ConsumeLine(UTF8ToUTF16(bad_cases[i]))) << bad_cases[i];
162 }
163 }
164
165 TEST_F(FtpDirectoryListingParsersTest, Vms) {
166 const struct SingleLineTestData good_cases[] = {
167 { "README.TXT;4 2 18-APR-2000 10:40:39.90",
168 net::FtpDirectoryListingEntry::FILE, "readme.txt", 1024,
169 2000, 4, 18, 10, 40 },
170 { ".WELCOME;1 2 13-FEB-2002 23:32:40.47",
171 net::FtpDirectoryListingEntry::FILE, ".welcome", 1024,
172 2002, 2, 13, 23, 32 },
173 { "FILE.;1 2 13-FEB-2002 23:32:40.47",
174 net::FtpDirectoryListingEntry::FILE, "file.", 1024,
175 2002, 2, 13, 23, 32 },
176 { "EXAMPLE.TXT;1 1 4-NOV-2009 06:02 [JOHNDOE] (RWED,RWED,,)",
177 net::FtpDirectoryListingEntry::FILE, "example.txt", 512,
178 2009, 11, 4, 6, 2 },
179 { "ANNOUNCE.TXT;2 1/16 12-MAR-2005 08:44:57 [SYSTEM] (RWED,RWED,RE,RE)",
180 net::FtpDirectoryListingEntry::FILE, "announce.txt", 512,
181 2005, 3, 12, 8, 44 },
182 { "TEST.DIR;1 1 4-MAR-1999 22:14:34 [UCX$NOBO,ANONYMOUS] (RWE,RWE,RWE,RWE)",
183 net::FtpDirectoryListingEntry::DIRECTORY, "test", -1,
184 1999, 3, 4, 22, 14 },
185 { "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (,,,)",
186 net::FtpDirectoryListingEntry::FILE, "announce.txt", 512,
187 2005, 3, 12, 8, 44 },
188 { "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (R,RW,RWD,RE)",
189 net::FtpDirectoryListingEntry::FILE, "announce.txt", 512,
190 2005, 3, 12, 8, 44 },
191 { "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (ED,RED,WD,WED)",
192 net::FtpDirectoryListingEntry::FILE, "announce.txt", 512,
193 2005, 3, 12, 8, 44 },
194 };
195 for (size_t i = 0; i < arraysize(good_cases); i++) {
196 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, good_cases[i].input));
197
198 net::FtpVmsDirectoryListingParser parser;
199 ASSERT_TRUE(
200 parser.ConsumeLine(ASCIIToUTF16("Directory ANONYMOUS_ROOT:[000000]")));
201 RunSingleLineTestCase(&parser, good_cases[i]);
202 }
203
204 const char* bad_cases[] = {
205 "Directory ROOT|garbage",
206
207 // Missing file version number.
208 "Directory ROOT|README.TXT 2 18-APR-2000 10:40:39",
209
210 // Missing extension.
211 "Directory ROOT|README;1 2 18-APR-2000 10:40:39",
212
213 // Malformed file size.
214 "Directory ROOT|README.TXT;1 garbage 18-APR-2000 10:40:39",
215 "Directory ROOT|README.TXT;1 -2 18-APR-2000 10:40:39",
216
217 // Malformed date.
218 "Directory ROOT|README.TXT;1 2 APR-2000 10:40:39",
219 "Directory ROOT|README.TXT;1 2 -18-APR-2000 10:40:39",
220 "Directory ROOT|README.TXT;1 2 18-APR 10:40:39",
221 "Directory ROOT|README.TXT;1 2 18-APR-2000 10",
222 "Directory ROOT|README.TXT;1 2 18-APR-2000 10:40.25",
223 "Directory ROOT|README.TXT;1 2 18-APR-2000 10:40.25.25",
224
225 // Empty line inside the listing.
226 "Directory ROOT|README.TXT;1 2 18-APR-2000 10:40:42"
227 "||README.TXT;1 2 18-APR-2000 10:40:42",
228
229 // Data after footer.
230 "Directory ROOT|README.TXT;4 2 18-APR-2000 10:40:39"
231 "||Total of 1 file|",
232 "Directory ROOT|README.TXT;4 2 18-APR-2000 10:40:39"
233 "||Total of 1 file|garbage",
234 "Directory ROOT|README.TXT;4 2 18-APR-2000 10:40:39"
235 "||Total of 1 file|Total of 1 file",
236
237 // Malformed security information.
238 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 (RWED,RWED,RE,RE)",
239 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 [SYSTEM]",
240 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 (SYSTEM) (RWED,RWED,RE,RE)",
241 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 [SYSTEM] [RWED,RWED,RE,RE]",
242 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED)",
243 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWED,RE,RE,RE)",
244 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWEDRWED,RE,RE)",
245 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,DEWR,RE,RE)",
246 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWED,Q,RE)",
247 "Directory ROOT|X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RRWWEEDD,RE,RE)",
248 };
249 for (size_t i = 0; i < arraysize(bad_cases); i++) {
250 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, bad_cases[i]));
251
252 std::vector<std::string> lines;
253 SplitString(bad_cases[i], '|', &lines);
254 net::FtpVmsDirectoryListingParser parser;
255 bool failed = false;
256 for (std::vector<std::string>::const_iterator i = lines.begin();
257 i != lines.end(); ++i) {
258 if (!parser.ConsumeLine(UTF8ToUTF16(*i))) {
259 failed = true;
260 break;
261 }
262 }
263 EXPECT_TRUE(failed);
264 }
265 }
266
267 } // namespace
OLDNEW
« no previous file with comments | « net/ftp/ftp_directory_listing_parsers.cc ('k') | net/ftp/ftp_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698