OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/ftp/ftp_directory_listing_buffer.h" | 5 #include "net/ftp/ftp_directory_listing_parser.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/string_tokenizer.h" | 11 #include "base/string_tokenizer.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/ftp/ftp_directory_listing_parser.h" | 15 #include "net/ftp/ftp_directory_listing_parser.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace net { | |
19 | |
18 namespace { | 20 namespace { |
19 | 21 |
20 TEST(FtpDirectoryListingBufferTest, Parse) { | 22 TEST(FtpDirectoryListingBufferTest, Parse) { |
21 const char* test_files[] = { | 23 const char* test_files[] = { |
22 "dir-listing-ls-1", | 24 "dir-listing-ls-1", |
23 "dir-listing-ls-1-utf8", | 25 "dir-listing-ls-1-utf8", |
24 "dir-listing-ls-2", | 26 "dir-listing-ls-2", |
25 "dir-listing-ls-3", | 27 "dir-listing-ls-3", |
26 "dir-listing-ls-4", | 28 "dir-listing-ls-4", |
27 "dir-listing-ls-5", | 29 "dir-listing-ls-5", |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 mock_current_time_exploded.month = 11; | 68 mock_current_time_exploded.month = 11; |
67 mock_current_time_exploded.day_of_month = 15; | 69 mock_current_time_exploded.day_of_month = 15; |
68 mock_current_time_exploded.hour = 12; | 70 mock_current_time_exploded.hour = 12; |
69 mock_current_time_exploded.minute = 45; | 71 mock_current_time_exploded.minute = 45; |
70 base::Time mock_current_time( | 72 base::Time mock_current_time( |
71 base::Time::FromLocalExploded(mock_current_time_exploded)); | 73 base::Time::FromLocalExploded(mock_current_time_exploded)); |
72 | 74 |
73 for (size_t i = 0; i < arraysize(test_files); i++) { | 75 for (size_t i = 0; i < arraysize(test_files); i++) { |
74 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, test_files[i])); | 76 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, test_files[i])); |
75 | 77 |
76 net::FtpDirectoryListingBuffer buffer(mock_current_time); | |
77 | |
78 std::string test_listing; | 78 std::string test_listing; |
79 EXPECT_TRUE(file_util::ReadFileToString(test_dir.AppendASCII(test_files[i]), | 79 EXPECT_TRUE(file_util::ReadFileToString(test_dir.AppendASCII(test_files[i]), |
80 &test_listing)); | 80 &test_listing)); |
81 | 81 |
82 EXPECT_EQ(net::OK, buffer.ConsumeData(test_listing.data(), | 82 std::vector<FtpDirectoryListingEntry> entries; |
83 test_listing.length())); | 83 EXPECT_EQ(OK, ParseFtpDirectoryListing(test_listing, |
84 EXPECT_EQ(net::OK, buffer.ProcessRemainingData()); | 84 mock_current_time, |
85 &entries)); | |
85 | 86 |
86 std::string expected_listing; | 87 std::string expected_listing; |
87 ASSERT_TRUE(file_util::ReadFileToString( | 88 ASSERT_TRUE(file_util::ReadFileToString( |
88 test_dir.AppendASCII(std::string(test_files[i]) + ".expected"), | 89 test_dir.AppendASCII(std::string(test_files[i]) + ".expected"), |
89 &expected_listing)); | 90 &expected_listing)); |
90 | 91 |
91 std::vector<std::string> lines; | 92 std::vector<std::string> lines; |
92 StringTokenizer tokenizer(expected_listing, "\r\n"); | 93 StringTokenizer tokenizer(expected_listing, "\r\n"); |
93 while (tokenizer.GetNext()) | 94 while (tokenizer.GetNext()) |
94 lines.push_back(tokenizer.token()); | 95 lines.push_back(tokenizer.token()); |
95 ASSERT_EQ(0U, lines.size() % 8); | 96 ASSERT_EQ(0U, lines.size() % 8); |
96 | 97 |
98 ASSERT_EQ(lines.size() / 8, entries.size()); | |
eroman
2011/03/24 23:09:35
nit: these two asserts could be combined into one:
Paweł Hajdan Jr.
2011/03/26 09:47:50
Done.
| |
99 | |
97 for (size_t i = 0; i < lines.size() / 8; i++) { | 100 for (size_t i = 0; i < lines.size() / 8; i++) { |
98 std::string type(lines[8 * i]); | 101 std::string type(lines[8 * i]); |
99 std::string name(lines[8 * i + 1]); | 102 std::string name(lines[8 * i + 1]); |
100 int64 size; | 103 int64 size; |
101 base::StringToInt64(lines[8 * i + 2], &size); | 104 base::StringToInt64(lines[8 * i + 2], &size); |
102 | 105 |
103 SCOPED_TRACE(base::StringPrintf("Filename: %s", name.c_str())); | 106 SCOPED_TRACE(base::StringPrintf("Filename: %s", name.c_str())); |
104 | 107 |
105 int year, month, day_of_month, hour, minute; | 108 int year, month, day_of_month, hour, minute; |
106 base::StringToInt(lines[8 * i + 3], &year); | 109 base::StringToInt(lines[8 * i + 3], &year); |
107 base::StringToInt(lines[8 * i + 4], &month); | 110 base::StringToInt(lines[8 * i + 4], &month); |
108 base::StringToInt(lines[8 * i + 5], &day_of_month); | 111 base::StringToInt(lines[8 * i + 5], &day_of_month); |
109 base::StringToInt(lines[8 * i + 6], &hour); | 112 base::StringToInt(lines[8 * i + 6], &hour); |
110 base::StringToInt(lines[8 * i + 7], &minute); | 113 base::StringToInt(lines[8 * i + 7], &minute); |
111 | 114 |
112 ASSERT_TRUE(buffer.EntryAvailable()); | 115 FtpDirectoryListingEntry entry = entries[i]; |
eroman
2011/03/24 23:09:35
nit: could make this |const FtpDirectoryListingEnt
Paweł Hajdan Jr.
2011/03/26 09:47:50
Done.
| |
113 net::FtpDirectoryListingEntry entry = buffer.PopEntry(); | |
114 | 116 |
115 if (type == "d") { | 117 if (type == "d") { |
116 EXPECT_EQ(net::FtpDirectoryListingEntry::DIRECTORY, entry.type); | 118 EXPECT_EQ(FtpDirectoryListingEntry::DIRECTORY, entry.type); |
117 } else if (type == "-") { | 119 } else if (type == "-") { |
118 EXPECT_EQ(net::FtpDirectoryListingEntry::FILE, entry.type); | 120 EXPECT_EQ(FtpDirectoryListingEntry::FILE, entry.type); |
119 } else if (type == "l") { | 121 } else if (type == "l") { |
120 EXPECT_EQ(net::FtpDirectoryListingEntry::SYMLINK, entry.type); | 122 EXPECT_EQ(FtpDirectoryListingEntry::SYMLINK, entry.type); |
121 } else { | 123 } else { |
122 ADD_FAILURE() << "invalid gold test data: " << type; | 124 ADD_FAILURE() << "invalid gold test data: " << type; |
123 } | 125 } |
124 | 126 |
125 EXPECT_EQ(UTF8ToUTF16(name), entry.name); | 127 EXPECT_EQ(UTF8ToUTF16(name), entry.name); |
126 EXPECT_EQ(size, entry.size); | 128 EXPECT_EQ(size, entry.size); |
127 | 129 |
128 base::Time::Exploded time_exploded; | 130 base::Time::Exploded time_exploded; |
129 entry.last_modified.LocalExplode(&time_exploded); | 131 entry.last_modified.LocalExplode(&time_exploded); |
130 EXPECT_EQ(year, time_exploded.year); | 132 EXPECT_EQ(year, time_exploded.year); |
131 EXPECT_EQ(month, time_exploded.month); | 133 EXPECT_EQ(month, time_exploded.month); |
132 EXPECT_EQ(day_of_month, time_exploded.day_of_month); | 134 EXPECT_EQ(day_of_month, time_exploded.day_of_month); |
133 EXPECT_EQ(hour, time_exploded.hour); | 135 EXPECT_EQ(hour, time_exploded.hour); |
134 EXPECT_EQ(minute, time_exploded.minute); | 136 EXPECT_EQ(minute, time_exploded.minute); |
135 } | 137 } |
136 EXPECT_FALSE(buffer.EntryAvailable()); | |
137 } | 138 } |
138 } | 139 } |
139 | 140 |
140 } // namespace | 141 } // namespace |
142 | |
143 } // namespace net | |
OLD | NEW |