| 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.GetCacheSizeForTesting("baz")); |
| 46 EXPECT_EQ(CacheStorage::kSizeUnknown, index.GetCacheSize("<not-present>")); | 48 EXPECT_EQ(CacheStorage::kSizeUnknown, |
| 49 index.GetCacheSizeForTesting("<not-present>")); |
| 50 } |
| 51 |
| 52 TEST_F(CacheStorageIndexTest, TestSetCachePadding) { |
| 53 CacheStorageIndex index; |
| 54 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 55 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 56 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 57 EXPECT_EQ(3u, index.num_entries()); |
| 58 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 59 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 60 |
| 61 EXPECT_TRUE(index.SetCachePadding("baz", 80)); |
| 62 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 80, index.GetPaddedStorageSize()); |
| 63 |
| 64 EXPECT_FALSE(index.SetCachePadding("baz", 80)); |
| 65 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 80, index.GetPaddedStorageSize()); |
| 66 |
| 67 EXPECT_EQ(80, index.GetCachePaddingForTesting("baz")); |
| 68 EXPECT_EQ(CacheStorage::kSizeUnknown, |
| 69 index.GetCachePaddingForTesting("<not-present>")); |
| 47 } | 70 } |
| 48 | 71 |
| 49 TEST_F(CacheStorageIndexTest, TestDoomCache) { | 72 TEST_F(CacheStorageIndexTest, TestDoomCache) { |
| 50 CacheStorageIndex index; | 73 CacheStorageIndex index; |
| 51 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 74 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 52 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 75 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 53 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 76 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 54 EXPECT_EQ(3u, index.num_entries()); | 77 EXPECT_EQ(3u, index.num_entries()); |
| 55 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 78 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 56 EXPECT_EQ(1031, index.GetStorageSize()); | 79 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 57 | 80 |
| 58 index.DoomCache("bar"); | 81 index.DoomCache("bar"); |
| 59 EXPECT_EQ(2u, index.num_entries()); | 82 EXPECT_EQ(2u, index.num_entries()); |
| 60 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); | 83 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 61 EXPECT_EQ(1031 - 19, index.GetStorageSize()); | 84 EXPECT_EQ(12 + 2 + 1000 + 4, index.GetPaddedStorageSize()); |
| 62 index.RestoreDoomedCache(); | 85 index.RestoreDoomedCache(); |
| 63 EXPECT_EQ(3u, index.num_entries()); | 86 EXPECT_EQ(3u, index.num_entries()); |
| 64 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 87 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 65 auto it = index.ordered_cache_metadata().begin(); | 88 auto it = index.ordered_cache_metadata().begin(); |
| 66 EXPECT_EQ("foo", (it++)->name); | 89 EXPECT_EQ("foo", (it++)->name); |
| 67 EXPECT_EQ("bar", (it++)->name); | 90 EXPECT_EQ("bar", (it++)->name); |
| 68 EXPECT_EQ("baz", (it++)->name); | 91 EXPECT_EQ("baz", (it++)->name); |
| 69 EXPECT_EQ(1031, index.GetStorageSize()); | 92 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 70 | 93 |
| 71 index.DoomCache("foo"); | 94 index.DoomCache("foo"); |
| 72 EXPECT_EQ(2u, index.num_entries()); | 95 EXPECT_EQ(2u, index.num_entries()); |
| 73 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); | 96 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 74 EXPECT_EQ(1031 - 12, index.GetStorageSize()); | 97 EXPECT_EQ(19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 75 index.FinalizeDoomedCache(); | 98 index.FinalizeDoomedCache(); |
| 76 EXPECT_EQ(2u, index.num_entries()); | 99 EXPECT_EQ(2u, index.num_entries()); |
| 77 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); | 100 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 78 EXPECT_EQ(1031 - 12, index.GetStorageSize()); | 101 EXPECT_EQ(19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 79 } | 102 } |
| 80 | 103 |
| 81 TEST_F(CacheStorageIndexTest, TestDelete) { | 104 TEST_F(CacheStorageIndexTest, TestDelete) { |
| 82 CacheStorageIndex index; | 105 CacheStorageIndex index; |
| 83 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 106 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 2, padding_key_)); |
| 84 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 107 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 3, padding_key_)); |
| 85 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 108 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 86 EXPECT_EQ(3u, index.num_entries()); | 109 EXPECT_EQ(3u, index.num_entries()); |
| 87 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 110 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 88 EXPECT_EQ(1031, index.GetStorageSize()); | 111 EXPECT_EQ(19 + 2 + 12 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 89 | 112 |
| 90 auto it = index.ordered_cache_metadata().begin(); | 113 auto it = index.ordered_cache_metadata().begin(); |
| 91 EXPECT_EQ("bar", it->name); | 114 EXPECT_EQ("bar", it->name); |
| 92 EXPECT_EQ(19u, it->size); | 115 EXPECT_EQ(19u, it->size); |
| 116 EXPECT_EQ(2u, it->padding); |
| 93 it++; | 117 it++; |
| 94 EXPECT_EQ("foo", it->name); | 118 EXPECT_EQ("foo", it->name); |
| 95 EXPECT_EQ(12u, it->size); | 119 EXPECT_EQ(12u, it->size); |
| 120 EXPECT_EQ(3u, it->padding); |
| 96 it++; | 121 it++; |
| 97 EXPECT_EQ("baz", it->name); | 122 EXPECT_EQ("baz", it->name); |
| 98 EXPECT_EQ(1000u, it->size); | 123 EXPECT_EQ(1000u, it->size); |
| 124 EXPECT_EQ(4u, it->padding); |
| 99 | 125 |
| 100 index.Delete("bar"); | 126 index.Delete("bar"); |
| 101 EXPECT_EQ(2u, index.num_entries()); | 127 EXPECT_EQ(2u, index.num_entries()); |
| 102 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); | 128 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 103 EXPECT_EQ(1012, index.GetStorageSize()); | 129 EXPECT_EQ(12 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 104 | 130 |
| 105 it = index.ordered_cache_metadata().begin(); | 131 it = index.ordered_cache_metadata().begin(); |
| 106 EXPECT_EQ("foo", it->name); | 132 EXPECT_EQ("foo", it->name); |
| 107 EXPECT_EQ(12u, it->size); | 133 EXPECT_EQ(12u, it->size); |
| 134 EXPECT_EQ(3u, it->padding); |
| 108 it++; | 135 it++; |
| 109 EXPECT_EQ("baz", it->name); | 136 EXPECT_EQ("baz", it->name); |
| 110 EXPECT_EQ(1000u, it->size); | 137 EXPECT_EQ(1000u, it->size); |
| 138 EXPECT_EQ(4u, it->padding); |
| 111 | 139 |
| 112 index.Delete("baz"); | 140 index.Delete("baz"); |
| 113 EXPECT_EQ(1u, index.num_entries()); | 141 EXPECT_EQ(1u, index.num_entries()); |
| 114 ASSERT_EQ(1u, index.ordered_cache_metadata().size()); | 142 ASSERT_EQ(1u, index.ordered_cache_metadata().size()); |
| 115 EXPECT_EQ(12, index.GetStorageSize()); | 143 EXPECT_EQ(12 + 3, index.GetPaddedStorageSize()); |
| 116 | 144 |
| 117 it = index.ordered_cache_metadata().begin(); | 145 it = index.ordered_cache_metadata().begin(); |
| 118 EXPECT_EQ("foo", it->name); | 146 EXPECT_EQ("foo", it->name); |
| 119 EXPECT_EQ(12u, it->size); | 147 EXPECT_EQ(12u, it->size); |
| 148 EXPECT_EQ(3u, it->padding); |
| 120 } | 149 } |
| 121 | 150 |
| 122 TEST_F(CacheStorageIndexTest, TestInsert) { | 151 TEST_F(CacheStorageIndexTest, TestInsert) { |
| 123 CacheStorageIndex index; | 152 CacheStorageIndex index; |
| 124 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 153 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 125 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 154 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 126 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 155 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 127 EXPECT_EQ(3u, index.num_entries()); | 156 EXPECT_EQ(3u, index.num_entries()); |
| 128 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); | 157 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 129 EXPECT_EQ(1031, index.GetStorageSize()); | 158 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize()); |
| 130 } | 159 } |
| 131 | 160 |
| 132 TEST_F(CacheStorageIndexTest, TestMoveOperator) { | 161 TEST_F(CacheStorageIndexTest, TestMoveOperator) { |
| 133 CacheStorageIndex index; | 162 CacheStorageIndex index; |
| 134 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); | 163 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12, 2, padding_key_)); |
| 135 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); | 164 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19, 3, padding_key_)); |
| 136 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); | 165 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000, 4, padding_key_)); |
| 137 | 166 |
| 138 CacheStorageIndex index2; | 167 CacheStorageIndex index2; |
| 139 index2 = std::move(index); | 168 index2 = std::move(index); |
| 140 | 169 |
| 141 EXPECT_EQ(3u, index2.num_entries()); | 170 EXPECT_EQ(3u, index2.num_entries()); |
| 142 EXPECT_EQ(3u, index2.ordered_cache_metadata().size()); | 171 EXPECT_EQ(3u, index2.ordered_cache_metadata().size()); |
| 143 ASSERT_EQ(1031, index2.GetStorageSize()); | 172 ASSERT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index2.GetPaddedStorageSize()); |
| 144 | 173 |
| 145 EXPECT_EQ(0u, index.num_entries()); | 174 EXPECT_EQ(0u, index.num_entries()); |
| 146 EXPECT_TRUE(index.ordered_cache_metadata().empty()); | 175 EXPECT_TRUE(index.ordered_cache_metadata().empty()); |
| 147 EXPECT_EQ(0u, index.GetStorageSize()); | 176 EXPECT_EQ(0u, index.GetPaddedStorageSize()); |
| 148 | 177 |
| 149 auto it = index2.ordered_cache_metadata().begin(); | 178 auto it = index2.ordered_cache_metadata().begin(); |
| 150 EXPECT_EQ("foo", it->name); | 179 EXPECT_EQ("foo", it->name); |
| 151 EXPECT_EQ(12u, it->size); | 180 EXPECT_EQ(12u, it->size); |
| 181 EXPECT_EQ(2u, it->padding); |
| 152 it++; | 182 it++; |
| 153 EXPECT_EQ("bar", it->name); | 183 EXPECT_EQ("bar", it->name); |
| 154 EXPECT_EQ(19u, it->size); | 184 EXPECT_EQ(19u, it->size); |
| 185 EXPECT_EQ(3u, it->padding); |
| 155 it++; | 186 it++; |
| 156 EXPECT_EQ("baz", it->name); | 187 EXPECT_EQ("baz", it->name); |
| 157 EXPECT_EQ(1000u, it->size); | 188 EXPECT_EQ(1000u, it->size); |
| 189 EXPECT_EQ(4u, it->padding); |
| 158 | 190 |
| 159 EXPECT_EQ(3u, index2.num_entries()); | 191 EXPECT_EQ(3u, index2.num_entries()); |
| 160 ASSERT_EQ(3u, index2.ordered_cache_metadata().size()); | 192 ASSERT_EQ(3u, index2.ordered_cache_metadata().size()); |
| 161 EXPECT_EQ(1031, index2.GetStorageSize()); | 193 EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index2.GetPaddedStorageSize()); |
| 162 } | 194 } |
| 163 | 195 |
| 164 } // namespace content | 196 } // namespace content |
| OLD | NEW |