| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/ftp/ftp_directory_listing_parser_unittest.h" | 5 #include "net/ftp/ftp_directory_listing_parser_unittest.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "net/ftp/ftp_directory_listing_parser_ls.h" | 9 #include "net/ftp/ftp_directory_listing_parser_ls.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 typedef net::FtpDirectoryListingParserTest FtpDirectoryListingParserLsTest; | 13 typedef net::FtpDirectoryListingParserTest FtpDirectoryListingParserLsTest; |
| 14 | 14 |
| 15 TEST_F(FtpDirectoryListingParserLsTest, Good) { | 15 TEST_F(FtpDirectoryListingParserLsTest, Good) { |
| 16 base::Time::Exploded now_exploded; | 16 base::Time mock_current_time; |
| 17 base::Time::Now().LocalExplode(&now_exploded); | 17 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994 12:45:26 GMT", |
| 18 &mock_current_time)); |
| 18 | 19 |
| 19 const struct SingleLineTestData good_cases[] = { | 20 const struct SingleLineTestData good_cases[] = { |
| 20 { "-rw-r--r-- 1 ftp ftp 528 Nov 01 2007 README", | 21 { "-rw-r--r-- 1 ftp ftp 528 Nov 01 2007 README", |
| 21 net::FtpDirectoryListingEntry::FILE, "README", 528, | 22 net::FtpDirectoryListingEntry::FILE, "README", 528, |
| 22 2007, 11, 1, 0, 0 }, | 23 2007, 11, 1, 0, 0 }, |
| 23 { "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 directory", | 24 { "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 directory", |
| 24 net::FtpDirectoryListingEntry::DIRECTORY, "directory", -1, | 25 net::FtpDirectoryListingEntry::DIRECTORY, "directory", -1, |
| 25 now_exploded.year, 5, 15, 18, 11 }, | 26 1994, 5, 15, 18, 11 }, |
| 26 { "lrwxrwxrwx 1 0 0 26 Sep 18 2008 pub -> vol/1/.CLUSTER/var_ftp/pub", | 27 { "lrwxrwxrwx 1 0 0 26 Sep 18 2008 pub -> vol/1/.CLUSTER/var_ftp/pub", |
| 27 net::FtpDirectoryListingEntry::SYMLINK, "pub", -1, | 28 net::FtpDirectoryListingEntry::SYMLINK, "pub", -1, |
| 28 2008, 9, 18, 0, 0 }, | 29 2008, 9, 18, 0, 0 }, |
| 29 { "lrwxrwxrwx 1 0 0 3 Oct 12 13:37 mirror -> pub", | 30 { "lrwxrwxrwx 1 0 0 3 Oct 12 13:37 mirror -> pub", |
| 30 net::FtpDirectoryListingEntry::SYMLINK, "mirror", -1, | 31 net::FtpDirectoryListingEntry::SYMLINK, "mirror", -1, |
| 31 now_exploded.year, 10, 12, 13, 37 }, | 32 1994, 10, 12, 13, 37 }, |
| 32 { "drwxrwsr-x 4 501 501 4096 Feb 20 2007 pub", | 33 { "drwxrwsr-x 4 501 501 4096 Feb 20 2007 pub", |
| 33 net::FtpDirectoryListingEntry::DIRECTORY, "pub", -1, | 34 net::FtpDirectoryListingEntry::DIRECTORY, "pub", -1, |
| 34 2007, 2, 20, 0, 0 }, | 35 2007, 2, 20, 0, 0 }, |
| 35 { "drwxr-xr-x 4 (?) (?) 4096 Apr 8 2007 jigdo", | 36 { "drwxr-xr-x 4 (?) (?) 4096 Apr 8 2007 jigdo", |
| 36 net::FtpDirectoryListingEntry::DIRECTORY, "jigdo", -1, | 37 net::FtpDirectoryListingEntry::DIRECTORY, "jigdo", -1, |
| 37 2007, 4, 8, 0, 0 }, | 38 2007, 4, 8, 0, 0 }, |
| 38 { "drwx-wx-wt 2 root wheel 512 Jul 1 02:15 incoming", | 39 { "drwx-wx-wt 2 root wheel 512 Jul 1 02:15 incoming", |
| 39 net::FtpDirectoryListingEntry::DIRECTORY, "incoming", -1, | 40 net::FtpDirectoryListingEntry::DIRECTORY, "incoming", -1, |
| 40 now_exploded.year, 7, 1, 2, 15 }, | 41 1994, 7, 1, 2, 15 }, |
| 41 { "-rw-r--r-- 1 2 3 3447432 May 18 2009 Foo - Manual.pdf", | 42 { "-rw-r--r-- 1 2 3 3447432 May 18 2009 Foo - Manual.pdf", |
| 42 net::FtpDirectoryListingEntry::FILE, "Foo - Manual.pdf", 3447432, | 43 net::FtpDirectoryListingEntry::FILE, "Foo - Manual.pdf", 3447432, |
| 43 2009, 5, 18, 0, 0 }, | 44 2009, 5, 18, 0, 0 }, |
| 44 { "d-wx-wx-wt+ 4 ftp 989 512 Dec 8 15:54 incoming", | 45 { "d-wx-wx-wt+ 4 ftp 989 512 Dec 8 15:54 incoming", |
| 45 net::FtpDirectoryListingEntry::DIRECTORY, "incoming", -1, | 46 net::FtpDirectoryListingEntry::DIRECTORY, "incoming", -1, |
| 46 now_exploded.year, 12, 8, 15, 54 }, | 47 1993, 12, 8, 15, 54 }, |
| 47 | 48 |
| 48 // Tests for the wu-ftpd variant: | 49 // Tests for the wu-ftpd variant: |
| 49 { "drwxr-xr-x 2 sys 512 Mar 27 2009 pub", | 50 { "drwxr-xr-x 2 sys 512 Mar 27 2009 pub", |
| 50 net::FtpDirectoryListingEntry::DIRECTORY, "pub", -1, | 51 net::FtpDirectoryListingEntry::DIRECTORY, "pub", -1, |
| 51 2009, 3, 27, 0, 0 }, | 52 2009, 3, 27, 0, 0 }, |
| 52 { "lrwxrwxrwx 0 0 26 Sep 18 2008 pub -> vol/1/.CLUSTER/var_ftp/pub", | 53 { "lrwxrwxrwx 0 0 26 Sep 18 2008 pub -> vol/1/.CLUSTER/var_ftp/pub", |
| 53 net::FtpDirectoryListingEntry::SYMLINK, "pub", -1, | 54 net::FtpDirectoryListingEntry::SYMLINK, "pub", -1, |
| 54 2008, 9, 18, 0, 0 }, | 55 2008, 9, 18, 0, 0 }, |
| 55 { "drwxr-xr-x (?) (?) 4096 Apr 8 2007 jigdo", | 56 { "drwxr-xr-x (?) (?) 4096 Apr 8 2007 jigdo", |
| 56 net::FtpDirectoryListingEntry::DIRECTORY, "jigdo", -1, | 57 net::FtpDirectoryListingEntry::DIRECTORY, "jigdo", -1, |
| 57 2007, 4, 8, 0, 0 }, | 58 2007, 4, 8, 0, 0 }, |
| 58 { "-rw-r--r-- 2 3 3447432 May 18 2009 Foo - Manual.pdf", | 59 { "-rw-r--r-- 2 3 3447432 May 18 2009 Foo - Manual.pdf", |
| 59 net::FtpDirectoryListingEntry::FILE, "Foo - Manual.pdf", 3447432, | 60 net::FtpDirectoryListingEntry::FILE, "Foo - Manual.pdf", 3447432, |
| 60 2009, 5, 18, 0, 0 }, | 61 2009, 5, 18, 0, 0 }, |
| 61 | 62 |
| 62 // Tests for "ls -l" style listings sent by an OS/2 server (FtpServer): | 63 // Tests for "ls -l" style listings sent by an OS/2 server (FtpServer): |
| 63 { "-r--r--r-- 1 ftp -A--- 13274 Mar 1 2006 UpTime.exe", | 64 { "-r--r--r-- 1 ftp -A--- 13274 Mar 1 2006 UpTime.exe", |
| 64 net::FtpDirectoryListingEntry::FILE, "UpTime.exe", 13274, | 65 net::FtpDirectoryListingEntry::FILE, "UpTime.exe", 13274, |
| 65 2006, 3, 1, 0, 0 }, | 66 2006, 3, 1, 0, 0 }, |
| 66 { "dr--r--r-- 1 ftp ----- 0 Nov 17 17:08 kernels", | 67 { "dr--r--r-- 1 ftp ----- 0 Nov 17 17:08 kernels", |
| 67 net::FtpDirectoryListingEntry::DIRECTORY, "kernels", -1, | 68 net::FtpDirectoryListingEntry::DIRECTORY, "kernels", -1, |
| 68 now_exploded.year, 11, 17, 17, 8 }, | 69 1993, 11, 17, 17, 8 }, |
| 69 | 70 |
| 70 // Tests for "ls -l" style listing sent by Xplain FTP Server. | 71 // Tests for "ls -l" style listing sent by Xplain FTP Server. |
| 71 { "drwxr-xr-x folder 0 Jul 17 2006 online", | 72 { "drwxr-xr-x folder 0 Jul 17 2006 online", |
| 72 net::FtpDirectoryListingEntry::DIRECTORY, "online", -1, | 73 net::FtpDirectoryListingEntry::DIRECTORY, "online", -1, |
| 73 2006, 7, 17, 0, 0 }, | 74 2006, 7, 17, 0, 0 }, |
| 74 }; | 75 }; |
| 75 for (size_t i = 0; i < arraysize(good_cases); i++) { | 76 for (size_t i = 0; i < arraysize(good_cases); i++) { |
| 76 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, good_cases[i].input)); | 77 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, good_cases[i].input)); |
| 77 | 78 |
| 78 net::FtpDirectoryListingParserLs parser; | 79 net::FtpDirectoryListingParserLs parser(mock_current_time); |
| 79 RunSingleLineTestCase(&parser, good_cases[i]); | 80 RunSingleLineTestCase(&parser, good_cases[i]); |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 | 83 |
| 83 TEST_F(FtpDirectoryListingParserLsTest, Bad) { | 84 TEST_F(FtpDirectoryListingParserLsTest, Bad) { |
| 85 base::Time mock_current_time; |
| 86 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994 12:45:26 GMT", |
| 87 &mock_current_time)); |
| 88 |
| 84 const char* bad_cases[] = { | 89 const char* bad_cases[] = { |
| 85 "garbage", | 90 "garbage", |
| 86 "-rw-r--r-- ftp ftp", | 91 "-rw-r--r-- ftp ftp", |
| 87 "-rw-r--rgb ftp ftp 528 Nov 01 2007 README", | 92 "-rw-r--rgb ftp ftp 528 Nov 01 2007 README", |
| 88 "-rw-rgbr-- ftp ftp 528 Nov 01 2007 README", | 93 "-rw-rgbr-- ftp ftp 528 Nov 01 2007 README", |
| 89 "qrwwr--r-- ftp ftp 528 Nov 01 2007 README", | 94 "qrwwr--r-- ftp ftp 528 Nov 01 2007 README", |
| 90 "-rw-r--r-- ftp ftp -528 Nov 01 2007 README", | 95 "-rw-r--r-- ftp ftp -528 Nov 01 2007 README", |
| 91 "-rw-r--r-- ftp ftp 528 Foo 01 2007 README", | 96 "-rw-r--r-- ftp ftp 528 Foo 01 2007 README", |
| 92 "-rw-r--r-- 1 ftp ftp", | 97 "-rw-r--r-- 1 ftp ftp", |
| 93 "-rw-r--rgb 1 ftp ftp 528 Nov 01 2007 README", | 98 "-rw-r--rgb 1 ftp ftp 528 Nov 01 2007 README", |
| 94 "-rw-rgbr-- 1 ftp ftp 528 Nov 01 2007 README", | 99 "-rw-rgbr-- 1 ftp ftp 528 Nov 01 2007 README", |
| 95 "qrwwr--r-- 1 ftp ftp 528 Nov 01 2007 README", | 100 "qrwwr--r-- 1 ftp ftp 528 Nov 01 2007 README", |
| 96 "-rw-r--r-- 1 ftp ftp -528 Nov 01 2007 README", | 101 "-rw-r--r-- 1 ftp ftp -528 Nov 01 2007 README", |
| 97 "-rw-r--r-- 1 ftp ftp 528 Foo 01 2007 README", | 102 "-rw-r--r-- 1 ftp ftp 528 Foo 01 2007 README", |
| 98 | 103 |
| 99 "d-wx-wx-wt++ 4 ftp 989 512 Dec 8 15:54 incoming", | 104 "d-wx-wx-wt++ 4 ftp 989 512 Dec 8 15:54 incoming", |
| 100 "d-wx-wx-wt$ 4 ftp 989 512 Dec 8 15:54 incoming", | 105 "d-wx-wx-wt$ 4 ftp 989 512 Dec 8 15:54 incoming", |
| 101 "-qqqqqqqqq+ 2 sys 512 Mar 27 2009 pub", | 106 "-qqqqqqqqq+ 2 sys 512 Mar 27 2009 pub", |
| 102 | 107 |
| 103 // Tests important for security: verify that after we detect the column | 108 // Tests important for security: verify that after we detect the column |
| 104 // offset we don't try to access invalid memory on malformed input. | 109 // offset we don't try to access invalid memory on malformed input. |
| 105 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11", | 110 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11", |
| 106 "drwxr-xr-x 3 ftp 4096 May 15 18:11", | 111 "drwxr-xr-x 3 ftp 4096 May 15 18:11", |
| 107 "drwxr-xr-x folder 0 May 15 18:11", | 112 "drwxr-xr-x folder 0 May 15 18:11", |
| 108 }; | 113 }; |
| 109 for (size_t i = 0; i < arraysize(bad_cases); i++) { | 114 for (size_t i = 0; i < arraysize(bad_cases); i++) { |
| 110 net::FtpDirectoryListingParserLs parser; | 115 net::FtpDirectoryListingParserLs parser(mock_current_time); |
| 111 EXPECT_FALSE(parser.ConsumeLine(UTF8ToUTF16(bad_cases[i]))) << bad_cases[i]; | 116 EXPECT_FALSE(parser.ConsumeLine(UTF8ToUTF16(bad_cases[i]))) << bad_cases[i]; |
| 112 } | 117 } |
| 113 } | 118 } |
| 114 | 119 |
| 115 } // namespace | 120 } // namespace |
| OLD | NEW |