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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
18 #include "base/test/test_file_util.h" | 17 #include "base/test/test_file_util.h" |
19 #include "build/build_config.h" | 18 #include "build/build_config.h" |
20 #include "content/browser/browser_thread_impl.h" | |
21 #include "content/public/browser/download_interrupt_reasons.h" | 19 #include "content/public/browser/download_interrupt_reasons.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
22 #include "crypto/secure_hash.h" | 21 #include "crypto/secure_hash.h" |
23 #include "crypto/sha2.h" | 22 #include "crypto/sha2.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
25 | 24 |
26 namespace content { | 25 namespace content { |
27 namespace { | 26 namespace { |
28 | 27 |
29 const char kTestData1[] = "Let's write some data to the file!\n"; | 28 const char kTestData1[] = "Let's write some data to the file!\n"; |
30 const char kTestData2[] = "Writing more data.\n"; | 29 const char kTestData2[] = "Writing more data.\n"; |
31 const char kTestData3[] = "Final line."; | 30 const char kTestData3[] = "Final line."; |
(...skipping 17 matching lines...) Expand all Loading... |
49 | 48 |
50 } // namespace | 49 } // namespace |
51 | 50 |
52 class BaseFileTest : public testing::Test { | 51 class BaseFileTest : public testing::Test { |
53 public: | 52 public: |
54 static const unsigned char kEmptySha256Hash[crypto::kSHA256Length]; | 53 static const unsigned char kEmptySha256Hash[crypto::kSHA256Length]; |
55 | 54 |
56 BaseFileTest() | 55 BaseFileTest() |
57 : expect_file_survives_(false), | 56 : expect_file_survives_(false), |
58 expect_in_progress_(true), | 57 expect_in_progress_(true), |
59 expected_error_(DOWNLOAD_INTERRUPT_REASON_NONE), | 58 expected_error_(DOWNLOAD_INTERRUPT_REASON_NONE) {} |
60 file_thread_(BrowserThread::FILE, &message_loop_) { | |
61 } | |
62 | 59 |
63 void SetUp() override { | 60 void SetUp() override { |
64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
65 base_file_.reset(new BaseFile(net::NetLogWithSource())); | 62 base_file_.reset(new BaseFile(net::NetLogWithSource())); |
66 } | 63 } |
67 | 64 |
68 void TearDown() override { | 65 void TearDown() override { |
69 EXPECT_FALSE(base_file_->in_progress()); | 66 EXPECT_FALSE(base_file_->in_progress()); |
70 if (!expected_error_) { | 67 if (!expected_error_) { |
71 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), | 68 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 bool expect_file_survives_; | 189 bool expect_file_survives_; |
193 | 190 |
194 // Expect the file to be in progress. | 191 // Expect the file to be in progress. |
195 bool expect_in_progress_; | 192 bool expect_in_progress_; |
196 | 193 |
197 private: | 194 private: |
198 // Keep track of what data should be saved to the disk file. | 195 // Keep track of what data should be saved to the disk file. |
199 std::string expected_data_; | 196 std::string expected_data_; |
200 DownloadInterruptReason expected_error_; | 197 DownloadInterruptReason expected_error_; |
201 | 198 |
202 // Mock file thread to satisfy debug checks in BaseFile. | 199 TestBrowserThreadBundle thread_bundle_; |
203 base::MessageLoop message_loop_; | |
204 BrowserThreadImpl file_thread_; | |
205 }; | 200 }; |
206 | 201 |
207 // This will initialize the entire array to zero. | 202 // This will initialize the entire array to zero. |
208 const unsigned char BaseFileTest::kEmptySha256Hash[] = { 0 }; | 203 const unsigned char BaseFileTest::kEmptySha256Hash[] = { 0 }; |
209 | 204 |
210 // Test the most basic scenario: just create the object and do a sanity check | 205 // Test the most basic scenario: just create the object and do a sanity check |
211 // on all its accessors. This is actually a case that rarely happens | 206 // on all its accessors. This is actually a case that rarely happens |
212 // in production, where we would at least Initialize it. | 207 // in production, where we would at least Initialize it. |
213 TEST_F(BaseFileTest, CreateDestroy) { | 208 TEST_F(BaseFileTest, CreateDestroy) { |
214 EXPECT_EQ(base::FilePath().value(), base_file_->full_path().value()); | 209 EXPECT_EQ(base::FilePath().value(), base_file_->full_path().value()); |
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 | 722 |
728 const char kData[] = "hello"; | 723 const char kData[] = "hello"; |
729 const int kDataLength = static_cast<int>(arraysize(kData) - 1); | 724 const int kDataLength = static_cast<int>(arraysize(kData) - 1); |
730 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); | 725 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); |
731 // The file that we created here should stick around when the BaseFile is | 726 // The file that we created here should stick around when the BaseFile is |
732 // destroyed during TearDown. | 727 // destroyed during TearDown. |
733 expect_file_survives_ = true; | 728 expect_file_survives_ = true; |
734 } | 729 } |
735 | 730 |
736 } // namespace content | 731 } // namespace content |
OLD | NEW |