1 // Copyright 2015 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/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/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 {
19 namespace test {
20 
21 namespace {
22 const char kServerConfigA[] = "server_config_a";
23 const char kSourceAddressTokenA[] = "source_address_token_a";
24 const char kCertSCTA[] = "cert_sct_a";
25 const char kChloHashA[] = "chlo_hash_a";
26 const char kServerConfigSigA[] = "server_config_sig_a";
27 const char kCertA[] = "cert_a";
28 const char kCertB[] = "cert_b";
29 }  // namespace
30 
31 class PropertiesBasedQuicServerInfoTest : public ::testing::Test {
32  protected:
PropertiesBasedQuicServerInfoTest()33   PropertiesBasedQuicServerInfoTest()
34       : server_id_("www.google.com", 443, PRIVACY_MODE_DISABLED),
35         server_info_(server_id_,
36                      NetworkIsolationKey(),
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_, NetworkIsolationKey(),
74                                              &http_server_properties_);
75   EXPECT_TRUE(server_info1.Load());
76 
77   // Verify the data.
78   const QuicServerInfo::State& state1 = server_info1.state();
79   EXPECT_EQ(1U, state1.certs.size());
80   VerifyInitialData(state1);
81 
82   // Update the data, by adding another cert.
83   QuicServerInfo::State* state2 = server_info1.mutable_state();
84   state2->certs.push_back(kCertB);
85   server_info1.Persist();
86 
87   // Read the persisted data and verify we have read the data correctly.
88   PropertiesBasedQuicServerInfo server_info2(server_id_, NetworkIsolationKey(),
89                                              &http_server_properties_);
90   EXPECT_TRUE(server_info2.Load());
91 
92   // Verify updated data.
93   const QuicServerInfo::State& state3 = server_info2.state();
94   VerifyInitialData(state3);
95   EXPECT_EQ(2U, state3.certs.size());
96   EXPECT_EQ(kCertB, state3.certs[1]);
97 }
98 
99 }  // namespace test
100 }  // namespace net
101