| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_index.h" | 5 #include "content/browser/cache_storage/cache_storage_index.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace content { | 11 namespace content { |
| 12 | 12 |
| 13 class CacheStorageIndexTest : public testing::Test { | 13 class CacheStorageIndexTest : public testing::Test { |
| 14 public: | 14 public: |
| 15 void SetUp() override {} | 15 void SetUp() override {} |
| 16 void TearDown() override {} | 16 void TearDown() override {} |
| 17 CacheStorageIndexTest() = default; | 17 CacheStorageIndexTest() : padding_key_("abcd1234") {} |
| 18 |
| 19 const std::string padding_key_; |
| 18 | 20 |
| 19 private: | 21 private: |
| 20 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndexTest); | 22 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndexTest); |
| 21 }; | 23 }; |
| 22 | 24 |
| 23 TEST_F(CacheStorageIndexTest, TestDefaultConstructor) { | 25 TEST_F(CacheStorageIndexTest, TestDefaultConstructor) { |
| 24 CacheStorageIndex index; | 26 CacheStorageIndex index; |
| 25 EXPECT_EQ(0u, index.num_entries()); | 27 EXPECT_EQ(0u, index.num_entries()); |
| 26 EXPECT_TRUE(index.ordered_cache_metadata().empty()); | 28 EXPECT_TRUE(index.ordered_cache_metadata().empty()); |
| 27 EXPECT_EQ(0u, index.GetStorageSize()); | 29 EXPECT_EQ(0u, index.GetPaddedStorageSize()); |
| 28 } | 30 } |
| 29 | 31 |
| 30 TEST_F(CacheStorageIndexTest, TestSetCacheSize) { | 32 TEST_F(CacheStorageIndexTest, TestSetCacheSize) { |
| 31 CacheStorageIndex index; | 33 CacheStorageIndex index; |
| 32 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 34 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 33 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 35 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 34 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 36 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 35 EXPECT_EQ(3u, index.num_entries()); | 37 EXPECT_EQ(3u, index.num_entries()); |
| 36 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 38 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 37 EXPECT_EQ(1031, index.GetStorageSize()); | 39 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 38 | 40 |
| 39 EXPECT_TRUE(index.SetCacheSize("baz", 2000)); | 41 EXPECT_TRUE(index.SetCacheSize("baz", 2000)); |
| 40 EXPECT_EQ(2031, index.GetStorageSize()); | 42 EXPECT_EQ(12 + 2 + 19 + 3 + 2000 + 4, index.GetPaddedStorageSize()); |
| 41 | 43 |
| 42 EXPECT_FALSE(index.SetCacheSize("baz", 2000)); | 44 EXPECT_FALSE(index.SetCacheSize("baz", 2000)); |
| 43 EXPECT_EQ(2031, index.GetStorageSize()); | 45 EXPECT_EQ(12 + 2 + 19 + 3 + 2000 + 4, index.GetPaddedStorageSize()); |
| 44 | 46 |
| 45 EXPECT_EQ(2000, index.GetCacheSize("baz")); | 47 EXPECT_EQ(2000, index.GetCacheSize("baz")); |
| 46 EXPECT_EQ(CacheStorage::kSizeUnknown, index.GetCacheSize("<not-present>")); | 48 EXPECT_EQ(CacheStorage::kSizeUnknown, index.GetCacheSize("<not-present>")); |
| 47 } | 49 } |
| 48 | 50 |
| 51 TEST_F(CacheStorageIndexTest, TestSetCachePadding) { |
| 52 CacheStorageIndex index; |
| 53 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 54 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 55 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 56 EXPECT_EQ(3u, index.num_entries()); |
| 57 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 58 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 59 |
| 60 EXPECT_TRUE(index.SetCachePadding("baz", 80)); |
| 61 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 80, index.GetPaddedStorageSize()); |
| 62 |
| 63 EXPECT_FALSE(index.SetCachePadding("baz", 80)); |
| 64 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 80, index.GetPaddedStorageSize()); |
| 65 |
| 66 EXPECT_EQ(80, index.GetCachePadding("baz")); |
| 67 EXPECT_EQ(CacheStorage::kSizeUnknown, index.GetCachePadding("<not-present>")); |
| 68 } |
| 69 |
| 49 TEST_F(CacheStorageIndexTest, TestDoomCache) { | 70 TEST_F(CacheStorageIndexTest, TestDoomCache) { |
| 50 CacheStorageIndex index; | 71 CacheStorageIndex index; |
| 51 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 72 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 52 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 73 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 53 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 74 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 54 EXPECT_EQ(3u, index.num_entries()); | 75 EXPECT_EQ(3u, index.num_entries()); |
| 55 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 76 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 56 EXPECT_EQ(1031, index.GetStorageSize()); | 77 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 57 | 78 |
| 58 index.DoomCache("bar"); | 79 index.DoomCache("bar"); |
| 59 EXPECT_EQ(2u, index.num_entries()); | 80 EXPECT_EQ(2u, index.num_entries()); |
| 60 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); | 81 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 61 EXPECT_EQ(1031 - 19, index.GetStorageSize()); | 82 EXPECT_EQ(12 + 2 + 1000 + 4, index.GetPaddedStorageSize()); |
| 62 index.RestoreDoomedCache(); | 83 index.RestoreDoomedCache(); |
| 63 EXPECT_EQ(3u, index.num_entries()); | 84 EXPECT_EQ(3u, index.num_entries()); |
| 64 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 85 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 65 auto it = index.ordered_cache_metadata().begin(); | 86 auto it = index.ordered_cache_metadata().begin(); |
| 66 EXPECT_EQ("foo", (it++)->name); | 87 EXPECT_EQ("foo", (it++)->name); |
| 67 EXPECT_EQ("bar", (it++)->name); | 88 EXPECT_EQ("bar", (it++)->name); |
| 68 EXPECT_EQ("baz", (it++)->name); | 89 EXPECT_EQ("baz", (it++)->name); |
| 69 EXPECT_EQ(1031, index.GetStorageSize()); | 90 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 70 | 91 |
| 71 index.DoomCache("foo"); | 92 index.DoomCache("foo"); |
| 72 EXPECT_EQ(2u, index.num_entries()); | 93 EXPECT_EQ(2u, index.num_entries()); |
| 73 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); | 94 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 74 EXPECT_EQ(1031 - 12, index.GetStorageSize()); | 95 EXPECT_EQ(19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 75 index.FinalizeDoomedCache(); | 96 index.FinalizeDoomedCache(); |
| 76 EXPECT_EQ(2u, index.num_entries()); | 97 EXPECT_EQ(2u, index.num_entries()); |
| 77 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); | 98 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 78 EXPECT_EQ(1031 - 12, index.GetStorageSize()); | 99 EXPECT_EQ(19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 79 } | 100 } |
| 80 | 101 |
| 81 TEST_F(CacheStorageIndexTest, TestDelete) { | 102 TEST_F(CacheStorageIndexTest, TestDelete) { |
| 82 CacheStorageIndex index; | 103 CacheStorageIndex index; |
| 83 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 104 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 2, padding_key_)); |
| 84 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 105 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 3, padding_key_)); |
| 85 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 106 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 86 EXPECT_EQ(3u, index.num_entries()); | 107 EXPECT_EQ(3u, index.num_entries()); |
| 87 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 108 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 88 EXPECT_EQ(1031, index.GetStorageSize()); | 109 EXPECT_EQ(19 + 2 + 12 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 89 | 110 |
| 90 auto it = index.ordered_cache_metadata().begin(); | 111 auto it = index.ordered_cache_metadata().begin(); |
| 91 EXPECT_EQ("bar", it->name); | 112 EXPECT_EQ("bar", it->name); |
| 92 EXPECT_EQ(19u, it->size); | 113 EXPECT_EQ(19u, it->size); |
| 114 EXPECT_EQ(2u, it->padding); |
| 93 it++; | 115 it++; |
| 94 EXPECT_EQ("foo", it->name); | 116 EXPECT_EQ("foo", it->name); |
| 95 EXPECT_EQ(12u, it->size); | 117 EXPECT_EQ(12u, it->size); |
| 118 EXPECT_EQ(3u, it->padding); |
| 96 it++; | 119 it++; |
| 97 EXPECT_EQ("baz", it->name); | 120 EXPECT_EQ("baz", it->name); |
| 98 EXPECT_EQ(1000u, it->size); | 121 EXPECT_EQ(1000u, it->size); |
| 122 EXPECT_EQ(4u, it->padding); |
| 99 | 123 |
| 100 index.Delete("bar"); | 124 index.Delete("bar"); |
| 101 EXPECT_EQ(2u, index.num_entries()); | 125 EXPECT_EQ(2u, index.num_entries()); |
| 102 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); | 126 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 103 EXPECT_EQ(1012, index.GetStorageSize()); | 127 EXPECT_EQ(12 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 104 | 128 |
| 105 it = index.ordered_cache_metadata().begin(); | 129 it = index.ordered_cache_metadata().begin(); |
| 106 EXPECT_EQ("foo", it->name); | 130 EXPECT_EQ("foo", it->name); |
| 107 EXPECT_EQ(12u, it->size); | 131 EXPECT_EQ(12u, it->size); |
| 132 EXPECT_EQ(3u, it->padding); |
| 108 it++; | 133 it++; |
| 109 EXPECT_EQ("baz", it->name); | 134 EXPECT_EQ("baz", it->name); |
| 110 EXPECT_EQ(1000u, it->size); | 135 EXPECT_EQ(1000u, it->size); |
| 136 EXPECT_EQ(4u, it->padding); |
| 111 | 137 |
| 112 index.Delete("baz"); | 138 index.Delete("baz"); |
| 113 EXPECT_EQ(1u, index.num_entries()); | 139 EXPECT_EQ(1u, index.num_entries()); |
| 114 ASSERT_EQ(1u, index.ordered_cache_metadata().size()); | 140 ASSERT_EQ(1u, index.ordered_cache_metadata().size()); |
| 115 EXPECT_EQ(12, index.GetStorageSize()); | 141 EXPECT_EQ(12 + 3, index.GetPaddedStorageSize()); |
| 116 | 142 |
| 117 it = index.ordered_cache_metadata().begin(); | 143 it = index.ordered_cache_metadata().begin(); |
| 118 EXPECT_EQ("foo", it->name); | 144 EXPECT_EQ("foo", it->name); |
| 119 EXPECT_EQ(12u, it->size); | 145 EXPECT_EQ(12u, it->size); |
| 146 EXPECT_EQ(3u, it->padding); |
| 120 } | 147 } |
| 121 | 148 |
| 122 TEST_F(CacheStorageIndexTest, TestInsert) { | 149 TEST_F(CacheStorageIndexTest, TestInsert) { |
| 123 CacheStorageIndex index; | 150 CacheStorageIndex index; |
| 124 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 151 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 125 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 152 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 126 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 153 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 127 EXPECT_EQ(3u, index.num_entries()); | 154 EXPECT_EQ(3u, index.num_entries()); |
| 128 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 155 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 129 EXPECT_EQ(1031, index.GetStorageSize()); | 156 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 130 } | 157 } |
| 131 | 158 |
| 132 TEST_F(CacheStorageIndexTest, TestMoveOperator) { | 159 TEST_F(CacheStorageIndexTest, TestMoveOperator) { |
| 133 CacheStorageIndex index; | 160 CacheStorageIndex index; |
| 134 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 161 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 135 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 162 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 136 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 163 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 137 | 164 |
| 138 CacheStorageIndex index2; | 165 CacheStorageIndex index2; |
| 139 index2 = std::move(index); | 166 index2 = std::move(index); |
| 140 | 167 |
| 141 EXPECT_EQ(3u, index2.num_entries()); | 168 EXPECT_EQ(3u, index2.num_entries()); |
| 142 EXPECT_EQ(3u, index2.ordered_cache_metadata().size()); | 169 EXPECT_EQ(3u, index2.ordered_cache_metadata().size()); |
| 143 ASSERT_EQ(1031, index2.GetStorageSize()); | 170 ASSERT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index2.GetPaddedStorageSize()); |
| 144 | 171 |
| 145 EXPECT_EQ(0u, index.num_entries()); | 172 EXPECT_EQ(0u, index.num_entries()); |
| 146 EXPECT_TRUE(index.ordered_cache_metadata().empty()); | 173 EXPECT_TRUE(index.ordered_cache_metadata().empty()); |
| 147 EXPECT_EQ(0u, index.GetStorageSize()); | 174 EXPECT_EQ(0u, index.GetPaddedStorageSize()); |
| 148 | 175 |
| 149 auto it = index2.ordered_cache_metadata().begin(); | 176 auto it = index2.ordered_cache_metadata().begin(); |
| 150 EXPECT_EQ("foo", it->name); | 177 EXPECT_EQ("foo", it->name); |
| 151 EXPECT_EQ(12u, it->size); | 178 EXPECT_EQ(12u, it->size); |
| 179 EXPECT_EQ(2u, it->padding); |
| 152 it++; | 180 it++; |
| 153 EXPECT_EQ("bar", it->name); | 181 EXPECT_EQ("bar", it->name); |
| 154 EXPECT_EQ(19u, it->size); | 182 EXPECT_EQ(19u, it->size); |
| 183 EXPECT_EQ(3u, it->padding); |
| 155 it++; | 184 it++; |
| 156 EXPECT_EQ("baz", it->name); | 185 EXPECT_EQ("baz", it->name); |
| 157 EXPECT_EQ(1000u, it->size); | 186 EXPECT_EQ(1000u, it->size); |
| 187 EXPECT_EQ(4u, it->padding); |
| 158 | 188 |
| 159 EXPECT_EQ(3u, index2.num_entries()); | 189 EXPECT_EQ(3u, index2.num_entries()); |
| 160 ASSERT_EQ(3u, index2.ordered_cache_metadata().size()); | 190 ASSERT_EQ(3u, index2.ordered_cache_metadata().size()); |
| 161 EXPECT_EQ(1031, index2.GetStorageSize()); | 191 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index2.GetPaddedStorageSize()); |
| 162 } | 192 } |
| 163 | 193 |
| 164 } // namespace content | 194 } // namespace content |
| OLD | NEW |