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

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: Improving the naming of source_sets in gn files. 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, and makes it return a known response. If
37 delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_)); 43 // |from_user| is true, it attaches a ResourceRequestInfo to the URLRequest,
44 // because requests from users have this info. If |from_user| is false, the
45 // request is presumed to be from a service, and the service name is set in the
46 // request's user data. (As an example suggestions service tag is attached). if
47 // |redirect| is true, it adds necessary socket data to have it follow redirect
48 // before getting the final response.
49 void RequestURL(net::URLRequestContext* context,
50 net::MockClientSocketFactory* socket_factory,
51 bool from_user,
52 bool redirect) {
53 net::MockRead redirect_mock_reads[] = {
54 net::MockRead("HTTP/1.1 302 Found\r\n"
55 "Location: http://bar.com/\r\n\r\n"),
56 net::MockRead(""),
mmenke 2015/09/04 20:51:45 This isn't needed (And is redundant with the next
amohammadkhan 2015/09/05 00:58:02 Done.
57 net::MockRead(net::SYNCHRONOUS, net::OK),
58 };
59 net::StaticSocketDataProvider redirect_socket_data_provider(
60 redirect_mock_reads, arraysize(redirect_mock_reads), nullptr, 0);
61 if (redirect) {
62 socket_factory->AddSocketDataProvider(&redirect_socket_data_provider);
63 }
mmenke 2015/09/04 20:51:45 nit: Don't use braces on single-line if.
amohammadkhan 2015/09/05 00:58:01 Done.
64
65 net::MockRead response_mock_reads[] = {
66 net::MockRead("HTTP/1.1 200 OK\r\n\r\n"),
67 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
74 net::TestDelegate test_delegate;
75 test_delegate.set_quit_on_complete(true);
76
77 scoped_ptr<net::URLRequest> request(context->CreateRequest(
78 GURL("http://example.com"), net::DEFAULT_PRIORITY, &test_delegate));
mmenke 2015/09/04 20:51:45 nit: include "url/gurl.h"
amohammadkhan 2015/09/05 00:58:01 Done.
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 }
110
111 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() {
mmenke 2015/09/04 20:51:45 Should get rid of this method and SetDelegate, and
amohammadkhan 2015/09/05 00:58:01 Done.
112 scoped_ptr<ChromeNetworkDelegate> network_delegate(
113 new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
114 return network_delegate.Pass();
115 }
116
117 void SetDelegate(net::NetworkDelegate* delegate) {
118 context_->set_client_socket_factory(&socket_factory_);
119 context_->set_network_delegate(network_delegate_.get());
120 context_->Init();
121 }
122
123 void Initialize() {
124 network_delegate_ = CreateNetworkDelegate();
125 SetDelegate(network_delegate_.get());
126 }
127
128 net::TestURLRequestContext* context() { return context_.get(); }
129 net::NetworkDelegate* network_delegate() { return network_delegate_.get(); }
130 net::MockClientSocketFactory* socket_factory() { return &socket_factory_; }
131
132 extensions::EventRouterForwarder* forwarder() {
mmenke 2015/09/04 20:51:45 This code doesn't really depend on whether or not
amohammadkhan 2015/09/05 00:58:02 I tried to remove it, it give me the following err
133 #if defined(ENABLE_EXTENSIONS)
134 return forwarder_.get();
38 #else 135 #else
39 delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_)); 136 return nullptr;
40 #endif 137 #endif
41 EXPECT_FALSE(delegate->FirstPartyOnlyCookieExperimentEnabled()); 138 }
42 } 139
43 140 private:
44 TEST(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { 141 content::TestBrowserThreadBundle thread_bundle_;
142 #if defined(ENABLE_EXTENSIONS)
143 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
144 #endif
145 TestingProfile profile_;
146 BooleanPrefMember enable_referrers_;
147 scoped_ptr<net::NetworkDelegate> network_delegate_;
148 scoped_ptr<net::TestURLRequestContext> context_;
149 net::MockClientSocketFactory socket_factory_;
mmenke 2015/09/04 20:51:45 This should be above TestURLRequestContext
amohammadkhan 2015/09/05 00:58:02 Done.
150
mmenke 2015/09/04 20:51:45 nit: Blank line not needed.
amohammadkhan 2015/09/05 00:58:01 Done.
151 };
152
153 // This function tests data use measurement for requests by services. it makes a
154 // query which is similar to a query of a service, so it should affect
155 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName
156 // histograms. AppState and ConnectionType dimensions are always Foreground and
157 // NotCellular respectively.
158 #if !defined(OS_IOS)
159 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTest) {
160 Initialize();
161 base::HistogramTester histogram_tester;
162
163 // A query from a service without redirection.
164 RequestURL(context(), socket_factory(), 0, 0);
mmenke 2015/09/04 20:51:45 0, 0 -> false, false
amohammadkhan 2015/09/05 00:58:02 Done.
165 histogram_tester.ExpectTotalCount(
166 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 1);
167 histogram_tester.ExpectTotalCount(
168 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 1);
169 // One upload and one download message, so totalCount should be 2.
170 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
171 histogram_tester.ExpectTotalCount(
172 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
173 histogram_tester.ExpectTotalCount(
174 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
175 }
176
177 // This function tests data use measurement for requests by user.The query from
178 // a user should affect DataUse.TrafficSize.User.Dimensions histogram. AppState
179 // and ConnectionType dimensions are always Foreground and NotCellular
180 // respectively.
181 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTest) {
182 Initialize();
183 base::HistogramTester histogram_tester;
184
185 // A query from user without redirection.
186 RequestURL(context(), socket_factory(), 1, 0);
mmenke 2015/09/04 20:51:45 1, 0 -> true, false
amohammadkhan 2015/09/05 00:58:02 Done.
187 histogram_tester.ExpectTotalCount(
188 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 1);
189 histogram_tester.ExpectTotalCount(
190 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 1);
191 histogram_tester.ExpectTotalCount(
192 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
193 histogram_tester.ExpectTotalCount(
194 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
195 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
196 }
197
198 // This function tests data use measurement for requests by services in case the
199 // request is redirected once. it makes a query which is similar to a query of a
200 // service, so it should affect DataUse.TrafficSize.System.Dimensions and
201 // DataUse.MessageSize.ServiceName histograms. AppState and ConnectionType
202 // dimensions are always Foreground and NotCellular respectively.
203 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTestWithRedirect) {
204 Initialize();
205 base::HistogramTester histogram_tester;
206
207 // A query from user with one redirection.
208 RequestURL(context(), socket_factory(), 0, 1);
mmenke 2015/09/04 20:51:45 false/true
amohammadkhan 2015/09/05 00:58:01 Done.
209 histogram_tester.ExpectTotalCount(
210 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 2);
211 histogram_tester.ExpectTotalCount(
212 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 2);
213 // Two uploads and two downloads message, so totalCount should be 4.
214 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 4);
215 histogram_tester.ExpectTotalCount(
216 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
217 histogram_tester.ExpectTotalCount(
218 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
219 }
220
221 // This function tests data use measurement for requests by user in case the
222 // request is redirected once.The query from a user should affect
223 // DataUse.TrafficSize.User.Dimensions histogram. AppState and ConnectionType
224 // dimensions are always Foreground and NotCellular respectively.
225 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTestWithRedirect) {
226 Initialize();
227 base::HistogramTester histogram_tester;
228
229 // A query from user with one redirection.
230 RequestURL(context(), socket_factory(), 1, 1);
mmenke 2015/09/04 20:51:45 true/true
amohammadkhan 2015/09/05 00:58:01 Done.
231
232 histogram_tester.ExpectTotalCount(
233 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 2);
234 histogram_tester.ExpectTotalCount(
235 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 2);
236 histogram_tester.ExpectTotalCount(
237 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
238 histogram_tester.ExpectTotalCount(
239 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
240 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
241 }
242
243 #endif
244
245 TEST_F(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) {
246 Initialize();
247 EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled());
248 }
249
250 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) {
45 base::CommandLine::ForCurrentProcess()->AppendSwitch( 251 base::CommandLine::ForCurrentProcess()->AppendSwitch(
46 switches::kEnableExperimentalWebPlatformFeatures); 252 switches::kEnableExperimentalWebPlatformFeatures);
47 BooleanPrefMember pref_member_; 253 Initialize();
48 scoped_ptr<ChromeNetworkDelegate> delegate; 254 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 } 255 }
59 256
60 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { 257 class ChromeNetworkDelegateSafeSearchTest : public testing::Test {
61 public: 258 public:
62 ChromeNetworkDelegateSafeSearchTest() 259 ChromeNetworkDelegateSafeSearchTest()
63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 260 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
64 #if defined(ENABLE_EXTENSIONS) 261 #if defined(ENABLE_EXTENSIONS)
65 forwarder_ = new extensions::EventRouterForwarder(); 262 forwarder_ = new extensions::EventRouterForwarder();
66 #endif 263 #endif
67 } 264 }
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 kBlockedFirstPartySite)); 458 kBlockedFirstPartySite));
262 459
263 cookie_settings_->SetCookieSetting( 460 cookie_settings_->SetCookieSetting(
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), 461 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
265 ContentSettingsPattern::Wildcard(), 462 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTING_BLOCK); 463 CONTENT_SETTING_BLOCK);
267 // Privacy mode is disabled as kAllowedSite is still getting cookies 464 // Privacy mode is disabled as kAllowedSite is still getting cookies
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, 465 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
269 kBlockedFirstPartySite)); 466 kBlockedFirstPartySite));
270 } 467 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698