xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_server_id_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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
3 // found in the LICENSE file.
4 
5 #include "quiche/quic/core/quic_server_id.h"
6 
7 #include <optional>
8 #include <string>
9 
10 #include "quiche/quic/platform/api/quic_test.h"
11 
12 namespace quic::test {
13 
14 namespace {
15 
16 using ::testing::Optional;
17 using ::testing::Property;
18 
19 class QuicServerIdTest : public QuicTest {};
20 
TEST_F(QuicServerIdTest,Constructor)21 TEST_F(QuicServerIdTest, Constructor) {
22   QuicServerId google_server_id("google.com", 10, false);
23   EXPECT_EQ("google.com", google_server_id.host());
24   EXPECT_EQ(10, google_server_id.port());
25   EXPECT_FALSE(google_server_id.privacy_mode_enabled());
26 
27   QuicServerId private_server_id("mail.google.com", 12, true);
28   EXPECT_EQ("mail.google.com", private_server_id.host());
29   EXPECT_EQ(12, private_server_id.port());
30   EXPECT_TRUE(private_server_id.privacy_mode_enabled());
31 }
32 
TEST_F(QuicServerIdTest,LessThan)33 TEST_F(QuicServerIdTest, LessThan) {
34   QuicServerId a_10_https("a.com", 10, false);
35   QuicServerId a_11_https("a.com", 11, false);
36   QuicServerId b_10_https("b.com", 10, false);
37   QuicServerId b_11_https("b.com", 11, false);
38 
39   QuicServerId a_10_https_private("a.com", 10, true);
40   QuicServerId a_11_https_private("a.com", 11, true);
41   QuicServerId b_10_https_private("b.com", 10, true);
42   QuicServerId b_11_https_private("b.com", 11, true);
43 
44   // Test combinations of host, port, and privacy being same on left and
45   // right side of less than.
46   EXPECT_FALSE(a_10_https < a_10_https);
47   EXPECT_TRUE(a_10_https < a_10_https_private);
48   EXPECT_FALSE(a_10_https_private < a_10_https);
49   EXPECT_FALSE(a_10_https_private < a_10_https_private);
50 
51   // Test with either host, port or https being different on left and right side
52   // of less than.
53   bool left_privacy;
54   bool right_privacy;
55   for (int i = 0; i < 4; i++) {
56     left_privacy = (i / 2 == 0);
57     right_privacy = (i % 2 == 0);
58     QuicServerId a_10_https_left_private("a.com", 10, left_privacy);
59     QuicServerId a_10_https_right_private("a.com", 10, right_privacy);
60     QuicServerId a_11_https_left_private("a.com", 11, left_privacy);
61     QuicServerId a_11_https_right_private("a.com", 11, right_privacy);
62 
63     QuicServerId b_10_https_left_private("b.com", 10, left_privacy);
64     QuicServerId b_10_https_right_private("b.com", 10, right_privacy);
65     QuicServerId b_11_https_left_private("b.com", 11, left_privacy);
66     QuicServerId b_11_https_right_private("b.com", 11, right_privacy);
67 
68     EXPECT_TRUE(a_10_https_left_private < a_11_https_right_private);
69     EXPECT_TRUE(a_10_https_left_private < b_10_https_right_private);
70     EXPECT_TRUE(a_10_https_left_private < b_11_https_right_private);
71     EXPECT_FALSE(a_11_https_left_private < a_10_https_right_private);
72     EXPECT_FALSE(a_11_https_left_private < b_10_https_right_private);
73     EXPECT_TRUE(a_11_https_left_private < b_11_https_right_private);
74     EXPECT_FALSE(b_10_https_left_private < a_10_https_right_private);
75     EXPECT_TRUE(b_10_https_left_private < a_11_https_right_private);
76     EXPECT_TRUE(b_10_https_left_private < b_11_https_right_private);
77     EXPECT_FALSE(b_11_https_left_private < a_10_https_right_private);
78     EXPECT_FALSE(b_11_https_left_private < a_11_https_right_private);
79     EXPECT_FALSE(b_11_https_left_private < b_10_https_right_private);
80   }
81 }
82 
TEST_F(QuicServerIdTest,Equals)83 TEST_F(QuicServerIdTest, Equals) {
84   bool left_privacy;
85   bool right_privacy;
86   for (int i = 0; i < 2; i++) {
87     left_privacy = right_privacy = (i == 0);
88     QuicServerId a_10_https_right_private("a.com", 10, right_privacy);
89     QuicServerId a_11_https_right_private("a.com", 11, right_privacy);
90     QuicServerId b_10_https_right_private("b.com", 10, right_privacy);
91     QuicServerId b_11_https_right_private("b.com", 11, right_privacy);
92 
93     EXPECT_NE(a_10_https_right_private, a_11_https_right_private);
94     EXPECT_NE(a_10_https_right_private, b_10_https_right_private);
95     EXPECT_NE(a_10_https_right_private, b_11_https_right_private);
96 
97     QuicServerId new_a_10_https_left_private("a.com", 10, left_privacy);
98     QuicServerId new_a_11_https_left_private("a.com", 11, left_privacy);
99     QuicServerId new_b_10_https_left_private("b.com", 10, left_privacy);
100     QuicServerId new_b_11_https_left_private("b.com", 11, left_privacy);
101 
102     EXPECT_EQ(new_a_10_https_left_private, a_10_https_right_private);
103     EXPECT_EQ(new_a_11_https_left_private, a_11_https_right_private);
104     EXPECT_EQ(new_b_10_https_left_private, b_10_https_right_private);
105     EXPECT_EQ(new_b_11_https_left_private, b_11_https_right_private);
106   }
107 
108   for (int i = 0; i < 2; i++) {
109     right_privacy = (i == 0);
110     QuicServerId a_10_https_right_private("a.com", 10, right_privacy);
111     QuicServerId a_11_https_right_private("a.com", 11, right_privacy);
112     QuicServerId b_10_https_right_private("b.com", 10, right_privacy);
113     QuicServerId b_11_https_right_private("b.com", 11, right_privacy);
114 
115     QuicServerId new_a_10_https_left_private("a.com", 10, false);
116 
117     EXPECT_NE(new_a_10_https_left_private, a_11_https_right_private);
118     EXPECT_NE(new_a_10_https_left_private, b_10_https_right_private);
119     EXPECT_NE(new_a_10_https_left_private, b_11_https_right_private);
120   }
121   QuicServerId a_10_https_private("a.com", 10, true);
122   QuicServerId new_a_10_https_no_private("a.com", 10, false);
123   EXPECT_NE(new_a_10_https_no_private, a_10_https_private);
124 }
125 
TEST_F(QuicServerIdTest,Parse)126 TEST_F(QuicServerIdTest, Parse) {
127   std::optional<QuicServerId> server_id =
128       QuicServerId::ParseFromHostPortString("host.test:500");
129 
130   EXPECT_THAT(server_id, Optional(Property(&QuicServerId::host, "host.test")));
131   EXPECT_THAT(server_id, Optional(Property(&QuicServerId::port, 500)));
132   EXPECT_THAT(server_id,
133               Optional(Property(&QuicServerId::privacy_mode_enabled, false)));
134 }
135 
TEST_F(QuicServerIdTest,CannotParseMissingPort)136 TEST_F(QuicServerIdTest, CannotParseMissingPort) {
137   std::optional<QuicServerId> server_id =
138       QuicServerId::ParseFromHostPortString("host.test");
139 
140   EXPECT_EQ(server_id, std::nullopt);
141 }
142 
TEST_F(QuicServerIdTest,CannotParseEmptyPort)143 TEST_F(QuicServerIdTest, CannotParseEmptyPort) {
144   std::optional<QuicServerId> server_id =
145       QuicServerId::ParseFromHostPortString("host.test:");
146 
147   EXPECT_EQ(server_id, std::nullopt);
148 }
149 
TEST_F(QuicServerIdTest,CannotParseEmptyHost)150 TEST_F(QuicServerIdTest, CannotParseEmptyHost) {
151   std::optional<QuicServerId> server_id =
152       QuicServerId::ParseFromHostPortString(":500");
153 
154   EXPECT_EQ(server_id, std::nullopt);
155 }
156 
TEST_F(QuicServerIdTest,CannotParseUserInfo)157 TEST_F(QuicServerIdTest, CannotParseUserInfo) {
158   std::optional<QuicServerId> server_id =
159       QuicServerId::ParseFromHostPortString("[email protected]:500");
160 
161   EXPECT_EQ(server_id, std::nullopt);
162 }
163 
TEST_F(QuicServerIdTest,ParseIpv6Literal)164 TEST_F(QuicServerIdTest, ParseIpv6Literal) {
165   std::optional<QuicServerId> server_id =
166       QuicServerId::ParseFromHostPortString("[::1]:400");
167 
168   EXPECT_THAT(server_id, Optional(Property(&QuicServerId::host, "[::1]")));
169   EXPECT_THAT(server_id, Optional(Property(&QuicServerId::port, 400)));
170   EXPECT_THAT(server_id,
171               Optional(Property(&QuicServerId::privacy_mode_enabled, false)));
172 }
173 
TEST_F(QuicServerIdTest,ParseUnbracketedIpv6Literal)174 TEST_F(QuicServerIdTest, ParseUnbracketedIpv6Literal) {
175   std::optional<QuicServerId> server_id =
176       QuicServerId::ParseFromHostPortString("::1:400");
177 
178   EXPECT_THAT(server_id, Optional(Property(&QuicServerId::host, "::1")));
179   EXPECT_THAT(server_id, Optional(Property(&QuicServerId::port, 400)));
180   EXPECT_THAT(server_id,
181               Optional(Property(&QuicServerId::privacy_mode_enabled, false)));
182 }
183 
TEST_F(QuicServerIdTest,AddBracketsToIpv6)184 TEST_F(QuicServerIdTest, AddBracketsToIpv6) {
185   QuicServerId server_id("::1", 100);
186 
187   EXPECT_EQ(server_id.GetHostWithIpv6Brackets(), "[::1]");
188   EXPECT_EQ(server_id.ToHostPortString(), "[::1]:100");
189 }
190 
TEST_F(QuicServerIdTest,AddBracketsAlreadyIncluded)191 TEST_F(QuicServerIdTest, AddBracketsAlreadyIncluded) {
192   QuicServerId server_id("[::1]", 100);
193 
194   EXPECT_EQ(server_id.GetHostWithIpv6Brackets(), "[::1]");
195   EXPECT_EQ(server_id.ToHostPortString(), "[::1]:100");
196 }
197 
TEST_F(QuicServerIdTest,AddBracketsNotAddedToNonIpv6)198 TEST_F(QuicServerIdTest, AddBracketsNotAddedToNonIpv6) {
199   QuicServerId server_id("host.test", 100);
200 
201   EXPECT_EQ(server_id.GetHostWithIpv6Brackets(), "host.test");
202   EXPECT_EQ(server_id.ToHostPortString(), "host.test:100");
203 }
204 
TEST_F(QuicServerIdTest,RemoveBracketsFromIpv6)205 TEST_F(QuicServerIdTest, RemoveBracketsFromIpv6) {
206   QuicServerId server_id("[::1]", 100);
207 
208   EXPECT_EQ(server_id.GetHostWithoutIpv6Brackets(), "::1");
209 }
210 
TEST_F(QuicServerIdTest,RemoveBracketsNotIncluded)211 TEST_F(QuicServerIdTest, RemoveBracketsNotIncluded) {
212   QuicServerId server_id("::1", 100);
213 
214   EXPECT_EQ(server_id.GetHostWithoutIpv6Brackets(), "::1");
215 }
216 
TEST_F(QuicServerIdTest,RemoveBracketsFromNonIpv6)217 TEST_F(QuicServerIdTest, RemoveBracketsFromNonIpv6) {
218   QuicServerId server_id("host.test", 100);
219 
220   EXPECT_EQ(server_id.GetHostWithoutIpv6Brackets(), "host.test");
221 }
222 
223 }  // namespace
224 
225 }  // namespace quic::test
226