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
|
// Copyright 2014 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 "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chrome/browser/net/quota_policy_channel_id_store.h"
#include "content/public/test/mock_special_storage_policy.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/test_data_directory.h"
#include "net/cookies/cookie_util.h"
#include "net/ssl/ssl_client_cert_type.h"
#include "net/test/cert_test_util.h"
#include "sql/statement.h"
#include "testing/gtest/include/gtest/gtest.h"
const base::FilePath::CharType kTestChannelIDFilename[] =
FILE_PATH_LITERAL("ChannelID");
class QuotaPolicyChannelIDStoreTest : public testing::Test {
public:
void Load(ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids) {
base::RunLoop run_loop;
store_->Load(base::Bind(&QuotaPolicyChannelIDStoreTest::OnLoaded,
base::Unretained(this),
&run_loop));
run_loop.Run();
channel_ids->swap(channel_ids_);
channel_ids_.clear();
}
void OnLoaded(base::RunLoop* run_loop,
scoped_ptr<ScopedVector<net::DefaultChannelIDStore::ChannelID> >
channel_ids) {
channel_ids_.swap(*channel_ids);
run_loop->Quit();
}
protected:
static base::Time GetTestCertExpirationTime() {
// Cert expiration time from 'dumpasn1 unittest.originbound.der':
// GeneralizedTime 19/11/2111 02:23:45 GMT
// base::Time::FromUTCExploded can't generate values past 2038 on 32-bit
// linux, so we use the raw value here.
return base::Time::FromInternalValue(GG_INT64_C(16121816625000000));
}
static base::Time GetTestCertCreationTime() {
// UTCTime 13/12/2011 02:23:45 GMT
base::Time::Exploded exploded_time;
exploded_time.year = 2011;
exploded_time.month = 12;
exploded_time.day_of_week = 0; // Unused.
exploded_time.day_of_month = 13;
exploded_time.hour = 2;
exploded_time.minute = 23;
exploded_time.second = 45;
exploded_time.millisecond = 0;
return base::Time::FromUTCExploded(exploded_time);
}
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
store_ = new QuotaPolicyChannelIDStore(
temp_dir_.path().Append(kTestChannelIDFilename),
base::ThreadTaskRunnerHandle::Get(),
NULL);
ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
Load(&channel_ids);
ASSERT_EQ(0u, channel_ids.size());
// Make sure the store gets written at least once.
store_->AddChannelID(
net::DefaultChannelIDStore::ChannelID("google.com",
base::Time::FromInternalValue(1),
base::Time::FromInternalValue(2),
"a",
"b"));
}
void TearDown() override {
store_ = NULL;
loop_.RunUntilIdle();
}
base::ScopedTempDir temp_dir_;
scoped_refptr<QuotaPolicyChannelIDStore> store_;
ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids_;
base::MessageLoop loop_;
};
// Test if data is stored as expected in the QuotaPolicy database.
TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) {
store_->AddChannelID(
net::DefaultChannelIDStore::ChannelID("foo.com",
base::Time::FromInternalValue(3),
base::Time::FromInternalValue(4),
"c",
"d"));
ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
// Replace the store effectively destroying the current one and forcing it
// to write its data to disk. Then we can see if after loading it again it
// is still there.
store_ = NULL;
// Make sure we wait until the destructor has run.
base::RunLoop().RunUntilIdle();
store_ = new QuotaPolicyChannelIDStore(
temp_dir_.path().Append(kTestChannelIDFilename),
base::MessageLoopProxy::current(),
NULL);
// Reload and test for persistence
Load(&channel_ids);
ASSERT_EQ(2U, channel_ids.size());
net::DefaultChannelIDStore::ChannelID* goog_channel_id;
net::DefaultChannelIDStore::ChannelID* foo_channel_id;
if (channel_ids[0]->server_identifier() == "google.com") {
goog_channel_id = channel_ids[0];
foo_channel_id = channel_ids[1];
} else {
goog_channel_id = channel_ids[1];
foo_channel_id = channel_ids[0];
}
ASSERT_EQ("google.com", goog_channel_id->server_identifier());
ASSERT_STREQ("a", goog_channel_id->private_key().c_str());
ASSERT_STREQ("b", goog_channel_id->cert().c_str());
ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue());
ASSERT_EQ(2, goog_channel_id->expiration_time().ToInternalValue());
ASSERT_EQ("foo.com", foo_channel_id->server_identifier());
ASSERT_STREQ("c", foo_channel_id->private_key().c_str());
ASSERT_STREQ("d", foo_channel_id->cert().c_str());
ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue());
ASSERT_EQ(4, foo_channel_id->expiration_time().ToInternalValue());
// Now delete the cert and check persistence again.
store_->DeleteChannelID(*channel_ids[0]);
store_->DeleteChannelID(*channel_ids[1]);
store_ = NULL;
// Make sure we wait until the destructor has run.
base::RunLoop().RunUntilIdle();
channel_ids.clear();
store_ = new QuotaPolicyChannelIDStore(
temp_dir_.path().Append(kTestChannelIDFilename),
base::MessageLoopProxy::current(),
NULL);
// Reload and check if the cert has been removed.
Load(&channel_ids);
ASSERT_EQ(0U, channel_ids.size());
}
// Test if data is stored as expected in the QuotaPolicy database.
TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) {
store_->AddChannelID(
net::DefaultChannelIDStore::ChannelID("foo.com",
base::Time::FromInternalValue(3),
base::Time::FromInternalValue(4),
"c",
"d"));
ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
// Replace the store effectively destroying the current one and forcing it
// to write its data to disk. Then we can see if after loading it again it
// is still there.
store_ = NULL;
// Make sure we wait until the destructor has run.
base::RunLoop().RunUntilIdle();
// Specify storage policy that makes "foo.com" session only.
scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
new content::MockSpecialStoragePolicy();
storage_policy->AddSessionOnly(
net::cookie_util::CookieOriginToURL("foo.com", true));
// Reload store, it should still have both channel ids.
store_ = new QuotaPolicyChannelIDStore(
temp_dir_.path().Append(kTestChannelIDFilename),
base::MessageLoopProxy::current(),
storage_policy);
Load(&channel_ids);
ASSERT_EQ(2U, channel_ids.size());
// Now close the store, and "foo.com" should be deleted according to policy.
store_ = NULL;
// Make sure we wait until the destructor has run.
base::RunLoop().RunUntilIdle();
channel_ids.clear();
store_ = new QuotaPolicyChannelIDStore(
temp_dir_.path().Append(kTestChannelIDFilename),
base::MessageLoopProxy::current(),
NULL);
// Reload and check that the "foo.com" cert has been removed.
Load(&channel_ids);
ASSERT_EQ(1U, channel_ids.size());
ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
}
|