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 3// found in the LICENSE file. 4 5#include "net/ftp/ftp_directory_listing_parser.h" 6 7#include "base/file_util.h" 8#include "base/format_macros.h" 9#include "base/path_service.h" 10#include "base/string_number_conversions.h" 11#include "base/string_tokenizer.h" 12#include "base/string_util.h" 13#include "base/utf_string_conversions.h" 14#include "net/base/net_errors.h" 15#include "net/ftp/ftp_directory_listing_parser.h" 16#include "testing/gtest/include/gtest/gtest.h" 17 18namespace net { 19 20namespace { 21 22TEST(FtpDirectoryListingBufferTest, Parse) { 23 const char* test_files[] = { 24 "dir-listing-ls-1", 25 "dir-listing-ls-1-utf8", 26 "dir-listing-ls-2", 27 "dir-listing-ls-3", 28 "dir-listing-ls-4", 29 "dir-listing-ls-5", 30 "dir-listing-ls-6", 31 "dir-listing-ls-7", 32 "dir-listing-ls-8", 33 "dir-listing-ls-9", 34 "dir-listing-ls-10", 35 "dir-listing-ls-11", 36 "dir-listing-ls-12", 37 "dir-listing-ls-13", 38 "dir-listing-ls-14", 39 "dir-listing-ls-15", 40 "dir-listing-ls-16", 41 "dir-listing-ls-17", 42 "dir-listing-ls-18", 43 "dir-listing-ls-19", 44 "dir-listing-ls-20", // TODO(phajdan.jr): should use windows-1251 encoding. 45 "dir-listing-ls-21", // TODO(phajdan.jr): should use windows-1251 encoding. 46 "dir-listing-ls-22", // TODO(phajdan.jr): should use windows-1251 encoding. 47 "dir-listing-ls-23", 48 "dir-listing-ls-24", 49 50 // Tests for Russian listings. The only difference between those 51 // files is character encoding: 52 "dir-listing-ls-25", // UTF-8 53 "dir-listing-ls-26", // KOI8-R 54 "dir-listing-ls-27", // windows-1251 55 56 "dir-listing-netware-1", 57 "dir-listing-netware-2", 58 "dir-listing-vms-1", 59 "dir-listing-vms-2", 60 "dir-listing-vms-3", 61 "dir-listing-vms-4", 62 "dir-listing-vms-5", 63 "dir-listing-windows-1", 64 "dir-listing-windows-2", 65 }; 66 67 FilePath test_dir; 68 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); 69 test_dir = test_dir.AppendASCII("net"); 70 test_dir = test_dir.AppendASCII("data"); 71 test_dir = test_dir.AppendASCII("ftp"); 72 73 base::Time::Exploded mock_current_time_exploded = { 0 }; 74 mock_current_time_exploded.year = 1994; 75 mock_current_time_exploded.month = 11; 76 mock_current_time_exploded.day_of_month = 15; 77 mock_current_time_exploded.hour = 12; 78 mock_current_time_exploded.minute = 45; 79 base::Time mock_current_time( 80 base::Time::FromLocalExploded(mock_current_time_exploded)); 81 82 for (size_t i = 0; i < arraysize(test_files); i++) { 83 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, test_files[i])); 84 85 std::string test_listing; 86 EXPECT_TRUE(file_util::ReadFileToString(test_dir.AppendASCII(test_files[i]), 87 &test_listing)); 88 89 std::vector<FtpDirectoryListingEntry> entries; 90 EXPECT_EQ(OK, ParseFtpDirectoryListing(test_listing, 91 mock_current_time, 92 &entries)); 93 94 std::string expected_listing; 95 ASSERT_TRUE(file_util::ReadFileToString( 96 test_dir.AppendASCII(std::string(test_files[i]) + ".expected"), 97 &expected_listing)); 98 99 std::vector<std::string> lines; 100 StringTokenizer tokenizer(expected_listing, "\r\n"); 101 while (tokenizer.GetNext()) 102 lines.push_back(tokenizer.token()); 103 104 ASSERT_EQ(8 * entries.size(), lines.size()); 105 106 for (size_t i = 0; i < lines.size() / 8; i++) { 107 std::string type(lines[8 * i]); 108 std::string name(lines[8 * i + 1]); 109 int64 size; 110 base::StringToInt64(lines[8 * i + 2], &size); 111 112 SCOPED_TRACE(base::StringPrintf("Filename: %s", name.c_str())); 113 114 int year, month, day_of_month, hour, minute; 115 base::StringToInt(lines[8 * i + 3], &year); 116 base::StringToInt(lines[8 * i + 4], &month); 117 base::StringToInt(lines[8 * i + 5], &day_of_month); 118 base::StringToInt(lines[8 * i + 6], &hour); 119 base::StringToInt(lines[8 * i + 7], &minute); 120 121 const FtpDirectoryListingEntry& entry = entries[i]; 122 123 if (type == "d") { 124 EXPECT_EQ(FtpDirectoryListingEntry::DIRECTORY, entry.type); 125 } else if (type == "-") { 126 EXPECT_EQ(FtpDirectoryListingEntry::FILE, entry.type); 127 } else if (type == "l") { 128 EXPECT_EQ(FtpDirectoryListingEntry::SYMLINK, entry.type); 129 } else { 130 ADD_FAILURE() << "invalid gold test data: " << type; 131 } 132 133 EXPECT_EQ(UTF8ToUTF16(name), entry.name); 134 EXPECT_EQ(size, entry.size); 135 136 base::Time::Exploded time_exploded; 137 entry.last_modified.LocalExplode(&time_exploded); 138 EXPECT_EQ(year, time_exploded.year); 139 EXPECT_EQ(month, time_exploded.month); 140 EXPECT_EQ(day_of_month, time_exploded.day_of_month); 141 EXPECT_EQ(hour, time_exploded.hour); 142 EXPECT_EQ(minute, time_exploded.minute); 143 } 144 } 145} 146 147} // namespace 148 149} // namespace net 150