Home | History | Annotate | Download | only in ftp
      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_directory_listing_parser_unittest.h"
      6 
      7 #include "base/format_macros.h"
      8 #include "base/strings/string_util.h"
      9 #include "base/strings/stringprintf.h"
     10 #include "base/strings/string_split.h"
     11 #include "base/strings/utf_string_conversions.h"
     12 #include "net/ftp/ftp_directory_listing_parser_vms.h"
     13 
     14 namespace net {
     15 
     16 namespace {
     17 
     18 typedef FtpDirectoryListingParserTest FtpDirectoryListingParserVmsTest;
     19 
     20 TEST_F(FtpDirectoryListingParserVmsTest, Good) {
     21   const struct SingleLineTestData good_cases[] = {
     22     { "README.TXT;4  2  18-APR-2000 10:40:39.90",
     23       FtpDirectoryListingEntry::FILE, "readme.txt", 1024,
     24       2000, 4, 18, 10, 40 },
     25     { ".WELCOME;1    2  13-FEB-2002 23:32:40.47",
     26       FtpDirectoryListingEntry::FILE, ".welcome", 1024,
     27       2002, 2, 13, 23, 32 },
     28     { "FILE.;1    2  13-FEB-2002 23:32:40.47",
     29       FtpDirectoryListingEntry::FILE, "file.", 1024,
     30       2002, 2, 13, 23, 32 },
     31     { "EXAMPLE.TXT;1  1   4-NOV-2009 06:02 [JOHNDOE] (RWED,RWED,,)",
     32       FtpDirectoryListingEntry::FILE, "example.txt", 512,
     33       2009, 11, 4, 6, 2 },
     34     { "ANNOUNCE.TXT;2 1/16 12-MAR-2005 08:44:57 [SYSTEM] (RWED,RWED,RE,RE)",
     35       FtpDirectoryListingEntry::FILE, "announce.txt", 512,
     36       2005, 3, 12, 8, 44 },
     37     { "TEST.DIR;1 1 4-MAR-1999 22:14:34 [UCX$NOBO,ANONYMOUS] (RWE,RWE,RWE,RWE)",
     38       FtpDirectoryListingEntry::DIRECTORY, "test", -1,
     39       1999, 3, 4, 22, 14 },
     40     { "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (,,,)",
     41       FtpDirectoryListingEntry::FILE, "announce.txt", 512,
     42       2005, 3, 12, 8, 44 },
     43     { "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (R,RW,RWD,RE)",
     44       FtpDirectoryListingEntry::FILE, "announce.txt", 512,
     45       2005, 3, 12, 8, 44 },
     46     { "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (ED,RED,WD,WED)",
     47       FtpDirectoryListingEntry::FILE, "announce.txt", 512,
     48       2005, 3, 12, 8, 44 },
     49     { "VMS721.ISO;2 ******  6-MAY-2008 09:29 [ANONY,ANONYMOUS] (RE,RWED,RE,RE)",
     50       FtpDirectoryListingEntry::FILE, "vms721.iso", -1,
     51       2008, 5, 6, 9, 29 },
     52   };
     53   for (size_t i = 0; i < arraysize(good_cases); i++) {
     54     SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i,
     55                                     good_cases[i].input));
     56 
     57     std::vector<base::string16> lines(
     58         GetSingleLineTestCase(good_cases[i].input));
     59 
     60     // The parser requires a directory header before accepting regular input.
     61     lines.insert(lines.begin(),
     62                  ASCIIToUTF16("Directory ANONYMOUS_ROOT:[000000]"));
     63 
     64     // A valid listing must also have a "Total" line at the end.
     65     lines.insert(lines.end(),
     66                  ASCIIToUTF16("Total of 1 file, 2 blocks."));
     67 
     68     std::vector<FtpDirectoryListingEntry> entries;
     69     EXPECT_TRUE(ParseFtpDirectoryListingVms(lines,
     70                                             &entries));
     71     VerifySingleLineTestCase(good_cases[i], entries);
     72   }
     73 }
     74 
     75 TEST_F(FtpDirectoryListingParserVmsTest, Bad) {
     76   const char* bad_cases[] = {
     77     "garbage",
     78 
     79     // Missing file version number.
     80     "README.TXT 2 18-APR-2000 10:40:39",
     81 
     82     // Missing extension.
     83     "README;1 2 18-APR-2000 10:40:39",
     84 
     85     // Malformed file size.
     86     "README.TXT;1 garbage 18-APR-2000 10:40:39",
     87     "README.TXT;1 -2 18-APR-2000 10:40:39",
     88 
     89     // Malformed date.
     90     "README.TXT;1 2 APR-2000 10:40:39",
     91     "README.TXT;1 2 -18-APR-2000 10:40:39",
     92     "README.TXT;1 2 18-APR 10:40:39",
     93     "README.TXT;1 2 18-APR-2000 10",
     94     "README.TXT;1 2 18-APR-2000 10:40.25",
     95     "README.TXT;1 2 18-APR-2000 10:40.25.25",
     96 
     97     // Malformed security information.
     98     "X.TXT;2 1 12-MAR-2005 08:44:57 (RWED,RWED,RE,RE)",
     99     "X.TXT;2 1 12-MAR-2005 08:44:57 [SYSTEM]",
    100     "X.TXT;2 1 12-MAR-2005 08:44:57 (SYSTEM) (RWED,RWED,RE,RE)",
    101     "X.TXT;2 1 12-MAR-2005 08:44:57 [SYSTEM] [RWED,RWED,RE,RE]",
    102     "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED)",
    103     "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWED,RE,RE,RE)",
    104     "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWEDRWED,RE,RE)",
    105     "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,DEWR,RE,RE)",
    106     "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWED,Q,RE)",
    107     "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RRWWEEDD,RE,RE)",
    108   };
    109   for (size_t i = 0; i < arraysize(bad_cases); i++) {
    110     SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, bad_cases[i]));
    111 
    112     std::vector<base::string16> lines(GetSingleLineTestCase(bad_cases[i]));
    113 
    114     // The parser requires a directory header before accepting regular input.
    115     lines.insert(lines.begin(),
    116                  ASCIIToUTF16("Directory ANONYMOUS_ROOT:[000000]"));
    117 
    118     // A valid listing must also have a "Total" line at the end.
    119     lines.insert(lines.end(),
    120                  ASCIIToUTF16("Total of 1 file, 2 blocks."));
    121 
    122     std::vector<FtpDirectoryListingEntry> entries;
    123     EXPECT_FALSE(ParseFtpDirectoryListingVms(lines,
    124                                              &entries));
    125   }
    126 }
    127 
    128 TEST_F(FtpDirectoryListingParserVmsTest, BadDataAfterFooter) {
    129   const char* bad_cases[] = {
    130     "garbage",
    131     "Total of 1 file, 2 blocks.",
    132     "Directory ANYNYMOUS_ROOT:[000000]",
    133   };
    134   for (size_t i = 0; i < arraysize(bad_cases); i++) {
    135     SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, bad_cases[i]));
    136 
    137     std::vector<base::string16> lines(
    138         GetSingleLineTestCase("README.TXT;4  2  18-APR-2000 10:40:39.90"));
    139 
    140     // The parser requires a directory header before accepting regular input.
    141     lines.insert(lines.begin(),
    142                  ASCIIToUTF16("Directory ANONYMOUS_ROOT:[000000]"));
    143 
    144     // A valid listing must also have a "Total" line at the end.
    145     lines.insert(lines.end(),
    146                  ASCIIToUTF16("Total of 1 file, 2 blocks."));
    147 
    148     {
    149       // Make sure the listing is valid before we add data after footer.
    150       std::vector<FtpDirectoryListingEntry> entries;
    151       EXPECT_TRUE(ParseFtpDirectoryListingVms(lines,
    152                                               &entries));
    153     }
    154 
    155     {
    156       // Insert a line at the end of the listing that should make it invalid.
    157       lines.insert(lines.end(),
    158                    ASCIIToUTF16(bad_cases[i]));
    159       std::vector<FtpDirectoryListingEntry> entries;
    160       EXPECT_FALSE(ParseFtpDirectoryListingVms(lines,
    161                                                &entries));
    162     }
    163   }
    164 }
    165 
    166 }  // namespace
    167 
    168 }  // namespace net
    169