Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / ftp / ftp_util_unittest.cc
blob121780bb65bb660e30fe4990d467047409312c79
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;
18 namespace net {
20 namespace {
22 TEST(FtpUtilTest, UnixFilePathToVMS) {
23 const struct {
24 const char* input;
25 const char* expected_output;
26 } kTestCases[] = {
27 { "", "" },
28 { "/", "[]" },
29 { "/a", "a" },
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" },
34 { "a", "a" },
35 { "a/b", "[.a]b" },
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) {
47 const struct {
48 const char* input;
49 const char* expected_output;
50 } kTestCases[] = {
51 { "", "" },
52 { "/", "" },
53 { "/a", "a:[000000]" },
54 { "/a/", "a:[000000]" },
55 { "/a/b", "a:[b]" },
56 { "/a/b/", "a:[b]" },
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]" },
63 { "a", "[.a]" },
64 { "a/", "[.a]" },
65 { "a/b", "[.a.b]" },
66 { "a/b/", "[.a.b]" },
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) {
80 const struct {
81 const char* input;
82 const char* expected_output;
83 } kTestCases[] = {
84 { "", "." },
85 { "[]", "/" },
86 { "a", "/a" },
87 { "a:[000000]", "/a" },
88 { "a:[000000]b", "/a/b" },
89 { "a:[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" },
96 { "[.a]", "a" },
97 { "[.a]b", "a/b" },
98 { "[.a.b]", "a/b" },
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" },
103 { "[.", "" },
105 // UNIX emulation:
106 { "/", "/" },
107 { "/a", "/a" },
108 { "/a/b", "/a/b" },
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));
124 const struct {
125 // Input.
126 const char* month;
127 const char* day;
128 const char* rest;
130 // Expected output.
131 int expected_year;
132 int expected_month;
133 int expected_day_of_month;
134 int expected_hour;
135 int expected_minute;
136 } kTestCases[] = {
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));
173 base::Time time;
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) {
191 const struct {
192 // Input.
193 const char* date;
194 const char* time;
196 // Expected output.
197 int expected_year;
198 int expected_month;
199 int expected_day_of_month;
200 int expected_hour;
201 int expected_minute;
202 } kTestCases[] = {
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));
213 base::Time time;
214 ASSERT_TRUE(FtpUtil::WindowsDateListingToTime(
215 UTF8ToUTF16(kTestCases[i].date), UTF8ToUTF16(kTestCases[i].time),
216 &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) {
231 const struct {
232 const char* text;
233 int column;
234 const char* expected_result;
235 } kTestCases[] = {
236 { "", 0, "" },
237 { "", 1, "" },
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));
257 } // namespace
259 } // namespace net