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

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 reviewers' 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"
12 #include "chrome/browser/content_settings/cookie_settings_factory.h" 14 #include "chrome/browser/content_settings/cookie_settings_factory.h"
13 #include "chrome/browser/net/safe_search_util.h" 15 #include "chrome/browser/net/safe_search_util.h"
14 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_pref_service_syncable.h" 17 #include "chrome/test/base/testing_pref_service_syncable.h"
16 #include "chrome/test/base/testing_profile.h" 18 #include "chrome/test/base/testing_profile.h"
17 #include "components/content_settings/core/browser/cookie_settings.h" 19 #include "components/content_settings/core/browser/cookie_settings.h"
18 #include "components/content_settings/core/common/pref_names.h" 20 #include "components/content_settings/core/common/pref_names.h"
21 #include "content/public/browser/resource_request_info.h"
19 #include "content/public/common/content_switches.h" 22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/resource_type.h"
20 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "net/base/request_priority.h" 25 #include "net/base/request_priority.h"
26 #include "net/socket/socket_test_util.h"
22 #include "net/url_request/url_request.h" 27 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_test_util.h" 28 #include "net/url_request/url_request_test_util.h"
29 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
25 31
26 #if defined(ENABLE_EXTENSIONS) 32 #if defined(ENABLE_EXTENSIONS)
27 #include "chrome/browser/extensions/event_router_forwarder.h" 33 #include "chrome/browser/extensions/event_router_forwarder.h"
28 #endif 34 #endif
29 35
30 TEST(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) { 36 #if !defined(OS_IOS)
31 BooleanPrefMember pref_member_; 37 #include "components/data_use_measurement/core/data_use_user_data.h"
32 scoped_ptr<ChromeNetworkDelegate> delegate; 38 #endif
33 39
34 #if defined(ENABLE_EXTENSIONS) 40 namespace {
35 scoped_refptr<extensions::EventRouterForwarder> forwarder = 41
36 new extensions::EventRouterForwarder(); 42 // This function requests a URL. If |from_user| is true, it attaches a
mmenke 2015/09/04 15:52:00 Maybe "This function requests a URL." -> "This fun
amohammadkhan 2015/09/04 17:25:11 Done.
37 delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_)); 43 // ResourceRequestInfo to the URLRequest, because requests from users have
44 // this info. If |from_user| is false, the request is presumed to be from a
45 // service, and the service name is set in the request's user data. (As an
46 // example suggestions service tag is attached). if |redirect| is true, it adds
47 // necessary socket data to have a redirection before the main request.
mmenke 2015/09/04 15:52:01 "necessary socket data to have it follow redirect
amohammadkhan 2015/09/04 17:25:11 Done.
48 void RequestURL(const net::URLRequestContext* context,
mmenke 2015/09/04 15:52:00 remove const.
amohammadkhan 2015/09/04 17:25:12 Done.
49 net::MockClientSocketFactory* socket_factory,
50 bool from_user,
51 bool redirect) {
52 net::MockRead redirect_mock_reads[] = {
53 net::MockRead("HTTP/1.1 302 Found\r\n"
54 "Location: http://bar.com/\r\n\r\n"),
55 net::MockRead(""), net::MockRead(net::SYNCHRONOUS, net::OK),
mmenke 2015/09/04 15:52:01 nit: Here, and below, suggest one read per line.
amohammadkhan 2015/09/04 17:25:12 Done. Although "git cl format" makes it in that fo
mmenke 2015/09/04 20:51:45 Ahh...I'm happy to defer to git cl format, then.
56 };
57 net::StaticSocketDataProvider redirect_socket_data_provider(
58 redirect_mock_reads, arraysize(redirect_mock_reads), nullptr, 0);
59 if (redirect) {
60 socket_factory->AddSocketDataProvider(&redirect_socket_data_provider);
61 }
62
63 net::MockRead response_mock_reads[] = {
64 net::MockRead("HTTP/1.1 200 OK\r\n\r\n"), net::MockRead("response body"),
65 net::MockRead(net::SYNCHRONOUS, net::OK),
66 };
67 net::StaticSocketDataProvider response_socket_data_provider(
68 response_mock_reads, arraysize(response_mock_reads), nullptr, 0);
69 socket_factory->AddSocketDataProvider(&response_socket_data_provider);
70
71 net::TestDelegate test_delegate;
72 test_delegate.set_quit_on_complete(true);
73
74 scoped_ptr<net::URLRequest> request(context->CreateRequest(
75 GURL("http://example.com"), net::DEFAULT_PRIORITY, &test_delegate));
76 if (from_user) {
77 content::ResourceRequestInfo::AllocateForTesting(
78 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2,
79 true, false, true, true);
80 } else {
81 request->SetUserData(
82 data_use_measurement::DataUseUserData::kUserDataKey,
83 new data_use_measurement::DataUseUserData(
84 data_use_measurement::DataUseUserData::SUGGESTIONS));
85 }
86 request->Start();
87 base::RunLoop().RunUntilIdle();
88 }
89
90 } // namespace
91
92 class ChromeNetworkDelegateTest : public testing::Test {
93 public:
94 ChromeNetworkDelegateTest()
95 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
96 context_(true) {
97 #if defined(ENABLE_EXTENSIONS)
98 forwarder_ = new extensions::EventRouterForwarder();
99 #endif
100 }
101
102 void SetUp() override {
103 ChromeNetworkDelegate::InitializePrefsOnUIThread(
104 &enable_referrers_, nullptr, nullptr, nullptr,
105 profile_.GetTestingPrefService());
106 }
107
108 protected:
mmenke 2015/09/04 15:52:00 nit: Protected doesn't really get us anything in
amohammadkhan 2015/09/04 17:25:11 Done.
109 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() {
110 scoped_ptr<ChromeNetworkDelegate> network_delegate(
111 new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
112 return network_delegate.Pass();
113 }
mmenke 2015/09/04 15:52:01 You are correct. I think it's cleaner to just do:
amohammadkhan 2015/09/04 17:25:11 Done.
114
115 void SetDelegate(net::NetworkDelegate* delegate) {
116 network_delegate_ = delegate;
117 context_.set_client_socket_factory(&socket_factory_);
118 context_.set_network_delegate(network_delegate_);
119 context_.Init();
120 }
121
122 net::TestURLRequestContext* context() { return &context_; }
123 net::NetworkDelegate* network_delegate() { return network_delegate_; }
124 net::MockClientSocketFactory* socket_factory() { return &socket_factory_; }
125 extensions::EventRouterForwarder* forwarder() {
mmenke 2015/09/04 15:52:01 Suggest a blank line above forwarder, to make the
amohammadkhan 2015/09/04 17:25:11 Done.
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 content::TestBrowserThreadBundle thread_bundle_;
135 net::TestURLRequestContext context_;
136 net::NetworkDelegate* network_delegate_;
mmenke 2015/09/04 15:52:01 This should go before the context_, as the context
amohammadkhan 2015/09/04 17:25:11 Done.
137 net::MockClientSocketFactory socket_factory_;
138 #if defined(ENABLE_EXTENSIONS)
139 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
140 #endif
141 TestingProfile profile_;
142 BooleanPrefMember enable_referrers_;
mmenke 2015/09/04 15:52:00 These last three should all go before the network
amohammadkhan 2015/09/04 17:25:11 Done.
143 };
144
145 // This function tests data use measurement for requests by services. it makes a
146 // query which is similar to a query of a service, so it should affect
147 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName
148 // histograms. AppState and ConnectionType dimensions are always Foreground and
149 // NotCellular respectively.
150 #if !defined(OS_IOS)
151 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTest) {
152 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
153 SetDelegate(delegate.get());
154 base::HistogramTester histogram_tester;
155
156 // A query from a service without redirection.
157 RequestURL(context(), socket_factory(), 0, 0);
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 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
176 SetDelegate(delegate.get());
177 base::HistogramTester histogram_tester;
178
179 // A query from user without redirection.
180 RequestURL(context(), socket_factory(), 1, 0);
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,
198 DataUseMeasurementServiceTestWithRedirecting) {
mmenke 2015/09/04 15:52:01 nit: Redirecting->Redirect
amohammadkhan 2015/09/04 17:25:11 Done.
199 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
200 SetDelegate(delegate.get());
201 base::HistogramTester histogram_tester;
202
203 // A query from user with one redirection.
204 RequestURL(context(), socket_factory(), 0, 1);
205 histogram_tester.ExpectTotalCount(
206 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 2);
207 histogram_tester.ExpectTotalCount(
208 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 2);
209 // One upload and one download message, so totalCount should be 2.
210 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 4);
211 histogram_tester.ExpectTotalCount(
212 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
213 histogram_tester.ExpectTotalCount(
214 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
215 }
216
217 // This function tests data use measurement for requests by user in case the
218 // request is redirected once.The query from a user should affect
219 // DataUse.TrafficSize.User.Dimensions histogram. AppState and ConnectionType
220 // dimensions are always Foreground and NotCellular respectively.
221 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTestWithRedirecting) {
222 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
223 SetDelegate(delegate.get());
224 base::HistogramTester histogram_tester;
225
226 // A query from user with one redirection.
227 RequestURL(context(), socket_factory(), 1, 1);
228
229 histogram_tester.ExpectTotalCount(
230 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 2);
231 histogram_tester.ExpectTotalCount(
232 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 2);
233 histogram_tester.ExpectTotalCount(
234 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
235 histogram_tester.ExpectTotalCount(
236 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
237 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
238 }
239
240 #endif
241
242 TEST_F(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) {
243 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
244 SetDelegate(delegate.get());
245 EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled());
246 }
247
248 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) {
45 base::CommandLine::ForCurrentProcess()->AppendSwitch( 249 base::CommandLine::ForCurrentProcess()->AppendSwitch(
46 switches::kEnableExperimentalWebPlatformFeatures); 250 switches::kEnableExperimentalWebPlatformFeatures);
47 BooleanPrefMember pref_member_; 251 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
48 scoped_ptr<ChromeNetworkDelegate> delegate; 252 SetDelegate(delegate.get());
49 253 EXPECT_TRUE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled());
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 } 254 }
59 255
60 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { 256 class ChromeNetworkDelegateSafeSearchTest : public testing::Test {
61 public: 257 public:
62 ChromeNetworkDelegateSafeSearchTest() 258 ChromeNetworkDelegateSafeSearchTest()
63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 259 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
64 #if defined(ENABLE_EXTENSIONS) 260 #if defined(ENABLE_EXTENSIONS)
65 forwarder_ = new extensions::EventRouterForwarder(); 261 forwarder_ = new extensions::EventRouterForwarder();
66 #endif 262 #endif
67 } 263 }
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 kBlockedFirstPartySite)); 457 kBlockedFirstPartySite));
262 458
263 cookie_settings_->SetCookieSetting( 459 cookie_settings_->SetCookieSetting(
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), 460 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
265 ContentSettingsPattern::Wildcard(), 461 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTING_BLOCK); 462 CONTENT_SETTING_BLOCK);
267 // Privacy mode is disabled as kAllowedSite is still getting cookies 463 // Privacy mode is disabled as kAllowedSite is still getting cookies
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, 464 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
269 kBlockedFirstPartySite)); 465 kBlockedFirstPartySite));
270 } 466 }
OLDNEW
« no previous file with comments | « chrome/browser/net/chrome_network_delegate.cc ('k') | chrome/browser/profiles/profile_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698