Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 5 | #include "shill/profile.h" |
| 6 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 10 | #include <base/file_path.h> |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 11 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 12 | #include <base/string_util.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 15 | #include "shill/glib.h" |
| 16 | #include "shill/key_file_store.h" |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 17 | #include "shill/mock_manager.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 18 | #include "shill/mock_profile.h" |
| 19 | #include "shill/mock_service.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 20 | #include "shill/mock_store.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 21 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 22 | #include "shill/service_under_test.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 23 | |
| 24 | using std::set; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 25 | using std::string; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 26 | using std::vector; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 27 | using testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 28 | using testing::Invoke; |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 29 | using testing::Mock; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 30 | using testing::Return; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 31 | using testing::SetArgumentPointee; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 32 | using testing::StrictMock; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 33 | |
| 34 | namespace shill { |
| 35 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 36 | class ProfileTest : public PropertyStoreTest { |
| 37 | public: |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 38 | ProfileTest() { |
| 39 | Profile::Identifier id("rather", "irrelevant"); |
| 40 | profile_ = new Profile(control_interface(), manager(), id, "", false); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | MockService *CreateMockService() { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 44 | return new StrictMock<MockService>(control_interface(), |
| 45 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 46 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 47 | manager()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 50 | virtual void SetUp() { |
| 51 | PropertyStoreTest::SetUp(); |
| 52 | FilePath final_path(storage_path()); |
| 53 | final_path = final_path.Append("test.profile"); |
| 54 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(&real_glib_)); |
| 55 | storage->set_path(final_path); |
| 56 | ASSERT_TRUE(storage->Open()); |
| 57 | profile_->set_storage(storage.release()); // Passes ownership. |
| 58 | } |
| 59 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 60 | bool ProfileInitStorage(const Profile::Identifier &id, |
| 61 | Profile::InitStorageOption storage_option, |
| 62 | bool save, |
| 63 | Error::Type error_type) { |
| 64 | Error error; |
| 65 | ProfileRefPtr profile( |
| 66 | new Profile(control_interface(), manager(), id, storage_path(), false)); |
| 67 | bool ret = profile->InitStorage(&real_glib_, storage_option, &error); |
| 68 | EXPECT_EQ(error_type, error.type()); |
| 69 | if (ret && save) { |
| 70 | EXPECT_TRUE(profile->Save()); |
| 71 | } |
| 72 | return ret; |
| 73 | } |
| 74 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 75 | protected: |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 76 | GLib real_glib_; |
| 77 | ProfileRefPtr profile_; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 80 | TEST_F(ProfileTest, DeleteEntry) { |
| 81 | scoped_ptr<MockManager> manager(new StrictMock<MockManager>( |
| 82 | control_interface(), dispatcher(), metrics(), glib())); |
| 83 | profile_->manager_ = manager.get(); |
| 84 | |
| 85 | MockStore *storage(new StrictMock<MockStore>()); |
| 86 | profile_->storage_.reset(storage); // Passes ownership |
| 87 | const string kEntryName("entry_name"); |
| 88 | |
| 89 | // If entry does not appear in storage, DeleteEntry() should return an error. |
| 90 | EXPECT_CALL(*storage, ContainsGroup(kEntryName)) |
| 91 | .WillOnce(Return(false)); |
| 92 | { |
| 93 | Error error; |
| 94 | profile_->DeleteEntry(kEntryName, &error); |
| 95 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 96 | } |
| 97 | |
| 98 | Mock::VerifyAndClearExpectations(storage); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 99 | |
| 100 | // If HandleProfileEntryDeletion() returns false, Profile should call |
| 101 | // DeleteGroup() itself. |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 102 | EXPECT_CALL(*storage, ContainsGroup(kEntryName)) |
| 103 | .WillOnce(Return(true)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 104 | EXPECT_CALL(*manager.get(), HandleProfileEntryDeletion(_, kEntryName)) |
| 105 | .WillOnce(Return(false)); |
| 106 | EXPECT_CALL(*storage, DeleteGroup(kEntryName)) |
| 107 | .WillOnce(Return(true)); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 108 | EXPECT_CALL(*storage, Flush()) |
| 109 | .WillOnce(Return(true)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 110 | { |
| 111 | Error error; |
| 112 | profile_->DeleteEntry(kEntryName, &error); |
| 113 | EXPECT_TRUE(error.IsSuccess()); |
| 114 | } |
| 115 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 116 | Mock::VerifyAndClearExpectations(storage); |
| 117 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 118 | // If HandleProfileEntryDeletion() returns true, Profile should not call |
| 119 | // DeleteGroup() itself. |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 120 | EXPECT_CALL(*storage, ContainsGroup(kEntryName)) |
| 121 | .WillOnce(Return(true)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 122 | EXPECT_CALL(*manager.get(), HandleProfileEntryDeletion(_, kEntryName)) |
| 123 | .WillOnce(Return(true)); |
| 124 | EXPECT_CALL(*storage, DeleteGroup(kEntryName)) |
| 125 | .Times(0); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 126 | EXPECT_CALL(*storage, Flush()) |
| 127 | .WillOnce(Return(true)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 128 | { |
| 129 | Error error; |
| 130 | profile_->DeleteEntry(kEntryName, &error); |
| 131 | EXPECT_TRUE(error.IsSuccess()); |
| 132 | } |
| 133 | } |
| 134 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 135 | TEST_F(ProfileTest, IsValidIdentifierToken) { |
| 136 | EXPECT_FALSE(Profile::IsValidIdentifierToken("")); |
| 137 | EXPECT_FALSE(Profile::IsValidIdentifierToken(" ")); |
| 138 | EXPECT_FALSE(Profile::IsValidIdentifierToken("-")); |
| 139 | EXPECT_FALSE(Profile::IsValidIdentifierToken("~")); |
| 140 | EXPECT_FALSE(Profile::IsValidIdentifierToken("_")); |
| 141 | EXPECT_TRUE(Profile::IsValidIdentifierToken("a")); |
| 142 | EXPECT_TRUE(Profile::IsValidIdentifierToken("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); |
| 143 | EXPECT_TRUE(Profile::IsValidIdentifierToken("abcdefghijklmnopqrstuvwxyz")); |
| 144 | EXPECT_TRUE(Profile::IsValidIdentifierToken("0123456789")); |
| 145 | } |
| 146 | |
| 147 | TEST_F(ProfileTest, ParseIdentifier) { |
| 148 | Profile::Identifier identifier; |
| 149 | EXPECT_FALSE(Profile::ParseIdentifier("", &identifier)); |
| 150 | EXPECT_FALSE(Profile::ParseIdentifier("~", &identifier)); |
| 151 | EXPECT_FALSE(Profile::ParseIdentifier("~foo", &identifier)); |
| 152 | EXPECT_FALSE(Profile::ParseIdentifier("~/", &identifier)); |
| 153 | EXPECT_FALSE(Profile::ParseIdentifier("~bar/", &identifier)); |
| 154 | EXPECT_FALSE(Profile::ParseIdentifier("~/zoo", &identifier)); |
| 155 | EXPECT_FALSE(Profile::ParseIdentifier("~./moo", &identifier)); |
| 156 | EXPECT_FALSE(Profile::ParseIdentifier("~valid/?", &identifier)); |
| 157 | EXPECT_FALSE(Profile::ParseIdentifier("~no//no", &identifier)); |
| 158 | EXPECT_FALSE(Profile::ParseIdentifier("~no~no", &identifier)); |
| 159 | |
| 160 | static const char kUser[] = "user"; |
| 161 | static const char kIdentifier[] = "identifier"; |
| 162 | EXPECT_TRUE(Profile::ParseIdentifier( |
| 163 | base::StringPrintf("~%s/%s", kUser, kIdentifier), |
| 164 | &identifier)); |
| 165 | EXPECT_EQ(kUser, identifier.user); |
| 166 | EXPECT_EQ(kIdentifier, identifier.identifier); |
| 167 | |
| 168 | EXPECT_FALSE(Profile::ParseIdentifier("!", &identifier)); |
| 169 | EXPECT_FALSE(Profile::ParseIdentifier("/nope", &identifier)); |
| 170 | |
| 171 | static const char kIdentifier2[] = "something"; |
| 172 | EXPECT_TRUE(Profile::ParseIdentifier(kIdentifier2, &identifier)); |
| 173 | EXPECT_EQ("", identifier.user); |
| 174 | EXPECT_EQ(kIdentifier2, identifier.identifier); |
| 175 | } |
| 176 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 177 | TEST_F(ProfileTest, GetFriendlyName) { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 178 | static const char kUser[] = "theUser"; |
| 179 | static const char kIdentifier[] = "theIdentifier"; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 180 | Profile::Identifier id(kIdentifier); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 181 | ProfileRefPtr profile( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 182 | new Profile(control_interface(), manager(), id, "", false)); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 183 | EXPECT_EQ(kIdentifier, profile->GetFriendlyName()); |
| 184 | id.user = kUser; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 185 | profile = new Profile(control_interface(), manager(), id, "", false); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 186 | EXPECT_EQ(string(kUser) + "/" + kIdentifier, profile->GetFriendlyName()); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TEST_F(ProfileTest, GetStoragePath) { |
| 190 | static const char kUser[] = "chronos"; |
| 191 | static const char kIdentifier[] = "someprofile"; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 192 | static const char kFormat[] = "/a/place/for/%s"; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 193 | FilePath path; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 194 | Profile::Identifier id(kIdentifier); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 195 | ProfileRefPtr profile( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 196 | new Profile(control_interface(), manager(), id, "", false)); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 197 | EXPECT_FALSE(profile->GetStoragePath(&path)); |
| 198 | id.user = kUser; |
| 199 | profile = |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 200 | new Profile(control_interface(), manager(), id, kFormat, false); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 201 | EXPECT_TRUE(profile->GetStoragePath(&path)); |
| 202 | string suffix = base::StringPrintf("/%s.profile", kIdentifier); |
| 203 | EXPECT_EQ(base::StringPrintf(kFormat, kUser) + suffix, path.value()); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 206 | TEST_F(ProfileTest, ServiceManagement) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 207 | scoped_refptr<MockService> service1(CreateMockService()); |
| 208 | scoped_refptr<MockService> service2(CreateMockService()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 209 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 210 | EXPECT_CALL(*service1.get(), Save(_)) |
| 211 | .WillRepeatedly(Invoke(service1.get(), &MockService::FauxSave)); |
| 212 | EXPECT_CALL(*service2.get(), Save(_)) |
| 213 | .WillRepeatedly(Invoke(service2.get(), &MockService::FauxSave)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 214 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 215 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 216 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 217 | |
| 218 | // Ensure services are in the profile now. |
| 219 | ASSERT_TRUE(profile_->ContainsService(service1)); |
| 220 | ASSERT_TRUE(profile_->ContainsService(service2)); |
| 221 | |
| 222 | // Ensure we can't add them twice. |
| 223 | ASSERT_FALSE(profile_->AdoptService(service1)); |
| 224 | ASSERT_FALSE(profile_->AdoptService(service2)); |
| 225 | |
| 226 | // Ensure that we can abandon individually, and that doing so is idempotent. |
| 227 | ASSERT_TRUE(profile_->AbandonService(service1)); |
| 228 | ASSERT_FALSE(profile_->ContainsService(service1)); |
| 229 | ASSERT_TRUE(profile_->AbandonService(service1)); |
| 230 | ASSERT_TRUE(profile_->ContainsService(service2)); |
| 231 | |
| 232 | // Clean up. |
| 233 | ASSERT_TRUE(profile_->AbandonService(service2)); |
| 234 | ASSERT_FALSE(profile_->ContainsService(service1)); |
| 235 | ASSERT_FALSE(profile_->ContainsService(service2)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 238 | TEST_F(ProfileTest, ServiceConfigure) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 239 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 240 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 241 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 242 | manager())); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 243 | service1->set_priority(service1->priority() + 1); // Change from default. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 244 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 245 | ASSERT_TRUE(profile_->ContainsService(service1)); |
| 246 | |
| 247 | // Create new service; ask Profile to merge it with a known, matching, |
| 248 | // service; ensure that settings from |service1| wind up in |service2|. |
| 249 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 250 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 251 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 252 | manager())); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 253 | int32 orig_priority = service2->priority(); |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 254 | ASSERT_TRUE(profile_->ConfigureService(service2)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 255 | ASSERT_EQ(service1->priority(), service2->priority()); |
| 256 | ASSERT_NE(orig_priority, service2->priority()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 257 | |
| 258 | // Clean up. |
| 259 | ASSERT_TRUE(profile_->AbandonService(service1)); |
| 260 | ASSERT_FALSE(profile_->ContainsService(service1)); |
| 261 | ASSERT_FALSE(profile_->ContainsService(service2)); |
| 262 | } |
| 263 | |
| 264 | TEST_F(ProfileTest, Save) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 265 | scoped_refptr<MockService> service1(CreateMockService()); |
| 266 | scoped_refptr<MockService> service2(CreateMockService()); |
| 267 | EXPECT_CALL(*service1.get(), Save(_)).WillOnce(Return(true)); |
| 268 | EXPECT_CALL(*service2.get(), Save(_)).WillOnce(Return(true)); |
| 269 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 270 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 271 | ASSERT_TRUE(profile_->AdoptService(service2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 272 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 273 | profile_->Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 276 | TEST_F(ProfileTest, EntryEnumeration) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 277 | scoped_refptr<MockService> service1(CreateMockService()); |
| 278 | scoped_refptr<MockService> service2(CreateMockService()); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 279 | string service1_storage_name = Technology::NameFromIdentifier( |
| 280 | Technology::kCellular) + "_1"; |
| 281 | string service2_storage_name = Technology::NameFromIdentifier( |
| 282 | Technology::kCellular) + "_2"; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 283 | EXPECT_CALL(*service1.get(), Save(_)) |
| 284 | .WillRepeatedly(Invoke(service1.get(), &MockService::FauxSave)); |
| 285 | EXPECT_CALL(*service2.get(), Save(_)) |
| 286 | .WillRepeatedly(Invoke(service2.get(), &MockService::FauxSave)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 287 | EXPECT_CALL(*service1.get(), GetStorageIdentifier()) |
| 288 | .WillRepeatedly(Return(service1_storage_name)); |
| 289 | EXPECT_CALL(*service2.get(), GetStorageIdentifier()) |
| 290 | .WillRepeatedly(Return(service2_storage_name)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 291 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 292 | string service1_name(service1->UniqueName()); |
| 293 | string service2_name(service2->UniqueName()); |
| 294 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 295 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 296 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 297 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 298 | Error error; |
| 299 | ASSERT_EQ(2, profile_->EnumerateEntries(&error).size()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 300 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 301 | ASSERT_TRUE(profile_->AbandonService(service1)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 302 | ASSERT_EQ(service2_storage_name, profile_->EnumerateEntries(&error)[0]); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 303 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 304 | ASSERT_TRUE(profile_->AbandonService(service2)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 305 | ASSERT_EQ(0, profile_->EnumerateEntries(&error).size()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 308 | TEST_F(ProfileTest, MatchesIdentifier) { |
| 309 | static const char kUser[] = "theUser"; |
| 310 | static const char kIdentifier[] = "theIdentifier"; |
| 311 | Profile::Identifier id(kUser, kIdentifier); |
| 312 | ProfileRefPtr profile( |
| 313 | new Profile(control_interface(), manager(), id, "", false)); |
| 314 | EXPECT_TRUE(profile->MatchesIdentifier(id)); |
| 315 | EXPECT_FALSE(profile->MatchesIdentifier(Profile::Identifier(kUser, ""))); |
| 316 | EXPECT_FALSE( |
| 317 | profile->MatchesIdentifier(Profile::Identifier("", kIdentifier))); |
| 318 | EXPECT_FALSE( |
| 319 | profile->MatchesIdentifier(Profile::Identifier(kIdentifier, kUser))); |
| 320 | } |
| 321 | |
| 322 | TEST_F(ProfileTest, InitStorage) { |
| 323 | Profile::Identifier id("theUser", "theIdentifier"); |
| 324 | |
| 325 | // Profile doesn't exist but we wanted it to. |
| 326 | EXPECT_FALSE(ProfileInitStorage(id, Profile::kOpenExisting, false, |
| 327 | Error::kNotFound)); |
| 328 | |
| 329 | // Success case, with a side effect of creating the profile. |
| 330 | EXPECT_TRUE(ProfileInitStorage(id, Profile::kCreateNew, true, |
| 331 | Error::kSuccess)); |
| 332 | |
| 333 | // The results from our two test cases above will now invert since |
| 334 | // the profile now exists. First, we now succeed if we require that |
| 335 | // the profile already exist... |
| 336 | EXPECT_TRUE(ProfileInitStorage(id, Profile::kOpenExisting, false, |
| 337 | Error::kSuccess)); |
| 338 | |
| 339 | // And we fail if we require that it doesn't. |
| 340 | EXPECT_FALSE(ProfileInitStorage(id, Profile::kCreateNew, false, |
| 341 | Error::kAlreadyExists)); |
| 342 | |
| 343 | // As a sanity check, ensure "create or open" works for both profile-exists... |
| 344 | EXPECT_TRUE(ProfileInitStorage(id, Profile::kCreateOrOpenExisting, false, |
| 345 | Error::kSuccess)); |
| 346 | |
| 347 | // ...and for a new profile that doesn't exist. |
| 348 | Profile::Identifier id2("theUser", "theIdentifier2"); |
| 349 | // Let's just make double-check that this profile really doesn't exist. |
| 350 | ASSERT_FALSE(ProfileInitStorage(id2, Profile::kOpenExisting, false, |
| 351 | Error::kNotFound)); |
| 352 | |
| 353 | // Then test that with "create or open" we succeed. |
| 354 | EXPECT_TRUE(ProfileInitStorage(id2, Profile::kCreateOrOpenExisting, false, |
| 355 | Error::kSuccess)); |
| 356 | } |
| 357 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 358 | } // namespace shill |