http_server_properties_manager_unittest.cc 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199
  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. #include "net/http/http_server_properties_manager.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/feature_list.h"
  9. #include "base/json/json_reader.h"
  10. #include "base/json/json_writer.h"
  11. #include "base/memory/ptr_util.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/run_loop.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "base/strings/stringprintf.h"
  16. #include "base/task/single_thread_task_runner.h"
  17. #include "base/test/bind.h"
  18. #include "base/test/scoped_feature_list.h"
  19. #include "base/threading/thread_task_runner_handle.h"
  20. #include "base/time/default_tick_clock.h"
  21. #include "base/time/time.h"
  22. #include "base/values.h"
  23. #include "net/base/features.h"
  24. #include "net/base/ip_address.h"
  25. #include "net/base/schemeful_site.h"
  26. #include "net/http/http_network_session.h"
  27. #include "net/http/http_server_properties.h"
  28. #include "net/test/test_with_task_environment.h"
  29. #include "testing/gmock/include/gmock/gmock.h"
  30. #include "testing/gtest/include/gtest/gtest.h"
  31. #include "url/gurl.h"
  32. namespace net {
  33. namespace {
  34. using base::StringPrintf;
  35. using ::testing::_;
  36. using ::testing::AtLeast;
  37. using ::testing::Invoke;
  38. using ::testing::Mock;
  39. using ::testing::StrictMock;
  40. enum class NetworkIsolationKeyMode {
  41. kDisabled,
  42. kEnabled,
  43. };
  44. const NetworkIsolationKeyMode kNetworkIsolationKeyModes[] = {
  45. NetworkIsolationKeyMode::kDisabled,
  46. NetworkIsolationKeyMode::kEnabled,
  47. };
  48. std::unique_ptr<base::test::ScopedFeatureList> SetNetworkIsolationKeyMode(
  49. NetworkIsolationKeyMode mode) {
  50. auto feature_list = std::make_unique<base::test::ScopedFeatureList>();
  51. switch (mode) {
  52. case NetworkIsolationKeyMode::kDisabled:
  53. feature_list->InitAndDisableFeature(
  54. features::kPartitionHttpServerPropertiesByNetworkIsolationKey);
  55. break;
  56. case NetworkIsolationKeyMode::kEnabled:
  57. feature_list->InitAndEnableFeature(
  58. features::kPartitionHttpServerPropertiesByNetworkIsolationKey);
  59. break;
  60. }
  61. return feature_list;
  62. }
  63. class MockPrefDelegate : public net::HttpServerProperties::PrefDelegate {
  64. public:
  65. MockPrefDelegate() = default;
  66. MockPrefDelegate(const MockPrefDelegate&) = delete;
  67. MockPrefDelegate& operator=(const MockPrefDelegate&) = delete;
  68. ~MockPrefDelegate() override = default;
  69. // HttpServerProperties::PrefDelegate implementation.
  70. const base::Value* GetServerProperties() const override { return &prefs_; }
  71. void SetServerProperties(const base::Value& value,
  72. base::OnceClosure callback) override {
  73. prefs_.DictClear();
  74. prefs_.MergeDictionary(&value);
  75. ++num_pref_updates_;
  76. if (!prefs_changed_callback_.is_null())
  77. std::move(prefs_changed_callback_).Run();
  78. if (!extra_prefs_changed_callback_.is_null())
  79. std::move(extra_prefs_changed_callback_).Run();
  80. set_properties_callback_ = std::move(callback);
  81. }
  82. void WaitForPrefLoad(base::OnceClosure callback) override {
  83. CHECK(prefs_changed_callback_.is_null());
  84. prefs_changed_callback_ = std::move(callback);
  85. }
  86. void InitializePrefs(const base::Value& value) {
  87. ASSERT_FALSE(prefs_changed_callback_.is_null());
  88. prefs_.DictClear();
  89. prefs_.MergeDictionary(&value);
  90. std::move(prefs_changed_callback_).Run();
  91. }
  92. int GetAndClearNumPrefUpdates() {
  93. int out = num_pref_updates_;
  94. num_pref_updates_ = 0;
  95. return out;
  96. }
  97. // Additional callback to call when prefs are updated, used to check prefs are
  98. // updated on destruction.
  99. void set_extra_update_prefs_callback(base::OnceClosure callback) {
  100. extra_prefs_changed_callback_ = std::move(callback);
  101. }
  102. // Returns the base::OnceCallback, if any, passed to the last call to
  103. // SetServerProperties().
  104. base::OnceClosure GetSetPropertiesCallback() {
  105. return std::move(set_properties_callback_);
  106. }
  107. private:
  108. base::Value prefs_ = base::Value(base::Value::Type::DICTIONARY);
  109. base::OnceClosure prefs_changed_callback_;
  110. base::OnceClosure extra_prefs_changed_callback_;
  111. int num_pref_updates_ = 0;
  112. base::OnceClosure set_properties_callback_;
  113. };
  114. // Converts |server_info_map| to a base::Value by running it through an
  115. // HttpServerPropertiesManager. Other fields are left empty.
  116. base::Value ServerInfoMapToValue(
  117. const HttpServerProperties::ServerInfoMap& server_info_map) {
  118. std::unique_ptr<MockPrefDelegate> pref_delegate =
  119. std::make_unique<MockPrefDelegate>();
  120. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  121. // Callback that shouldn't be invoked - this method short-circuits loading
  122. // prefs by calling HttpServerPropertiesManager::WriteToPrefs() before prefs
  123. // are loaded.
  124. HttpServerPropertiesManager::OnPrefsLoadedCallback on_prefs_loaded_callback =
  125. base::BindOnce(
  126. [](std::unique_ptr<HttpServerProperties::ServerInfoMap>
  127. server_info_map,
  128. const IPAddress& last_quic_address,
  129. std::unique_ptr<HttpServerProperties::QuicServerInfoMap>
  130. quic_server_info_map,
  131. std::unique_ptr<BrokenAlternativeServiceList>
  132. broken_alternative_service_list,
  133. std::unique_ptr<RecentlyBrokenAlternativeServices>
  134. recently_broken_alternative_services) { ADD_FAILURE(); });
  135. HttpServerPropertiesManager manager(
  136. std::move(pref_delegate), std::move(on_prefs_loaded_callback),
  137. 10 /* max_server_configs_stored_in_properties */, nullptr /* net_log */,
  138. base::DefaultTickClock::GetInstance());
  139. manager.WriteToPrefs(
  140. server_info_map, HttpServerPropertiesManager::GetCannonicalSuffix(),
  141. IPAddress() /* last_quic_address */,
  142. HttpServerProperties::QuicServerInfoMap(10),
  143. BrokenAlternativeServiceList(), RecentlyBrokenAlternativeServices(10),
  144. base::OnceClosure());
  145. return unowned_pref_delegate->GetServerProperties()->Clone();
  146. }
  147. // Does the inverse of ServerInfoMapToValue(). Ignores fields other than the
  148. // ServerInfoMap.
  149. std::unique_ptr<HttpServerProperties::ServerInfoMap> ValueToServerInfoMap(
  150. const base::Value& value) {
  151. if (!value.is_dict())
  152. return nullptr;
  153. std::unique_ptr<MockPrefDelegate> pref_delegate =
  154. std::make_unique<MockPrefDelegate>();
  155. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  156. std::unique_ptr<HttpServerProperties::ServerInfoMap> out;
  157. bool callback_invoked = false;
  158. HttpServerPropertiesManager::OnPrefsLoadedCallback on_prefs_loaded_callback =
  159. base::BindLambdaForTesting(
  160. [&](std::unique_ptr<HttpServerProperties::ServerInfoMap>
  161. server_info_map,
  162. const IPAddress& last_quic_address,
  163. std::unique_ptr<HttpServerProperties::QuicServerInfoMap>
  164. quic_server_info_map,
  165. std::unique_ptr<BrokenAlternativeServiceList>
  166. broken_alternative_service_list,
  167. std::unique_ptr<RecentlyBrokenAlternativeServices>
  168. recently_broken_alternative_services) {
  169. ASSERT_FALSE(callback_invoked);
  170. callback_invoked = true;
  171. out = std::move(server_info_map);
  172. });
  173. HttpServerPropertiesManager manager(
  174. std::move(pref_delegate), std::move(on_prefs_loaded_callback),
  175. 10 /* max_server_configs_stored_in_properties */, nullptr /* net_log */,
  176. base::DefaultTickClock::GetInstance());
  177. unowned_pref_delegate->InitializePrefs(value);
  178. EXPECT_TRUE(callback_invoked);
  179. return out;
  180. }
  181. } // namespace
  182. class HttpServerPropertiesManagerTest : public testing::Test,
  183. public WithTaskEnvironment {
  184. public:
  185. HttpServerPropertiesManagerTest(const HttpServerPropertiesManagerTest&) =
  186. delete;
  187. HttpServerPropertiesManagerTest& operator=(
  188. const HttpServerPropertiesManagerTest&) = delete;
  189. protected:
  190. HttpServerPropertiesManagerTest()
  191. : WithTaskEnvironment(
  192. base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  193. void SetUp() override {
  194. one_day_from_now_ = base::Time::Now() + base::Days(1);
  195. advertised_versions_ = DefaultSupportedQuicVersions();
  196. auto pref_delegate = std::make_unique<MockPrefDelegate>();
  197. pref_delegate_ = pref_delegate.get();
  198. http_server_props_ = std::make_unique<HttpServerProperties>(
  199. std::move(pref_delegate), /*net_log=*/nullptr, GetMockTickClock());
  200. EXPECT_FALSE(http_server_props_->IsInitialized());
  201. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  202. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  203. }
  204. // Wrapper around |pref_delegate_|'s InitializePrefs() method that has a
  205. // couple extra expectations about whether any tasks are posted, and if a pref
  206. // update is queued.
  207. //
  208. // |expect_pref_update| should be true if a pref update is expected to be
  209. // queued in response to the load.
  210. void InitializePrefs(
  211. const base::Value& dict = base::Value(base::Value::Type::DICTIONARY),
  212. bool expect_pref_update = false) {
  213. EXPECT_FALSE(http_server_props_->IsInitialized());
  214. pref_delegate_->InitializePrefs(dict);
  215. EXPECT_TRUE(http_server_props_->IsInitialized());
  216. if (!expect_pref_update) {
  217. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  218. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  219. } else {
  220. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  221. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  222. FastForwardUntilNoTasksRemain();
  223. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  224. }
  225. }
  226. void TearDown() override {
  227. // Run pending non-delayed tasks but don't FastForwardUntilNoTasksRemain()
  228. // as some delayed tasks may forever repost (e.g. because impl doesn't use a
  229. // mock clock and doesn't see timings as having expired, ref.
  230. // HttpServerProperties::
  231. // ScheduleBrokenAlternateProtocolMappingsExpiration()).
  232. base::RunLoop().RunUntilIdle();
  233. http_server_props_.reset();
  234. }
  235. bool HasAlternativeService(const url::SchemeHostPort& server,
  236. const NetworkIsolationKey& network_isolation_key) {
  237. const AlternativeServiceInfoVector alternative_service_info_vector =
  238. http_server_props_->GetAlternativeServiceInfos(server,
  239. network_isolation_key);
  240. return !alternative_service_info_vector.empty();
  241. }
  242. // Returns a dictionary with only the version field populated.
  243. static base::Value DictWithVersion() {
  244. base::Value::Dict http_server_properties_dict;
  245. http_server_properties_dict.Set("version", 5);
  246. return base::Value(std::move(http_server_properties_dict));
  247. }
  248. raw_ptr<MockPrefDelegate>
  249. pref_delegate_; // Owned by HttpServerPropertiesManager.
  250. std::unique_ptr<HttpServerProperties> http_server_props_;
  251. base::Time one_day_from_now_;
  252. quic::ParsedQuicVersionVector advertised_versions_;
  253. };
  254. TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
  255. base::Value::Dict server_pref_dict;
  256. // Set supports_spdy for www.google.com:65536.
  257. server_pref_dict.Set("supports_spdy", true);
  258. // Set up alternative_service for www.google.com:65536.
  259. base::Value::Dict alternative_service_dict;
  260. alternative_service_dict.Set("protocol_str", "h2");
  261. alternative_service_dict.Set("port", 80);
  262. base::Value::List alternative_service_list;
  263. alternative_service_list.Append(std::move(alternative_service_dict));
  264. server_pref_dict.Set("alternative_service",
  265. std::move(alternative_service_list));
  266. // Set up ServerNetworkStats for www.google.com:65536.
  267. base::Value::Dict stats;
  268. stats.Set("srtt", 10);
  269. server_pref_dict.Set("network_stats", std::move(stats));
  270. // Set the server preference for www.google.com:65536.
  271. base::Value::Dict servers_dict;
  272. servers_dict.Set("www.google.com:65536", std::move(server_pref_dict));
  273. base::Value::List servers_list;
  274. servers_list.Append(std::move(servers_dict));
  275. base::Value http_server_properties_dict = DictWithVersion();
  276. http_server_properties_dict.GetDict().Set("servers", std::move(servers_list));
  277. // Set quic_server_info for www.google.com:65536.
  278. base::Value::Dict quic_servers_dict;
  279. base::Value::Dict quic_server_pref_dict1;
  280. quic_server_pref_dict1.Set("server_info", "quic_server_info1");
  281. quic_servers_dict.Set("http://mail.google.com:65536",
  282. std::move(quic_server_pref_dict1));
  283. http_server_properties_dict.GetDict().Set("quic_servers",
  284. std::move(quic_servers_dict));
  285. // Set up the pref.
  286. InitializePrefs(http_server_properties_dict);
  287. // Verify that nothing is set.
  288. HostPortPair google_host_port_pair =
  289. HostPortPair::FromString("www.google.com:65536");
  290. url::SchemeHostPort gooler_server("http", google_host_port_pair.host(),
  291. google_host_port_pair.port());
  292. EXPECT_FALSE(http_server_props_->SupportsRequestPriority(
  293. gooler_server, NetworkIsolationKey()));
  294. EXPECT_FALSE(HasAlternativeService(gooler_server, NetworkIsolationKey()));
  295. const ServerNetworkStats* stats1 = http_server_props_->GetServerNetworkStats(
  296. gooler_server, NetworkIsolationKey());
  297. EXPECT_EQ(nullptr, stats1);
  298. EXPECT_EQ(0u, http_server_props_->quic_server_info_map().size());
  299. }
  300. TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
  301. base::Value::Dict server_pref_dict;
  302. // Set supports_spdy for www.google.com:80.
  303. server_pref_dict.Set("supports_spdy", true);
  304. // Set up alternative_service for www.google.com:80.
  305. base::Value::Dict alternative_service_dict;
  306. alternative_service_dict.Set("protocol_str", "h2");
  307. alternative_service_dict.Set("port", 65536);
  308. base::Value::List alternative_service_list;
  309. alternative_service_list.Append(std::move(alternative_service_dict));
  310. server_pref_dict.Set("alternative_service",
  311. std::move(alternative_service_list));
  312. // Set the server preference for www.google.com:80.
  313. base::Value::Dict servers_dict;
  314. servers_dict.Set("www.google.com:80", std::move(server_pref_dict));
  315. base::Value::List servers_list;
  316. servers_list.Append(std::move(servers_dict));
  317. base::Value http_server_properties_dict = DictWithVersion();
  318. http_server_properties_dict.GetDict().Set("servers", std::move(servers_list));
  319. // Set up the pref.
  320. InitializePrefs(http_server_properties_dict);
  321. // Verify alternative service is not set.
  322. EXPECT_FALSE(
  323. HasAlternativeService(url::SchemeHostPort("http", "www.google.com", 80),
  324. NetworkIsolationKey()));
  325. }
  326. TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) {
  327. InitializePrefs();
  328. // Add mail.google.com:443 as a supporting spdy server.
  329. url::SchemeHostPort spdy_server("https", "mail.google.com", 443);
  330. EXPECT_FALSE(http_server_props_->SupportsRequestPriority(
  331. spdy_server, NetworkIsolationKey()));
  332. http_server_props_->SetSupportsSpdy(spdy_server, NetworkIsolationKey(), true);
  333. // Setting the value to the same thing again should not trigger another pref
  334. // update.
  335. http_server_props_->SetSupportsSpdy(spdy_server, NetworkIsolationKey(), true);
  336. // Run the task.
  337. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  338. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  339. FastForwardUntilNoTasksRemain();
  340. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  341. // Setting the value to the same thing again should not trigger another pref
  342. // update.
  343. http_server_props_->SetSupportsSpdy(spdy_server, NetworkIsolationKey(), true);
  344. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  345. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  346. EXPECT_TRUE(http_server_props_->SupportsRequestPriority(
  347. spdy_server, NetworkIsolationKey()));
  348. }
  349. // Regression test for crbug.com/670519. Test that there is only one pref update
  350. // scheduled if multiple updates happen in a given time period. Subsequent pref
  351. // update could also be scheduled once the previous scheduled update is
  352. // completed.
  353. TEST_F(HttpServerPropertiesManagerTest,
  354. SinglePrefUpdateForTwoSpdyServerCacheChanges) {
  355. InitializePrefs();
  356. // Post an update task. SetSupportsSpdy calls ScheduleUpdatePrefs with a delay
  357. // of 60ms.
  358. url::SchemeHostPort spdy_server("https", "mail.google.com", 443);
  359. EXPECT_FALSE(http_server_props_->SupportsRequestPriority(
  360. spdy_server, NetworkIsolationKey()));
  361. http_server_props_->SetSupportsSpdy(spdy_server, NetworkIsolationKey(), true);
  362. // The pref update task should be scheduled.
  363. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  364. // Move forward the task runner short by 20ms.
  365. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting() -
  366. base::Milliseconds(20));
  367. // Set another spdy server to trigger another call to
  368. // ScheduleUpdatePrefs. There should be no new update posted.
  369. url::SchemeHostPort spdy_server2("https", "drive.google.com", 443);
  370. http_server_props_->SetSupportsSpdy(spdy_server2, NetworkIsolationKey(),
  371. true);
  372. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  373. // Move forward the extra 20ms. The pref update should be executed.
  374. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  375. FastForwardBy(base::Milliseconds(20));
  376. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  377. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  378. EXPECT_TRUE(http_server_props_->SupportsRequestPriority(
  379. spdy_server, NetworkIsolationKey()));
  380. EXPECT_TRUE(http_server_props_->SupportsRequestPriority(
  381. spdy_server2, NetworkIsolationKey()));
  382. // Set the third spdy server to trigger one more call to
  383. // ScheduleUpdatePrefs. A new update task should be posted now since the
  384. // previous one is completed.
  385. url::SchemeHostPort spdy_server3("https", "maps.google.com", 443);
  386. http_server_props_->SetSupportsSpdy(spdy_server3, NetworkIsolationKey(),
  387. true);
  388. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  389. // Run the task.
  390. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  391. FastForwardUntilNoTasksRemain();
  392. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  393. }
  394. TEST_F(HttpServerPropertiesManagerTest, GetAlternativeServiceInfos) {
  395. InitializePrefs();
  396. url::SchemeHostPort spdy_server_mail("http", "mail.google.com", 80);
  397. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  398. const AlternativeService alternative_service(kProtoHTTP2, "mail.google.com",
  399. 443);
  400. http_server_props_->SetHttp2AlternativeService(
  401. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  402. one_day_from_now_);
  403. // ExpectScheduleUpdatePrefs() should be called only once.
  404. http_server_props_->SetHttp2AlternativeService(
  405. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  406. one_day_from_now_);
  407. // Run the task.
  408. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  409. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  410. FastForwardUntilNoTasksRemain();
  411. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  412. AlternativeServiceInfoVector alternative_service_info_vector =
  413. http_server_props_->GetAlternativeServiceInfos(spdy_server_mail,
  414. NetworkIsolationKey());
  415. ASSERT_EQ(1u, alternative_service_info_vector.size());
  416. EXPECT_EQ(alternative_service,
  417. alternative_service_info_vector[0].alternative_service());
  418. }
  419. TEST_F(HttpServerPropertiesManagerTest, SetAlternativeServices) {
  420. InitializePrefs();
  421. url::SchemeHostPort spdy_server_mail("http", "mail.google.com", 80);
  422. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  423. AlternativeServiceInfoVector alternative_service_info_vector;
  424. const AlternativeService alternative_service1(kProtoHTTP2, "mail.google.com",
  425. 443);
  426. alternative_service_info_vector.push_back(
  427. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  428. alternative_service1, one_day_from_now_));
  429. const AlternativeService alternative_service2(kProtoQUIC, "mail.google.com",
  430. 1234);
  431. alternative_service_info_vector.push_back(
  432. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  433. alternative_service2, one_day_from_now_, advertised_versions_));
  434. http_server_props_->SetAlternativeServices(
  435. spdy_server_mail, NetworkIsolationKey(), alternative_service_info_vector);
  436. // ExpectScheduleUpdatePrefs() should be called only once.
  437. http_server_props_->SetAlternativeServices(
  438. spdy_server_mail, NetworkIsolationKey(), alternative_service_info_vector);
  439. // Run the task.
  440. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  441. FastForwardUntilNoTasksRemain();
  442. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  443. AlternativeServiceInfoVector alternative_service_info_vector2 =
  444. http_server_props_->GetAlternativeServiceInfos(spdy_server_mail,
  445. NetworkIsolationKey());
  446. ASSERT_EQ(2u, alternative_service_info_vector2.size());
  447. EXPECT_EQ(alternative_service1,
  448. alternative_service_info_vector2[0].alternative_service());
  449. EXPECT_EQ(alternative_service2,
  450. alternative_service_info_vector2[1].alternative_service());
  451. }
  452. TEST_F(HttpServerPropertiesManagerTest, SetAlternativeServicesEmpty) {
  453. InitializePrefs();
  454. url::SchemeHostPort spdy_server_mail("http", "mail.google.com", 80);
  455. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  456. const AlternativeService alternative_service(kProtoHTTP2, "mail.google.com",
  457. 443);
  458. http_server_props_->SetAlternativeServices(
  459. spdy_server_mail, NetworkIsolationKey(), AlternativeServiceInfoVector());
  460. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  461. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  462. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  463. }
  464. TEST_F(HttpServerPropertiesManagerTest, ConfirmAlternativeService) {
  465. InitializePrefs();
  466. url::SchemeHostPort spdy_server_mail;
  467. AlternativeService alternative_service;
  468. spdy_server_mail = url::SchemeHostPort("http", "mail.google.com", 80);
  469. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  470. alternative_service = AlternativeService(kProtoHTTP2, "mail.google.com", 443);
  471. http_server_props_->SetHttp2AlternativeService(
  472. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  473. one_day_from_now_);
  474. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  475. alternative_service, NetworkIsolationKey()));
  476. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  477. alternative_service, NetworkIsolationKey()));
  478. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  479. http_server_props_->MarkAlternativeServiceBroken(alternative_service,
  480. NetworkIsolationKey());
  481. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  482. alternative_service, NetworkIsolationKey()));
  483. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  484. alternative_service, NetworkIsolationKey()));
  485. // In addition to the pref update task, there's now a task to mark the
  486. // alternative service as no longer broken.
  487. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  488. http_server_props_->ConfirmAlternativeService(alternative_service,
  489. NetworkIsolationKey());
  490. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  491. alternative_service, NetworkIsolationKey()));
  492. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  493. alternative_service, NetworkIsolationKey()));
  494. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  495. // Run the task.
  496. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  497. FastForwardUntilNoTasksRemain();
  498. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  499. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  500. alternative_service, NetworkIsolationKey()));
  501. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  502. alternative_service, NetworkIsolationKey()));
  503. }
  504. // Check the case that prefs are loaded only after setting alternative service
  505. // info. Prefs should not be written until after the load happens.
  506. TEST_F(HttpServerPropertiesManagerTest, LateLoadAlternativeServiceInfo) {
  507. url::SchemeHostPort spdy_server_mail("http", "mail.google.com", 80);
  508. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  509. const AlternativeService alternative_service(kProtoHTTP2, "mail.google.com",
  510. 443);
  511. http_server_props_->SetHttp2AlternativeService(
  512. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  513. one_day_from_now_);
  514. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  515. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  516. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  517. AlternativeServiceInfoVector alternative_service_info_vector =
  518. http_server_props_->GetAlternativeServiceInfos(spdy_server_mail,
  519. NetworkIsolationKey());
  520. ASSERT_EQ(1u, alternative_service_info_vector.size());
  521. EXPECT_EQ(alternative_service,
  522. alternative_service_info_vector[0].alternative_service());
  523. // Initializing prefs does not result in a task to write the prefs.
  524. InitializePrefs(base::Value(base::Value::Type::DICTIONARY),
  525. true /* expect_pref_update */);
  526. alternative_service_info_vector =
  527. http_server_props_->GetAlternativeServiceInfos(spdy_server_mail,
  528. NetworkIsolationKey());
  529. EXPECT_EQ(1u, alternative_service_info_vector.size());
  530. // Updating the entry should result in a task to save prefs. Have to at least
  531. // double (or half) the lifetime, to ensure the change triggers a save to
  532. // prefs.
  533. http_server_props_->SetHttp2AlternativeService(
  534. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  535. one_day_from_now_ + base::Days(2));
  536. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  537. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  538. FastForwardUntilNoTasksRemain();
  539. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  540. alternative_service_info_vector =
  541. http_server_props_->GetAlternativeServiceInfos(spdy_server_mail,
  542. NetworkIsolationKey());
  543. EXPECT_EQ(1u, alternative_service_info_vector.size());
  544. }
  545. // Check the case that prefs are cleared before they're loaded.
  546. TEST_F(HttpServerPropertiesManagerTest,
  547. ClearPrefsBeforeLoadAlternativeServiceInfo) {
  548. url::SchemeHostPort spdy_server_mail("http", "mail.google.com", 80);
  549. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  550. const AlternativeService alternative_service(kProtoHTTP2, "mail.google.com",
  551. 443);
  552. http_server_props_->SetHttp2AlternativeService(
  553. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  554. one_day_from_now_);
  555. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  556. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  557. FastForwardUntilNoTasksRemain();
  558. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  559. AlternativeServiceInfoVector alternative_service_info_vector =
  560. http_server_props_->GetAlternativeServiceInfos(spdy_server_mail,
  561. NetworkIsolationKey());
  562. ASSERT_EQ(1u, alternative_service_info_vector.size());
  563. EXPECT_EQ(alternative_service,
  564. alternative_service_info_vector[0].alternative_service());
  565. // Clearing prefs should result in a task to write the prefs.
  566. bool callback_invoked_ = false;
  567. http_server_props_->Clear(base::BindOnce(
  568. [](bool* callback_invoked) {
  569. EXPECT_FALSE(*callback_invoked);
  570. *callback_invoked = true;
  571. },
  572. &callback_invoked_));
  573. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  574. EXPECT_FALSE(callback_invoked_);
  575. std::move(pref_delegate_->GetSetPropertiesCallback()).Run();
  576. EXPECT_TRUE(callback_invoked_);
  577. alternative_service_info_vector =
  578. http_server_props_->GetAlternativeServiceInfos(spdy_server_mail,
  579. NetworkIsolationKey());
  580. EXPECT_EQ(0u, alternative_service_info_vector.size());
  581. // Re-creating the entry should result in a task to save prefs.
  582. http_server_props_->SetHttp2AlternativeService(
  583. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  584. one_day_from_now_);
  585. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  586. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  587. FastForwardUntilNoTasksRemain();
  588. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  589. alternative_service_info_vector =
  590. http_server_props_->GetAlternativeServiceInfos(spdy_server_mail,
  591. NetworkIsolationKey());
  592. EXPECT_EQ(1u, alternative_service_info_vector.size());
  593. }
  594. TEST_F(HttpServerPropertiesManagerTest,
  595. ConfirmBrokenUntilDefaultNetworkChanges) {
  596. InitializePrefs();
  597. url::SchemeHostPort spdy_server_mail;
  598. AlternativeService alternative_service;
  599. spdy_server_mail = url::SchemeHostPort("http", "mail.google.com", 80);
  600. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  601. alternative_service = AlternativeService(kProtoHTTP2, "mail.google.com", 443);
  602. http_server_props_->SetHttp2AlternativeService(
  603. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  604. one_day_from_now_);
  605. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  606. alternative_service, NetworkIsolationKey()));
  607. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  608. alternative_service, NetworkIsolationKey()));
  609. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  610. http_server_props_->MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
  611. alternative_service, NetworkIsolationKey());
  612. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  613. alternative_service, NetworkIsolationKey()));
  614. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  615. alternative_service, NetworkIsolationKey()));
  616. // In addition to the pref update task, there's now a task to mark the
  617. // alternative service as no longer broken.
  618. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  619. http_server_props_->ConfirmAlternativeService(alternative_service,
  620. NetworkIsolationKey());
  621. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  622. alternative_service, NetworkIsolationKey()));
  623. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  624. alternative_service, NetworkIsolationKey()));
  625. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  626. // Run the task.
  627. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  628. FastForwardUntilNoTasksRemain();
  629. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  630. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  631. alternative_service, NetworkIsolationKey()));
  632. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  633. alternative_service, NetworkIsolationKey()));
  634. }
  635. TEST_F(HttpServerPropertiesManagerTest,
  636. OnDefaultNetworkChangedWithBrokenUntilDefaultNetworkChanges) {
  637. InitializePrefs();
  638. url::SchemeHostPort spdy_server_mail;
  639. AlternativeService alternative_service;
  640. spdy_server_mail = url::SchemeHostPort("http", "mail.google.com", 80);
  641. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  642. alternative_service = AlternativeService(kProtoHTTP2, "mail.google.com", 443);
  643. http_server_props_->SetHttp2AlternativeService(
  644. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  645. one_day_from_now_);
  646. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  647. alternative_service, NetworkIsolationKey()));
  648. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  649. alternative_service, NetworkIsolationKey()));
  650. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  651. http_server_props_->MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
  652. alternative_service, NetworkIsolationKey());
  653. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  654. alternative_service, NetworkIsolationKey()));
  655. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  656. alternative_service, NetworkIsolationKey()));
  657. // In addition to the pref update task, there's now a task to mark the
  658. // alternative service as no longer broken.
  659. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  660. http_server_props_->OnDefaultNetworkChanged();
  661. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  662. alternative_service, NetworkIsolationKey()));
  663. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  664. alternative_service, NetworkIsolationKey()));
  665. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  666. // Run the task.
  667. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  668. FastForwardUntilNoTasksRemain();
  669. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  670. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  671. alternative_service, NetworkIsolationKey()));
  672. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  673. alternative_service, NetworkIsolationKey()));
  674. }
  675. TEST_F(HttpServerPropertiesManagerTest, OnDefaultNetworkChangedWithBrokenOnly) {
  676. InitializePrefs();
  677. url::SchemeHostPort spdy_server_mail;
  678. AlternativeService alternative_service;
  679. spdy_server_mail = url::SchemeHostPort("http", "mail.google.com", 80);
  680. EXPECT_FALSE(HasAlternativeService(spdy_server_mail, NetworkIsolationKey()));
  681. alternative_service = AlternativeService(kProtoHTTP2, "mail.google.com", 443);
  682. http_server_props_->SetHttp2AlternativeService(
  683. spdy_server_mail, NetworkIsolationKey(), alternative_service,
  684. one_day_from_now_);
  685. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  686. alternative_service, NetworkIsolationKey()));
  687. EXPECT_FALSE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  688. alternative_service, NetworkIsolationKey()));
  689. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  690. http_server_props_->MarkAlternativeServiceBroken(alternative_service,
  691. NetworkIsolationKey());
  692. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  693. alternative_service, NetworkIsolationKey()));
  694. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  695. alternative_service, NetworkIsolationKey()));
  696. // In addition to the pref update task, there's now a task to mark the
  697. // alternative service as no longer broken.
  698. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  699. http_server_props_->OnDefaultNetworkChanged();
  700. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  701. alternative_service, NetworkIsolationKey()));
  702. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  703. alternative_service, NetworkIsolationKey()));
  704. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  705. // Run the task.
  706. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  707. FastForwardUntilNoTasksRemain();
  708. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  709. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  710. alternative_service, NetworkIsolationKey()));
  711. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  712. alternative_service, NetworkIsolationKey()));
  713. }
  714. TEST_F(HttpServerPropertiesManagerTest, LastLocalAddressWhenQuicWorked) {
  715. InitializePrefs();
  716. IPAddress actual_address(127, 0, 0, 1);
  717. EXPECT_FALSE(http_server_props_->HasLastLocalAddressWhenQuicWorked());
  718. EXPECT_FALSE(
  719. http_server_props_->WasLastLocalAddressWhenQuicWorked(actual_address));
  720. http_server_props_->SetLastLocalAddressWhenQuicWorked(actual_address);
  721. // Another task should not be scheduled.
  722. http_server_props_->SetLastLocalAddressWhenQuicWorked(actual_address);
  723. // Run the task.
  724. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  725. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  726. FastForwardUntilNoTasksRemain();
  727. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  728. EXPECT_TRUE(
  729. http_server_props_->WasLastLocalAddressWhenQuicWorked(actual_address));
  730. // Another task should not be scheduled.
  731. http_server_props_->SetLastLocalAddressWhenQuicWorked(actual_address);
  732. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  733. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  734. }
  735. TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) {
  736. InitializePrefs();
  737. url::SchemeHostPort mail_server("http", "mail.google.com", 80);
  738. const ServerNetworkStats* stats = http_server_props_->GetServerNetworkStats(
  739. mail_server, NetworkIsolationKey());
  740. EXPECT_EQ(nullptr, stats);
  741. ServerNetworkStats stats1;
  742. stats1.srtt = base::Microseconds(10);
  743. http_server_props_->SetServerNetworkStats(mail_server, NetworkIsolationKey(),
  744. stats1);
  745. // Another task should not be scheduled.
  746. http_server_props_->SetServerNetworkStats(mail_server, NetworkIsolationKey(),
  747. stats1);
  748. // Run the task.
  749. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  750. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  751. FastForwardUntilNoTasksRemain();
  752. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  753. // Another task should not be scheduled.
  754. http_server_props_->SetServerNetworkStats(mail_server, NetworkIsolationKey(),
  755. stats1);
  756. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  757. EXPECT_EQ(GetPendingMainThreadTaskCount(), 0u);
  758. const ServerNetworkStats* stats2 = http_server_props_->GetServerNetworkStats(
  759. mail_server, NetworkIsolationKey());
  760. EXPECT_EQ(10, stats2->srtt.ToInternalValue());
  761. http_server_props_->ClearServerNetworkStats(mail_server,
  762. NetworkIsolationKey());
  763. // Run the task.
  764. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  765. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  766. FastForwardUntilNoTasksRemain();
  767. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  768. EXPECT_EQ(nullptr, http_server_props_->GetServerNetworkStats(
  769. mail_server, NetworkIsolationKey()));
  770. }
  771. TEST_F(HttpServerPropertiesManagerTest, QuicServerInfo) {
  772. InitializePrefs();
  773. quic::QuicServerId mail_quic_server_id("mail.google.com", 80, false);
  774. EXPECT_EQ(nullptr, http_server_props_->GetQuicServerInfo(
  775. mail_quic_server_id, NetworkIsolationKey()));
  776. std::string quic_server_info1("quic_server_info1");
  777. http_server_props_->SetQuicServerInfo(
  778. mail_quic_server_id, NetworkIsolationKey(), quic_server_info1);
  779. // Another task should not be scheduled.
  780. http_server_props_->SetQuicServerInfo(
  781. mail_quic_server_id, NetworkIsolationKey(), quic_server_info1);
  782. // Run the task.
  783. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  784. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  785. FastForwardUntilNoTasksRemain();
  786. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  787. EXPECT_EQ(quic_server_info1, *http_server_props_->GetQuicServerInfo(
  788. mail_quic_server_id, NetworkIsolationKey()));
  789. // Another task should not be scheduled.
  790. http_server_props_->SetQuicServerInfo(
  791. mail_quic_server_id, NetworkIsolationKey(), quic_server_info1);
  792. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  793. EXPECT_EQ(0u, GetPendingMainThreadTaskCount());
  794. }
  795. TEST_F(HttpServerPropertiesManagerTest, Clear) {
  796. InitializePrefs();
  797. const url::SchemeHostPort spdy_server("https", "mail.google.com", 443);
  798. const IPAddress actual_address(127, 0, 0, 1);
  799. const quic::QuicServerId mail_quic_server_id("mail.google.com", 80, false);
  800. const std::string quic_server_info1("quic_server_info1");
  801. const AlternativeService alternative_service(kProtoHTTP2, "mail.google.com",
  802. 1234);
  803. const AlternativeService broken_alternative_service(
  804. kProtoHTTP2, "broken.google.com", 1234);
  805. AlternativeServiceInfoVector alt_svc_info_vector;
  806. alt_svc_info_vector.push_back(
  807. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  808. alternative_service, one_day_from_now_));
  809. alt_svc_info_vector.push_back(
  810. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  811. broken_alternative_service, one_day_from_now_));
  812. http_server_props_->SetAlternativeServices(spdy_server, NetworkIsolationKey(),
  813. alt_svc_info_vector);
  814. http_server_props_->MarkAlternativeServiceBroken(broken_alternative_service,
  815. NetworkIsolationKey());
  816. http_server_props_->SetSupportsSpdy(spdy_server, NetworkIsolationKey(), true);
  817. http_server_props_->SetLastLocalAddressWhenQuicWorked(actual_address);
  818. ServerNetworkStats stats;
  819. stats.srtt = base::Microseconds(10);
  820. http_server_props_->SetServerNetworkStats(spdy_server, NetworkIsolationKey(),
  821. stats);
  822. http_server_props_->SetQuicServerInfo(
  823. mail_quic_server_id, NetworkIsolationKey(), quic_server_info1);
  824. // Advance time by just enough so that the prefs update task is executed but
  825. // not the task to expire the brokenness of |broken_alternative_service|.
  826. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  827. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  828. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  829. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  830. broken_alternative_service, NetworkIsolationKey()));
  831. EXPECT_TRUE(http_server_props_->SupportsRequestPriority(
  832. spdy_server, NetworkIsolationKey()));
  833. EXPECT_TRUE(HasAlternativeService(spdy_server, NetworkIsolationKey()));
  834. EXPECT_TRUE(
  835. http_server_props_->WasLastLocalAddressWhenQuicWorked(actual_address));
  836. const ServerNetworkStats* stats1 = http_server_props_->GetServerNetworkStats(
  837. spdy_server, NetworkIsolationKey());
  838. EXPECT_EQ(10, stats1->srtt.ToInternalValue());
  839. EXPECT_EQ(quic_server_info1, *http_server_props_->GetQuicServerInfo(
  840. mail_quic_server_id, NetworkIsolationKey()));
  841. // Clear http server data, which should instantly update prefs.
  842. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  843. bool callback_invoked_ = false;
  844. http_server_props_->Clear(base::BindOnce(
  845. [](bool* callback_invoked) {
  846. EXPECT_FALSE(*callback_invoked);
  847. *callback_invoked = true;
  848. },
  849. &callback_invoked_));
  850. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  851. EXPECT_FALSE(callback_invoked_);
  852. std::move(pref_delegate_->GetSetPropertiesCallback()).Run();
  853. EXPECT_TRUE(callback_invoked_);
  854. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  855. broken_alternative_service, NetworkIsolationKey()));
  856. EXPECT_FALSE(http_server_props_->SupportsRequestPriority(
  857. spdy_server, NetworkIsolationKey()));
  858. EXPECT_FALSE(HasAlternativeService(spdy_server, NetworkIsolationKey()));
  859. EXPECT_FALSE(http_server_props_->HasLastLocalAddressWhenQuicWorked());
  860. const ServerNetworkStats* stats2 = http_server_props_->GetServerNetworkStats(
  861. spdy_server, NetworkIsolationKey());
  862. EXPECT_EQ(nullptr, stats2);
  863. EXPECT_EQ(nullptr, http_server_props_->GetQuicServerInfo(
  864. mail_quic_server_id, NetworkIsolationKey()));
  865. }
  866. // https://crbug.com/444956: Add 200 alternative_service servers followed by
  867. // supports_quic and verify we have read supports_quic from prefs.
  868. TEST_F(HttpServerPropertiesManagerTest, BadLastLocalAddressWhenQuicWorked) {
  869. base::Value::List servers_list;
  870. for (int i = 1; i <= 200; ++i) {
  871. // Set up alternative_service for www.google.com:i.
  872. base::Value::Dict server_dict;
  873. base::Value::Dict alternative_service_dict;
  874. alternative_service_dict.Set("protocol_str", "quic");
  875. alternative_service_dict.Set("port", i);
  876. base::Value::List alternative_service_list;
  877. alternative_service_list.Append(std::move(alternative_service_dict));
  878. server_dict.Set("alternative_service", std::move(alternative_service_list));
  879. server_dict.Set("server", StringPrintf("https://www.google.com:%d", i));
  880. server_dict.Set("isolation", base::Value(base::Value::Type::LIST));
  881. servers_list.Append(std::move(server_dict));
  882. }
  883. // Set the server preference for http://mail.google.com server.
  884. base::Value::Dict server_dict2;
  885. server_dict2.Set("server", "https://mail.google.com");
  886. server_dict2.Set("isolation", base::Value(base::Value::Type::LIST));
  887. servers_list.Append(std::move(server_dict2));
  888. base::Value http_server_properties_dict = DictWithVersion();
  889. http_server_properties_dict.GetDict().Set("servers", std::move(servers_list));
  890. // Set up SupportsQuic for 127.0.0.1
  891. base::Value::Dict supports_quic;
  892. supports_quic.Set("used_quic", true);
  893. supports_quic.Set("address", "127.0.0.1");
  894. http_server_properties_dict.GetDict().Set("supports_quic",
  895. std::move(supports_quic));
  896. // Set up the pref.
  897. InitializePrefs(http_server_properties_dict);
  898. // Verify alternative service.
  899. for (int i = 1; i <= 200; ++i) {
  900. GURL server_gurl;
  901. server_gurl = GURL(StringPrintf("https://www.google.com:%d", i));
  902. url::SchemeHostPort server(server_gurl);
  903. AlternativeServiceInfoVector alternative_service_info_vector =
  904. http_server_props_->GetAlternativeServiceInfos(server,
  905. NetworkIsolationKey());
  906. ASSERT_EQ(1u, alternative_service_info_vector.size());
  907. EXPECT_EQ(
  908. kProtoQUIC,
  909. alternative_service_info_vector[0].alternative_service().protocol);
  910. EXPECT_EQ(i, alternative_service_info_vector[0].alternative_service().port);
  911. }
  912. // Verify WasLastLocalAddressWhenQuicWorked.
  913. ASSERT_TRUE(http_server_props_->WasLastLocalAddressWhenQuicWorked(
  914. IPAddress::IPv4Localhost()));
  915. }
  916. TEST_F(HttpServerPropertiesManagerTest, UpdatePrefsWithCache) {
  917. InitializePrefs();
  918. const url::SchemeHostPort server_www("https", "www.google.com", 80);
  919. const url::SchemeHostPort server_mail("https", "mail.google.com", 80);
  920. // #1 & #2: Set alternate protocol.
  921. AlternativeServiceInfoVector alternative_service_info_vector;
  922. AlternativeService www_alternative_service1(kProtoHTTP2, "", 443);
  923. base::Time expiration1;
  924. ASSERT_TRUE(base::Time::FromUTCString("2036-12-01 10:00:00", &expiration1));
  925. alternative_service_info_vector.push_back(
  926. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  927. www_alternative_service1, expiration1));
  928. AlternativeService www_alternative_service2(kProtoHTTP2, "www.google.com",
  929. 1234);
  930. base::Time expiration2;
  931. ASSERT_TRUE(base::Time::FromUTCString("2036-12-31 10:00:00", &expiration2));
  932. alternative_service_info_vector.push_back(
  933. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  934. www_alternative_service2, expiration2));
  935. http_server_props_->SetAlternativeServices(server_www, NetworkIsolationKey(),
  936. alternative_service_info_vector);
  937. AlternativeService mail_alternative_service(kProtoHTTP2, "foo.google.com",
  938. 444);
  939. base::Time expiration3 = base::Time::Max();
  940. http_server_props_->SetHttp2AlternativeService(
  941. server_mail, NetworkIsolationKey(), mail_alternative_service,
  942. expiration3);
  943. http_server_props_->MarkAlternativeServiceBroken(www_alternative_service2,
  944. NetworkIsolationKey());
  945. http_server_props_->MarkAlternativeServiceRecentlyBroken(
  946. mail_alternative_service, NetworkIsolationKey());
  947. // #3: Set SPDY server map
  948. http_server_props_->SetSupportsSpdy(server_www, NetworkIsolationKey(), false);
  949. http_server_props_->SetSupportsSpdy(server_mail, NetworkIsolationKey(), true);
  950. http_server_props_->SetSupportsSpdy(
  951. url::SchemeHostPort("http", "not_persisted.com", 80),
  952. NetworkIsolationKey(), false);
  953. // #4: Set ServerNetworkStats.
  954. ServerNetworkStats stats;
  955. stats.srtt = base::TimeDelta::FromInternalValue(42);
  956. http_server_props_->SetServerNetworkStats(server_mail, NetworkIsolationKey(),
  957. stats);
  958. // #5: Set quic_server_info string.
  959. quic::QuicServerId mail_quic_server_id("mail.google.com", 80, false);
  960. std::string quic_server_info1("quic_server_info1");
  961. http_server_props_->SetQuicServerInfo(
  962. mail_quic_server_id, NetworkIsolationKey(), quic_server_info1);
  963. // #6: Set SupportsQuic.
  964. IPAddress actual_address(127, 0, 0, 1);
  965. http_server_props_->SetLastLocalAddressWhenQuicWorked(actual_address);
  966. base::Time time_before_prefs_update = base::Time::Now();
  967. // Update Prefs.
  968. // The task runner has a remaining pending task to expire
  969. // |www_alternative_service2| in 5 minutes. Fast forward enough such
  970. // that the prefs update task is executed but not the task to expire
  971. // |broken_alternative_service|.
  972. EXPECT_EQ(2u, GetPendingMainThreadTaskCount());
  973. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  974. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  975. EXPECT_EQ(1u, GetPendingMainThreadTaskCount());
  976. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  977. base::Time time_after_prefs_update = base::Time::Now();
  978. // Verify |pref_delegate_|'s server dict.
  979. // In HttpServerPropertiesManager, broken alternative services' expiration
  980. // times are converted from TimeTicks to Time before being written to JSON by
  981. // using the difference between Time::Now() and TimeTicks::Now().
  982. // To verify these expiration times, |time_before_prefs_update| and
  983. // |time_after_prefs_update| provide lower and upper bounds for the
  984. // Time::Now() value used by the manager for this conversion.
  985. //
  986. // A copy of |pref_delegate_|'s server dict will be created, and the broken
  987. // alternative service's "broken_until" field is removed and verified
  988. // separately. The rest of the server dict copy is verified afterwards.
  989. base::Value server_dict = pref_delegate_->GetServerProperties()->Clone();
  990. ASSERT_TRUE(server_dict.is_dict());
  991. // Extract and remove the "broken_until" string for "www.google.com:1234".
  992. base::Value::List* broken_alt_svc_list =
  993. server_dict.GetDict().FindList("broken_alternative_services");
  994. ASSERT_TRUE(broken_alt_svc_list);
  995. ASSERT_EQ(2u, broken_alt_svc_list->size());
  996. base::Value& broken_alt_svcs_list_entry = (*broken_alt_svc_list)[0];
  997. const std::string* broken_until_str =
  998. broken_alt_svcs_list_entry.GetDict().FindString("broken_until");
  999. ASSERT_TRUE(broken_until_str);
  1000. const std::string expiration_string = *broken_until_str;
  1001. broken_alt_svcs_list_entry.GetDict().Remove("broken_until");
  1002. // Expiration time of "www.google.com:1234" should be 5 minutes minus the
  1003. // update-prefs-delay from when the prefs were written.
  1004. int64_t expiration_int64;
  1005. ASSERT_TRUE(base::StringToInt64(expiration_string, &expiration_int64));
  1006. base::TimeDelta expiration_delta =
  1007. base::Minutes(5) - HttpServerProperties::GetUpdatePrefsDelayForTesting();
  1008. time_t time_t_of_prefs_update = static_cast<time_t>(expiration_int64);
  1009. EXPECT_LE((time_before_prefs_update + expiration_delta).ToTimeT(),
  1010. time_t_of_prefs_update);
  1011. EXPECT_GE((time_after_prefs_update + expiration_delta).ToTimeT(),
  1012. time_t_of_prefs_update);
  1013. // Verify all other preferences.
  1014. const char expected_json[] =
  1015. "{"
  1016. "\"broken_alternative_services\":"
  1017. "[{\"broken_count\":1,\"host\":\"www.google.com\",\"isolation\":[],"
  1018. "\"port\":1234,\"protocol_str\":\"h2\"},"
  1019. "{\"broken_count\":1,\"host\":\"foo.google.com\",\"isolation\":[],"
  1020. "\"port\":444,\"protocol_str\":\"h2\"}],"
  1021. "\"quic_servers\":"
  1022. "[{\"isolation\":[],"
  1023. "\"server_id\":\"https://mail.google.com:80\","
  1024. "\"server_info\":\"quic_server_info1\"}],"
  1025. "\"servers\":["
  1026. "{\"alternative_service\":[{\"advertised_alpns\":[],"
  1027. "\"expiration\":\"13756212000000000\",\"port\":443,"
  1028. "\"protocol_str\":\"h2\"},"
  1029. "{\"advertised_alpns\":[],\"expiration\":\"13758804000000000\","
  1030. "\"host\":\"www.google.com\",\"port\":1234,\"protocol_str\":\"h2\"}],"
  1031. "\"isolation\":[],"
  1032. "\"server\":\"https://www.google.com:80\"},"
  1033. "{\"alternative_service\":[{\"advertised_alpns\":[],"
  1034. "\"expiration\":\"9223372036854775807\",\"host\":\"foo.google.com\","
  1035. "\"port\":444,\"protocol_str\":\"h2\"}],"
  1036. "\"isolation\":[],"
  1037. "\"network_stats\":{\"srtt\":42},"
  1038. "\"server\":\"https://mail.google.com:80\","
  1039. "\"supports_spdy\":true}],"
  1040. "\"supports_quic\":{\"address\":\"127.0.0.1\",\"used_quic\":true},"
  1041. "\"version\":5}";
  1042. std::string preferences_json;
  1043. EXPECT_TRUE(base::JSONWriter::Write(server_dict, &preferences_json));
  1044. EXPECT_EQ(expected_json, preferences_json);
  1045. }
  1046. TEST_F(HttpServerPropertiesManagerTest, ParseAlternativeServiceInfo) {
  1047. InitializePrefs();
  1048. std::unique_ptr<base::Value> server_dict = base::JSONReader::ReadDeprecated(
  1049. "{\"alternative_service\":[{\"port\":443,\"protocol_str\":\"h2\"},"
  1050. "{\"port\":123,\"protocol_str\":\"quic\","
  1051. "\"expiration\":\"9223372036854775807\"},{\"host\":\"example.org\","
  1052. "\"port\":1234,\"protocol_str\":\"h2\","
  1053. "\"expiration\":\"13758804000000000\"}]}");
  1054. ASSERT_TRUE(server_dict);
  1055. ASSERT_TRUE(server_dict->is_dict());
  1056. const url::SchemeHostPort server("https", "example.com", 443);
  1057. HttpServerProperties::ServerInfo server_info;
  1058. EXPECT_TRUE(HttpServerPropertiesManager::ParseAlternativeServiceInfo(
  1059. server, server_dict->GetDict(), &server_info));
  1060. ASSERT_TRUE(server_info.alternative_services.has_value());
  1061. AlternativeServiceInfoVector alternative_service_info_vector =
  1062. server_info.alternative_services.value();
  1063. ASSERT_EQ(3u, alternative_service_info_vector.size());
  1064. EXPECT_EQ(kProtoHTTP2,
  1065. alternative_service_info_vector[0].alternative_service().protocol);
  1066. EXPECT_EQ("", alternative_service_info_vector[0].alternative_service().host);
  1067. EXPECT_EQ(443, alternative_service_info_vector[0].alternative_service().port);
  1068. // Expiration defaults to one day from now, testing with tolerance.
  1069. const base::Time now = base::Time::Now();
  1070. const base::Time expiration = alternative_service_info_vector[0].expiration();
  1071. EXPECT_LE(now + base::Hours(23), expiration);
  1072. EXPECT_GE(now + base::Days(1), expiration);
  1073. EXPECT_EQ(kProtoQUIC,
  1074. alternative_service_info_vector[1].alternative_service().protocol);
  1075. EXPECT_EQ("", alternative_service_info_vector[1].alternative_service().host);
  1076. EXPECT_EQ(123, alternative_service_info_vector[1].alternative_service().port);
  1077. // numeric_limits<int64_t>::max() represents base::Time::Max().
  1078. EXPECT_EQ(base::Time::Max(), alternative_service_info_vector[1].expiration());
  1079. EXPECT_EQ(kProtoHTTP2,
  1080. alternative_service_info_vector[2].alternative_service().protocol);
  1081. EXPECT_EQ("example.org",
  1082. alternative_service_info_vector[2].alternative_service().host);
  1083. EXPECT_EQ(1234,
  1084. alternative_service_info_vector[2].alternative_service().port);
  1085. base::Time expected_expiration;
  1086. ASSERT_TRUE(
  1087. base::Time::FromUTCString("2036-12-31 10:00:00", &expected_expiration));
  1088. EXPECT_EQ(expected_expiration,
  1089. alternative_service_info_vector[2].expiration());
  1090. // No other fields should have been populated.
  1091. server_info.alternative_services.reset();
  1092. EXPECT_TRUE(server_info.empty());
  1093. }
  1094. // Regression test for https://crbug.com/615497.
  1095. TEST_F(HttpServerPropertiesManagerTest, DoNotLoadAltSvcForInsecureOrigins) {
  1096. InitializePrefs();
  1097. std::unique_ptr<base::Value> server_dict = base::JSONReader::ReadDeprecated(
  1098. "{\"alternative_service\":[{\"port\":443,\"protocol_str\":\"h2\","
  1099. "\"expiration\":\"9223372036854775807\"}]}");
  1100. ASSERT_TRUE(server_dict);
  1101. ASSERT_TRUE(server_dict->is_dict());
  1102. const url::SchemeHostPort server("http", "example.com", 80);
  1103. HttpServerProperties::ServerInfo server_info;
  1104. EXPECT_FALSE(HttpServerPropertiesManager::ParseAlternativeServiceInfo(
  1105. server, server_dict->GetDict(), &server_info));
  1106. EXPECT_TRUE(server_info.empty());
  1107. }
  1108. // Do not persist expired alternative service entries to disk.
  1109. TEST_F(HttpServerPropertiesManagerTest, DoNotPersistExpiredAlternativeService) {
  1110. InitializePrefs();
  1111. AlternativeServiceInfoVector alternative_service_info_vector;
  1112. const AlternativeService broken_alternative_service(
  1113. kProtoHTTP2, "broken.example.com", 443);
  1114. const base::Time time_one_day_later = base::Time::Now() + base::Days(1);
  1115. alternative_service_info_vector.push_back(
  1116. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  1117. broken_alternative_service, time_one_day_later));
  1118. // #1: MarkAlternativeServiceBroken().
  1119. http_server_props_->MarkAlternativeServiceBroken(broken_alternative_service,
  1120. NetworkIsolationKey());
  1121. const AlternativeService expired_alternative_service(
  1122. kProtoHTTP2, "expired.example.com", 443);
  1123. const base::Time time_one_day_ago = base::Time::Now() - base::Days(1);
  1124. alternative_service_info_vector.push_back(
  1125. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  1126. expired_alternative_service, time_one_day_ago));
  1127. const AlternativeService valid_alternative_service(kProtoHTTP2,
  1128. "valid.example.com", 443);
  1129. alternative_service_info_vector.push_back(
  1130. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  1131. valid_alternative_service, time_one_day_later));
  1132. const url::SchemeHostPort server("https", "www.example.com", 443);
  1133. // #2: SetAlternativeServices().
  1134. http_server_props_->SetAlternativeServices(server, NetworkIsolationKey(),
  1135. alternative_service_info_vector);
  1136. // |net_test_task_runner_| has a remaining pending task to expire
  1137. // |broken_alternative_service| at |time_one_day_later|. Fast forward enough
  1138. // such that the prefs update task is executed but not the task to expire
  1139. // |broken_alternative_service|.
  1140. EXPECT_EQ(2U, GetPendingMainThreadTaskCount());
  1141. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  1142. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  1143. EXPECT_EQ(1U, GetPendingMainThreadTaskCount());
  1144. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  1145. const base::Value* pref_dict = pref_delegate_->GetServerProperties();
  1146. const base::Value::List* servers_list =
  1147. pref_dict->GetDict().FindList("servers");
  1148. ASSERT_TRUE(servers_list);
  1149. auto it = servers_list->begin();
  1150. const base::Value& server_pref_dict = *it;
  1151. ASSERT_TRUE(server_pref_dict.is_dict());
  1152. const std::string* server_str =
  1153. server_pref_dict.GetDict().FindString("server");
  1154. ASSERT_TRUE(server_str);
  1155. EXPECT_EQ("https://www.example.com", *server_str);
  1156. const base::Value* network_isolation_key_value =
  1157. server_pref_dict.GetDict().Find("isolation");
  1158. ASSERT_TRUE(network_isolation_key_value);
  1159. ASSERT_EQ(base::Value::Type::LIST, network_isolation_key_value->type());
  1160. EXPECT_TRUE(network_isolation_key_value->GetList().empty());
  1161. const base::Value::List* altsvc_list =
  1162. server_pref_dict.GetDict().FindList("alternative_service");
  1163. ASSERT_TRUE(altsvc_list);
  1164. ASSERT_EQ(2u, altsvc_list->size());
  1165. const base::Value& altsvc_entry = (*altsvc_list)[0];
  1166. ASSERT_TRUE(altsvc_entry.is_dict());
  1167. const std::string* hostname = altsvc_entry.GetDict().FindString("host");
  1168. ASSERT_TRUE(hostname);
  1169. EXPECT_EQ("broken.example.com", *hostname);
  1170. const base::Value& altsvc_entry2 = (*altsvc_list)[1];
  1171. ASSERT_TRUE(altsvc_entry.is_dict());
  1172. hostname = altsvc_entry2.GetDict().FindString("host");
  1173. ASSERT_TRUE(hostname);
  1174. EXPECT_EQ("valid.example.com", *hostname);
  1175. }
  1176. // Test that expired alternative service entries on disk are ignored.
  1177. TEST_F(HttpServerPropertiesManagerTest, DoNotLoadExpiredAlternativeService) {
  1178. InitializePrefs();
  1179. base::Value::List alternative_service_list;
  1180. base::Value::Dict expired_dict;
  1181. expired_dict.Set("protocol_str", "h2");
  1182. expired_dict.Set("host", "expired.example.com");
  1183. expired_dict.Set("port", 443);
  1184. base::Time time_one_day_ago = base::Time::Now() - base::Days(1);
  1185. expired_dict.Set("expiration",
  1186. base::NumberToString(time_one_day_ago.ToInternalValue()));
  1187. alternative_service_list.Append(std::move(expired_dict));
  1188. base::Value::Dict valid_dict;
  1189. valid_dict.Set("protocol_str", "h2");
  1190. valid_dict.Set("host", "valid.example.com");
  1191. valid_dict.Set("port", 443);
  1192. valid_dict.Set("expiration",
  1193. base::NumberToString(one_day_from_now_.ToInternalValue()));
  1194. alternative_service_list.Append(std::move(valid_dict));
  1195. base::Value::Dict server_pref_dict;
  1196. server_pref_dict.Set("alternative_service",
  1197. std::move(alternative_service_list));
  1198. const url::SchemeHostPort server("https", "example.com", 443);
  1199. HttpServerProperties::ServerInfo server_info;
  1200. ASSERT_TRUE(HttpServerPropertiesManager::ParseAlternativeServiceInfo(
  1201. server, server_pref_dict, &server_info));
  1202. ASSERT_TRUE(server_info.alternative_services.has_value());
  1203. AlternativeServiceInfoVector alternative_service_info_vector =
  1204. server_info.alternative_services.value();
  1205. ASSERT_EQ(1u, alternative_service_info_vector.size());
  1206. EXPECT_EQ(kProtoHTTP2,
  1207. alternative_service_info_vector[0].alternative_service().protocol);
  1208. EXPECT_EQ("valid.example.com",
  1209. alternative_service_info_vector[0].alternative_service().host);
  1210. EXPECT_EQ(443, alternative_service_info_vector[0].alternative_service().port);
  1211. EXPECT_EQ(one_day_from_now_, alternative_service_info_vector[0].expiration());
  1212. // No other fields should have been populated.
  1213. server_info.alternative_services.reset();
  1214. EXPECT_TRUE(server_info.empty());
  1215. }
  1216. // Make sure prefs are updated on destruction.
  1217. TEST_F(HttpServerPropertiesManagerTest, UpdatePrefsOnShutdown) {
  1218. InitializePrefs();
  1219. int pref_updates = 0;
  1220. pref_delegate_->set_extra_update_prefs_callback(
  1221. base::BindRepeating([](int* updates) { (*updates)++; }, &pref_updates));
  1222. http_server_props_.reset();
  1223. EXPECT_EQ(1, pref_updates);
  1224. }
  1225. TEST_F(HttpServerPropertiesManagerTest, PersistAdvertisedVersionsToPref) {
  1226. InitializePrefs();
  1227. const url::SchemeHostPort server_www("https", "www.google.com", 80);
  1228. const url::SchemeHostPort server_mail("https", "mail.google.com", 80);
  1229. // #1 & #2: Set alternate protocol.
  1230. AlternativeServiceInfoVector alternative_service_info_vector;
  1231. // Quic alternative service set with two advertised QUIC versions.
  1232. AlternativeService quic_alternative_service1(kProtoQUIC, "", 443);
  1233. base::Time expiration1;
  1234. ASSERT_TRUE(base::Time::FromUTCString("2036-12-01 10:00:00", &expiration1));
  1235. quic::ParsedQuicVersionVector advertised_versions = {
  1236. quic::ParsedQuicVersion::Q046(), quic::ParsedQuicVersion::Q043()};
  1237. alternative_service_info_vector.push_back(
  1238. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  1239. quic_alternative_service1, expiration1, advertised_versions));
  1240. // HTTP/2 alternative service should not set any advertised version.
  1241. AlternativeService h2_alternative_service(kProtoHTTP2, "www.google.com",
  1242. 1234);
  1243. base::Time expiration2;
  1244. ASSERT_TRUE(base::Time::FromUTCString("2036-12-31 10:00:00", &expiration2));
  1245. alternative_service_info_vector.push_back(
  1246. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  1247. h2_alternative_service, expiration2));
  1248. http_server_props_->SetAlternativeServices(server_www, NetworkIsolationKey(),
  1249. alternative_service_info_vector);
  1250. // Set another QUIC alternative service with a single advertised QUIC version.
  1251. AlternativeService mail_alternative_service(kProtoQUIC, "foo.google.com",
  1252. 444);
  1253. base::Time expiration3 = base::Time::Max();
  1254. http_server_props_->SetQuicAlternativeService(
  1255. server_mail, NetworkIsolationKey(), mail_alternative_service, expiration3,
  1256. advertised_versions_);
  1257. // #3: Set ServerNetworkStats.
  1258. ServerNetworkStats stats;
  1259. stats.srtt = base::TimeDelta::FromInternalValue(42);
  1260. http_server_props_->SetServerNetworkStats(server_mail, NetworkIsolationKey(),
  1261. stats);
  1262. // #4: Set quic_server_info string.
  1263. quic::QuicServerId mail_quic_server_id("mail.google.com", 80, false);
  1264. std::string quic_server_info1("quic_server_info1");
  1265. http_server_props_->SetQuicServerInfo(
  1266. mail_quic_server_id, NetworkIsolationKey(), quic_server_info1);
  1267. // #5: Set SupportsQuic.
  1268. IPAddress actual_address(127, 0, 0, 1);
  1269. http_server_props_->SetLastLocalAddressWhenQuicWorked(actual_address);
  1270. // Update Prefs.
  1271. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  1272. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1273. FastForwardUntilNoTasksRemain();
  1274. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  1275. // Verify preferences with correct advertised version field.
  1276. const char expected_json[] =
  1277. "{\"quic_servers\":["
  1278. "{\"isolation\":[],"
  1279. "\"server_id\":\"https://mail.google.com:80\","
  1280. "\"server_info\":\"quic_server_info1\"}],"
  1281. "\"servers\":["
  1282. "{\"alternative_service\":[{"
  1283. "\"advertised_alpns\":[\"h3-Q046\",\"h3-Q043\"],\"expiration\":"
  1284. "\"13756212000000000\","
  1285. "\"port\":443,\"protocol_str\":\"quic\"},{\"advertised_alpns\":[],"
  1286. "\"expiration\":\"13758804000000000\",\"host\":\"www.google.com\","
  1287. "\"port\":1234,\"protocol_str\":\"h2\"}],"
  1288. "\"isolation\":[],"
  1289. "\"server\":\"https://www.google.com:80\"},"
  1290. "{\"alternative_service\":[{"
  1291. "\"advertised_alpns\":[\"h3\"],"
  1292. "\"expiration\":\"9223372036854775807\","
  1293. "\"host\":\"foo.google.com\",\"port\":444,\"protocol_str\":\"quic\"}],"
  1294. "\"isolation\":[],"
  1295. "\"network_stats\":{\"srtt\":42},"
  1296. "\"server\":\"https://mail.google.com:80\"}],"
  1297. "\"supports_quic\":{"
  1298. "\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":5}";
  1299. const base::Value* http_server_properties =
  1300. pref_delegate_->GetServerProperties();
  1301. std::string preferences_json;
  1302. EXPECT_TRUE(
  1303. base::JSONWriter::Write(*http_server_properties, &preferences_json));
  1304. EXPECT_EQ(expected_json, preferences_json);
  1305. }
  1306. TEST_F(HttpServerPropertiesManagerTest, ReadAdvertisedVersionsFromPref) {
  1307. InitializePrefs();
  1308. std::unique_ptr<base::Value> server_dict = base::JSONReader::ReadDeprecated(
  1309. "{\"alternative_service\":["
  1310. "{\"port\":443,\"protocol_str\":\"quic\"},"
  1311. "{\"port\":123,\"protocol_str\":\"quic\","
  1312. "\"expiration\":\"9223372036854775807\","
  1313. // Add 33 which we know is not supported, as regression test for
  1314. // https://crbug.com/1061509
  1315. "\"advertised_alpns\":[\"h3-Q033\",\"h3-Q046\",\"h3-Q043\"]}]}");
  1316. ASSERT_TRUE(server_dict);
  1317. ASSERT_TRUE(server_dict->is_dict());
  1318. const url::SchemeHostPort server("https", "example.com", 443);
  1319. HttpServerProperties::ServerInfo server_info;
  1320. EXPECT_TRUE(HttpServerPropertiesManager::ParseAlternativeServiceInfo(
  1321. server, server_dict->GetDict(), &server_info));
  1322. ASSERT_TRUE(server_info.alternative_services.has_value());
  1323. AlternativeServiceInfoVector alternative_service_info_vector =
  1324. server_info.alternative_services.value();
  1325. ASSERT_EQ(2u, alternative_service_info_vector.size());
  1326. // Verify the first alternative service with no advertised version listed.
  1327. EXPECT_EQ(kProtoQUIC,
  1328. alternative_service_info_vector[0].alternative_service().protocol);
  1329. EXPECT_EQ("", alternative_service_info_vector[0].alternative_service().host);
  1330. EXPECT_EQ(443, alternative_service_info_vector[0].alternative_service().port);
  1331. // Expiration defaults to one day from now, testing with tolerance.
  1332. const base::Time now = base::Time::Now();
  1333. const base::Time expiration = alternative_service_info_vector[0].expiration();
  1334. EXPECT_LE(now + base::Hours(23), expiration);
  1335. EXPECT_GE(now + base::Days(1), expiration);
  1336. EXPECT_TRUE(alternative_service_info_vector[0].advertised_versions().empty());
  1337. // Verify the second alterntaive service with two advertised versions.
  1338. EXPECT_EQ(kProtoQUIC,
  1339. alternative_service_info_vector[1].alternative_service().protocol);
  1340. EXPECT_EQ("", alternative_service_info_vector[1].alternative_service().host);
  1341. EXPECT_EQ(123, alternative_service_info_vector[1].alternative_service().port);
  1342. EXPECT_EQ(base::Time::Max(), alternative_service_info_vector[1].expiration());
  1343. // Verify advertised versions.
  1344. const quic::ParsedQuicVersionVector loaded_advertised_versions =
  1345. alternative_service_info_vector[1].advertised_versions();
  1346. ASSERT_EQ(2u, loaded_advertised_versions.size());
  1347. EXPECT_EQ(quic::ParsedQuicVersion::Q043(), loaded_advertised_versions[0]);
  1348. EXPECT_EQ(quic::ParsedQuicVersion::Q046(), loaded_advertised_versions[1]);
  1349. // No other fields should have been populated.
  1350. server_info.alternative_services.reset();
  1351. EXPECT_TRUE(server_info.empty());
  1352. }
  1353. TEST_F(HttpServerPropertiesManagerTest,
  1354. UpdatePrefWhenAdvertisedVersionsChange) {
  1355. InitializePrefs();
  1356. const url::SchemeHostPort server_www("https", "www.google.com", 80);
  1357. // #1: Set alternate protocol.
  1358. AlternativeServiceInfoVector alternative_service_info_vector;
  1359. // Quic alternative service set with a single QUIC version: Q046.
  1360. AlternativeService quic_alternative_service1(kProtoQUIC, "", 443);
  1361. base::Time expiration1;
  1362. ASSERT_TRUE(base::Time::FromUTCString("2036-12-01 10:00:00", &expiration1));
  1363. alternative_service_info_vector.push_back(
  1364. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  1365. quic_alternative_service1, expiration1, advertised_versions_));
  1366. http_server_props_->SetAlternativeServices(server_www, NetworkIsolationKey(),
  1367. alternative_service_info_vector);
  1368. // Set quic_server_info string.
  1369. quic::QuicServerId mail_quic_server_id("mail.google.com", 80, false);
  1370. std::string quic_server_info1("quic_server_info1");
  1371. http_server_props_->SetQuicServerInfo(
  1372. mail_quic_server_id, NetworkIsolationKey(), quic_server_info1);
  1373. // Set SupportsQuic.
  1374. IPAddress actual_address(127, 0, 0, 1);
  1375. http_server_props_->SetLastLocalAddressWhenQuicWorked(actual_address);
  1376. // Update Prefs.
  1377. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  1378. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1379. FastForwardUntilNoTasksRemain();
  1380. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  1381. // Verify preferences with correct advertised version field.
  1382. const char expected_json[] =
  1383. "{\"quic_servers\":"
  1384. "[{\"isolation\":[],"
  1385. "\"server_id\":\"https://mail.google.com:80\","
  1386. "\"server_info\":\"quic_server_info1\"}],"
  1387. "\"servers\":["
  1388. "{\"alternative_service\":[{"
  1389. "\"advertised_alpns\":[\"h3\"],"
  1390. "\"expiration\":\"13756212000000000\",\"port\":443,"
  1391. "\"protocol_str\":\"quic\"}],"
  1392. "\"isolation\":[],"
  1393. "\"server\":\"https://www.google.com:80\"}],"
  1394. "\"supports_quic\":"
  1395. "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":5}";
  1396. const base::Value* http_server_properties =
  1397. pref_delegate_->GetServerProperties();
  1398. std::string preferences_json;
  1399. EXPECT_TRUE(
  1400. base::JSONWriter::Write(*http_server_properties, &preferences_json));
  1401. EXPECT_EQ(expected_json, preferences_json);
  1402. // #2: Set AlternativeService with different advertised_versions for the same
  1403. // AlternativeService.
  1404. AlternativeServiceInfoVector alternative_service_info_vector_2;
  1405. // Quic alternative service set with two advertised QUIC versions.
  1406. quic::ParsedQuicVersionVector advertised_versions = {
  1407. quic::ParsedQuicVersion::Q046(), quic::ParsedQuicVersion::Q043()};
  1408. alternative_service_info_vector_2.push_back(
  1409. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  1410. quic_alternative_service1, expiration1, advertised_versions));
  1411. http_server_props_->SetAlternativeServices(server_www, NetworkIsolationKey(),
  1412. alternative_service_info_vector_2);
  1413. // Update Prefs.
  1414. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  1415. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1416. FastForwardUntilNoTasksRemain();
  1417. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  1418. // Verify preferences updated with new advertised versions.
  1419. const char expected_json_updated[] =
  1420. "{\"quic_servers\":"
  1421. "[{\"isolation\":[],"
  1422. "\"server_id\":\"https://mail.google.com:80\","
  1423. "\"server_info\":\"quic_server_info1\"}],"
  1424. "\"servers\":["
  1425. "{\"alternative_service\":"
  1426. "[{\"advertised_alpns\":[\"h3-Q046\",\"h3-Q043\"],"
  1427. "\"expiration\":\"13756212000000000\",\"port\":443,"
  1428. "\"protocol_str\":\"quic\"}],"
  1429. "\"isolation\":[],"
  1430. "\"server\":\"https://www.google.com:80\"}],"
  1431. "\"supports_quic\":"
  1432. "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":5}";
  1433. EXPECT_TRUE(
  1434. base::JSONWriter::Write(*http_server_properties, &preferences_json));
  1435. EXPECT_EQ(expected_json_updated, preferences_json);
  1436. // #3: Set AlternativeService with same advertised_versions.
  1437. AlternativeServiceInfoVector alternative_service_info_vector_3;
  1438. // A same set of QUIC versions but listed in a different order.
  1439. quic::ParsedQuicVersionVector advertised_versions_2 = {
  1440. quic::ParsedQuicVersion::Q043(), quic::ParsedQuicVersion::Q046()};
  1441. alternative_service_info_vector_3.push_back(
  1442. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  1443. quic_alternative_service1, expiration1, advertised_versions_2));
  1444. http_server_props_->SetAlternativeServices(server_www, NetworkIsolationKey(),
  1445. alternative_service_info_vector_3);
  1446. // Change in version ordering causes prefs update.
  1447. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  1448. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1449. FastForwardUntilNoTasksRemain();
  1450. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  1451. // Verify preferences updated with new advertised versions.
  1452. const char expected_json_updated2[] =
  1453. "{\"quic_servers\":"
  1454. "[{\"isolation\":[],"
  1455. "\"server_id\":\"https://mail.google.com:80\","
  1456. "\"server_info\":\"quic_server_info1\"}],"
  1457. "\"servers\":["
  1458. "{\"alternative_service\":"
  1459. "[{\"advertised_alpns\":[\"h3-Q043\",\"h3-Q046\"],"
  1460. "\"expiration\":\"13756212000000000\",\"port\":443,"
  1461. "\"protocol_str\":\"quic\"}],"
  1462. "\"isolation\":[],"
  1463. "\"server\":\"https://www.google.com:80\"}],"
  1464. "\"supports_quic\":"
  1465. "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":5}";
  1466. EXPECT_TRUE(
  1467. base::JSONWriter::Write(*http_server_properties, &preferences_json));
  1468. EXPECT_EQ(expected_json_updated2, preferences_json);
  1469. }
  1470. TEST_F(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) {
  1471. AlternativeService cached_broken_service(kProtoQUIC, "cached_broken", 443);
  1472. AlternativeService cached_broken_service2(kProtoQUIC, "cached_broken2", 443);
  1473. AlternativeService cached_recently_broken_service(kProtoQUIC,
  1474. "cached_rbroken", 443);
  1475. http_server_props_->MarkAlternativeServiceBroken(cached_broken_service,
  1476. NetworkIsolationKey());
  1477. http_server_props_->MarkAlternativeServiceBroken(cached_broken_service2,
  1478. NetworkIsolationKey());
  1479. http_server_props_->MarkAlternativeServiceRecentlyBroken(
  1480. cached_recently_broken_service, NetworkIsolationKey());
  1481. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  1482. // There should be a task to remove remove alt services from the cache of
  1483. // broken alt services. There should be no task to update the prefs, since the
  1484. // prefs file hasn't been loaded yet.
  1485. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1486. // Load the |pref_delegate_| with some JSON to verify updating the cache from
  1487. // prefs. For the broken alternative services "www.google.com:1234" and
  1488. // "cached_broken", the expiration time will be one day from now.
  1489. std::string expiration_str =
  1490. base::NumberToString(static_cast<int64_t>(one_day_from_now_.ToTimeT()));
  1491. std::unique_ptr<base::Value> server_dict = base::JSONReader::ReadDeprecated(
  1492. "{"
  1493. "\"broken_alternative_services\":["
  1494. "{\"broken_until\":\"" +
  1495. expiration_str +
  1496. "\","
  1497. "\"host\":\"www.google.com\",\"isolation\":[],"
  1498. "\"port\":1234,\"protocol_str\":\"h2\"},"
  1499. "{\"broken_count\":2,\"broken_until\":\"" +
  1500. expiration_str +
  1501. "\","
  1502. "\"host\":\"cached_broken\",\"isolation\":[],"
  1503. "\"port\":443,\"protocol_str\":\"quic\"},"
  1504. "{\"broken_count\":3,"
  1505. "\"host\":\"cached_rbroken\",\"isolation\":[],"
  1506. "\"port\":443,\"protocol_str\":\"quic\"}],"
  1507. "\"quic_servers\":["
  1508. "{\"isolation\":[],"
  1509. "\"server_id\":\"https://mail.google.com:80\","
  1510. "\"server_info\":\"quic_server_info1\"}"
  1511. "],"
  1512. "\"servers\":["
  1513. "{\"server\":\"https://www.google.com:80\","
  1514. "\"isolation\":[],"
  1515. "\"alternative_service\":["
  1516. "{\"expiration\":\"13756212000000000\",\"port\":443,"
  1517. "\"protocol_str\":\"h2\"},"
  1518. "{\"expiration\":\"13758804000000000\",\"host\":\"www.google.com\","
  1519. "\"port\":1234,\"protocol_str\":\"h2\"}"
  1520. "]"
  1521. "},"
  1522. "{\"server\":\"https://mail.google.com:80\","
  1523. "\"isolation\":[],"
  1524. "\"alternative_service\":["
  1525. "{\"expiration\":\"9223372036854775807\",\"host\":\"foo.google.com\","
  1526. "\"port\":444,\"protocol_str\":\"h2\"}"
  1527. "],"
  1528. "\"network_stats\":{\"srtt\":42}"
  1529. "}"
  1530. "],"
  1531. "\"supports_quic\":"
  1532. "{\"address\":\"127.0.0.1\",\"used_quic\":true},"
  1533. "\"version\":5"
  1534. "}");
  1535. ASSERT_TRUE(server_dict);
  1536. ASSERT_TRUE(server_dict->is_dict());
  1537. // Don't use the test fixture's InitializePrefs() method, since there are
  1538. // pending tasks. Initializing prefs should queue a pref update task, since
  1539. // prefs have been modified.
  1540. pref_delegate_->InitializePrefs(*server_dict);
  1541. EXPECT_TRUE(http_server_props_->IsInitialized());
  1542. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  1543. // Run until prefs are updated.
  1544. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  1545. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  1546. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1547. //
  1548. // Verify alternative service info for https://www.google.com
  1549. //
  1550. AlternativeServiceInfoVector alternative_service_info_vector =
  1551. http_server_props_->GetAlternativeServiceInfos(
  1552. url::SchemeHostPort("https", "www.google.com", 80),
  1553. NetworkIsolationKey());
  1554. ASSERT_EQ(2u, alternative_service_info_vector.size());
  1555. EXPECT_EQ(kProtoHTTP2,
  1556. alternative_service_info_vector[0].alternative_service().protocol);
  1557. EXPECT_EQ("www.google.com",
  1558. alternative_service_info_vector[0].alternative_service().host);
  1559. EXPECT_EQ(443, alternative_service_info_vector[0].alternative_service().port);
  1560. EXPECT_EQ(
  1561. "13756212000000000",
  1562. base::NumberToString(
  1563. alternative_service_info_vector[0].expiration().ToInternalValue()));
  1564. EXPECT_EQ(kProtoHTTP2,
  1565. alternative_service_info_vector[1].alternative_service().protocol);
  1566. EXPECT_EQ("www.google.com",
  1567. alternative_service_info_vector[1].alternative_service().host);
  1568. EXPECT_EQ(1234,
  1569. alternative_service_info_vector[1].alternative_service().port);
  1570. EXPECT_EQ(
  1571. "13758804000000000",
  1572. base::NumberToString(
  1573. alternative_service_info_vector[1].expiration().ToInternalValue()));
  1574. //
  1575. // Verify alternative service info for https://mail.google.com
  1576. //
  1577. alternative_service_info_vector =
  1578. http_server_props_->GetAlternativeServiceInfos(
  1579. url::SchemeHostPort("https", "mail.google.com", 80),
  1580. NetworkIsolationKey());
  1581. ASSERT_EQ(1u, alternative_service_info_vector.size());
  1582. EXPECT_EQ(kProtoHTTP2,
  1583. alternative_service_info_vector[0].alternative_service().protocol);
  1584. EXPECT_EQ("foo.google.com",
  1585. alternative_service_info_vector[0].alternative_service().host);
  1586. EXPECT_EQ(444, alternative_service_info_vector[0].alternative_service().port);
  1587. EXPECT_EQ(
  1588. "9223372036854775807",
  1589. base::NumberToString(
  1590. alternative_service_info_vector[0].expiration().ToInternalValue()));
  1591. //
  1592. // Verify broken alternative services.
  1593. //
  1594. AlternativeService prefs_broken_service(kProtoHTTP2, "www.google.com", 1234);
  1595. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1596. cached_broken_service, NetworkIsolationKey()));
  1597. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1598. cached_broken_service2, NetworkIsolationKey()));
  1599. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1600. prefs_broken_service, NetworkIsolationKey()));
  1601. // Verify brokenness expiration times.
  1602. // |cached_broken_service|'s expiration time should've been overwritten by the
  1603. // prefs to be approximately 1 day from now. |cached_broken_service2|'s
  1604. // expiration time should still be 5 minutes due to being marked broken.
  1605. // |prefs_broken_service|'s expiration time should be approximately 1 day from
  1606. // now which comes from the prefs.
  1607. FastForwardBy(base::Minutes(5) -
  1608. HttpServerProperties::GetUpdatePrefsDelayForTesting());
  1609. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1610. cached_broken_service, NetworkIsolationKey()));
  1611. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  1612. cached_broken_service2, NetworkIsolationKey()));
  1613. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1614. prefs_broken_service, NetworkIsolationKey()));
  1615. FastForwardBy(base::Days(1));
  1616. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  1617. cached_broken_service, NetworkIsolationKey()));
  1618. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  1619. cached_broken_service2, NetworkIsolationKey()));
  1620. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  1621. prefs_broken_service, NetworkIsolationKey()));
  1622. // Now that |prefs_broken_service|'s brokenness has expired, it should've
  1623. // been removed from the alternative services info vectors of all servers.
  1624. alternative_service_info_vector =
  1625. http_server_props_->GetAlternativeServiceInfos(
  1626. url::SchemeHostPort("https", "www.google.com", 80),
  1627. NetworkIsolationKey());
  1628. ASSERT_EQ(1u, alternative_service_info_vector.size());
  1629. //
  1630. // Verify recently broken alternative services.
  1631. //
  1632. // If an entry is already in cache, the broken count in the prefs should
  1633. // overwrite the one in the cache.
  1634. // |prefs_broken_service| should have broken-count 1 from prefs.
  1635. // |cached_recently_broken_service| should have broken-count 3 from prefs.
  1636. // |cached_broken_service| should have broken-count 2 from prefs.
  1637. // |cached_broken_service2| should have broken-count 1 from being marked
  1638. // broken.
  1639. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  1640. prefs_broken_service, NetworkIsolationKey()));
  1641. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  1642. cached_recently_broken_service, NetworkIsolationKey()));
  1643. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  1644. cached_broken_service, NetworkIsolationKey()));
  1645. EXPECT_TRUE(http_server_props_->WasAlternativeServiceRecentlyBroken(
  1646. cached_broken_service2, NetworkIsolationKey()));
  1647. // Make sure |prefs_broken_service| has the right expiration delay when marked
  1648. // broken. Since |prefs_broken_service| had no broken_count specified in the
  1649. // prefs, a broken_count value of 1 should have been assumed by
  1650. // |http_server_props_|.
  1651. http_server_props_->MarkAlternativeServiceBroken(prefs_broken_service,
  1652. NetworkIsolationKey());
  1653. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  1654. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1655. FastForwardBy(base::Minutes(10) - base::TimeDelta::FromInternalValue(1));
  1656. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1657. prefs_broken_service, NetworkIsolationKey()));
  1658. FastForwardBy(base::TimeDelta::FromInternalValue(1));
  1659. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  1660. prefs_broken_service, NetworkIsolationKey()));
  1661. // Make sure |cached_recently_broken_service| has the right expiration delay
  1662. // when marked broken.
  1663. http_server_props_->MarkAlternativeServiceBroken(
  1664. cached_recently_broken_service, NetworkIsolationKey());
  1665. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1666. FastForwardBy(base::Minutes(40) - base::TimeDelta::FromInternalValue(1));
  1667. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1668. cached_recently_broken_service, NetworkIsolationKey()));
  1669. FastForwardBy(base::TimeDelta::FromInternalValue(1));
  1670. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  1671. cached_recently_broken_service, NetworkIsolationKey()));
  1672. // Make sure |cached_broken_service| has the right expiration delay when
  1673. // marked broken.
  1674. http_server_props_->MarkAlternativeServiceBroken(cached_broken_service,
  1675. NetworkIsolationKey());
  1676. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1677. FastForwardBy(base::Minutes(20) - base::TimeDelta::FromInternalValue(1));
  1678. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1679. cached_broken_service, NetworkIsolationKey()));
  1680. FastForwardBy(base::TimeDelta::FromInternalValue(1));
  1681. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  1682. cached_broken_service, NetworkIsolationKey()));
  1683. // Make sure |cached_broken_service2| has the right expiration delay when
  1684. // marked broken.
  1685. http_server_props_->MarkAlternativeServiceBroken(cached_broken_service2,
  1686. NetworkIsolationKey());
  1687. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  1688. FastForwardBy(base::Minutes(10) - base::TimeDelta::FromInternalValue(1));
  1689. EXPECT_TRUE(http_server_props_->IsAlternativeServiceBroken(
  1690. cached_broken_service2, NetworkIsolationKey()));
  1691. FastForwardBy(base::TimeDelta::FromInternalValue(1));
  1692. EXPECT_FALSE(http_server_props_->IsAlternativeServiceBroken(
  1693. cached_broken_service2, NetworkIsolationKey()));
  1694. //
  1695. // Verify ServerNetworkStats.
  1696. //
  1697. const ServerNetworkStats* server_network_stats =
  1698. http_server_props_->GetServerNetworkStats(
  1699. url::SchemeHostPort("https", "mail.google.com", 80),
  1700. NetworkIsolationKey());
  1701. EXPECT_TRUE(server_network_stats);
  1702. EXPECT_EQ(server_network_stats->srtt, base::TimeDelta::FromInternalValue(42));
  1703. //
  1704. // Verify QUIC server info.
  1705. //
  1706. const std::string* quic_server_info = http_server_props_->GetQuicServerInfo(
  1707. quic::QuicServerId("mail.google.com", 80, false), NetworkIsolationKey());
  1708. EXPECT_EQ("quic_server_info1", *quic_server_info);
  1709. //
  1710. // Verify supports QUIC.
  1711. //
  1712. IPAddress actual_address(127, 0, 0, 1);
  1713. EXPECT_TRUE(
  1714. http_server_props_->WasLastLocalAddressWhenQuicWorked(actual_address));
  1715. EXPECT_EQ(4, pref_delegate_->GetAndClearNumPrefUpdates());
  1716. }
  1717. // Check the interaction of ForceHTTP11 with saving/restoring settings.
  1718. // In particular, ForceHTTP11 is not saved, and it should not overwrite or be
  1719. // overitten by loaded data.
  1720. TEST_F(HttpServerPropertiesManagerTest, ForceHTTP11) {
  1721. const url::SchemeHostPort kServer1("https", "foo.test", 443);
  1722. const url::SchemeHostPort kServer2("https", "bar.test", 443);
  1723. const url::SchemeHostPort kServer3("https", "baz.test", 443);
  1724. // Create and initialize an HttpServerProperties with no state.
  1725. std::unique_ptr<MockPrefDelegate> pref_delegate =
  1726. std::make_unique<MockPrefDelegate>();
  1727. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  1728. std::unique_ptr<HttpServerProperties> properties =
  1729. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  1730. /*net_log=*/nullptr,
  1731. GetMockTickClock());
  1732. unowned_pref_delegate->InitializePrefs(
  1733. base::Value(base::Value::Type::DICTIONARY));
  1734. // Set kServer1 to support H2, but require HTTP/1.1. Set kServer2 to only
  1735. // require HTTP/1.1.
  1736. EXPECT_FALSE(properties->GetSupportsSpdy(kServer1, NetworkIsolationKey()));
  1737. EXPECT_FALSE(properties->RequiresHTTP11(kServer1, NetworkIsolationKey()));
  1738. EXPECT_FALSE(properties->GetSupportsSpdy(kServer2, NetworkIsolationKey()));
  1739. EXPECT_FALSE(properties->RequiresHTTP11(kServer2, NetworkIsolationKey()));
  1740. properties->SetSupportsSpdy(kServer1, NetworkIsolationKey(), true);
  1741. properties->SetHTTP11Required(kServer1, NetworkIsolationKey());
  1742. properties->SetHTTP11Required(kServer2, NetworkIsolationKey());
  1743. EXPECT_TRUE(properties->GetSupportsSpdy(kServer1, NetworkIsolationKey()));
  1744. EXPECT_TRUE(properties->RequiresHTTP11(kServer1, NetworkIsolationKey()));
  1745. EXPECT_FALSE(properties->GetSupportsSpdy(kServer2, NetworkIsolationKey()));
  1746. EXPECT_TRUE(properties->RequiresHTTP11(kServer2, NetworkIsolationKey()));
  1747. // Wait until the data's been written to prefs, and then tear down the
  1748. // HttpServerProperties.
  1749. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  1750. base::Value saved_value =
  1751. unowned_pref_delegate->GetServerProperties()->Clone();
  1752. properties.reset();
  1753. // Only information on kServer1 should have been saved to prefs.
  1754. std::string preferences_json;
  1755. base::JSONWriter::Write(saved_value, &preferences_json);
  1756. EXPECT_EQ(
  1757. "{\"servers\":["
  1758. "{\"isolation\":[],"
  1759. "\"server\":\"https://foo.test\","
  1760. "\"supports_spdy\":true}],"
  1761. "\"version\":5}",
  1762. preferences_json);
  1763. // Create a new HttpServerProperties using the value saved to prefs above.
  1764. pref_delegate = std::make_unique<MockPrefDelegate>();
  1765. unowned_pref_delegate = pref_delegate.get();
  1766. properties = std::make_unique<HttpServerProperties>(
  1767. std::move(pref_delegate), /*net_log=*/nullptr, GetMockTickClock());
  1768. // Before the data has loaded, set kServer1 and kServer3 as requiring
  1769. // HTTP/1.1.
  1770. EXPECT_FALSE(properties->GetSupportsSpdy(kServer1, NetworkIsolationKey()));
  1771. EXPECT_FALSE(properties->RequiresHTTP11(kServer1, NetworkIsolationKey()));
  1772. properties->SetHTTP11Required(kServer1, NetworkIsolationKey());
  1773. properties->SetHTTP11Required(kServer3, NetworkIsolationKey());
  1774. EXPECT_FALSE(properties->GetSupportsSpdy(kServer1, NetworkIsolationKey()));
  1775. EXPECT_TRUE(properties->RequiresHTTP11(kServer1, NetworkIsolationKey()));
  1776. EXPECT_FALSE(properties->GetSupportsSpdy(kServer2, NetworkIsolationKey()));
  1777. EXPECT_FALSE(properties->RequiresHTTP11(kServer2, NetworkIsolationKey()));
  1778. EXPECT_FALSE(properties->GetSupportsSpdy(kServer3, NetworkIsolationKey()));
  1779. EXPECT_TRUE(properties->RequiresHTTP11(kServer3, NetworkIsolationKey()));
  1780. // The data loads.
  1781. unowned_pref_delegate->InitializePrefs(saved_value);
  1782. // The properties should contain a combination of the old and new data.
  1783. EXPECT_TRUE(properties->GetSupportsSpdy(kServer1, NetworkIsolationKey()));
  1784. EXPECT_TRUE(properties->RequiresHTTP11(kServer1, NetworkIsolationKey()));
  1785. EXPECT_FALSE(properties->GetSupportsSpdy(kServer2, NetworkIsolationKey()));
  1786. EXPECT_FALSE(properties->RequiresHTTP11(kServer2, NetworkIsolationKey()));
  1787. EXPECT_FALSE(properties->GetSupportsSpdy(kServer3, NetworkIsolationKey()));
  1788. EXPECT_TRUE(properties->RequiresHTTP11(kServer3, NetworkIsolationKey()));
  1789. }
  1790. TEST_F(HttpServerPropertiesManagerTest, NetworkIsolationKeyServerInfo) {
  1791. const SchemefulSite kSite1(GURL("https://foo.test/"));
  1792. const SchemefulSite kSite2(GURL("https://bar.test/"));
  1793. const SchemefulSite kOpaqueSite(GURL("data:text/plain,Hello World"));
  1794. const url::SchemeHostPort kServer("https", "baz.test", 443);
  1795. const url::SchemeHostPort kServer2("https", "zab.test", 443);
  1796. HttpServerProperties::ServerInfo server_info;
  1797. server_info.supports_spdy = true;
  1798. for (auto save_network_isolation_key_mode : kNetworkIsolationKeyModes) {
  1799. SCOPED_TRACE(static_cast<int>(save_network_isolation_key_mode));
  1800. // Save prefs using |save_network_isolation_key_mode|.
  1801. base::Value saved_value;
  1802. {
  1803. // Configure the the feature.
  1804. std::unique_ptr<base::test::ScopedFeatureList> feature_list =
  1805. SetNetworkIsolationKeyMode(save_network_isolation_key_mode);
  1806. // This parameter is normally calculated by HttpServerProperties based on
  1807. // the kPartitionHttpServerPropertiesByNetworkIsolationKey feature, but
  1808. // this test doesn't use that class.
  1809. bool use_network_isolation_key =
  1810. save_network_isolation_key_mode != NetworkIsolationKeyMode::kDisabled;
  1811. HttpServerProperties::ServerInfoMap server_info_map;
  1812. // Add server info entry using two origins with value of |server_info|.
  1813. // NetworkIsolationKey's constructor takes the state of the
  1814. // kAppendFrameOriginToNetworkIsolationKey feature into account, so need
  1815. // to make sure to call the constructor after setting up the feature
  1816. // above.
  1817. HttpServerProperties::ServerInfoMapKey server_info_key(
  1818. kServer, NetworkIsolationKey(kSite1, kSite2),
  1819. use_network_isolation_key);
  1820. server_info_map.Put(server_info_key, server_info);
  1821. // Also add an etry with an opaque origin, if |use_network_isolation_key|
  1822. // is true. This value should not be saved to disk, since opaque origins
  1823. // are only meaningful within a browsing session.
  1824. if (use_network_isolation_key) {
  1825. HttpServerProperties::ServerInfoMapKey server_info_key2(
  1826. kServer2, NetworkIsolationKey(kOpaqueSite, kOpaqueSite),
  1827. use_network_isolation_key);
  1828. server_info_map.Put(server_info_key2, server_info);
  1829. }
  1830. saved_value = ServerInfoMapToValue(server_info_map);
  1831. }
  1832. for (auto load_network_isolation_key_mode : kNetworkIsolationKeyModes) {
  1833. SCOPED_TRACE(static_cast<int>(load_network_isolation_key_mode));
  1834. std::unique_ptr<base::test::ScopedFeatureList> feature_list =
  1835. SetNetworkIsolationKeyMode(load_network_isolation_key_mode);
  1836. std::unique_ptr<HttpServerProperties::ServerInfoMap> server_info_map2 =
  1837. ValueToServerInfoMap(saved_value);
  1838. ASSERT_TRUE(server_info_map2);
  1839. if (save_network_isolation_key_mode ==
  1840. NetworkIsolationKeyMode::kDisabled) {
  1841. // If NetworkIsolationKey was disabled when saving, it was saved with an
  1842. // empty NetworkIsolationKey, which should always be loaded
  1843. // successfully. This is needed to continue to support consumers that
  1844. // don't use NetworkIsolationKeys.
  1845. ASSERT_EQ(1u, server_info_map2->size());
  1846. const HttpServerProperties::ServerInfoMapKey& server_info_key2 =
  1847. server_info_map2->begin()->first;
  1848. const HttpServerProperties::ServerInfo& server_info2 =
  1849. server_info_map2->begin()->second;
  1850. EXPECT_EQ(kServer, server_info_key2.server);
  1851. EXPECT_EQ(NetworkIsolationKey(),
  1852. server_info_key2.network_isolation_key);
  1853. EXPECT_EQ(server_info, server_info2);
  1854. } else if (save_network_isolation_key_mode ==
  1855. load_network_isolation_key_mode) {
  1856. // If the save and load modes are the same, the load should succeed, and
  1857. // the network isolation keys should match.
  1858. ASSERT_EQ(1u, server_info_map2->size());
  1859. const HttpServerProperties::ServerInfoMapKey& server_info_key2 =
  1860. server_info_map2->begin()->first;
  1861. const HttpServerProperties::ServerInfo& server_info2 =
  1862. server_info_map2->begin()->second;
  1863. EXPECT_EQ(kServer, server_info_key2.server);
  1864. EXPECT_EQ(NetworkIsolationKey(kSite1, kSite2),
  1865. server_info_key2.network_isolation_key);
  1866. EXPECT_EQ(server_info, server_info2);
  1867. } else {
  1868. // Otherwise, the NetworkIsolationKey doesn't make sense with the
  1869. // current feature values, so the ServerInfo should be discarded.
  1870. EXPECT_EQ(0u, server_info_map2->size());
  1871. }
  1872. }
  1873. }
  1874. }
  1875. // Tests a full round trip with a NetworkIsolationKey, using the
  1876. // HttpServerProperties interface.
  1877. TEST_F(HttpServerPropertiesManagerTest, NetworkIsolationKeyIntegration) {
  1878. const SchemefulSite kSite(GURL("https://foo.test/"));
  1879. const NetworkIsolationKey kNetworkIsolationKey(kSite, kSite);
  1880. const url::SchemeHostPort kServer("https", "baz.test", 443);
  1881. const SchemefulSite kOpaqueSite(GURL("data:text/plain,Hello World"));
  1882. const NetworkIsolationKey kOpaqueSiteNetworkIsolationKey(kOpaqueSite,
  1883. kOpaqueSite);
  1884. const url::SchemeHostPort kServer2("https", "zab.test", 443);
  1885. base::test::ScopedFeatureList feature_list;
  1886. feature_list.InitAndEnableFeature(
  1887. features::kPartitionHttpServerPropertiesByNetworkIsolationKey);
  1888. // Create and initialize an HttpServerProperties with no state.
  1889. std::unique_ptr<MockPrefDelegate> pref_delegate =
  1890. std::make_unique<MockPrefDelegate>();
  1891. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  1892. std::unique_ptr<HttpServerProperties> properties =
  1893. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  1894. /*net_log=*/nullptr,
  1895. GetMockTickClock());
  1896. unowned_pref_delegate->InitializePrefs(
  1897. base::Value(base::Value::Type::DICTIONARY));
  1898. // Set a values using kNetworkIsolationKey.
  1899. properties->SetSupportsSpdy(kServer, kNetworkIsolationKey, true);
  1900. EXPECT_TRUE(properties->GetSupportsSpdy(kServer, kNetworkIsolationKey));
  1901. EXPECT_FALSE(
  1902. properties->GetSupportsSpdy(kServer, kOpaqueSiteNetworkIsolationKey));
  1903. EXPECT_FALSE(properties->GetSupportsSpdy(kServer, NetworkIsolationKey()));
  1904. // Opaque origins should works with HttpServerProperties, but not be persisted
  1905. // to disk.
  1906. properties->SetSupportsSpdy(kServer2, kOpaqueSiteNetworkIsolationKey, true);
  1907. EXPECT_FALSE(properties->GetSupportsSpdy(kServer2, kNetworkIsolationKey));
  1908. EXPECT_TRUE(
  1909. properties->GetSupportsSpdy(kServer2, kOpaqueSiteNetworkIsolationKey));
  1910. EXPECT_FALSE(properties->GetSupportsSpdy(kServer2, NetworkIsolationKey()));
  1911. // Wait until the data's been written to prefs, and then tear down the
  1912. // HttpServerProperties.
  1913. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  1914. base::Value saved_value =
  1915. unowned_pref_delegate->GetServerProperties()->Clone();
  1916. properties.reset();
  1917. // Create a new HttpServerProperties using the value saved to prefs above.
  1918. pref_delegate = std::make_unique<MockPrefDelegate>();
  1919. unowned_pref_delegate = pref_delegate.get();
  1920. properties = std::make_unique<HttpServerProperties>(
  1921. std::move(pref_delegate), /*net_log=*/nullptr, GetMockTickClock());
  1922. unowned_pref_delegate->InitializePrefs(saved_value);
  1923. // The information set using kNetworkIsolationKey on the original
  1924. // HttpServerProperties should also be set on the restored
  1925. // HttpServerProperties.
  1926. EXPECT_TRUE(properties->GetSupportsSpdy(kServer, kNetworkIsolationKey));
  1927. EXPECT_FALSE(
  1928. properties->GetSupportsSpdy(kServer, kOpaqueSiteNetworkIsolationKey));
  1929. EXPECT_FALSE(properties->GetSupportsSpdy(kServer, NetworkIsolationKey()));
  1930. // The information set using kOpaqueSiteNetworkIsolationKey should not have
  1931. // been restored.
  1932. EXPECT_FALSE(properties->GetSupportsSpdy(kServer2, kNetworkIsolationKey));
  1933. EXPECT_FALSE(
  1934. properties->GetSupportsSpdy(kServer2, kOpaqueSiteNetworkIsolationKey));
  1935. EXPECT_FALSE(properties->GetSupportsSpdy(kServer2, NetworkIsolationKey()));
  1936. }
  1937. // Tests a full round trip to prefs and back in the canonical suffix case.
  1938. // Enable NetworkIsolationKeys, as they have some interactions with the
  1939. // canonical suffix logic.
  1940. TEST_F(HttpServerPropertiesManagerTest,
  1941. CanonicalSuffixRoundTripWithNetworkIsolationKey) {
  1942. const SchemefulSite kSite1(GURL("https://foo.test/"));
  1943. const SchemefulSite kSite2(GURL("https://bar.test/"));
  1944. const NetworkIsolationKey kNetworkIsolationKey1(kSite1, kSite1);
  1945. const NetworkIsolationKey kNetworkIsolationKey2(kSite2, kSite2);
  1946. // Three servers with the same canonical suffix (".c.youtube.com").
  1947. const url::SchemeHostPort kServer1("https", "foo.c.youtube.com", 443);
  1948. const url::SchemeHostPort kServer2("https", "bar.c.youtube.com", 443);
  1949. const url::SchemeHostPort kServer3("https", "baz.c.youtube.com", 443);
  1950. base::test::ScopedFeatureList feature_list;
  1951. feature_list.InitAndEnableFeature(
  1952. features::kPartitionHttpServerPropertiesByNetworkIsolationKey);
  1953. // Create three alt service vectors of different lengths.
  1954. base::Time expiration = base::Time::Now() + base::Days(1);
  1955. AlternativeServiceInfo alt_service1 =
  1956. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  1957. AlternativeService(kProtoQUIC, "foopy.c.youtube.com", 1234),
  1958. expiration, DefaultSupportedQuicVersions());
  1959. AlternativeServiceInfo alt_service2 =
  1960. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  1961. AlternativeService(kProtoHTTP2, "foopy.c.youtube.com", 443),
  1962. expiration);
  1963. AlternativeServiceInfo alt_service3 =
  1964. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  1965. AlternativeService(kProtoHTTP2, "foopy2.c.youtube.com", 443),
  1966. expiration);
  1967. AlternativeServiceInfoVector alt_service_vector1 = {alt_service1};
  1968. AlternativeServiceInfoVector alt_service_vector2 = {alt_service1,
  1969. alt_service2};
  1970. AlternativeServiceInfoVector alt_service_vector3 = {
  1971. alt_service1, alt_service2, alt_service3};
  1972. // Create and initialize an HttpServerProperties with no state.
  1973. std::unique_ptr<MockPrefDelegate> pref_delegate =
  1974. std::make_unique<MockPrefDelegate>();
  1975. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  1976. std::unique_ptr<HttpServerProperties> properties =
  1977. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  1978. /*net_log=*/nullptr,
  1979. GetMockTickClock());
  1980. unowned_pref_delegate->InitializePrefs(
  1981. base::Value(base::Value::Type::DICTIONARY));
  1982. // Set alternative services for kServer1 using kNetworkIsolationKey1. That
  1983. // information should be retrieved when fetching information for any server
  1984. // with the same canonical suffix, when using kNetworkIsolationKey1.
  1985. properties->SetAlternativeServices(kServer1, kNetworkIsolationKey1,
  1986. alt_service_vector1);
  1987. EXPECT_EQ(
  1988. 1u,
  1989. properties->GetAlternativeServiceInfos(kServer1, kNetworkIsolationKey1)
  1990. .size());
  1991. EXPECT_EQ(
  1992. 1u,
  1993. properties->GetAlternativeServiceInfos(kServer2, kNetworkIsolationKey1)
  1994. .size());
  1995. EXPECT_EQ(
  1996. 1u,
  1997. properties->GetAlternativeServiceInfos(kServer3, kNetworkIsolationKey1)
  1998. .size());
  1999. EXPECT_EQ(
  2000. 0u,
  2001. properties->GetAlternativeServiceInfos(kServer1, kNetworkIsolationKey2)
  2002. .size());
  2003. // Set different alternative services for kServer2 using
  2004. // kNetworkIsolationKey1. It should not affect information retrieved for
  2005. // kServer1, but should for kServer2 and kServer3.
  2006. properties->SetAlternativeServices(kServer2, kNetworkIsolationKey1,
  2007. alt_service_vector2);
  2008. EXPECT_EQ(
  2009. 1u,
  2010. properties->GetAlternativeServiceInfos(kServer1, kNetworkIsolationKey1)
  2011. .size());
  2012. EXPECT_EQ(
  2013. 2u,
  2014. properties->GetAlternativeServiceInfos(kServer2, kNetworkIsolationKey1)
  2015. .size());
  2016. EXPECT_EQ(
  2017. 2u,
  2018. properties->GetAlternativeServiceInfos(kServer3, kNetworkIsolationKey1)
  2019. .size());
  2020. EXPECT_EQ(
  2021. 0u,
  2022. properties->GetAlternativeServiceInfos(kServer1, kNetworkIsolationKey2)
  2023. .size());
  2024. // Set different information for kServer1 using kNetworkIsolationKey2. It
  2025. // should not affect information stored for kNetworkIsolationKey1.
  2026. properties->SetAlternativeServices(kServer1, kNetworkIsolationKey2,
  2027. alt_service_vector3);
  2028. EXPECT_EQ(
  2029. 1u,
  2030. properties->GetAlternativeServiceInfos(kServer1, kNetworkIsolationKey1)
  2031. .size());
  2032. EXPECT_EQ(
  2033. 2u,
  2034. properties->GetAlternativeServiceInfos(kServer2, kNetworkIsolationKey1)
  2035. .size());
  2036. EXPECT_EQ(
  2037. 2u,
  2038. properties->GetAlternativeServiceInfos(kServer3, kNetworkIsolationKey1)
  2039. .size());
  2040. EXPECT_EQ(
  2041. 3u,
  2042. properties->GetAlternativeServiceInfos(kServer1, kNetworkIsolationKey2)
  2043. .size());
  2044. EXPECT_EQ(
  2045. 3u,
  2046. properties->GetAlternativeServiceInfos(kServer2, kNetworkIsolationKey2)
  2047. .size());
  2048. EXPECT_EQ(
  2049. 3u,
  2050. properties->GetAlternativeServiceInfos(kServer3, kNetworkIsolationKey2)
  2051. .size());
  2052. // Wait until the data's been written to prefs, and then tear down the
  2053. // HttpServerProperties.
  2054. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  2055. base::Value saved_value =
  2056. unowned_pref_delegate->GetServerProperties()->Clone();
  2057. properties.reset();
  2058. // Create a new HttpServerProperties using the value saved to prefs above.
  2059. pref_delegate = std::make_unique<MockPrefDelegate>();
  2060. unowned_pref_delegate = pref_delegate.get();
  2061. properties = std::make_unique<HttpServerProperties>(
  2062. std::move(pref_delegate), /*net_log=*/nullptr, GetMockTickClock());
  2063. unowned_pref_delegate->InitializePrefs(saved_value);
  2064. // Only the last of the values learned for kNetworkIsolationKey1 should have
  2065. // been saved, and the value for kNetworkIsolationKey2 as well. The canonical
  2066. // suffix logic should still be respected.
  2067. EXPECT_EQ(
  2068. 2u,
  2069. properties->GetAlternativeServiceInfos(kServer1, kNetworkIsolationKey1)
  2070. .size());
  2071. EXPECT_EQ(
  2072. 2u,
  2073. properties->GetAlternativeServiceInfos(kServer2, kNetworkIsolationKey1)
  2074. .size());
  2075. EXPECT_EQ(
  2076. 2u,
  2077. properties->GetAlternativeServiceInfos(kServer3, kNetworkIsolationKey1)
  2078. .size());
  2079. EXPECT_EQ(
  2080. 3u,
  2081. properties->GetAlternativeServiceInfos(kServer1, kNetworkIsolationKey2)
  2082. .size());
  2083. EXPECT_EQ(
  2084. 3u,
  2085. properties->GetAlternativeServiceInfos(kServer2, kNetworkIsolationKey2)
  2086. .size());
  2087. EXPECT_EQ(
  2088. 3u,
  2089. properties->GetAlternativeServiceInfos(kServer3, kNetworkIsolationKey2)
  2090. .size());
  2091. }
  2092. // Tests a full round trip with a NetworkIsolationKey, using the
  2093. // HttpServerProperties interface and setting alternative services as broken.
  2094. TEST_F(HttpServerPropertiesManagerTest,
  2095. NetworkIsolationKeyBrokenAltServiceRoundTrip) {
  2096. const SchemefulSite kSite1(GURL("https://foo1.test/"));
  2097. const SchemefulSite kSite2(GURL("https://foo2.test/"));
  2098. const AlternativeService kAlternativeService1(kProtoHTTP2,
  2099. "alt.service1.test", 443);
  2100. const AlternativeService kAlternativeService2(kProtoHTTP2,
  2101. "alt.service2.test", 443);
  2102. for (auto save_network_isolation_key_mode : kNetworkIsolationKeyModes) {
  2103. SCOPED_TRACE(static_cast<int>(save_network_isolation_key_mode));
  2104. // Save prefs using |save_network_isolation_key_mode|.
  2105. base::Value saved_value;
  2106. {
  2107. // Configure the the feature.
  2108. std::unique_ptr<base::test::ScopedFeatureList> feature_list =
  2109. SetNetworkIsolationKeyMode(save_network_isolation_key_mode);
  2110. // The NetworkIsolationKey constructor checks the field trial state, so
  2111. // need to create the keys only after setting up the field trials.
  2112. const NetworkIsolationKey kNetworkIsolationKey1(kSite1, kSite1);
  2113. const NetworkIsolationKey kNetworkIsolationKey2(kSite2, kSite2);
  2114. // Create and initialize an HttpServerProperties, must be done after
  2115. // setting the feature.
  2116. std::unique_ptr<MockPrefDelegate> pref_delegate =
  2117. std::make_unique<MockPrefDelegate>();
  2118. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  2119. std::unique_ptr<HttpServerProperties> properties =
  2120. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  2121. /*net_log=*/nullptr,
  2122. GetMockTickClock());
  2123. unowned_pref_delegate->InitializePrefs(
  2124. base::Value(base::Value::Type::DICTIONARY));
  2125. // Set kAlternativeService1 as broken in the context of
  2126. // kNetworkIsolationKey1, and kAlternativeService2 as broken in the
  2127. // context of the empty NetworkIsolationKey2, and recently broken in the
  2128. // context of the empty NetworkIsolationKey.
  2129. properties->MarkAlternativeServiceBroken(kAlternativeService1,
  2130. kNetworkIsolationKey1);
  2131. properties->MarkAlternativeServiceRecentlyBroken(kAlternativeService2,
  2132. NetworkIsolationKey());
  2133. properties->MarkAlternativeServiceBroken(kAlternativeService2,
  2134. kNetworkIsolationKey2);
  2135. // Verify values were set.
  2136. EXPECT_TRUE(properties->IsAlternativeServiceBroken(
  2137. kAlternativeService1, kNetworkIsolationKey1));
  2138. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2139. kAlternativeService1, kNetworkIsolationKey1));
  2140. // When NetworkIsolationKeys are disabled, kAlternativeService2 is marked
  2141. // as broken regardless of the values passed to NetworkIsolationKey's
  2142. // constructor.
  2143. EXPECT_EQ(
  2144. save_network_isolation_key_mode == NetworkIsolationKeyMode::kDisabled,
  2145. properties->IsAlternativeServiceBroken(kAlternativeService2,
  2146. NetworkIsolationKey()));
  2147. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2148. kAlternativeService2, NetworkIsolationKey()));
  2149. EXPECT_TRUE(properties->IsAlternativeServiceBroken(
  2150. kAlternativeService2, kNetworkIsolationKey2));
  2151. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2152. kAlternativeService2, kNetworkIsolationKey2));
  2153. // If NetworkIsolationKeys are enabled, there should be no
  2154. // cross-contamination of the NetworkIsolationKeys.
  2155. if (save_network_isolation_key_mode !=
  2156. NetworkIsolationKeyMode::kDisabled) {
  2157. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2158. kAlternativeService2, kNetworkIsolationKey1));
  2159. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2160. kAlternativeService2, kNetworkIsolationKey1));
  2161. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2162. kAlternativeService1, NetworkIsolationKey()));
  2163. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2164. kAlternativeService1, NetworkIsolationKey()));
  2165. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2166. kAlternativeService1, kNetworkIsolationKey2));
  2167. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2168. kAlternativeService1, kNetworkIsolationKey2));
  2169. }
  2170. // Wait until the data's been written to prefs, and then create a copy of
  2171. // the prefs data.
  2172. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  2173. saved_value = unowned_pref_delegate->GetServerProperties()->Clone();
  2174. }
  2175. // Now try and load the data in each of the feature modes.
  2176. for (auto load_network_isolation_key_mode : kNetworkIsolationKeyModes) {
  2177. SCOPED_TRACE(static_cast<int>(load_network_isolation_key_mode));
  2178. std::unique_ptr<base::test::ScopedFeatureList> feature_list =
  2179. SetNetworkIsolationKeyMode(load_network_isolation_key_mode);
  2180. // The NetworkIsolationKey constructor checks the field trial state, so
  2181. // need to create the keys only after setting up the field trials.
  2182. const NetworkIsolationKey kNetworkIsolationKey1(kSite1, kSite1);
  2183. const NetworkIsolationKey kNetworkIsolationKey2(kSite2, kSite2);
  2184. // Create a new HttpServerProperties, loading the data from before.
  2185. std::unique_ptr<MockPrefDelegate> pref_delegate =
  2186. std::make_unique<MockPrefDelegate>();
  2187. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  2188. std::unique_ptr<HttpServerProperties> properties =
  2189. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  2190. /*net_log=*/nullptr,
  2191. GetMockTickClock());
  2192. unowned_pref_delegate->InitializePrefs(saved_value);
  2193. if (save_network_isolation_key_mode ==
  2194. NetworkIsolationKeyMode::kDisabled) {
  2195. // If NetworkIsolationKey was disabled when saving, it was saved with an
  2196. // empty NetworkIsolationKey, which should always be loaded
  2197. // successfully. This is needed to continue to support consumers that
  2198. // don't use NetworkIsolationKeys.
  2199. EXPECT_TRUE(properties->IsAlternativeServiceBroken(
  2200. kAlternativeService1, NetworkIsolationKey()));
  2201. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2202. kAlternativeService1, NetworkIsolationKey()));
  2203. EXPECT_TRUE(properties->IsAlternativeServiceBroken(
  2204. kAlternativeService2, NetworkIsolationKey()));
  2205. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2206. kAlternativeService2, NetworkIsolationKey()));
  2207. } else if (save_network_isolation_key_mode ==
  2208. load_network_isolation_key_mode) {
  2209. // If the save and load modes are the same, the load should succeed, and
  2210. // the network isolation keys should match.
  2211. EXPECT_TRUE(properties->IsAlternativeServiceBroken(
  2212. kAlternativeService1, kNetworkIsolationKey1));
  2213. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2214. kAlternativeService1, kNetworkIsolationKey1));
  2215. // When NetworkIsolationKeys are disabled, kAlternativeService2 is
  2216. // marked as broken regardless of the values passed to
  2217. // NetworkIsolationKey's constructor.
  2218. EXPECT_EQ(save_network_isolation_key_mode ==
  2219. NetworkIsolationKeyMode::kDisabled,
  2220. properties->IsAlternativeServiceBroken(
  2221. kAlternativeService2, NetworkIsolationKey()));
  2222. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2223. kAlternativeService2, NetworkIsolationKey()));
  2224. EXPECT_TRUE(properties->IsAlternativeServiceBroken(
  2225. kAlternativeService2, kNetworkIsolationKey2));
  2226. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2227. kAlternativeService2, kNetworkIsolationKey2));
  2228. // If NetworkIsolationKeys are enabled, there should be no
  2229. // cross-contamination of the NetworkIsolationKeys.
  2230. if (save_network_isolation_key_mode !=
  2231. NetworkIsolationKeyMode::kDisabled) {
  2232. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2233. kAlternativeService2, kNetworkIsolationKey1));
  2234. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2235. kAlternativeService2, kNetworkIsolationKey1));
  2236. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2237. kAlternativeService1, NetworkIsolationKey()));
  2238. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2239. kAlternativeService1, NetworkIsolationKey()));
  2240. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2241. kAlternativeService1, kNetworkIsolationKey2));
  2242. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2243. kAlternativeService1, kNetworkIsolationKey2));
  2244. }
  2245. } else {
  2246. // Otherwise, only the values set with an empty NetworkIsolationKey
  2247. // should have been loaded successfully.
  2248. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2249. kAlternativeService1, kNetworkIsolationKey1));
  2250. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2251. kAlternativeService1, kNetworkIsolationKey1));
  2252. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2253. kAlternativeService2, NetworkIsolationKey()));
  2254. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2255. kAlternativeService2, NetworkIsolationKey()));
  2256. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2257. kAlternativeService2, kNetworkIsolationKey2));
  2258. // If the load mode is NetworkIsolationKeyMode::kDisabled,
  2259. // kNetworkIsolationKey2 is NetworkIsolationKey().
  2260. EXPECT_EQ(load_network_isolation_key_mode ==
  2261. NetworkIsolationKeyMode::kDisabled,
  2262. properties->WasAlternativeServiceRecentlyBroken(
  2263. kAlternativeService2, kNetworkIsolationKey2));
  2264. // There should be no cross-contamination of NetworkIsolationKeys, if
  2265. // NetworkIsolationKeys are enabled.
  2266. if (load_network_isolation_key_mode !=
  2267. NetworkIsolationKeyMode::kDisabled) {
  2268. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2269. kAlternativeService2, kNetworkIsolationKey1));
  2270. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2271. kAlternativeService2, kNetworkIsolationKey1));
  2272. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2273. kAlternativeService1, NetworkIsolationKey()));
  2274. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2275. kAlternativeService1, NetworkIsolationKey()));
  2276. EXPECT_FALSE(properties->IsAlternativeServiceBroken(
  2277. kAlternativeService1, kNetworkIsolationKey2));
  2278. EXPECT_FALSE(properties->WasAlternativeServiceRecentlyBroken(
  2279. kAlternativeService1, kNetworkIsolationKey2));
  2280. }
  2281. }
  2282. }
  2283. }
  2284. }
  2285. // Make sure broken alt services with opaque origins aren't saved.
  2286. TEST_F(HttpServerPropertiesManagerTest,
  2287. NetworkIsolationKeyBrokenAltServiceOpaqueOrigin) {
  2288. const SchemefulSite kOpaqueSite(GURL("data:text/plain,Hello World"));
  2289. const NetworkIsolationKey kNetworkIsolationKey(kOpaqueSite, kOpaqueSite);
  2290. const AlternativeService kAlternativeService(kProtoHTTP2, "alt.service1.test",
  2291. 443);
  2292. base::test::ScopedFeatureList feature_list;
  2293. feature_list.InitAndEnableFeature(
  2294. features::kPartitionHttpServerPropertiesByNetworkIsolationKey);
  2295. // Create and initialize an HttpServerProperties, must be done after
  2296. // setting the feature.
  2297. std::unique_ptr<MockPrefDelegate> pref_delegate =
  2298. std::make_unique<MockPrefDelegate>();
  2299. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  2300. std::unique_ptr<HttpServerProperties> properties =
  2301. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  2302. /*net_log=*/nullptr,
  2303. GetMockTickClock());
  2304. unowned_pref_delegate->InitializePrefs(
  2305. base::Value(base::Value::Type::DICTIONARY));
  2306. properties->MarkAlternativeServiceBroken(kAlternativeService,
  2307. kNetworkIsolationKey);
  2308. // Verify values were set.
  2309. EXPECT_TRUE(properties->IsAlternativeServiceBroken(kAlternativeService,
  2310. kNetworkIsolationKey));
  2311. EXPECT_TRUE(properties->WasAlternativeServiceRecentlyBroken(
  2312. kAlternativeService, kNetworkIsolationKey));
  2313. // Wait until the data's been written to prefs, and then create a copy of
  2314. // the prefs data.
  2315. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  2316. // No information should have been saved to prefs.
  2317. std::string preferences_json;
  2318. base::JSONWriter::Write(*unowned_pref_delegate->GetServerProperties(),
  2319. &preferences_json);
  2320. EXPECT_EQ("{\"servers\":[],\"version\":5}", preferences_json);
  2321. }
  2322. // Tests a full round trip with a NetworkIsolationKey, using the
  2323. // HttpServerProperties interface and setting QuicServerInfo.
  2324. TEST_F(HttpServerPropertiesManagerTest,
  2325. NetworkIsolationKeyQuicServerInfoRoundTrip) {
  2326. const SchemefulSite kSite1(GURL("https://foo1.test/"));
  2327. const SchemefulSite kSite2(GURL("https://foo2.test/"));
  2328. const quic::QuicServerId kServer1("foo", 443,
  2329. false /* privacy_mode_enabled */);
  2330. const quic::QuicServerId kServer2("foo", 443,
  2331. true /* privacy_mode_enabled */);
  2332. const char kQuicServerInfo1[] = "info1";
  2333. const char kQuicServerInfo2[] = "info2";
  2334. const char kQuicServerInfo3[] = "info3";
  2335. for (auto save_network_isolation_key_mode : kNetworkIsolationKeyModes) {
  2336. SCOPED_TRACE(static_cast<int>(save_network_isolation_key_mode));
  2337. // Save prefs using |save_network_isolation_key_mode|.
  2338. base::Value saved_value;
  2339. {
  2340. // Configure the the feature.
  2341. std::unique_ptr<base::test::ScopedFeatureList> feature_list =
  2342. SetNetworkIsolationKeyMode(save_network_isolation_key_mode);
  2343. // The NetworkIsolationKey constructor checks the field trial state, so
  2344. // need to create the keys only after setting up the field trials.
  2345. const NetworkIsolationKey kNetworkIsolationKey1(kSite1, kSite1);
  2346. const NetworkIsolationKey kNetworkIsolationKey2(kSite2, kSite2);
  2347. // Create and initialize an HttpServerProperties, must be done after
  2348. // setting the feature.
  2349. std::unique_ptr<MockPrefDelegate> pref_delegate =
  2350. std::make_unique<MockPrefDelegate>();
  2351. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  2352. std::unique_ptr<HttpServerProperties> properties =
  2353. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  2354. /*net_log=*/nullptr,
  2355. GetMockTickClock());
  2356. unowned_pref_delegate->InitializePrefs(
  2357. base::Value(base::Value::Type::DICTIONARY));
  2358. // Set kServer1 to kQuicServerInfo1 in the context of
  2359. // kNetworkIsolationKey1, Set kServer2 to kQuicServerInfo2 in the context
  2360. // of kNetworkIsolationKey2, and kServer1 to kQuicServerInfo3 in the
  2361. // context of NetworkIsolationKey().
  2362. properties->SetQuicServerInfo(kServer1, kNetworkIsolationKey1,
  2363. kQuicServerInfo1);
  2364. properties->SetQuicServerInfo(kServer2, kNetworkIsolationKey2,
  2365. kQuicServerInfo2);
  2366. properties->SetQuicServerInfo(kServer1, NetworkIsolationKey(),
  2367. kQuicServerInfo3);
  2368. // Verify values were set.
  2369. if (save_network_isolation_key_mode !=
  2370. NetworkIsolationKeyMode::kDisabled) {
  2371. EXPECT_EQ(kQuicServerInfo1, *properties->GetQuicServerInfo(
  2372. kServer1, kNetworkIsolationKey1));
  2373. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2374. kServer1, kNetworkIsolationKey2));
  2375. EXPECT_EQ(kQuicServerInfo3, *properties->GetQuicServerInfo(
  2376. kServer1, NetworkIsolationKey()));
  2377. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2378. kServer2, kNetworkIsolationKey1));
  2379. EXPECT_EQ(kQuicServerInfo2, *properties->GetQuicServerInfo(
  2380. kServer2, kNetworkIsolationKey2));
  2381. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2382. kServer2, NetworkIsolationKey()));
  2383. } else {
  2384. EXPECT_EQ(kQuicServerInfo3, *properties->GetQuicServerInfo(
  2385. kServer1, NetworkIsolationKey()));
  2386. EXPECT_EQ(kQuicServerInfo2, *properties->GetQuicServerInfo(
  2387. kServer2, NetworkIsolationKey()));
  2388. }
  2389. // Wait until the data's been written to prefs, and then create a copy of
  2390. // the prefs data.
  2391. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  2392. saved_value = unowned_pref_delegate->GetServerProperties()->Clone();
  2393. }
  2394. // Now try and load the data in each of the feature modes.
  2395. for (auto load_network_isolation_key_mode : kNetworkIsolationKeyModes) {
  2396. SCOPED_TRACE(static_cast<int>(load_network_isolation_key_mode));
  2397. std::unique_ptr<base::test::ScopedFeatureList> feature_list =
  2398. SetNetworkIsolationKeyMode(load_network_isolation_key_mode);
  2399. // The NetworkIsolationKey constructor checks the field trial state, so
  2400. // need to create the keys only after setting up the field trials.
  2401. const NetworkIsolationKey kNetworkIsolationKey1(kSite1, kSite1);
  2402. const NetworkIsolationKey kNetworkIsolationKey2(kSite2, kSite2);
  2403. // Create a new HttpServerProperties, loading the data from before.
  2404. std::unique_ptr<MockPrefDelegate> pref_delegate =
  2405. std::make_unique<MockPrefDelegate>();
  2406. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  2407. std::unique_ptr<HttpServerProperties> properties =
  2408. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  2409. /*net_log=*/nullptr,
  2410. GetMockTickClock());
  2411. unowned_pref_delegate->InitializePrefs(saved_value);
  2412. if (save_network_isolation_key_mode ==
  2413. NetworkIsolationKeyMode::kDisabled) {
  2414. // If NetworkIsolationKey was disabled when saving, entries were saved
  2415. // with an empty NetworkIsolationKey, which should always be loaded
  2416. // successfully. This is needed to continue to support consumers that
  2417. // don't use NetworkIsolationKeys.
  2418. EXPECT_EQ(kQuicServerInfo3, *properties->GetQuicServerInfo(
  2419. kServer1, NetworkIsolationKey()));
  2420. EXPECT_EQ(kQuicServerInfo2, *properties->GetQuicServerInfo(
  2421. kServer2, NetworkIsolationKey()));
  2422. if (load_network_isolation_key_mode !=
  2423. NetworkIsolationKeyMode::kDisabled) {
  2424. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2425. kServer1, kNetworkIsolationKey1));
  2426. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2427. kServer1, kNetworkIsolationKey2));
  2428. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2429. kServer2, kNetworkIsolationKey1));
  2430. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2431. kServer2, kNetworkIsolationKey2));
  2432. }
  2433. } else if (save_network_isolation_key_mode ==
  2434. load_network_isolation_key_mode) {
  2435. // If the save and load modes are the same, the load should succeed, and
  2436. // the network isolation keys should match.
  2437. EXPECT_EQ(kQuicServerInfo1, *properties->GetQuicServerInfo(
  2438. kServer1, kNetworkIsolationKey1));
  2439. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2440. kServer1, kNetworkIsolationKey2));
  2441. EXPECT_EQ(kQuicServerInfo3, *properties->GetQuicServerInfo(
  2442. kServer1, NetworkIsolationKey()));
  2443. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2444. kServer2, kNetworkIsolationKey1));
  2445. EXPECT_EQ(kQuicServerInfo2, *properties->GetQuicServerInfo(
  2446. kServer2, kNetworkIsolationKey2));
  2447. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2448. kServer2, NetworkIsolationKey()));
  2449. } else {
  2450. // Otherwise, only the value set with an empty NetworkIsolationKey
  2451. // should have been loaded successfully.
  2452. EXPECT_EQ(kQuicServerInfo3, *properties->GetQuicServerInfo(
  2453. kServer1, NetworkIsolationKey()));
  2454. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2455. kServer2, kNetworkIsolationKey1));
  2456. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2457. kServer2, kNetworkIsolationKey2));
  2458. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2459. kServer2, NetworkIsolationKey()));
  2460. // There should be no cross-contamination of NetworkIsolationKeys, if
  2461. // NetworkIsolationKeys are enabled.
  2462. if (load_network_isolation_key_mode !=
  2463. NetworkIsolationKeyMode::kDisabled) {
  2464. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2465. kServer1, kNetworkIsolationKey1));
  2466. EXPECT_EQ(nullptr, properties->GetQuicServerInfo(
  2467. kServer1, kNetworkIsolationKey2));
  2468. }
  2469. }
  2470. }
  2471. }
  2472. }
  2473. // Tests a full round trip to prefs and back in the canonical suffix for
  2474. // QuicServerInfo case. Enable NetworkIsolationKeys, as they have some
  2475. // interactions with the canonical suffix logic.
  2476. TEST_F(HttpServerPropertiesManagerTest,
  2477. NetworkIsolationKeyQuicServerInfoCanonicalSuffixRoundTrip) {
  2478. const SchemefulSite kSite1(GURL("https://foo.test/"));
  2479. const SchemefulSite kSite2(GURL("https://bar.test/"));
  2480. const NetworkIsolationKey kNetworkIsolationKey1(kSite1, kSite1);
  2481. const NetworkIsolationKey kNetworkIsolationKey2(kSite2, kSite2);
  2482. // Three servers with the same canonical suffix (".c.youtube.com").
  2483. const quic::QuicServerId kServer1("foo.c.youtube.com", 443,
  2484. false /* privacy_mode_enabled */);
  2485. const quic::QuicServerId kServer2("bar.c.youtube.com", 443,
  2486. false /* privacy_mode_enabled */);
  2487. const quic::QuicServerId kServer3("baz.c.youtube.com", 443,
  2488. false /* privacy_mode_enabled */);
  2489. const char kQuicServerInfo1[] = "info1";
  2490. const char kQuicServerInfo2[] = "info2";
  2491. const char kQuicServerInfo3[] = "info3";
  2492. base::test::ScopedFeatureList feature_list;
  2493. feature_list.InitAndEnableFeature(
  2494. features::kPartitionHttpServerPropertiesByNetworkIsolationKey);
  2495. // Create and initialize an HttpServerProperties with no state.
  2496. std::unique_ptr<MockPrefDelegate> pref_delegate =
  2497. std::make_unique<MockPrefDelegate>();
  2498. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  2499. std::unique_ptr<HttpServerProperties> properties =
  2500. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  2501. /*net_log=*/nullptr,
  2502. GetMockTickClock());
  2503. unowned_pref_delegate->InitializePrefs(
  2504. base::Value(base::Value::Type::DICTIONARY));
  2505. // Set kQuicServerInfo1 for kServer1 using kNetworkIsolationKey1. That
  2506. // information should be retrieved when fetching information for any server
  2507. // with the same canonical suffix, when using kNetworkIsolationKey1.
  2508. properties->SetQuicServerInfo(kServer1, kNetworkIsolationKey1,
  2509. kQuicServerInfo1);
  2510. EXPECT_EQ(kQuicServerInfo1,
  2511. *properties->GetQuicServerInfo(kServer1, kNetworkIsolationKey1));
  2512. EXPECT_EQ(kQuicServerInfo1,
  2513. *properties->GetQuicServerInfo(kServer2, kNetworkIsolationKey1));
  2514. EXPECT_EQ(kQuicServerInfo1,
  2515. *properties->GetQuicServerInfo(kServer3, kNetworkIsolationKey1));
  2516. EXPECT_FALSE(properties->GetQuicServerInfo(kServer1, kNetworkIsolationKey2));
  2517. // Set kQuicServerInfo2 for kServer2 using kNetworkIsolationKey1. It should
  2518. // not affect information retrieved for kServer1, but should for kServer2 and
  2519. // kServer3.
  2520. properties->SetQuicServerInfo(kServer2, kNetworkIsolationKey1,
  2521. kQuicServerInfo2);
  2522. EXPECT_EQ(kQuicServerInfo1,
  2523. *properties->GetQuicServerInfo(kServer1, kNetworkIsolationKey1));
  2524. EXPECT_EQ(kQuicServerInfo2,
  2525. *properties->GetQuicServerInfo(kServer2, kNetworkIsolationKey1));
  2526. EXPECT_EQ(kQuicServerInfo2,
  2527. *properties->GetQuicServerInfo(kServer3, kNetworkIsolationKey1));
  2528. EXPECT_FALSE(properties->GetQuicServerInfo(kServer1, kNetworkIsolationKey2));
  2529. // Set kQuicServerInfo3 for kServer1 using kNetworkIsolationKey2. It should
  2530. // not affect information stored for kNetworkIsolationKey1.
  2531. properties->SetQuicServerInfo(kServer1, kNetworkIsolationKey2,
  2532. kQuicServerInfo3);
  2533. EXPECT_EQ(kQuicServerInfo1,
  2534. *properties->GetQuicServerInfo(kServer1, kNetworkIsolationKey1));
  2535. EXPECT_EQ(kQuicServerInfo2,
  2536. *properties->GetQuicServerInfo(kServer2, kNetworkIsolationKey1));
  2537. EXPECT_EQ(kQuicServerInfo2,
  2538. *properties->GetQuicServerInfo(kServer3, kNetworkIsolationKey1));
  2539. EXPECT_EQ(kQuicServerInfo3,
  2540. *properties->GetQuicServerInfo(kServer1, kNetworkIsolationKey2));
  2541. EXPECT_EQ(kQuicServerInfo3,
  2542. *properties->GetQuicServerInfo(kServer2, kNetworkIsolationKey2));
  2543. EXPECT_EQ(kQuicServerInfo3,
  2544. *properties->GetQuicServerInfo(kServer3, kNetworkIsolationKey2));
  2545. // Wait until the data's been written to prefs, and then tear down the
  2546. // HttpServerProperties.
  2547. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  2548. base::Value saved_value =
  2549. unowned_pref_delegate->GetServerProperties()->Clone();
  2550. properties.reset();
  2551. // Create a new HttpServerProperties using the value saved to prefs above.
  2552. pref_delegate = std::make_unique<MockPrefDelegate>();
  2553. unowned_pref_delegate = pref_delegate.get();
  2554. properties = std::make_unique<HttpServerProperties>(
  2555. std::move(pref_delegate), /*net_log=*/nullptr, GetMockTickClock());
  2556. unowned_pref_delegate->InitializePrefs(saved_value);
  2557. // All values should have been saved and be retrievable by suffix-matching
  2558. // servers.
  2559. //
  2560. // TODO(mmenke): The rest of this test corresponds exactly to behavior in
  2561. // CanonicalSuffixRoundTripWithNetworkIsolationKey. It seems like these lines
  2562. // should correspond as well.
  2563. EXPECT_EQ(kQuicServerInfo1,
  2564. *properties->GetQuicServerInfo(kServer1, kNetworkIsolationKey1));
  2565. EXPECT_EQ(kQuicServerInfo2,
  2566. *properties->GetQuicServerInfo(kServer2, kNetworkIsolationKey1));
  2567. EXPECT_EQ(kQuicServerInfo2,
  2568. *properties->GetQuicServerInfo(kServer3, kNetworkIsolationKey1));
  2569. EXPECT_EQ(kQuicServerInfo3,
  2570. *properties->GetQuicServerInfo(kServer1, kNetworkIsolationKey2));
  2571. EXPECT_EQ(kQuicServerInfo3,
  2572. *properties->GetQuicServerInfo(kServer2, kNetworkIsolationKey2));
  2573. EXPECT_EQ(kQuicServerInfo3,
  2574. *properties->GetQuicServerInfo(kServer3, kNetworkIsolationKey2));
  2575. }
  2576. // Make sure QuicServerInfo associated with NetworkIsolationKeys with opaque
  2577. // origins aren't saved.
  2578. TEST_F(HttpServerPropertiesManagerTest,
  2579. NetworkIsolationKeyQuicServerInfoOpaqueOrigin) {
  2580. const SchemefulSite kOpaqueSite(GURL("data:text/plain,Hello World"));
  2581. const NetworkIsolationKey kNetworkIsolationKey(kOpaqueSite, kOpaqueSite);
  2582. const quic::QuicServerId kServer("foo", 443,
  2583. false /* privacy_mode_enabled */);
  2584. base::test::ScopedFeatureList feature_list;
  2585. feature_list.InitAndEnableFeature(
  2586. features::kPartitionHttpServerPropertiesByNetworkIsolationKey);
  2587. // Create and initialize an HttpServerProperties, must be done after
  2588. // setting the feature.
  2589. std::unique_ptr<MockPrefDelegate> pref_delegate =
  2590. std::make_unique<MockPrefDelegate>();
  2591. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  2592. std::unique_ptr<HttpServerProperties> properties =
  2593. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  2594. /*net_log=*/nullptr,
  2595. GetMockTickClock());
  2596. unowned_pref_delegate->InitializePrefs(
  2597. base::Value(base::Value::Type::DICTIONARY));
  2598. properties->SetQuicServerInfo(kServer, kNetworkIsolationKey,
  2599. "QuicServerInfo");
  2600. EXPECT_TRUE(properties->GetQuicServerInfo(kServer, kNetworkIsolationKey));
  2601. // Wait until the data's been written to prefs, and then create a copy of
  2602. // the prefs data.
  2603. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  2604. // No information should have been saved to prefs.
  2605. std::string preferences_json;
  2606. base::JSONWriter::Write(*unowned_pref_delegate->GetServerProperties(),
  2607. &preferences_json);
  2608. EXPECT_EQ("{\"quic_servers\":[],\"servers\":[],\"version\":5}",
  2609. preferences_json);
  2610. }
  2611. TEST_F(HttpServerPropertiesManagerTest, AdvertisedVersionsRoundTrip) {
  2612. for (const quic::ParsedQuicVersion& version : quic::AllSupportedVersions()) {
  2613. if (version.AlpnDeferToRFCv1()) {
  2614. // These versions currently do not support Alt-Svc.
  2615. continue;
  2616. }
  2617. // Reset test infrastructure.
  2618. TearDown();
  2619. SetUp();
  2620. InitializePrefs();
  2621. // Create alternate version information.
  2622. const url::SchemeHostPort server("https", "quic.example.org", 443);
  2623. AlternativeServiceInfoVector alternative_service_info_vector_in;
  2624. AlternativeService quic_alternative_service(kProtoQUIC, "", 443);
  2625. base::Time expiration;
  2626. ASSERT_TRUE(base::Time::FromUTCString("2036-12-01 10:00:00", &expiration));
  2627. quic::ParsedQuicVersionVector advertised_versions = {version};
  2628. alternative_service_info_vector_in.push_back(
  2629. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  2630. quic_alternative_service, expiration, advertised_versions));
  2631. http_server_props_->SetAlternativeServices(
  2632. server, NetworkIsolationKey(), alternative_service_info_vector_in);
  2633. // Save to JSON.
  2634. EXPECT_EQ(0, pref_delegate_->GetAndClearNumPrefUpdates());
  2635. EXPECT_NE(0u, GetPendingMainThreadTaskCount());
  2636. FastForwardUntilNoTasksRemain();
  2637. EXPECT_EQ(1, pref_delegate_->GetAndClearNumPrefUpdates());
  2638. const base::Value* http_server_properties =
  2639. pref_delegate_->GetServerProperties();
  2640. std::string preferences_json;
  2641. EXPECT_TRUE(
  2642. base::JSONWriter::Write(*http_server_properties, &preferences_json));
  2643. // Reset test infrastructure.
  2644. TearDown();
  2645. SetUp();
  2646. InitializePrefs();
  2647. // Read from JSON.
  2648. std::unique_ptr<base::Value> preferences_dict =
  2649. base::JSONReader::ReadDeprecated(preferences_json);
  2650. ASSERT_TRUE(preferences_dict);
  2651. ASSERT_TRUE(preferences_dict->is_dict());
  2652. const base::Value::List* servers_list =
  2653. preferences_dict->GetDict().FindList("servers");
  2654. ASSERT_TRUE(servers_list);
  2655. ASSERT_EQ(servers_list->size(), 1u);
  2656. const base::Value& server_dict = (*servers_list)[0];
  2657. HttpServerProperties::ServerInfo server_info;
  2658. EXPECT_TRUE(HttpServerPropertiesManager::ParseAlternativeServiceInfo(
  2659. server, server_dict.GetDict(), &server_info));
  2660. ASSERT_TRUE(server_info.alternative_services.has_value());
  2661. AlternativeServiceInfoVector alternative_service_info_vector_out =
  2662. server_info.alternative_services.value();
  2663. ASSERT_EQ(1u, alternative_service_info_vector_out.size());
  2664. EXPECT_EQ(
  2665. kProtoQUIC,
  2666. alternative_service_info_vector_out[0].alternative_service().protocol);
  2667. // Ensure we correctly parsed the version.
  2668. EXPECT_EQ(advertised_versions,
  2669. alternative_service_info_vector_out[0].advertised_versions());
  2670. }
  2671. }
  2672. TEST_F(HttpServerPropertiesManagerTest, SameOrderAfterReload) {
  2673. const SchemefulSite kSite1(GURL("https://foo.test/"));
  2674. const SchemefulSite kSite2(GURL("https://bar.test/"));
  2675. const NetworkIsolationKey kNetworkIsolationKey1(kSite1, kSite1);
  2676. const NetworkIsolationKey kNetworkIsolationKey2(kSite2, kSite2);
  2677. base::test::ScopedFeatureList feature_list;
  2678. feature_list.InitAndEnableFeature(
  2679. features::kPartitionHttpServerPropertiesByNetworkIsolationKey);
  2680. // Create and initialize an HttpServerProperties with no state.
  2681. std::unique_ptr<MockPrefDelegate> pref_delegate =
  2682. std::make_unique<MockPrefDelegate>();
  2683. MockPrefDelegate* unowned_pref_delegate = pref_delegate.get();
  2684. std::unique_ptr<HttpServerProperties> properties =
  2685. std::make_unique<HttpServerProperties>(std::move(pref_delegate),
  2686. /*net_log=*/nullptr,
  2687. GetMockTickClock());
  2688. unowned_pref_delegate->InitializePrefs(
  2689. base::Value(base::Value::Type::DICTIONARY));
  2690. // Set alternative_service info.
  2691. base::Time expiration = base::Time::Now() + base::Days(1);
  2692. AlternativeServiceInfo alt_service1 =
  2693. AlternativeServiceInfo::CreateQuicAlternativeServiceInfo(
  2694. AlternativeService(kProtoQUIC, "1.example", 1234), expiration,
  2695. DefaultSupportedQuicVersions());
  2696. AlternativeServiceInfo alt_service2 =
  2697. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  2698. AlternativeService(kProtoHTTP2, "2.example", 443), expiration);
  2699. AlternativeServiceInfo alt_service3 =
  2700. AlternativeServiceInfo::CreateHttp2AlternativeServiceInfo(
  2701. AlternativeService(kProtoHTTP2, "3.example", 443), expiration);
  2702. const url::SchemeHostPort kServer1("https", "1.example", 443);
  2703. const url::SchemeHostPort kServer2("https", "2.example", 443);
  2704. const url::SchemeHostPort kServer3("https", "3.example", 443);
  2705. properties->SetAlternativeServices(kServer1, kNetworkIsolationKey1,
  2706. {alt_service1});
  2707. properties->SetAlternativeServices(kServer2, kNetworkIsolationKey1,
  2708. {alt_service2});
  2709. properties->SetAlternativeServices(kServer3, kNetworkIsolationKey2,
  2710. {alt_service3});
  2711. // Set quic_server_info.
  2712. quic::QuicServerId quic_server_id1("quic1.example", 80, false);
  2713. quic::QuicServerId quic_server_id2("quic2.example", 80, false);
  2714. quic::QuicServerId quic_server_id3("quic3.example", 80, false);
  2715. properties->SetQuicServerInfo(quic_server_id1, kNetworkIsolationKey1,
  2716. "quic_server_info1");
  2717. properties->SetQuicServerInfo(quic_server_id2, kNetworkIsolationKey1,
  2718. "quic_server_info2");
  2719. properties->SetQuicServerInfo(quic_server_id3, kNetworkIsolationKey2,
  2720. "quic_server_info3");
  2721. // Set broken_alternative_service info.
  2722. AlternativeService broken_service1(kProtoQUIC, "broken1.example", 443);
  2723. AlternativeService broken_service2(kProtoQUIC, "broken2.example", 443);
  2724. AlternativeService broken_service3(kProtoQUIC, "broken3.example", 443);
  2725. properties->MarkAlternativeServiceBroken(broken_service1,
  2726. kNetworkIsolationKey1);
  2727. FastForwardBy(base::Milliseconds(1));
  2728. properties->MarkAlternativeServiceBroken(broken_service2,
  2729. kNetworkIsolationKey1);
  2730. FastForwardBy(base::Milliseconds(1));
  2731. properties->MarkAlternativeServiceBroken(broken_service3,
  2732. kNetworkIsolationKey2);
  2733. // The first item of `server_info_map` must be the latest item.
  2734. EXPECT_EQ(3u, properties->server_info_map_for_testing().size());
  2735. EXPECT_EQ(
  2736. properties->server_info_map_for_testing().begin()->first.server.host(),
  2737. "3.example");
  2738. // The first item of `recently_broken_alternative_services` must be the latest
  2739. // item.
  2740. EXPECT_EQ(3u, properties->broken_alternative_services_for_testing()
  2741. .recently_broken_alternative_services()
  2742. .size());
  2743. EXPECT_EQ("broken3.example",
  2744. properties->broken_alternative_services_for_testing()
  2745. .recently_broken_alternative_services()
  2746. .begin()
  2747. ->first.alternative_service.host);
  2748. // The first item of `quic_server_info_map` must be the latest item.
  2749. EXPECT_EQ(3u, properties->quic_server_info_map_for_testing().size());
  2750. EXPECT_EQ("quic3.example", properties->quic_server_info_map_for_testing()
  2751. .begin()
  2752. ->first.server_id.host());
  2753. // The first item of `broken_alternative_service_list` must be the oldest
  2754. // item.
  2755. EXPECT_EQ(3u, properties->broken_alternative_services_for_testing()
  2756. .broken_alternative_service_list()
  2757. .size());
  2758. EXPECT_EQ("broken1.example",
  2759. properties->broken_alternative_services_for_testing()
  2760. .broken_alternative_service_list()
  2761. .begin()
  2762. ->first.alternative_service.host);
  2763. // Wait until the data's been written to prefs, and then tear down the
  2764. // HttpServerProperties.
  2765. FastForwardBy(HttpServerProperties::GetUpdatePrefsDelayForTesting());
  2766. base::Value saved_value =
  2767. unowned_pref_delegate->GetServerProperties()->Clone();
  2768. // Create a new HttpServerProperties using the value saved to prefs above.
  2769. pref_delegate = std::make_unique<MockPrefDelegate>();
  2770. unowned_pref_delegate = pref_delegate.get();
  2771. properties = std::make_unique<HttpServerProperties>(
  2772. std::move(pref_delegate), /*net_log=*/nullptr, GetMockTickClock());
  2773. unowned_pref_delegate->InitializePrefs(saved_value);
  2774. // The first item of `server_info_map` must be the latest item.
  2775. EXPECT_EQ(3u, properties->server_info_map_for_testing().size());
  2776. EXPECT_EQ(
  2777. properties->server_info_map_for_testing().begin()->first.server.host(),
  2778. "3.example");
  2779. // The first item of `recently_broken_alternative_services` must be the latest
  2780. // item.
  2781. EXPECT_EQ(3u, properties->broken_alternative_services_for_testing()
  2782. .recently_broken_alternative_services()
  2783. .size());
  2784. EXPECT_EQ("broken3.example",
  2785. properties->broken_alternative_services_for_testing()
  2786. .recently_broken_alternative_services()
  2787. .begin()
  2788. ->first.alternative_service.host);
  2789. // The first item of `quic_server_info_map` must be the latest item.
  2790. EXPECT_EQ(3u, properties->quic_server_info_map_for_testing().size());
  2791. EXPECT_EQ("quic3.example", properties->quic_server_info_map_for_testing()
  2792. .begin()
  2793. ->first.server_id.host());
  2794. // The first item of `broken_alternative_service_list` must be the oldest
  2795. // item.
  2796. EXPECT_EQ(3u, properties->broken_alternative_services_for_testing()
  2797. .broken_alternative_service_list()
  2798. .size());
  2799. EXPECT_EQ("broken1.example",
  2800. properties->broken_alternative_services_for_testing()
  2801. .broken_alternative_service_list()
  2802. .begin()
  2803. ->first.alternative_service.host);
  2804. }
  2805. } // namespace net