1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/macros.h"
6 #include "base/memory/ptr_util.h"
7 #include "base/run_loop.h"
8 #include "base/test/task_environment.h"
9 #include "components/prefs/testing_pref_service.h"
10 #include "components/web_resource/eula_accepted_notifier.h"
11 #include "components/web_resource/resource_request_allowed_notifier_test_util.h"
12 #include "services/network/test/test_network_connection_tracker.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace web_resource {
16
17 // EulaAcceptedNotifier test class that allows mocking the EULA accepted state
18 // and issuing simulated notifications.
19 class TestEulaAcceptedNotifier : public EulaAcceptedNotifier {
20 public:
TestEulaAcceptedNotifier()21 TestEulaAcceptedNotifier()
22 : EulaAcceptedNotifier(nullptr),
23 eula_accepted_(false) {
24 }
~TestEulaAcceptedNotifier()25 ~TestEulaAcceptedNotifier() override {}
26
IsEulaAccepted()27 bool IsEulaAccepted() override { return eula_accepted_; }
28
SetEulaAcceptedForTesting(bool eula_accepted)29 void SetEulaAcceptedForTesting(bool eula_accepted) {
30 eula_accepted_ = eula_accepted;
31 }
32
SimulateEulaAccepted()33 void SimulateEulaAccepted() {
34 NotifyObserver();
35 }
36
37 private:
38 bool eula_accepted_;
39
40 DISALLOW_COPY_AND_ASSIGN(TestEulaAcceptedNotifier);
41 };
42
43 enum class ConnectionTrackerResponseMode {
44 kSynchronous,
45 kAsynchronous,
46 };
47
48 // A test fixture class for ResourceRequestAllowedNotifier tests that require
49 // network state simulations. This also acts as the service implementing the
50 // ResourceRequestAllowedNotifier::Observer interface.
51 class ResourceRequestAllowedNotifierTest
52 : public testing::Test,
53 public ResourceRequestAllowedNotifier::Observer,
54 public testing::WithParamInterface<ConnectionTrackerResponseMode> {
55 public:
ResourceRequestAllowedNotifierTest()56 ResourceRequestAllowedNotifierTest()
57 : resource_request_allowed_notifier_(
58 &prefs_,
59 network::TestNetworkConnectionTracker::GetInstance()),
60 eula_notifier_(new TestEulaAcceptedNotifier),
61 was_notified_(false) {
62 auto* tracker = network::TestNetworkConnectionTracker::GetInstance();
63 tracker->SetRespondSynchronously(
64 GetParam() == ConnectionTrackerResponseMode::kSynchronous);
65 tracker->SetConnectionType(network::mojom::ConnectionType::CONNECTION_WIFI);
66
67 resource_request_allowed_notifier_.InitWithEulaAcceptNotifier(
68 this, base::WrapUnique(eula_notifier_));
69 }
~ResourceRequestAllowedNotifierTest()70 ~ResourceRequestAllowedNotifierTest() override {}
71
was_notified() const72 bool was_notified() const { return was_notified_; }
73
74 // ResourceRequestAllowedNotifier::Observer override:
OnResourceRequestsAllowed()75 void OnResourceRequestsAllowed() override { was_notified_ = true; }
76
SimulateNetworkConnectionChange(network::mojom::ConnectionType type)77 void SimulateNetworkConnectionChange(network::mojom::ConnectionType type) {
78 network::TestNetworkConnectionTracker::GetInstance()->SetConnectionType(
79 type);
80 base::RunLoop().RunUntilIdle();
81 }
82
83 // Simulate a resource request from the test service. It returns true if
84 // resource request is allowed. Otherwise returns false and will change the
85 // result of was_notified() to true when the request is allowed.
SimulateResourceRequest()86 bool SimulateResourceRequest() {
87 return resource_request_allowed_notifier_.ResourceRequestsAllowed();
88 }
89
SimulateEulaAccepted()90 void SimulateEulaAccepted() {
91 eula_notifier_->SimulateEulaAccepted();
92 }
93
94 // Eula manipulation methods:
SetNeedsEulaAcceptance(bool needs_acceptance)95 void SetNeedsEulaAcceptance(bool needs_acceptance) {
96 eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance);
97 }
98
SetWaitingForEula(bool waiting)99 void SetWaitingForEula(bool waiting) {
100 resource_request_allowed_notifier_.SetWaitingForEulaForTesting(waiting);
101 }
102
103 // Used in tests involving the EULA. Disables both the EULA accepted state
104 // and the network.
DisableEulaAndNetwork()105 void DisableEulaAndNetwork() {
106 SimulateNetworkConnectionChange(
107 network::mojom::ConnectionType::CONNECTION_NONE);
108 SetWaitingForEula(true);
109 SetNeedsEulaAcceptance(true);
110 }
111
SetUp()112 void SetUp() override {
113 // Assume the test service has already requested permission, as all tests
114 // just test that criteria changes notify the server.
115 // Set default EULA state to done (not waiting and EULA accepted) to
116 // simplify non-ChromeOS tests.
117 SetWaitingForEula(false);
118 SetNeedsEulaAcceptance(false);
119 }
120
121 private:
122 base::test::SingleThreadTaskEnvironment task_environment_{
123 base::test::SingleThreadTaskEnvironment::MainThreadType::UI};
124 TestRequestAllowedNotifier resource_request_allowed_notifier_;
125 TestingPrefServiceSimple prefs_;
126 TestEulaAcceptedNotifier* eula_notifier_; // Weak, owned by RRAN.
127 bool was_notified_;
128
129 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest);
130 };
131
TEST_P(ResourceRequestAllowedNotifierTest,NotifyOnInitialNetworkState)132 TEST_P(ResourceRequestAllowedNotifierTest, NotifyOnInitialNetworkState) {
133 if (GetParam() == ConnectionTrackerResponseMode::kSynchronous) {
134 EXPECT_TRUE(SimulateResourceRequest());
135 } else {
136 EXPECT_FALSE(SimulateResourceRequest());
137 base::RunLoop().RunUntilIdle();
138 EXPECT_TRUE(was_notified());
139 }
140 }
141
TEST_P(ResourceRequestAllowedNotifierTest,DoNotNotifyIfOffline)142 TEST_P(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) {
143 SimulateNetworkConnectionChange(
144 network::mojom::ConnectionType::CONNECTION_NONE);
145 EXPECT_FALSE(SimulateResourceRequest());
146
147 SimulateNetworkConnectionChange(
148 network::mojom::ConnectionType::CONNECTION_NONE);
149 EXPECT_FALSE(was_notified());
150 }
151
TEST_P(ResourceRequestAllowedNotifierTest,DoNotNotifyIfOnlineToOnline)152 TEST_P(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) {
153 SimulateNetworkConnectionChange(
154 network::mojom::ConnectionType::CONNECTION_WIFI);
155 EXPECT_TRUE(SimulateResourceRequest());
156
157 SimulateNetworkConnectionChange(
158 network::mojom::ConnectionType::CONNECTION_ETHERNET);
159 EXPECT_FALSE(was_notified());
160 }
161
TEST_P(ResourceRequestAllowedNotifierTest,NotifyOnReconnect)162 TEST_P(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) {
163 SimulateNetworkConnectionChange(
164 network::mojom::ConnectionType::CONNECTION_NONE);
165 EXPECT_FALSE(SimulateResourceRequest());
166
167 SimulateNetworkConnectionChange(
168 network::mojom::ConnectionType::CONNECTION_ETHERNET);
169 EXPECT_TRUE(was_notified());
170 }
171
TEST_P(ResourceRequestAllowedNotifierTest,NoNotifyOnWardriving)172 TEST_P(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) {
173 SimulateNetworkConnectionChange(
174 network::mojom::ConnectionType::CONNECTION_WIFI);
175 EXPECT_TRUE(SimulateResourceRequest());
176
177 SimulateNetworkConnectionChange(
178 network::mojom::ConnectionType::CONNECTION_WIFI);
179 EXPECT_FALSE(was_notified());
180 SimulateNetworkConnectionChange(
181 network::mojom::ConnectionType::CONNECTION_3G);
182 EXPECT_FALSE(was_notified());
183 SimulateNetworkConnectionChange(
184 network::mojom::ConnectionType::CONNECTION_4G);
185 EXPECT_FALSE(was_notified());
186 SimulateNetworkConnectionChange(
187 network::mojom::ConnectionType::CONNECTION_WIFI);
188 EXPECT_FALSE(was_notified());
189 }
190
TEST_P(ResourceRequestAllowedNotifierTest,NoNotifyOnFlakyConnection)191 TEST_P(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) {
192 SimulateNetworkConnectionChange(
193 network::mojom::ConnectionType::CONNECTION_WIFI);
194 EXPECT_TRUE(SimulateResourceRequest());
195
196 SimulateNetworkConnectionChange(
197 network::mojom::ConnectionType::CONNECTION_WIFI);
198 EXPECT_FALSE(was_notified());
199 SimulateNetworkConnectionChange(
200 network::mojom::ConnectionType::CONNECTION_NONE);
201 EXPECT_FALSE(was_notified());
202 SimulateNetworkConnectionChange(
203 network::mojom::ConnectionType::CONNECTION_WIFI);
204 EXPECT_FALSE(was_notified());
205 }
206
TEST_P(ResourceRequestAllowedNotifierTest,NotifyOnFlakyConnection)207 TEST_P(ResourceRequestAllowedNotifierTest, NotifyOnFlakyConnection) {
208 // First, the observer queries the state while the network is connected.
209 SimulateNetworkConnectionChange(
210 network::mojom::ConnectionType::CONNECTION_WIFI);
211 EXPECT_TRUE(SimulateResourceRequest());
212
213 SimulateNetworkConnectionChange(
214 network::mojom::ConnectionType::CONNECTION_WIFI);
215 EXPECT_FALSE(was_notified());
216 SimulateNetworkConnectionChange(
217 network::mojom::ConnectionType::CONNECTION_NONE);
218 EXPECT_FALSE(was_notified());
219
220 // Now, the observer queries the state while the network is disconnected.
221 EXPECT_FALSE(SimulateResourceRequest());
222
223 SimulateNetworkConnectionChange(
224 network::mojom::ConnectionType::CONNECTION_WIFI);
225 EXPECT_TRUE(was_notified());
226 }
227
TEST_P(ResourceRequestAllowedNotifierTest,NoNotifyOnEulaAfterGoOffline)228 TEST_P(ResourceRequestAllowedNotifierTest, NoNotifyOnEulaAfterGoOffline) {
229 DisableEulaAndNetwork();
230 EXPECT_FALSE(SimulateResourceRequest());
231
232 SimulateNetworkConnectionChange(
233 network::mojom::ConnectionType::CONNECTION_WIFI);
234 EXPECT_FALSE(was_notified());
235 SimulateNetworkConnectionChange(
236 network::mojom::ConnectionType::CONNECTION_NONE);
237 EXPECT_FALSE(was_notified());
238 SimulateEulaAccepted();
239 EXPECT_FALSE(was_notified());
240 }
241
TEST_P(ResourceRequestAllowedNotifierTest,NoRequestNoNotify)242 TEST_P(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) {
243 // Ensure that if the observing service does not request access, it does not
244 // get notified, even if the criteria are met. Note that this is done by not
245 // calling SimulateResourceRequest here.
246 SimulateNetworkConnectionChange(
247 network::mojom::ConnectionType::CONNECTION_NONE);
248 SimulateNetworkConnectionChange(
249 network::mojom::ConnectionType::CONNECTION_ETHERNET);
250 EXPECT_FALSE(was_notified());
251 }
252
TEST_P(ResourceRequestAllowedNotifierTest,EulaOnlyNetworkOffline)253 TEST_P(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) {
254 DisableEulaAndNetwork();
255 EXPECT_FALSE(SimulateResourceRequest());
256
257 SimulateEulaAccepted();
258 EXPECT_FALSE(was_notified());
259 }
260
TEST_P(ResourceRequestAllowedNotifierTest,EulaFirst)261 TEST_P(ResourceRequestAllowedNotifierTest, EulaFirst) {
262 DisableEulaAndNetwork();
263 EXPECT_FALSE(SimulateResourceRequest());
264
265 SimulateEulaAccepted();
266 EXPECT_FALSE(was_notified());
267
268 SimulateNetworkConnectionChange(
269 network::mojom::ConnectionType::CONNECTION_WIFI);
270 EXPECT_TRUE(was_notified());
271 }
272
TEST_P(ResourceRequestAllowedNotifierTest,NetworkFirst)273 TEST_P(ResourceRequestAllowedNotifierTest, NetworkFirst) {
274 DisableEulaAndNetwork();
275 EXPECT_FALSE(SimulateResourceRequest());
276
277 SimulateNetworkConnectionChange(
278 network::mojom::ConnectionType::CONNECTION_WIFI);
279 EXPECT_FALSE(was_notified());
280
281 SimulateEulaAccepted();
282 EXPECT_TRUE(was_notified());
283 }
284
TEST_P(ResourceRequestAllowedNotifierTest,NoRequestNoNotifyEula)285 TEST_P(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) {
286 // Ensure that if the observing service does not request access, it does not
287 // get notified, even if the criteria are met. Note that this is done by not
288 // calling SimulateResourceRequest here.
289 DisableEulaAndNetwork();
290
291 SimulateNetworkConnectionChange(
292 network::mojom::ConnectionType::CONNECTION_WIFI);
293 EXPECT_FALSE(was_notified());
294
295 SimulateEulaAccepted();
296 EXPECT_FALSE(was_notified());
297 }
298
299 INSTANTIATE_TEST_SUITE_P(
300 All,
301 ResourceRequestAllowedNotifierTest,
302 testing::Values(ConnectionTrackerResponseMode::kSynchronous,
303 ConnectionTrackerResponseMode::kAsynchronous));
304
305 } // namespace web_resource
306