OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/cache_storage/cache_storage.h" | 5 #include "content/browser/cache_storage/cache_storage.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/guid.h" |
10 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
11 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
12 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "content/browser/cache_storage/cache_storage.pb.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
14 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
15 #include "storage/browser/quota/quota_manager_proxy.h" | 19 #include "storage/browser/quota/quota_manager_proxy.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
17 | 21 |
18 namespace content { | 22 namespace content { |
19 | 23 |
20 namespace { | 24 namespace { |
21 const char kOrigin[] = "http://example.com/"; | 25 const char kOrigin[] = "http://example.com/"; |
| 26 |
| 27 void EnumerateCachesCallback(CacheStorage::StringVector* cache_names_out, |
| 28 CacheStorageError* error_out, |
| 29 const CacheStorage::StringVector& cache_names, |
| 30 CacheStorageError error) { |
| 31 *cache_names_out = cache_names; |
| 32 *error_out = error; |
| 33 } |
22 } | 34 } |
23 | 35 |
24 class TestCacheStorage : public CacheStorage { | 36 class TestCacheStorage : public CacheStorage { |
25 public: | 37 public: |
26 explicit TestCacheStorage(const base::FilePath& file_path) | 38 explicit TestCacheStorage(const base::FilePath& file_path) |
27 : CacheStorage(file_path, | 39 : CacheStorage(file_path, |
28 false /* memory_only */, | 40 false /* memory_only */, |
29 base::ThreadTaskRunnerHandle::Get().get(), | 41 base::ThreadTaskRunnerHandle::Get().get(), |
30 scoped_refptr<net::URLRequestContextGetter>(), | 42 scoped_refptr<net::URLRequestContextGetter>(), |
31 scoped_refptr<storage::QuotaManagerProxy>(), | 43 scoped_refptr<storage::QuotaManagerProxy>(), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 class CacheStorageTest : public testing::Test { | 75 class CacheStorageTest : public testing::Test { |
64 protected: | 76 protected: |
65 CacheStorageTest() | 77 CacheStorageTest() |
66 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 78 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
67 | 79 |
68 void SetUp() override { | 80 void SetUp() override { |
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 81 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
70 test_cache_storage_.reset(new TestCacheStorage(temp_dir_.path())); | 82 test_cache_storage_.reset(new TestCacheStorage(temp_dir_.path())); |
71 } | 83 } |
72 | 84 |
73 bool OpenCache(const std::string& cache_name) { | 85 bool OpenCache(const base::string16& cache_name) { |
74 bool callback_called = false; | 86 bool callback_called = false; |
75 test_cache_storage_->OpenCache( | 87 test_cache_storage_->OpenCache( |
76 cache_name, base::Bind(&CacheStorageTest::OpenCacheCallback, | 88 cache_name, base::Bind(&CacheStorageTest::OpenCacheCallback, |
77 base::Unretained(this), &callback_called)); | 89 base::Unretained(this), &callback_called)); |
78 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
79 EXPECT_TRUE(callback_called); | 91 EXPECT_TRUE(callback_called); |
80 return callback_error_ == CACHE_STORAGE_OK; | 92 return callback_error_ == CACHE_STORAGE_OK; |
81 } | 93 } |
82 | 94 |
83 void OpenCacheCallback(bool* callback_called, | 95 void OpenCacheCallback(bool* callback_called, |
84 const scoped_refptr<CacheStorageCache>& cache, | 96 const scoped_refptr<CacheStorageCache>& cache, |
85 CacheStorageError error) { | 97 CacheStorageError error) { |
86 *callback_called = true; | 98 *callback_called = true; |
87 callback_cache_ = cache; | 99 callback_cache_ = cache; |
88 callback_error_ = error; | 100 callback_error_ = error; |
89 } | 101 } |
90 | 102 |
| 103 bool EnumerateCaches(CacheStorage::StringVector* cache_names) { |
| 104 CacheStorageError error = CACHE_STORAGE_ERROR_NOT_FOUND; |
| 105 test_cache_storage_->EnumerateCaches( |
| 106 base::Bind(&EnumerateCachesCallback, base::Unretained(cache_names), |
| 107 base::Unretained(&error))); |
| 108 base::RunLoop().RunUntilIdle(); |
| 109 return error == CACHE_STORAGE_OK; |
| 110 } |
| 111 |
91 base::ScopedTempDir temp_dir_; | 112 base::ScopedTempDir temp_dir_; |
92 TestBrowserThreadBundle browser_thread_bundle_; | 113 TestBrowserThreadBundle browser_thread_bundle_; |
93 scoped_ptr<TestCacheStorage> test_cache_storage_; | 114 scoped_ptr<TestCacheStorage> test_cache_storage_; |
94 | 115 |
95 scoped_refptr<CacheStorageCache> callback_cache_; | 116 scoped_refptr<CacheStorageCache> callback_cache_; |
96 CacheStorageError callback_error_; | 117 CacheStorageError callback_error_; |
97 }; | 118 }; |
98 | 119 |
99 TEST_F(CacheStorageTest, PreserveCache) { | 120 TEST_F(CacheStorageTest, PreserveCache) { |
100 test_cache_storage_->set_delay_preserved_cache_callback(true); | 121 test_cache_storage_->set_delay_preserved_cache_callback(true); |
101 EXPECT_TRUE(OpenCache("foo")); | 122 EXPECT_TRUE(OpenCache(base::UTF8ToUTF16("foo"))); |
102 EXPECT_TRUE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); | 123 EXPECT_TRUE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
103 test_cache_storage_->RunPreservedCacheCallback(); | 124 test_cache_storage_->RunPreservedCacheCallback(); |
104 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); | 125 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
105 | 126 |
106 // Try opening it again, since the cache is already open (callback_cache_ is | 127 // Try opening it again, since the cache is already open (callback_cache_ is |
107 // referencing it) the cache shouldn't be preserved again. | 128 // referencing it) the cache shouldn't be preserved again. |
108 EXPECT_TRUE(OpenCache("foo")); | 129 EXPECT_TRUE(OpenCache(base::UTF8ToUTF16("foo"))); |
109 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); | 130 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
110 | 131 |
111 // Remove the reference to the cache so that it's deleted. After that, the | 132 // Remove the reference to the cache so that it's deleted. After that, the |
112 // next time it's opened will require the cache to be created again and | 133 // next time it's opened will require the cache to be created again and |
113 // preserved. | 134 // preserved. |
114 callback_cache_ = nullptr; | 135 callback_cache_ = nullptr; |
115 EXPECT_TRUE(OpenCache("foo")); | 136 EXPECT_TRUE(OpenCache(base::UTF8ToUTF16("foo"))); |
116 EXPECT_TRUE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); | 137 EXPECT_TRUE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
117 test_cache_storage_->RunPreservedCacheCallback(); | 138 test_cache_storage_->RunPreservedCacheCallback(); |
118 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); | 139 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
119 } | 140 } |
120 | 141 |
| 142 // Until M50 we didn't use |name_utf16| but used |name| in CacheStorageIndex. |
| 143 // This test checks the backward compatibility. |
| 144 TEST_F(CacheStorageTest, ReadOldIndex) { |
| 145 CacheStorageIndex index; |
| 146 index.set_origin(kOrigin); |
| 147 CacheStorageIndex::Cache* index_cache1 = index.add_cache(); |
| 148 index_cache1->set_name("aaa"); |
| 149 index_cache1->set_cache_dir( |
| 150 temp_dir_.path().AppendASCII(base::GenerateGUID()).value()); |
| 151 CacheStorageIndex::Cache* index_cache2 = index.add_cache(); |
| 152 index_cache2->set_name("bbb"); |
| 153 index_cache2->set_cache_dir( |
| 154 temp_dir_.path().AppendASCII(base::GenerateGUID()).value()); |
| 155 |
| 156 std::string serialized; |
| 157 ASSERT_TRUE(index.SerializeToString(&serialized)); |
| 158 base::WriteFile(temp_dir_.path().AppendASCII(CacheStorage::kIndexFileName), |
| 159 serialized.c_str(), serialized.size()); |
| 160 |
| 161 CacheStorage::StringVector cache_names; |
| 162 EXPECT_TRUE(EnumerateCaches(&cache_names)); |
| 163 EXPECT_EQ(2u, cache_names.size()); |
| 164 EXPECT_EQ(base::UTF8ToUTF16("aaa"), cache_names[0]); |
| 165 EXPECT_EQ(base::UTF8ToUTF16("bbb"), cache_names[1]); |
| 166 } |
| 167 |
121 } // namespace content | 168 } // namespace content |
OLD | NEW |