shill: WiFiProvider: Unit-test OnEndpointUpdated

Add unit tests for WiFiProvider::OnEndpointUpdated.

BUG=chromium:277309
TEST=Run unit tests

Change-Id: Ieee83fab5604966f5d35ecb042a32d4c6b7f5dc2
Reviewed-on: https://gerrit.chromium.org/gerrit/66672
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Commit-Queue: Paul Stewart <pstew@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/wifi_endpoint.h b/wifi_endpoint.h
index 1bf54ca..63d785b 100644
--- a/wifi_endpoint.h
+++ b/wifi_endpoint.h
@@ -100,6 +100,7 @@
   FRIEND_TEST(WiFiEndpointTest, ParseKeyManagementMethodsPSK);
   FRIEND_TEST(WiFiEndpointTest, ParseKeyManagementMethodsEAPAndPSK);
   FRIEND_TEST(WiFiProviderTest, OnEndpointAddedWithSecurity);
+  FRIEND_TEST(WiFiProviderTest, OnEndpointUpdated);
   FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA80211w);
   FRIEND_TEST(WiFiServiceUpdateFromEndpointsTest, EndpointModified);
   FRIEND_TEST(WiFiServiceUpdateFromEndpointsTest, Ieee80211w);
diff --git a/wifi_provider_unittest.cc b/wifi_provider_unittest.cc
index f3c2b67..a63a577 100644
--- a/wifi_provider_unittest.cc
+++ b/wifi_provider_unittest.cc
@@ -1076,6 +1076,55 @@
   provider_.OnEndpointRemoved(endpoint);
 }
 
+TEST_F(WiFiProviderTest, OnEndpointUpdated) {
+  provider_.Start();
+
+  // Create an endpoint and associate it with a mock service.
+  const string ssid("an_ssid");
+  WiFiEndpointRefPtr endpoint = MakeEndpoint(ssid, "00:00:00:00:00:00", 0, 0);
+
+  const vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
+  MockWiFiServiceRefPtr open_service = AddMockService(ssid_bytes,
+                                                      flimflam::kModeManaged,
+                                                      flimflam::kSecurityNone,
+                                                      false);
+  EXPECT_CALL(*open_service, AddEndpoint(RefPtrMatch(endpoint)));
+  EXPECT_CALL(manager_, UpdateService(RefPtrMatch(open_service)));
+  provider_.OnEndpointAdded(endpoint);
+  Mock::VerifyAndClearExpectations(open_service);
+
+  // WiFiProvider is running and endpoint matches this service.
+  EXPECT_CALL(*open_service, NotifyEndpointUpdated(RefPtrMatch(endpoint)));
+  EXPECT_CALL(*open_service, AddEndpoint(_)).Times(0);
+  provider_.OnEndpointUpdated(endpoint);
+  Mock::VerifyAndClearExpectations(open_service);
+
+  // If the endpoint is changed in a way that causes it to match a different
+  // service, the provider should transfer the endpoint from one service to
+  // the other.
+  MockWiFiServiceRefPtr rsn_service = AddMockService(ssid_bytes,
+                                                     flimflam::kModeManaged,
+                                                     flimflam::kSecurityRsn,
+                                                     false);
+  EXPECT_CALL(*open_service, RemoveEndpoint(RefPtrMatch(endpoint)));
+  // We are playing out a scenario where the open service is not removed
+  // since it still claims to have more endpoints remaining.
+  EXPECT_CALL(*open_service, HasEndpoints()).WillOnce(Return(true));
+  EXPECT_CALL(*rsn_service, AddEndpoint(RefPtrMatch(endpoint)));
+  EXPECT_CALL(manager_, UpdateService(RefPtrMatch(open_service)));
+  EXPECT_CALL(manager_, UpdateService(RefPtrMatch(rsn_service)));
+  endpoint->set_security_mode(flimflam::kSecurityRsn);
+  provider_.OnEndpointUpdated(endpoint);
+}
+
+TEST_F(WiFiProviderTest, OnEndpointUpdatedWhileStopped) {
+  // If we don't call provider_.Start(), OnEndpointUpdated should not
+  // cause a crash even if a service matching the endpoint does not exist.
+  const string ssid("an_ssid");
+  WiFiEndpointRefPtr endpoint = MakeEndpoint(ssid, "00:00:00:00:00:00", 0, 0);
+  provider_.OnEndpointUpdated(endpoint);
+}
+
 TEST_F(WiFiProviderTest, OnServiceUnloaded) {
   // This function should never unregister services itself -- the Manager
   // will automatically deregister the service if OnServiceUnloaded()