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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/device_bound_sessions/session_inclusion_rules.h"
#include <initializer_list>
#include "base/strings/string_util.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/device_bound_sessions/proto/storage.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace net::device_bound_sessions {
namespace {
using Result = SessionInclusionRules::InclusionResult;
// These tests depend on the registry_controlled_domains code, so assert ahead
// of time that the eTLD+1 is what we expect, for clarity and to avoid confusing
// test failures.
void AssertDomainAndRegistry(const url::Origin& origin,
const std::string& expected_domain_and_registry) {
ASSERT_EQ(
registry_controlled_domains::GetDomainAndRegistry(
origin, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES),
expected_domain_and_registry)
<< "Unexpected domain and registry.";
}
struct EvaluateUrlTestCase {
const char* url;
Result expected_result;
};
void CheckEvaluateUrlTestCases(
const SessionInclusionRules& inclusion_rules,
std::initializer_list<EvaluateUrlTestCase> test_cases) {
for (const auto& test_case : test_cases) {
SCOPED_TRACE(test_case.url);
EXPECT_EQ(inclusion_rules.EvaluateRequestUrl(GURL(test_case.url)),
test_case.expected_result);
}
}
struct AddUrlRuleTestCase {
Result rule_type;
const char* host_pattern;
const char* path_prefix;
bool expected_is_added;
};
void CheckAddUrlRuleTestCases(
SessionInclusionRules& inclusion_rules,
std::initializer_list<AddUrlRuleTestCase> test_cases) {
for (const auto& test_case : test_cases) {
SCOPED_TRACE(base::JoinString(
{test_case.host_pattern, test_case.path_prefix}, ", "));
bool is_added = inclusion_rules.AddUrlRuleIfValid(
test_case.rule_type, test_case.host_pattern, test_case.path_prefix);
EXPECT_EQ(is_added, test_case.expected_is_added);
}
}
TEST(SessionInclusionRulesTest, DefaultConstructorMatchesNothing) {
SessionInclusionRules inclusion_rules;
EXPECT_FALSE(inclusion_rules.may_include_site_for_testing());
EXPECT_EQ(Result::kExclude,
inclusion_rules.EvaluateRequestUrl(GURL("https://origin.test")));
EXPECT_EQ(Result::kExclude, inclusion_rules.EvaluateRequestUrl(GURL()));
}
TEST(SessionInclusionRulesTest, DefaultIncludeOriginMayNotIncludeSite) {
url::Origin subdomain_origin =
url::Origin::Create(GURL("https://some.site.test"));
AssertDomainAndRegistry(subdomain_origin, "site.test");
SessionInclusionRules inclusion_rules{subdomain_origin};
EXPECT_FALSE(inclusion_rules.may_include_site_for_testing());
CheckEvaluateUrlTestCases(
inclusion_rules, {// URL not valid.
{"", Result::kExclude},
// Origins match.
{"https://some.site.test", Result::kInclude},
// Path is allowed.
{"https://some.site.test/path", Result::kInclude},
// Not same scheme.
{"http://some.site.test", Result::kExclude},
// Not same host (same-site subdomain).
{"https://some.other.site.test", Result::kExclude},
// Not same host (superdomain).
{"https://site.test", Result::kExclude},
// Unrelated site.
{"https://unrelated.test", Result::kExclude},
// Not same port.
{"https://some.site.test:8888", Result::kExclude}});
}
TEST(SessionInclusionRulesTest, DefaultIncludeOriginThoughMayIncludeSite) {
url::Origin root_site_origin = url::Origin::Create(GURL("https://site.test"));
AssertDomainAndRegistry(root_site_origin, "site.test");
SessionInclusionRules inclusion_rules{root_site_origin};
EXPECT_TRUE(inclusion_rules.may_include_site_for_testing());
// All expectations are as above. Even though including the site is allowed,
// because the origin's host is its root eTLD+1, it is still limited to a
// default origin inclusion_rules because it did not set include_site.
CheckEvaluateUrlTestCases(inclusion_rules,
{// URL not valid.
{"", Result::kExclude},
// Origins match.
{"https://site.test", Result::kInclude},
// Path is allowed.
{"https://site.test/path", Result::kInclude},
// Not same scheme.
{"http://site.test", Result::kExclude},
// Not same host (same-site subdomain).
{"https://other.site.test", Result::kExclude},
// Not same host (superdomain).
{"https://test", Result::kExclude},
// Unrelated site.
{"https://unrelated.test", Result::kExclude},
// Not same port.
{"https://site.test:8888", Result::kExclude}});
}
TEST(SessionInclusionRulesTest, IncludeSiteAttemptedButNotAllowed) {
url::Origin subdomain_origin =
url::Origin::Create(GURL("https://some.site.test"));
AssertDomainAndRegistry(subdomain_origin, "site.test");
SessionInclusionRules inclusion_rules{subdomain_origin};
EXPECT_FALSE(inclusion_rules.may_include_site_for_testing());
// Only the origin is included.
CheckEvaluateUrlTestCases(inclusion_rules,
{{"https://some.site.test", Result::kInclude},
{"https://other.site.test", Result::kExclude}});
// This shouldn't do anything.
inclusion_rules.SetIncludeSite(true);
EXPECT_FALSE(inclusion_rules.may_include_site_for_testing());
// Still only the origin is included.
CheckEvaluateUrlTestCases(inclusion_rules,
{{"https://some.site.test", Result::kInclude},
{"https://other.site.test", Result::kExclude}});
}
TEST(SessionInclusionRulesTest, IncludeSite) {
url::Origin root_site_origin = url::Origin::Create(GURL("https://site.test"));
AssertDomainAndRegistry(root_site_origin, "site.test");
SessionInclusionRules inclusion_rules{root_site_origin};
EXPECT_TRUE(inclusion_rules.may_include_site_for_testing());
inclusion_rules.SetIncludeSite(true);
CheckEvaluateUrlTestCases(
inclusion_rules, {// URL not valid.
{"", Result::kExclude},
// Origins match.
{"https://site.test", Result::kInclude},
// Path is allowed.
{"https://site.test/path", Result::kInclude},
// Not same scheme (site is schemeful).
{"http://site.test", Result::kExclude},
// Same-site subdomain is allowed.
{"https://some.site.test", Result::kInclude},
{"https://some.other.site.test", Result::kInclude},
// Not same host (superdomain).
{"https://test", Result::kExclude},
// Unrelated site.
{"https://unrelated.test", Result::kExclude},
// Other port is allowed because whole site is included.
{"https://site.test:8888", Result::kInclude}});
}
TEST(SessionInclusionRulesTest, AddUrlRuleToOriginOnly) {
url::Origin subdomain_origin =
url::Origin::Create(GURL("https://some.site.test"));
AssertDomainAndRegistry(subdomain_origin, "site.test");
SessionInclusionRules inclusion_rules{subdomain_origin};
EXPECT_FALSE(inclusion_rules.may_include_site_for_testing());
// Only the origin is allowed, since the setting origin is not the root
// eTLD+1. The only acceptable rules are limited to the origin/same host.
CheckAddUrlRuleTestCases(
inclusion_rules,
{// Host pattern equals origin's host. Path is valid.
{Result::kExclude, "some.site.test", "/static", true},
// Add an opposite rule to check later.
{Result::kInclude, "some.site.test", "/static/included", true},
// Path not valid.
{Result::kExclude, "some.site.test", "NotAPath", false},
// Other host patterns not accepted.
{Result::kExclude, "*.site.test", "/", false},
{Result::kExclude, "unrelated.test", "/", false},
{Result::kExclude, "site.test", "/", false},
{Result::kExclude, "other.site.test", "/", false},
{Result::kExclude, "https://some.site.test", "/", false},
{Result::kExclude, "some.site.test:443", "/", false}});
EXPECT_EQ(inclusion_rules.num_url_rules_for_testing(), 2u);
CheckEvaluateUrlTestCases(
inclusion_rules,
{// Matches the rule.
{"https://some.site.test/static", Result::kExclude},
// A path under the rule's path prefix is subject to the rule.
{"https://some.site.test/static/some/thing", Result::kExclude},
// These do not match the rule, so are subject to the basic rules (the
// origin).
{"https://some.site.test/staticcccccccc", Result::kInclude},
{"https://other.site.test/static", Result::kExclude},
// The more recently added rule wins out.
{"https://some.site.test/static/included", Result::kInclude}});
// Note that what matters is when the rule was added, not how specific the URL
// path prefix is. Let's add another rule now to show that.
EXPECT_TRUE(inclusion_rules.AddUrlRuleIfValid(Result::kExclude,
"some.site.test", "/"));
EXPECT_EQ(Result::kExclude, inclusion_rules.EvaluateRequestUrl(GURL(
"https://some.site.test/static/included")));
}
TEST(SessionInclusionRulesTest, AddUrlRuleToOriginThatMayIncludeSite) {
url::Origin root_site_origin = url::Origin::Create(GURL("https://site.test"));
AssertDomainAndRegistry(root_site_origin, "site.test");
SessionInclusionRules inclusion_rules{root_site_origin};
EXPECT_TRUE(inclusion_rules.may_include_site_for_testing());
// Without any rules yet, the basic rules is just the origin, because
// include_site was not set.
CheckEvaluateUrlTestCases(inclusion_rules,
{{"https://site.test/static", Result::kInclude},
{"https://other.site.test", Result::kExclude}});
// Since the origin's host is the root eTLD+1, it is allowed to set rules that
// affect URLs other than the setting origin (but still within the site).
CheckAddUrlRuleTestCases(inclusion_rules,
{{Result::kExclude, "excluded.site.test", "/", true},
{Result::kInclude, "included.site.test", "/", true},
{Result::kExclude, "site.test", "/static", true},
// Rules outside of the site are not allowed.
{Result::kExclude, "unrelated.test", "/", false}});
EXPECT_EQ(inclusion_rules.num_url_rules_for_testing(), 3u);
CheckEvaluateUrlTestCases(
inclusion_rules,
{// Path is excluded by rule.
{"https://site.test/static", Result::kExclude},
// Rule excludes URL explicitly.
{"https://excluded.site.test", Result::kExclude},
// Session is origin-scoped, so this rule is ignored.
{"https://included.site.test", Result::kExclude},
// Rule does not apply to wrong scheme.
{"http://included.site.test", Result::kExclude},
// No rules applies to these URLs, so the basic
// rules (origin) applies.
{"https://other.site.test", Result::kExclude},
{"https://site.test/stuff", Result::kInclude}});
}
TEST(SessionInclusionRulesTest, AddUrlRuleToRulesIncludingSite) {
url::Origin root_site_origin = url::Origin::Create(GURL("https://site.test"));
AssertDomainAndRegistry(root_site_origin, "site.test");
SessionInclusionRules inclusion_rules{root_site_origin};
EXPECT_TRUE(inclusion_rules.may_include_site_for_testing());
inclusion_rules.SetIncludeSite(true);
// Without any rules yet, the basic rules is the site.
CheckEvaluateUrlTestCases(inclusion_rules,
{{"https://site.test/static", Result::kInclude},
{"https://other.site.test", Result::kInclude}});
// Since the origin's host is the root eTLD+1, it is allowed to set rules that
// affect URLs other than the setting origin (but still within the site).
CheckAddUrlRuleTestCases(inclusion_rules,
{{Result::kExclude, "excluded.site.test", "/", true},
{Result::kInclude, "included.site.test", "/", true},
{Result::kExclude, "site.test", "/static", true},
// Rules outside of the site are not allowed.
{Result::kExclude, "unrelated.test", "/", false}});
EXPECT_EQ(inclusion_rules.num_url_rules_for_testing(), 3u);
CheckEvaluateUrlTestCases(
inclusion_rules,
{// Path is excluded by rule.
{"https://site.test/static", Result::kExclude},
// Rule excludes URL explicitly.
{"https://excluded.site.test", Result::kExclude},
// Rule includes URL explicitly.
{"https://included.site.test", Result::kInclude},
// Rule does not apply to wrong scheme.
{"http://included.site.test", Result::kExclude},
// No rule applies to these URLs, so the basic rules (site) applies.
{"https://other.site.test", Result::kInclude},
{"https://site.test/stuff", Result::kInclude}});
// "include_site" takes priority over session inclusion rules, so when
// it is revoked, cross-origin rules are now excluded.
inclusion_rules.SetIncludeSite(false);
CheckEvaluateUrlTestCases(
inclusion_rules,
{// Path is excluded by rule.
{"https://site.test/static", Result::kExclude},
// Rule excludes URL explicitly.
{"https://excluded.site.test", Result::kExclude},
// Rule includes URL explicitly, but is cross-origin.
{"https://included.site.test", Result::kExclude},
// No rules applies to these URLs, so the basic
// rules (which is now the origin) applies.
{"https://other.site.test", Result::kExclude},
{"https://site.test/stuff", Result::kInclude}});
}
TEST(SessionInclusionRulesTest, UrlRuleParsing) {
url::Origin root_site_origin = url::Origin::Create(GURL("https://site.test"));
AssertDomainAndRegistry(root_site_origin, "site.test");
// Use the most permissive type of inclusion_rules, to hit the interesting
// edge cases.
SessionInclusionRules inclusion_rules{root_site_origin};
EXPECT_TRUE(inclusion_rules.may_include_site_for_testing());
CheckAddUrlRuleTestCases(
inclusion_rules,
{// Empty host pattern not permitted.
{Result::kExclude, "", "/", false},
// Host pattern that is only whitespace is not permitted.
{Result::kExclude, " ", "/", false},
// Forbidden characters in host_pattern.
{Result::kExclude, "https://site.test", "/", false},
{Result::kExclude, "site.test:8888", "/", false},
{Result::kExclude, "site.test,other.test", "/", false},
// Non-IPv6-allowable characters within the brackets.
{Result::kExclude, "[*.:abcd::3:4:ff]", "/", false},
{Result::kExclude, "[1:ab+cd::3:4:ff]", "/", false},
{Result::kExclude, "[[1:abcd::3:4:ff]]", "/", false},
// Internal wildcard characters are forbidden in the host pattern.
{Result::kExclude, "sub.*.site.test", "/", false},
// Multiple wildcard characters are forbidden in the host pattern.
{Result::kExclude, "*.sub.*.site.test", "/", false},
// Wildcard must be followed by a dot.
{Result::kExclude, "*site.test", "/", false},
// Wildcard must be followed by a non-eTLD.
{Result::kExclude, "*.com", "/", false},
// Other sites are not allowed.
{Result::kExclude, "unrelated.site", "/", false},
// Other hosts with no registrable domain are not allowed.
{Result::kExclude, "4.31.198.44", "/", false},
{Result::kExclude, "[1:abcd::3:4:ff]", "/", false},
{Result::kExclude, "co.uk", "/", false},
{Result::kExclude, "com", "/", false}});
}
TEST(SessionInclusionRulesTest, UrlRuleParsingTopLevelDomain) {
url::Origin tld_origin = url::Origin::Create(GURL("https://com"));
AssertDomainAndRegistry(tld_origin, "");
SessionInclusionRules inclusion_rules{tld_origin};
EXPECT_FALSE(inclusion_rules.may_include_site_for_testing());
CheckAddUrlRuleTestCases(
inclusion_rules,
{// Exact host is allowed.
{Result::kExclude, "com", "/", true},
// Wildcards are not permitted.
{Result::kExclude, "*.com", "/", false},
// Other hosts with no registrable domain are not allowed.
{Result::kExclude, "4.31.198.44", "/", false},
{Result::kExclude, "[1:abcd::3:4:ff]", "/", false},
{Result::kExclude, "co.uk", "/", false}});
}
TEST(SessionInclusionRulesTest, UrlRuleParsingIPv4Address) {
url::Origin ip_origin = url::Origin::Create(GURL("https://4.31.198.44"));
AssertDomainAndRegistry(ip_origin, "");
SessionInclusionRules inclusion_rules{ip_origin};
EXPECT_FALSE(inclusion_rules.may_include_site_for_testing());
CheckAddUrlRuleTestCases(
inclusion_rules,
{// Exact host is allowed.
{Result::kExclude, "4.31.198.44", "/", true},
// Wildcards are not permitted.
{Result::kExclude, "*.31.198.44", "/", false},
{Result::kExclude, "*.4.31.198.44", "/", false},
// Other hosts with no registrable domain are not allowed.
{Result::kExclude, "[1:abcd::3:4:ff]", "/", false},
{Result::kExclude, "co.uk", "/", false},
{Result::kExclude, "com", "/", false}});
}
TEST(SessionInclusionRulesTest, UrlRuleParsingIPv6Address) {
url::Origin ipv6_origin =
url::Origin::Create(GURL("https://[1:abcd::3:4:ff]"));
AssertDomainAndRegistry(ipv6_origin, "");
SessionInclusionRules inclusion_rules{ipv6_origin};
EXPECT_FALSE(inclusion_rules.may_include_site_for_testing());
CheckAddUrlRuleTestCases(
inclusion_rules,
{// Exact host is allowed.
{Result::kExclude, "[1:abcd::3:4:ff]", "/", true},
// Wildcards are not permitted.
{Result::kExclude, "*.[1:abcd::3:4:ff]", "/", false},
// Brackets mismatched.
{Result::kExclude, "[1:abcd::3:4:ff", "/", false},
{Result::kExclude, "1:abcd::3:4:ff]", "/", false},
// Non-IPv6-allowable characters within the brackets.
{Result::kExclude, "[*.:abcd::3:4:ff]", "/", false},
{Result::kExclude, "[1:ab+cd::3:4:ff]", "/", false},
{Result::kExclude, "[[1:abcd::3:4:ff]]", "/", false},
// Other hosts with no registrable domain are not allowed.
{Result::kExclude, "4.31.198.44", "/", false},
{Result::kExclude, "co.uk", "/", false},
{Result::kExclude, "com", "/", false}});
}
// This test is more to document the current behavior than anything else. We may
// discover a need for more comprehensive support for port numbers in the
// future, in which case:
// TODO(chlily): Support port numbers in URL rules.
TEST(SessionInclusionRulesTest, NonstandardPort) {
url::Origin nonstandard_port_origin =
url::Origin::Create(GURL("https://site.test:8888"));
AssertDomainAndRegistry(nonstandard_port_origin, "site.test");
SessionInclusionRules inclusion_rules{nonstandard_port_origin};
EXPECT_TRUE(inclusion_rules.may_include_site_for_testing());
// Without any URL rules, the default origin rule allows only the same origin.
CheckEvaluateUrlTestCases(inclusion_rules,
{{"https://site.test", Result::kExclude},
{"https://site.test:8888", Result::kInclude},
{"https://other.site.test", Result::kExclude}});
// If we include_site, then same-site URLs regardless of port number are
// included.
inclusion_rules.SetIncludeSite(true);
CheckEvaluateUrlTestCases(inclusion_rules,
{{"https://site.test", Result::kInclude},
{"https://site.test:8888", Result::kInclude},
{"https://site.test:1234", Result::kInclude},
{"https://other.site.test", Result::kInclude}});
// However, adding URL rules to an inclusion_rules based on such an origin may
// lead to unintuitive outcomes. It is not possible to specify a rule that
// applies to the same origin as the setting origin if the setting origin has
// a nonstandard port.
CheckAddUrlRuleTestCases(
inclusion_rules,
{// The pattern is rejected due to the colon, despite being the
// same origin.
{Result::kExclude, "site.test:8888", "/", false},
// A rule with the same host without port specified is accepted.
// This rule applies to any URL with the specified host.
{Result::kExclude, "site.test", "/", true},
// Any explicitly specified port is rejected (due to the colon),
// even if it's the standard one.
{Result::kExclude, "site.test:443", "/", false}});
EXPECT_EQ(inclusion_rules.num_url_rules_for_testing(), 1u);
CheckEvaluateUrlTestCases(
inclusion_rules,
{// This is same-origin but gets caught in the "site.test" rule because
// the rule didn't specify a port.
{"https://site.test:8888", Result::kExclude},
// This is same-site but gets caught in the "site.test" rule because
// the rule didn't specify a port.
{"https://site.test:1234", Result::kExclude},
// Same-site is included by basic rules.
{"https://other.site.test", Result::kInclude},
// Also excluded explicitly by rule.
{"https://site.test", Result::kExclude},
{"https://site.test:443", Result::kExclude}});
}
TEST(SessionInclusionRulesTest, ToFromProto) {
// Create a valid SessionInclusionRules object with default inclusion rule and
// a couple of additional URL rules.
url::Origin root_site_origin = url::Origin::Create(GURL("https://site.test"));
AssertDomainAndRegistry(root_site_origin, "site.test");
SessionInclusionRules inclusion_rules{root_site_origin};
EXPECT_TRUE(inclusion_rules.may_include_site_for_testing());
inclusion_rules.SetIncludeSite(true);
EXPECT_TRUE(inclusion_rules.AddUrlRuleIfValid(Result::kExclude,
"excluded.site.test", "/"));
EXPECT_TRUE(inclusion_rules.AddUrlRuleIfValid(Result::kInclude,
"included.site.test", "/"));
// Create a corresponding proto object and validate.
proto::SessionInclusionRules proto = inclusion_rules.ToProto();
EXPECT_EQ(root_site_origin.Serialize(), proto.origin());
EXPECT_TRUE(proto.do_include_site());
ASSERT_EQ(proto.url_rules().size(), 2);
{
const auto& rule = proto.url_rules(0);
EXPECT_EQ(rule.rule_type(), proto::RuleType::EXCLUDE);
EXPECT_EQ(rule.host_matcher_rule(), "excluded.site.test");
EXPECT_EQ(rule.path_prefix(), "/");
}
{
const auto& rule = proto.url_rules(1);
EXPECT_EQ(rule.rule_type(), proto::RuleType::INCLUDE);
EXPECT_EQ(rule.host_matcher_rule(), "included.site.test");
EXPECT_EQ(rule.path_prefix(), "/");
}
// Create a SessionInclusionRules object from the proto and verify
// that it is the same as the original.
std::unique_ptr<SessionInclusionRules> restored_inclusion_rules =
SessionInclusionRules::CreateFromProto(proto);
ASSERT_TRUE(restored_inclusion_rules != nullptr);
EXPECT_EQ(*restored_inclusion_rules, inclusion_rules);
}
TEST(SessionInclusionRulesTest, FailCreateFromInvalidProto) {
// Empty proto.
{
proto::SessionInclusionRules proto;
EXPECT_FALSE(SessionInclusionRules::CreateFromProto(proto));
}
// Opaque origin.
{
proto::SessionInclusionRules proto;
proto.set_origin("about:blank");
proto.set_do_include_site(false);
EXPECT_FALSE(SessionInclusionRules::CreateFromProto(proto));
}
// Create a fully populated proto.
url::Origin root_site_origin = url::Origin::Create(GURL("https://site.test"));
SessionInclusionRules inclusion_rules{root_site_origin};
inclusion_rules.SetIncludeSite(true);
inclusion_rules.AddUrlRuleIfValid(Result::kExclude, "excluded.site.test",
"/");
inclusion_rules.AddUrlRuleIfValid(Result::kInclude, "included.site.test",
"/");
proto::SessionInclusionRules proto = inclusion_rules.ToProto();
// Test for missing proto fields by clearing the fields one at a time.
{
proto::SessionInclusionRules p(proto);
p.clear_origin();
EXPECT_FALSE(SessionInclusionRules::CreateFromProto(p));
}
{
proto::SessionInclusionRules p(proto);
p.clear_do_include_site();
EXPECT_FALSE(SessionInclusionRules::CreateFromProto(p));
}
// URL rules with missing parameters.
{
proto::SessionInclusionRules p(proto);
p.mutable_url_rules(0)->clear_rule_type();
EXPECT_FALSE(SessionInclusionRules::CreateFromProto(p));
}
{
proto::SessionInclusionRules p(proto);
p.mutable_url_rules(0)->clear_host_matcher_rule();
EXPECT_FALSE(SessionInclusionRules::CreateFromProto(p));
}
{
proto::SessionInclusionRules p(proto);
p.mutable_url_rules(0)->clear_path_prefix();
EXPECT_FALSE(SessionInclusionRules::CreateFromProto(p));
}
}
} // namespace
} // namespace net::device_bound_sessions
|