• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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),
34         server_info_(server_id_,
35                      PRIVACY_MODE_DISABLED,
36                      NetworkAnonymizationKey(),
37                      &http_server_properties_) {}
38 
39   // Initialize |server_info_| object and persist it.
InitializeAndPersist()40   void InitializeAndPersist() {
41     QuicServerInfo::State* state = server_info_.mutable_state();
42     EXPECT_TRUE(state->certs.empty());
43 
44     state->server_config = kServerConfigA;
45     state->source_address_token = kSourceAddressTokenA;
46     state->server_config_sig = kServerConfigSigA;
47     state->cert_sct = kCertSCTA;
48     state->chlo_hash = kChloHashA;
49     state->certs.push_back(kCertA);
50     server_info_.Persist();
51   }
52 
53   // Verify the data that is persisted in InitializeAndPersist().
VerifyInitialData(const QuicServerInfo::State & state)54   void VerifyInitialData(const QuicServerInfo::State& state) {
55     EXPECT_EQ(kServerConfigA, state.server_config);
56     EXPECT_EQ(kSourceAddressTokenA, state.source_address_token);
57     EXPECT_EQ(kCertSCTA, state.cert_sct);
58     EXPECT_EQ(kChloHashA, state.chlo_hash);
59     EXPECT_EQ(kServerConfigSigA, state.server_config_sig);
60     EXPECT_EQ(kCertA, state.certs[0]);
61   }
62 
63   HttpServerProperties http_server_properties_;
64   quic::QuicServerId server_id_;
65   PropertiesBasedQuicServerInfo server_info_;
66 };
67 
68 // Test persisting, reading and verifying and then updating and verifing.
TEST_F(PropertiesBasedQuicServerInfoTest,Update)69 TEST_F(PropertiesBasedQuicServerInfoTest, Update) {
70   InitializeAndPersist();
71 
72   // Read the persisted data and verify we have read the data correctly.
73   PropertiesBasedQuicServerInfo server_info1(server_id_, PRIVACY_MODE_DISABLED,
74                                              NetworkAnonymizationKey(),
75                                              &http_server_properties_);
76   EXPECT_TRUE(server_info1.Load());
77 
78   // Verify the data.
79   const QuicServerInfo::State& state1 = server_info1.state();
80   EXPECT_EQ(1U, state1.certs.size());
81   VerifyInitialData(state1);
82 
83   // Update the data, by adding another cert.
84   QuicServerInfo::State* state2 = server_info1.mutable_state();
85   state2->certs.push_back(kCertB);
86   server_info1.Persist();
87 
88   // Read the persisted data and verify we have read the data correctly.
89   PropertiesBasedQuicServerInfo server_info2(server_id_, PRIVACY_MODE_DISABLED,
90                                              NetworkAnonymizationKey(),
91                                              &http_server_properties_);
92   EXPECT_TRUE(server_info2.Load());
93 
94   // Verify updated data.
95   const QuicServerInfo::State& state3 = server_info2.state();
96   VerifyInitialData(state3);
97   EXPECT_EQ(2U, state3.certs.size());
98   EXPECT_EQ(kCertB, state3.certs[1]);
99 }
100 
101 }  // namespace net::test
102