OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
| 8 #include "base/string_number_conversions.h" |
8 #include "chrome/browser/browser_thread.h" | 9 #include "chrome/browser/browser_thread.h" |
9 #include "chrome/browser/download/base_file.h" | 10 #include "chrome/browser/download/base_file.h" |
10 #include "net/base/file_stream.h" | 11 #include "net/base/file_stream.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 const char kTestData1[] = "Let's write some data to the file!\n"; | 16 const char kTestData1[] = "Let's write some data to the file!\n"; |
16 const char kTestData2[] = "Writing more data.\n"; | 17 const char kTestData2[] = "Writing more data.\n"; |
17 const char kTestData3[] = "Final line."; | 18 const char kTestData3[] = "Final line."; |
18 | 19 |
19 class BaseFileTest : public testing::Test { | 20 class BaseFileTest : public testing::Test { |
20 public: | 21 public: |
21 BaseFileTest() : file_thread_(BrowserThread::FILE, &message_loop_) { | 22 BaseFileTest() : file_thread_(BrowserThread::FILE, &message_loop_) { |
22 } | 23 } |
23 | 24 |
24 virtual void SetUp() { | 25 virtual void SetUp() { |
25 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 26 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
26 base_file_.reset(new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_)); | 27 base_file_.reset( |
| 28 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_)); |
27 } | 29 } |
28 | 30 |
29 virtual void TearDown() { | 31 virtual void TearDown() { |
30 EXPECT_FALSE(base_file_->in_progress()); | 32 EXPECT_FALSE(base_file_->in_progress()); |
31 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 33 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
32 base_file_->bytes_so_far()); | 34 base_file_->bytes_so_far()); |
33 | 35 |
34 if (!expected_data_.empty()) { | 36 if (!expected_data_.empty()) { |
35 // Make sure the data has been properly written to disk. | 37 // Make sure the data has been properly written to disk. |
36 std::string disk_data; | 38 std::string disk_data; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 // Test the most basic scenario: just create the object and do a sanity check | 75 // Test the most basic scenario: just create the object and do a sanity check |
74 // on all its accessors. This is actually a case that rarely happens | 76 // on all its accessors. This is actually a case that rarely happens |
75 // in production, where we would at least Initialize it. | 77 // in production, where we would at least Initialize it. |
76 TEST_F(BaseFileTest, CreateDestroy) { | 78 TEST_F(BaseFileTest, CreateDestroy) { |
77 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); | 79 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); |
78 EXPECT_FALSE(base_file_->path_renamed()); | 80 EXPECT_FALSE(base_file_->path_renamed()); |
79 } | 81 } |
80 | 82 |
81 // Cancel the download explicitly. | 83 // Cancel the download explicitly. |
82 TEST_F(BaseFileTest, Cancel) { | 84 TEST_F(BaseFileTest, Cancel) { |
83 ASSERT_TRUE(base_file_->Initialize()); | 85 ASSERT_TRUE(base_file_->Initialize(false)); |
84 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); | 86 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); |
85 base_file_->Cancel(); | 87 base_file_->Cancel(); |
86 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); | 88 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); |
87 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); | 89 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); |
88 EXPECT_FALSE(base_file_->path_renamed()); | 90 EXPECT_FALSE(base_file_->path_renamed()); |
89 } | 91 } |
90 | 92 |
91 // Write data to the file once. | 93 // Write data to the file once. |
92 TEST_F(BaseFileTest, SingleWrite) { | 94 TEST_F(BaseFileTest, SingleWrite) { |
93 ASSERT_TRUE(base_file_->Initialize()); | 95 ASSERT_TRUE(base_file_->Initialize(false)); |
94 AppendDataToFile(kTestData1); | 96 AppendDataToFile(kTestData1); |
95 base_file_->Finish(); | 97 base_file_->Finish(); |
96 | 98 |
97 EXPECT_FALSE(base_file_->path_renamed()); | 99 EXPECT_FALSE(base_file_->path_renamed()); |
98 } | 100 } |
99 | 101 |
100 // Write data to the file multiple times. | 102 // Write data to the file multiple times. |
101 TEST_F(BaseFileTest, MultipleWrites) { | 103 TEST_F(BaseFileTest, MultipleWrites) { |
102 ASSERT_TRUE(base_file_->Initialize()); | 104 ASSERT_TRUE(base_file_->Initialize(false)); |
103 AppendDataToFile(kTestData1); | 105 AppendDataToFile(kTestData1); |
104 AppendDataToFile(kTestData2); | 106 AppendDataToFile(kTestData2); |
105 AppendDataToFile(kTestData3); | 107 AppendDataToFile(kTestData3); |
| 108 std::string hash; |
| 109 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
106 base_file_->Finish(); | 110 base_file_->Finish(); |
107 | 111 |
108 EXPECT_FALSE(base_file_->path_renamed()); | 112 EXPECT_FALSE(base_file_->path_renamed()); |
109 } | 113 } |
110 | 114 |
| 115 // Write data to the file once and calculate its sha256 hash. |
| 116 TEST_F(BaseFileTest, SingleWriteWithHash) { |
| 117 ASSERT_TRUE(base_file_->Initialize(true)); |
| 118 AppendDataToFile(kTestData1); |
| 119 base_file_->Finish(); |
| 120 |
| 121 EXPECT_FALSE(base_file_->path_renamed()); |
| 122 |
| 123 std::string hash; |
| 124 base_file_->GetSha256Hash(&hash); |
| 125 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
| 126 base::HexEncode(hash.data(), hash.size())); |
| 127 } |
| 128 |
| 129 // Write data to the file multiple times and calculate its sha256 hash. |
| 130 TEST_F(BaseFileTest, MultipleWritesWithHash) { |
| 131 std::string hash; |
| 132 |
| 133 ASSERT_TRUE(base_file_->Initialize(true)); |
| 134 AppendDataToFile(kTestData1); |
| 135 AppendDataToFile(kTestData2); |
| 136 AppendDataToFile(kTestData3); |
| 137 // no hash before Finish() is called either. |
| 138 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
| 139 base_file_->Finish(); |
| 140 |
| 141 EXPECT_FALSE(base_file_->path_renamed()); |
| 142 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); |
| 143 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", |
| 144 base::HexEncode(hash.data(), hash.size())); |
| 145 } |
| 146 |
111 // Rename the file after all writes to it. | 147 // Rename the file after all writes to it. |
112 TEST_F(BaseFileTest, WriteThenRename) { | 148 TEST_F(BaseFileTest, WriteThenRename) { |
113 ASSERT_TRUE(base_file_->Initialize()); | 149 ASSERT_TRUE(base_file_->Initialize(false)); |
114 | 150 |
115 FilePath initial_path(base_file_->full_path()); | 151 FilePath initial_path(base_file_->full_path()); |
116 EXPECT_TRUE(file_util::PathExists(initial_path)); | 152 EXPECT_TRUE(file_util::PathExists(initial_path)); |
117 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 153 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
118 EXPECT_FALSE(file_util::PathExists(new_path)); | 154 EXPECT_FALSE(file_util::PathExists(new_path)); |
119 | 155 |
120 AppendDataToFile(kTestData1); | 156 AppendDataToFile(kTestData1); |
121 | 157 |
122 EXPECT_TRUE(base_file_->Rename(new_path, true)); | 158 EXPECT_TRUE(base_file_->Rename(new_path, true)); |
123 EXPECT_FALSE(file_util::PathExists(initial_path)); | 159 EXPECT_FALSE(file_util::PathExists(initial_path)); |
124 EXPECT_TRUE(file_util::PathExists(new_path)); | 160 EXPECT_TRUE(file_util::PathExists(new_path)); |
125 | 161 |
126 base_file_->Finish(); | 162 base_file_->Finish(); |
127 | 163 |
128 EXPECT_TRUE(base_file_->path_renamed()); | 164 EXPECT_TRUE(base_file_->path_renamed()); |
129 } | 165 } |
130 | 166 |
131 // Rename the file while the download is still in progress. | 167 // Rename the file while the download is still in progress. |
132 TEST_F(BaseFileTest, RenameWhileInProgress) { | 168 TEST_F(BaseFileTest, RenameWhileInProgress) { |
133 ASSERT_TRUE(base_file_->Initialize()); | 169 ASSERT_TRUE(base_file_->Initialize(false)); |
134 | 170 |
135 FilePath initial_path(base_file_->full_path()); | 171 FilePath initial_path(base_file_->full_path()); |
136 EXPECT_TRUE(file_util::PathExists(initial_path)); | 172 EXPECT_TRUE(file_util::PathExists(initial_path)); |
137 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 173 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
138 EXPECT_FALSE(file_util::PathExists(new_path)); | 174 EXPECT_FALSE(file_util::PathExists(new_path)); |
139 | 175 |
140 AppendDataToFile(kTestData1); | 176 AppendDataToFile(kTestData1); |
141 | 177 |
142 EXPECT_TRUE(base_file_->in_progress()); | 178 EXPECT_TRUE(base_file_->in_progress()); |
143 EXPECT_TRUE(base_file_->Rename(new_path, true)); | 179 EXPECT_TRUE(base_file_->Rename(new_path, true)); |
144 EXPECT_FALSE(file_util::PathExists(initial_path)); | 180 EXPECT_FALSE(file_util::PathExists(initial_path)); |
145 EXPECT_TRUE(file_util::PathExists(new_path)); | 181 EXPECT_TRUE(file_util::PathExists(new_path)); |
146 | 182 |
147 AppendDataToFile(kTestData2); | 183 AppendDataToFile(kTestData2); |
148 | 184 |
149 base_file_->Finish(); | 185 base_file_->Finish(); |
150 | 186 |
151 EXPECT_TRUE(base_file_->path_renamed()); | 187 EXPECT_TRUE(base_file_->path_renamed()); |
152 } | 188 } |
153 | 189 |
154 } // namespace | 190 } // namespace |
OLD | NEW |