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

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

Issue 6670085: FTP: Detect the character encoding only after the entire listing is received. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test coverage Created 9 years, 8 months 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 | Annotate | Revision Log
OLDNEW
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
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
97 ASSERT_EQ(8 * entries.size(), lines.size());
96 98
97 for (size_t i = 0; i < lines.size() / 8; i++) { 99 for (size_t i = 0; i < lines.size() / 8; i++) {
98 std::string type(lines[8 * i]); 100 std::string type(lines[8 * i]);
99 std::string name(lines[8 * i + 1]); 101 std::string name(lines[8 * i + 1]);
100 int64 size; 102 int64 size;
101 base::StringToInt64(lines[8 * i + 2], &size); 103 base::StringToInt64(lines[8 * i + 2], &size);
102 104
103 SCOPED_TRACE(base::StringPrintf("Filename: %s", name.c_str())); 105 SCOPED_TRACE(base::StringPrintf("Filename: %s", name.c_str()));
104 106
105 int year, month, day_of_month, hour, minute; 107 int year, month, day_of_month, hour, minute;
106 base::StringToInt(lines[8 * i + 3], &year); 108 base::StringToInt(lines[8 * i + 3], &year);
107 base::StringToInt(lines[8 * i + 4], &month); 109 base::StringToInt(lines[8 * i + 4], &month);
108 base::StringToInt(lines[8 * i + 5], &day_of_month); 110 base::StringToInt(lines[8 * i + 5], &day_of_month);
109 base::StringToInt(lines[8 * i + 6], &hour); 111 base::StringToInt(lines[8 * i + 6], &hour);
110 base::StringToInt(lines[8 * i + 7], &minute); 112 base::StringToInt(lines[8 * i + 7], &minute);
111 113
112 ASSERT_TRUE(buffer.EntryAvailable()); 114 const FtpDirectoryListingEntry& entry = entries[i];
113 net::FtpDirectoryListingEntry entry = buffer.PopEntry();
114 115
115 if (type == "d") { 116 if (type == "d") {
116 EXPECT_EQ(net::FtpDirectoryListingEntry::DIRECTORY, entry.type); 117 EXPECT_EQ(FtpDirectoryListingEntry::DIRECTORY, entry.type);
117 } else if (type == "-") { 118 } else if (type == "-") {
118 EXPECT_EQ(net::FtpDirectoryListingEntry::FILE, entry.type); 119 EXPECT_EQ(FtpDirectoryListingEntry::FILE, entry.type);
119 } else if (type == "l") { 120 } else if (type == "l") {
120 EXPECT_EQ(net::FtpDirectoryListingEntry::SYMLINK, entry.type); 121 EXPECT_EQ(FtpDirectoryListingEntry::SYMLINK, entry.type);
121 } else { 122 } else {
122 ADD_FAILURE() << "invalid gold test data: " << type; 123 ADD_FAILURE() << "invalid gold test data: " << type;
123 } 124 }
124 125
125 EXPECT_EQ(UTF8ToUTF16(name), entry.name); 126 EXPECT_EQ(UTF8ToUTF16(name), entry.name);
126 EXPECT_EQ(size, entry.size); 127 EXPECT_EQ(size, entry.size);
127 128
128 base::Time::Exploded time_exploded; 129 base::Time::Exploded time_exploded;
129 entry.last_modified.LocalExplode(&time_exploded); 130 entry.last_modified.LocalExplode(&time_exploded);
130 EXPECT_EQ(year, time_exploded.year); 131 EXPECT_EQ(year, time_exploded.year);
131 EXPECT_EQ(month, time_exploded.month); 132 EXPECT_EQ(month, time_exploded.month);
132 EXPECT_EQ(day_of_month, time_exploded.day_of_month); 133 EXPECT_EQ(day_of_month, time_exploded.day_of_month);
133 EXPECT_EQ(hour, time_exploded.hour); 134 EXPECT_EQ(hour, time_exploded.hour);
134 EXPECT_EQ(minute, time_exploded.minute); 135 EXPECT_EQ(minute, time_exploded.minute);
135 } 136 }
136 EXPECT_FALSE(buffer.EntryAvailable());
137 } 137 }
138 } 138 }
139 139
140 } // namespace 140 } // namespace
141
142 } // namespace net
OLDNEW
« no previous file with comments | « net/ftp/ftp_directory_listing_parser_unittest.h ('k') | net/ftp/ftp_directory_listing_parser_vms.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698