ftp_directory_listing_parser_ls_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "net/ftp/ftp_directory_listing_parser_ls.h" 11 12namespace net { 13 14namespace { 15 16typedef FtpDirectoryListingParserTest FtpDirectoryListingParserLsTest; 17 18TEST_F(FtpDirectoryListingParserLsTest, Good) { 19 const struct SingleLineTestData good_cases[] = { 20 { "-rw-r--r-- 1 ftp ftp 528 Nov 01 2007 README", 21 FtpDirectoryListingEntry::FILE, "README", 528, 22 2007, 11, 1, 0, 0 }, 23 { "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 directory", 24 FtpDirectoryListingEntry::DIRECTORY, "directory", -1, 25 1994, 5, 15, 18, 11 }, 26 { "lrwxrwxrwx 1 0 0 26 Sep 18 2008 pub -> vol/1/.CLUSTER/var_ftp/pub", 27 FtpDirectoryListingEntry::SYMLINK, "pub", -1, 28 2008, 9, 18, 0, 0 }, 29 { "lrwxrwxrwx 1 0 0 3 Oct 12 13:37 mirror -> pub", 30 FtpDirectoryListingEntry::SYMLINK, "mirror", -1, 31 1994, 10, 12, 13, 37 }, 32 { "drwxrwsr-x 4 501 501 4096 Feb 20 2007 pub", 33 FtpDirectoryListingEntry::DIRECTORY, "pub", -1, 34 2007, 2, 20, 0, 0 }, 35 { "drwxr-xr-x 4 (?) (?) 4096 Apr 8 2007 jigdo", 36 FtpDirectoryListingEntry::DIRECTORY, "jigdo", -1, 37 2007, 4, 8, 0, 0 }, 38 { "drwx-wx-wt 2 root wheel 512 Jul 1 02:15 incoming", 39 FtpDirectoryListingEntry::DIRECTORY, "incoming", -1, 40 1994, 7, 1, 2, 15 }, 41 { "-rw-r--r-- 1 2 3 3447432 May 18 2009 Foo - Manual.pdf", 42 FtpDirectoryListingEntry::FILE, "Foo - Manual.pdf", 3447432, 43 2009, 5, 18, 0, 0 }, 44 { "d-wx-wx-wt+ 4 ftp 989 512 Dec 8 15:54 incoming", 45 FtpDirectoryListingEntry::DIRECTORY, "incoming", -1, 46 1993, 12, 8, 15, 54 }, 47 { "drwxrwxrwx 1 owner group 1024 Sep 13 0:30 audio", 48 FtpDirectoryListingEntry::DIRECTORY, "audio", -1, 49 1994, 9, 13, 0, 30 }, 50 { "lrwxrwxrwx 1 0 0 26 Sep 18 2008 pub", 51 FtpDirectoryListingEntry::SYMLINK, "pub", -1, 52 2008, 9, 18, 0, 0 }, 53 { "-rw-r--r-- 1 ftp ftp -528 Nov 01 2007 README", 54 FtpDirectoryListingEntry::FILE, "README", -1, 55 2007, 11, 1, 0, 0 }, 56 57 // Tests for the wu-ftpd variant: 58 { "drwxr-xr-x 2 sys 512 Mar 27 2009 pub", 59 FtpDirectoryListingEntry::DIRECTORY, "pub", -1, 60 2009, 3, 27, 0, 0 }, 61 { "lrwxrwxrwx 0 0 26 Sep 18 2008 pub -> vol/1/.CLUSTER/var_ftp/pub", 62 FtpDirectoryListingEntry::SYMLINK, "pub", -1, 63 2008, 9, 18, 0, 0 }, 64 { "drwxr-xr-x (?) (?) 4096 Apr 8 2007 jigdo", 65 FtpDirectoryListingEntry::DIRECTORY, "jigdo", -1, 66 2007, 4, 8, 0, 0 }, 67 { "-rw-r--r-- 2 3 3447432 May 18 2009 Foo - Manual.pdf", 68 FtpDirectoryListingEntry::FILE, "Foo - Manual.pdf", 3447432, 69 2009, 5, 18, 0, 0 }, 70 71 // Tests for "ls -l" style listings sent by an OS/2 server (FtpServer): 72 { "-r--r--r-- 1 ftp -A--- 13274 Mar 1 2006 UpTime.exe", 73 FtpDirectoryListingEntry::FILE, "UpTime.exe", 13274, 74 2006, 3, 1, 0, 0 }, 75 { "dr--r--r-- 1 ftp ----- 0 Nov 17 17:08 kernels", 76 FtpDirectoryListingEntry::DIRECTORY, "kernels", -1, 77 1993, 11, 17, 17, 8 }, 78 79 // Tests for "ls -l" style listing sent by Xplain FTP Server. 80 { "drwxr-xr-x folder 0 Jul 17 2006 online", 81 FtpDirectoryListingEntry::DIRECTORY, "online", -1, 82 2006, 7, 17, 0, 0 }, 83 84 // Tests for "ls -l" style listing with owning group name 85 // not separated from file size (http://crbug.com/58963). 86 { "-rw-r--r-- 1 ftpadmin ftpadmin125435904 Apr 9 2008 .pureftpd-upload", 87 FtpDirectoryListingEntry::FILE, ".pureftpd-upload", 0, 88 2008, 4, 9, 0, 0 }, 89 90 // Tests for "ls -l" style listing with number of links 91 // not separated from permission listing (http://crbug.com/70394). 92 { "drwxr-xr-x1732 266 111 90112 Jun 21 2001 .rda_2", 93 FtpDirectoryListingEntry::DIRECTORY, ".rda_2", -1, 94 2001, 6, 21, 0, 0 }, 95 96 // Tests for "ls -l" style listing with group name containing spaces. 97 { "drwxrwxr-x 3 %%%% Domain Users 4096 Dec 9 2009 %%%%%", 98 FtpDirectoryListingEntry::DIRECTORY, "%%%%%", -1, 99 2009, 12, 9, 0, 0 }, 100 101 // Tests for "ls -l" style listing in Russian locale (note the swapped 102 // parts order: the day of month is the first, before month). 103 { "-rwxrwxr-x 1 ftp ftp 123 23 \xd0\xbc\xd0\xb0\xd0\xb9 2011 test", 104 FtpDirectoryListingEntry::FILE, "test", 123, 105 2011, 5, 23, 0, 0 }, 106 { "drwxrwxr-x 1 ftp ftp 4096 19 \xd0\xbe\xd0\xba\xd1\x82 2011 dir", 107 FtpDirectoryListingEntry::DIRECTORY, "dir", -1, 108 2011, 10, 19, 0, 0 }, 109 110 // Plan9 sends entry type "a" for append-only files. 111 { "ar-xr-xr-x 2 none none 512 Apr 26 17:52 plan9", 112 FtpDirectoryListingEntry::FILE, "plan9", 512, 113 1994, 4, 26, 17, 52 }, 114 115 // Hylafax sends a shorter permission listing. 116 { "drwxrwx 2 10 4096 Jul 28 02:41 tmp", 117 FtpDirectoryListingEntry::DIRECTORY, "tmp", -1, 118 1994, 7, 28, 2, 41 }, 119 }; 120 for (size_t i = 0; i < arraysize(good_cases); i++) { 121 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, 122 good_cases[i].input)); 123 124 std::vector<FtpDirectoryListingEntry> entries; 125 EXPECT_TRUE(ParseFtpDirectoryListingLs( 126 GetSingleLineTestCase(good_cases[i].input), 127 GetMockCurrentTime(), 128 &entries)); 129 VerifySingleLineTestCase(good_cases[i], entries); 130 } 131} 132 133TEST_F(FtpDirectoryListingParserLsTest, Ignored) { 134 const char* ignored_cases[] = { 135 "drwxr-xr-x 2 0 0 4096 Mar 18 2007 ", // http://crbug.com/60065 136 137 "ftpd: .: Permission denied", 138 "ftpd-BSD: .: Permission denied", 139 "ls: .: EDC5111I Permission denied.", 140 141 // Tests important for security: verify that after we detect the column 142 // offset we don't try to access invalid memory on malformed input. 143 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11", 144 "drwxr-xr-x 3 ftp 4096 May 15 18:11", 145 "drwxr-xr-x folder 0 May 15 18:11", 146 }; 147 for (size_t i = 0; i < arraysize(ignored_cases); i++) { 148 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, 149 ignored_cases[i])); 150 151 std::vector<FtpDirectoryListingEntry> entries; 152 EXPECT_TRUE(ParseFtpDirectoryListingLs( 153 GetSingleLineTestCase(ignored_cases[i]), 154 GetMockCurrentTime(), 155 &entries)); 156 EXPECT_EQ(0U, entries.size()); 157 } 158} 159 160TEST_F(FtpDirectoryListingParserLsTest, Bad) { 161 const char* bad_cases[] = { 162 " foo", 163 "garbage", 164 "-rw-r--r-- ftp ftp", 165 "-rw-rgbr-- ftp ftp 528 Nov 01 2007 README", 166 "qrwwr--r-- ftp ftp 528 Nov 01 2007 README", 167 "-rw-r--r-- ftp ftp 528 Foo 01 2007 README", 168 "-rw-r--r-- 1 ftp ftp", 169 "-rw-rgbr-- 1 ftp ftp 528 Nov 01 2007 README", 170 "qrwwr--r-- 1 ftp ftp 528 Nov 01 2007 README", 171 "-rw-r--r-- 1 ftp ftp 528 Foo 01 2007 README", 172 "drwxrwxrwx 1 owner group 1024 Sep 13 0:3 audio", 173 174 "-qqqqqqqqq+ 2 sys 512 Mar 27 2009 pub", 175 }; 176 for (size_t i = 0; i < arraysize(bad_cases); i++) { 177 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, 178 bad_cases[i])); 179 180 std::vector<FtpDirectoryListingEntry> entries; 181 EXPECT_FALSE(ParseFtpDirectoryListingLs(GetSingleLineTestCase(bad_cases[i]), 182 GetMockCurrentTime(), 183 &entries)); 184 } 185} 186 187} // namespace 188 189} // namespace net 190