| 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" | 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" |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 244 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 245 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 245 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 246 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 246 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); |
| 247 std::string hash; | 247 std::string hash; |
| 248 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 248 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
| 249 base_file_->Finish(); | 249 base_file_->Finish(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 // Write data to the file once and calculate its sha256 hash. | 252 // Write data to the file once and calculate its sha256 hash. |
| 253 TEST_F(BaseFileTest, SingleWriteWithHash) { | 253 TEST_F(BaseFileTest, SingleWriteWithHash) { |
| 254 std::string partial_hash; |
| 254 ASSERT_EQ(net::OK, base_file_->Initialize(true)); | 255 ASSERT_EQ(net::OK, base_file_->Initialize(true)); |
| 256 // Can get partial hashes before Finish() is called. |
| 257 EXPECT_TRUE(base_file_->GetSha256Hash(&partial_hash)); |
| 255 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 258 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 259 EXPECT_TRUE(base_file_->GetSha256Hash(&partial_hash)); |
| 256 base_file_->Finish(); | 260 base_file_->Finish(); |
| 257 | 261 |
| 258 std::string hash; | 262 std::string hash; |
| 259 base_file_->GetSha256Hash(&hash); | 263 base_file_->GetSha256Hash(&hash); |
| 260 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 264 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
| 261 base::HexEncode(hash.data(), hash.size())); | 265 base::HexEncode(hash.data(), hash.size())); |
| 262 } | 266 } |
| 263 | 267 |
| 264 // Write data to the file multiple times and calculate its sha256 hash. | 268 // Write data to the file multiple times and calculate its sha256 hash. |
| 265 TEST_F(BaseFileTest, MultipleWritesWithHash) { | 269 TEST_F(BaseFileTest, MultipleWritesWithHash) { |
| 266 std::string hash; | 270 std::string hash; |
| 267 | 271 |
| 268 ASSERT_EQ(net::OK, base_file_->Initialize(true)); | 272 ASSERT_EQ(net::OK, base_file_->Initialize(true)); |
| 269 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 273 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 270 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 274 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 271 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 275 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); |
| 272 // no hash before Finish() is called either. | |
| 273 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | |
| 274 base_file_->Finish(); | 276 base_file_->Finish(); |
| 275 | 277 |
| 276 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); | 278 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); |
| 277 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", | 279 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", |
| 278 base::HexEncode(hash.data(), hash.size())); | 280 base::HexEncode(hash.data(), hash.size())); |
| 279 } | 281 } |
| 280 | 282 |
| 281 // Rename the file after all writes to it. | 283 // Rename the file after all writes to it. |
| 282 TEST_F(BaseFileTest, WriteThenRename) { | 284 TEST_F(BaseFileTest, WriteThenRename) { |
| 283 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 285 ASSERT_EQ(net::OK, base_file_->Initialize(false)); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 expect_file_survives_ = true; | 407 expect_file_survives_ = true; |
| 406 } | 408 } |
| 407 | 409 |
| 408 TEST_F(BaseFileTest, IsEmptySha256Hash) { | 410 TEST_F(BaseFileTest, IsEmptySha256Hash) { |
| 409 std::string empty(BaseFile::kSha256HashLen, '\x00'); | 411 std::string empty(BaseFile::kSha256HashLen, '\x00'); |
| 410 EXPECT_TRUE(BaseFile::IsEmptySha256Hash(empty)); | 412 EXPECT_TRUE(BaseFile::IsEmptySha256Hash(empty)); |
| 411 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); | 413 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); |
| 412 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(not_empty)); | 414 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(not_empty)); |
| 413 EXPECT_FALSE(BaseFile::IsEmptySha256Hash("")); | 415 EXPECT_FALSE(BaseFile::IsEmptySha256Hash("")); |
| 414 } | 416 } |
| OLD | NEW |