summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/dom_storage/local_storage_cached_area_unittest.cc
blob: 341cbc23f338d3a874ca7655463ce66af4a39186 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/dom_storage/local_storage_cached_area.h"

#include <stdint.h>

#include <list>

#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "content/common/leveldb_wrapper.mojom.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/renderer/dom_storage/local_storage_cached_areas.h"
#include "content/renderer/dom_storage/mock_leveldb_wrapper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/scheduler/test/fake_renderer_scheduler.h"

namespace content {

class LocalStorageCachedAreaTest : public testing::Test {
 public:
  LocalStorageCachedAreaTest()
      : kOrigin(url::Origin::Create(GURL("http://dom_storage/"))),
        kKey(base::ASCIIToUTF16("key")),
        kValue(base::ASCIIToUTF16("value")),
        kPageUrl("http://dom_storage/page"),
        kStorageAreaId("7"),
        kSource(kPageUrl.spec() + "\n" + kStorageAreaId),
        renderer_scheduler_(new blink::scheduler::FakeRendererScheduler()),
        cached_areas_(&mock_leveldb_wrapper_, renderer_scheduler_.get()) {}

  const url::Origin kOrigin;
  const base::string16 kKey;
  const base::string16 kValue;
  const GURL kPageUrl;
  const std::string kStorageAreaId;
  const std::string kSource;

  bool IsCacheLoaded(LocalStorageCachedArea* cached_area) {
    return cached_area->map_.get();
  }

  bool IsIgnoringAllMutations(LocalStorageCachedArea* cached_area) {
    return cached_area->ignore_all_mutations_;
  }

  bool IsIgnoringKeyMutations(LocalStorageCachedArea* cached_area,
                              const base::string16& key) {
    return cached_area->ignore_key_mutations_.find(key) !=
           cached_area->ignore_key_mutations_.end();
  }

  void ResetAll(LocalStorageCachedArea* cached_area) {
    mock_leveldb_wrapper_.CompleteAllPendingCallbacks();
    mock_leveldb_wrapper_.ResetObservations();
    cached_area->Reset();
  }

  void ResetCacheOnly(LocalStorageCachedArea* cached_area) {
    cached_area->Reset();
  }

  static std::vector<uint8_t> String16ToUint8Vector(
      const base::string16& input) {
    return LocalStorageCachedArea::String16ToUint8Vector(
        input, LocalStorageCachedArea::FormatOption::kLocalStorageDetectFormat);
  }

  static base::string16 Uint8VectorToString16(
      const std::vector<uint8_t>& input) {
    return LocalStorageCachedArea::Uint8VectorToString16(
        input, LocalStorageCachedArea::FormatOption::kLocalStorageDetectFormat);
  }

 protected:
  TestBrowserThreadBundle test_browser_thread_bundle_;
  MockLevelDBWrapper mock_leveldb_wrapper_;
  std::unique_ptr<blink::scheduler::WebMainThreadScheduler> renderer_scheduler_;
  LocalStorageCachedAreas cached_areas_;
};

TEST_F(LocalStorageCachedAreaTest, Basics) {
  EXPECT_FALSE(mock_leveldb_wrapper_.HasBindings());
  scoped_refptr<LocalStorageCachedArea> cached_area =
      cached_areas_.GetCachedArea(kOrigin);
  EXPECT_EQ(kOrigin, cached_area->origin());
  EXPECT_TRUE(mock_leveldb_wrapper_.HasBindings());

  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));

  const std::string kStorageAreaId = "123";
  EXPECT_EQ(0u, cached_area->GetLength());
  EXPECT_TRUE(cached_area->SetItem(kKey, kValue, kPageUrl, kStorageAreaId));
  EXPECT_EQ(1u, cached_area->GetLength());
  EXPECT_EQ(kKey, cached_area->GetKey(0).string());
  EXPECT_EQ(kValue, cached_area->GetItem(kKey).string());
  cached_area->RemoveItem(kKey, kPageUrl, kStorageAreaId);
  EXPECT_EQ(0u, cached_area->GetLength());
}

TEST_F(LocalStorageCachedAreaTest, Getters) {
  scoped_refptr<LocalStorageCachedArea> cached_area =
      cached_areas_.GetCachedArea(kOrigin);

  // GetLength, we expect to see one call to load in the db.
  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));
  EXPECT_EQ(0u, cached_area->GetLength());
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_get_all());
  EXPECT_EQ(1u, mock_leveldb_wrapper_.pending_callbacks().size());
  EXPECT_TRUE(IsIgnoringAllMutations(cached_area.get()));
  mock_leveldb_wrapper_.CompleteAllPendingCallbacks();
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsIgnoringAllMutations(cached_area.get()));

  // GetKey, expect the one call to load.
  ResetAll(cached_area.get());
  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(cached_area->GetKey(2).is_null());
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_get_all());
  EXPECT_EQ(1u, mock_leveldb_wrapper_.pending_callbacks().size());

  // GetItem, ditto.
  ResetAll(cached_area.get());
  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(cached_area->GetItem(kKey).is_null());
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_get_all());
  EXPECT_EQ(1u, mock_leveldb_wrapper_.pending_callbacks().size());
}

TEST_F(LocalStorageCachedAreaTest, Setters) {
  scoped_refptr<LocalStorageCachedArea> cached_area =
      cached_areas_.GetCachedArea(kOrigin);

  // SetItem, we expect a call to load followed by a call to put in the db.
  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(cached_area->SetItem(kKey, kValue, kPageUrl, kStorageAreaId));
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_get_all());
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_put());
  EXPECT_EQ(kSource, mock_leveldb_wrapper_.observed_source());
  EXPECT_EQ(String16ToUint8Vector(kKey), mock_leveldb_wrapper_.observed_key());
  EXPECT_EQ(String16ToUint8Vector(kValue),
            mock_leveldb_wrapper_.observed_value());
  EXPECT_EQ(2u, mock_leveldb_wrapper_.pending_callbacks().size());

  // Clear, we expect a just the one call to clear in the db since
  // there's no need to load the data prior to deleting it.
  ResetAll(cached_area.get());
  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));
  cached_area->Clear(kPageUrl, kStorageAreaId);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_delete_all());
  EXPECT_EQ(kSource, mock_leveldb_wrapper_.observed_source());
  EXPECT_EQ(1u, mock_leveldb_wrapper_.pending_callbacks().size());

  // RemoveItem with nothing to remove, expect just one call to load.
  ResetAll(cached_area.get());
  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));
  cached_area->RemoveItem(kKey, kPageUrl, kStorageAreaId);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_get_all());
  EXPECT_FALSE(mock_leveldb_wrapper_.observed_delete());
  EXPECT_EQ(1u, mock_leveldb_wrapper_.pending_callbacks().size());

  // RemoveItem with something to remove, expect a call to load followed
  // by a call to remove.
  ResetAll(cached_area.get());
  mock_leveldb_wrapper_
      .mutable_get_all_return_values()[String16ToUint8Vector(kKey)] =
      String16ToUint8Vector(kValue);
  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));
  cached_area->RemoveItem(kKey, kPageUrl, kStorageAreaId);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_get_all());
  EXPECT_TRUE(mock_leveldb_wrapper_.observed_delete());
  EXPECT_EQ(kSource, mock_leveldb_wrapper_.observed_source());
  EXPECT_EQ(String16ToUint8Vector(kKey), mock_leveldb_wrapper_.observed_key());
  EXPECT_EQ(2u, mock_leveldb_wrapper_.pending_callbacks().size());
}

TEST_F(LocalStorageCachedAreaTest, MutationsAreIgnoredUntilLoadCompletion) {
  scoped_refptr<LocalStorageCachedArea> cached_area =
      cached_areas_.GetCachedArea(kOrigin);
  mojom::LevelDBObserver* observer = cached_area.get();

  EXPECT_TRUE(cached_area->GetItem(kKey).is_null());
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  EXPECT_TRUE(IsIgnoringAllMutations(cached_area.get()));

  // Before load completion, the mutation should be ignored.
  observer->KeyAdded(String16ToUint8Vector(kKey), String16ToUint8Vector(kValue),
                     kSource);
  EXPECT_TRUE(cached_area->GetItem(kKey).is_null());

  // Call the load completion callback.
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsIgnoringAllMutations(cached_area.get()));

  // Verify that mutations are now applied.
  observer->KeyAdded(String16ToUint8Vector(kKey), String16ToUint8Vector(kValue),
                     kSource);
  EXPECT_EQ(kValue, cached_area->GetItem(kKey).string());
}

TEST_F(LocalStorageCachedAreaTest, MutationsAreIgnoredUntilClearCompletion) {
  scoped_refptr<LocalStorageCachedArea> cached_area =
      cached_areas_.GetCachedArea(kOrigin);

  cached_area->Clear(kPageUrl, kStorageAreaId);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsIgnoringAllMutations(cached_area.get()));
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsIgnoringAllMutations(cached_area.get()));

  // Verify that calling Clear twice works as expected, the first
  // completion callback should have been cancelled.
  ResetCacheOnly(cached_area.get());
  cached_area->Clear(kPageUrl, kStorageAreaId);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsIgnoringAllMutations(cached_area.get()));
  cached_area->Clear(kPageUrl, kStorageAreaId);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsIgnoringAllMutations(cached_area.get()));
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsIgnoringAllMutations(cached_area.get()));
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsIgnoringAllMutations(cached_area.get()));
}

TEST_F(LocalStorageCachedAreaTest, KeyMutationsAreIgnoredUntilCompletion) {
  scoped_refptr<LocalStorageCachedArea> cached_area =
      cached_areas_.GetCachedArea(kOrigin);
  mojom::LevelDBObserver* observer = cached_area.get();

  // SetItem
  EXPECT_TRUE(cached_area->SetItem(kKey, kValue, kPageUrl, kStorageAreaId));
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);  // load completion
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsIgnoringAllMutations(cached_area.get()));
  EXPECT_TRUE(IsIgnoringKeyMutations(cached_area.get(), kKey));
  observer->KeyDeleted(String16ToUint8Vector(kKey), {0}, kSource);
  mock_leveldb_wrapper_.Flush();
  EXPECT_EQ(kValue, cached_area->GetItem(kKey).string());
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);  // set completion
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsIgnoringKeyMutations(cached_area.get(), kKey));

  // RemoveItem
  cached_area->RemoveItem(kKey, kPageUrl, kStorageAreaId);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsIgnoringKeyMutations(cached_area.get(), kKey));
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);  // remove completion
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsIgnoringKeyMutations(cached_area.get(), kKey));

  // Multiple mutations to the same key.
  EXPECT_TRUE(cached_area->SetItem(kKey, kValue, kPageUrl, kStorageAreaId));
  cached_area->RemoveItem(kKey, kPageUrl, kStorageAreaId);
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsIgnoringKeyMutations(cached_area.get(), kKey));
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);  // set completion
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsIgnoringKeyMutations(cached_area.get(), kKey));
  mock_leveldb_wrapper_.CompleteOnePendingCallback(true);  // remove completion
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsIgnoringKeyMutations(cached_area.get(), kKey));

  // A failed set item operation should Reset the cache.
  EXPECT_TRUE(cached_area->SetItem(kKey, kValue, kPageUrl, kStorageAreaId));
  mock_leveldb_wrapper_.Flush();
  EXPECT_TRUE(IsIgnoringKeyMutations(cached_area.get(), kKey));
  mock_leveldb_wrapper_.CompleteOnePendingCallback(false);
  mock_leveldb_wrapper_.Flush();
  EXPECT_FALSE(IsCacheLoaded(cached_area.get()));
}

TEST_F(LocalStorageCachedAreaTest, StringEncoding) {
  base::string16 ascii_key = base::ASCIIToUTF16("simplekey");
  base::string16 non_ascii_key = base::ASCIIToUTF16("key");
  non_ascii_key.push_back(0xd83d);
  non_ascii_key.push_back(0xde00);
  EXPECT_EQ(Uint8VectorToString16(String16ToUint8Vector(ascii_key)), ascii_key);
  EXPECT_EQ(Uint8VectorToString16(String16ToUint8Vector(non_ascii_key)),
            non_ascii_key);
  EXPECT_LT(String16ToUint8Vector(ascii_key).size(), ascii_key.size() * 2);
  EXPECT_GT(String16ToUint8Vector(non_ascii_key).size(),
            non_ascii_key.size() * 2);
}

TEST_F(LocalStorageCachedAreaTest, BrowserDisconnect) {
  scoped_refptr<LocalStorageCachedArea> cached_area =
      cached_areas_.GetCachedArea(kOrigin);

  // GetLength to prime the cache.
  mock_leveldb_wrapper_
      .mutable_get_all_return_values()[String16ToUint8Vector(kKey)] =
      String16ToUint8Vector(kValue);
  EXPECT_EQ(1u, cached_area->GetLength());
  EXPECT_TRUE(IsCacheLoaded(cached_area.get()));
  mock_leveldb_wrapper_.CompleteAllPendingCallbacks();
  mock_leveldb_wrapper_.ResetObservations();

  // Now disconnect the pipe from the browser, simulating situations where the
  // browser might be forced to destroy the LevelDBWrapperImpl.
  mock_leveldb_wrapper_.CloseAllBindings();

  // Getters should still function.
  EXPECT_EQ(1u, cached_area->GetLength());
  EXPECT_EQ(kValue, cached_area->GetItem(kKey).string());

  // And setters should also still function.
  cached_area->RemoveItem(kKey, kPageUrl, kStorageAreaId);
  EXPECT_EQ(0u, cached_area->GetLength());
  EXPECT_TRUE(cached_area->GetItem(kKey).is_null());

  // Even resetting the cache should still allow class to function properly.
  ResetCacheOnly(cached_area.get());
  EXPECT_TRUE(cached_area->GetItem(kKey).is_null());
  EXPECT_TRUE(cached_area->SetItem(kKey, kValue, kPageUrl, kStorageAreaId));
  EXPECT_EQ(1u, cached_area->GetLength());
  EXPECT_EQ(kValue, cached_area->GetItem(kKey).string());
}

}  // namespace content