Home | History | Annotate | Download | only in ftp
      1 // Copyright (c) 2009 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/time.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace {
     14 
     15 TEST(FtpUtilTest, UnixFilePathToVMS) {
     16   const struct {
     17     const char* input;
     18     const char* expected_output;
     19   } kTestCases[] = {
     20     { "",           ""            },
     21     { "/",          "[]"          },
     22     { "/a",         "a"           },
     23     { "/a/b",       "a:[000000]b" },
     24     { "/a/b/c",     "a:[b]c"      },
     25     { "/a/b/c/d",   "a:[b.c]d"    },
     26     { "/a/b/c/d/e", "a:[b.c.d]e"  },
     27     { "a",          "a"           },
     28     { "a/b",        "[.a]b"       },
     29     { "a/b/c",      "[.a.b]c"     },
     30     { "a/b/c/d",    "[.a.b.c]d"   },
     31   };
     32   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
     33     EXPECT_EQ(kTestCases[i].expected_output,
     34               net::FtpUtil::UnixFilePathToVMS(kTestCases[i].input))
     35         << kTestCases[i].input;
     36   }
     37 }
     38 
     39 TEST(FtpUtilTest, UnixDirectoryPathToVMS) {
     40   const struct {
     41     const char* input;
     42     const char* expected_output;
     43   } kTestCases[] = {
     44     { "",            ""            },
     45     { "/",           ""            },
     46     { "/a",          "a:[000000]"  },
     47     { "/a/",         "a:[000000]"  },
     48     { "/a/b",        "a:[b]"       },
     49     { "/a/b/",       "a:[b]"       },
     50     { "/a/b/c",      "a:[b.c]"     },
     51     { "/a/b/c/",     "a:[b.c]"     },
     52     { "/a/b/c/d",    "a:[b.c.d]"   },
     53     { "/a/b/c/d/",   "a:[b.c.d]"   },
     54     { "/a/b/c/d/e",  "a:[b.c.d.e]" },
     55     { "/a/b/c/d/e/", "a:[b.c.d.e]" },
     56     { "a",           "[.a]"        },
     57     { "a/",          "[.a]"        },
     58     { "a/b",         "[.a.b]"      },
     59     { "a/b/",        "[.a.b]"      },
     60     { "a/b/c",       "[.a.b.c]"    },
     61     { "a/b/c/",      "[.a.b.c]"    },
     62     { "a/b/c/d",     "[.a.b.c.d]"  },
     63     { "a/b/c/d/",    "[.a.b.c.d]"  },
     64   };
     65   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
     66     EXPECT_EQ(kTestCases[i].expected_output,
     67               net::FtpUtil::UnixDirectoryPathToVMS(kTestCases[i].input))
     68         << kTestCases[i].input;
     69   }
     70 }
     71 
     72 TEST(FtpUtilTest, VMSPathToUnix) {
     73   const struct {
     74     const char* input;
     75     const char* expected_output;
     76   } kTestCases[] = {
     77     { "",            "."          },
     78     { "[]",          "/"          },
     79     { "a",           "/a"         },
     80     { "a:[000000]",  "/a"         },
     81     { "a:[000000]b", "/a/b"       },
     82     { "a:[b]",       "/a/b"       },
     83     { "a:[b]c",      "/a/b/c"     },
     84     { "a:[b.c]",     "/a/b/c"     },
     85     { "a:[b.c]d",    "/a/b/c/d"   },
     86     { "a:[b.c.d]",   "/a/b/c/d"   },
     87     { "a:[b.c.d]e",  "/a/b/c/d/e" },
     88     { "a:[b.c.d.e]", "/a/b/c/d/e" },
     89     { "[.a]",        "a"          },
     90     { "[.a]b",       "a/b"        },
     91     { "[.a.b]",      "a/b"        },
     92     { "[.a.b]c",     "a/b/c"      },
     93     { "[.a.b.c]",    "a/b/c"      },
     94     { "[.a.b.c]d",   "a/b/c/d"    },
     95     { "[.a.b.c.d]",  "a/b/c/d"    },
     96   };
     97   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
     98     EXPECT_EQ(kTestCases[i].expected_output,
     99               net::FtpUtil::VMSPathToUnix(kTestCases[i].input))
    100         << kTestCases[i].input;
    101   }
    102 }
    103 
    104 TEST(FtpUtilTest, LsDateListingToTime) {
    105   base::Time::Exploded now_exploded;
    106   base::Time::Now().LocalExplode(&now_exploded);
    107 
    108   const struct {
    109     // Input.
    110     const char* month;
    111     const char* day;
    112     const char* rest;
    113 
    114     // Expected output.
    115     int expected_year;
    116     int expected_month;
    117     int expected_day_of_month;
    118     int expected_hour;
    119     int expected_minute;
    120   } kTestCases[] = {
    121     { "Nov", "01", "2007", 2007, 11, 1, 0, 0 },
    122     { "Jul", "25", "13:37", now_exploded.year, 7, 25, 13, 37 },
    123 
    124     // Test date listings in German, we should support them for FTP servers
    125     // giving localized listings.
    126     { "M\xc3\xa4r", "13", "2009", 2009, 3, 13, 0, 0 },
    127     { "Mai", "1", "10:10", now_exploded.year, 5, 1, 10, 10 },
    128     { "Okt", "14", "21:18", now_exploded.year, 10, 14, 21, 18 },
    129     { "Dez", "25", "2008", 2008, 12, 25, 0, 0 },
    130   };
    131   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
    132     SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s %s %s", i,
    133                               kTestCases[i].month, kTestCases[i].day,
    134                               kTestCases[i].rest));
    135 
    136     base::Time time;
    137     ASSERT_TRUE(net::FtpUtil::LsDateListingToTime(
    138         UTF8ToUTF16(kTestCases[i].month), UTF8ToUTF16(kTestCases[i].day),
    139         UTF8ToUTF16(kTestCases[i].rest), &time));
    140 
    141     base::Time::Exploded time_exploded;
    142     time.LocalExplode(&time_exploded);
    143     EXPECT_EQ(kTestCases[i].expected_year, time_exploded.year);
    144     EXPECT_EQ(kTestCases[i].expected_month, time_exploded.month);
    145     EXPECT_EQ(kTestCases[i].expected_day_of_month, time_exploded.day_of_month);
    146     EXPECT_EQ(kTestCases[i].expected_hour, time_exploded.hour);
    147     EXPECT_EQ(kTestCases[i].expected_minute, time_exploded.minute);
    148     EXPECT_EQ(0, time_exploded.second);
    149     EXPECT_EQ(0, time_exploded.millisecond);
    150   }
    151 }
    152 
    153 TEST(FtpUtilTest, GetStringPartAfterColumns) {
    154   const struct {
    155     const char* text;
    156     int column;
    157     const char* expected_result;
    158   } kTestCases[] = {
    159     { "", 0, "" },
    160     { "", 1, "" },
    161     { "foo abc", 0, "foo abc" },
    162     { "foo abc", 1, "abc" },
    163     { "  foo   abc", 0, "foo   abc" },
    164     { "  foo   abc", 1, "abc" },
    165     { "  foo   abc", 2, "" },
    166     { "  foo   abc ", 0, "foo   abc" },
    167     { "  foo   abc ", 1, "abc" },
    168     { "  foo   abc ", 2, "" },
    169   };
    170   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) {
    171     SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s %d",
    172                               i, kTestCases[i].text, kTestCases[i].column));
    173 
    174     EXPECT_EQ(ASCIIToUTF16(kTestCases[i].expected_result),
    175               net::FtpUtil::GetStringPartAfterColumns(
    176                   ASCIIToUTF16(kTestCases[i].text), kTestCases[i].column));
    177   }
    178 }
    179 
    180 }  // namespace
    181