http_response_info_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #include "net/http/http_response_info.h"
  5. #include "base/pickle.h"
  6. #include "net/cert/signed_certificate_timestamp.h"
  7. #include "net/cert/signed_certificate_timestamp_and_status.h"
  8. #include "net/http/http_response_headers.h"
  9. #include "net/ssl/ssl_connection_status_flags.h"
  10. #include "net/test/cert_test_util.h"
  11. #include "net/test/ct_test_util.h"
  12. #include "net/test/test_data_directory.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace net {
  16. namespace {
  17. class HttpResponseInfoTest : public testing::Test {
  18. protected:
  19. void SetUp() override {
  20. response_info_.headers = base::MakeRefCounted<HttpResponseHeaders>("");
  21. }
  22. void PickleAndRestore(const HttpResponseInfo& response_info,
  23. HttpResponseInfo* restored_response_info) const {
  24. base::Pickle pickle;
  25. response_info.Persist(&pickle, false, false);
  26. bool truncated = false;
  27. EXPECT_TRUE(restored_response_info->InitFromPickle(pickle, &truncated));
  28. }
  29. HttpResponseInfo response_info_;
  30. };
  31. TEST_F(HttpResponseInfoTest, UnusedSincePrefetchDefault) {
  32. EXPECT_FALSE(response_info_.unused_since_prefetch);
  33. }
  34. TEST_F(HttpResponseInfoTest, UnusedSincePrefetchCopy) {
  35. response_info_.unused_since_prefetch = true;
  36. HttpResponseInfo response_info_clone(response_info_);
  37. EXPECT_TRUE(response_info_clone.unused_since_prefetch);
  38. }
  39. TEST_F(HttpResponseInfoTest, UnusedSincePrefetchPersistFalse) {
  40. HttpResponseInfo restored_response_info;
  41. PickleAndRestore(response_info_, &restored_response_info);
  42. EXPECT_FALSE(restored_response_info.unused_since_prefetch);
  43. }
  44. TEST_F(HttpResponseInfoTest, UnusedSincePrefetchPersistTrue) {
  45. response_info_.unused_since_prefetch = true;
  46. HttpResponseInfo restored_response_info;
  47. PickleAndRestore(response_info_, &restored_response_info);
  48. EXPECT_TRUE(restored_response_info.unused_since_prefetch);
  49. }
  50. TEST_F(HttpResponseInfoTest, PKPBypassPersistTrue) {
  51. response_info_.ssl_info.pkp_bypassed = true;
  52. HttpResponseInfo restored_response_info;
  53. PickleAndRestore(response_info_, &restored_response_info);
  54. EXPECT_TRUE(restored_response_info.ssl_info.pkp_bypassed);
  55. }
  56. TEST_F(HttpResponseInfoTest, PKPBypassPersistFalse) {
  57. response_info_.ssl_info.pkp_bypassed = false;
  58. HttpResponseInfo restored_response_info;
  59. PickleAndRestore(response_info_, &restored_response_info);
  60. EXPECT_FALSE(restored_response_info.ssl_info.pkp_bypassed);
  61. }
  62. TEST_F(HttpResponseInfoTest, AsyncRevalidationRequestedDefault) {
  63. EXPECT_FALSE(response_info_.async_revalidation_requested);
  64. }
  65. TEST_F(HttpResponseInfoTest, AsyncRevalidationRequestedCopy) {
  66. response_info_.async_revalidation_requested = true;
  67. net::HttpResponseInfo response_info_clone(response_info_);
  68. EXPECT_TRUE(response_info_clone.async_revalidation_requested);
  69. }
  70. TEST_F(HttpResponseInfoTest, AsyncRevalidationRequestedAssign) {
  71. response_info_.async_revalidation_requested = true;
  72. net::HttpResponseInfo response_info_clone;
  73. response_info_clone = response_info_;
  74. EXPECT_TRUE(response_info_clone.async_revalidation_requested);
  75. }
  76. TEST_F(HttpResponseInfoTest, AsyncRevalidationRequestedNotPersisted) {
  77. response_info_.async_revalidation_requested = true;
  78. net::HttpResponseInfo restored_response_info;
  79. PickleAndRestore(response_info_, &restored_response_info);
  80. EXPECT_FALSE(restored_response_info.async_revalidation_requested);
  81. }
  82. TEST_F(HttpResponseInfoTest, StaleRevalidationTimeoutDefault) {
  83. EXPECT_TRUE(response_info_.stale_revalidate_timeout.is_null());
  84. }
  85. TEST_F(HttpResponseInfoTest, StaleRevalidationTimeoutCopy) {
  86. base::Time test_time = base::Time::FromDoubleT(1000);
  87. response_info_.stale_revalidate_timeout = test_time;
  88. HttpResponseInfo response_info_clone(response_info_);
  89. EXPECT_EQ(test_time, response_info_clone.stale_revalidate_timeout);
  90. }
  91. TEST_F(HttpResponseInfoTest, StaleRevalidationTimeoutRestoreValue) {
  92. base::Time test_time = base::Time::FromDoubleT(1000);
  93. response_info_.stale_revalidate_timeout = test_time;
  94. HttpResponseInfo restored_response_info;
  95. PickleAndRestore(response_info_, &restored_response_info);
  96. EXPECT_EQ(test_time, restored_response_info.stale_revalidate_timeout);
  97. }
  98. TEST_F(HttpResponseInfoTest, StaleRevalidationTimeoutRestoreNoValue) {
  99. EXPECT_TRUE(response_info_.stale_revalidate_timeout.is_null());
  100. HttpResponseInfo restored_response_info;
  101. PickleAndRestore(response_info_, &restored_response_info);
  102. EXPECT_TRUE(restored_response_info.stale_revalidate_timeout.is_null());
  103. }
  104. // Test that key_exchange_group is preserved for ECDHE ciphers.
  105. TEST_F(HttpResponseInfoTest, KeyExchangeGroupECDHE) {
  106. response_info_.ssl_info.cert =
  107. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  108. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
  109. &response_info_.ssl_info.connection_status);
  110. SSLConnectionStatusSetCipherSuite(
  111. 0xcca8 /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */,
  112. &response_info_.ssl_info.connection_status);
  113. response_info_.ssl_info.key_exchange_group = 23; // X25519
  114. net::HttpResponseInfo restored_response_info;
  115. PickleAndRestore(response_info_, &restored_response_info);
  116. EXPECT_EQ(23, restored_response_info.ssl_info.key_exchange_group);
  117. }
  118. // Test that key_exchange_group is preserved for TLS 1.3.
  119. TEST_F(HttpResponseInfoTest, KeyExchangeGroupTLS13) {
  120. response_info_.ssl_info.cert =
  121. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  122. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_3,
  123. &response_info_.ssl_info.connection_status);
  124. SSLConnectionStatusSetCipherSuite(0x1303 /* TLS_CHACHA20_POLY1305_SHA256 */,
  125. &response_info_.ssl_info.connection_status);
  126. response_info_.ssl_info.key_exchange_group = 23; // X25519
  127. net::HttpResponseInfo restored_response_info;
  128. PickleAndRestore(response_info_, &restored_response_info);
  129. EXPECT_EQ(23, restored_response_info.ssl_info.key_exchange_group);
  130. }
  131. // Test that key_exchange_group is discarded for non-ECDHE ciphers prior to TLS
  132. // 1.3, to account for the historical key_exchange_info field. See
  133. // https://crbug.com/639421.
  134. TEST_F(HttpResponseInfoTest, LegacyKeyExchangeInfoDHE) {
  135. response_info_.ssl_info.cert =
  136. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  137. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
  138. &response_info_.ssl_info.connection_status);
  139. SSLConnectionStatusSetCipherSuite(
  140. 0x0093 /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */,
  141. &response_info_.ssl_info.connection_status);
  142. response_info_.ssl_info.key_exchange_group = 1024;
  143. net::HttpResponseInfo restored_response_info;
  144. PickleAndRestore(response_info_, &restored_response_info);
  145. EXPECT_EQ(0, restored_response_info.ssl_info.key_exchange_group);
  146. }
  147. // Test that key_exchange_group is discarded for unknown ciphers prior to TLS
  148. // 1.3, to account for the historical key_exchange_info field. See
  149. // https://crbug.com/639421.
  150. TEST_F(HttpResponseInfoTest, LegacyKeyExchangeInfoUnknown) {
  151. response_info_.ssl_info.cert =
  152. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  153. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
  154. &response_info_.ssl_info.connection_status);
  155. SSLConnectionStatusSetCipherSuite(0xffff,
  156. &response_info_.ssl_info.connection_status);
  157. response_info_.ssl_info.key_exchange_group = 1024;
  158. net::HttpResponseInfo restored_response_info;
  159. PickleAndRestore(response_info_, &restored_response_info);
  160. EXPECT_EQ(0, restored_response_info.ssl_info.key_exchange_group);
  161. }
  162. // Test that peer_signature_algorithm is preserved.
  163. TEST_F(HttpResponseInfoTest, PeerSignatureAlgorithm) {
  164. response_info_.ssl_info.cert =
  165. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  166. response_info_.ssl_info.peer_signature_algorithm =
  167. 0x0804; // rsa_pss_rsae_sha256
  168. net::HttpResponseInfo restored_response_info;
  169. PickleAndRestore(response_info_, &restored_response_info);
  170. EXPECT_EQ(0x0804, restored_response_info.ssl_info.peer_signature_algorithm);
  171. }
  172. // Test that encrypted_client_hello is preserved.
  173. TEST_F(HttpResponseInfoTest, EncryptedClientHello) {
  174. response_info_.ssl_info.cert =
  175. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  176. {
  177. net::HttpResponseInfo restored_response_info;
  178. PickleAndRestore(response_info_, &restored_response_info);
  179. EXPECT_FALSE(restored_response_info.ssl_info.encrypted_client_hello);
  180. }
  181. response_info_.ssl_info.encrypted_client_hello = true;
  182. {
  183. net::HttpResponseInfo restored_response_info;
  184. PickleAndRestore(response_info_, &restored_response_info);
  185. EXPECT_TRUE(restored_response_info.ssl_info.encrypted_client_hello);
  186. }
  187. }
  188. // Tests that cache entries loaded over SSLv3 (no longer supported) are dropped.
  189. TEST_F(HttpResponseInfoTest, FailsInitFromPickleWithSSLV3) {
  190. // A valid certificate is needed for ssl_info.is_valid() to be true.
  191. response_info_.ssl_info.cert =
  192. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  193. // Non-SSLv3 versions should succeed.
  194. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
  195. &response_info_.ssl_info.connection_status);
  196. base::Pickle tls12_pickle;
  197. response_info_.Persist(&tls12_pickle, false, false);
  198. bool truncated = false;
  199. net::HttpResponseInfo restored_tls12_response_info;
  200. EXPECT_TRUE(
  201. restored_tls12_response_info.InitFromPickle(tls12_pickle, &truncated));
  202. EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_2,
  203. SSLConnectionStatusToVersion(
  204. restored_tls12_response_info.ssl_info.connection_status));
  205. EXPECT_FALSE(truncated);
  206. // SSLv3 should fail.
  207. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_SSL3,
  208. &response_info_.ssl_info.connection_status);
  209. base::Pickle ssl3_pickle;
  210. response_info_.Persist(&ssl3_pickle, false, false);
  211. net::HttpResponseInfo restored_ssl3_response_info;
  212. EXPECT_FALSE(
  213. restored_ssl3_response_info.InitFromPickle(ssl3_pickle, &truncated));
  214. }
  215. // Test that `dns_aliases` is preserved.
  216. TEST_F(HttpResponseInfoTest, DnsAliases) {
  217. response_info_.dns_aliases = {"alias1", "alias2", "alias3"};
  218. net::HttpResponseInfo restored_response_info;
  219. PickleAndRestore(response_info_, &restored_response_info);
  220. EXPECT_THAT(restored_response_info.dns_aliases,
  221. testing::ElementsAre("alias1", "alias2", "alias3"));
  222. }
  223. // Test that an empty `dns_aliases` is preserved and doesn't throw an error.
  224. TEST_F(HttpResponseInfoTest, EmptyDnsAliases) {
  225. response_info_.dns_aliases = {};
  226. net::HttpResponseInfo restored_response_info;
  227. PickleAndRestore(response_info_, &restored_response_info);
  228. EXPECT_TRUE(restored_response_info.dns_aliases.empty());
  229. }
  230. } // namespace
  231. } // namespace net