ftp_directory_listing_parser_vms_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/string_util.h"
9#include "base/stringprintf.h"
10#include "base/strings/string_split.h"
11#include "base/utf_string_conversions.h"
12#include "net/ftp/ftp_directory_listing_parser_vms.h"
13
14namespace net {
15
16namespace {
17
18typedef FtpDirectoryListingParserTest FtpDirectoryListingParserVmsTest;
19
20TEST_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<string16> lines(GetSingleLineTestCase(good_cases[i].input));
58
59    // The parser requires a directory header before accepting regular input.
60    lines.insert(lines.begin(),
61                 ASCIIToUTF16("Directory ANONYMOUS_ROOT:[000000]"));
62
63    // A valid listing must also have a "Total" line at the end.
64    lines.insert(lines.end(),
65                 ASCIIToUTF16("Total of 1 file, 2 blocks."));
66
67    std::vector<FtpDirectoryListingEntry> entries;
68    EXPECT_TRUE(ParseFtpDirectoryListingVms(lines,
69                                            &entries));
70    VerifySingleLineTestCase(good_cases[i], entries);
71  }
72}
73
74TEST_F(FtpDirectoryListingParserVmsTest, Bad) {
75  const char* bad_cases[] = {
76    "garbage",
77
78    // Missing file version number.
79    "README.TXT 2 18-APR-2000 10:40:39",
80
81    // Missing extension.
82    "README;1 2 18-APR-2000 10:40:39",
83
84    // Malformed file size.
85    "README.TXT;1 garbage 18-APR-2000 10:40:39",
86    "README.TXT;1 -2 18-APR-2000 10:40:39",
87
88    // Malformed date.
89    "README.TXT;1 2 APR-2000 10:40:39",
90    "README.TXT;1 2 -18-APR-2000 10:40:39",
91    "README.TXT;1 2 18-APR 10:40:39",
92    "README.TXT;1 2 18-APR-2000 10",
93    "README.TXT;1 2 18-APR-2000 10:40.25",
94    "README.TXT;1 2 18-APR-2000 10:40.25.25",
95
96    // Malformed security information.
97    "X.TXT;2 1 12-MAR-2005 08:44:57 (RWED,RWED,RE,RE)",
98    "X.TXT;2 1 12-MAR-2005 08:44:57 [SYSTEM]",
99    "X.TXT;2 1 12-MAR-2005 08:44:57 (SYSTEM) (RWED,RWED,RE,RE)",
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 [X] (RWED)",
102    "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWED,RE,RE,RE)",
103    "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWEDRWED,RE,RE)",
104    "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,DEWR,RE,RE)",
105    "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWED,Q,RE)",
106    "X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RRWWEEDD,RE,RE)",
107  };
108  for (size_t i = 0; i < arraysize(bad_cases); i++) {
109    SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, bad_cases[i]));
110
111    std::vector<string16> lines(GetSingleLineTestCase(bad_cases[i]));
112
113    // The parser requires a directory header before accepting regular input.
114    lines.insert(lines.begin(),
115                 ASCIIToUTF16("Directory ANONYMOUS_ROOT:[000000]"));
116
117    // A valid listing must also have a "Total" line at the end.
118    lines.insert(lines.end(),
119                 ASCIIToUTF16("Total of 1 file, 2 blocks."));
120
121    std::vector<FtpDirectoryListingEntry> entries;
122    EXPECT_FALSE(ParseFtpDirectoryListingVms(lines,
123                                             &entries));
124  }
125}
126
127TEST_F(FtpDirectoryListingParserVmsTest, BadDataAfterFooter) {
128  const char* bad_cases[] = {
129    "garbage",
130    "Total of 1 file, 2 blocks.",
131    "Directory ANYNYMOUS_ROOT:[000000]",
132  };
133  for (size_t i = 0; i < arraysize(bad_cases); i++) {
134    SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, bad_cases[i]));
135
136    std::vector<string16> lines(
137        GetSingleLineTestCase("README.TXT;4  2  18-APR-2000 10:40:39.90"));
138
139    // The parser requires a directory header before accepting regular input.
140    lines.insert(lines.begin(),
141                 ASCIIToUTF16("Directory ANONYMOUS_ROOT:[000000]"));
142
143    // A valid listing must also have a "Total" line at the end.
144    lines.insert(lines.end(),
145                 ASCIIToUTF16("Total of 1 file, 2 blocks."));
146
147    {
148      // Make sure the listing is valid before we add data after footer.
149      std::vector<FtpDirectoryListingEntry> entries;
150      EXPECT_TRUE(ParseFtpDirectoryListingVms(lines,
151                                              &entries));
152    }
153
154    {
155      // Insert a line at the end of the listing that should make it invalid.
156      lines.insert(lines.end(),
157                   ASCIIToUTF16(bad_cases[i]));
158      std::vector<FtpDirectoryListingEntry> entries;
159      EXPECT_FALSE(ParseFtpDirectoryListingVms(lines,
160                                               &entries));
161    }
162  }
163}
164
165}  // namespace
166
167}  // namespace net
168