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