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 #ifndef NET_FTP_FTP_DIRECTORY_LISTING_PARSER_UNITTEST_H_
6 #define NET_FTP_FTP_DIRECTORY_LISTING_PARSER_UNITTEST_H_
7 
8 #include <stdint.h>
9 
10 #include <vector>
11 
12 #include "base/strings/utf_string_conversions.h"
13 #include "net/ftp/ftp_directory_listing_parser.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace net {
17 
18 class FtpDirectoryListingParserTest : public testing::Test {
19  public:
20   struct SingleLineTestData {
21     const char* input;
22     FtpDirectoryListingEntry::Type type;
23     const char* filename;
24     int64_t size;
25     int year;
26     int month;
27     int day_of_month;
28     int hour;
29     int minute;
30   };
31 
32  protected:
FtpDirectoryListingParserTest()33   FtpDirectoryListingParserTest() {}
34 
GetSingleLineTestCase(const std::string & text)35   std::vector<base::string16> GetSingleLineTestCase(const std::string& text) {
36     std::vector<base::string16> lines;
37     lines.push_back(base::UTF8ToUTF16(text));
38     return lines;
39   }
40 
VerifySingleLineTestCase(const SingleLineTestData & test_case,const std::vector<FtpDirectoryListingEntry> & entries)41   void VerifySingleLineTestCase(
42       const SingleLineTestData& test_case,
43       const std::vector<FtpDirectoryListingEntry>& entries) {
44     ASSERT_FALSE(entries.empty());
45 
46     FtpDirectoryListingEntry entry = entries[0];
47     EXPECT_EQ(test_case.type, entry.type);
48     EXPECT_EQ(base::UTF8ToUTF16(test_case.filename), entry.name);
49     EXPECT_EQ(test_case.size, entry.size);
50 
51     base::Time::Exploded time_exploded;
52     entry.last_modified.UTCExplode(&time_exploded);
53 
54     // Only test members displayed on the directory listing.
55     EXPECT_EQ(test_case.year, time_exploded.year);
56     EXPECT_EQ(test_case.month, time_exploded.month);
57     EXPECT_EQ(test_case.day_of_month, time_exploded.day_of_month);
58     EXPECT_EQ(test_case.hour, time_exploded.hour);
59     EXPECT_EQ(test_case.minute, time_exploded.minute);
60 
61     EXPECT_EQ(1U, entries.size());
62   }
63 
GetMockCurrentTime()64   base::Time GetMockCurrentTime() {
65     base::Time::Exploded mock_current_time_exploded = { 0 };
66     mock_current_time_exploded.year = 1994;
67     mock_current_time_exploded.month = 11;
68     mock_current_time_exploded.day_of_month = 15;
69     mock_current_time_exploded.hour = 12;
70     mock_current_time_exploded.minute = 45;
71 
72     base::Time out_time;
73     EXPECT_TRUE(
74         base::Time::FromUTCExploded(mock_current_time_exploded, &out_time));
75     return out_time;
76   }
77 };
78 
79 }  // namespace net
80 
81 #endif  // NET_FTP_FTP_DIRECTORY_LISTING_PARSER_UNITTEST_H_
82