[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 1 | // Copyright 2014 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 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 7 | #include "base/bind.h" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 8 | #include "base/macros.h" |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 9 | #include "base/run_loop.h" |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 10 | #include "base/strings/string_number_conversions.h" |
| 11 | #include "base/strings/stringprintf.h" |
Carlos Caballero | b8281a6 | 2019-06-10 20:12:22 | [diff] [blame] | 12 | #include "base/test/scoped_task_environment.h" |
Steven Bennetts | b4bf6cd | 2019-04-05 17:02:57 | [diff] [blame] | 13 | #include "chromeos/dbus/shill/shill_clients.h" |
Steven Bennetts | d11e440f | 2019-04-04 01:09:00 | [diff] [blame] | 14 | #include "chromeos/dbus/shill/shill_manager_client.h" |
satorux | de6a5dc | 2014-12-12 22:30:37 | [diff] [blame] | 15 | #include "chromeos/geolocation/simple_geolocation_provider.h" |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 16 | #include "chromeos/geolocation/simple_geolocation_request_test_monitor.h" |
| 17 | #include "chromeos/network/geolocation_handler.h" |
| 18 | #include "chromeos/network/network_handler.h" |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 19 | #include "net/http/http_response_headers.h" |
| 20 | #include "net/http/http_status_code.h" |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 21 | #include "net/url_request/url_request_status.h" |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 22 | #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h" |
| 23 | #include "services/network/test/test_url_loader_factory.h" |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 25 | #include "third_party/cros_system_api/dbus/service_constants.h" |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 29 | constexpr int kRequestRetryIntervalMilliSeconds = 200; |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 30 | |
| 31 | // This should be different from default to prevent SimpleGeolocationRequest |
| 32 | // from modifying it. |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 33 | constexpr char kTestGeolocationProviderUrl[] = |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 34 | "https://localhost/geolocation/v1/geolocate?"; |
| 35 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 36 | constexpr char kSimpleResponseBody[] = |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 37 | "{\n" |
| 38 | " \"location\": {\n" |
| 39 | " \"lat\": 51.0,\n" |
| 40 | " \"lng\": -0.1\n" |
| 41 | " },\n" |
| 42 | " \"accuracy\": 1200.4\n" |
| 43 | "}"; |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 44 | constexpr char kIPOnlyRequestBody[] = "{\"considerIp\": \"true\"}"; |
| 45 | constexpr char kOneWiFiAPRequestBody[] = |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 46 | "{" |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 47 | "\"considerIp\":true," |
| 48 | "\"wifiAccessPoints\":[" |
| 49 | "{" |
| 50 | "\"channel\":1," |
| 51 | "\"macAddress\":\"01:00:00:00:00:00\"," |
| 52 | "\"signalStrength\":10," |
| 53 | "\"signalToNoiseRatio\":0" |
| 54 | "}" |
| 55 | "]" |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 56 | "}"; |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 57 | constexpr char kOneCellTowerRequestBody[] = |
| 58 | "{" |
| 59 | "\"cellTowers\":[" |
| 60 | "{" |
| 61 | "\"cellId\":\"1\"," |
Niranjan Kumar | 4c2ce9f | 2017-10-03 22:28:51 | [diff] [blame] | 62 | "\"locationAreaCode\":\"3\"," |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 63 | "\"mobileCountryCode\":\"100\"," |
| 64 | "\"mobileNetworkCode\":\"101\"" |
| 65 | "}" |
| 66 | "]," |
| 67 | "\"considerIp\":true" |
| 68 | "}"; |
| 69 | constexpr char kExpectedPosition[] = |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 70 | "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, " |
| 71 | "error_code=0, error_message='', status=1 (OK)"; |
| 72 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 73 | constexpr char kWiFiAP1MacAddress[] = "01:00:00:00:00:00"; |
| 74 | constexpr char kCellTower1MNC[] = "101"; |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 75 | } // anonymous namespace |
| 76 | |
| 77 | namespace chromeos { |
| 78 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 79 | // This implements fake Google MAPS Geolocation API remote endpoint. |
| 80 | class TestGeolocationAPILoaderFactory : public network::TestURLLoaderFactory { |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 81 | public: |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 82 | TestGeolocationAPILoaderFactory(const GURL& url, |
| 83 | const std::string& response, |
| 84 | const size_t require_retries) |
| 85 | : url_(url), response_(response), require_retries_(require_retries) { |
| 86 | SetInterceptor(base::BindRepeating( |
| 87 | &TestGeolocationAPILoaderFactory::Intercept, base::Unretained(this))); |
| 88 | AddResponseWithCode(net::HTTP_INTERNAL_SERVER_ERROR); |
| 89 | } |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 90 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 91 | void Intercept(const network::ResourceRequest& request) { |
| 92 | EXPECT_EQ(url_, request.url); |
| 93 | |
| 94 | EXPECT_NE(nullptr, provider_); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 95 | EXPECT_EQ(provider_->requests_.size(), 1U); |
| 96 | |
benchan | 6f8ca9f | 2016-08-30 14:14:09 | [diff] [blame] | 97 | SimpleGeolocationRequest* geolocation_request = |
| 98 | provider_->requests_[0].get(); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 99 | |
| 100 | const base::TimeDelta base_retry_interval = |
| 101 | base::TimeDelta::FromMilliseconds(kRequestRetryIntervalMilliSeconds); |
| 102 | geolocation_request->set_retry_sleep_on_server_error_for_testing( |
| 103 | base_retry_interval); |
| 104 | geolocation_request->set_retry_sleep_on_bad_response_for_testing( |
| 105 | base_retry_interval); |
| 106 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 107 | if (++attempts_ > require_retries_) |
| 108 | AddResponseWithCode(net::OK); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 109 | } |
| 110 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 111 | void SetSimpleGeolocationProvider(SimpleGeolocationProvider* provider) { |
| 112 | provider_ = provider; |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 113 | } |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 114 | size_t attempts() const { return attempts_; } |
| 115 | |
| 116 | private: |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 117 | void AddResponseWithCode(int error_code) { |
| 118 | network::ResourceResponseHead response_head; |
| 119 | response_head.headers = base::MakeRefCounted<net::HttpResponseHeaders>(""); |
| 120 | response_head.headers->AddHeader("Content-Type: application/json"); |
| 121 | // If AddResponse() is called multiple times for the same URL, the last |
| 122 | // one is the one used so there is no need for ClearResponses(). |
| 123 | AddResponse(url_, response_head, response_, |
| 124 | network::URLLoaderCompletionStatus(error_code)); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 125 | } |
| 126 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 127 | GURL url_; |
| 128 | std::string response_; |
| 129 | const size_t require_retries_; |
| 130 | size_t attempts_ = 0; |
| 131 | SimpleGeolocationProvider* provider_; |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 132 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(TestGeolocationAPILoaderFactory); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | class GeolocationReceiver { |
| 137 | public: |
| 138 | GeolocationReceiver() : server_error_(false) {} |
| 139 | |
| 140 | void OnRequestDone(const Geoposition& position, |
| 141 | bool server_error, |
| 142 | const base::TimeDelta elapsed) { |
| 143 | position_ = position; |
| 144 | server_error_ = server_error; |
| 145 | elapsed_ = elapsed; |
| 146 | |
| 147 | message_loop_runner_->Quit(); |
| 148 | } |
| 149 | |
| 150 | void WaitUntilRequestDone() { |
| 151 | message_loop_runner_.reset(new base::RunLoop); |
| 152 | message_loop_runner_->Run(); |
| 153 | } |
| 154 | |
| 155 | const Geoposition& position() const { return position_; } |
| 156 | bool server_error() const { return server_error_; } |
| 157 | base::TimeDelta elapsed() const { return elapsed_; } |
| 158 | |
| 159 | private: |
| 160 | Geoposition position_; |
| 161 | bool server_error_; |
| 162 | base::TimeDelta elapsed_; |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 163 | std::unique_ptr<base::RunLoop> message_loop_runner_; |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 164 | }; |
| 165 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 166 | class WirelessTestMonitor : public SimpleGeolocationRequestTestMonitor { |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 167 | public: |
Chris Watkins | 2c529d6 | 2017-11-29 02:14:41 | [diff] [blame] | 168 | WirelessTestMonitor() = default; |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 169 | |
| 170 | void OnRequestCreated(SimpleGeolocationRequest* request) override {} |
| 171 | void OnStart(SimpleGeolocationRequest* request) override { |
| 172 | last_request_body_ = request->FormatRequestBodyForTesting(); |
| 173 | } |
| 174 | |
| 175 | const std::string& last_request_body() const { return last_request_body_; } |
| 176 | |
| 177 | private: |
| 178 | std::string last_request_body_; |
| 179 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 180 | DISALLOW_COPY_AND_ASSIGN(WirelessTestMonitor); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 181 | }; |
| 182 | |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 183 | class SimpleGeolocationTest : public testing::Test { |
| 184 | private: |
Carlos Caballero | b8281a6 | 2019-06-10 20:12:22 | [diff] [blame] | 185 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | TEST_F(SimpleGeolocationTest, ResponseOK) { |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 189 | TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 190 | kSimpleResponseBody, |
| 191 | 0 /* require_retries */); |
| 192 | SimpleGeolocationProvider provider( |
| 193 | base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( |
| 194 | &url_factory), |
| 195 | GURL(kTestGeolocationProviderUrl)); |
| 196 | url_factory.SetSimpleGeolocationProvider(&provider); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 197 | |
| 198 | GeolocationReceiver receiver; |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 199 | provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, false, |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 200 | base::Bind(&GeolocationReceiver::OnRequestDone, |
| 201 | base::Unretained(&receiver))); |
| 202 | receiver.WaitUntilRequestDone(); |
| 203 | |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 204 | EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 205 | EXPECT_FALSE(receiver.server_error()); |
| 206 | EXPECT_EQ(1U, url_factory.attempts()); |
| 207 | } |
| 208 | |
| 209 | TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) { |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 210 | TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 211 | kSimpleResponseBody, |
| 212 | 3 /* require_retries */); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 213 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 214 | SimpleGeolocationProvider provider( |
| 215 | base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( |
| 216 | &url_factory), |
| 217 | GURL(kTestGeolocationProviderUrl)); |
| 218 | url_factory.SetSimpleGeolocationProvider(&provider); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 219 | |
| 220 | GeolocationReceiver receiver; |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 221 | provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, false, |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 222 | base::Bind(&GeolocationReceiver::OnRequestDone, |
| 223 | base::Unretained(&receiver))); |
| 224 | receiver.WaitUntilRequestDone(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 225 | EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 226 | EXPECT_FALSE(receiver.server_error()); |
| 227 | EXPECT_EQ(4U, url_factory.attempts()); |
| 228 | } |
| 229 | |
| 230 | TEST_F(SimpleGeolocationTest, InvalidResponse) { |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 231 | TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 232 | "invalid JSON string", |
| 233 | 0 /* require_retries */); |
| 234 | SimpleGeolocationProvider provider( |
| 235 | base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( |
| 236 | &url_factory), |
| 237 | GURL(kTestGeolocationProviderUrl)); |
| 238 | url_factory.SetSimpleGeolocationProvider(&provider); |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 239 | |
| 240 | GeolocationReceiver receiver; |
[email protected] | 8fafd87 | 2014-05-23 18:31:32 | [diff] [blame] | 241 | |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 242 | const int timeout_seconds = 1; |
[email protected] | 8fafd87 | 2014-05-23 18:31:32 | [diff] [blame] | 243 | size_t expected_retries = static_cast<size_t>( |
| 244 | timeout_seconds * 1000 / kRequestRetryIntervalMilliSeconds); |
| 245 | ASSERT_GE(expected_retries, 2U); |
| 246 | |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 247 | provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds), |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 248 | false, false, |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 249 | base::Bind(&GeolocationReceiver::OnRequestDone, |
| 250 | base::Unretained(&receiver))); |
| 251 | receiver.WaitUntilRequestDone(); |
| 252 | |
| 253 | EXPECT_EQ( |
| 254 | "latitude=200.000000, longitude=200.000000, accuracy=-1.000000, " |
| 255 | "error_code=0, error_message='SimpleGeolocation provider at " |
| 256 | "'https://localhost/' : JSONReader failed: Line: 1, column: 1, " |
| 257 | "Unexpected token..', status=4 (TIMEOUT)", |
| 258 | receiver.position().ToString()); |
| 259 | EXPECT_TRUE(receiver.server_error()); |
[email protected] | 8fafd87 | 2014-05-23 18:31:32 | [diff] [blame] | 260 | EXPECT_GE(url_factory.attempts(), 2U); |
| 261 | if (url_factory.attempts() > expected_retries + 1) { |
| 262 | LOG(WARNING) |
| 263 | << "SimpleGeolocationTest::InvalidResponse: Too many attempts (" |
thakis | e5b5786 | 2016-07-18 19:23:19 | [diff] [blame] | 264 | << url_factory.attempts() << "), no more than " << expected_retries + 1 |
[email protected] | 8fafd87 | 2014-05-23 18:31:32 | [diff] [blame] | 265 | << " expected."; |
| 266 | } |
| 267 | if (url_factory.attempts() < expected_retries - 1) { |
| 268 | LOG(WARNING) |
| 269 | << "SimpleGeolocationTest::InvalidResponse: Too little attempts (" |
thakis | e5b5786 | 2016-07-18 19:23:19 | [diff] [blame] | 270 | << url_factory.attempts() << "), greater than " << expected_retries - 1 |
[email protected] | 8fafd87 | 2014-05-23 18:31:32 | [diff] [blame] | 271 | << " expected."; |
| 272 | } |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 273 | } |
| 274 | |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 275 | TEST_F(SimpleGeolocationTest, NoWiFi) { |
Steven Bennetts | b4bf6cd | 2019-04-05 17:02:57 | [diff] [blame] | 276 | shill_clients::InitializeFakes(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 277 | NetworkHandler::Initialize(); |
| 278 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 279 | WirelessTestMonitor requests_monitor; |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 280 | SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); |
| 281 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 282 | TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 283 | kSimpleResponseBody, |
| 284 | 0 /* require_retries */); |
| 285 | SimpleGeolocationProvider provider( |
| 286 | base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( |
| 287 | &url_factory), |
| 288 | GURL(kTestGeolocationProviderUrl)); |
| 289 | url_factory.SetSimpleGeolocationProvider(&provider); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 290 | |
| 291 | GeolocationReceiver receiver; |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 292 | provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), true, false, |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 293 | base::Bind(&GeolocationReceiver::OnRequestDone, |
| 294 | base::Unretained(&receiver))); |
| 295 | receiver.WaitUntilRequestDone(); |
| 296 | EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| 297 | |
| 298 | EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 299 | EXPECT_FALSE(receiver.server_error()); |
| 300 | EXPECT_EQ(1U, url_factory.attempts()); |
| 301 | |
| 302 | NetworkHandler::Shutdown(); |
Steven Bennetts | b4bf6cd | 2019-04-05 17:02:57 | [diff] [blame] | 303 | shill_clients::Shutdown(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 304 | } |
| 305 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 306 | // Test sending of WiFi Access points and Cell Towers. |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 307 | // (This is mostly derived from GeolocationHandlerTest.) |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 308 | class SimpleGeolocationWirelessTest : public ::testing::TestWithParam<bool> { |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 309 | public: |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 310 | SimpleGeolocationWirelessTest() : manager_test_(nullptr) {} |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 311 | |
Chris Watkins | 2c529d6 | 2017-11-29 02:14:41 | [diff] [blame] | 312 | ~SimpleGeolocationWirelessTest() override = default; |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 313 | |
| 314 | void SetUp() override { |
Steven Bennetts | b4bf6cd | 2019-04-05 17:02:57 | [diff] [blame] | 315 | shill_clients::InitializeFakes(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 316 | // Get the test interface for manager / device. |
Steven Bennetts | b4bf6cd | 2019-04-05 17:02:57 | [diff] [blame] | 317 | manager_test_ = ShillManagerClient::Get()->GetTestInterface(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 318 | ASSERT_TRUE(manager_test_); |
| 319 | geolocation_handler_.reset(new GeolocationHandler()); |
| 320 | geolocation_handler_->Init(); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 321 | base::RunLoop().RunUntilIdle(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void TearDown() override { |
| 325 | geolocation_handler_.reset(); |
Steven Bennetts | b4bf6cd | 2019-04-05 17:02:57 | [diff] [blame] | 326 | shill_clients::Shutdown(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | bool GetWifiAccessPoints() { |
| 330 | return geolocation_handler_->GetWifiAccessPoints(&wifi_access_points_, |
| 331 | nullptr); |
| 332 | } |
| 333 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 334 | bool GetCellTowers() { |
| 335 | return geolocation_handler_->GetNetworkInformation(nullptr, &cell_towers_); |
| 336 | } |
| 337 | |
| 338 | // This should remain in sync with the format of shill (chromeos) dict entries |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 339 | void AddAccessPoint(int idx) { |
| 340 | base::DictionaryValue properties; |
| 341 | std::string mac_address = |
| 342 | base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", idx, 0, 0, 0, 0, 0); |
Raul Tambre | d252a66 | 2019-02-11 20:29:42 | [diff] [blame] | 343 | std::string channel = base::NumberToString(idx); |
| 344 | std::string strength = base::NumberToString(idx * 10); |
jdoerrie | fbb03dd | 2017-08-17 14:43:26 | [diff] [blame] | 345 | properties.SetKey(shill::kGeoMacAddressProperty, base::Value(mac_address)); |
| 346 | properties.SetKey(shill::kGeoChannelProperty, base::Value(channel)); |
| 347 | properties.SetKey(shill::kGeoSignalStrengthProperty, base::Value(strength)); |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 348 | manager_test_->AddGeoNetwork(shill::kGeoWifiAccessPointsProperty, |
| 349 | properties); |
| 350 | base::RunLoop().RunUntilIdle(); |
| 351 | } |
| 352 | |
| 353 | // This should remain in sync with the format of shill (chromeos) dict entries |
| 354 | void AddCellTower(int idx) { |
| 355 | base::DictionaryValue properties; |
Raul Tambre | d252a66 | 2019-02-11 20:29:42 | [diff] [blame] | 356 | std::string ci = base::NumberToString(idx); |
| 357 | std::string lac = base::NumberToString(idx * 3); |
| 358 | std::string mcc = base::NumberToString(idx * 100); |
| 359 | std::string mnc = base::NumberToString(idx * 100 + 1); |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 360 | |
jdoerrie | fbb03dd | 2017-08-17 14:43:26 | [diff] [blame] | 361 | properties.SetKey(shill::kGeoCellIdProperty, base::Value(ci)); |
| 362 | properties.SetKey(shill::kGeoLocationAreaCodeProperty, base::Value(lac)); |
| 363 | properties.SetKey(shill::kGeoMobileCountryCodeProperty, base::Value(mcc)); |
| 364 | properties.SetKey(shill::kGeoMobileNetworkCodeProperty, base::Value(mnc)); |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 365 | |
| 366 | manager_test_->AddGeoNetwork(shill::kGeoCellTowersProperty, properties); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 367 | base::RunLoop().RunUntilIdle(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | protected: |
Carlos Caballero | f17e6e2 | 2019-07-27 03:10:44 | [diff] [blame] | 371 | base::test::ScopedTaskEnvironment scoped_task_environment_{ |
| 372 | base::test::ScopedTaskEnvironment::MainThreadType::UI}; |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 373 | std::unique_ptr<GeolocationHandler> geolocation_handler_; |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 374 | ShillManagerClient::TestInterface* manager_test_; |
| 375 | WifiAccessPointVector wifi_access_points_; |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 376 | CellTowerVector cell_towers_; |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 377 | |
| 378 | private: |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 379 | DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationWirelessTest); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | // Parameter is enable/disable sending of WiFi data. |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 383 | TEST_P(SimpleGeolocationWirelessTest, WiFiExists) { |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 384 | NetworkHandler::Initialize(); |
| 385 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 386 | WirelessTestMonitor requests_monitor; |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 387 | SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); |
| 388 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 389 | TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 390 | kSimpleResponseBody, |
| 391 | 0 /* require_retries */); |
| 392 | SimpleGeolocationProvider provider( |
| 393 | base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( |
| 394 | &url_factory), |
| 395 | GURL(kTestGeolocationProviderUrl)); |
| 396 | url_factory.SetSimpleGeolocationProvider(&provider); |
Jun Mukai | df9e5f7d | 2019-01-07 23:47:06 | [diff] [blame] | 397 | provider.set_geolocation_handler(geolocation_handler_.get()); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 398 | { |
| 399 | GeolocationReceiver receiver; |
| 400 | provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(), |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 401 | false, |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 402 | base::Bind(&GeolocationReceiver::OnRequestDone, |
| 403 | base::Unretained(&receiver))); |
| 404 | receiver.WaitUntilRequestDone(); |
| 405 | EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| 406 | |
| 407 | EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 408 | EXPECT_FALSE(receiver.server_error()); |
| 409 | EXPECT_EQ(1U, url_factory.attempts()); |
| 410 | } |
| 411 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 412 | // Add cell and wifi to ensure only wifi is sent when cellular disabled. |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 413 | AddAccessPoint(1); |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 414 | AddCellTower(1); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 415 | base::RunLoop().RunUntilIdle(); |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 416 | // Initial call should return false and request access points. |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 417 | EXPECT_FALSE(GetWifiAccessPoints()); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 418 | base::RunLoop().RunUntilIdle(); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 419 | // Second call should return true since we have an access point. |
| 420 | EXPECT_TRUE(GetWifiAccessPoints()); |
| 421 | ASSERT_EQ(1u, wifi_access_points_.size()); |
| 422 | EXPECT_EQ(kWiFiAP1MacAddress, wifi_access_points_[0].mac_address); |
| 423 | EXPECT_EQ(1, wifi_access_points_[0].channel); |
| 424 | |
| 425 | { |
| 426 | GeolocationReceiver receiver; |
| 427 | provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(), |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 428 | false, |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 429 | base::Bind(&GeolocationReceiver::OnRequestDone, |
| 430 | base::Unretained(&receiver))); |
| 431 | receiver.WaitUntilRequestDone(); |
| 432 | if (GetParam()) { |
| 433 | // Sending WiFi data is enabled. |
| 434 | EXPECT_EQ(kOneWiFiAPRequestBody, requests_monitor.last_request_body()); |
| 435 | } else { |
| 436 | // Sending WiFi data is disabled. |
| 437 | EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| 438 | } |
| 439 | |
| 440 | EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 441 | EXPECT_FALSE(receiver.server_error()); |
| 442 | // This is total. |
| 443 | EXPECT_EQ(2U, url_factory.attempts()); |
| 444 | } |
| 445 | NetworkHandler::Shutdown(); |
| 446 | } |
| 447 | |
| 448 | // This test verifies that WiFi data is sent only if sending was requested. |
Victor Costan | fdc907e8 | 2019-01-28 18:10:40 | [diff] [blame] | 449 | INSTANTIATE_TEST_SUITE_P(EnableDisableSendingWifiData, |
| 450 | SimpleGeolocationWirelessTest, |
| 451 | testing::Bool()); |
alemate | 9af1ade | 2016-03-23 23:21:44 | [diff] [blame] | 452 | |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 453 | TEST_P(SimpleGeolocationWirelessTest, CellularExists) { |
| 454 | NetworkHandler::Initialize(); |
| 455 | |
| 456 | WirelessTestMonitor requests_monitor; |
| 457 | SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); |
| 458 | |
Sergio Villar Senin | 5a1ed478 | 2018-08-21 19:26:40 | [diff] [blame] | 459 | TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 460 | kSimpleResponseBody, |
| 461 | 0 /* require_retries */); |
| 462 | SimpleGeolocationProvider provider( |
| 463 | base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( |
| 464 | &url_factory), |
| 465 | GURL(kTestGeolocationProviderUrl)); |
| 466 | url_factory.SetSimpleGeolocationProvider(&provider); |
Jun Mukai | df9e5f7d | 2019-01-07 23:47:06 | [diff] [blame] | 467 | provider.set_geolocation_handler(geolocation_handler_.get()); |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 468 | { |
| 469 | GeolocationReceiver receiver; |
| 470 | provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, |
| 471 | GetParam(), |
| 472 | base::Bind(&GeolocationReceiver::OnRequestDone, |
| 473 | base::Unretained(&receiver))); |
| 474 | receiver.WaitUntilRequestDone(); |
| 475 | EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| 476 | |
| 477 | EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 478 | EXPECT_FALSE(receiver.server_error()); |
| 479 | EXPECT_EQ(1U, url_factory.attempts()); |
| 480 | } |
| 481 | |
| 482 | AddCellTower(1); |
| 483 | base::RunLoop().RunUntilIdle(); |
| 484 | // Initial call should return false and request cell towers. |
| 485 | EXPECT_FALSE(GetCellTowers()); |
| 486 | base::RunLoop().RunUntilIdle(); |
| 487 | // Second call should return true since we have a tower. |
| 488 | EXPECT_TRUE(GetCellTowers()); |
| 489 | ASSERT_EQ(1u, cell_towers_.size()); |
| 490 | EXPECT_EQ(kCellTower1MNC, cell_towers_[0].mnc); |
Raul Tambre | d252a66 | 2019-02-11 20:29:42 | [diff] [blame] | 491 | EXPECT_EQ(base::NumberToString(1), cell_towers_[0].ci); |
skylarc | eaf50cb | 2017-02-14 01:16:28 | [diff] [blame] | 492 | |
| 493 | { |
| 494 | GeolocationReceiver receiver; |
| 495 | provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, |
| 496 | GetParam(), |
| 497 | base::Bind(&GeolocationReceiver::OnRequestDone, |
| 498 | base::Unretained(&receiver))); |
| 499 | receiver.WaitUntilRequestDone(); |
| 500 | if (GetParam()) { |
| 501 | // Sending Cellular data is enabled. |
| 502 | EXPECT_EQ(kOneCellTowerRequestBody, requests_monitor.last_request_body()); |
| 503 | } else { |
| 504 | // Sending Cellular data is disabled. |
| 505 | EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| 506 | } |
| 507 | |
| 508 | EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 509 | EXPECT_FALSE(receiver.server_error()); |
| 510 | // This is total. |
| 511 | EXPECT_EQ(2U, url_factory.attempts()); |
| 512 | } |
| 513 | NetworkHandler::Shutdown(); |
| 514 | } |
| 515 | |
[email protected] | 4e7fcd38 | 2014-05-19 22:00:33 | [diff] [blame] | 516 | } // namespace chromeos |