Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: chrome/browser/net/quota_policy_channel_id_store_unittest.cc

Issue 1476263004: Remove ScopedVector from IDStores (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 11 matching lines...) Expand all
22 #include "net/test/cert_test_util.h" 22 #include "net/test/cert_test_util.h"
23 #include "net/test/channel_id_test_util.h" 23 #include "net/test/channel_id_test_util.h"
24 #include "sql/statement.h" 24 #include "sql/statement.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 const base::FilePath::CharType kTestChannelIDFilename[] = 27 const base::FilePath::CharType kTestChannelIDFilename[] =
28 FILE_PATH_LITERAL("ChannelID"); 28 FILE_PATH_LITERAL("ChannelID");
29 29
30 class QuotaPolicyChannelIDStoreTest : public testing::Test { 30 class QuotaPolicyChannelIDStoreTest : public testing::Test {
31 public: 31 public:
32 void Load(ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids) { 32 void Load(std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>>*
mmenke 2015/11/30 20:33:16 include <vector>
33 channel_ids) {
33 base::RunLoop run_loop; 34 base::RunLoop run_loop;
34 store_->Load(base::Bind(&QuotaPolicyChannelIDStoreTest::OnLoaded, 35 store_->Load(base::Bind(&QuotaPolicyChannelIDStoreTest::OnLoaded,
35 base::Unretained(this), 36 base::Unretained(this),
36 &run_loop)); 37 &run_loop));
37 run_loop.Run(); 38 run_loop.Run();
38 channel_ids->swap(channel_ids_); 39 channel_ids->swap(channel_ids_);
39 channel_ids_.clear(); 40 channel_ids_.clear();
40 } 41 }
41 42
42 void OnLoaded(base::RunLoop* run_loop, 43 void OnLoaded(
43 scoped_ptr<ScopedVector<net::DefaultChannelIDStore::ChannelID> > 44 base::RunLoop* run_loop,
44 channel_ids) { 45 scoped_ptr<std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>>>
46 channel_ids) {
45 channel_ids_.swap(*channel_ids); 47 channel_ids_.swap(*channel_ids);
46 run_loop->Quit(); 48 run_loop->Quit();
47 } 49 }
48 50
49 protected: 51 protected:
50 void SetUp() override { 52 void SetUp() override {
51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
52 store_ = new QuotaPolicyChannelIDStore( 54 store_ = new QuotaPolicyChannelIDStore(
53 temp_dir_.path().Append(kTestChannelIDFilename), 55 temp_dir_.path().Append(kTestChannelIDFilename),
54 base::ThreadTaskRunnerHandle::Get(), 56 base::ThreadTaskRunnerHandle::Get(),
55 NULL); 57 NULL);
56 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 58 std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>> channel_ids;
57 Load(&channel_ids); 59 Load(&channel_ids);
58 ASSERT_EQ(0u, channel_ids.size()); 60 ASSERT_EQ(0u, channel_ids.size());
59 } 61 }
60 62
61 void TearDown() override { 63 void TearDown() override {
62 store_ = NULL; 64 store_ = NULL;
63 loop_.RunUntilIdle(); 65 loop_.RunUntilIdle();
64 } 66 }
65 67
66 base::ScopedTempDir temp_dir_; 68 base::ScopedTempDir temp_dir_;
67 scoped_refptr<QuotaPolicyChannelIDStore> store_; 69 scoped_refptr<QuotaPolicyChannelIDStore> store_;
68 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids_; 70 std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>> channel_ids_;
69 base::MessageLoop loop_; 71 base::MessageLoop loop_;
70 }; 72 };
71 73
72 // Test if data is stored as expected in the QuotaPolicy database. 74 // Test if data is stored as expected in the QuotaPolicy database.
73 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { 75 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) {
74 scoped_ptr<crypto::ECPrivateKey> goog_key(crypto::ECPrivateKey::Create()); 76 scoped_ptr<crypto::ECPrivateKey> goog_key(crypto::ECPrivateKey::Create());
75 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); 77 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create());
76 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( 78 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
77 "google.com", base::Time::FromInternalValue(1), 79 "google.com", base::Time::FromInternalValue(1),
78 make_scoped_ptr(goog_key->Copy()))); 80 make_scoped_ptr(goog_key->Copy())));
79 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( 81 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
80 "foo.com", base::Time::FromInternalValue(3), 82 "foo.com", base::Time::FromInternalValue(3),
81 make_scoped_ptr(foo_key->Copy()))); 83 make_scoped_ptr(foo_key->Copy())));
82 84
83 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 85 std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>> channel_ids;
84 // Replace the store effectively destroying the current one and forcing it 86 // Replace the store effectively destroying the current one and forcing it
85 // to write its data to disk. Then we can see if after loading it again it 87 // to write its data to disk. Then we can see if after loading it again it
86 // is still there. 88 // is still there.
87 store_ = NULL; 89 store_ = NULL;
88 // Make sure we wait until the destructor has run. 90 // Make sure we wait until the destructor has run.
89 base::RunLoop().RunUntilIdle(); 91 base::RunLoop().RunUntilIdle();
90 store_ = new QuotaPolicyChannelIDStore( 92 store_ = new QuotaPolicyChannelIDStore(
91 temp_dir_.path().Append(kTestChannelIDFilename), 93 temp_dir_.path().Append(kTestChannelIDFilename),
92 base::ThreadTaskRunnerHandle::Get(), 94 base::ThreadTaskRunnerHandle::Get(),
93 NULL); 95 NULL);
94 96
95 // Reload and test for persistence 97 // Reload and test for persistence
96 Load(&channel_ids); 98 Load(&channel_ids);
97 ASSERT_EQ(2U, channel_ids.size()); 99 ASSERT_EQ(2U, channel_ids.size());
98 net::DefaultChannelIDStore::ChannelID* goog_channel_id; 100 scoped_ptr<net::DefaultChannelIDStore::ChannelID> goog_channel_id;
99 net::DefaultChannelIDStore::ChannelID* foo_channel_id; 101 scoped_ptr<net::DefaultChannelIDStore::ChannelID> foo_channel_id;
100 if (channel_ids[0]->server_identifier() == "google.com") { 102 if (channel_ids[0]->server_identifier() == "google.com") {
101 goog_channel_id = channel_ids[0]; 103 goog_channel_id = std::move(channel_ids[0]);
102 foo_channel_id = channel_ids[1]; 104 foo_channel_id = std::move(channel_ids[1]);
mmenke 2015/11/30 20:33:16 Any reason for passing ownership here? Doesn't se
103 } else { 105 } else {
104 goog_channel_id = channel_ids[1]; 106 goog_channel_id = std::move(channel_ids[1]);
105 foo_channel_id = channel_ids[0]; 107 foo_channel_id = std::move(channel_ids[0]);
106 } 108 }
107 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); 109 ASSERT_EQ("google.com", goog_channel_id->server_identifier());
108 EXPECT_TRUE(net::KeysEqual(goog_key.get(), goog_channel_id->key())); 110 EXPECT_TRUE(net::KeysEqual(goog_key.get(), goog_channel_id->key()));
109 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); 111 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue());
110 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); 112 ASSERT_EQ("foo.com", foo_channel_id->server_identifier());
111 EXPECT_TRUE(net::KeysEqual(foo_key.get(), foo_channel_id->key())); 113 EXPECT_TRUE(net::KeysEqual(foo_key.get(), foo_channel_id->key()));
112 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); 114 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue());
113 115
114 // Now delete the channel ID and check persistence again. 116 // Now delete the channel ID and check persistence again.
mmenke 2015/11/30 20:33:16 Remove extra space.
115 store_->DeleteChannelID(*channel_ids[0]); 117 store_->DeleteChannelID(*goog_channel_id);
116 store_->DeleteChannelID(*channel_ids[1]); 118 store_->DeleteChannelID(*foo_channel_id);
117 store_ = NULL; 119 store_ = NULL;
118 // Make sure we wait until the destructor has run. 120 // Make sure we wait until the destructor has run.
119 base::RunLoop().RunUntilIdle(); 121 base::RunLoop().RunUntilIdle();
120 channel_ids.clear(); 122 channel_ids.clear();
121 store_ = new QuotaPolicyChannelIDStore( 123 store_ = new QuotaPolicyChannelIDStore(
122 temp_dir_.path().Append(kTestChannelIDFilename), 124 temp_dir_.path().Append(kTestChannelIDFilename),
123 base::ThreadTaskRunnerHandle::Get(), 125 base::ThreadTaskRunnerHandle::Get(),
124 NULL); 126 NULL);
125 127
126 // Reload and check if the channel ID has been removed. 128 // Reload and check if the channel ID has been removed.
127 Load(&channel_ids); 129 Load(&channel_ids);
128 ASSERT_EQ(0U, channel_ids.size()); 130 ASSERT_EQ(0U, channel_ids.size());
129 } 131 }
130 132
131 // Test if data is stored as expected in the QuotaPolicy database. 133 // Test if data is stored as expected in the QuotaPolicy database.
132 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { 134 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) {
133 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( 135 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
134 "google.com", base::Time::FromInternalValue(1), 136 "google.com", base::Time::FromInternalValue(1),
135 make_scoped_ptr(crypto::ECPrivateKey::Create()))); 137 make_scoped_ptr(crypto::ECPrivateKey::Create())));
136 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( 138 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
137 "nonpersistent.com", base::Time::FromInternalValue(3), 139 "nonpersistent.com", base::Time::FromInternalValue(3),
138 make_scoped_ptr(crypto::ECPrivateKey::Create()))); 140 make_scoped_ptr(crypto::ECPrivateKey::Create())));
139 141
140 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 142 std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>> channel_ids;
141 // Replace the store effectively destroying the current one and forcing it 143 // Replace the store effectively destroying the current one and forcing it
142 // to write its data to disk. Then we can see if after loading it again it 144 // to write its data to disk. Then we can see if after loading it again it
143 // is still there. 145 // is still there.
144 store_ = NULL; 146 store_ = NULL;
145 // Make sure we wait until the destructor has run. 147 // Make sure we wait until the destructor has run.
146 base::RunLoop().RunUntilIdle(); 148 base::RunLoop().RunUntilIdle();
147 // Specify storage policy that makes "nonpersistent.com" session only. 149 // Specify storage policy that makes "nonpersistent.com" session only.
148 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = 150 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
149 new content::MockSpecialStoragePolicy(); 151 new content::MockSpecialStoragePolicy();
150 storage_policy->AddSessionOnly( 152 storage_policy->AddSessionOnly(
(...skipping 29 matching lines...) Expand all
180 base::ThreadTaskRunnerHandle::Get(), 182 base::ThreadTaskRunnerHandle::Get(),
181 NULL); 183 NULL);
182 184
183 // Reload and check that the nonpersistent.com channel IDs have been removed. 185 // Reload and check that the nonpersistent.com channel IDs have been removed.
184 Load(&channel_ids); 186 Load(&channel_ids);
185 ASSERT_EQ(2U, channel_ids.size()); 187 ASSERT_EQ(2U, channel_ids.size());
186 for (const auto& id : channel_ids) { 188 for (const auto& id : channel_ids) {
187 ASSERT_NE("nonpersistent.com", id->server_identifier()); 189 ASSERT_NE("nonpersistent.com", id->server_identifier());
188 } 190 }
189 } 191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698