Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/net/chrome_network_delegate_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698