OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chromeos/components/tether/host_scan_cache.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <unordered_map> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/test/scoped_task_environment.h" |
| 14 #include "base/timer/mock_timer.h" |
| 15 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
| 16 #include "chromeos/components/tether/fake_active_host.h" |
| 17 #include "chromeos/components/tether/fake_host_scan_cache.h" |
| 18 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" |
| 19 #include "chromeos/dbus/dbus_thread_manager.h" |
| 20 #include "chromeos/network/network_state.h" |
| 21 #include "chromeos/network/network_state_handler.h" |
| 22 #include "chromeos/network/network_state_test.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 using testing::NiceMock; |
| 27 using testing::Invoke; |
| 28 |
| 29 namespace chromeos { |
| 30 |
| 31 namespace tether { |
| 32 |
| 33 namespace { |
| 34 |
| 35 const char kTetherGuid0[] = "kTetherGuid0"; |
| 36 const char kTetherGuid1[] = "kTetherGuid1"; |
| 37 const char kTetherGuid2[] = "kTetherGuid2"; |
| 38 const char kTetherGuid3[] = "kTetherGuid3"; |
| 39 |
| 40 const char kTetherDeviceName0[] = "kDeviceName1"; |
| 41 const char kTetherDeviceName1[] = "kDeviceName2"; |
| 42 const char kTetherDeviceName2[] = "kDeviceName3"; |
| 43 const char kTetherDeviceName3[] = "kDeviceName4"; |
| 44 |
| 45 const char kTetherCarrier0[] = "kTetherCarrier0"; |
| 46 const char kTetherCarrier1[] = "kTetherCarrier1"; |
| 47 const char kTetherCarrier2[] = "kTetherCarrier2"; |
| 48 const char kTetherCarrier3[] = "kTetherCarrier3"; |
| 49 |
| 50 const int kTetherBatteryPercentage0 = 20; |
| 51 const int kTetherBatteryPercentage1 = 40; |
| 52 const int kTetherBatteryPercentage2 = 60; |
| 53 const int kTetherBatteryPercentage3 = 80; |
| 54 |
| 55 const int kTetherSignalStrength0 = 25; |
| 56 const int kTetherSignalStrength1 = 50; |
| 57 const int kTetherSignalStrength2 = 75; |
| 58 const int kTetherSignalStrength3 = 100; |
| 59 |
| 60 // MockTimer which invokes a callback in its destructor. |
| 61 class ExtendedMockTimer : public base::MockTimer { |
| 62 public: |
| 63 explicit ExtendedMockTimer(const base::Closure& destructor_callback) |
| 64 : base::MockTimer(true /* retain_user_task */, false /* is_repeating */), |
| 65 destructor_callback_(destructor_callback) {} |
| 66 |
| 67 ~ExtendedMockTimer() override { destructor_callback_.Run(); } |
| 68 |
| 69 private: |
| 70 base::Closure destructor_callback_; |
| 71 }; |
| 72 |
| 73 } // namespace |
| 74 |
| 75 // TODO(khorimoto): The test uses a FakeHostScanCache to keep an in-memory |
| 76 // cache of expected values. This has the potential to be confusing, since this |
| 77 // is the test for HostScanCache. Clean this up to avoid using FakeHostScanCache |
| 78 // if possible. |
| 79 class HostScanCacheTest : public NetworkStateTest { |
| 80 protected: |
| 81 class TestTimerFactory : public HostScanCache::TimerFactory { |
| 82 public: |
| 83 TestTimerFactory() {} |
| 84 ~TestTimerFactory() {} |
| 85 |
| 86 std::unordered_map<std::string, ExtendedMockTimer*>& |
| 87 tether_network_guid_to_timer_map() { |
| 88 return tether_network_guid_to_timer_map_; |
| 89 } |
| 90 |
| 91 void set_tether_network_guid_for_next_timer( |
| 92 const std::string& tether_network_guid_for_next_timer) { |
| 93 tether_network_guid_for_next_timer_ = tether_network_guid_for_next_timer; |
| 94 } |
| 95 |
| 96 // HostScanCache::TimerFactory: |
| 97 std::unique_ptr<base::Timer> CreateOneShotTimer() override { |
| 98 EXPECT_FALSE(tether_network_guid_for_next_timer_.empty()); |
| 99 ExtendedMockTimer* mock_timer = new ExtendedMockTimer(base::Bind( |
| 100 &TestTimerFactory::OnActiveTimerDestructor, base::Unretained(this), |
| 101 tether_network_guid_for_next_timer_)); |
| 102 tether_network_guid_to_timer_map_[tether_network_guid_for_next_timer_] = |
| 103 mock_timer; |
| 104 return base::WrapUnique(mock_timer); |
| 105 } |
| 106 |
| 107 private: |
| 108 void OnActiveTimerDestructor(const std::string& tether_network_guid) { |
| 109 tether_network_guid_to_timer_map_.erase( |
| 110 tether_network_guid_to_timer_map_.find(tether_network_guid)); |
| 111 } |
| 112 |
| 113 std::string tether_network_guid_for_next_timer_; |
| 114 std::unordered_map<std::string, ExtendedMockTimer*> |
| 115 tether_network_guid_to_timer_map_; |
| 116 }; |
| 117 |
| 118 HostScanCacheTest() {} |
| 119 |
| 120 void SetUp() override { |
| 121 DBusThreadManager::Initialize(); |
| 122 NetworkStateTest::SetUp(); |
| 123 network_state_handler()->SetTetherTechnologyState( |
| 124 NetworkStateHandler::TECHNOLOGY_ENABLED); |
| 125 |
| 126 test_timer_factory_ = new TestTimerFactory(); |
| 127 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); |
| 128 mock_tether_host_response_recorder_ = |
| 129 base::MakeUnique<NiceMock<MockTetherHostResponseRecorder>>(); |
| 130 device_id_tether_network_guid_map_ = |
| 131 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); |
| 132 |
| 133 ON_CALL(*mock_tether_host_response_recorder_, |
| 134 GetPreviouslyConnectedHostIds()) |
| 135 .WillByDefault( |
| 136 Invoke(this, &HostScanCacheTest::GetPreviouslyConnectedHostIds)); |
| 137 |
| 138 host_scan_cache_ = base::MakeUnique<HostScanCache>( |
| 139 network_state_handler(), fake_active_host_.get(), |
| 140 mock_tether_host_response_recorder_.get(), |
| 141 device_id_tether_network_guid_map_.get()); |
| 142 host_scan_cache_->SetTimerFactoryForTest( |
| 143 base::WrapUnique(test_timer_factory_)); |
| 144 |
| 145 // To track what is expected to be contained in the cache, maintain a |
| 146 // FakeHostScanCache in memory and update it alongside |host_scan_cache_|. |
| 147 // Use a std::vector to track which device IDs correspond to devices whose |
| 148 // Tether networks' HasConnectedToHost fields are expected to be set. |
| 149 expected_cache_ = base::MakeUnique<FakeHostScanCache>(); |
| 150 has_connected_to_host_device_ids_.clear(); |
| 151 } |
| 152 |
| 153 void TearDown() override { |
| 154 ShutdownNetworkState(); |
| 155 NetworkStateTest::TearDown(); |
| 156 DBusThreadManager::Shutdown(); |
| 157 } |
| 158 |
| 159 void FireTimer(const std::string& tether_network_guid) { |
| 160 ExtendedMockTimer* timer = |
| 161 test_timer_factory_ |
| 162 ->tether_network_guid_to_timer_map()[tether_network_guid]; |
| 163 ASSERT_TRUE(timer); |
| 164 timer->Fire(); |
| 165 |
| 166 // If the device whose correlated timer has fired is not the active host, it |
| 167 // is expected to be removed from the cache. |
| 168 EXPECT_EQ(fake_active_host_->GetTetherNetworkGuid(), |
| 169 expected_cache_->active_host_tether_network_guid()); |
| 170 if (fake_active_host_->GetTetherNetworkGuid() != tether_network_guid) { |
| 171 expected_cache_->RemoveHostScanResult(tether_network_guid); |
| 172 } |
| 173 } |
| 174 |
| 175 std::vector<std::string> GetPreviouslyConnectedHostIds() const { |
| 176 return has_connected_to_host_device_ids_; |
| 177 } |
| 178 |
| 179 void SetActiveHost(const std::string& tether_network_guid) { |
| 180 fake_active_host_->SetActiveHostConnected( |
| 181 device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( |
| 182 tether_network_guid), |
| 183 tether_network_guid, "wifiNetworkGuid"); |
| 184 expected_cache_->set_active_host_tether_network_guid(tether_network_guid); |
| 185 } |
| 186 |
| 187 void SetHasConnectedToHost(const std::string& tether_network_guid) { |
| 188 has_connected_to_host_device_ids_.push_back( |
| 189 device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( |
| 190 tether_network_guid)); |
| 191 mock_tether_host_response_recorder_ |
| 192 ->NotifyObserversPreviouslyConnectedHostIdsChanged(); |
| 193 } |
| 194 |
| 195 // Sets host scan results in the cache for the device at index |index|. Index |
| 196 // can be from 0 to 3 and corresponds to the index of the constants declared |
| 197 // at the top of this test file. |
| 198 void SetCacheScanResultForDeviceIndex(int32_t index) { |
| 199 // There are 4 sets of test constants. |
| 200 ASSERT_TRUE(index >= 0 && index <= 3); |
| 201 |
| 202 std::string tether_network_guid; |
| 203 std::string device_name; |
| 204 std::string carrier; |
| 205 int battery_percentage; |
| 206 int signal_strength; |
| 207 |
| 208 switch (index) { |
| 209 case 0: |
| 210 tether_network_guid = kTetherGuid0; |
| 211 device_name = kTetherDeviceName0; |
| 212 carrier = kTetherCarrier0; |
| 213 battery_percentage = kTetherBatteryPercentage0; |
| 214 signal_strength = kTetherSignalStrength0; |
| 215 break; |
| 216 case 1: |
| 217 tether_network_guid = kTetherGuid1; |
| 218 device_name = kTetherDeviceName1; |
| 219 carrier = kTetherCarrier1; |
| 220 battery_percentage = kTetherBatteryPercentage1; |
| 221 signal_strength = kTetherSignalStrength1; |
| 222 break; |
| 223 case 2: |
| 224 tether_network_guid = kTetherGuid2; |
| 225 device_name = kTetherDeviceName2; |
| 226 carrier = kTetherCarrier2; |
| 227 battery_percentage = kTetherBatteryPercentage2; |
| 228 signal_strength = kTetherSignalStrength2; |
| 229 break; |
| 230 case 3: |
| 231 tether_network_guid = kTetherGuid3; |
| 232 device_name = kTetherDeviceName3; |
| 233 carrier = kTetherCarrier3; |
| 234 battery_percentage = kTetherBatteryPercentage3; |
| 235 signal_strength = kTetherSignalStrength3; |
| 236 break; |
| 237 default: |
| 238 NOTREACHED(); |
| 239 // Set values for |battery_percentage| and |signal_strength| here to |
| 240 // prevent a compiler warning which says that they may be unset at this |
| 241 // point. |
| 242 battery_percentage = 0; |
| 243 signal_strength = 0; |
| 244 break; |
| 245 } |
| 246 |
| 247 SetHostScanResult(tether_network_guid, device_name, carrier, |
| 248 battery_percentage, signal_strength); |
| 249 } |
| 250 |
| 251 void SetHostScanResult(const std::string& tether_network_guid, |
| 252 const std::string& device_name, |
| 253 const std::string& carrier, |
| 254 int battery_percentage, |
| 255 int signal_strength) { |
| 256 test_timer_factory_->set_tether_network_guid_for_next_timer( |
| 257 tether_network_guid); |
| 258 host_scan_cache_->SetHostScanResult(tether_network_guid, device_name, |
| 259 carrier, battery_percentage, |
| 260 signal_strength); |
| 261 expected_cache_->SetHostScanResult(tether_network_guid, device_name, |
| 262 carrier, battery_percentage, |
| 263 signal_strength); |
| 264 } |
| 265 |
| 266 void RemoveHostScanResult(const std::string& tether_network_guid) { |
| 267 host_scan_cache_->RemoveHostScanResult(tether_network_guid); |
| 268 expected_cache_->RemoveHostScanResult(tether_network_guid); |
| 269 } |
| 270 |
| 271 void ClearCacheExceptForActiveHost() { |
| 272 host_scan_cache_->ClearCacheExceptForActiveHost(); |
| 273 expected_cache_->ClearCacheExceptForActiveHost(); |
| 274 } |
| 275 |
| 276 bool HasConnectedToHost(const std::string& tether_network_guid) { |
| 277 auto it = |
| 278 std::find(has_connected_to_host_device_ids_.begin(), |
| 279 has_connected_to_host_device_ids_.end(), tether_network_guid); |
| 280 return it != has_connected_to_host_device_ids_.end(); |
| 281 } |
| 282 |
| 283 // Verifies that the information present in |expected_cache_| and |
| 284 // |has_connected_to_host_device_ids_| mirrors what |host_scan_cache_| has set |
| 285 // in NetworkStateHandler. |
| 286 void VerifyCacheMatchesNetworkStack() { |
| 287 for (auto& it : expected_cache_->cache()) { |
| 288 const std::string tether_network_guid = it.first; |
| 289 const FakeHostScanCache::CacheEntry cache_entry = it.second; |
| 290 |
| 291 // Ensure that each entry in |expected_cache_| matches the |
| 292 // corresponding entry in NetworkStateHandler. |
| 293 const NetworkState* tether_network_state = |
| 294 network_state_handler()->GetNetworkStateFromGuid(tether_network_guid); |
| 295 ASSERT_TRUE(tether_network_state); |
| 296 EXPECT_EQ(cache_entry.device_name, tether_network_state->name()); |
| 297 EXPECT_EQ(cache_entry.carrier, tether_network_state->carrier()); |
| 298 EXPECT_EQ(cache_entry.battery_percentage, |
| 299 tether_network_state->battery_percentage()); |
| 300 EXPECT_EQ(cache_entry.signal_strength, |
| 301 tether_network_state->signal_strength()); |
| 302 EXPECT_EQ(HasConnectedToHost(tether_network_guid), |
| 303 tether_network_state->tether_has_connected_to_host()); |
| 304 |
| 305 // Ensure that each entry has an actively-running Timer. |
| 306 auto timer_map_it = |
| 307 test_timer_factory_->tether_network_guid_to_timer_map().begin(); |
| 308 EXPECT_NE(timer_map_it, |
| 309 test_timer_factory_->tether_network_guid_to_timer_map().end()); |
| 310 EXPECT_TRUE(timer_map_it->second->IsRunning()); |
| 311 } |
| 312 } |
| 313 |
| 314 const base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 315 |
| 316 TestTimerFactory* test_timer_factory_; |
| 317 std::unique_ptr<FakeActiveHost> fake_active_host_; |
| 318 std::unique_ptr<NiceMock<MockTetherHostResponseRecorder>> |
| 319 mock_tether_host_response_recorder_; |
| 320 // TODO(hansberry): Use a fake for this when a real mapping scheme is created. |
| 321 std::unique_ptr<DeviceIdTetherNetworkGuidMap> |
| 322 device_id_tether_network_guid_map_; |
| 323 |
| 324 std::vector<std::string> has_connected_to_host_device_ids_; |
| 325 std::unique_ptr<FakeHostScanCache> expected_cache_; |
| 326 |
| 327 std::unique_ptr<HostScanCache> host_scan_cache_; |
| 328 |
| 329 private: |
| 330 DISALLOW_COPY_AND_ASSIGN(HostScanCacheTest); |
| 331 }; |
| 332 |
| 333 TEST_F(HostScanCacheTest, TestSetScanResultsAndLetThemExpire) { |
| 334 SetCacheScanResultForDeviceIndex(0); |
| 335 EXPECT_EQ(1u, expected_cache_->size()); |
| 336 VerifyCacheMatchesNetworkStack(); |
| 337 |
| 338 SetCacheScanResultForDeviceIndex(1); |
| 339 EXPECT_EQ(2u, expected_cache_->size()); |
| 340 VerifyCacheMatchesNetworkStack(); |
| 341 |
| 342 SetCacheScanResultForDeviceIndex(2); |
| 343 EXPECT_EQ(3u, expected_cache_->size()); |
| 344 VerifyCacheMatchesNetworkStack(); |
| 345 |
| 346 SetCacheScanResultForDeviceIndex(3); |
| 347 EXPECT_EQ(4u, expected_cache_->size()); |
| 348 VerifyCacheMatchesNetworkStack(); |
| 349 |
| 350 FireTimer(kTetherGuid0); |
| 351 EXPECT_EQ(3u, expected_cache_->size()); |
| 352 VerifyCacheMatchesNetworkStack(); |
| 353 |
| 354 FireTimer(kTetherGuid1); |
| 355 EXPECT_EQ(2u, expected_cache_->size()); |
| 356 VerifyCacheMatchesNetworkStack(); |
| 357 |
| 358 FireTimer(kTetherGuid2); |
| 359 EXPECT_EQ(1u, expected_cache_->size()); |
| 360 VerifyCacheMatchesNetworkStack(); |
| 361 |
| 362 FireTimer(kTetherGuid3); |
| 363 EXPECT_TRUE(expected_cache_->empty()); |
| 364 VerifyCacheMatchesNetworkStack(); |
| 365 } |
| 366 |
| 367 TEST_F(HostScanCacheTest, TestSetScanResultThenUpdateAndRemove) { |
| 368 SetCacheScanResultForDeviceIndex(0); |
| 369 EXPECT_EQ(1u, expected_cache_->size()); |
| 370 VerifyCacheMatchesNetworkStack(); |
| 371 |
| 372 // Change the fields for tether network with GUID |kTetherGuid0| to the |
| 373 // fields corresponding to |kTetherGuid1|. |
| 374 SetHostScanResult(kTetherGuid0, kTetherDeviceName0, kTetherCarrier1, |
| 375 kTetherBatteryPercentage1, kTetherSignalStrength1); |
| 376 EXPECT_EQ(1u, expected_cache_->size()); |
| 377 VerifyCacheMatchesNetworkStack(); |
| 378 |
| 379 // Now, remove that result. |
| 380 RemoveHostScanResult(kTetherGuid0); |
| 381 EXPECT_TRUE(expected_cache_->empty()); |
| 382 VerifyCacheMatchesNetworkStack(); |
| 383 } |
| 384 |
| 385 TEST_F(HostScanCacheTest, TestSetScanResult_SetActiveHost_ThenClear) { |
| 386 SetCacheScanResultForDeviceIndex(0); |
| 387 EXPECT_EQ(1u, expected_cache_->size()); |
| 388 VerifyCacheMatchesNetworkStack(); |
| 389 |
| 390 SetCacheScanResultForDeviceIndex(1); |
| 391 EXPECT_EQ(2u, expected_cache_->size()); |
| 392 VerifyCacheMatchesNetworkStack(); |
| 393 |
| 394 SetCacheScanResultForDeviceIndex(2); |
| 395 EXPECT_EQ(3u, expected_cache_->size()); |
| 396 VerifyCacheMatchesNetworkStack(); |
| 397 |
| 398 // Now, set the active host to be the device 0. |
| 399 SetActiveHost(kTetherGuid0); |
| 400 |
| 401 // Clear the cache except for the active host. |
| 402 ClearCacheExceptForActiveHost(); |
| 403 EXPECT_EQ(1u, expected_cache_->size()); |
| 404 VerifyCacheMatchesNetworkStack(); |
| 405 |
| 406 // Attempt to remove the active host. This operation should fail since |
| 407 // removing the active host from the cache is not allowed. |
| 408 RemoveHostScanResult(kTetherGuid0); |
| 409 EXPECT_EQ(1u, expected_cache_->size()); |
| 410 VerifyCacheMatchesNetworkStack(); |
| 411 |
| 412 // Fire the timer for the active host. Likewise, this should not result in the |
| 413 // cache entry being removed. |
| 414 FireTimer(kTetherGuid0); |
| 415 EXPECT_EQ(1u, expected_cache_->size()); |
| 416 VerifyCacheMatchesNetworkStack(); |
| 417 |
| 418 // Now, unset the active host. |
| 419 SetActiveHost(""); |
| 420 |
| 421 // Removing the device should now succeed. |
| 422 RemoveHostScanResult(kTetherGuid0); |
| 423 EXPECT_TRUE(expected_cache_->empty()); |
| 424 VerifyCacheMatchesNetworkStack(); |
| 425 } |
| 426 |
| 427 TEST_F(HostScanCacheTest, TestHasConnectedToHost) { |
| 428 // Before the test starts, set device 0 as having already connected. |
| 429 SetHasConnectedToHost(kTetherGuid0); |
| 430 |
| 431 SetCacheScanResultForDeviceIndex(0); |
| 432 EXPECT_EQ(1u, expected_cache_->size()); |
| 433 VerifyCacheMatchesNetworkStack(); |
| 434 |
| 435 SetCacheScanResultForDeviceIndex(1); |
| 436 EXPECT_EQ(2u, expected_cache_->size()); |
| 437 VerifyCacheMatchesNetworkStack(); |
| 438 |
| 439 // Simulate a connection to device 1. |
| 440 SetActiveHost(kTetherGuid1); |
| 441 SetHasConnectedToHost(kTetherGuid1); |
| 442 VerifyCacheMatchesNetworkStack(); |
| 443 } |
| 444 |
| 445 } // namespace tether |
| 446 |
| 447 } // namespace chromeos |
OLD | NEW |