OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/download/base_file.h" |
| 6 |
5 #include "base/file_util.h" | 7 #include "base/file_util.h" |
6 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
7 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
8 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "chrome/browser/download/download_util.h" |
9 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
10 #include "content/browser/download/base_file.h" | |
11 #include "net/base/file_stream.h" | 13 #include "net/base/file_stream.h" |
| 14 #include "net/base/mock_file_stream.h" |
| 15 #include "net/base/net_errors.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
13 | 17 |
14 namespace { | 18 namespace { |
15 | 19 |
16 const char kTestData1[] = "Let's write some data to the file!\n"; | 20 const char kTestData1[] = "Let's write some data to the file!\n"; |
17 const char kTestData2[] = "Writing more data.\n"; | 21 const char kTestData2[] = "Writing more data.\n"; |
18 const char kTestData3[] = "Final line."; | 22 const char kTestData3[] = "Final line."; |
19 | 23 |
| 24 } // namespace |
| 25 |
20 class BaseFileTest : public testing::Test { | 26 class BaseFileTest : public testing::Test { |
21 public: | 27 public: |
22 BaseFileTest() | 28 BaseFileTest() |
23 : expect_file_survives_(false), | 29 : expect_file_survives_(false), |
24 file_thread_(BrowserThread::FILE, &message_loop_) { | 30 file_thread_(BrowserThread::FILE, &message_loop_) { |
25 } | 31 } |
26 | 32 |
27 virtual void SetUp() { | 33 virtual void SetUp() { |
28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
29 base_file_.reset( | 35 base_file_.reset( |
(...skipping 14 matching lines...) Expand all Loading... |
44 EXPECT_EQ(expected_data_, disk_data); | 50 EXPECT_EQ(expected_data_, disk_data); |
45 } | 51 } |
46 | 52 |
47 // Make sure the mock BrowserThread outlives the BaseFile to satisfy | 53 // Make sure the mock BrowserThread outlives the BaseFile to satisfy |
48 // thread checks inside it. | 54 // thread checks inside it. |
49 base_file_.reset(); | 55 base_file_.reset(); |
50 | 56 |
51 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path)); | 57 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path)); |
52 } | 58 } |
53 | 59 |
54 void AppendDataToFile(const std::string& data) { | 60 bool OpenMockFileStream() { |
55 ASSERT_TRUE(base_file_->in_progress()); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
56 base_file_->AppendDataToFile(data.data(), data.size()); | 62 |
| 63 FilePath path; |
| 64 if (!file_util::CreateTemporaryFile(&path)) |
| 65 return false; |
| 66 |
| 67 // Create a new file stream. |
| 68 mock_file_stream_.reset(new testing::MockFileStream); |
| 69 if (mock_file_stream_->Open( |
| 70 path, |
| 71 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE) != 0) { |
| 72 mock_file_stream_.reset(); |
| 73 return false; |
| 74 } |
| 75 |
| 76 return true; |
| 77 } |
| 78 |
| 79 void ForceError(net::Error error) { |
| 80 mock_file_stream_->set_forced_error(error); |
| 81 } |
| 82 |
| 83 bool AppendDataToFile(const std::string& data) { |
| 84 EXPECT_TRUE(base_file_->in_progress()); |
| 85 bool appended = base_file_->AppendDataToFile(data.data(), data.size()); |
57 expected_data_ += data; | 86 expected_data_ += data; |
58 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 87 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
59 base_file_->bytes_so_far()); | 88 base_file_->bytes_so_far()); |
| 89 return appended; |
60 } | 90 } |
61 | 91 |
62 protected: | 92 protected: |
63 linked_ptr<net::FileStream> file_stream_; | 93 linked_ptr<net::FileStream> file_stream_; |
| 94 linked_ptr<testing::MockFileStream> mock_file_stream_; |
64 | 95 |
65 // BaseClass instance we are testing. | 96 // BaseClass instance we are testing. |
66 scoped_ptr<BaseFile> base_file_; | 97 scoped_ptr<BaseFile> base_file_; |
67 | 98 |
68 // Temporary directory for renamed downloads. | 99 // Temporary directory for renamed downloads. |
69 ScopedTempDir temp_dir_; | 100 ScopedTempDir temp_dir_; |
70 | 101 |
71 // Expect the file to survive deletion of the BaseFile instance. | 102 // Expect the file to survive deletion of the BaseFile instance. |
72 bool expect_file_survives_; | 103 bool expect_file_survives_; |
73 | 104 |
(...skipping 19 matching lines...) Expand all Loading... |
93 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); | 124 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); |
94 base_file_->Cancel(); | 125 base_file_->Cancel(); |
95 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); | 126 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); |
96 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); | 127 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); |
97 } | 128 } |
98 | 129 |
99 // Write data to the file and detach it, so it doesn't get deleted | 130 // Write data to the file and detach it, so it doesn't get deleted |
100 // automatically when base_file_ is destructed. | 131 // automatically when base_file_ is destructed. |
101 TEST_F(BaseFileTest, WriteAndDetach) { | 132 TEST_F(BaseFileTest, WriteAndDetach) { |
102 ASSERT_TRUE(base_file_->Initialize(false)); | 133 ASSERT_TRUE(base_file_->Initialize(false)); |
103 AppendDataToFile(kTestData1); | 134 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
104 base_file_->Finish(); | 135 base_file_->Finish(); |
105 base_file_->Detach(); | 136 base_file_->Detach(); |
106 expect_file_survives_ = true; | 137 expect_file_survives_ = true; |
107 } | 138 } |
108 | 139 |
109 // Write data to the file and detach it, and calculate its sha256 hash. | 140 // Write data to the file and detach it, and calculate its sha256 hash. |
110 TEST_F(BaseFileTest, WriteWithHashAndDetach) { | 141 TEST_F(BaseFileTest, WriteWithHashAndDetach) { |
111 ASSERT_TRUE(base_file_->Initialize(true)); | 142 ASSERT_TRUE(base_file_->Initialize(true)); |
112 AppendDataToFile(kTestData1); | 143 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
113 base_file_->Finish(); | 144 base_file_->Finish(); |
114 | 145 |
115 std::string hash; | 146 std::string hash; |
116 base_file_->GetSha256Hash(&hash); | 147 base_file_->GetSha256Hash(&hash); |
117 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 148 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
118 base::HexEncode(hash.data(), hash.size())); | 149 base::HexEncode(hash.data(), hash.size())); |
119 | 150 |
120 base_file_->Detach(); | 151 base_file_->Detach(); |
121 expect_file_survives_ = true; | 152 expect_file_survives_ = true; |
122 } | 153 } |
123 | 154 |
124 // Rename the file after writing to it, then detach. | 155 // Rename the file after writing to it, then detach. |
125 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { | 156 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { |
126 ASSERT_TRUE(base_file_->Initialize(false)); | 157 ASSERT_TRUE(base_file_->Initialize(false)); |
127 | 158 |
128 FilePath initial_path(base_file_->full_path()); | 159 FilePath initial_path(base_file_->full_path()); |
129 EXPECT_TRUE(file_util::PathExists(initial_path)); | 160 EXPECT_TRUE(file_util::PathExists(initial_path)); |
130 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 161 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
131 EXPECT_FALSE(file_util::PathExists(new_path)); | 162 EXPECT_FALSE(file_util::PathExists(new_path)); |
132 | 163 |
133 AppendDataToFile(kTestData1); | 164 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
134 | 165 |
135 EXPECT_TRUE(base_file_->Rename(new_path)); | 166 EXPECT_TRUE(base_file_->Rename(new_path)); |
136 EXPECT_FALSE(file_util::PathExists(initial_path)); | 167 EXPECT_FALSE(file_util::PathExists(initial_path)); |
137 EXPECT_TRUE(file_util::PathExists(new_path)); | 168 EXPECT_TRUE(file_util::PathExists(new_path)); |
138 | 169 |
139 base_file_->Finish(); | 170 base_file_->Finish(); |
140 base_file_->Detach(); | 171 base_file_->Detach(); |
141 expect_file_survives_ = true; | 172 expect_file_survives_ = true; |
142 } | 173 } |
143 | 174 |
144 // Write data to the file once. | 175 // Write data to the file once. |
145 TEST_F(BaseFileTest, SingleWrite) { | 176 TEST_F(BaseFileTest, SingleWrite) { |
146 ASSERT_TRUE(base_file_->Initialize(false)); | 177 ASSERT_TRUE(base_file_->Initialize(false)); |
147 AppendDataToFile(kTestData1); | 178 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
148 base_file_->Finish(); | 179 base_file_->Finish(); |
149 } | 180 } |
150 | 181 |
151 // Write data to the file multiple times. | 182 // Write data to the file multiple times. |
152 TEST_F(BaseFileTest, MultipleWrites) { | 183 TEST_F(BaseFileTest, MultipleWrites) { |
153 ASSERT_TRUE(base_file_->Initialize(false)); | 184 ASSERT_TRUE(base_file_->Initialize(false)); |
154 AppendDataToFile(kTestData1); | 185 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
155 AppendDataToFile(kTestData2); | 186 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
156 AppendDataToFile(kTestData3); | 187 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
157 std::string hash; | 188 std::string hash; |
158 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 189 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
159 base_file_->Finish(); | 190 base_file_->Finish(); |
160 } | 191 } |
161 | 192 |
162 // Write data to the file once and calculate its sha256 hash. | 193 // Write data to the file once and calculate its sha256 hash. |
163 TEST_F(BaseFileTest, SingleWriteWithHash) { | 194 TEST_F(BaseFileTest, SingleWriteWithHash) { |
164 ASSERT_TRUE(base_file_->Initialize(true)); | 195 ASSERT_TRUE(base_file_->Initialize(true)); |
165 AppendDataToFile(kTestData1); | 196 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
166 base_file_->Finish(); | 197 base_file_->Finish(); |
167 | 198 |
168 std::string hash; | 199 std::string hash; |
169 base_file_->GetSha256Hash(&hash); | 200 base_file_->GetSha256Hash(&hash); |
170 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 201 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
171 base::HexEncode(hash.data(), hash.size())); | 202 base::HexEncode(hash.data(), hash.size())); |
172 } | 203 } |
173 | 204 |
174 // Write data to the file multiple times and calculate its sha256 hash. | 205 // Write data to the file multiple times and calculate its sha256 hash. |
175 TEST_F(BaseFileTest, MultipleWritesWithHash) { | 206 TEST_F(BaseFileTest, MultipleWritesWithHash) { |
176 std::string hash; | 207 std::string hash; |
177 | 208 |
178 ASSERT_TRUE(base_file_->Initialize(true)); | 209 ASSERT_TRUE(base_file_->Initialize(true)); |
179 AppendDataToFile(kTestData1); | 210 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
180 AppendDataToFile(kTestData2); | 211 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
181 AppendDataToFile(kTestData3); | 212 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
182 // no hash before Finish() is called either. | 213 // no hash before Finish() is called either. |
183 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 214 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
184 base_file_->Finish(); | 215 base_file_->Finish(); |
185 | 216 |
186 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); | 217 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); |
187 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", | 218 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", |
188 base::HexEncode(hash.data(), hash.size())); | 219 base::HexEncode(hash.data(), hash.size())); |
189 } | 220 } |
190 | 221 |
191 // Rename the file after all writes to it. | 222 // Rename the file after all writes to it. |
192 TEST_F(BaseFileTest, WriteThenRename) { | 223 TEST_F(BaseFileTest, WriteThenRename) { |
193 ASSERT_TRUE(base_file_->Initialize(false)); | 224 ASSERT_TRUE(base_file_->Initialize(false)); |
194 | 225 |
195 FilePath initial_path(base_file_->full_path()); | 226 FilePath initial_path(base_file_->full_path()); |
196 EXPECT_TRUE(file_util::PathExists(initial_path)); | 227 EXPECT_TRUE(file_util::PathExists(initial_path)); |
197 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 228 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
198 EXPECT_FALSE(file_util::PathExists(new_path)); | 229 EXPECT_FALSE(file_util::PathExists(new_path)); |
199 | 230 |
200 AppendDataToFile(kTestData1); | 231 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
201 | 232 |
202 EXPECT_TRUE(base_file_->Rename(new_path)); | 233 EXPECT_TRUE(base_file_->Rename(new_path)); |
203 EXPECT_FALSE(file_util::PathExists(initial_path)); | 234 EXPECT_FALSE(file_util::PathExists(initial_path)); |
204 EXPECT_TRUE(file_util::PathExists(new_path)); | 235 EXPECT_TRUE(file_util::PathExists(new_path)); |
205 | 236 |
206 base_file_->Finish(); | 237 base_file_->Finish(); |
207 } | 238 } |
208 | 239 |
209 // Rename the file while the download is still in progress. | 240 // Rename the file while the download is still in progress. |
210 TEST_F(BaseFileTest, RenameWhileInProgress) { | 241 TEST_F(BaseFileTest, RenameWhileInProgress) { |
211 ASSERT_TRUE(base_file_->Initialize(false)); | 242 ASSERT_TRUE(base_file_->Initialize(false)); |
212 | 243 |
213 FilePath initial_path(base_file_->full_path()); | 244 FilePath initial_path(base_file_->full_path()); |
214 EXPECT_TRUE(file_util::PathExists(initial_path)); | 245 EXPECT_TRUE(file_util::PathExists(initial_path)); |
215 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 246 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
216 EXPECT_FALSE(file_util::PathExists(new_path)); | 247 EXPECT_FALSE(file_util::PathExists(new_path)); |
217 | 248 |
218 AppendDataToFile(kTestData1); | 249 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
219 | 250 |
220 EXPECT_TRUE(base_file_->in_progress()); | 251 EXPECT_TRUE(base_file_->in_progress()); |
221 EXPECT_TRUE(base_file_->Rename(new_path)); | 252 EXPECT_TRUE(base_file_->Rename(new_path)); |
222 EXPECT_FALSE(file_util::PathExists(initial_path)); | 253 EXPECT_FALSE(file_util::PathExists(initial_path)); |
223 EXPECT_TRUE(file_util::PathExists(new_path)); | 254 EXPECT_TRUE(file_util::PathExists(new_path)); |
224 | 255 |
225 AppendDataToFile(kTestData2); | 256 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
226 | 257 |
227 base_file_->Finish(); | 258 base_file_->Finish(); |
228 } | 259 } |
229 | 260 |
230 } // namespace | 261 // Write data to the file multiple times. |
| 262 TEST_F(BaseFileTest, MultipleWritesWithError) { |
| 263 ASSERT_TRUE(OpenMockFileStream()); |
| 264 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), |
| 265 GURL(), GURL(), 0, mock_file_stream_)); |
| 266 ASSERT_TRUE(base_file_->Initialize(false)); |
| 267 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 268 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 269 ForceError(net::ERR_FAILED); |
| 270 ASSERT_FALSE(AppendDataToFile(kTestData3)); |
| 271 std::string hash; |
| 272 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
| 273 base_file_->Finish(); |
| 274 } |
OLD | NEW |