OLD | NEW |
(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_buffer.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/string_tokenizer.h" |
| 10 #include "base/string_util.h" |
| 11 #include "net/base/net_errors.h" |
| 12 #include "net/ftp/ftp_directory_listing_parsers.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 TEST(FtpDirectoryListingBufferTest, Parse) { |
| 18 const char* test_files[] = { |
| 19 "dir-listing-ls-1", |
| 20 "dir-listing-ls-2", |
| 21 }; |
| 22 |
| 23 FilePath test_dir; |
| 24 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
| 25 test_dir = test_dir.AppendASCII("net"); |
| 26 test_dir = test_dir.AppendASCII("data"); |
| 27 test_dir = test_dir.AppendASCII("ftp"); |
| 28 |
| 29 for (size_t i = 0; i < arraysize(test_files); i++) { |
| 30 SCOPED_TRACE(StringPrintf("Test[%d]: %s", i, test_files[i])); |
| 31 |
| 32 net::FtpDirectoryListingBuffer buffer; |
| 33 |
| 34 std::string test_listing; |
| 35 EXPECT_TRUE(file_util::ReadFileToString(test_dir.AppendASCII(test_files[i]), |
| 36 &test_listing)); |
| 37 |
| 38 EXPECT_EQ(net::OK, buffer.ConsumeData(test_listing.data(), |
| 39 test_listing.length())); |
| 40 EXPECT_EQ(net::OK, buffer.ProcessRemainingData()); |
| 41 |
| 42 std::string expected_listing; |
| 43 ASSERT_TRUE(file_util::ReadFileToString( |
| 44 test_dir.AppendASCII(std::string(test_files[i]) + ".expected"), |
| 45 &expected_listing)); |
| 46 |
| 47 std::vector<std::string> lines; |
| 48 StringTokenizer tokenizer(expected_listing, "\r\n"); |
| 49 while (tokenizer.GetNext()) |
| 50 lines.push_back(tokenizer.token()); |
| 51 ASSERT_EQ(0U, lines.size() % 7); |
| 52 |
| 53 for (size_t i = 0; i < lines.size() / 7; i++) { |
| 54 std::string type(lines[7 * i]); |
| 55 std::string name(lines[7 * i + 1]); |
| 56 |
| 57 SCOPED_TRACE(StringPrintf("Filename: %s", name.c_str())); |
| 58 |
| 59 int year; |
| 60 if (lines[7 * i + 2] == "current") { |
| 61 base::Time::Exploded now_exploded; |
| 62 base::Time::Now().LocalExplode(&now_exploded); |
| 63 year = now_exploded.year; |
| 64 } else { |
| 65 year = StringToInt(lines[7 * i + 2]); |
| 66 } |
| 67 int month = StringToInt(lines[7 * i + 3]); |
| 68 int day_of_month = StringToInt(lines[7 * i + 4]); |
| 69 int hour = StringToInt(lines[7 * i + 5]); |
| 70 int minute = StringToInt(lines[7 * i + 6]); |
| 71 |
| 72 ASSERT_TRUE(buffer.EntryAvailable()); |
| 73 net::FtpDirectoryListingEntry entry = buffer.PopEntry(); |
| 74 |
| 75 if (type == "d") { |
| 76 EXPECT_EQ(net::FtpDirectoryListingEntry::DIRECTORY, entry.type); |
| 77 } else if (type == "-") { |
| 78 EXPECT_EQ(net::FtpDirectoryListingEntry::FILE, entry.type); |
| 79 } else if (type == "l") { |
| 80 EXPECT_EQ(net::FtpDirectoryListingEntry::SYMLINK, entry.type); |
| 81 } else { |
| 82 ADD_FAILURE() << "invalid gold test data: " << type; |
| 83 } |
| 84 |
| 85 EXPECT_EQ(UTF8ToUTF16(name), entry.name); |
| 86 |
| 87 base::Time::Exploded time_exploded; |
| 88 entry.last_modified.LocalExplode(&time_exploded); |
| 89 EXPECT_EQ(year, time_exploded.year); |
| 90 EXPECT_EQ(month, time_exploded.month); |
| 91 EXPECT_EQ(day_of_month, time_exploded.day_of_month); |
| 92 EXPECT_EQ(hour, time_exploded.hour); |
| 93 EXPECT_EQ(minute, time_exploded.minute); |
| 94 EXPECT_EQ(0, time_exploded.second); |
| 95 EXPECT_EQ(0, time_exploded.millisecond); |
| 96 } |
| 97 EXPECT_FALSE(buffer.EntryAvailable()); |
| 98 } |
| 99 } |
| 100 |
| 101 } // namespace |
OLD | NEW |