Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: content/browser/download/base_file_unittest.cc

Issue 8404049: Added member data to classes to support download resumption. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged with trunk Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/download_create_info.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 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 "crypto/secure_hash.h"
14 #include "net/base/file_stream.h" 15 #include "net/base/file_stream.h"
15 #include "net/base/mock_file_stream.h" 16 #include "net/base/mock_file_stream.h"
16 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 using content::BrowserThread; 20 using content::BrowserThread;
20 using content::BrowserThreadImpl; 21 using content::BrowserThreadImpl;
21 22
22 namespace { 23 namespace {
23 24
24 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";
25 const char kTestData2[] = "Writing more data.\n"; 26 const char kTestData2[] = "Writing more data.\n";
26 const char kTestData3[] = "Final line."; 27 const char kTestData3[] = "Final line.";
27 const char kTestData4[] = "supercalifragilisticexpialidocious"; 28 const char kTestData4[] = "supercalifragilisticexpialidocious";
28 const int kTestDataLength1 = arraysize(kTestData1) - 1; 29 const int kTestDataLength1 = arraysize(kTestData1) - 1;
29 const int kTestDataLength2 = arraysize(kTestData2) - 1; 30 const int kTestDataLength2 = arraysize(kTestData2) - 1;
30 const int kTestDataLength3 = arraysize(kTestData3) - 1; 31 const int kTestDataLength3 = arraysize(kTestData3) - 1;
31 const int kTestDataLength4 = arraysize(kTestData4) - 1; 32 const int kTestDataLength4 = arraysize(kTestData4) - 1;
32 const int kElapsedTimeSeconds = 5; 33 const int kElapsedTimeSeconds = 5;
33 const base::TimeDelta kElapsedTimeDelta = base::TimeDelta::FromSeconds( 34 const base::TimeDelta kElapsedTimeDelta = base::TimeDelta::FromSeconds(
34 kElapsedTimeSeconds); 35 kElapsedTimeSeconds);
35 36
36 } // namespace 37 } // namespace
37 38
38 class BaseFileTest : public testing::Test { 39 class BaseFileTest : public testing::Test {
39 public: 40 public:
41 static const size_t kSha256HashLen = 32;
42 static const unsigned char kEmptySha256Hash[kSha256HashLen];
43
40 BaseFileTest() 44 BaseFileTest()
41 : expect_file_survives_(false), 45 : expect_file_survives_(false),
42 expect_in_progress_(true), 46 expect_in_progress_(true),
43 expected_error_(false), 47 expected_error_(false),
44 file_thread_(BrowserThread::FILE, &message_loop_) { 48 file_thread_(BrowserThread::FILE, &message_loop_) {
45 } 49 }
46 50
47 virtual void SetUp() { 51 virtual void SetUp() {
52 ResetHash();
48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49 base_file_.reset( 54 base_file_.reset(
50 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_)); 55 new BaseFile(FilePath(), GURL(), GURL(), 0, false, "", file_stream_));
51 } 56 }
52 57
53 virtual void TearDown() { 58 virtual void TearDown() {
54 EXPECT_FALSE(base_file_->in_progress()); 59 EXPECT_FALSE(base_file_->in_progress());
55 if (!expected_error_) { 60 if (!expected_error_) {
56 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 61 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
57 base_file_->bytes_so_far()); 62 base_file_->bytes_so_far());
58 } 63 }
59 64
60 FilePath full_path = base_file_->full_path(); 65 FilePath full_path = base_file_->full_path();
61 66
62 if (!expected_data_.empty() && !expected_error_) { 67 if (!expected_data_.empty() && !expected_error_) {
63 // Make sure the data has been properly written to disk. 68 // Make sure the data has been properly written to disk.
64 std::string disk_data; 69 std::string disk_data;
65 EXPECT_TRUE(file_util::ReadFileToString(full_path, &disk_data)); 70 EXPECT_TRUE(file_util::ReadFileToString(full_path, &disk_data));
66 EXPECT_EQ(expected_data_, disk_data); 71 EXPECT_EQ(expected_data_, disk_data);
67 } 72 }
68 73
69 // Make sure the mock BrowserThread outlives the BaseFile to satisfy 74 // Make sure the mock BrowserThread outlives the BaseFile to satisfy
70 // thread checks inside it. 75 // thread checks inside it.
71 base_file_.reset(); 76 base_file_.reset();
72 77
73 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path)); 78 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path));
74 } 79 }
75 80
81 void ResetHash() {
82 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256));
83 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen);
84 }
85
86 void UpdateHash(const char* data, size_t length) {
87 secure_hash_->Update(data, length);
88 }
89
90 std::string GetFinalHash() {
91 std::string hash;
92 secure_hash_->Finish(sha256_hash_, kSha256HashLen);
93 hash.assign(reinterpret_cast<const char*>(sha256_hash_),
94 sizeof(sha256_hash_));
95 return hash;
96 }
97
98 void MakeFileWithHash() {
99 base_file_.reset(
100 new BaseFile(FilePath(), GURL(), GURL(), 0, true, "", file_stream_));
101 }
102
76 bool OpenMockFileStream() { 103 bool OpenMockFileStream() {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
78 105
79 FilePath path; 106 FilePath path;
80 if (!file_util::CreateTemporaryFile(&path)) 107 if (!file_util::CreateTemporaryFile(&path))
81 return false; 108 return false;
82 109
83 // Create a new file stream. 110 // Create a new file stream.
84 mock_file_stream_.reset(new net::testing::MockFileStream); 111 mock_file_stream_.reset(new net::testing::MockFileStream);
85 if (mock_file_stream_->Open( 112 if (mock_file_stream_->Open(
(...skipping 28 matching lines...) Expand all
114 return appended; 141 return appended;
115 } 142 }
116 143
117 void set_expected_data(const std::string& data) { expected_data_ = data; } 144 void set_expected_data(const std::string& data) { expected_data_ = data; }
118 145
119 // Helper functions. 146 // Helper functions.
120 // Create a file. Returns the complete file path. 147 // Create a file. Returns the complete file path.
121 static FilePath CreateTestFile() { 148 static FilePath CreateTestFile() {
122 FilePath file_name; 149 FilePath file_name;
123 linked_ptr<net::FileStream> dummy_file_stream; 150 linked_ptr<net::FileStream> dummy_file_stream;
124 BaseFile file(FilePath(), GURL(), GURL(), 0, dummy_file_stream); 151 BaseFile file(FilePath(), GURL(), GURL(), 0, false, "", dummy_file_stream);
125 152
126 EXPECT_EQ(net::OK, file.Initialize(false)); 153 EXPECT_EQ(net::OK, file.Initialize());
127 file_name = file.full_path(); 154 file_name = file.full_path();
128 EXPECT_NE(FilePath::StringType(), file_name.value()); 155 EXPECT_NE(FilePath::StringType(), file_name.value());
129 156
130 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); 157 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4));
131 158
132 // Keep the file from getting deleted when existing_file_name is deleted. 159 // Keep the file from getting deleted when existing_file_name is deleted.
133 file.Detach(); 160 file.Detach();
134 161
135 return file_name; 162 return file_name;
136 } 163 }
137 164
138 // Create a file with the specified file name. 165 // Create a file with the specified file name.
139 static void CreateFileWithName(const FilePath& file_name) { 166 static void CreateFileWithName(const FilePath& file_name) {
140 EXPECT_NE(FilePath::StringType(), file_name.value()); 167 EXPECT_NE(FilePath::StringType(), file_name.value());
141 linked_ptr<net::FileStream> dummy_file_stream; 168 linked_ptr<net::FileStream> dummy_file_stream;
142 BaseFile duplicate_file(file_name, GURL(), GURL(), 0, dummy_file_stream); 169 BaseFile duplicate_file(
143 EXPECT_EQ(net::OK, duplicate_file.Initialize(false)); 170 file_name, GURL(), GURL(), 0, false, "", dummy_file_stream);
171 EXPECT_EQ(net::OK, duplicate_file.Initialize());
144 // Write something into it. 172 // Write something into it.
145 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); 173 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4);
146 // Detach the file so it isn't deleted on destruction of |duplicate_file|. 174 // Detach the file so it isn't deleted on destruction of |duplicate_file|.
147 duplicate_file.Detach(); 175 duplicate_file.Detach();
148 } 176 }
149 177
150 int64 CurrentSpeedAtTime(base::TimeTicks current_time) { 178 int64 CurrentSpeedAtTime(base::TimeTicks current_time) {
151 EXPECT_TRUE(base_file_.get()); 179 EXPECT_TRUE(base_file_.get());
152 return base_file_->CurrentSpeedAtTime(current_time); 180 return base_file_->CurrentSpeedAtTime(current_time);
153 } 181 }
(...skipping 12 matching lines...) Expand all
166 194
167 // Temporary directory for renamed downloads. 195 // Temporary directory for renamed downloads.
168 ScopedTempDir temp_dir_; 196 ScopedTempDir temp_dir_;
169 197
170 // Expect the file to survive deletion of the BaseFile instance. 198 // Expect the file to survive deletion of the BaseFile instance.
171 bool expect_file_survives_; 199 bool expect_file_survives_;
172 200
173 // Expect the file to be in progress. 201 // Expect the file to be in progress.
174 bool expect_in_progress_; 202 bool expect_in_progress_;
175 203
204 // Hash calculator.
205 scoped_ptr<crypto::SecureHash> secure_hash_;
206
207 unsigned char sha256_hash_[kSha256HashLen];
208
176 private: 209 private:
177 // Keep track of what data should be saved to the disk file. 210 // Keep track of what data should be saved to the disk file.
178 std::string expected_data_; 211 std::string expected_data_;
179 bool expected_error_; 212 bool expected_error_;
180 213
181 // Mock file thread to satisfy debug checks in BaseFile. 214 // Mock file thread to satisfy debug checks in BaseFile.
182 MessageLoop message_loop_; 215 MessageLoop message_loop_;
183 BrowserThreadImpl file_thread_; 216 BrowserThreadImpl file_thread_;
184 }; 217 };
185 218
219 // This will initialize the entire array to zero.
220 const unsigned char BaseFileTest::kEmptySha256Hash[] = { 0 };
221
186 // Test the most basic scenario: just create the object and do a sanity check 222 // Test the most basic scenario: just create the object and do a sanity check
187 // on all its accessors. This is actually a case that rarely happens 223 // on all its accessors. This is actually a case that rarely happens
188 // in production, where we would at least Initialize it. 224 // in production, where we would at least Initialize it.
189 TEST_F(BaseFileTest, CreateDestroy) { 225 TEST_F(BaseFileTest, CreateDestroy) {
190 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); 226 EXPECT_EQ(FilePath().value(), base_file_->full_path().value());
191 } 227 }
192 228
193 // Cancel the download explicitly. 229 // Cancel the download explicitly.
194 TEST_F(BaseFileTest, Cancel) { 230 TEST_F(BaseFileTest, Cancel) {
195 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 231 ASSERT_EQ(net::OK, base_file_->Initialize());
196 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); 232 EXPECT_TRUE(file_util::PathExists(base_file_->full_path()));
197 base_file_->Cancel(); 233 base_file_->Cancel();
198 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); 234 EXPECT_FALSE(file_util::PathExists(base_file_->full_path()));
199 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); 235 EXPECT_NE(FilePath().value(), base_file_->full_path().value());
200 } 236 }
201 237
202 // Write data to the file and detach it, so it doesn't get deleted 238 // Write data to the file and detach it, so it doesn't get deleted
203 // automatically when base_file_ is destructed. 239 // automatically when base_file_ is destructed.
204 TEST_F(BaseFileTest, WriteAndDetach) { 240 TEST_F(BaseFileTest, WriteAndDetach) {
205 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 241 ASSERT_EQ(net::OK, base_file_->Initialize());
206 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 242 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
207 base_file_->Finish(); 243 base_file_->Finish();
208 base_file_->Detach(); 244 base_file_->Detach();
209 expect_file_survives_ = true; 245 expect_file_survives_ = true;
210 } 246 }
211 247
212 // Write data to the file and detach it, and calculate its sha256 hash. 248 // Write data to the file and detach it, and calculate its sha256 hash.
213 TEST_F(BaseFileTest, WriteWithHashAndDetach) { 249 TEST_F(BaseFileTest, WriteWithHashAndDetach) {
214 ASSERT_EQ(net::OK, base_file_->Initialize(true)); 250 // Calculate the final hash.
251 ResetHash();
252 UpdateHash(kTestData1, kTestDataLength1);
253 std::string expected_hash = GetFinalHash();
254 std::string expected_hash_hex =
255 base::HexEncode(expected_hash.data(), expected_hash.size());
256
257 MakeFileWithHash();
258 ASSERT_EQ(net::OK, base_file_->Initialize());
215 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 259 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
216 base_file_->Finish(); 260 base_file_->Finish();
217 261
218 std::string hash; 262 std::string hash;
219 base_file_->GetSha256Hash(&hash); 263 base_file_->GetHash(&hash);
220 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", 264 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE",
221 base::HexEncode(hash.data(), hash.size())); 265 expected_hash_hex);
266 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size()));
222 267
223 base_file_->Detach(); 268 base_file_->Detach();
224 expect_file_survives_ = true; 269 expect_file_survives_ = true;
225 } 270 }
226 271
227 // Rename the file after writing to it, then detach. 272 // Rename the file after writing to it, then detach.
228 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { 273 TEST_F(BaseFileTest, WriteThenRenameAndDetach) {
229 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 274 ASSERT_EQ(net::OK, base_file_->Initialize());
230 275
231 FilePath initial_path(base_file_->full_path()); 276 FilePath initial_path(base_file_->full_path());
232 EXPECT_TRUE(file_util::PathExists(initial_path)); 277 EXPECT_TRUE(file_util::PathExists(initial_path));
233 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 278 FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
234 EXPECT_FALSE(file_util::PathExists(new_path)); 279 EXPECT_FALSE(file_util::PathExists(new_path));
235 280
236 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 281 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
237 282
238 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); 283 EXPECT_EQ(net::OK, base_file_->Rename(new_path));
239 EXPECT_FALSE(file_util::PathExists(initial_path)); 284 EXPECT_FALSE(file_util::PathExists(initial_path));
240 EXPECT_TRUE(file_util::PathExists(new_path)); 285 EXPECT_TRUE(file_util::PathExists(new_path));
241 286
242 base_file_->Finish(); 287 base_file_->Finish();
243 base_file_->Detach(); 288 base_file_->Detach();
244 expect_file_survives_ = true; 289 expect_file_survives_ = true;
245 } 290 }
246 291
247 // Write data to the file once. 292 // Write data to the file once.
248 TEST_F(BaseFileTest, SingleWrite) { 293 TEST_F(BaseFileTest, SingleWrite) {
249 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 294 ASSERT_EQ(net::OK, base_file_->Initialize());
250 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 295 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
251 base_file_->Finish(); 296 base_file_->Finish();
252 } 297 }
253 298
254 // Write data to the file multiple times. 299 // Write data to the file multiple times.
255 TEST_F(BaseFileTest, MultipleWrites) { 300 TEST_F(BaseFileTest, MultipleWrites) {
256 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 301 ASSERT_EQ(net::OK, base_file_->Initialize());
257 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 302 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
258 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 303 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
259 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 304 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
260 std::string hash; 305 std::string hash;
261 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); 306 EXPECT_FALSE(base_file_->GetHash(&hash));
262 base_file_->Finish(); 307 base_file_->Finish();
263 } 308 }
264 309
265 // Write data to the file once and calculate its sha256 hash. 310 // Write data to the file once and calculate its sha256 hash.
266 TEST_F(BaseFileTest, SingleWriteWithHash) { 311 TEST_F(BaseFileTest, SingleWriteWithHash) {
267 ASSERT_EQ(net::OK, base_file_->Initialize(true)); 312 // Calculate the final hash.
313 ResetHash();
314 UpdateHash(kTestData1, kTestDataLength1);
315 std::string expected_hash = GetFinalHash();
316 std::string expected_hash_hex =
317 base::HexEncode(expected_hash.data(), expected_hash.size());
318
319 MakeFileWithHash();
320 ASSERT_EQ(net::OK, base_file_->Initialize());
321 // Can get partial hash states before Finish() is called.
322 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str());
268 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 323 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
324 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str());
269 base_file_->Finish(); 325 base_file_->Finish();
270 326
271 std::string hash; 327 std::string hash;
272 base_file_->GetSha256Hash(&hash); 328 base_file_->GetHash(&hash);
273 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", 329 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size()));
274 base::HexEncode(hash.data(), hash.size()));
275 } 330 }
276 331
277 // Write data to the file multiple times and calculate its sha256 hash. 332 // Write data to the file multiple times and calculate its sha256 hash.
278 TEST_F(BaseFileTest, MultipleWritesWithHash) { 333 TEST_F(BaseFileTest, MultipleWritesWithHash) {
334 // Calculate the final hash.
335 ResetHash();
336 UpdateHash(kTestData1, kTestDataLength1);
337 UpdateHash(kTestData2, kTestDataLength2);
338 UpdateHash(kTestData3, kTestDataLength3);
339 std::string expected_hash = GetFinalHash();
340 std::string expected_hash_hex =
341 base::HexEncode(expected_hash.data(), expected_hash.size());
342
279 std::string hash; 343 std::string hash;
280 344 MakeFileWithHash();
281 ASSERT_EQ(net::OK, base_file_->Initialize(true)); 345 ASSERT_EQ(net::OK, base_file_->Initialize());
282 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 346 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
283 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 347 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
284 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 348 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
285 // no hash before Finish() is called either. 349 // No hash before Finish() is called.
286 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); 350 EXPECT_FALSE(base_file_->GetHash(&hash));
287 base_file_->Finish(); 351 base_file_->Finish();
288 352
289 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); 353 EXPECT_TRUE(base_file_->GetHash(&hash));
290 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", 354 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8",
291 base::HexEncode(hash.data(), hash.size())); 355 expected_hash_hex);
356 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size()));
357 }
358
359 // Write data to the file multiple times, interrupt it, and continue using
360 // another file. Calculate the resulting combined sha256 hash.
361 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) {
362 // Calculate the final hash.
363 ResetHash();
364 UpdateHash(kTestData1, kTestDataLength1);
365 UpdateHash(kTestData2, kTestDataLength2);
366 UpdateHash(kTestData3, kTestDataLength3);
367 std::string expected_hash = GetFinalHash();
368 std::string expected_hash_hex =
369 base::HexEncode(expected_hash.data(), expected_hash.size());
370
371 MakeFileWithHash();
372 ASSERT_EQ(net::OK, base_file_->Initialize());
373 // Write some data
374 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
375 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
376 // Get the hash state and file name.
377 std::string hash_state;
378 hash_state = base_file_->GetHashState();
379 // Finish the file.
380 base_file_->Finish();
381
382 // Create another file
383 linked_ptr<net::FileStream> second_stream;
384 BaseFile second_file(FilePath(),
385 GURL(),
386 GURL(),
387 base_file_->bytes_so_far(),
388 true,
389 hash_state,
390 second_stream);
391 ASSERT_EQ(net::OK, second_file.Initialize());
392 std::string data(kTestData3);
393 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size()));
394 second_file.Finish();
395
396 std::string hash;
397 EXPECT_TRUE(second_file.GetHash(&hash));
398 // This will fail until getting the hash state is supported in SecureHash.
399 EXPECT_STREQ(expected_hash_hex.c_str(),
400 base::HexEncode(hash.data(), hash.size()).c_str());
292 } 401 }
293 402
294 // Rename the file after all writes to it. 403 // Rename the file after all writes to it.
295 TEST_F(BaseFileTest, WriteThenRename) { 404 TEST_F(BaseFileTest, WriteThenRename) {
296 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 405 ASSERT_EQ(net::OK, base_file_->Initialize());
297 406
298 FilePath initial_path(base_file_->full_path()); 407 FilePath initial_path(base_file_->full_path());
299 EXPECT_TRUE(file_util::PathExists(initial_path)); 408 EXPECT_TRUE(file_util::PathExists(initial_path));
300 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 409 FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
301 EXPECT_FALSE(file_util::PathExists(new_path)); 410 EXPECT_FALSE(file_util::PathExists(new_path));
302 411
303 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 412 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
304 413
305 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); 414 EXPECT_EQ(net::OK, base_file_->Rename(new_path));
306 EXPECT_FALSE(file_util::PathExists(initial_path)); 415 EXPECT_FALSE(file_util::PathExists(initial_path));
307 EXPECT_TRUE(file_util::PathExists(new_path)); 416 EXPECT_TRUE(file_util::PathExists(new_path));
308 417
309 base_file_->Finish(); 418 base_file_->Finish();
310 } 419 }
311 420
312 // Rename the file while the download is still in progress. 421 // Rename the file while the download is still in progress.
313 TEST_F(BaseFileTest, RenameWhileInProgress) { 422 TEST_F(BaseFileTest, RenameWhileInProgress) {
314 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 423 ASSERT_EQ(net::OK, base_file_->Initialize());
315 424
316 FilePath initial_path(base_file_->full_path()); 425 FilePath initial_path(base_file_->full_path());
317 EXPECT_TRUE(file_util::PathExists(initial_path)); 426 EXPECT_TRUE(file_util::PathExists(initial_path));
318 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 427 FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
319 EXPECT_FALSE(file_util::PathExists(new_path)); 428 EXPECT_FALSE(file_util::PathExists(new_path));
320 429
321 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 430 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
322 431
323 EXPECT_TRUE(base_file_->in_progress()); 432 EXPECT_TRUE(base_file_->in_progress());
324 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); 433 EXPECT_EQ(net::OK, base_file_->Rename(new_path));
325 EXPECT_FALSE(file_util::PathExists(initial_path)); 434 EXPECT_FALSE(file_util::PathExists(initial_path));
326 EXPECT_TRUE(file_util::PathExists(new_path)); 435 EXPECT_TRUE(file_util::PathExists(new_path));
327 436
328 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 437 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
329 438
330 base_file_->Finish(); 439 base_file_->Finish();
331 } 440 }
332 441
333 // Write data to the file multiple times. 442 // Write data to the file multiple times.
334 TEST_F(BaseFileTest, MultipleWritesWithError) { 443 TEST_F(BaseFileTest, MultipleWritesWithError) {
335 ASSERT_TRUE(OpenMockFileStream()); 444 ASSERT_TRUE(OpenMockFileStream());
336 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), 445 base_file_.reset(new BaseFile(mock_file_stream_->get_path(),
337 GURL(), GURL(), 0, mock_file_stream_)); 446 GURL(),
338 EXPECT_EQ(net::OK, base_file_->Initialize(false)); 447 GURL(),
448 0,
449 false,
450 "",
451 mock_file_stream_));
452 EXPECT_EQ(net::OK, base_file_->Initialize());
339 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 453 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
340 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 454 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
341 ForceError(net::ERR_ACCESS_DENIED); 455 ForceError(net::ERR_ACCESS_DENIED);
342 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); 456 ASSERT_NE(net::OK, AppendDataToFile(kTestData3));
343 std::string hash; 457 std::string hash;
344 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); 458 EXPECT_FALSE(base_file_->GetHash(&hash));
345 base_file_->Finish(); 459 base_file_->Finish();
346 } 460 }
347 461
348 // Try to write to uninitialized file. 462 // Try to write to uninitialized file.
349 TEST_F(BaseFileTest, UninitializedFile) { 463 TEST_F(BaseFileTest, UninitializedFile) {
350 expect_in_progress_ = false; 464 expect_in_progress_ = false;
351 EXPECT_EQ(net::ERR_INVALID_HANDLE, AppendDataToFile(kTestData1)); 465 EXPECT_EQ(net::ERR_INVALID_HANDLE, AppendDataToFile(kTestData1));
352 } 466 }
353 467
354 // Create two |BaseFile|s with the same file, and attempt to write to both. 468 // Create two |BaseFile|s with the same file, and attempt to write to both.
355 // Overwrite base_file_ with another file with the same name and 469 // Overwrite base_file_ with another file with the same name and
356 // non-zero contents, and make sure the last file to close 'wins'. 470 // non-zero contents, and make sure the last file to close 'wins'.
357 TEST_F(BaseFileTest, DuplicateBaseFile) { 471 TEST_F(BaseFileTest, DuplicateBaseFile) {
358 EXPECT_EQ(net::OK, base_file_->Initialize(false)); 472 EXPECT_EQ(net::OK, base_file_->Initialize());
359 473
360 // Create another |BaseFile| referring to the file that |base_file_| owns. 474 // Create another |BaseFile| referring to the file that |base_file_| owns.
361 CreateFileWithName(base_file_->full_path()); 475 CreateFileWithName(base_file_->full_path());
362 476
363 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 477 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
364 base_file_->Finish(); 478 base_file_->Finish();
365 } 479 }
366 480
367 // Create a file and append to it. 481 // Create a file and append to it.
368 TEST_F(BaseFileTest, AppendToBaseFile) { 482 TEST_F(BaseFileTest, AppendToBaseFile) {
369 // Create a new file. 483 // Create a new file.
370 FilePath existing_file_name = CreateTestFile(); 484 FilePath existing_file_name = CreateTestFile();
371 485
372 set_expected_data(kTestData4); 486 set_expected_data(kTestData4);
373 487
374 // Use the file we've just created. 488 // Use the file we've just created.
375 base_file_.reset( 489 base_file_.reset(new BaseFile(existing_file_name,
376 new BaseFile(existing_file_name, GURL(), GURL(), kTestDataLength4, 490 GURL(),
377 file_stream_)); 491 GURL(),
492 kTestDataLength4,
493 false,
494 "",
495 file_stream_));
378 496
379 EXPECT_EQ(net::OK, base_file_->Initialize(false)); 497 EXPECT_EQ(net::OK, base_file_->Initialize());
380 498
381 const FilePath file_name = base_file_->full_path(); 499 const FilePath file_name = base_file_->full_path();
382 EXPECT_NE(FilePath::StringType(), file_name.value()); 500 EXPECT_NE(FilePath::StringType(), file_name.value());
383 501
384 // Write into the file. 502 // Write into the file.
385 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); 503 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1));
386 504
387 base_file_->Finish(); 505 base_file_->Finish();
388 base_file_->Detach(); 506 base_file_->Detach();
389 expect_file_survives_ = true; 507 expect_file_survives_ = true;
390 } 508 }
391 509
392 // Create a read-only file and attempt to write to it. 510 // Create a read-only file and attempt to write to it.
393 TEST_F(BaseFileTest, ReadonlyBaseFile) { 511 TEST_F(BaseFileTest, ReadonlyBaseFile) {
394 // Create a new file. 512 // Create a new file.
395 FilePath readonly_file_name = CreateTestFile(); 513 FilePath readonly_file_name = CreateTestFile();
396 514
397 // Make it read-only. 515 // Make it read-only.
398 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); 516 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name));
399 517
400 // Try to overwrite it. 518 // Try to overwrite it.
401 base_file_.reset( 519 base_file_.reset(new BaseFile(readonly_file_name,
402 new BaseFile(readonly_file_name, GURL(), GURL(), 0, file_stream_)); 520 GURL(),
521 GURL(),
522 0,
523 false,
524 "",
525 file_stream_));
403 526
404 expect_in_progress_ = false; 527 expect_in_progress_ = false;
405 528
406 int init_error = base_file_->Initialize(false); 529 int init_error = base_file_->Initialize();
407 DVLOG(1) << " init_error = " << init_error; 530 DVLOG(1) << " init_error = " << init_error;
408 EXPECT_NE(net::OK, init_error); 531 EXPECT_NE(net::OK, init_error);
409 532
410 const FilePath file_name = base_file_->full_path(); 533 const FilePath file_name = base_file_->full_path();
411 EXPECT_NE(FilePath::StringType(), file_name.value()); 534 EXPECT_NE(FilePath::StringType(), file_name.value());
412 535
413 // Write into the file. 536 // Write into the file.
414 EXPECT_NE(net::OK, AppendDataToFile(kTestData1)); 537 EXPECT_NE(net::OK, AppendDataToFile(kTestData1));
415 538
416 base_file_->Finish(); 539 base_file_->Finish();
417 base_file_->Detach(); 540 base_file_->Detach();
418 expect_file_survives_ = true; 541 expect_file_survives_ = true;
419 } 542 }
420 543
421 TEST_F(BaseFileTest, IsEmptySha256Hash) { 544 TEST_F(BaseFileTest, IsEmptyHash) {
422 std::string empty(BaseFile::kSha256HashLen, '\x00'); 545 std::string empty(BaseFile::kSha256HashLen, '\x00');
423 EXPECT_TRUE(BaseFile::IsEmptySha256Hash(empty)); 546 EXPECT_TRUE(BaseFile::IsEmptyHash(empty));
424 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); 547 std::string not_empty(BaseFile::kSha256HashLen, '\x01');
425 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(not_empty)); 548 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty));
426 EXPECT_FALSE(BaseFile::IsEmptySha256Hash("")); 549 EXPECT_FALSE(BaseFile::IsEmptyHash(""));
427 } 550 }
428 551
429 // Test that calculating speed after no writes. 552 // Test that calculating speed after no writes.
430 TEST_F(BaseFileTest, SpeedWithoutWrite) { 553 TEST_F(BaseFileTest, SpeedWithoutWrite) {
431 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 554 ASSERT_EQ(net::OK, base_file_->Initialize());
432 base::TimeTicks current = StartTick() + kElapsedTimeDelta; 555 base::TimeTicks current = StartTick() + kElapsedTimeDelta;
433 ASSERT_EQ(0, CurrentSpeedAtTime(current)); 556 ASSERT_EQ(0, CurrentSpeedAtTime(current));
434 base_file_->Finish(); 557 base_file_->Finish();
435 } 558 }
436 559
437 // Test that calculating speed after a single write. 560 // Test that calculating speed after a single write.
438 TEST_F(BaseFileTest, SpeedAfterSingleWrite) { 561 TEST_F(BaseFileTest, SpeedAfterSingleWrite) {
439 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 562 ASSERT_EQ(net::OK, base_file_->Initialize());
440 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 563 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
441 base::TimeTicks current = StartTick() + kElapsedTimeDelta; 564 base::TimeTicks current = StartTick() + kElapsedTimeDelta;
442 int64 expected_speed = kTestDataLength1 / kElapsedTimeSeconds; 565 int64 expected_speed = kTestDataLength1 / kElapsedTimeSeconds;
443 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); 566 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current));
444 base_file_->Finish(); 567 base_file_->Finish();
445 } 568 }
446 569
447 // Test that calculating speed after a multiple writes. 570 // Test that calculating speed after a multiple writes.
448 TEST_F(BaseFileTest, SpeedAfterMultipleWrite) { 571 TEST_F(BaseFileTest, SpeedAfterMultipleWrite) {
449 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 572 ASSERT_EQ(net::OK, base_file_->Initialize());
450 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 573 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
451 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 574 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
452 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 575 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
453 ASSERT_EQ(net::OK, AppendDataToFile(kTestData4)); 576 ASSERT_EQ(net::OK, AppendDataToFile(kTestData4));
454 base::TimeTicks current = StartTick() + kElapsedTimeDelta; 577 base::TimeTicks current = StartTick() + kElapsedTimeDelta;
455 int64 expected_speed = (kTestDataLength1 + kTestDataLength2 + 578 int64 expected_speed = (kTestDataLength1 + kTestDataLength2 +
456 kTestDataLength3 + kTestDataLength4) / kElapsedTimeSeconds; 579 kTestDataLength3 + kTestDataLength4) / kElapsedTimeSeconds;
457 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); 580 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current));
458 base_file_->Finish(); 581 base_file_->Finish();
459 } 582 }
460 583
461 // Test that calculating speed after no delay - should not divide by 0. 584 // Test that calculating speed after no delay - should not divide by 0.
462 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { 585 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) {
463 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 586 ASSERT_EQ(net::OK, base_file_->Initialize());
464 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 587 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
465 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); 588 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick()));
466 base_file_->Finish(); 589 base_file_->Finish();
467 } 590 }
OLDNEW
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/download_create_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698