OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" | 5 #include "content/browser/download/base_file.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/test/test_file_util.h" | 12 #include "base/test/test_file_util.h" |
13 #include "content/browser/browser_thread_impl.h" | 13 #include "content/browser/browser_thread_impl.h" |
| 14 #include "content/public/browser/download_interrupt_reasons.h" |
14 #include "crypto/secure_hash.h" | 15 #include "crypto/secure_hash.h" |
15 #include "net/base/file_stream.h" | 16 #include "net/base/file_stream.h" |
16 #include "net/base/mock_file_stream.h" | 17 #include "net/base/mock_file_stream.h" |
17 #include "net/base/net_errors.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 using content::BrowserThread; | 20 using content::BrowserThread; |
21 using content::BrowserThreadImpl; | 21 using content::BrowserThreadImpl; |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const char kTestData1[] = "Let's write some data to the file!\n"; | 25 const char kTestData1[] = "Let's write some data to the file!\n"; |
26 const char kTestData2[] = "Writing more data.\n"; | 26 const char kTestData2[] = "Writing more data.\n"; |
27 const char kTestData3[] = "Final line."; | 27 const char kTestData3[] = "Final line."; |
28 const char kTestData4[] = "supercalifragilisticexpialidocious"; | 28 const char kTestData4[] = "supercalifragilisticexpialidocious"; |
29 const int kTestDataLength1 = arraysize(kTestData1) - 1; | 29 const int kTestDataLength1 = arraysize(kTestData1) - 1; |
30 const int kTestDataLength2 = arraysize(kTestData2) - 1; | 30 const int kTestDataLength2 = arraysize(kTestData2) - 1; |
31 const int kTestDataLength3 = arraysize(kTestData3) - 1; | 31 const int kTestDataLength3 = arraysize(kTestData3) - 1; |
32 const int kTestDataLength4 = arraysize(kTestData4) - 1; | 32 const int kTestDataLength4 = arraysize(kTestData4) - 1; |
33 const int kElapsedTimeSeconds = 5; | 33 const int kElapsedTimeSeconds = 5; |
34 const base::TimeDelta kElapsedTimeDelta = base::TimeDelta::FromSeconds( | 34 const base::TimeDelta kElapsedTimeDelta = base::TimeDelta::FromSeconds( |
35 kElapsedTimeSeconds); | 35 kElapsedTimeSeconds); |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 class BaseFileTest : public testing::Test { | 39 class BaseFileTest : public testing::Test { |
40 public: | 40 public: |
41 static const size_t kSha256HashLen = 32; | 41 static const size_t kSha256HashLen = 32; |
42 static const unsigned char kEmptySha256Hash[kSha256HashLen]; | 42 static const unsigned char kEmptySha256Hash[kSha256HashLen]; |
43 | 43 |
44 BaseFileTest() | 44 BaseFileTest() |
45 : expect_file_survives_(false), | 45 : expect_file_survives_(false), |
46 expect_in_progress_(true), | 46 expect_in_progress_(true), |
47 expected_error_(false), | 47 expected_error_(content::DOWNLOAD_INTERRUPT_REASON_NONE), |
48 file_thread_(BrowserThread::FILE, &message_loop_) { | 48 file_thread_(BrowserThread::FILE, &message_loop_) { |
49 } | 49 } |
50 | 50 |
51 virtual void SetUp() { | 51 virtual void SetUp() { |
52 ResetHash(); | 52 ResetHash(); |
53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
54 base_file_.reset(new BaseFile(FilePath(), | 54 base_file_.reset(new BaseFile(FilePath(), |
55 GURL(), | 55 GURL(), |
56 GURL(), | 56 GURL(), |
57 0, | 57 0, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 base_file_.reset(new BaseFile(FilePath(), | 105 base_file_.reset(new BaseFile(FilePath(), |
106 GURL(), | 106 GURL(), |
107 GURL(), | 107 GURL(), |
108 0, | 108 0, |
109 true, | 109 true, |
110 "", | 110 "", |
111 scoped_ptr<net::FileStream>(), | 111 scoped_ptr<net::FileStream>(), |
112 net::BoundNetLog())); | 112 net::BoundNetLog())); |
113 } | 113 } |
114 | 114 |
115 int AppendDataToFile(const std::string& data) { | 115 bool InitializeFile() { |
| 116 content::DownloadInterruptReason result = |
| 117 base_file_->Initialize(temp_dir_.path()); |
| 118 EXPECT_EQ(expected_error_, result); |
| 119 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; |
| 120 } |
| 121 |
| 122 bool AppendDataToFile(const std::string& data) { |
116 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); | 123 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); |
117 int appended = base_file_->AppendDataToFile(data.data(), data.size()); | 124 content::DownloadInterruptReason result = |
118 if (appended == net::OK) | 125 base_file_->AppendDataToFile(data.data(), data.size()); |
119 EXPECT_TRUE(expect_in_progress_) | 126 if (result == content::DOWNLOAD_INTERRUPT_REASON_NONE) |
120 << " appended = " << appended; | 127 EXPECT_TRUE(expect_in_progress_) << " result = " << result; |
| 128 |
| 129 EXPECT_EQ(expected_error_, result); |
121 if (base_file_->in_progress()) { | 130 if (base_file_->in_progress()) { |
122 expected_data_ += data; | 131 expected_data_ += data; |
123 if (!expected_error_) { | 132 if (expected_error_ == content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
124 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 133 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
125 base_file_->bytes_so_far()); | 134 base_file_->bytes_so_far()); |
126 } | 135 } |
127 } | 136 } |
128 return appended; | 137 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; |
129 } | 138 } |
130 | 139 |
131 void set_expected_data(const std::string& data) { expected_data_ = data; } | 140 void set_expected_data(const std::string& data) { expected_data_ = data; } |
132 | 141 |
133 // Helper functions. | 142 // Helper functions. |
134 // Create a file. Returns the complete file path. | 143 // Create a file. Returns the complete file path. |
135 FilePath CreateTestFile() { | 144 FilePath CreateTestFile() { |
136 FilePath file_name; | 145 FilePath file_name; |
137 BaseFile file(FilePath(), | 146 BaseFile file(FilePath(), |
138 GURL(), | 147 GURL(), |
139 GURL(), | 148 GURL(), |
140 0, | 149 0, |
141 false, | 150 false, |
142 "", | 151 "", |
143 scoped_ptr<net::FileStream>(), | 152 scoped_ptr<net::FileStream>(), |
144 net::BoundNetLog()); | 153 net::BoundNetLog()); |
145 | 154 |
146 EXPECT_EQ(net::OK, file.Initialize(temp_dir_.path())); | 155 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 156 file.Initialize(temp_dir_.path())); |
147 file_name = file.full_path(); | 157 file_name = file.full_path(); |
148 EXPECT_NE(FilePath::StringType(), file_name.value()); | 158 EXPECT_NE(FilePath::StringType(), file_name.value()); |
149 | 159 |
150 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); | 160 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 161 file.AppendDataToFile(kTestData4, kTestDataLength4)); |
151 | 162 |
152 // Keep the file from getting deleted when existing_file_name is deleted. | 163 // Keep the file from getting deleted when existing_file_name is deleted. |
153 file.Detach(); | 164 file.Detach(); |
154 | 165 |
155 return file_name; | 166 return file_name; |
156 } | 167 } |
157 | 168 |
158 // Create a file with the specified file name. | 169 // Create a file with the specified file name. |
159 void CreateFileWithName(const FilePath& file_name) { | 170 void CreateFileWithName(const FilePath& file_name) { |
160 EXPECT_NE(FilePath::StringType(), file_name.value()); | 171 EXPECT_NE(FilePath::StringType(), file_name.value()); |
161 BaseFile duplicate_file(file_name, | 172 BaseFile duplicate_file(file_name, |
162 GURL(), | 173 GURL(), |
163 GURL(), | 174 GURL(), |
164 0, | 175 0, |
165 false, | 176 false, |
166 "", | 177 "", |
167 scoped_ptr<net::FileStream>(), | 178 scoped_ptr<net::FileStream>(), |
168 net::BoundNetLog()); | 179 net::BoundNetLog()); |
169 EXPECT_EQ(net::OK, duplicate_file.Initialize(temp_dir_.path())); | 180 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 181 duplicate_file.Initialize(temp_dir_.path())); |
170 // Write something into it. | 182 // Write something into it. |
171 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 183 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
172 // Detach the file so it isn't deleted on destruction of |duplicate_file|. | 184 // Detach the file so it isn't deleted on destruction of |duplicate_file|. |
173 duplicate_file.Detach(); | 185 duplicate_file.Detach(); |
174 } | 186 } |
175 | 187 |
176 int64 CurrentSpeedAtTime(base::TimeTicks current_time) { | 188 int64 CurrentSpeedAtTime(base::TimeTicks current_time) { |
177 EXPECT_TRUE(base_file_.get()); | 189 EXPECT_TRUE(base_file_.get()); |
178 return base_file_->CurrentSpeedAtTime(current_time); | 190 return base_file_->CurrentSpeedAtTime(current_time); |
179 } | 191 } |
180 | 192 |
181 base::TimeTicks StartTick() { | 193 base::TimeTicks StartTick() { |
182 EXPECT_TRUE(base_file_.get()); | 194 EXPECT_TRUE(base_file_.get()); |
183 return base_file_->start_tick_; | 195 return base_file_->start_tick_; |
184 } | 196 } |
185 | 197 |
186 void set_expected_error(net::Error err) { | 198 void set_expected_error(content::DownloadInterruptReason err) { |
187 expected_error_ = err; | 199 expected_error_ = err; |
188 } | 200 } |
189 | 201 |
190 protected: | 202 protected: |
191 linked_ptr<net::testing::MockFileStream> mock_file_stream_; | 203 linked_ptr<net::testing::MockFileStream> mock_file_stream_; |
192 | 204 |
193 // BaseClass instance we are testing. | 205 // BaseClass instance we are testing. |
194 scoped_ptr<BaseFile> base_file_; | 206 scoped_ptr<BaseFile> base_file_; |
195 | 207 |
196 // Temporary directory for renamed downloads. | 208 // Temporary directory for renamed downloads. |
197 ScopedTempDir temp_dir_; | 209 ScopedTempDir temp_dir_; |
198 | 210 |
199 // Expect the file to survive deletion of the BaseFile instance. | 211 // Expect the file to survive deletion of the BaseFile instance. |
200 bool expect_file_survives_; | 212 bool expect_file_survives_; |
201 | 213 |
202 // Expect the file to be in progress. | 214 // Expect the file to be in progress. |
203 bool expect_in_progress_; | 215 bool expect_in_progress_; |
204 | 216 |
205 // Hash calculator. | 217 // Hash calculator. |
206 scoped_ptr<crypto::SecureHash> secure_hash_; | 218 scoped_ptr<crypto::SecureHash> secure_hash_; |
207 | 219 |
208 unsigned char sha256_hash_[kSha256HashLen]; | 220 unsigned char sha256_hash_[kSha256HashLen]; |
209 | 221 |
210 private: | 222 private: |
211 // Keep track of what data should be saved to the disk file. | 223 // Keep track of what data should be saved to the disk file. |
212 std::string expected_data_; | 224 std::string expected_data_; |
213 bool expected_error_; | 225 content::DownloadInterruptReason expected_error_; |
214 | 226 |
215 // Mock file thread to satisfy debug checks in BaseFile. | 227 // Mock file thread to satisfy debug checks in BaseFile. |
216 MessageLoop message_loop_; | 228 MessageLoop message_loop_; |
217 BrowserThreadImpl file_thread_; | 229 BrowserThreadImpl file_thread_; |
218 }; | 230 }; |
219 | 231 |
220 // This will initialize the entire array to zero. | 232 // This will initialize the entire array to zero. |
221 const unsigned char BaseFileTest::kEmptySha256Hash[] = { 0 }; | 233 const unsigned char BaseFileTest::kEmptySha256Hash[] = { 0 }; |
222 | 234 |
223 // Test the most basic scenario: just create the object and do a sanity check | 235 // Test the most basic scenario: just create the object and do a sanity check |
224 // on all its accessors. This is actually a case that rarely happens | 236 // on all its accessors. This is actually a case that rarely happens |
225 // in production, where we would at least Initialize it. | 237 // in production, where we would at least Initialize it. |
226 TEST_F(BaseFileTest, CreateDestroy) { | 238 TEST_F(BaseFileTest, CreateDestroy) { |
227 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); | 239 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); |
228 } | 240 } |
229 | 241 |
230 // Cancel the download explicitly. | 242 // Cancel the download explicitly. |
231 TEST_F(BaseFileTest, Cancel) { | 243 TEST_F(BaseFileTest, Cancel) { |
232 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 244 ASSERT_TRUE(InitializeFile()); |
233 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); | 245 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); |
234 base_file_->Cancel(); | 246 base_file_->Cancel(); |
235 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); | 247 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); |
236 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); | 248 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); |
237 } | 249 } |
238 | 250 |
239 // Write data to the file and detach it, so it doesn't get deleted | 251 // Write data to the file and detach it, so it doesn't get deleted |
240 // automatically when base_file_ is destructed. | 252 // automatically when base_file_ is destructed. |
241 TEST_F(BaseFileTest, WriteAndDetach) { | 253 TEST_F(BaseFileTest, WriteAndDetach) { |
242 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 254 ASSERT_TRUE(InitializeFile()); |
243 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 255 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
244 base_file_->Finish(); | 256 base_file_->Finish(); |
245 base_file_->Detach(); | 257 base_file_->Detach(); |
246 expect_file_survives_ = true; | 258 expect_file_survives_ = true; |
247 } | 259 } |
248 | 260 |
249 // Write data to the file and detach it, and calculate its sha256 hash. | 261 // Write data to the file and detach it, and calculate its sha256 hash. |
250 TEST_F(BaseFileTest, WriteWithHashAndDetach) { | 262 TEST_F(BaseFileTest, WriteWithHashAndDetach) { |
251 // Calculate the final hash. | 263 // Calculate the final hash. |
252 ResetHash(); | 264 ResetHash(); |
253 UpdateHash(kTestData1, kTestDataLength1); | 265 UpdateHash(kTestData1, kTestDataLength1); |
254 std::string expected_hash = GetFinalHash(); | 266 std::string expected_hash = GetFinalHash(); |
255 std::string expected_hash_hex = | 267 std::string expected_hash_hex = |
256 base::HexEncode(expected_hash.data(), expected_hash.size()); | 268 base::HexEncode(expected_hash.data(), expected_hash.size()); |
257 | 269 |
258 MakeFileWithHash(); | 270 MakeFileWithHash(); |
259 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 271 ASSERT_TRUE(InitializeFile()); |
260 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 272 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
261 base_file_->Finish(); | 273 base_file_->Finish(); |
262 | 274 |
263 std::string hash; | 275 std::string hash; |
264 base_file_->GetHash(&hash); | 276 base_file_->GetHash(&hash); |
265 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 277 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
266 expected_hash_hex); | 278 expected_hash_hex); |
267 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); | 279 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); |
268 | 280 |
269 base_file_->Detach(); | 281 base_file_->Detach(); |
270 expect_file_survives_ = true; | 282 expect_file_survives_ = true; |
271 } | 283 } |
272 | 284 |
273 // Rename the file after writing to it, then detach. | 285 // Rename the file after writing to it, then detach. |
274 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { | 286 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { |
275 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 287 ASSERT_TRUE(InitializeFile()); |
276 | 288 |
277 FilePath initial_path(base_file_->full_path()); | 289 FilePath initial_path(base_file_->full_path()); |
278 EXPECT_TRUE(file_util::PathExists(initial_path)); | 290 EXPECT_TRUE(file_util::PathExists(initial_path)); |
279 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 291 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
280 EXPECT_FALSE(file_util::PathExists(new_path)); | 292 EXPECT_FALSE(file_util::PathExists(new_path)); |
281 | 293 |
282 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 294 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
283 | 295 |
284 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); | 296 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 297 base_file_->Rename(new_path)); |
285 EXPECT_FALSE(file_util::PathExists(initial_path)); | 298 EXPECT_FALSE(file_util::PathExists(initial_path)); |
286 EXPECT_TRUE(file_util::PathExists(new_path)); | 299 EXPECT_TRUE(file_util::PathExists(new_path)); |
287 | 300 |
288 base_file_->Finish(); | 301 base_file_->Finish(); |
289 base_file_->Detach(); | 302 base_file_->Detach(); |
290 expect_file_survives_ = true; | 303 expect_file_survives_ = true; |
291 } | 304 } |
292 | 305 |
293 // Write data to the file once. | 306 // Write data to the file once. |
294 TEST_F(BaseFileTest, SingleWrite) { | 307 TEST_F(BaseFileTest, SingleWrite) { |
295 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 308 ASSERT_TRUE(InitializeFile()); |
296 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 309 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
297 base_file_->Finish(); | 310 base_file_->Finish(); |
298 } | 311 } |
299 | 312 |
300 // Write data to the file multiple times. | 313 // Write data to the file multiple times. |
301 TEST_F(BaseFileTest, MultipleWrites) { | 314 TEST_F(BaseFileTest, MultipleWrites) { |
302 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 315 ASSERT_TRUE(InitializeFile()); |
303 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 316 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
304 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 317 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
305 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 318 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
306 std::string hash; | 319 std::string hash; |
307 EXPECT_FALSE(base_file_->GetHash(&hash)); | 320 EXPECT_FALSE(base_file_->GetHash(&hash)); |
308 base_file_->Finish(); | 321 base_file_->Finish(); |
309 } | 322 } |
310 | 323 |
311 // Write data to the file once and calculate its sha256 hash. | 324 // Write data to the file once and calculate its sha256 hash. |
312 TEST_F(BaseFileTest, SingleWriteWithHash) { | 325 TEST_F(BaseFileTest, SingleWriteWithHash) { |
313 // Calculate the final hash. | 326 // Calculate the final hash. |
314 ResetHash(); | 327 ResetHash(); |
315 UpdateHash(kTestData1, kTestDataLength1); | 328 UpdateHash(kTestData1, kTestDataLength1); |
316 std::string expected_hash = GetFinalHash(); | 329 std::string expected_hash = GetFinalHash(); |
317 std::string expected_hash_hex = | 330 std::string expected_hash_hex = |
318 base::HexEncode(expected_hash.data(), expected_hash.size()); | 331 base::HexEncode(expected_hash.data(), expected_hash.size()); |
319 | 332 |
320 MakeFileWithHash(); | 333 MakeFileWithHash(); |
321 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 334 ASSERT_TRUE(InitializeFile()); |
322 // Can get partial hash states before Finish() is called. | 335 // Can get partial hash states before Finish() is called. |
323 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); | 336 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); |
324 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 337 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
325 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); | 338 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); |
326 base_file_->Finish(); | 339 base_file_->Finish(); |
327 | 340 |
328 std::string hash; | 341 std::string hash; |
329 base_file_->GetHash(&hash); | 342 base_file_->GetHash(&hash); |
330 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); | 343 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); |
331 } | 344 } |
332 | 345 |
333 // Write data to the file multiple times and calculate its sha256 hash. | 346 // Write data to the file multiple times and calculate its sha256 hash. |
334 TEST_F(BaseFileTest, MultipleWritesWithHash) { | 347 TEST_F(BaseFileTest, MultipleWritesWithHash) { |
335 // Calculate the final hash. | 348 // Calculate the final hash. |
336 ResetHash(); | 349 ResetHash(); |
337 UpdateHash(kTestData1, kTestDataLength1); | 350 UpdateHash(kTestData1, kTestDataLength1); |
338 UpdateHash(kTestData2, kTestDataLength2); | 351 UpdateHash(kTestData2, kTestDataLength2); |
339 UpdateHash(kTestData3, kTestDataLength3); | 352 UpdateHash(kTestData3, kTestDataLength3); |
340 std::string expected_hash = GetFinalHash(); | 353 std::string expected_hash = GetFinalHash(); |
341 std::string expected_hash_hex = | 354 std::string expected_hash_hex = |
342 base::HexEncode(expected_hash.data(), expected_hash.size()); | 355 base::HexEncode(expected_hash.data(), expected_hash.size()); |
343 | 356 |
344 std::string hash; | 357 std::string hash; |
345 MakeFileWithHash(); | 358 MakeFileWithHash(); |
346 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 359 ASSERT_TRUE(InitializeFile()); |
347 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 360 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
348 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 361 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
349 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 362 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
350 // No hash before Finish() is called. | 363 // No hash before Finish() is called. |
351 EXPECT_FALSE(base_file_->GetHash(&hash)); | 364 EXPECT_FALSE(base_file_->GetHash(&hash)); |
352 base_file_->Finish(); | 365 base_file_->Finish(); |
353 | 366 |
354 EXPECT_TRUE(base_file_->GetHash(&hash)); | 367 EXPECT_TRUE(base_file_->GetHash(&hash)); |
355 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", | 368 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", |
356 expected_hash_hex); | 369 expected_hash_hex); |
357 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); | 370 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); |
358 } | 371 } |
359 | 372 |
360 // Write data to the file multiple times, interrupt it, and continue using | 373 // Write data to the file multiple times, interrupt it, and continue using |
361 // another file. Calculate the resulting combined sha256 hash. | 374 // another file. Calculate the resulting combined sha256 hash. |
362 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { | 375 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { |
363 // Calculate the final hash. | 376 // Calculate the final hash. |
364 ResetHash(); | 377 ResetHash(); |
365 UpdateHash(kTestData1, kTestDataLength1); | 378 UpdateHash(kTestData1, kTestDataLength1); |
366 UpdateHash(kTestData2, kTestDataLength2); | 379 UpdateHash(kTestData2, kTestDataLength2); |
367 UpdateHash(kTestData3, kTestDataLength3); | 380 UpdateHash(kTestData3, kTestDataLength3); |
368 std::string expected_hash = GetFinalHash(); | 381 std::string expected_hash = GetFinalHash(); |
369 std::string expected_hash_hex = | 382 std::string expected_hash_hex = |
370 base::HexEncode(expected_hash.data(), expected_hash.size()); | 383 base::HexEncode(expected_hash.data(), expected_hash.size()); |
371 | 384 |
372 MakeFileWithHash(); | 385 MakeFileWithHash(); |
373 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 386 ASSERT_TRUE(InitializeFile()); |
374 // Write some data | 387 // Write some data |
375 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 388 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
376 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 389 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
377 // Get the hash state and file name. | 390 // Get the hash state and file name. |
378 std::string hash_state; | 391 std::string hash_state; |
379 hash_state = base_file_->GetHashState(); | 392 hash_state = base_file_->GetHashState(); |
380 // Finish the file. | 393 // Finish the file. |
381 base_file_->Finish(); | 394 base_file_->Finish(); |
382 | 395 |
383 // Create another file | 396 // Create another file |
384 BaseFile second_file(FilePath(), | 397 BaseFile second_file(FilePath(), |
385 GURL(), | 398 GURL(), |
386 GURL(), | 399 GURL(), |
387 base_file_->bytes_so_far(), | 400 base_file_->bytes_so_far(), |
388 true, | 401 true, |
389 hash_state, | 402 hash_state, |
390 scoped_ptr<net::FileStream>(), | 403 scoped_ptr<net::FileStream>(), |
391 net::BoundNetLog()); | 404 net::BoundNetLog()); |
392 ASSERT_EQ(net::OK, second_file.Initialize(temp_dir_.path())); | 405 ASSERT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 406 second_file.Initialize(temp_dir_.path())); |
393 std::string data(kTestData3); | 407 std::string data(kTestData3); |
394 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size())); | 408 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 409 second_file.AppendDataToFile(data.data(), data.size())); |
395 second_file.Finish(); | 410 second_file.Finish(); |
396 | 411 |
397 std::string hash; | 412 std::string hash; |
398 EXPECT_TRUE(second_file.GetHash(&hash)); | 413 EXPECT_TRUE(second_file.GetHash(&hash)); |
399 // This will fail until getting the hash state is supported in SecureHash. | 414 // This will fail until getting the hash state is supported in SecureHash. |
400 EXPECT_STREQ(expected_hash_hex.c_str(), | 415 EXPECT_STREQ(expected_hash_hex.c_str(), |
401 base::HexEncode(hash.data(), hash.size()).c_str()); | 416 base::HexEncode(hash.data(), hash.size()).c_str()); |
402 } | 417 } |
403 | 418 |
404 // Rename the file after all writes to it. | 419 // Rename the file after all writes to it. |
405 TEST_F(BaseFileTest, WriteThenRename) { | 420 TEST_F(BaseFileTest, WriteThenRename) { |
406 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 421 ASSERT_TRUE(InitializeFile()); |
407 | 422 |
408 FilePath initial_path(base_file_->full_path()); | 423 FilePath initial_path(base_file_->full_path()); |
409 EXPECT_TRUE(file_util::PathExists(initial_path)); | 424 EXPECT_TRUE(file_util::PathExists(initial_path)); |
410 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 425 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
411 EXPECT_FALSE(file_util::PathExists(new_path)); | 426 EXPECT_FALSE(file_util::PathExists(new_path)); |
412 | 427 |
413 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 428 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
414 | 429 |
415 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); | 430 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 431 base_file_->Rename(new_path)); |
416 EXPECT_FALSE(file_util::PathExists(initial_path)); | 432 EXPECT_FALSE(file_util::PathExists(initial_path)); |
417 EXPECT_TRUE(file_util::PathExists(new_path)); | 433 EXPECT_TRUE(file_util::PathExists(new_path)); |
418 | 434 |
419 base_file_->Finish(); | 435 base_file_->Finish(); |
420 } | 436 } |
421 | 437 |
422 // Rename the file while the download is still in progress. | 438 // Rename the file while the download is still in progress. |
423 TEST_F(BaseFileTest, RenameWhileInProgress) { | 439 TEST_F(BaseFileTest, RenameWhileInProgress) { |
424 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 440 ASSERT_TRUE(InitializeFile()); |
425 | 441 |
426 FilePath initial_path(base_file_->full_path()); | 442 FilePath initial_path(base_file_->full_path()); |
427 EXPECT_TRUE(file_util::PathExists(initial_path)); | 443 EXPECT_TRUE(file_util::PathExists(initial_path)); |
428 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 444 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
429 EXPECT_FALSE(file_util::PathExists(new_path)); | 445 EXPECT_FALSE(file_util::PathExists(new_path)); |
430 | 446 |
431 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 447 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
432 | 448 |
433 EXPECT_TRUE(base_file_->in_progress()); | 449 EXPECT_TRUE(base_file_->in_progress()); |
434 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); | 450 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 451 base_file_->Rename(new_path)); |
435 EXPECT_FALSE(file_util::PathExists(initial_path)); | 452 EXPECT_FALSE(file_util::PathExists(initial_path)); |
436 EXPECT_TRUE(file_util::PathExists(new_path)); | 453 EXPECT_TRUE(file_util::PathExists(new_path)); |
437 | 454 |
438 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 455 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
439 | 456 |
440 base_file_->Finish(); | 457 base_file_->Finish(); |
441 } | 458 } |
442 | 459 |
443 // Test that a failed rename reports the correct error. | 460 // Test that a failed rename reports the correct error. |
444 TEST_F(BaseFileTest, RenameWithError) { | 461 TEST_F(BaseFileTest, RenameWithError) { |
445 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 462 ASSERT_TRUE(InitializeFile()); |
446 | 463 |
447 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so | 464 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so |
448 // that the rename will fail. | 465 // that the rename will fail. |
449 FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); | 466 FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); |
450 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); | 467 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); |
451 | 468 |
452 FilePath new_path(test_dir.AppendASCII("TestFile")); | 469 FilePath new_path(test_dir.AppendASCII("TestFile")); |
453 EXPECT_FALSE(file_util::PathExists(new_path)); | 470 EXPECT_FALSE(file_util::PathExists(new_path)); |
454 | 471 |
455 { | 472 { |
456 file_util::PermissionRestorer restore_permissions_for(test_dir); | 473 file_util::PermissionRestorer restore_permissions_for(test_dir); |
457 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); | 474 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); |
458 EXPECT_EQ(net::ERR_ACCESS_DENIED, base_file_->Rename(new_path)); | 475 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 476 base_file_->Rename(new_path)); |
459 } | 477 } |
460 | 478 |
461 base_file_->Finish(); | 479 base_file_->Finish(); |
462 } | 480 } |
463 | 481 |
464 // Write data to the file multiple times. | 482 // Write data to the file multiple times. |
465 TEST_F(BaseFileTest, MultipleWritesWithError) { | 483 TEST_F(BaseFileTest, MultipleWritesWithError) { |
466 FilePath path; | 484 FilePath path; |
467 ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); | 485 ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); |
468 // Create a new file stream. scoped_ptr takes ownership and passes it to | 486 // Create a new file stream. scoped_ptr takes ownership and passes it to |
(...skipping 12 matching lines...) Expand all Loading... |
481 // not deleting it until it is reset. | 499 // not deleting it until it is reset. |
482 | 500 |
483 base_file_.reset(new BaseFile(mock_file_stream->get_path(), | 501 base_file_.reset(new BaseFile(mock_file_stream->get_path(), |
484 GURL(), | 502 GURL(), |
485 GURL(), | 503 GURL(), |
486 0, | 504 0, |
487 false, | 505 false, |
488 "", | 506 "", |
489 mock_file_stream_scoped_ptr.Pass(), | 507 mock_file_stream_scoped_ptr.Pass(), |
490 net::BoundNetLog())); | 508 net::BoundNetLog())); |
491 EXPECT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 509 ASSERT_TRUE(InitializeFile()); |
492 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 510 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
493 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 511 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
494 mock_file_stream->set_forced_error(net::ERR_ACCESS_DENIED); | 512 mock_file_stream->set_forced_error(net::ERR_ACCESS_DENIED); |
495 set_expected_error(net::ERR_ACCESS_DENIED); | 513 set_expected_error(content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
496 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); | 514 ASSERT_FALSE(AppendDataToFile(kTestData3)); |
497 std::string hash; | 515 std::string hash; |
498 EXPECT_FALSE(base_file_->GetHash(&hash)); | 516 EXPECT_FALSE(base_file_->GetHash(&hash)); |
499 base_file_->Finish(); | 517 base_file_->Finish(); |
500 } | 518 } |
501 | 519 |
502 // Try to write to uninitialized file. | 520 // Try to write to uninitialized file. |
503 TEST_F(BaseFileTest, UninitializedFile) { | 521 TEST_F(BaseFileTest, UninitializedFile) { |
504 expect_in_progress_ = false; | 522 expect_in_progress_ = false; |
505 EXPECT_EQ(net::ERR_INVALID_HANDLE, AppendDataToFile(kTestData1)); | 523 set_expected_error(content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 524 EXPECT_FALSE(AppendDataToFile(kTestData1)); |
506 } | 525 } |
507 | 526 |
508 // Create two |BaseFile|s with the same file, and attempt to write to both. | 527 // Create two |BaseFile|s with the same file, and attempt to write to both. |
509 // Overwrite base_file_ with another file with the same name and | 528 // Overwrite base_file_ with another file with the same name and |
510 // non-zero contents, and make sure the last file to close 'wins'. | 529 // non-zero contents, and make sure the last file to close 'wins'. |
511 TEST_F(BaseFileTest, DuplicateBaseFile) { | 530 TEST_F(BaseFileTest, DuplicateBaseFile) { |
512 EXPECT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 531 ASSERT_TRUE(InitializeFile()); |
513 | 532 |
514 // Create another |BaseFile| referring to the file that |base_file_| owns. | 533 // Create another |BaseFile| referring to the file that |base_file_| owns. |
515 CreateFileWithName(base_file_->full_path()); | 534 CreateFileWithName(base_file_->full_path()); |
516 | 535 |
517 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 536 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
518 base_file_->Finish(); | 537 base_file_->Finish(); |
519 } | 538 } |
520 | 539 |
521 // Create a file and append to it. | 540 // Create a file and append to it. |
522 TEST_F(BaseFileTest, AppendToBaseFile) { | 541 TEST_F(BaseFileTest, AppendToBaseFile) { |
523 // Create a new file. | 542 // Create a new file. |
524 FilePath existing_file_name = CreateTestFile(); | 543 FilePath existing_file_name = CreateTestFile(); |
525 | 544 |
526 set_expected_data(kTestData4); | 545 set_expected_data(kTestData4); |
527 | 546 |
528 // Use the file we've just created. | 547 // Use the file we've just created. |
529 base_file_.reset(new BaseFile(existing_file_name, | 548 base_file_.reset(new BaseFile(existing_file_name, |
530 GURL(), | 549 GURL(), |
531 GURL(), | 550 GURL(), |
532 kTestDataLength4, | 551 kTestDataLength4, |
533 false, | 552 false, |
534 "", | 553 "", |
535 scoped_ptr<net::FileStream>(), | 554 scoped_ptr<net::FileStream>(), |
536 net::BoundNetLog())); | 555 net::BoundNetLog())); |
537 | 556 |
538 EXPECT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 557 ASSERT_TRUE(InitializeFile()); |
539 | 558 |
540 const FilePath file_name = base_file_->full_path(); | 559 const FilePath file_name = base_file_->full_path(); |
541 EXPECT_NE(FilePath::StringType(), file_name.value()); | 560 EXPECT_NE(FilePath::StringType(), file_name.value()); |
542 | 561 |
543 // Write into the file. | 562 // Write into the file. |
544 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); | 563 EXPECT_TRUE(AppendDataToFile(kTestData1)); |
545 | 564 |
546 base_file_->Finish(); | 565 base_file_->Finish(); |
547 base_file_->Detach(); | 566 base_file_->Detach(); |
548 expect_file_survives_ = true; | 567 expect_file_survives_ = true; |
549 } | 568 } |
550 | 569 |
551 // Create a read-only file and attempt to write to it. | 570 // Create a read-only file and attempt to write to it. |
552 TEST_F(BaseFileTest, ReadonlyBaseFile) { | 571 TEST_F(BaseFileTest, ReadonlyBaseFile) { |
553 // Create a new file. | 572 // Create a new file. |
554 FilePath readonly_file_name = CreateTestFile(); | 573 FilePath readonly_file_name = CreateTestFile(); |
555 | 574 |
556 // Restore permissions to the file when we are done with this test. | 575 // Restore permissions to the file when we are done with this test. |
557 file_util::PermissionRestorer restore_permissions(readonly_file_name); | 576 file_util::PermissionRestorer restore_permissions(readonly_file_name); |
558 | 577 |
559 // Make it read-only. | 578 // Make it read-only. |
560 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); | 579 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); |
561 | 580 |
562 // Try to overwrite it. | 581 // Try to overwrite it. |
563 base_file_.reset(new BaseFile(readonly_file_name, | 582 base_file_.reset(new BaseFile(readonly_file_name, |
564 GURL(), | 583 GURL(), |
565 GURL(), | 584 GURL(), |
566 0, | 585 0, |
567 false, | 586 false, |
568 "", | 587 "", |
569 scoped_ptr<net::FileStream>(), | 588 scoped_ptr<net::FileStream>(), |
570 net::BoundNetLog())); | 589 net::BoundNetLog())); |
571 | 590 |
572 expect_in_progress_ = false; | 591 expect_in_progress_ = false; |
573 | 592 set_expected_error(content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
574 int init_error = base_file_->Initialize(temp_dir_.path()); | 593 EXPECT_FALSE(InitializeFile()); |
575 DVLOG(1) << " init_error = " << init_error; | |
576 EXPECT_NE(net::OK, init_error); | |
577 | 594 |
578 const FilePath file_name = base_file_->full_path(); | 595 const FilePath file_name = base_file_->full_path(); |
579 EXPECT_NE(FilePath::StringType(), file_name.value()); | 596 EXPECT_NE(FilePath::StringType(), file_name.value()); |
580 | 597 |
581 // Write into the file. | 598 // Write into the file. |
582 EXPECT_NE(net::OK, AppendDataToFile(kTestData1)); | 599 set_expected_error(content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 600 EXPECT_FALSE(AppendDataToFile(kTestData1)); |
583 | 601 |
584 base_file_->Finish(); | 602 base_file_->Finish(); |
585 base_file_->Detach(); | 603 base_file_->Detach(); |
586 expect_file_survives_ = true; | 604 expect_file_survives_ = true; |
587 } | 605 } |
588 | 606 |
589 TEST_F(BaseFileTest, IsEmptyHash) { | 607 TEST_F(BaseFileTest, IsEmptyHash) { |
590 std::string empty(BaseFile::kSha256HashLen, '\x00'); | 608 std::string empty(BaseFile::kSha256HashLen, '\x00'); |
591 EXPECT_TRUE(BaseFile::IsEmptyHash(empty)); | 609 EXPECT_TRUE(BaseFile::IsEmptyHash(empty)); |
592 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); | 610 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); |
593 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty)); | 611 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty)); |
594 EXPECT_FALSE(BaseFile::IsEmptyHash("")); | 612 EXPECT_FALSE(BaseFile::IsEmptyHash("")); |
595 } | 613 } |
596 | 614 |
597 // Test that calculating speed after no writes. | 615 // Test that calculating speed after no writes. |
598 TEST_F(BaseFileTest, SpeedWithoutWrite) { | 616 TEST_F(BaseFileTest, SpeedWithoutWrite) { |
599 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 617 ASSERT_TRUE(InitializeFile()); |
600 base::TimeTicks current = StartTick() + kElapsedTimeDelta; | 618 base::TimeTicks current = StartTick() + kElapsedTimeDelta; |
601 ASSERT_EQ(0, CurrentSpeedAtTime(current)); | 619 ASSERT_EQ(0, CurrentSpeedAtTime(current)); |
602 base_file_->Finish(); | 620 base_file_->Finish(); |
603 } | 621 } |
604 | 622 |
605 // Test that calculating speed after a single write. | 623 // Test that calculating speed after a single write. |
606 TEST_F(BaseFileTest, SpeedAfterSingleWrite) { | 624 TEST_F(BaseFileTest, SpeedAfterSingleWrite) { |
607 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 625 ASSERT_TRUE(InitializeFile()); |
608 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 626 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
609 base::TimeTicks current = StartTick() + kElapsedTimeDelta; | 627 base::TimeTicks current = StartTick() + kElapsedTimeDelta; |
610 int64 expected_speed = kTestDataLength1 / kElapsedTimeSeconds; | 628 int64 expected_speed = kTestDataLength1 / kElapsedTimeSeconds; |
611 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); | 629 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); |
612 base_file_->Finish(); | 630 base_file_->Finish(); |
613 } | 631 } |
614 | 632 |
615 // Test that calculating speed after a multiple writes. | 633 // Test that calculating speed after a multiple writes. |
616 TEST_F(BaseFileTest, SpeedAfterMultipleWrite) { | 634 TEST_F(BaseFileTest, SpeedAfterMultipleWrite) { |
617 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 635 ASSERT_TRUE(InitializeFile()); |
618 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 636 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
619 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 637 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
620 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 638 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
621 ASSERT_EQ(net::OK, AppendDataToFile(kTestData4)); | 639 ASSERT_TRUE(AppendDataToFile(kTestData4)); |
622 base::TimeTicks current = StartTick() + kElapsedTimeDelta; | 640 base::TimeTicks current = StartTick() + kElapsedTimeDelta; |
623 int64 expected_speed = (kTestDataLength1 + kTestDataLength2 + | 641 int64 expected_speed = (kTestDataLength1 + kTestDataLength2 + |
624 kTestDataLength3 + kTestDataLength4) / kElapsedTimeSeconds; | 642 kTestDataLength3 + kTestDataLength4) / kElapsedTimeSeconds; |
625 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); | 643 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); |
626 base_file_->Finish(); | 644 base_file_->Finish(); |
627 } | 645 } |
628 | 646 |
629 // Test that calculating speed after no delay - should not divide by 0. | 647 // Test that calculating speed after no delay - should not divide by 0. |
630 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { | 648 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { |
631 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 649 ASSERT_TRUE(InitializeFile()); |
632 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 650 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
633 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); | 651 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); |
634 base_file_->Finish(); | 652 base_file_->Finish(); |
635 } | 653 } |
636 | 654 |
637 // Test that a temporary file is created in the default download directory. | 655 // Test that a temporary file is created in the default download directory. |
638 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { | 656 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { |
639 ASSERT_TRUE(base_file_->full_path().empty()); | 657 ASSERT_TRUE(base_file_->full_path().empty()); |
640 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path())); | 658 ASSERT_TRUE(InitializeFile()); |
641 EXPECT_FALSE(base_file_->full_path().empty()); | 659 EXPECT_FALSE(base_file_->full_path().empty()); |
642 | 660 |
643 // On Windows, CreateTemporaryFileInDir() will cause a path with short names | 661 // On Windows, CreateTemporaryFileInDir() will cause a path with short names |
644 // to be expanded into a path with long names. Thus temp_dir.path() might not | 662 // to be expanded into a path with long names. Thus temp_dir.path() might not |
645 // be a string-wise match to base_file_->full_path().DirName() even though | 663 // be a string-wise match to base_file_->full_path().DirName() even though |
646 // they are in the same directory. | 664 // they are in the same directory. |
647 FilePath temp_file; | 665 FilePath temp_file; |
648 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 666 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
649 &temp_file)); | 667 &temp_file)); |
650 ASSERT_FALSE(temp_file.empty()); | 668 ASSERT_FALSE(temp_file.empty()); |
651 EXPECT_STREQ(temp_file.DirName().value().c_str(), | 669 EXPECT_STREQ(temp_file.DirName().value().c_str(), |
652 base_file_->full_path().DirName().value().c_str()); | 670 base_file_->full_path().DirName().value().c_str()); |
653 base_file_->Finish(); | 671 base_file_->Finish(); |
654 } | 672 } |
OLD | NEW |