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 "net/quic/quic_server_id.h" 6 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 using std::string; 10 11 namespace net { 12 13 namespace { 14 15 TEST(QuicServerIdTest, ToString) { 16 HostPortPair google_host_port_pair("google.com", 10); 17 18 QuicServerId google_http_server_id(google_host_port_pair, false, 19 PRIVACY_MODE_DISABLED); 20 string google_http_server_id_str = google_http_server_id.ToString(); 21 EXPECT_EQ("http://google.com:10", google_http_server_id_str); 22 23 QuicServerId google_https_server_id(google_host_port_pair, true, 24 PRIVACY_MODE_DISABLED); 25 string google_https_server_id_str = google_https_server_id.ToString(); 26 EXPECT_EQ("https://google.com:10", google_https_server_id_str); 27 28 QuicServerId private_http_server_id(google_host_port_pair, false, 29 PRIVACY_MODE_ENABLED); 30 string private_http_server_id_str = private_http_server_id.ToString(); 31 EXPECT_EQ("http://google.com:10/private", private_http_server_id_str); 32 33 QuicServerId private_https_server_id(google_host_port_pair, true, 34 PRIVACY_MODE_ENABLED); 35 string private_https_server_id_str = private_https_server_id.ToString(); 36 EXPECT_EQ("https://google.com:10/private", private_https_server_id_str); 37 } 38 39 TEST(QuicServerIdTest, LessThan) { 40 QuicServerId a_10_http(HostPortPair("a.com", 10), false, 41 PRIVACY_MODE_DISABLED); 42 QuicServerId a_10_https(HostPortPair("a.com", 10), true, 43 PRIVACY_MODE_DISABLED); 44 QuicServerId a_11_http(HostPortPair("a.com", 11), false, 45 PRIVACY_MODE_DISABLED); 46 QuicServerId a_11_https(HostPortPair("a.com", 11), true, 47 PRIVACY_MODE_DISABLED); 48 QuicServerId b_10_http(HostPortPair("b.com", 10), false, 49 PRIVACY_MODE_DISABLED); 50 QuicServerId b_10_https(HostPortPair("b.com", 10), true, 51 PRIVACY_MODE_DISABLED); 52 QuicServerId b_11_http(HostPortPair("b.com", 11), false, 53 PRIVACY_MODE_DISABLED); 54 QuicServerId b_11_https(HostPortPair("b.com", 11), true, 55 PRIVACY_MODE_DISABLED); 56 57 QuicServerId a_10_http_private(HostPortPair("a.com", 10), false, 58 PRIVACY_MODE_ENABLED); 59 QuicServerId a_10_https_private(HostPortPair("a.com", 10), true, 60 PRIVACY_MODE_ENABLED); 61 QuicServerId a_11_http_private(HostPortPair("a.com", 11), false, 62 PRIVACY_MODE_ENABLED); 63 QuicServerId a_11_https_private(HostPortPair("a.com", 11), true, 64 PRIVACY_MODE_ENABLED); 65 QuicServerId b_10_http_private(HostPortPair("b.com", 10), false, 66 PRIVACY_MODE_ENABLED); 67 QuicServerId b_10_https_private(HostPortPair("b.com", 10), true, 68 PRIVACY_MODE_ENABLED); 69 QuicServerId b_11_http_private(HostPortPair("b.com", 11), false, 70 PRIVACY_MODE_ENABLED); 71 QuicServerId b_11_https_private(HostPortPair("b.com", 11), true, 72 PRIVACY_MODE_ENABLED); 73 74 // Test combinations of host, port, https and privacy being same on left and 75 // right side of less than. 76 EXPECT_FALSE(a_10_http < a_10_http); 77 EXPECT_TRUE(a_10_http < a_10_https); 78 EXPECT_FALSE(a_10_https < a_10_http); 79 EXPECT_FALSE(a_10_https < a_10_https); 80 81 EXPECT_TRUE(a_10_http < a_10_http_private); 82 EXPECT_TRUE(a_10_http < a_10_https_private); 83 EXPECT_FALSE(a_10_https < a_10_http_private); 84 EXPECT_TRUE(a_10_https < a_10_https_private); 85 86 EXPECT_FALSE(a_10_http_private < a_10_http); 87 EXPECT_TRUE(a_10_http_private < a_10_https); 88 EXPECT_FALSE(a_10_https_private < a_10_http); 89 EXPECT_FALSE(a_10_https_private < a_10_https); 90 91 EXPECT_FALSE(a_10_http_private < a_10_http_private); 92 EXPECT_TRUE(a_10_http_private < a_10_https_private); 93 EXPECT_FALSE(a_10_https_private < a_10_http_private); 94 EXPECT_FALSE(a_10_https_private < a_10_https_private); 95 96 // Test with either host, port or https being different on left and right side 97 // of less than. 98 PrivacyMode left_privacy; 99 PrivacyMode right_privacy; 100 for (int i = 0; i < 4; i++) { 101 switch (i) { 102 case 0: 103 left_privacy = PRIVACY_MODE_DISABLED; 104 right_privacy = PRIVACY_MODE_DISABLED; 105 break; 106 case 1: 107 left_privacy = PRIVACY_MODE_DISABLED; 108 right_privacy = PRIVACY_MODE_ENABLED; 109 break; 110 case 2: 111 left_privacy = PRIVACY_MODE_ENABLED; 112 right_privacy = PRIVACY_MODE_DISABLED; 113 break; 114 case 3: 115 left_privacy = PRIVACY_MODE_ENABLED; 116 right_privacy = PRIVACY_MODE_ENABLED; 117 break; 118 } 119 QuicServerId a_10_http_left_private(HostPortPair("a.com", 10), false, 120 left_privacy); 121 QuicServerId a_10_http_right_private(HostPortPair("a.com", 10), false, 122 right_privacy); 123 QuicServerId a_10_https_left_private(HostPortPair("a.com", 10), true, 124 left_privacy); 125 QuicServerId a_10_https_right_private(HostPortPair("a.com", 10), true, 126 right_privacy); 127 QuicServerId a_11_http_left_private(HostPortPair("a.com", 11), false, 128 left_privacy); 129 QuicServerId a_11_http_right_private(HostPortPair("a.com", 11), false, 130 right_privacy); 131 QuicServerId a_11_https_left_private(HostPortPair("a.com", 11), true, 132 left_privacy); 133 QuicServerId a_11_https_right_private(HostPortPair("a.com", 11), true, 134 right_privacy); 135 136 QuicServerId b_10_http_left_private(HostPortPair("b.com", 10), false, 137 left_privacy); 138 QuicServerId b_10_http_right_private(HostPortPair("b.com", 10), false, 139 right_privacy); 140 QuicServerId b_10_https_left_private(HostPortPair("b.com", 10), true, 141 left_privacy); 142 QuicServerId b_10_https_right_private(HostPortPair("b.com", 10), true, 143 right_privacy); 144 QuicServerId b_11_http_left_private(HostPortPair("b.com", 11), false, 145 left_privacy); 146 QuicServerId b_11_http_right_private(HostPortPair("b.com", 11), false, 147 right_privacy); 148 QuicServerId b_11_https_left_private(HostPortPair("b.com", 11), true, 149 left_privacy); 150 QuicServerId b_11_https_right_private(HostPortPair("b.com", 11), true, 151 right_privacy); 152 153 EXPECT_TRUE(a_10_http_left_private < a_11_http_right_private); 154 EXPECT_TRUE(a_10_http_left_private < a_11_https_right_private); 155 EXPECT_TRUE(a_10_https_left_private < a_11_http_right_private); 156 EXPECT_TRUE(a_10_https_left_private < a_11_https_right_private); 157 158 EXPECT_TRUE(a_10_http_left_private < b_10_http_right_private); 159 EXPECT_TRUE(a_10_http_left_private < b_10_https_right_private); 160 EXPECT_TRUE(a_10_https_left_private < b_10_http_right_private); 161 EXPECT_TRUE(a_10_https_left_private < b_10_https_right_private); 162 163 EXPECT_TRUE(a_10_http_left_private < b_11_http_right_private); 164 EXPECT_TRUE(a_10_http_left_private < b_11_https_right_private); 165 EXPECT_TRUE(a_10_https_left_private < b_11_http_right_private); 166 EXPECT_TRUE(a_10_https_left_private < b_11_https_right_private); 167 168 EXPECT_FALSE(a_11_http_left_private < a_10_http_right_private); 169 EXPECT_FALSE(a_11_http_left_private < a_10_https_right_private); 170 EXPECT_FALSE(a_11_https_left_private < a_10_http_right_private); 171 EXPECT_FALSE(a_11_https_left_private < a_10_https_right_private); 172 173 EXPECT_FALSE(a_11_http_left_private < b_10_http_right_private); 174 EXPECT_FALSE(a_11_http_left_private < b_10_https_right_private); 175 EXPECT_FALSE(a_11_https_left_private < b_10_http_right_private); 176 EXPECT_FALSE(a_11_https_left_private < b_10_https_right_private); 177 178 EXPECT_TRUE(a_11_http_left_private < b_11_http_right_private); 179 EXPECT_TRUE(a_11_http_left_private < b_11_https_right_private); 180 EXPECT_TRUE(a_11_https_left_private < b_11_http_right_private); 181 EXPECT_TRUE(a_11_https_left_private < b_11_https_right_private); 182 183 EXPECT_FALSE(b_10_http_left_private < a_10_http_right_private); 184 EXPECT_FALSE(b_10_http_left_private < a_10_https_right_private); 185 EXPECT_FALSE(b_10_https_left_private < a_10_http_right_private); 186 EXPECT_FALSE(b_10_https_left_private < a_10_https_right_private); 187 188 EXPECT_TRUE(b_10_http_left_private < a_11_http_right_private); 189 EXPECT_TRUE(b_10_http_left_private < a_11_https_right_private); 190 EXPECT_TRUE(b_10_https_left_private < a_11_http_right_private); 191 EXPECT_TRUE(b_10_https_left_private < a_11_https_right_private); 192 193 EXPECT_TRUE(b_10_http_left_private < b_11_http_right_private); 194 EXPECT_TRUE(b_10_http_left_private < b_11_https_right_private); 195 EXPECT_TRUE(b_10_https_left_private < b_11_http_right_private); 196 EXPECT_TRUE(b_10_https_left_private < b_11_https_right_private); 197 198 EXPECT_FALSE(b_11_http_left_private < a_10_http_right_private); 199 EXPECT_FALSE(b_11_http_left_private < a_10_https_right_private); 200 EXPECT_FALSE(b_11_https_left_private < a_10_http_right_private); 201 EXPECT_FALSE(b_11_https_left_private < a_10_https_right_private); 202 203 EXPECT_FALSE(b_11_http_left_private < a_11_http_right_private); 204 EXPECT_FALSE(b_11_http_left_private < a_11_https_right_private); 205 EXPECT_FALSE(b_11_https_left_private < a_11_http_right_private); 206 EXPECT_FALSE(b_11_https_left_private < a_11_https_right_private); 207 208 EXPECT_FALSE(b_11_http_left_private < b_10_http_right_private); 209 EXPECT_FALSE(b_11_http_left_private < b_10_https_right_private); 210 EXPECT_FALSE(b_11_https_left_private < b_10_http_right_private); 211 EXPECT_FALSE(b_11_https_left_private < b_10_https_right_private); 212 } 213 } 214 215 TEST(QuicServerIdTest, Equals) { 216 PrivacyMode left_privacy; 217 PrivacyMode right_privacy; 218 for (int i = 0; i < 2; i++) { 219 switch (i) { 220 case 0: 221 left_privacy = PRIVACY_MODE_DISABLED; 222 right_privacy = PRIVACY_MODE_DISABLED; 223 break; 224 case 1: 225 left_privacy = PRIVACY_MODE_ENABLED; 226 right_privacy = PRIVACY_MODE_ENABLED; 227 break; 228 } 229 QuicServerId a_10_http_right_private(HostPortPair("a.com", 10), false, 230 right_privacy); 231 QuicServerId a_10_https_right_private(HostPortPair("a.com", 10), true, 232 right_privacy); 233 QuicServerId a_11_http_right_private(HostPortPair("a.com", 11), false, 234 right_privacy); 235 QuicServerId a_11_https_right_private(HostPortPair("a.com", 11), true, 236 right_privacy); 237 QuicServerId b_10_http_right_private(HostPortPair("b.com", 10), false, 238 right_privacy); 239 QuicServerId b_10_https_right_private(HostPortPair("b.com", 10), true, 240 right_privacy); 241 QuicServerId b_11_http_right_private(HostPortPair("b.com", 11), false, 242 right_privacy); 243 QuicServerId b_11_https_right_private(HostPortPair("b.com", 11), true, 244 right_privacy); 245 246 QuicServerId new_a_10_http_left_private(HostPortPair("a.com", 10), false, 247 left_privacy); 248 QuicServerId new_a_10_https_left_private(HostPortPair("a.com", 10), true, 249 left_privacy); 250 QuicServerId new_a_11_http_left_private(HostPortPair("a.com", 11), false, 251 left_privacy); 252 QuicServerId new_a_11_https_left_private(HostPortPair("a.com", 11), true, 253 left_privacy); 254 QuicServerId new_b_10_http_left_private(HostPortPair("b.com", 10), false, 255 left_privacy); 256 QuicServerId new_b_10_https_left_private(HostPortPair("b.com", 10), true, 257 left_privacy); 258 QuicServerId new_b_11_http_left_private(HostPortPair("b.com", 11), false, 259 left_privacy); 260 QuicServerId new_b_11_https_left_private(HostPortPair("b.com", 11), true, 261 left_privacy); 262 263 EXPECT_EQ(new_a_10_http_left_private, a_10_http_right_private); 264 EXPECT_EQ(new_a_10_https_left_private, a_10_https_right_private); 265 EXPECT_EQ(new_a_11_http_left_private, a_11_http_right_private); 266 EXPECT_EQ(new_a_11_https_left_private, a_11_https_right_private); 267 EXPECT_EQ(new_b_10_http_left_private, b_10_http_right_private); 268 EXPECT_EQ(new_b_10_https_left_private, b_10_https_right_private); 269 EXPECT_EQ(new_b_11_http_left_private, b_11_http_right_private); 270 EXPECT_EQ(new_b_11_https_left_private, b_11_https_right_private); 271 } 272 273 for (int i = 0; i < 2; i++) { 274 switch (i) { 275 case 0: 276 right_privacy = PRIVACY_MODE_DISABLED; 277 break; 278 case 1: 279 right_privacy = PRIVACY_MODE_ENABLED; 280 break; 281 } 282 QuicServerId a_10_http_right_private(HostPortPair("a.com", 10), false, 283 right_privacy); 284 QuicServerId a_10_https_right_private(HostPortPair("a.com", 10), true, 285 right_privacy); 286 QuicServerId a_11_http_right_private(HostPortPair("a.com", 11), false, 287 right_privacy); 288 QuicServerId a_11_https_right_private(HostPortPair("a.com", 11), true, 289 right_privacy); 290 QuicServerId b_10_http_right_private(HostPortPair("b.com", 10), false, 291 right_privacy); 292 QuicServerId b_10_https_right_private(HostPortPair("b.com", 10), true, 293 right_privacy); 294 QuicServerId b_11_http_right_private(HostPortPair("b.com", 11), false, 295 right_privacy); 296 QuicServerId b_11_https_right_private(HostPortPair("b.com", 11), true, 297 right_privacy); 298 299 QuicServerId new_a_10_http_left_private(HostPortPair("a.com", 10), false, 300 PRIVACY_MODE_DISABLED); 301 302 EXPECT_FALSE(new_a_10_http_left_private == a_10_https_right_private); 303 EXPECT_FALSE(new_a_10_http_left_private == a_11_http_right_private); 304 EXPECT_FALSE(new_a_10_http_left_private == b_10_http_right_private); 305 EXPECT_FALSE(new_a_10_http_left_private == a_11_https_right_private); 306 EXPECT_FALSE(new_a_10_http_left_private == b_10_https_right_private); 307 EXPECT_FALSE(new_a_10_http_left_private == b_11_http_right_private); 308 EXPECT_FALSE(new_a_10_http_left_private == b_11_https_right_private); 309 } 310 QuicServerId a_10_http_private(HostPortPair("a.com", 10), false, 311 PRIVACY_MODE_ENABLED); 312 QuicServerId new_a_10_http_no_private(HostPortPair("a.com", 10), false, 313 PRIVACY_MODE_DISABLED); 314 EXPECT_FALSE(new_a_10_http_no_private == a_10_http_private); 315 } 316 317 } // namespace 318 319 } // namespace net 320