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