[shill] Enable Profile objects to manage a list of Services to persist.

The Manager will hang on to a list of active services.  They will be sorted,
someday, in an order that makes sense.  Every service will be associated with
a Profile, though it may the an ephemeral profile that won't persist state to
disk.  Profiles will maintain a map of service name to service references to
track the services whose state they persist to disk.  Services may move between
Profiles, and will certainly need to be bound to one after they are registered
with the Manager, so support for this is provided as well.

BUG=chromium-os:17436
TEST=unit tests

Change-Id: Id43a0e1d97302b6f574bd2213d4f3d176bb5223f
Reviewed-on: http://gerrit.chromium.org/gerrit/4033
Reviewed-by: Chris Masone <cmasone@chromium.org>
Tested-by: Chris Masone <cmasone@chromium.org>
diff --git a/profile_unittest.cc b/profile_unittest.cc
index 8729d19..115afe8 100644
--- a/profile_unittest.cc
+++ b/profile_unittest.cc
@@ -4,15 +4,32 @@
 
 #include "shill/profile.h"
 
+#include <string>
+#include <vector>
+
 #include <base/string_util.h>
 #include <gtest/gtest.h>
 
+#include "shill/mock_profile.h"
+#include "shill/mock_service.h"
+#include "shill/property_store_unittest.h"
+
+using std::set;
 using std::string;
-using testing::Test;
+using std::vector;
+using testing::Return;
+using testing::StrictMock;
 
 namespace shill {
 
-class ProfileTest : public Test {
+class ProfileTest : public PropertyStoreTest {
+ public:
+  ProfileTest()
+      : profile_(new Profile(&control_interface_, &glib_, &manager_)) {
+  }
+
+ protected:
+  ProfileRefPtr profile_;
 };
 
 TEST_F(ProfileTest, IsValidIdentifierToken) {
@@ -82,4 +99,67 @@
   EXPECT_EQ("/home/chronos/user/flimflam/someprofile.profile", path.value());
 }
 
+TEST_F(ProfileTest, ServiceManagement) {
+  const char kService1[] = "service1";
+  const char kService2[] = "wifi_service2";
+  {
+    ServiceRefPtr service1(
+        new StrictMock<MockService>(&control_interface_,
+                                    &dispatcher_,
+                                    &manager_,
+                                    kService1));
+    ServiceRefPtr service2(
+        new StrictMock<MockService>(&control_interface_,
+                                    &dispatcher_,
+                                    &manager_,
+                                    kService2));
+    ASSERT_TRUE(profile_->AdoptService(service1));
+    ASSERT_TRUE(profile_->AdoptService(service2));
+    ASSERT_FALSE(profile_->AdoptService(service1));
+  }
+  ASSERT_TRUE(profile_->FindService(kService1).get() != NULL);
+  ASSERT_TRUE(profile_->FindService(kService2).get() != NULL);
+
+  ASSERT_TRUE(profile_->AbandonService(kService1));
+  ASSERT_TRUE(profile_->FindService(kService1).get() == NULL);
+  ASSERT_TRUE(profile_->FindService(kService2).get() != NULL);
+
+  ASSERT_FALSE(profile_->AbandonService(kService1));
+  ASSERT_TRUE(profile_->AbandonService(kService2));
+  ASSERT_TRUE(profile_->FindService(kService1).get() == NULL);
+  ASSERT_TRUE(profile_->FindService(kService2).get() == NULL);
+}
+
+TEST_F(ProfileTest, EntryEnumeration) {
+  const char *kServices[] = { "service1",
+                              "wifi_service2" };
+  scoped_refptr<MockService> service1(
+      new StrictMock<MockService>(&control_interface_,
+                                  &dispatcher_,
+                                  &manager_,
+                                  kServices[0]));
+  EXPECT_CALL(*service1.get(), GetRpcIdentifier())
+      .WillRepeatedly(Return(kServices[0]));
+  scoped_refptr<MockService> service2(
+      new StrictMock<MockService>(&control_interface_,
+                                  &dispatcher_,
+                                  &manager_,
+                                  kServices[1]));
+  EXPECT_CALL(*service2.get(), GetRpcIdentifier())
+      .WillRepeatedly(Return(kServices[1]));
+  ASSERT_TRUE(profile_->AdoptService(service1));
+  ASSERT_TRUE(profile_->AdoptService(service2));
+
+  ASSERT_EQ(profile_->EnumerateEntries().size(), 2);
+
+  ASSERT_TRUE(profile_->AbandonService(kServices[0]));
+  ASSERT_EQ(profile_->EnumerateEntries()[0], kServices[1]);
+
+  ASSERT_FALSE(profile_->AbandonService(kServices[0]));
+  ASSERT_EQ(profile_->EnumerateEntries()[0], kServices[1]);
+
+  ASSERT_TRUE(profile_->AbandonService(kServices[1]));
+  ASSERT_EQ(profile_->EnumerateEntries().size(), 0);
+}
+
 } // namespace shill