1 // Copyright 2023 The Chromium Authors
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 "net/spdy/spdy_session_key.h"
6
7 #include "net/base/network_anonymization_key.h"
8 #include "net/base/proxy_chain.h"
9 #include "net/base/proxy_server.h"
10 #include "net/base/schemeful_site.h"
11 #include "net/dns/public/secure_dns_policy.h"
12 #include "net/socket/socket_tag.h"
13 #include "url/gurl.h"
14
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace net {
18
19 namespace {
20
21 // Check for equality of session keys, and inequality when various pieces of the
22 // key differ. The SocketTag is only used on Android, and the NAK is only used
23 // when network partitioning is enabled.
TEST(SpdySessionKeyTest,Equality)24 TEST(SpdySessionKeyTest, Equality) {
25 SpdySessionKey key(HostPortPair("www.example.org", 80), ProxyChain::Direct(),
26 PRIVACY_MODE_DISABLED,
27 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
28 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow);
29 EXPECT_EQ(key,
30 SpdySessionKey(HostPortPair("www.example.org", 80),
31 ProxyChain::Direct(), PRIVACY_MODE_DISABLED,
32 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
33 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow));
34 EXPECT_NE(key,
35 SpdySessionKey(HostPortPair("otherproxy", 80), ProxyChain::Direct(),
36 PRIVACY_MODE_DISABLED,
37 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
38 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow));
39 EXPECT_NE(key, SpdySessionKey(
40 HostPortPair("www.example.org", 80),
41 ProxyChain::FromSchemeHostAndPort(
42 ProxyServer::Scheme::SCHEME_HTTPS, "otherproxy", 443),
43 PRIVACY_MODE_DISABLED,
44 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
45 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow));
46 EXPECT_NE(key,
47 SpdySessionKey(HostPortPair("www.example.org", 80),
48 ProxyChain::Direct(), PRIVACY_MODE_ENABLED,
49 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
50 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow));
51 EXPECT_NE(key,
52 SpdySessionKey(HostPortPair("www.example.org", 80),
53 ProxyChain::Direct(), PRIVACY_MODE_DISABLED,
54 SpdySessionKey::IsProxySession::kTrue, SocketTag(),
55 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow));
56 #if BUILDFLAG(IS_ANDROID)
57 EXPECT_NE(key, SpdySessionKey(HostPortPair("www.example.org", 80),
58 ProxyChain::Direct(), PRIVACY_MODE_DISABLED,
59 SpdySessionKey::IsProxySession::kFalse,
60 SocketTag(999, 999), NetworkAnonymizationKey(),
61 SecureDnsPolicy::kAllow));
62 #endif // BUILDFLAG(IS_ANDROID)
63 if (NetworkAnonymizationKey::IsPartitioningEnabled()) {
64 EXPECT_NE(
65 key, SpdySessionKey(HostPortPair("www.example.org", 80),
66 ProxyChain::Direct(), PRIVACY_MODE_DISABLED,
67 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
68 NetworkAnonymizationKey::CreateSameSite(
69 SchemefulSite(GURL("http://a.test/"))),
70 SecureDnsPolicy::kAllow));
71 }
72 EXPECT_NE(key, SpdySessionKey(HostPortPair("www.example.org", 80),
73 ProxyChain::Direct(), PRIVACY_MODE_DISABLED,
74 SpdySessionKey::IsProxySession::kFalse,
75 SocketTag(), NetworkAnonymizationKey(),
76 SecureDnsPolicy::kDisable));
77 }
78
79 // The operator< implementation is suitable for storing distinct keys in a set.
TEST(SpdySessionKeyTest,Set)80 TEST(SpdySessionKeyTest, Set) {
81 std::vector<SpdySessionKey> session_keys = {
82 SpdySessionKey(HostPortPair("www.example.org", 80), ProxyChain::Direct(),
83 PRIVACY_MODE_DISABLED,
84 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
85 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow),
86 SpdySessionKey(HostPortPair("otherproxy", 80), ProxyChain::Direct(),
87 PRIVACY_MODE_DISABLED,
88 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
89 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow),
90 SpdySessionKey(HostPortPair("www.example.org", 80),
91 ProxyChain::FromSchemeHostAndPort(
92 ProxyServer::Scheme::SCHEME_HTTPS, "otherproxy", 443),
93 PRIVACY_MODE_DISABLED,
94 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
95 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow),
96 SpdySessionKey(HostPortPair("www.example.org", 80),
97 ProxyChain({
98 ProxyServer::FromSchemeHostAndPort(
99 ProxyServer::Scheme::SCHEME_HTTPS, "proxy1", 443),
100 ProxyServer::FromSchemeHostAndPort(
101 ProxyServer::Scheme::SCHEME_HTTPS, "proxy2", 443),
102 }),
103 PRIVACY_MODE_DISABLED,
104 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
105 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow),
106 SpdySessionKey(HostPortPair("www.example.org", 80), ProxyChain::Direct(),
107 PRIVACY_MODE_ENABLED,
108 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
109 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow),
110 SpdySessionKey(HostPortPair("www.example.org", 80), ProxyChain::Direct(),
111 PRIVACY_MODE_DISABLED,
112 SpdySessionKey::IsProxySession::kTrue, SocketTag(),
113 NetworkAnonymizationKey(), SecureDnsPolicy::kAllow),
114 SpdySessionKey(HostPortPair("www.example.org", 80), ProxyChain::Direct(),
115 PRIVACY_MODE_DISABLED,
116 SpdySessionKey::IsProxySession::kFalse, SocketTag(),
117 NetworkAnonymizationKey(), SecureDnsPolicy::kDisable),
118 };
119 std::set<SpdySessionKey> key_set(session_keys.begin(), session_keys.end());
120 ASSERT_EQ(session_keys.size(), key_set.size());
121 }
122
123 } // namespace
124
125 } // namespace net
126