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 338 339 340 341 342 343 344
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/k_anonymity_service/remote_trust_token_query_answerer.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_task_environment.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/trust_tokens.mojom.h"
#include "services/network/test/test_network_context.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/origin.h"
namespace {
constexpr char kMainFrameOrigin[] = "https://www.example.com/";
// A fake TrustTokenQueryAnswerer that replies with canned answers and has the
// instrumentation to support a mojo disconnect.
class FakeTrustTokenQueryAnswerer
: public network::mojom::TrustTokenQueryAnswerer {
public:
// Implementation of network::mojom::TrustTokenQueryAnswerer
void HasTrustTokens(const url::Origin& issuer,
HasTrustTokensCallback callback) override {
if (!callbacks_enabled_) {
// Grab the callback and hold it. The callback will never be invoked and
// is just held until the pipe is closed since Mojo DCHECKs if they're
// deleted earlier.
has_tokens_callback_ = std::move(callback);
return;
}
std::move(callback).Run(network::mojom::HasTrustTokensResult::New(
network::mojom::TrustTokenOperationStatus::kOk,
/*has_trust_tokens=*/false));
}
void HasRedemptionRecord(const url::Origin& issuer,
HasRedemptionRecordCallback callback) override {
if (!callbacks_enabled_) {
// Grab the callback and hold it. The callback will never be invoked and
// is just held until the pipe is closed since Mojo DCHECKs if they're
// deleted earlier.
has_redemption_record_callback_ = std::move(callback);
return;
}
std::move(callback).Run(network::mojom::HasRedemptionRecordResult::New(
network::mojom::TrustTokenOperationStatus::kOk,
/*has_redemption_record=*/false));
}
void DisableTrustTokenQueryCallbacks() { callbacks_enabled_ = false; }
protected:
bool callbacks_enabled_ = true;
HasTrustTokensCallback has_tokens_callback_;
HasRedemptionRecordCallback has_redemption_record_callback_;
};
class FakeNetworkContext : public network::TestNetworkContext {
public:
FakeNetworkContext()
: answerer_(std::make_unique<FakeTrustTokenQueryAnswerer>()),
self_(this) {}
// Creates a new mojo receiver to replace the existing one. Should only be
// called when the current receiver is disconnected.
void GetTrustTokenQueryAnswerer(
mojo::PendingReceiver<network::mojom::TrustTokenQueryAnswerer> receiver,
const url::Origin& top_frame_origin) override {
answerer_receiver_ = std::make_unique<
mojo::Receiver<network::mojom::TrustTokenQueryAnswerer>>(
answerer_.get(), std::move(receiver));
}
mojo::PendingRemote<network::mojom::NetworkContext> CreatePendingRemote() {
return self_.BindNewPipeAndPassRemote();
}
void DisconnectTrustTokenQueryAnswerer() {
answerer_receiver_->reset();
ASSERT_FALSE(answerer_receiver_->is_bound());
answerer_ = std::make_unique<FakeTrustTokenQueryAnswerer>();
}
void DisableTrustTokenQueryCallbacks() {
answerer_->DisableTrustTokenQueryCallbacks();
}
private:
std::unique_ptr<FakeTrustTokenQueryAnswerer> answerer_;
mojo::Receiver<network::mojom::NetworkContext> self_;
std::unique_ptr<mojo::Receiver<network::mojom::TrustTokenQueryAnswerer>>
answerer_receiver_;
};
class RemoteTrustTokenQueryAnswererTest : public testing::Test {
public:
RemoteTrustTokenQueryAnswererTest()
: issuer_(url::Origin::Create(GURL(kMainFrameOrigin))) {
DCHECK(network::IsOriginPotentiallyTrustworthy(issuer_));
DCHECK(issuer_.scheme() == url::kHttpsScheme);
profile_.GetDefaultStoragePartition()->SetNetworkContextForTesting(
context_.CreatePendingRemote());
}
std::unique_ptr<RemoteTrustTokenQueryAnswerer> CreateAnswerer() {
return std::make_unique<RemoteTrustTokenQueryAnswerer>(issuer_, &profile_);
}
network::mojom::HasTrustTokensResultPtr CallHasTrustTokensAndWait(
RemoteTrustTokenQueryAnswerer* answerer) {
network::mojom::HasTrustTokensResultPtr has_tokens;
base::RunLoop run_loop;
answerer->HasTrustTokens(
issuer_, base::BindLambdaForTesting(
[&run_loop, &has_tokens](
network::mojom::HasTrustTokensResultPtr result) {
has_tokens = std::move(result);
run_loop.Quit();
}));
run_loop.Run();
return has_tokens;
}
network::mojom::HasRedemptionRecordResultPtr CallHasRedemptionRecordAndWait(
RemoteTrustTokenQueryAnswerer* answerer) {
network::mojom::HasRedemptionRecordResultPtr has_redemption_record;
base::RunLoop run_loop;
answerer->HasRedemptionRecord(
issuer_, base::BindLambdaForTesting(
[&run_loop, &has_redemption_record](
network::mojom::HasRedemptionRecordResultPtr result) {
has_redemption_record = std::move(result);
run_loop.Quit();
}));
run_loop.Run();
return has_redemption_record;
}
void DisconnectTrustTokenQueryAnswerer() {
context_.DisconnectTrustTokenQueryAnswerer();
}
void DisableTrustTokenQueryCallbacks() {
context_.DisableTrustTokenQueryCallbacks();
}
const url::Origin& issuer() const { return issuer_; }
private:
content::BrowserTaskEnvironment task_environment_;
url::Origin issuer_;
FakeNetworkContext context_;
TestingProfile profile_;
};
TEST_F(RemoteTrustTokenQueryAnswererTest, SurvivesDisconnectBetweenCalls) {
TestingProfile profile;
auto answerer = CreateAnswerer();
auto has_tokens_result = CallHasTrustTokensAndWait(answerer.get());
auto has_redemption_record_result =
CallHasRedemptionRecordAndWait(answerer.get());
ASSERT_TRUE(has_tokens_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_tokens_result->status);
EXPECT_FALSE(has_tokens_result->has_trust_tokens);
ASSERT_TRUE(has_redemption_record_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_redemption_record_result->status);
EXPECT_FALSE(has_redemption_record_result->has_redemption_record);
// Disconnect
DisconnectTrustTokenQueryAnswerer();
// |answerer| will receive an error notification, but it's not
// guaranteed to have arrived at this point. Flush the remote to make sure
// the notification has been received.
answerer->FlushForTesting();
has_tokens_result = CallHasTrustTokensAndWait(answerer.get());
has_redemption_record_result = CallHasRedemptionRecordAndWait(answerer.get());
ASSERT_TRUE(has_tokens_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_tokens_result->status);
EXPECT_FALSE(has_tokens_result->has_trust_tokens);
ASSERT_TRUE(has_redemption_record_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_redemption_record_result->status);
EXPECT_FALSE(has_redemption_record_result->has_redemption_record);
}
TEST_F(RemoteTrustTokenQueryAnswererTest, SurvivesDisconnectInHasTrustTokens) {
TestingProfile profile;
auto answerer = CreateAnswerer();
auto has_tokens_result = CallHasTrustTokensAndWait(answerer.get());
ASSERT_TRUE(has_tokens_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_tokens_result->status);
EXPECT_FALSE(has_tokens_result->has_trust_tokens);
{
base::RunLoop run_loop;
DisableTrustTokenQueryCallbacks();
answerer->HasTrustTokens(
issuer(), base::BindLambdaForTesting(
[&run_loop, &has_tokens_result](
network::mojom::HasTrustTokensResultPtr result) {
has_tokens_result = std::move(result);
run_loop.Quit();
}));
DisconnectTrustTokenQueryAnswerer();
run_loop.Run();
EXPECT_FALSE(has_tokens_result);
}
has_tokens_result = CallHasTrustTokensAndWait(answerer.get());
ASSERT_TRUE(has_tokens_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_tokens_result->status);
EXPECT_FALSE(has_tokens_result->has_trust_tokens);
}
TEST_F(RemoteTrustTokenQueryAnswererTest,
SurvivesDisconnectInHasRedemptionRecord) {
TestingProfile profile;
auto answerer = CreateAnswerer();
auto has_redemption_record_result =
CallHasRedemptionRecordAndWait(answerer.get());
ASSERT_TRUE(has_redemption_record_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_redemption_record_result->status);
EXPECT_FALSE(has_redemption_record_result->has_redemption_record);
{
base::RunLoop run_loop;
DisableTrustTokenQueryCallbacks();
answerer->HasRedemptionRecord(
issuer(), base::BindLambdaForTesting(
[&run_loop, &has_redemption_record_result](
network::mojom::HasRedemptionRecordResultPtr result) {
has_redemption_record_result = std::move(result);
run_loop.Quit();
}));
DisconnectTrustTokenQueryAnswerer();
run_loop.Run();
EXPECT_FALSE(has_redemption_record_result);
}
has_redemption_record_result = CallHasRedemptionRecordAndWait(answerer.get());
ASSERT_TRUE(has_redemption_record_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_redemption_record_result->status);
EXPECT_FALSE(has_redemption_record_result->has_redemption_record);
}
TEST_F(RemoteTrustTokenQueryAnswererTest,
HasTrustTokensHandlesSynchronousCall) {
TestingProfile profile;
auto answerer = CreateAnswerer();
auto has_tokens_result = CallHasTrustTokensAndWait(answerer.get());
ASSERT_TRUE(has_tokens_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_tokens_result->status);
EXPECT_FALSE(has_tokens_result->has_trust_tokens);
{
base::RunLoop run_loop;
answerer->HasTrustTokens(
issuer(),
base::BindLambdaForTesting(
[&](network::mojom::HasTrustTokensResultPtr result) {
ASSERT_TRUE(result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
result->status);
EXPECT_FALSE(result->has_trust_tokens);
answerer->HasTrustTokens(
issuer(),
base::BindLambdaForTesting(
[&](network::mojom::HasTrustTokensResultPtr result) {
ASSERT_TRUE(result);
EXPECT_EQ(
network::mojom::TrustTokenOperationStatus::kOk,
result->status);
EXPECT_FALSE(result->has_trust_tokens);
run_loop.Quit();
}));
}));
run_loop.Run();
}
}
TEST_F(RemoteTrustTokenQueryAnswererTest,
HasRedemptionRecordHandlesSynchronousCall) {
TestingProfile profile;
auto answerer = CreateAnswerer();
auto has_tokens_result = CallHasRedemptionRecordAndWait(answerer.get());
ASSERT_TRUE(has_tokens_result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
has_tokens_result->status);
EXPECT_FALSE(has_tokens_result->has_redemption_record);
{
base::RunLoop run_loop;
answerer->HasRedemptionRecord(
issuer(),
base::BindLambdaForTesting(
[&](network::mojom::HasRedemptionRecordResultPtr result) {
ASSERT_TRUE(result);
EXPECT_EQ(network::mojom::TrustTokenOperationStatus::kOk,
result->status);
EXPECT_FALSE(result->has_redemption_record);
answerer->HasRedemptionRecord(
issuer(),
base::BindLambdaForTesting(
[&](network::mojom::HasRedemptionRecordResultPtr result) {
ASSERT_TRUE(result);
EXPECT_EQ(
network::mojom::TrustTokenOperationStatus::kOk,
result->status);
EXPECT_FALSE(result->has_redemption_record);
run_loop.Quit();
}));
}));
run_loop.Run();
}
}
} // namespace
|