File: cache_storage_index_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (255 lines) | stat: -rw-r--r-- 9,279 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/cache_storage/cache_storage_index.h"

#include <list>
#include <utility>
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

constexpr char16_t kUnicodeHighSurrogate = 0xD800;

TEST(CacheStorageIndexTest, TestDefaultConstructor) {
  CacheStorageIndex index;
  EXPECT_EQ(0u, index.num_entries());
  EXPECT_TRUE(index.ordered_cache_metadata().empty());
  EXPECT_EQ(0u, index.GetPaddedStorageSize());
}

TEST(CacheStorageIndexTest, TestSetCacheSize) {
  CacheStorageIndex index;

  index.Insert(
      CacheStorageIndex::CacheMetadata(u"foo", /*size=*/12, /*padding=*/2));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"bar", /*size=*/19, /*padding=*/3));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"baz", /*size=*/1000, /*padding=*/4));

  EXPECT_EQ(3u, index.num_entries());
  ASSERT_EQ(3u, index.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize());

  EXPECT_TRUE(index.SetCacheSize(u"baz", 2000));
  EXPECT_EQ(12 + 2 + 19 + 3 + 2000 + 4, index.GetPaddedStorageSize());

  EXPECT_FALSE(index.SetCacheSize(u"baz", 2000));
  EXPECT_EQ(12 + 2 + 19 + 3 + 2000 + 4, index.GetPaddedStorageSize());

  EXPECT_EQ(2000, index.GetCacheSizeForTesting(u"baz"));
  EXPECT_EQ(CacheStorage::kSizeUnknown,
            index.GetCacheSizeForTesting(u"<not-present>"));
}

TEST(CacheStorageIndexTest, TestSetCachePadding) {
  CacheStorageIndex index;
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"foo", /*size=*/12, /*padding=*/2));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"bar", /*size=*/19, /*padding=*/3));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"baz", /*size=*/1000, /*padding=*/4));
  EXPECT_EQ(3u, index.num_entries());
  ASSERT_EQ(3u, index.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize());

  EXPECT_TRUE(index.SetCachePadding(u"baz", 80));
  EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 80, index.GetPaddedStorageSize());

  EXPECT_FALSE(index.SetCachePadding(u"baz", 80));
  EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 80, index.GetPaddedStorageSize());

  EXPECT_EQ(80, index.GetCachePaddingForTesting(u"baz"));
  EXPECT_EQ(CacheStorage::kSizeUnknown,
            index.GetCachePaddingForTesting(u"<not-present>"));
}

TEST(CacheStorageIndexTest, TestDoomCache) {
  CacheStorageIndex index;
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"foo", /*size=*/12, /*padding=*/2));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"bar", /*size=*/19, /*padding=*/3));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"baz", /*size=*/1000, /*padding=*/4));
  EXPECT_EQ(3u, index.num_entries());
  ASSERT_EQ(3u, index.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize());

  index.DoomCache(u"bar");
  EXPECT_EQ(2u, index.num_entries());
  ASSERT_EQ(2u, index.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 2 + 1000 + 4, index.GetPaddedStorageSize());
  index.RestoreDoomedCache();
  EXPECT_EQ(3u, index.num_entries());
  ASSERT_EQ(3u, index.ordered_cache_metadata().size());
  auto it = index.ordered_cache_metadata().begin();
  EXPECT_EQ(u"foo", (it++)->name);
  EXPECT_EQ(u"bar", (it++)->name);
  EXPECT_EQ(u"baz", (it++)->name);
  EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize());

  index.DoomCache(u"foo");
  EXPECT_EQ(2u, index.num_entries());
  ASSERT_EQ(2u, index.ordered_cache_metadata().size());
  EXPECT_EQ(19 + 3 + 1000 + 4, index.GetPaddedStorageSize());
  index.FinalizeDoomedCache();
  EXPECT_EQ(2u, index.num_entries());
  ASSERT_EQ(2u, index.ordered_cache_metadata().size());
  EXPECT_EQ(19 + 3 + 1000 + 4, index.GetPaddedStorageSize());
}

TEST(CacheStorageIndexTest, TestDelete) {
  CacheStorageIndex index;
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"bar", /*size=*/19, /*padding=*/2));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"foo", /*size=*/12, /*padding=*/3));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"baz", /*size=*/1000, /*padding=*/4));
  EXPECT_EQ(3u, index.num_entries());
  ASSERT_EQ(3u, index.ordered_cache_metadata().size());
  EXPECT_EQ(19 + 2 + 12 + 3 + 1000 + 4, index.GetPaddedStorageSize());

  auto it = index.ordered_cache_metadata().begin();
  EXPECT_EQ(u"bar", it->name);
  EXPECT_EQ(19u, it->size);
  EXPECT_EQ(2u, it->padding);
  it++;
  EXPECT_EQ(u"foo", it->name);
  EXPECT_EQ(12u, it->size);
  EXPECT_EQ(3u, it->padding);
  it++;
  EXPECT_EQ(u"baz", it->name);
  EXPECT_EQ(1000u, it->size);
  EXPECT_EQ(4u, it->padding);

  index.Delete(u"bar");
  EXPECT_EQ(2u, index.num_entries());
  ASSERT_EQ(2u, index.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 3 + 1000 + 4, index.GetPaddedStorageSize());

  it = index.ordered_cache_metadata().begin();
  EXPECT_EQ(u"foo", it->name);
  EXPECT_EQ(12u, it->size);
  EXPECT_EQ(3u, it->padding);
  it++;
  EXPECT_EQ(u"baz", it->name);
  EXPECT_EQ(1000u, it->size);
  EXPECT_EQ(4u, it->padding);

  index.Delete(u"baz");
  EXPECT_EQ(1u, index.num_entries());
  ASSERT_EQ(1u, index.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 3, index.GetPaddedStorageSize());

  it = index.ordered_cache_metadata().begin();
  EXPECT_EQ(u"foo", it->name);
  EXPECT_EQ(12u, it->size);
  EXPECT_EQ(3u, it->padding);
}

TEST(CacheStorageIndexTest, TestInsert) {
  CacheStorageIndex index;
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"foo", /*size=*/12, /*padding=*/2));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"bar", /*size=*/19, /*padding=*/3));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"baz", /*size=*/1000, /*padding=*/4));
  EXPECT_EQ(3u, index.num_entries());
  ASSERT_EQ(3u, index.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index.GetPaddedStorageSize());
}

TEST(CacheStorageIndexTest, TestMoveOperator) {
  CacheStorageIndex index;
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"foo", /*size=*/12, /*padding=*/2));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"bar", /*size=*/19, /*padding=*/3));
  index.Insert(
      CacheStorageIndex::CacheMetadata(u"baz", /*size=*/1000, /*padding=*/4));

  CacheStorageIndex index2;
  index2 = std::move(index);

  EXPECT_EQ(3u, index2.num_entries());
  EXPECT_EQ(3u, index2.ordered_cache_metadata().size());
  ASSERT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index2.GetPaddedStorageSize());

  EXPECT_EQ(0u, index.num_entries());
  EXPECT_TRUE(index.ordered_cache_metadata().empty());
  EXPECT_EQ(0u, index.GetPaddedStorageSize());

  auto it = index2.ordered_cache_metadata().begin();
  EXPECT_EQ(u"foo", it->name);
  EXPECT_EQ(12u, it->size);
  EXPECT_EQ(2u, it->padding);
  it++;
  EXPECT_EQ(u"bar", it->name);
  EXPECT_EQ(19u, it->size);
  EXPECT_EQ(3u, it->padding);
  it++;
  EXPECT_EQ(u"baz", it->name);
  EXPECT_EQ(1000u, it->size);
  EXPECT_EQ(4u, it->padding);

  EXPECT_EQ(3u, index2.num_entries());
  ASSERT_EQ(3u, index2.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 2 + 19 + 3 + 1000 + 4, index2.GetPaddedStorageSize());
}

// This test verifies that all u16string cache name that have an invalid UTF-16
// character are stored as is and not replaced by the 'replacement character'.
TEST(CacheStorageIndexTest, TestInvalidCacheName) {
  CacheStorageIndex index;

  // Insert a cache with an invalid UTF-16 character.
  std::u16string invalid_utf16_name = u"bad";
  // kUnicodeHighSurrogate is considered an invalid UTF-16 character
  // when not followed by a low surrogate.
  invalid_utf16_name.push_back(kUnicodeHighSurrogate);
  index.Insert(CacheStorageIndex::CacheMetadata(invalid_utf16_name, /*size=*/12,
                                                /*padding=*/2));

  // Verify that the cache was inserted.
  EXPECT_EQ(1u, index.num_entries());
  ASSERT_EQ(1u, index.ordered_cache_metadata().size());
  EXPECT_EQ(12 + 2, index.GetPaddedStorageSize());

  // Verify that the cache name is stored correctly.
  auto it = index.ordered_cache_metadata().begin();
  EXPECT_EQ(invalid_utf16_name, it->name);
  EXPECT_EQ(12, it->size);
  EXPECT_EQ(2, it->padding);

  // Verify that the invalid character is stored as is.
  const std::u16string& stored_cache_name = it->name;
  EXPECT_EQ(stored_cache_name.size(), invalid_utf16_name.size());
  for (size_t i = 0; i < invalid_utf16_name.size(); ++i) {
    EXPECT_EQ(stored_cache_name[i], invalid_utf16_name[i]);
  }

  // Attempt to set the cache size and padding for the invalid name.
  EXPECT_TRUE(index.SetCacheSize(invalid_utf16_name, 20));
  EXPECT_EQ(20 + 2, index.GetPaddedStorageSize());

  EXPECT_TRUE(index.SetCachePadding(invalid_utf16_name, 5));
  EXPECT_EQ(20 + 5, index.GetPaddedStorageSize());

  // Verify the updated size and padding.
  EXPECT_EQ(20, index.GetCacheSizeForTesting(invalid_utf16_name));
  EXPECT_EQ(5, index.GetCachePaddingForTesting(invalid_utf16_name));

  // Delete the cache with the invalid name.
  index.Delete(invalid_utf16_name);
  EXPECT_EQ(0u, index.num_entries());
  ASSERT_EQ(0u, index.ordered_cache_metadata().size());
  EXPECT_EQ(0, index.GetPaddedStorageSize());
}

}  // namespace content