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
3 // found in the LICENSE file.
5 #include "net/ftp/ftp_directory_listing_parser.h"
7 #include "base/file_util.h"
8 #include "base/format_macros.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/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"
22 class FtpDirectoryListingParserTest
23 : public testing::TestWithParam
<const char*> {
26 TEST_P(FtpDirectoryListingParserTest
, Parse
) {
27 base::FilePath test_dir
;
28 PathService::Get(base::DIR_SOURCE_ROOT
, &test_dir
);
29 test_dir
= test_dir
.AppendASCII("net");
30 test_dir
= test_dir
.AppendASCII("data");
31 test_dir
= test_dir
.AppendASCII("ftp");
33 base::Time::Exploded mock_current_time_exploded
= { 0 };
34 mock_current_time_exploded
.year
= 1994;
35 mock_current_time_exploded
.month
= 11;
36 mock_current_time_exploded
.day_of_month
= 15;
37 mock_current_time_exploded
.hour
= 12;
38 mock_current_time_exploded
.minute
= 45;
39 base::Time
mock_current_time(
40 base::Time::FromLocalExploded(mock_current_time_exploded
));
42 SCOPED_TRACE(base::StringPrintf("Test case: %s", GetParam()));
44 std::string test_listing
;
45 EXPECT_TRUE(base::ReadFileToString(test_dir
.AppendASCII(GetParam()),
48 std::vector
<FtpDirectoryListingEntry
> entries
;
49 EXPECT_EQ(OK
, ParseFtpDirectoryListing(test_listing
,
53 std::string expected_listing
;
54 ASSERT_TRUE(base::ReadFileToString(
55 test_dir
.AppendASCII(std::string(GetParam()) + ".expected"),
58 std::vector
<std::string
> lines
;
59 base::SplitStringUsingSubstr(expected_listing
, "\r\n", &lines
);
61 // Special case for empty listings.
62 if (lines
.size() == 1 && lines
[0].empty())
65 ASSERT_EQ(9 * entries
.size(), lines
.size());
67 for (size_t i
= 0; i
< lines
.size() / 9; i
++) {
68 std::string
type(lines
[9 * i
]);
69 std::string
name(lines
[9 * i
+ 1]);
71 base::StringToInt64(lines
[9 * i
+ 2], &size
);
73 SCOPED_TRACE(base::StringPrintf("Filename: %s", name
.c_str()));
75 int year
, month
, day_of_month
, hour
, minute
;
76 base::StringToInt(lines
[9 * i
+ 3], &year
);
77 base::StringToInt(lines
[9 * i
+ 4], &month
);
78 base::StringToInt(lines
[9 * i
+ 5], &day_of_month
);
79 base::StringToInt(lines
[9 * i
+ 6], &hour
);
80 base::StringToInt(lines
[9 * i
+ 7], &minute
);
82 const FtpDirectoryListingEntry
& entry
= entries
[i
];
85 EXPECT_EQ(FtpDirectoryListingEntry::DIRECTORY
, entry
.type
);
86 } else if (type
== "-") {
87 EXPECT_EQ(FtpDirectoryListingEntry::FILE, entry
.type
);
88 } else if (type
== "l") {
89 EXPECT_EQ(FtpDirectoryListingEntry::SYMLINK
, entry
.type
);
91 ADD_FAILURE() << "invalid gold test data: " << type
;
94 EXPECT_EQ(base::UTF8ToUTF16(name
), entry
.name
);
95 EXPECT_EQ(size
, entry
.size
);
97 base::Time::Exploded time_exploded
;
98 entry
.last_modified
.LocalExplode(&time_exploded
);
99 EXPECT_EQ(year
, time_exploded
.year
);
100 EXPECT_EQ(month
, time_exploded
.month
);
101 EXPECT_EQ(day_of_month
, time_exploded
.day_of_month
);
102 EXPECT_EQ(hour
, time_exploded
.hour
);
103 EXPECT_EQ(minute
, time_exploded
.minute
);
107 const char* kTestFiles
[] = {
109 "dir-listing-ls-1-utf8",
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.
134 // Tests for Russian listings. The only difference between those
135 // files is character encoding:
136 "dir-listing-ls-25", // UTF-8
137 "dir-listing-ls-26", // KOI8-R
138 "dir-listing-ls-27", // windows-1251
140 "dir-listing-ls-28", // Hylafax FTP server
144 "dir-listing-ls-32", // busybox
146 "dir-listing-netware-1",
147 "dir-listing-netware-2",
148 "dir-listing-netware-3", // Spaces in file names.
158 "dir-listing-windows-1",
159 "dir-listing-windows-2",
162 INSTANTIATE_TEST_CASE_P(, FtpDirectoryListingParserTest
,
163 testing::ValuesIn(kTestFiles
));