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

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: Adding profileManager and fixing stack-use-after-return error. 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 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 if (from_user) {
78 content::ResourceRequestInfo::AllocateForTesting(
79 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2,
80 true, false, true, true);
81 } else {
82 request->SetUserData(
83 data_use_measurement::DataUseUserData::kUserDataKey,
84 new data_use_measurement::DataUseUserData(
85 data_use_measurement::DataUseUserData::SUGGESTIONS));
86 }
87 request->Start();
88 base::RunLoop().RunUntilIdle();
89 }
90
91 } // namespace
92
93 class ChromeNetworkDelegateTest : public testing::Test {
94 public:
95 ChromeNetworkDelegateTest()
96 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
97 context_(new net::TestURLRequestContext(true)) {
98 #if defined(ENABLE_EXTENSIONS)
99 forwarder_ = new extensions::EventRouterForwarder();
100 #endif
101 }
102
103 void SetUp() override {
104 ChromeNetworkDelegate::InitializePrefsOnUIThread(
105 &enable_referrers_, nullptr, nullptr, nullptr,
106 profile_.GetTestingPrefService());
107 profile_manager_.reset(
108 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
109 ASSERT_TRUE(profile_manager_->SetUp());
110 }
111
112 void Initialize() {
113 network_delegate_.reset(
114 new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
115 context_->set_client_socket_factory(&socket_factory_);
116 context_->set_network_delegate(network_delegate_.get());
117 context_->Init();
118 }
119
120 net::TestURLRequestContext* context() { return context_.get(); }
121 net::NetworkDelegate* network_delegate() { return network_delegate_.get(); }
122 net::MockClientSocketFactory* socket_factory() { return &socket_factory_; }
123
124 extensions::EventRouterForwarder* forwarder() {
125 #if defined(ENABLE_EXTENSIONS)
126 return forwarder_.get();
38 #else 127 #else
39 delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_)); 128 return nullptr;
40 #endif 129 #endif
41 EXPECT_FALSE(delegate->FirstPartyOnlyCookieExperimentEnabled()); 130 }
42 } 131
43 132 private:
44 TEST(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { 133 scoped_ptr<TestingProfileManager> profile_manager_;
134 content::TestBrowserThreadBundle thread_bundle_;
135 #if defined(ENABLE_EXTENSIONS)
136 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
137 #endif
138 TestingProfile profile_;
139 BooleanPrefMember enable_referrers_;
140 scoped_ptr<net::NetworkDelegate> network_delegate_;
141 net::MockClientSocketFactory socket_factory_;
142 scoped_ptr<net::TestURLRequestContext> context_;
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 Initialize();
153 base::HistogramTester histogram_tester;
154
155 // A query from a service without redirection.
156 RequestURL(context(), socket_factory(), false, false);
157 histogram_tester.ExpectTotalCount(
158 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 1);
159 histogram_tester.ExpectTotalCount(
160 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 1);
161 // One upload and one download message, so totalCount should be 2.
162 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
163 histogram_tester.ExpectTotalCount(
164 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
165 histogram_tester.ExpectTotalCount(
166 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
167 }
168
169 // This function tests data use measurement for requests by user.The query from
170 // a user should affect DataUse.TrafficSize.User.Dimensions histogram. AppState
171 // and ConnectionType dimensions are always Foreground and NotCellular
172 // respectively.
173 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTest) {
174 Initialize();
175 base::HistogramTester histogram_tester;
176
177 // A query from user without redirection.
178 RequestURL(context(), socket_factory(), true, false);
179 histogram_tester.ExpectTotalCount(
180 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 1);
181 histogram_tester.ExpectTotalCount(
182 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 1);
183 histogram_tester.ExpectTotalCount(
184 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
185 histogram_tester.ExpectTotalCount(
186 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
187 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
188 }
189
190 // This function tests data use measurement for requests by services in case the
191 // request is redirected once. it makes a query which is similar to a query of a
192 // service, so it should affect DataUse.TrafficSize.System.Dimensions and
193 // DataUse.MessageSize.ServiceName histograms. AppState and ConnectionType
194 // dimensions are always Foreground and NotCellular respectively.
195 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTestWithRedirect) {
196 Initialize();
197 base::HistogramTester histogram_tester;
198
199 // A query from user with one redirection.
200 RequestURL(context(), socket_factory(), false, true);
201 histogram_tester.ExpectTotalCount(
202 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 2);
203 histogram_tester.ExpectTotalCount(
204 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 2);
205 // Two uploads and two downloads message, so totalCount should be 4.
206 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 4);
207 histogram_tester.ExpectTotalCount(
208 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
209 histogram_tester.ExpectTotalCount(
210 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
211 }
212
213 // This function tests data use measurement for requests by user in case the
214 // request is redirected once.The query from a user should affect
215 // DataUse.TrafficSize.User.Dimensions histogram. AppState and ConnectionType
216 // dimensions are always Foreground and NotCellular respectively.
217 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTestWithRedirect) {
218 Initialize();
219 base::HistogramTester histogram_tester;
220
221 // A query from user with one redirection.
222 RequestURL(context(), socket_factory(), true, true);
223
224 histogram_tester.ExpectTotalCount(
225 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 2);
226 histogram_tester.ExpectTotalCount(
227 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 2);
228 histogram_tester.ExpectTotalCount(
229 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
230 histogram_tester.ExpectTotalCount(
231 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
232 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
233 }
234
235 #endif
236
237 TEST_F(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) {
238 Initialize();
239 EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled());
240 }
241
242 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) {
45 base::CommandLine::ForCurrentProcess()->AppendSwitch( 243 base::CommandLine::ForCurrentProcess()->AppendSwitch(
46 switches::kEnableExperimentalWebPlatformFeatures); 244 switches::kEnableExperimentalWebPlatformFeatures);
47 BooleanPrefMember pref_member_; 245 Initialize();
48 scoped_ptr<ChromeNetworkDelegate> delegate; 246 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 } 247 }
59 248
60 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { 249 class ChromeNetworkDelegateSafeSearchTest : public testing::Test {
61 public: 250 public:
62 ChromeNetworkDelegateSafeSearchTest() 251 ChromeNetworkDelegateSafeSearchTest()
63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 252 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
64 #if defined(ENABLE_EXTENSIONS) 253 #if defined(ENABLE_EXTENSIONS)
65 forwarder_ = new extensions::EventRouterForwarder(); 254 forwarder_ = new extensions::EventRouterForwarder();
66 #endif 255 #endif
67 } 256 }
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 kBlockedFirstPartySite)); 450 kBlockedFirstPartySite));
262 451
263 cookie_settings_->SetCookieSetting( 452 cookie_settings_->SetCookieSetting(
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), 453 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
265 ContentSettingsPattern::Wildcard(), 454 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTING_BLOCK); 455 CONTENT_SETTING_BLOCK);
267 // Privacy mode is disabled as kAllowedSite is still getting cookies 456 // Privacy mode is disabled as kAllowedSite is still getting cookies
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, 457 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
269 kBlockedFirstPartySite)); 458 kBlockedFirstPartySite));
270 } 459 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698