| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_parser.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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/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 { | 18 namespace net { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 class FtpDirectoryListingParserTest | 22 class FtpDirectoryListingParserTest |
| 23 : public testing::TestWithParam<const char*> { | 23 : public testing::TestWithParam<const char*> {}; |
| 24 }; | |
| 25 | 24 |
| 26 TEST_P(FtpDirectoryListingParserTest, Parse) { | 25 TEST_P(FtpDirectoryListingParserTest, Parse) { |
| 27 base::FilePath test_dir; | 26 base::FilePath test_dir; |
| 28 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); | 27 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
| 29 test_dir = test_dir.AppendASCII("net"); | 28 test_dir = test_dir.AppendASCII("net"); |
| 30 test_dir = test_dir.AppendASCII("data"); | 29 test_dir = test_dir.AppendASCII("data"); |
| 31 test_dir = test_dir.AppendASCII("ftp"); | 30 test_dir = test_dir.AppendASCII("ftp"); |
| 32 | 31 |
| 33 base::Time::Exploded mock_current_time_exploded = { 0 }; | 32 base::Time::Exploded mock_current_time_exploded = {0}; |
| 34 mock_current_time_exploded.year = 1994; | 33 mock_current_time_exploded.year = 1994; |
| 35 mock_current_time_exploded.month = 11; | 34 mock_current_time_exploded.month = 11; |
| 36 mock_current_time_exploded.day_of_month = 15; | 35 mock_current_time_exploded.day_of_month = 15; |
| 37 mock_current_time_exploded.hour = 12; | 36 mock_current_time_exploded.hour = 12; |
| 38 mock_current_time_exploded.minute = 45; | 37 mock_current_time_exploded.minute = 45; |
| 39 base::Time mock_current_time( | 38 base::Time mock_current_time( |
| 40 base::Time::FromLocalExploded(mock_current_time_exploded)); | 39 base::Time::FromLocalExploded(mock_current_time_exploded)); |
| 41 | 40 |
| 42 SCOPED_TRACE(base::StringPrintf("Test case: %s", GetParam())); | 41 SCOPED_TRACE(base::StringPrintf("Test case: %s", GetParam())); |
| 43 | 42 |
| 44 std::string test_listing; | 43 std::string test_listing; |
| 45 EXPECT_TRUE(base::ReadFileToString(test_dir.AppendASCII(GetParam()), | 44 EXPECT_TRUE( |
| 46 &test_listing)); | 45 base::ReadFileToString(test_dir.AppendASCII(GetParam()), &test_listing)); |
| 47 | 46 |
| 48 std::vector<FtpDirectoryListingEntry> entries; | 47 std::vector<FtpDirectoryListingEntry> entries; |
| 49 EXPECT_EQ(OK, ParseFtpDirectoryListing(test_listing, | 48 EXPECT_EQ( |
| 50 mock_current_time, | 49 OK, ParseFtpDirectoryListing(test_listing, mock_current_time, &entries)); |
| 51 &entries)); | |
| 52 | 50 |
| 53 std::string expected_listing; | 51 std::string expected_listing; |
| 54 ASSERT_TRUE(base::ReadFileToString( | 52 ASSERT_TRUE(base::ReadFileToString( |
| 55 test_dir.AppendASCII(std::string(GetParam()) + ".expected"), | 53 test_dir.AppendASCII(std::string(GetParam()) + ".expected"), |
| 56 &expected_listing)); | 54 &expected_listing)); |
| 57 | 55 |
| 58 std::vector<std::string> lines; | 56 std::vector<std::string> lines; |
| 59 base::SplitStringUsingSubstr(expected_listing, "\r\n", &lines); | 57 base::SplitStringUsingSubstr(expected_listing, "\r\n", &lines); |
| 60 | 58 |
| 61 // Special case for empty listings. | 59 // Special case for empty listings. |
| 62 if (lines.size() == 1 && lines[0].empty()) | 60 if (lines.size() == 1 && lines[0].empty()) |
| 63 lines.clear(); | 61 lines.clear(); |
| 64 | 62 |
| 65 ASSERT_EQ(9 * entries.size(), lines.size()); | 63 ASSERT_EQ(9 * entries.size(), lines.size()); |
| 66 | 64 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 entry.last_modified.LocalExplode(&time_exploded); | 96 entry.last_modified.LocalExplode(&time_exploded); |
| 99 EXPECT_EQ(year, time_exploded.year); | 97 EXPECT_EQ(year, time_exploded.year); |
| 100 EXPECT_EQ(month, time_exploded.month); | 98 EXPECT_EQ(month, time_exploded.month); |
| 101 EXPECT_EQ(day_of_month, time_exploded.day_of_month); | 99 EXPECT_EQ(day_of_month, time_exploded.day_of_month); |
| 102 EXPECT_EQ(hour, time_exploded.hour); | 100 EXPECT_EQ(hour, time_exploded.hour); |
| 103 EXPECT_EQ(minute, time_exploded.minute); | 101 EXPECT_EQ(minute, time_exploded.minute); |
| 104 } | 102 } |
| 105 } | 103 } |
| 106 | 104 |
| 107 const char* kTestFiles[] = { | 105 const char* kTestFiles[] = { |
| 108 "dir-listing-ls-1", | 106 "dir-listing-ls-1", "dir-listing-ls-1-utf8", "dir-listing-ls-2", |
| 109 "dir-listing-ls-1-utf8", | 107 "dir-listing-ls-3", "dir-listing-ls-4", "dir-listing-ls-5", |
| 110 "dir-listing-ls-2", | 108 "dir-listing-ls-6", "dir-listing-ls-7", "dir-listing-ls-8", |
| 111 "dir-listing-ls-3", | 109 "dir-listing-ls-9", "dir-listing-ls-10", "dir-listing-ls-11", |
| 112 "dir-listing-ls-4", | 110 "dir-listing-ls-12", "dir-listing-ls-13", "dir-listing-ls-14", |
| 113 "dir-listing-ls-5", | 111 "dir-listing-ls-15", "dir-listing-ls-16", "dir-listing-ls-17", |
| 114 "dir-listing-ls-6", | 112 "dir-listing-ls-18", "dir-listing-ls-19", |
| 115 "dir-listing-ls-7", | 113 "dir-listing-ls-20", // TODO(phajdan.jr): should use windows-1251 encoding. |
| 116 "dir-listing-ls-8", | 114 "dir-listing-ls-21", // TODO(phajdan.jr): should use windows-1251 encoding. |
| 117 "dir-listing-ls-9", | 115 "dir-listing-ls-22", // TODO(phajdan.jr): should use windows-1251 encoding. |
| 118 "dir-listing-ls-10", | 116 "dir-listing-ls-23", "dir-listing-ls-24", |
| 119 "dir-listing-ls-11", | |
| 120 "dir-listing-ls-12", | |
| 121 "dir-listing-ls-13", | |
| 122 "dir-listing-ls-14", | |
| 123 "dir-listing-ls-15", | |
| 124 "dir-listing-ls-16", | |
| 125 "dir-listing-ls-17", | |
| 126 "dir-listing-ls-18", | |
| 127 "dir-listing-ls-19", | |
| 128 "dir-listing-ls-20", // TODO(phajdan.jr): should use windows-1251 encoding. | |
| 129 "dir-listing-ls-21", // TODO(phajdan.jr): should use windows-1251 encoding. | |
| 130 "dir-listing-ls-22", // TODO(phajdan.jr): should use windows-1251 encoding. | |
| 131 "dir-listing-ls-23", | |
| 132 "dir-listing-ls-24", | |
| 133 | 117 |
| 134 // Tests for Russian listings. The only difference between those | 118 // Tests for Russian listings. The only difference between those |
| 135 // files is character encoding: | 119 // files is character encoding: |
| 136 "dir-listing-ls-25", // UTF-8 | 120 "dir-listing-ls-25", // UTF-8 |
| 137 "dir-listing-ls-26", // KOI8-R | 121 "dir-listing-ls-26", // KOI8-R |
| 138 "dir-listing-ls-27", // windows-1251 | 122 "dir-listing-ls-27", // windows-1251 |
| 139 | 123 "dir-listing-ls-28", // Hylafax FTP server |
| 140 "dir-listing-ls-28", // Hylafax FTP server | 124 "dir-listing-ls-29", "dir-listing-ls-30", "dir-listing-ls-31", |
| 141 "dir-listing-ls-29", | 125 "dir-listing-ls-32", // busybox |
| 142 "dir-listing-ls-30", | 126 "dir-listing-netware-1", "dir-listing-netware-2", |
| 143 "dir-listing-ls-31", | 127 "dir-listing-netware-3", // Spaces in file names. |
| 144 "dir-listing-ls-32", // busybox | 128 "dir-listing-os2-1", "dir-listing-vms-1", "dir-listing-vms-2", |
| 145 | 129 "dir-listing-vms-3", "dir-listing-vms-4", "dir-listing-vms-5", |
| 146 "dir-listing-netware-1", | 130 "dir-listing-vms-6", "dir-listing-vms-7", "dir-listing-vms-8", |
| 147 "dir-listing-netware-2", | 131 "dir-listing-windows-1", "dir-listing-windows-2", |
| 148 "dir-listing-netware-3", // Spaces in file names. | |
| 149 "dir-listing-os2-1", | |
| 150 "dir-listing-vms-1", | |
| 151 "dir-listing-vms-2", | |
| 152 "dir-listing-vms-3", | |
| 153 "dir-listing-vms-4", | |
| 154 "dir-listing-vms-5", | |
| 155 "dir-listing-vms-6", | |
| 156 "dir-listing-vms-7", | |
| 157 "dir-listing-vms-8", | |
| 158 "dir-listing-windows-1", | |
| 159 "dir-listing-windows-2", | |
| 160 }; | 132 }; |
| 161 | 133 |
| 162 INSTANTIATE_TEST_CASE_P(, FtpDirectoryListingParserTest, | 134 INSTANTIATE_TEST_CASE_P(, |
| 135 FtpDirectoryListingParserTest, |
| 163 testing::ValuesIn(kTestFiles)); | 136 testing::ValuesIn(kTestFiles)); |
| 164 | 137 |
| 165 } // namespace | 138 } // namespace |
| 166 | 139 |
| 167 } // namespace net | 140 } // namespace net |
| OLD | NEW |