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