xref: /aosp_15_r20/external/cronet/net/quic/properties_based_quic_server_info_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 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/quic/properties_based_quic_server_info.h"
6 
7 #include <string>
8 
9 #include "net/base/host_port_pair.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/privacy_mode.h"
12 #include "net/http/http_server_properties.h"
13 #include "net/test/gtest_util.h"
14 #include "net/third_party/quiche/src/quiche/quic/core/quic_server_id.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 namespace net::test {
19 
20 namespace {
21 const char kServerConfigA[] = "server_config_a";
22 const char kSourceAddressTokenA[] = "source_address_token_a";
23 const char kCertSCTA[] = "cert_sct_a";
24 const char kChloHashA[] = "chlo_hash_a";
25 const char kServerConfigSigA[] = "server_config_sig_a";
26 const char kCertA[] = "cert_a";
27 const char kCertB[] = "cert_b";
28 }  // namespace
29 
30 class PropertiesBasedQuicServerInfoTest : public ::testing::Test {
31  protected:
PropertiesBasedQuicServerInfoTest()32   PropertiesBasedQuicServerInfoTest()
33       : server_id_("www.google.com", 443, PRIVACY_MODE_DISABLED),
34         server_info_(server_id_,
35                      NetworkAnonymizationKey(),
36                      &http_server_properties_) {}
37 
38   // Initialize |server_info_| object and persist it.
InitializeAndPersist()39   void InitializeAndPersist() {
40     QuicServerInfo::State* state = server_info_.mutable_state();
41     EXPECT_TRUE(state->certs.empty());
42 
43     state->server_config = kServerConfigA;
44     state->source_address_token = kSourceAddressTokenA;
45     state->server_config_sig = kServerConfigSigA;
46     state->cert_sct = kCertSCTA;
47     state->chlo_hash = kChloHashA;
48     state->certs.push_back(kCertA);
49     server_info_.Persist();
50   }
51 
52   // Verify the data that is persisted in InitializeAndPersist().
VerifyInitialData(const QuicServerInfo::State & state)53   void VerifyInitialData(const QuicServerInfo::State& state) {
54     EXPECT_EQ(kServerConfigA, state.server_config);
55     EXPECT_EQ(kSourceAddressTokenA, state.source_address_token);
56     EXPECT_EQ(kCertSCTA, state.cert_sct);
57     EXPECT_EQ(kChloHashA, state.chlo_hash);
58     EXPECT_EQ(kServerConfigSigA, state.server_config_sig);
59     EXPECT_EQ(kCertA, state.certs[0]);
60   }
61 
62   HttpServerProperties http_server_properties_;
63   quic::QuicServerId server_id_;
64   PropertiesBasedQuicServerInfo server_info_;
65 };
66 
67 // Test persisting, reading and verifying and then updating and verifing.
TEST_F(PropertiesBasedQuicServerInfoTest,Update)68 TEST_F(PropertiesBasedQuicServerInfoTest, Update) {
69   InitializeAndPersist();
70 
71   // Read the persisted data and verify we have read the data correctly.
72   PropertiesBasedQuicServerInfo server_info1(
73       server_id_, NetworkAnonymizationKey(), &http_server_properties_);
74   EXPECT_TRUE(server_info1.Load());
75 
76   // Verify the data.
77   const QuicServerInfo::State& state1 = server_info1.state();
78   EXPECT_EQ(1U, state1.certs.size());
79   VerifyInitialData(state1);
80 
81   // Update the data, by adding another cert.
82   QuicServerInfo::State* state2 = server_info1.mutable_state();
83   state2->certs.push_back(kCertB);
84   server_info1.Persist();
85 
86   // Read the persisted data and verify we have read the data correctly.
87   PropertiesBasedQuicServerInfo server_info2(
88       server_id_, NetworkAnonymizationKey(), &http_server_properties_);
89   EXPECT_TRUE(server_info2.Load());
90 
91   // Verify updated data.
92   const QuicServerInfo::State& state3 = server_info2.state();
93   VerifyInitialData(state3);
94   EXPECT_EQ(2U, state3.certs.size());
95   EXPECT_EQ(kCertB, state3.certs[1]);
96 }
97 
98 }  // namespace net::test
99