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.
5 #include "net/ftp/ftp_util.h"
7 #include "base/basictypes.h"
8 #include "base/format_macros.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::ASCIIToUTF16
;
16 using base::UTF8ToUTF16
;
22 TEST(FtpUtilTest
, UnixFilePathToVMS
) {
25 const char* expected_output
;
30 { "/a/b", "a:[000000]b" },
31 { "/a/b/c", "a:[b]c" },
32 { "/a/b/c/d", "a:[b.c]d" },
33 { "/a/b/c/d/e", "a:[b.c.d]e" },
36 { "a/b/c", "[.a.b]c" },
37 { "a/b/c/d", "[.a.b.c]d" },
39 for (size_t i
= 0; i
< arraysize(kTestCases
); i
++) {
40 EXPECT_EQ(kTestCases
[i
].expected_output
,
41 FtpUtil::UnixFilePathToVMS(kTestCases
[i
].input
))
42 << kTestCases
[i
].input
;
46 TEST(FtpUtilTest
, UnixDirectoryPathToVMS
) {
49 const char* expected_output
;
53 { "/a", "a:[000000]" },
54 { "/a/", "a:[000000]" },
57 { "/a/b/c", "a:[b.c]" },
58 { "/a/b/c/", "a:[b.c]" },
59 { "/a/b/c/d", "a:[b.c.d]" },
60 { "/a/b/c/d/", "a:[b.c.d]" },
61 { "/a/b/c/d/e", "a:[b.c.d.e]" },
62 { "/a/b/c/d/e/", "a:[b.c.d.e]" },
67 { "a/b/c", "[.a.b.c]" },
68 { "a/b/c/", "[.a.b.c]" },
69 { "a/b/c/d", "[.a.b.c.d]" },
70 { "a/b/c/d/", "[.a.b.c.d]" },
72 for (size_t i
= 0; i
< arraysize(kTestCases
); i
++) {
73 EXPECT_EQ(kTestCases
[i
].expected_output
,
74 FtpUtil::UnixDirectoryPathToVMS(kTestCases
[i
].input
))
75 << kTestCases
[i
].input
;
79 TEST(FtpUtilTest
, VMSPathToUnix
) {
82 const char* expected_output
;
87 { "a:[000000]", "/a" },
88 { "a:[000000]b", "/a/b" },
90 { "a:[b]c", "/a/b/c" },
91 { "a:[b.c]", "/a/b/c" },
92 { "a:[b.c]d", "/a/b/c/d" },
93 { "a:[b.c.d]", "/a/b/c/d" },
94 { "a:[b.c.d]e", "/a/b/c/d/e" },
95 { "a:[b.c.d.e]", "/a/b/c/d/e" },
99 { "[.a.b]c", "a/b/c" },
100 { "[.a.b.c]", "a/b/c" },
101 { "[.a.b.c]d", "a/b/c/d" },
102 { "[.a.b.c.d]", "a/b/c/d" },
109 { "/a/b/c", "/a/b/c" },
110 { "/a/b/c/d", "/a/b/c/d" },
112 for (size_t i
= 0; i
< arraysize(kTestCases
); i
++) {
113 EXPECT_EQ(kTestCases
[i
].expected_output
,
114 FtpUtil::VMSPathToUnix(kTestCases
[i
].input
))
115 << kTestCases
[i
].input
;
119 TEST(FtpUtilTest
, LsDateListingToTime
) {
120 base::Time mock_current_time
;
121 ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994 12:45:26 GMT",
122 &mock_current_time
));
133 int expected_day_of_month
;
137 { "Nov", "01", "2007", 2007, 11, 1, 0, 0 },
138 { "Jul", "25", "13:37", 1994, 7, 25, 13, 37 },
140 // Test date listings in German.
141 { "M\xc3\xa4r", "13", "2009", 2009, 3, 13, 0, 0 },
142 { "Mai", "1", "10:10", 1994, 5, 1, 10, 10 },
143 { "Okt", "14", "21:18", 1994, 10, 14, 21, 18 },
144 { "Dez", "25", "2008", 2008, 12, 25, 0, 0 },
146 // Test date listings in Russian.
147 { "\xd1\x8f\xd0\xbd\xd0\xb2", "1", "2011", 2011, 1, 1, 0, 0 },
148 { "\xd1\x84\xd0\xb5\xd0\xb2", "1", "2011", 2011, 2, 1, 0, 0 },
149 { "\xd0\xbc\xd0\xb0\xd1\x80", "1", "2011", 2011, 3, 1, 0, 0 },
150 { "\xd0\xb0\xd0\xbf\xd1\x80", "1", "2011", 2011, 4, 1, 0, 0 },
151 { "\xd0\xbc\xd0\xb0\xd0\xb9", "1", "2011", 2011, 5, 1, 0, 0 },
152 { "\xd0\xb8\xd1\x8e\xd0\xbd", "1", "2011", 2011, 6, 1, 0, 0 },
153 { "\xd0\xb8\xd1\x8e\xd0\xbb", "1", "2011", 2011, 7, 1, 0, 0 },
154 { "\xd0\xb0\xd0\xb2\xd0\xb3", "1", "2011", 2011, 8, 1, 0, 0 },
155 { "\xd1\x81\xd0\xb5\xd0\xbd", "1", "2011", 2011, 9, 1, 0, 0 },
156 { "\xd0\xbe\xd0\xba\xd1\x82", "1", "2011", 2011, 10, 1, 0, 0 },
157 { "\xd0\xbd\xd0\xbe\xd1\x8f", "1", "2011", 2011, 11, 1, 0, 0 },
158 { "\xd0\xb4\xd0\xb5\xd0\xba", "1", "2011", 2011, 12, 1, 0, 0 },
160 // Test current year detection.
161 { "Nov", "01", "12:00", 1994, 11, 1, 12, 0 },
162 { "Nov", "15", "12:00", 1994, 11, 15, 12, 0 },
163 { "Nov", "16", "12:00", 1993, 11, 16, 12, 0 },
164 { "Jan", "01", "08:30", 1994, 1, 1, 8, 30 },
165 { "Sep", "02", "09:00", 1994, 9, 2, 9, 0 },
166 { "Dec", "06", "21:00", 1993, 12, 6, 21, 0 },
168 for (size_t i
= 0; i
< arraysize(kTestCases
); i
++) {
169 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s %s %s", i
,
170 kTestCases
[i
].month
, kTestCases
[i
].day
,
171 kTestCases
[i
].rest
));
174 ASSERT_TRUE(FtpUtil::LsDateListingToTime(
175 UTF8ToUTF16(kTestCases
[i
].month
), UTF8ToUTF16(kTestCases
[i
].day
),
176 UTF8ToUTF16(kTestCases
[i
].rest
), mock_current_time
, &time
));
178 base::Time::Exploded time_exploded
;
179 time
.LocalExplode(&time_exploded
);
180 EXPECT_EQ(kTestCases
[i
].expected_year
, time_exploded
.year
);
181 EXPECT_EQ(kTestCases
[i
].expected_month
, time_exploded
.month
);
182 EXPECT_EQ(kTestCases
[i
].expected_day_of_month
, time_exploded
.day_of_month
);
183 EXPECT_EQ(kTestCases
[i
].expected_hour
, time_exploded
.hour
);
184 EXPECT_EQ(kTestCases
[i
].expected_minute
, time_exploded
.minute
);
185 EXPECT_EQ(0, time_exploded
.second
);
186 EXPECT_EQ(0, time_exploded
.millisecond
);
190 TEST(FtpUtilTest
, WindowsDateListingToTime
) {
199 int expected_day_of_month
;
203 { "11-01-07", "12:42", 2007, 11, 1, 12, 42 },
204 { "11-01-07", "12:42AM", 2007, 11, 1, 0, 42 },
205 { "11-01-07", "12:42PM", 2007, 11, 1, 12, 42 },
207 { "11-01-2007", "12:42", 2007, 11, 1, 12, 42 },
209 for (size_t i
= 0; i
< arraysize(kTestCases
); i
++) {
210 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s %s", i
,
211 kTestCases
[i
].date
, kTestCases
[i
].time
));
214 ASSERT_TRUE(FtpUtil::WindowsDateListingToTime(
215 UTF8ToUTF16(kTestCases
[i
].date
), UTF8ToUTF16(kTestCases
[i
].time
),
218 base::Time::Exploded time_exploded
;
219 time
.LocalExplode(&time_exploded
);
220 EXPECT_EQ(kTestCases
[i
].expected_year
, time_exploded
.year
);
221 EXPECT_EQ(kTestCases
[i
].expected_month
, time_exploded
.month
);
222 EXPECT_EQ(kTestCases
[i
].expected_day_of_month
, time_exploded
.day_of_month
);
223 EXPECT_EQ(kTestCases
[i
].expected_hour
, time_exploded
.hour
);
224 EXPECT_EQ(kTestCases
[i
].expected_minute
, time_exploded
.minute
);
225 EXPECT_EQ(0, time_exploded
.second
);
226 EXPECT_EQ(0, time_exploded
.millisecond
);
230 TEST(FtpUtilTest
, GetStringPartAfterColumns
) {
234 const char* expected_result
;
238 { "foo abc", 0, "foo abc" },
239 { "foo abc", 1, "abc" },
240 { " foo abc", 0, "foo abc" },
241 { " foo abc", 1, "abc" },
242 { " foo abc", 2, "" },
243 { " foo abc ", 0, "foo abc" },
244 { " foo abc ", 1, "abc" },
245 { " foo abc ", 2, "" },
247 for (size_t i
= 0; i
< arraysize(kTestCases
); i
++) {
248 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s %d", i
,
249 kTestCases
[i
].text
, kTestCases
[i
].column
));
251 EXPECT_EQ(ASCIIToUTF16(kTestCases
[i
].expected_result
),
252 FtpUtil::GetStringPartAfterColumns(
253 ASCIIToUTF16(kTestCases
[i
].text
), kTestCases
[i
].column
));