1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stddef.h>
6 #include <stdint.h>
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
13 #include "third_party/blink/public/platform/web_blob_info.h"
14 #include "third_party/blink/public/web/web_heap.h"
15 #include "third_party/blink/renderer/modules/indexeddb/idb_key_range.h"
16 #include "third_party/blink/renderer/modules/indexeddb/mock_web_idb_callbacks.h"
17 #include "third_party/blink/renderer/modules/indexeddb/web_idb_transaction_impl.h"
18 
19 using testing::_;
20 using testing::Invoke;
21 using testing::StrictMock;
22 using testing::WithArgs;
23 
24 namespace blink {
25 
26 class WebIDBTransactionImplTest : public testing::Test {};
27 
TEST_F(WebIDBTransactionImplTest,ValueSizeTest)28 TEST_F(WebIDBTransactionImplTest, ValueSizeTest) {
29   // For testing use a much smaller maximum size to prevent allocating >100 MB
30   // of memory, which crashes on memory-constrained systems.
31   const size_t kMaxValueSizeForTesting = 10 * 1024 * 1024;  // 10 MB
32 
33   const Vector<char> data(kMaxValueSizeForTesting + 1);
34   const scoped_refptr<SharedBuffer> value_data =
35       SharedBuffer::Create(&data.front(), data.size());
36   const Vector<WebBlobInfo> blob_info;
37   auto value = std::make_unique<IDBValue>(value_data, blob_info);
38   std::unique_ptr<IDBKey> key = IDBKey::CreateNumber(0);
39   const int64_t transaction_id = 1;
40   const int64_t object_store_id = 2;
41   std::unique_ptr<StrictMock<MockWebIDBCallbacks>> callbacks =
42       std::make_unique<StrictMock<MockWebIDBCallbacks>>();
43 
44   ASSERT_GT(value_data->size() + key->SizeEstimate(), kMaxValueSizeForTesting);
45   ThreadState::Current()->CollectAllGarbageForTesting();
46   EXPECT_CALL(*callbacks, Error(_, _)).Times(1);
47 
48   WebIDBTransactionImpl transaction_impl(
49       blink::scheduler::GetSingleThreadTaskRunnerForTesting(), transaction_id);
50   transaction_impl.max_put_value_size_ = kMaxValueSizeForTesting;
51   transaction_impl.Put(object_store_id, std::move(value), std::move(key),
52                        mojom::IDBPutMode::AddOrUpdate, std::move(callbacks),
53                        Vector<IDBIndexKeys>());
54 }
55 
TEST_F(WebIDBTransactionImplTest,KeyAndValueSizeTest)56 TEST_F(WebIDBTransactionImplTest, KeyAndValueSizeTest) {
57   // For testing use a much smaller maximum size to prevent allocating >100 MB
58   // of memory, which crashes on memory-constrained systems.
59   const size_t kMaxValueSizeForTesting = 10 * 1024 * 1024;  // 10 MB
60   const size_t kKeySize = 1024 * 1024;
61 
62   const Vector<char> data(kMaxValueSizeForTesting - kKeySize);
63   const scoped_refptr<SharedBuffer> value_data =
64       SharedBuffer::Create(&data.front(), data.size());
65   const Vector<WebBlobInfo> blob_info;
66   auto value = std::make_unique<IDBValue>(value_data, blob_info);
67   const int64_t transaction_id = 1;
68   const int64_t object_store_id = 2;
69   std::unique_ptr<StrictMock<MockWebIDBCallbacks>> callbacks =
70       std::make_unique<StrictMock<MockWebIDBCallbacks>>();
71 
72   // For this test, we want IDBKey::SizeEstimate() minus kKeySize to be the
73   // smallest value > 0.  An IDBKey with a string has a size_estimate_ equal to
74   // kOverheadSize (~16) + (string.length * sizeof(UChar)).  Create
75   // |kKeySize / sizeof(UChar)| characters in String.
76   const unsigned int number_of_chars = kKeySize / sizeof(UChar);
77   Vector<UChar> key_string_vector;
78   key_string_vector.ReserveInitialCapacity(number_of_chars);
79   key_string_vector.Fill(u'0', number_of_chars);
80   String key_string(key_string_vector);
81   DCHECK_EQ(key_string.length(), number_of_chars);
82 
83   std::unique_ptr<IDBKey> key = IDBKey::CreateString(key_string);
84   DCHECK_EQ(value_data->size(), kMaxValueSizeForTesting - kKeySize);
85   DCHECK_GT(key->SizeEstimate() - kKeySize, static_cast<size_t>(0));
86   DCHECK_GT(value_data->size() + key->SizeEstimate(), kMaxValueSizeForTesting);
87 
88   ThreadState::Current()->CollectAllGarbageForTesting();
89   EXPECT_CALL(*callbacks, Error(_, _)).Times(1);
90 
91   WebIDBTransactionImpl transaction_impl(
92       blink::scheduler::GetSingleThreadTaskRunnerForTesting(), transaction_id);
93   transaction_impl.max_put_value_size_ = kMaxValueSizeForTesting;
94   transaction_impl.Put(object_store_id, std::move(value), std::move(key),
95                        mojom::IDBPutMode::AddOrUpdate, std::move(callbacks),
96                        Vector<IDBIndexKeys>());
97 }
98 
99 }  // namespace blink
100