ftp_util_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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_util.h"
6
7#include "base/basictypes.h"
8#include "base/format_macros.h"
9#include "base/string_util.h"
10#include "base/stringprintf.h"
11#include "base/time.h"
12#include "base/utf_string_conversions.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace {
16
17TEST(FtpUtilTest, UnixFilePathToVMS) {
18  const struct {
19    const char* input;
20    const char* expected_output;
21  } kTestCases[] = {
22    { "",           ""            },
23    { "/",          "[]"          },
24    { "/a",         "a"           },
25    { "/a/b",       "a:[000000]b" },
26    { "/a/b/c",     "a:[b]c"      },
27    { "/a/b/c/d",   "a:[b.c]d"    },
28    { "/a/b/c/d/e", "a:[b.c.d]e"  },
29    { "a",          "a"           },
30    { "a/b",        "[.a]b"       },
31    { "a/b/c",      "[.a.b]c"     },
32    { "a/b/c/d",    "[.a.b.c]d"   },
33  };
34  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
35    EXPECT_EQ(kTestCases[i].expected_output,
36              net::FtpUtil::UnixFilePathToVMS(kTestCases[i].input))
37        << kTestCases[i].input;
38  }
39}
40
41TEST(FtpUtilTest, UnixDirectoryPathToVMS) {
42  const struct {
43    const char* input;
44    const char* expected_output;
45  } kTestCases[] = {
46    { "",            ""            },
47    { "/",           ""            },
48    { "/a",          "a:[000000]"  },
49    { "/a/",         "a:[000000]"  },
50    { "/a/b",        "a:[b]"       },
51    { "/a/b/",       "a:[b]"       },
52    { "/a/b/c",      "a:[b.c]"     },
53    { "/a/b/c/",     "a:[b.c]"     },
54    { "/a/b/c/d",    "a:[b.c.d]"   },
55    { "/a/b/c/d/",   "a:[b.c.d]"   },
56    { "/a/b/c/d/e",  "a:[b.c.d.e]" },
57    { "/a/b/c/d/e/", "a:[b.c.d.e]" },
58    { "a",           "[.a]"        },
59    { "a/",          "[.a]"        },
60    { "a/b",         "[.a.b]"      },
61    { "a/b/",        "[.a.b]"      },
62    { "a/b/c",       "[.a.b.c]"    },
63    { "a/b/c/",      "[.a.b.c]"    },
64    { "a/b/c/d",     "[.a.b.c.d]"  },
65    { "a/b/c/d/",    "[.a.b.c.d]"  },
66  };
67  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
68    EXPECT_EQ(kTestCases[i].expected_output,
69              net::FtpUtil::UnixDirectoryPathToVMS(kTestCases[i].input))
70        << kTestCases[i].input;
71  }
72}
73
74TEST(FtpUtilTest, VMSPathToUnix) {
75  const struct {
76    const char* input;
77    const char* expected_output;
78  } kTestCases[] = {
79    { "",            "."          },
80    { "[]",          "/"          },
81    { "a",           "/a"         },
82    { "a:[000000]",  "/a"         },
83    { "a:[000000]b", "/a/b"       },
84    { "a:[b]",       "/a/b"       },
85    { "a:[b]c",      "/a/b/c"     },
86    { "a:[b.c]",     "/a/b/c"     },
87    { "a:[b.c]d",    "/a/b/c/d"   },
88    { "a:[b.c.d]",   "/a/b/c/d"   },
89    { "a:[b.c.d]e",  "/a/b/c/d/e" },
90    { "a:[b.c.d.e]", "/a/b/c/d/e" },
91    { "[.a]",        "a"          },
92    { "[.a]b",       "a/b"        },
93    { "[.a.b]",      "a/b"        },
94    { "[.a.b]c",     "a/b/c"      },
95    { "[.a.b.c]",    "a/b/c"      },
96    { "[.a.b.c]d",   "a/b/c/d"    },
97    { "[.a.b.c.d]",  "a/b/c/d"    },
98    { "[.",          ""           },
99  };
100  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
101    EXPECT_EQ(kTestCases[i].expected_output,
102              net::FtpUtil::VMSPathToUnix(kTestCases[i].input))
103        << kTestCases[i].input;
104  }
105}
106
107TEST(FtpUtilTest, LsDateListingToTime) {
108  base::Time mock_current_time;
109  ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994 12:45:26 GMT",
110                                     &mock_current_time));
111
112  const struct {
113    // Input.
114    const char* month;
115    const char* day;
116    const char* rest;
117
118    // Expected output.
119    int expected_year;
120    int expected_month;
121    int expected_day_of_month;
122    int expected_hour;
123    int expected_minute;
124  } kTestCases[] = {
125    { "Nov", "01", "2007", 2007, 11, 1, 0, 0 },
126    { "Jul", "25", "13:37", 1994, 7, 25, 13, 37 },
127
128    // Test date listings in German.
129    { "M\xc3\xa4r", "13", "2009", 2009, 3, 13, 0, 0 },
130    { "Mai", "1", "10:10", 1994, 5, 1, 10, 10 },
131    { "Okt", "14", "21:18", 1994, 10, 14, 21, 18 },
132    { "Dez", "25", "2008", 2008, 12, 25, 0, 0 },
133
134    // Test date listings in Russian.
135    { "\xd1\x8f\xd0\xbd\xd0\xb2", "1", "2011", 2011, 1, 1, 0, 0 },
136    { "\xd1\x84\xd0\xb5\xd0\xb2", "1", "2011", 2011, 2, 1, 0, 0 },
137    { "\xd0\xbc\xd0\xb0\xd1\x80", "1", "2011", 2011, 3, 1, 0, 0 },
138    { "\xd0\xb0\xd0\xbf\xd1\x80", "1", "2011", 2011, 4, 1, 0, 0 },
139    { "\xd0\xbc\xd0\xb0\xd0\xb9", "1", "2011", 2011, 5, 1, 0, 0 },
140    { "\xd0\xb8\xd1\x8e\xd0\xbd", "1", "2011", 2011, 6, 1, 0, 0 },
141    { "\xd0\xb8\xd1\x8e\xd0\xbb", "1", "2011", 2011, 7, 1, 0, 0 },
142    { "\xd0\xb0\xd0\xb2\xd0\xb3", "1", "2011", 2011, 8, 1, 0, 0 },
143    { "\xd1\x81\xd0\xb5\xd0\xbd", "1", "2011", 2011, 9, 1, 0, 0 },
144    { "\xd0\xbe\xd0\xba\xd1\x82", "1", "2011", 2011, 10, 1, 0, 0 },
145    { "\xd0\xbd\xd0\xbe\xd1\x8f", "1", "2011", 2011, 11, 1, 0, 0 },
146    { "\xd0\xb4\xd0\xb5\xd0\xba", "1", "2011", 2011, 12, 1, 0, 0 },
147
148    // Test current year detection.
149    { "Nov", "01", "12:00", 1994, 11, 1, 12, 0 },
150    { "Nov", "15", "12:00", 1994, 11, 15, 12, 0 },
151    { "Nov", "16", "12:00", 1993, 11, 16, 12, 0 },
152    { "Jan", "01", "08:30", 1994, 1, 1, 8, 30 },
153    { "Sep", "02", "09:00", 1994, 9, 2, 9, 0 },
154    { "Dec", "06", "21:00", 1993, 12, 6, 21, 0 },
155  };
156  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
157    SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s %s %s", i,
158                                    kTestCases[i].month, kTestCases[i].day,
159                                    kTestCases[i].rest));
160
161    base::Time time;
162    ASSERT_TRUE(net::FtpUtil::LsDateListingToTime(
163        UTF8ToUTF16(kTestCases[i].month), UTF8ToUTF16(kTestCases[i].day),
164        UTF8ToUTF16(kTestCases[i].rest), mock_current_time, &time));
165
166    base::Time::Exploded time_exploded;
167    time.LocalExplode(&time_exploded);
168    EXPECT_EQ(kTestCases[i].expected_year, time_exploded.year);
169    EXPECT_EQ(kTestCases[i].expected_month, time_exploded.month);
170    EXPECT_EQ(kTestCases[i].expected_day_of_month, time_exploded.day_of_month);
171    EXPECT_EQ(kTestCases[i].expected_hour, time_exploded.hour);
172    EXPECT_EQ(kTestCases[i].expected_minute, time_exploded.minute);
173    EXPECT_EQ(0, time_exploded.second);
174    EXPECT_EQ(0, time_exploded.millisecond);
175  }
176}
177
178TEST(FtpUtilTest, WindowsDateListingToTime) {
179  const struct {
180    // Input.
181    const char* date;
182    const char* time;
183
184    // Expected output.
185    int expected_year;
186    int expected_month;
187    int expected_day_of_month;
188    int expected_hour;
189    int expected_minute;
190  } kTestCases[] = {
191    { "11-01-07", "12:42", 2007, 11, 1, 12, 42 },
192    { "11-01-07", "12:42AM", 2007, 11, 1, 0, 42 },
193    { "11-01-07", "12:42PM", 2007, 11, 1, 12, 42 },
194
195    { "11-01-2007", "12:42", 2007, 11, 1, 12, 42 },
196  };
197  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
198    SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s %s", i,
199                                    kTestCases[i].date, kTestCases[i].time));
200
201    base::Time time;
202    ASSERT_TRUE(net::FtpUtil::WindowsDateListingToTime(
203                    UTF8ToUTF16(kTestCases[i].date),
204                    UTF8ToUTF16(kTestCases[i].time),
205                    &time));
206
207    base::Time::Exploded time_exploded;
208    time.LocalExplode(&time_exploded);
209    EXPECT_EQ(kTestCases[i].expected_year, time_exploded.year);
210    EXPECT_EQ(kTestCases[i].expected_month, time_exploded.month);
211    EXPECT_EQ(kTestCases[i].expected_day_of_month, time_exploded.day_of_month);
212    EXPECT_EQ(kTestCases[i].expected_hour, time_exploded.hour);
213    EXPECT_EQ(kTestCases[i].expected_minute, time_exploded.minute);
214    EXPECT_EQ(0, time_exploded.second);
215    EXPECT_EQ(0, time_exploded.millisecond);
216  }
217}
218
219TEST(FtpUtilTest, GetStringPartAfterColumns) {
220  const struct {
221    const char* text;
222    int column;
223    const char* expected_result;
224  } kTestCases[] = {
225    { "", 0, "" },
226    { "", 1, "" },
227    { "foo abc", 0, "foo abc" },
228    { "foo abc", 1, "abc" },
229    { "  foo   abc", 0, "foo   abc" },
230    { "  foo   abc", 1, "abc" },
231    { "  foo   abc", 2, "" },
232    { "  foo   abc ", 0, "foo   abc" },
233    { "  foo   abc ", 1, "abc" },
234    { "  foo   abc ", 2, "" },
235  };
236  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
237    SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s %d", i,
238                                    kTestCases[i].text, kTestCases[i].column));
239
240    EXPECT_EQ(ASCIIToUTF16(kTestCases[i].expected_result),
241              net::FtpUtil::GetStringPartAfterColumns(
242                  ASCIIToUTF16(kTestCases[i].text), kTestCases[i].column));
243  }
244}
245
246}  // namespace
247