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(L"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, GetStringPartAfterColumns) { 179 const struct { 180 const char* text; 181 int column; 182 const char* expected_result; 183 } kTestCases[] = { 184 { "", 0, "" }, 185 { "", 1, "" }, 186 { "foo abc", 0, "foo abc" }, 187 { "foo abc", 1, "abc" }, 188 { " foo abc", 0, "foo abc" }, 189 { " foo abc", 1, "abc" }, 190 { " foo abc", 2, "" }, 191 { " foo abc ", 0, "foo abc" }, 192 { " foo abc ", 1, "abc" }, 193 { " foo abc ", 2, "" }, 194 }; 195 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) { 196 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s %d", i, 197 kTestCases[i].text, kTestCases[i].column)); 198 199 EXPECT_EQ(ASCIIToUTF16(kTestCases[i].expected_result), 200 net::FtpUtil::GetStringPartAfterColumns( 201 ASCIIToUTF16(kTestCases[i].text), kTestCases[i].column)); 202 } 203} 204 205} // namespace 206