| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chrome/browser/net/chrome_network_delegate.h" | 5 #include "chrome/browser/net/chrome_network_delegate.h" | 
| 6 | 6 | 
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" | 
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" | 
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" | 
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" | 
| 11 #include "base/prefs/pref_member.h" | 11 #include "base/prefs/pref_member.h" | 
|  | 12 #include "base/run_loop.h" | 
|  | 13 #include "base/test/histogram_tester.h" | 
|  | 14 #include "chrome/browser/browser_process.h" | 
| 12 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 15 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 
| 13 #include "chrome/browser/net/safe_search_util.h" | 16 #include "chrome/browser/net/safe_search_util.h" | 
| 14 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" | 
|  | 18 #include "chrome/test/base/testing_browser_process.h" | 
| 15 #include "chrome/test/base/testing_pref_service_syncable.h" | 19 #include "chrome/test/base/testing_pref_service_syncable.h" | 
| 16 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" | 
|  | 21 #include "chrome/test/base/testing_profile_manager.h" | 
| 17 #include "components/content_settings/core/browser/cookie_settings.h" | 22 #include "components/content_settings/core/browser/cookie_settings.h" | 
| 18 #include "components/content_settings/core/common/pref_names.h" | 23 #include "components/content_settings/core/common/pref_names.h" | 
|  | 24 #include "content/public/browser/resource_request_info.h" | 
| 19 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" | 
|  | 26 #include "content/public/common/resource_type.h" | 
| 20 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" | 
| 21 #include "net/base/request_priority.h" | 28 #include "net/base/request_priority.h" | 
|  | 29 #include "net/socket/socket_test_util.h" | 
| 22 #include "net/url_request/url_request.h" | 30 #include "net/url_request/url_request.h" | 
| 23 #include "net/url_request/url_request_test_util.h" | 31 #include "net/url_request/url_request_test_util.h" | 
| 24 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" | 
|  | 33 #include "url/gurl.h" | 
| 25 | 34 | 
| 26 #if defined(ENABLE_EXTENSIONS) | 35 #if defined(ENABLE_EXTENSIONS) | 
| 27 #include "chrome/browser/extensions/event_router_forwarder.h" | 36 #include "chrome/browser/extensions/event_router_forwarder.h" | 
| 28 #endif | 37 #endif | 
| 29 | 38 | 
| 30 TEST(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) { | 39 #if !defined(OS_IOS) | 
| 31   BooleanPrefMember pref_member_; | 40 #include "components/data_use_measurement/core/data_use_user_data.h" | 
| 32   scoped_ptr<ChromeNetworkDelegate> delegate; | 41 #endif | 
| 33 | 42 | 
| 34 #if defined(ENABLE_EXTENSIONS) | 43 namespace { | 
| 35   scoped_refptr<extensions::EventRouterForwarder> forwarder = | 44 | 
| 36       new extensions::EventRouterForwarder(); | 45 // This function requests a URL, and makes it return a known response. If | 
| 37   delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_)); | 46 // |from_user| is true, it attaches a ResourceRequestInfo to the URLRequest, | 
|  | 47 // because requests from users have this info. If |from_user| is false, the | 
|  | 48 // request is presumed to be from a service, and the service name is set in the | 
|  | 49 // request's user data. (As an example suggestions service tag is attached). if | 
|  | 50 // |redirect| is true, it adds necessary socket data to have it follow redirect | 
|  | 51 // before getting the final response. | 
|  | 52 void RequestURL(net::URLRequestContext* context, | 
|  | 53                 net::MockClientSocketFactory* socket_factory, | 
|  | 54                 bool from_user, | 
|  | 55                 bool redirect) { | 
|  | 56   net::MockRead redirect_mock_reads[] = { | 
|  | 57       net::MockRead("HTTP/1.1 302 Found\r\n" | 
|  | 58                     "Location: http://bar.com/\r\n\r\n"), | 
|  | 59       net::MockRead(net::SYNCHRONOUS, net::OK), | 
|  | 60   }; | 
|  | 61   net::StaticSocketDataProvider redirect_socket_data_provider( | 
|  | 62       redirect_mock_reads, arraysize(redirect_mock_reads), nullptr, 0); | 
|  | 63 | 
|  | 64   if (redirect) | 
|  | 65     socket_factory->AddSocketDataProvider(&redirect_socket_data_provider); | 
|  | 66   net::MockRead response_mock_reads[] = { | 
|  | 67       net::MockRead("HTTP/1.1 200 OK\r\n\r\n"), net::MockRead("response body"), | 
|  | 68       net::MockRead(net::SYNCHRONOUS, net::OK), | 
|  | 69   }; | 
|  | 70   net::StaticSocketDataProvider response_socket_data_provider( | 
|  | 71       response_mock_reads, arraysize(response_mock_reads), nullptr, 0); | 
|  | 72   socket_factory->AddSocketDataProvider(&response_socket_data_provider); | 
|  | 73   net::TestDelegate test_delegate; | 
|  | 74   test_delegate.set_quit_on_complete(true); | 
|  | 75   scoped_ptr<net::URLRequest> request(context->CreateRequest( | 
|  | 76       GURL("http://example.com"), net::DEFAULT_PRIORITY, &test_delegate)); | 
|  | 77 | 
|  | 78   if (from_user) { | 
|  | 79     content::ResourceRequestInfo::AllocateForTesting( | 
|  | 80         request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2, | 
|  | 81         true, false, true, true); | 
|  | 82   } else { | 
|  | 83     request->SetUserData( | 
|  | 84         data_use_measurement::DataUseUserData::kUserDataKey, | 
|  | 85         new data_use_measurement::DataUseUserData( | 
|  | 86             data_use_measurement::DataUseUserData::SUGGESTIONS)); | 
|  | 87   } | 
|  | 88   request->Start(); | 
|  | 89   base::RunLoop().RunUntilIdle(); | 
|  | 90 } | 
|  | 91 | 
|  | 92 }  // namespace | 
|  | 93 | 
|  | 94 class ChromeNetworkDelegateTest : public testing::Test { | 
|  | 95  public: | 
|  | 96   ChromeNetworkDelegateTest() | 
|  | 97       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 
|  | 98         context_(new net::TestURLRequestContext(true)) { | 
|  | 99 #if defined(ENABLE_EXTENSIONS) | 
|  | 100     forwarder_ = new extensions::EventRouterForwarder(); | 
|  | 101 #endif | 
|  | 102   } | 
|  | 103 | 
|  | 104   void SetUp() override { | 
|  | 105     ChromeNetworkDelegate::InitializePrefsOnUIThread( | 
|  | 106         &enable_referrers_, nullptr, nullptr, nullptr, | 
|  | 107         profile_.GetTestingPrefService()); | 
|  | 108     profile_manager_.reset( | 
|  | 109         new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | 
|  | 110     ASSERT_TRUE(profile_manager_->SetUp()); | 
|  | 111   } | 
|  | 112 | 
|  | 113   void Initialize() { | 
|  | 114     network_delegate_.reset( | 
|  | 115         new ChromeNetworkDelegate(forwarder(), &enable_referrers_)); | 
|  | 116     context_->set_client_socket_factory(&socket_factory_); | 
|  | 117     context_->set_network_delegate(network_delegate_.get()); | 
|  | 118     context_->Init(); | 
|  | 119   } | 
|  | 120 | 
|  | 121   net::TestURLRequestContext* context() { return context_.get(); } | 
|  | 122   net::NetworkDelegate* network_delegate() { return network_delegate_.get(); } | 
|  | 123   net::MockClientSocketFactory* socket_factory() { return &socket_factory_; } | 
|  | 124 | 
|  | 125   extensions::EventRouterForwarder* forwarder() { | 
|  | 126 #if defined(ENABLE_EXTENSIONS) | 
|  | 127     return forwarder_.get(); | 
| 38 #else | 128 #else | 
| 39   delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_)); | 129     return nullptr; | 
| 40 #endif | 130 #endif | 
| 41   EXPECT_FALSE(delegate->FirstPartyOnlyCookieExperimentEnabled()); | 131   } | 
| 42 } | 132 | 
| 43 | 133  private: | 
| 44 TEST(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { | 134   scoped_ptr<TestingProfileManager> profile_manager_; | 
|  | 135   content::TestBrowserThreadBundle thread_bundle_; | 
|  | 136 #if defined(ENABLE_EXTENSIONS) | 
|  | 137   scoped_refptr<extensions::EventRouterForwarder> forwarder_; | 
|  | 138 #endif | 
|  | 139   TestingProfile profile_; | 
|  | 140   BooleanPrefMember enable_referrers_; | 
|  | 141   scoped_ptr<net::NetworkDelegate> network_delegate_; | 
|  | 142   net::MockClientSocketFactory socket_factory_; | 
|  | 143   scoped_ptr<net::TestURLRequestContext> context_; | 
|  | 144 }; | 
|  | 145 | 
|  | 146 // This function tests data use measurement for requests by services. it makes a | 
|  | 147 // query which is similar to a query of a service, so it should affect | 
|  | 148 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName | 
|  | 149 // histograms. AppState and ConnectionType dimensions are always Foreground and | 
|  | 150 // NotCellular respectively. | 
|  | 151 #if !defined(OS_IOS) | 
|  | 152 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTest) { | 
|  | 153   Initialize(); | 
|  | 154   base::HistogramTester histogram_tester; | 
|  | 155 | 
|  | 156   // A query from a service without redirection. | 
|  | 157   RequestURL(context(), socket_factory(), false, false); | 
|  | 158   histogram_tester.ExpectTotalCount( | 
|  | 159       "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 1); | 
|  | 160   histogram_tester.ExpectTotalCount( | 
|  | 161       "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 1); | 
|  | 162   // One upload and one download message, so totalCount should be 2. | 
|  | 163   histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); | 
|  | 164   histogram_tester.ExpectTotalCount( | 
|  | 165       "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0); | 
|  | 166   histogram_tester.ExpectTotalCount( | 
|  | 167       "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0); | 
|  | 168 } | 
|  | 169 | 
|  | 170 // This function tests data use measurement for requests by user.The query from | 
|  | 171 // a user should affect DataUse.TrafficSize.User.Dimensions histogram. AppState | 
|  | 172 // and ConnectionType dimensions are always Foreground and NotCellular | 
|  | 173 // respectively. | 
|  | 174 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTest) { | 
|  | 175   Initialize(); | 
|  | 176   base::HistogramTester histogram_tester; | 
|  | 177 | 
|  | 178   // A query from user without redirection. | 
|  | 179   RequestURL(context(), socket_factory(), true, false); | 
|  | 180   histogram_tester.ExpectTotalCount( | 
|  | 181       "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 1); | 
|  | 182   histogram_tester.ExpectTotalCount( | 
|  | 183       "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 1); | 
|  | 184   histogram_tester.ExpectTotalCount( | 
|  | 185       "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0); | 
|  | 186   histogram_tester.ExpectTotalCount( | 
|  | 187       "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0); | 
|  | 188   histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0); | 
|  | 189 } | 
|  | 190 | 
|  | 191 // This function tests data use measurement for requests by services in case the | 
|  | 192 // request is redirected once. it makes a query which is similar to a query of a | 
|  | 193 // service, so it should affect DataUse.TrafficSize.System.Dimensions and | 
|  | 194 // DataUse.MessageSize.ServiceName histograms. AppState and ConnectionType | 
|  | 195 // dimensions are always Foreground and NotCellular respectively. | 
|  | 196 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTestWithRedirect) { | 
|  | 197   Initialize(); | 
|  | 198   base::HistogramTester histogram_tester; | 
|  | 199 | 
|  | 200   // A query from user with one redirection. | 
|  | 201   RequestURL(context(), socket_factory(), false, true); | 
|  | 202   histogram_tester.ExpectTotalCount( | 
|  | 203       "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 2); | 
|  | 204   histogram_tester.ExpectTotalCount( | 
|  | 205       "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 2); | 
|  | 206   // Two uploads and two downloads message, so totalCount should be 4. | 
|  | 207   histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 4); | 
|  | 208   histogram_tester.ExpectTotalCount( | 
|  | 209       "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0); | 
|  | 210   histogram_tester.ExpectTotalCount( | 
|  | 211       "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0); | 
|  | 212 } | 
|  | 213 | 
|  | 214 // This function tests data use measurement for requests by user in case the | 
|  | 215 // request is redirected once.The query from a user should affect | 
|  | 216 // DataUse.TrafficSize.User.Dimensions histogram. AppState and ConnectionType | 
|  | 217 // dimensions are always Foreground and NotCellular respectively. | 
|  | 218 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTestWithRedirect) { | 
|  | 219   Initialize(); | 
|  | 220   base::HistogramTester histogram_tester; | 
|  | 221 | 
|  | 222   // A query from user with one redirection. | 
|  | 223   RequestURL(context(), socket_factory(), true, true); | 
|  | 224 | 
|  | 225   histogram_tester.ExpectTotalCount( | 
|  | 226       "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 2); | 
|  | 227   histogram_tester.ExpectTotalCount( | 
|  | 228       "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 2); | 
|  | 229   histogram_tester.ExpectTotalCount( | 
|  | 230       "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0); | 
|  | 231   histogram_tester.ExpectTotalCount( | 
|  | 232       "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0); | 
|  | 233   histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0); | 
|  | 234 } | 
|  | 235 | 
|  | 236 #endif | 
|  | 237 | 
|  | 238 TEST_F(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) { | 
|  | 239   Initialize(); | 
|  | 240   EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); | 
|  | 241 } | 
|  | 242 | 
|  | 243 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { | 
| 45   base::CommandLine::ForCurrentProcess()->AppendSwitch( | 244   base::CommandLine::ForCurrentProcess()->AppendSwitch( | 
| 46       switches::kEnableExperimentalWebPlatformFeatures); | 245       switches::kEnableExperimentalWebPlatformFeatures); | 
| 47   BooleanPrefMember pref_member_; | 246   Initialize(); | 
| 48   scoped_ptr<ChromeNetworkDelegate> delegate; | 247   EXPECT_TRUE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); | 
| 49 |  | 
| 50 #if defined(ENABLE_EXTENSIONS) |  | 
| 51   scoped_refptr<extensions::EventRouterForwarder> forwarder = |  | 
| 52       new extensions::EventRouterForwarder(); |  | 
| 53   delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_)); |  | 
| 54 #else |  | 
| 55   delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_)); |  | 
| 56 #endif |  | 
| 57   EXPECT_TRUE(delegate->FirstPartyOnlyCookieExperimentEnabled()); |  | 
| 58 } | 248 } | 
| 59 | 249 | 
| 60 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { | 250 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { | 
| 61  public: | 251  public: | 
| 62   ChromeNetworkDelegateSafeSearchTest() | 252   ChromeNetworkDelegateSafeSearchTest() | 
| 63       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 253       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 
| 64 #if defined(ENABLE_EXTENSIONS) | 254 #if defined(ENABLE_EXTENSIONS) | 
| 65     forwarder_ = new extensions::EventRouterForwarder(); | 255     forwarder_ = new extensions::EventRouterForwarder(); | 
| 66 #endif | 256 #endif | 
| 67   } | 257   } | 
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 261                                                        kBlockedFirstPartySite)); | 451                                                        kBlockedFirstPartySite)); | 
| 262 | 452 | 
| 263   cookie_settings_->SetCookieSetting( | 453   cookie_settings_->SetCookieSetting( | 
| 264       ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 454       ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 
| 265       ContentSettingsPattern::Wildcard(), | 455       ContentSettingsPattern::Wildcard(), | 
| 266       CONTENT_SETTING_BLOCK); | 456       CONTENT_SETTING_BLOCK); | 
| 267   // Privacy mode is disabled as kAllowedSite is still getting cookies | 457   // Privacy mode is disabled as kAllowedSite is still getting cookies | 
| 268   EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 458   EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 
| 269                                                        kBlockedFirstPartySite)); | 459                                                        kBlockedFirstPartySite)); | 
| 270 } | 460 } | 
| OLD | NEW | 
|---|