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

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

Issue 6023006: Add support to sha256 hash the downloaded file.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/scoped_temp_dir.h" 7 #include "base/scoped_temp_dir.h"
8 #include "chrome/browser/browser_thread.h" 8 #include "chrome/browser/browser_thread.h"
9 #include "chrome/browser/download/base_file.h" 9 #include "chrome/browser/download/base_file.h"
10 #include "net/base/file_stream.h" 10 #include "net/base/file_stream.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace { 13 namespace {
14 14
15 const char kTestData1[] = "Let's write some data to the file!\n"; 15 const char kTestData1[] = "Let's write some data to the file!\n";
16 const char kTestData2[] = "Writing more data.\n"; 16 const char kTestData2[] = "Writing more data.\n";
17 const char kTestData3[] = "Final line."; 17 const char kTestData3[] = "Final line.";
18 18
19 class BaseFileTest : public testing::Test { 19 class BaseFileTest : public testing::Test {
20 public: 20 public:
21 BaseFileTest() : file_thread_(BrowserThread::FILE, &message_loop_) { 21 BaseFileTest() : file_thread_(BrowserThread::FILE, &message_loop_) {
22 } 22 }
23 23
24 virtual void SetUp() { 24 virtual void SetUp() {
25 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 25 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
26 base_file_.reset(new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_)); 26 base_file_.reset(
27 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_, false));
27 } 28 }
28 29
29 virtual void TearDown() { 30 virtual void TearDown() {
30 EXPECT_FALSE(base_file_->in_progress()); 31 EXPECT_FALSE(base_file_->in_progress());
31 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 32 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
32 base_file_->bytes_so_far()); 33 base_file_->bytes_so_far());
33 34
34 if (!expected_data_.empty()) { 35 if (!expected_data_.empty()) {
35 // Make sure the data has been properly written to disk. 36 // Make sure the data has been properly written to disk.
36 std::string disk_data; 37 std::string disk_data;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 TEST_F(BaseFileTest, MultipleWrites) { 102 TEST_F(BaseFileTest, MultipleWrites) {
102 ASSERT_TRUE(base_file_->Initialize()); 103 ASSERT_TRUE(base_file_->Initialize());
103 AppendDataToFile(kTestData1); 104 AppendDataToFile(kTestData1);
104 AppendDataToFile(kTestData2); 105 AppendDataToFile(kTestData2);
105 AppendDataToFile(kTestData3); 106 AppendDataToFile(kTestData3);
106 base_file_->Finish(); 107 base_file_->Finish();
107 108
108 EXPECT_FALSE(base_file_->path_renamed()); 109 EXPECT_FALSE(base_file_->path_renamed());
109 } 110 }
110 111
112 // Write data to the file once and calculate its sha256 hash.
113 TEST_F(BaseFileTest, SingleWriteWithHash) {
114 base_file_.reset(
115 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_, true));
116 ASSERT_TRUE(base_file_->Initialize());
117 AppendDataToFile(kTestData1);
118 base_file_->Finish();
119
120 EXPECT_FALSE(base_file_->path_renamed());
121
122 std::string hash;
123 base_file_->GetSha256Hash(&hash);
124 EXPECT_EQ("0b2d3f3f7943ad64b860df94d05cb56a8a97c6ec5768b5b70b930c5aa7fa9ade",
125 hash);
126 }
127
128 // Write data to the file multiple times and calculate its sha256 hash.
129 TEST_F(BaseFileTest, MultipleWritesWithHash) {
130 base_file_.reset(
131 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_, true));
132 ASSERT_TRUE(base_file_->Initialize());
133 AppendDataToFile(kTestData1);
134 AppendDataToFile(kTestData2);
135 AppendDataToFile(kTestData3);
136 base_file_->Finish();
137
138 EXPECT_FALSE(base_file_->path_renamed());
139
140 std::string hash;
141 base_file_->GetSha256Hash(&hash);
142 EXPECT_EQ("cbf68bf10f8003db86b31343afac8c7175bd03fb5fc905650f8c80af087443a8",
143 hash);
144 }
145
146
111 // Rename the file after all writes to it. 147 // Rename the file after all writes to it.
112 TEST_F(BaseFileTest, WriteThenRename) { 148 TEST_F(BaseFileTest, WriteThenRename) {
113 ASSERT_TRUE(base_file_->Initialize()); 149 ASSERT_TRUE(base_file_->Initialize());
114 150
115 FilePath initial_path(base_file_->full_path()); 151 FilePath initial_path(base_file_->full_path());
116 EXPECT_TRUE(file_util::PathExists(initial_path)); 152 EXPECT_TRUE(file_util::PathExists(initial_path));
117 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 153 FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
118 EXPECT_FALSE(file_util::PathExists(new_path)); 154 EXPECT_FALSE(file_util::PathExists(new_path));
119 155
120 AppendDataToFile(kTestData1); 156 AppendDataToFile(kTestData1);
(...skipping 24 matching lines...) Expand all
145 EXPECT_TRUE(file_util::PathExists(new_path)); 181 EXPECT_TRUE(file_util::PathExists(new_path));
146 182
147 AppendDataToFile(kTestData2); 183 AppendDataToFile(kTestData2);
148 184
149 base_file_->Finish(); 185 base_file_->Finish();
150 186
151 EXPECT_TRUE(base_file_->path_renamed()); 187 EXPECT_TRUE(base_file_->path_renamed());
152 } 188 }
153 189
154 } // namespace 190 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698