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> |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 11 | #include <base/file_util.h> |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 12 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 13 | #include <base/stringprintf.h> |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 14 | #include <base/string_util.h> |
| 15 | #include <gtest/gtest.h> |
| 16 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 17 | #include "shill/glib.h" |
| 18 | #include "shill/key_file_store.h" |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 19 | #include "shill/mock_manager.h" |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 20 | #include "shill/mock_metrics.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 21 | #include "shill/mock_profile.h" |
| 22 | #include "shill/mock_service.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 23 | #include "shill/mock_store.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 24 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 25 | #include "shill/service_under_test.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 26 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 27 | using base::FilePath; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 28 | using std::set; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 29 | using std::string; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 30 | using std::vector; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 31 | using testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 32 | using testing::Invoke; |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 33 | using testing::Mock; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 34 | using testing::Return; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 35 | using testing::SetArgumentPointee; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 36 | using testing::StrictMock; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 37 | |
| 38 | namespace shill { |
| 39 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 40 | class ProfileTest : public PropertyStoreTest { |
| 41 | public: |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 42 | ProfileTest() : mock_metrics_(new MockMetrics(NULL)) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 43 | Profile::Identifier id("rather", "irrelevant"); |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 44 | profile_ = |
| 45 | new Profile(control_interface(), metrics(), manager(), id, "", false); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | MockService *CreateMockService() { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 49 | return new StrictMock<MockService>(control_interface(), |
| 50 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 51 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 52 | manager()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 55 | virtual void SetUp() { |
| 56 | PropertyStoreTest::SetUp(); |
| 57 | FilePath final_path(storage_path()); |
| 58 | final_path = final_path.Append("test.profile"); |
| 59 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(&real_glib_)); |
| 60 | storage->set_path(final_path); |
| 61 | ASSERT_TRUE(storage->Open()); |
| 62 | profile_->set_storage(storage.release()); // Passes ownership. |
| 63 | } |
| 64 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 65 | bool ProfileInitStorage(const Profile::Identifier &id, |
| 66 | Profile::InitStorageOption storage_option, |
| 67 | bool save, |
| 68 | Error::Type error_type) { |
| 69 | Error error; |
| 70 | ProfileRefPtr profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 71 | new Profile(control_interface(), mock_metrics_.get(), manager(), id, |
| 72 | storage_path(), false)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 73 | bool ret = profile->InitStorage(&real_glib_, storage_option, &error); |
| 74 | EXPECT_EQ(error_type, error.type()); |
| 75 | if (ret && save) { |
| 76 | EXPECT_TRUE(profile->Save()); |
| 77 | } |
| 78 | return ret; |
| 79 | } |
| 80 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 81 | protected: |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 82 | GLib real_glib_; |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 83 | scoped_ptr<MockMetrics> mock_metrics_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 84 | ProfileRefPtr profile_; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 87 | TEST_F(ProfileTest, DeleteEntry) { |
| 88 | scoped_ptr<MockManager> manager(new StrictMock<MockManager>( |
| 89 | control_interface(), dispatcher(), metrics(), glib())); |
| 90 | profile_->manager_ = manager.get(); |
| 91 | |
| 92 | MockStore *storage(new StrictMock<MockStore>()); |
| 93 | profile_->storage_.reset(storage); // Passes ownership |
| 94 | const string kEntryName("entry_name"); |
| 95 | |
| 96 | // If entry does not appear in storage, DeleteEntry() should return an error. |
| 97 | EXPECT_CALL(*storage, ContainsGroup(kEntryName)) |
| 98 | .WillOnce(Return(false)); |
| 99 | { |
| 100 | Error error; |
| 101 | profile_->DeleteEntry(kEntryName, &error); |
| 102 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 103 | } |
| 104 | |
| 105 | Mock::VerifyAndClearExpectations(storage); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 106 | |
| 107 | // If HandleProfileEntryDeletion() returns false, Profile should call |
| 108 | // DeleteGroup() itself. |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 109 | EXPECT_CALL(*storage, ContainsGroup(kEntryName)) |
| 110 | .WillOnce(Return(true)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 111 | EXPECT_CALL(*manager.get(), HandleProfileEntryDeletion(_, kEntryName)) |
| 112 | .WillOnce(Return(false)); |
| 113 | EXPECT_CALL(*storage, DeleteGroup(kEntryName)) |
| 114 | .WillOnce(Return(true)); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 115 | EXPECT_CALL(*storage, Flush()) |
| 116 | .WillOnce(Return(true)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 117 | { |
| 118 | Error error; |
| 119 | profile_->DeleteEntry(kEntryName, &error); |
| 120 | EXPECT_TRUE(error.IsSuccess()); |
| 121 | } |
| 122 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 123 | Mock::VerifyAndClearExpectations(storage); |
| 124 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 125 | // If HandleProfileEntryDeletion() returns true, Profile should not call |
| 126 | // DeleteGroup() itself. |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 127 | EXPECT_CALL(*storage, ContainsGroup(kEntryName)) |
| 128 | .WillOnce(Return(true)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 129 | EXPECT_CALL(*manager.get(), HandleProfileEntryDeletion(_, kEntryName)) |
| 130 | .WillOnce(Return(true)); |
| 131 | EXPECT_CALL(*storage, DeleteGroup(kEntryName)) |
| 132 | .Times(0); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 133 | EXPECT_CALL(*storage, Flush()) |
| 134 | .WillOnce(Return(true)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 135 | { |
| 136 | Error error; |
| 137 | profile_->DeleteEntry(kEntryName, &error); |
| 138 | EXPECT_TRUE(error.IsSuccess()); |
| 139 | } |
| 140 | } |
| 141 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 142 | TEST_F(ProfileTest, IsValidIdentifierToken) { |
| 143 | EXPECT_FALSE(Profile::IsValidIdentifierToken("")); |
| 144 | EXPECT_FALSE(Profile::IsValidIdentifierToken(" ")); |
| 145 | EXPECT_FALSE(Profile::IsValidIdentifierToken("-")); |
| 146 | EXPECT_FALSE(Profile::IsValidIdentifierToken("~")); |
| 147 | EXPECT_FALSE(Profile::IsValidIdentifierToken("_")); |
| 148 | EXPECT_TRUE(Profile::IsValidIdentifierToken("a")); |
| 149 | EXPECT_TRUE(Profile::IsValidIdentifierToken("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); |
| 150 | EXPECT_TRUE(Profile::IsValidIdentifierToken("abcdefghijklmnopqrstuvwxyz")); |
| 151 | EXPECT_TRUE(Profile::IsValidIdentifierToken("0123456789")); |
| 152 | } |
| 153 | |
| 154 | TEST_F(ProfileTest, ParseIdentifier) { |
| 155 | Profile::Identifier identifier; |
| 156 | EXPECT_FALSE(Profile::ParseIdentifier("", &identifier)); |
| 157 | EXPECT_FALSE(Profile::ParseIdentifier("~", &identifier)); |
| 158 | EXPECT_FALSE(Profile::ParseIdentifier("~foo", &identifier)); |
| 159 | EXPECT_FALSE(Profile::ParseIdentifier("~/", &identifier)); |
| 160 | EXPECT_FALSE(Profile::ParseIdentifier("~bar/", &identifier)); |
| 161 | EXPECT_FALSE(Profile::ParseIdentifier("~/zoo", &identifier)); |
| 162 | EXPECT_FALSE(Profile::ParseIdentifier("~./moo", &identifier)); |
| 163 | EXPECT_FALSE(Profile::ParseIdentifier("~valid/?", &identifier)); |
| 164 | EXPECT_FALSE(Profile::ParseIdentifier("~no//no", &identifier)); |
| 165 | EXPECT_FALSE(Profile::ParseIdentifier("~no~no", &identifier)); |
| 166 | |
| 167 | static const char kUser[] = "user"; |
| 168 | static const char kIdentifier[] = "identifier"; |
| 169 | EXPECT_TRUE(Profile::ParseIdentifier( |
| 170 | base::StringPrintf("~%s/%s", kUser, kIdentifier), |
| 171 | &identifier)); |
| 172 | EXPECT_EQ(kUser, identifier.user); |
| 173 | EXPECT_EQ(kIdentifier, identifier.identifier); |
| 174 | |
| 175 | EXPECT_FALSE(Profile::ParseIdentifier("!", &identifier)); |
| 176 | EXPECT_FALSE(Profile::ParseIdentifier("/nope", &identifier)); |
| 177 | |
| 178 | static const char kIdentifier2[] = "something"; |
| 179 | EXPECT_TRUE(Profile::ParseIdentifier(kIdentifier2, &identifier)); |
| 180 | EXPECT_EQ("", identifier.user); |
| 181 | EXPECT_EQ(kIdentifier2, identifier.identifier); |
| 182 | } |
| 183 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 184 | TEST_F(ProfileTest, IdentifierToString) { |
| 185 | Profile::Identifier identifier; |
| 186 | static const char kUser[] = "user"; |
| 187 | static const char kIdentifier[] = "identifier"; |
| 188 | identifier.user = kUser; |
| 189 | identifier.identifier = kIdentifier; |
| 190 | EXPECT_EQ(base::StringPrintf("~%s/%s", kUser, kIdentifier), |
| 191 | Profile::IdentifierToString(identifier)); |
| 192 | } |
| 193 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 194 | TEST_F(ProfileTest, GetFriendlyName) { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 195 | static const char kUser[] = "theUser"; |
| 196 | static const char kIdentifier[] = "theIdentifier"; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 197 | Profile::Identifier id(kIdentifier); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 198 | ProfileRefPtr profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 199 | new Profile(control_interface(), metrics(), manager(), id, "", false)); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 200 | EXPECT_EQ(kIdentifier, profile->GetFriendlyName()); |
| 201 | id.user = kUser; |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 202 | profile = |
| 203 | new Profile(control_interface(), metrics(), manager(), id, "", false); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 204 | EXPECT_EQ(string(kUser) + "/" + kIdentifier, profile->GetFriendlyName()); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | TEST_F(ProfileTest, GetStoragePath) { |
| 208 | static const char kUser[] = "chronos"; |
| 209 | static const char kIdentifier[] = "someprofile"; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 210 | static const char kFormat[] = "/a/place/for/%s"; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 211 | FilePath path; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 212 | Profile::Identifier id(kIdentifier); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 213 | ProfileRefPtr profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 214 | new Profile(control_interface(), metrics(), manager(), id, "", false)); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 215 | EXPECT_FALSE(profile->GetStoragePath(&path)); |
| 216 | id.user = kUser; |
| 217 | profile = |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 218 | new Profile(control_interface(), metrics(), manager(), id, kFormat, |
| 219 | false); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 220 | EXPECT_TRUE(profile->GetStoragePath(&path)); |
| 221 | string suffix = base::StringPrintf("/%s.profile", kIdentifier); |
| 222 | EXPECT_EQ(base::StringPrintf(kFormat, kUser) + suffix, path.value()); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 225 | TEST_F(ProfileTest, ServiceManagement) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 226 | scoped_refptr<MockService> service1(CreateMockService()); |
| 227 | scoped_refptr<MockService> service2(CreateMockService()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 228 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 229 | EXPECT_CALL(*service1.get(), Save(_)) |
| 230 | .WillRepeatedly(Invoke(service1.get(), &MockService::FauxSave)); |
| 231 | EXPECT_CALL(*service2.get(), Save(_)) |
| 232 | .WillRepeatedly(Invoke(service2.get(), &MockService::FauxSave)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 233 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 234 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 235 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 236 | |
| 237 | // Ensure services are in the profile now. |
| 238 | ASSERT_TRUE(profile_->ContainsService(service1)); |
| 239 | ASSERT_TRUE(profile_->ContainsService(service2)); |
| 240 | |
| 241 | // Ensure we can't add them twice. |
| 242 | ASSERT_FALSE(profile_->AdoptService(service1)); |
| 243 | ASSERT_FALSE(profile_->AdoptService(service2)); |
| 244 | |
| 245 | // Ensure that we can abandon individually, and that doing so is idempotent. |
| 246 | ASSERT_TRUE(profile_->AbandonService(service1)); |
| 247 | ASSERT_FALSE(profile_->ContainsService(service1)); |
| 248 | ASSERT_TRUE(profile_->AbandonService(service1)); |
| 249 | ASSERT_TRUE(profile_->ContainsService(service2)); |
| 250 | |
| 251 | // Clean up. |
| 252 | ASSERT_TRUE(profile_->AbandonService(service2)); |
| 253 | ASSERT_FALSE(profile_->ContainsService(service1)); |
| 254 | ASSERT_FALSE(profile_->ContainsService(service2)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 257 | TEST_F(ProfileTest, ServiceConfigure) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 258 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 259 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 260 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 261 | manager())); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 262 | // Change prioirty from default. |
| 263 | service1->SetPriority(service1->priority() + 1, NULL); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 264 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 265 | ASSERT_TRUE(profile_->ContainsService(service1)); |
| 266 | |
| 267 | // Create new service; ask Profile to merge it with a known, matching, |
| 268 | // service; ensure that settings from |service1| wind up in |service2|. |
| 269 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 270 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 271 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 272 | manager())); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 273 | int32 orig_priority = service2->priority(); |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 274 | ASSERT_TRUE(profile_->ConfigureService(service2)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 275 | ASSERT_EQ(service1->priority(), service2->priority()); |
| 276 | ASSERT_NE(orig_priority, service2->priority()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 277 | |
| 278 | // Clean up. |
| 279 | ASSERT_TRUE(profile_->AbandonService(service1)); |
| 280 | ASSERT_FALSE(profile_->ContainsService(service1)); |
| 281 | ASSERT_FALSE(profile_->ContainsService(service2)); |
| 282 | } |
| 283 | |
| 284 | TEST_F(ProfileTest, Save) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 285 | scoped_refptr<MockService> service1(CreateMockService()); |
| 286 | scoped_refptr<MockService> service2(CreateMockService()); |
| 287 | EXPECT_CALL(*service1.get(), Save(_)).WillOnce(Return(true)); |
| 288 | EXPECT_CALL(*service2.get(), Save(_)).WillOnce(Return(true)); |
| 289 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 290 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 291 | ASSERT_TRUE(profile_->AdoptService(service2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 292 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 293 | profile_->Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 296 | TEST_F(ProfileTest, EntryEnumeration) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 297 | scoped_refptr<MockService> service1(CreateMockService()); |
| 298 | scoped_refptr<MockService> service2(CreateMockService()); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 299 | string service1_storage_name = Technology::NameFromIdentifier( |
| 300 | Technology::kCellular) + "_1"; |
| 301 | string service2_storage_name = Technology::NameFromIdentifier( |
| 302 | Technology::kCellular) + "_2"; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 303 | EXPECT_CALL(*service1.get(), Save(_)) |
| 304 | .WillRepeatedly(Invoke(service1.get(), &MockService::FauxSave)); |
| 305 | EXPECT_CALL(*service2.get(), Save(_)) |
| 306 | .WillRepeatedly(Invoke(service2.get(), &MockService::FauxSave)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 307 | EXPECT_CALL(*service1.get(), GetStorageIdentifier()) |
| 308 | .WillRepeatedly(Return(service1_storage_name)); |
| 309 | EXPECT_CALL(*service2.get(), GetStorageIdentifier()) |
| 310 | .WillRepeatedly(Return(service2_storage_name)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 311 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 312 | string service1_name(service1->unique_name()); |
| 313 | string service2_name(service2->unique_name()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 314 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 315 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 316 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 317 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 318 | Error error; |
| 319 | ASSERT_EQ(2, profile_->EnumerateEntries(&error).size()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 320 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 321 | ASSERT_TRUE(profile_->AbandonService(service1)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 322 | ASSERT_EQ(service2_storage_name, profile_->EnumerateEntries(&error)[0]); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 323 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 324 | ASSERT_TRUE(profile_->AbandonService(service2)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 325 | ASSERT_EQ(0, profile_->EnumerateEntries(&error).size()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 328 | TEST_F(ProfileTest, LoadUserProfileList) { |
| 329 | FilePath list_path(FilePath(storage_path()).Append("test.profile")); |
| 330 | vector<Profile::Identifier> identifiers = |
| 331 | Profile::LoadUserProfileList(list_path); |
| 332 | EXPECT_TRUE(identifiers.empty()); |
| 333 | |
| 334 | const char kUser0[] = "scarecrow"; |
| 335 | const char kUser1[] = "jeans"; |
| 336 | const char kIdentifier0[] = "rattlesnake"; |
| 337 | const char kIdentifier1[] = "ceiling"; |
| 338 | const char kHash0[] = "neighbors"; |
| 339 | string data(base::StringPrintf("\n" |
| 340 | "~userbut/nospacehere\n" |
| 341 | "defaultprofile notaccepted\n" |
| 342 | "~%s/%s %s\n" |
| 343 | "~userbutno/hash\n" |
| 344 | " ~dontaccept/leadingspaces hash\n" |
| 345 | "~this_username_fails_to_parse/id hash\n" |
| 346 | "~%s/%s \n\n", |
| 347 | kUser0, kIdentifier0, kHash0, |
| 348 | kUser1, kIdentifier1)); |
| 349 | EXPECT_EQ(data.size(), |
| 350 | file_util::WriteFile(list_path, data.data(), data.size())); |
| 351 | identifiers = Profile::LoadUserProfileList(list_path); |
| 352 | EXPECT_EQ(2, identifiers.size()); |
| 353 | EXPECT_EQ(kUser0, identifiers[0].user); |
| 354 | EXPECT_EQ(kIdentifier0, identifiers[0].identifier); |
| 355 | EXPECT_EQ(kHash0, identifiers[0].user_hash); |
| 356 | EXPECT_EQ(kUser1, identifiers[1].user); |
| 357 | EXPECT_EQ(kIdentifier1, identifiers[1].identifier); |
| 358 | EXPECT_EQ("", identifiers[1].user_hash); |
| 359 | } |
| 360 | |
| 361 | TEST_F(ProfileTest, SaveUserProfileList) { |
| 362 | const char kUser0[] = "user0"; |
| 363 | const char kIdentifier0[] = "id0"; |
| 364 | Profile::Identifier id0(kUser0, kIdentifier0); |
| 365 | const char kHash0[] = "hash0"; |
| 366 | id0.user_hash = kHash0; |
| 367 | vector<ProfileRefPtr> profiles; |
| 368 | profiles.push_back(new Profile(control_interface(), metrics(), manager(), |
| 369 | id0, "", false)); |
| 370 | |
| 371 | const char kUser1[] = "user1"; |
| 372 | const char kIdentifier1[] = "id1"; |
| 373 | Profile::Identifier id1(kUser1, kIdentifier1); |
| 374 | const char kHash1[] = "hash1"; |
| 375 | id1.user_hash = kHash1; |
| 376 | profiles.push_back(new Profile(control_interface(), metrics(), manager(), |
| 377 | id1, "", false)); |
| 378 | |
| 379 | |
| 380 | const char kIdentifier2[] = "id2"; |
| 381 | Profile::Identifier id2("", kIdentifier2); |
| 382 | const char kHash2[] = "hash2"; |
| 383 | id1.user_hash = kHash2; |
| 384 | profiles.push_back(new Profile(control_interface(), metrics(), manager(), |
| 385 | id2, "", false)); |
| 386 | |
| 387 | FilePath list_path(FilePath(storage_path()).Append("test.profile")); |
| 388 | EXPECT_TRUE(Profile::SaveUserProfileList(list_path, profiles)); |
| 389 | |
| 390 | string profile_data; |
| 391 | EXPECT_TRUE(file_util::ReadFileToString(list_path, &profile_data)); |
| 392 | EXPECT_EQ(StringPrintf("~%s/%s %s\n~%s/%s %s\n", |
| 393 | kUser0, kIdentifier0, kHash0, |
| 394 | kUser1, kIdentifier1, kHash1), |
| 395 | profile_data); |
| 396 | } |
| 397 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 398 | TEST_F(ProfileTest, MatchesIdentifier) { |
| 399 | static const char kUser[] = "theUser"; |
| 400 | static const char kIdentifier[] = "theIdentifier"; |
| 401 | Profile::Identifier id(kUser, kIdentifier); |
| 402 | ProfileRefPtr profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 403 | new Profile(control_interface(), metrics(), manager(), id, "", false)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 404 | EXPECT_TRUE(profile->MatchesIdentifier(id)); |
| 405 | EXPECT_FALSE(profile->MatchesIdentifier(Profile::Identifier(kUser, ""))); |
| 406 | EXPECT_FALSE( |
| 407 | profile->MatchesIdentifier(Profile::Identifier("", kIdentifier))); |
| 408 | EXPECT_FALSE( |
| 409 | profile->MatchesIdentifier(Profile::Identifier(kIdentifier, kUser))); |
| 410 | } |
| 411 | |
| 412 | TEST_F(ProfileTest, InitStorage) { |
| 413 | Profile::Identifier id("theUser", "theIdentifier"); |
| 414 | |
| 415 | // Profile doesn't exist but we wanted it to. |
| 416 | EXPECT_FALSE(ProfileInitStorage(id, Profile::kOpenExisting, false, |
| 417 | Error::kNotFound)); |
| 418 | |
| 419 | // Success case, with a side effect of creating the profile. |
| 420 | EXPECT_TRUE(ProfileInitStorage(id, Profile::kCreateNew, true, |
| 421 | Error::kSuccess)); |
| 422 | |
| 423 | // The results from our two test cases above will now invert since |
| 424 | // the profile now exists. First, we now succeed if we require that |
| 425 | // the profile already exist... |
| 426 | EXPECT_TRUE(ProfileInitStorage(id, Profile::kOpenExisting, false, |
| 427 | Error::kSuccess)); |
| 428 | |
| 429 | // And we fail if we require that it doesn't. |
| 430 | EXPECT_FALSE(ProfileInitStorage(id, Profile::kCreateNew, false, |
| 431 | Error::kAlreadyExists)); |
| 432 | |
| 433 | // As a sanity check, ensure "create or open" works for both profile-exists... |
| 434 | EXPECT_TRUE(ProfileInitStorage(id, Profile::kCreateOrOpenExisting, false, |
| 435 | Error::kSuccess)); |
| 436 | |
| 437 | // ...and for a new profile that doesn't exist. |
| 438 | Profile::Identifier id2("theUser", "theIdentifier2"); |
| 439 | // Let's just make double-check that this profile really doesn't exist. |
| 440 | ASSERT_FALSE(ProfileInitStorage(id2, Profile::kOpenExisting, false, |
| 441 | Error::kNotFound)); |
| 442 | |
| 443 | // Then test that with "create or open" we succeed. |
| 444 | EXPECT_TRUE(ProfileInitStorage(id2, Profile::kCreateOrOpenExisting, false, |
| 445 | Error::kSuccess)); |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 446 | |
| 447 | // Corrupt the profile storage. |
| 448 | string suffix(base::StringPrintf("/%s.profile", id.identifier.c_str())); |
| 449 | FilePath final_path( |
| 450 | base::StringPrintf(storage_path().c_str(), id.user.c_str()) + suffix); |
| 451 | string data = "]corrupt_data["; |
| 452 | EXPECT_EQ(data.size(), |
| 453 | file_util::WriteFile(final_path, data.data(), data.size())); |
| 454 | |
| 455 | // Then test that we fail to open this file. |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 456 | EXPECT_CALL(*mock_metrics_, NotifyCorruptedProfile()); |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 457 | EXPECT_FALSE(ProfileInitStorage(id, Profile::kOpenExisting, false, |
| 458 | Error::kInternalError)); |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 459 | Mock::VerifyAndClearExpectations(mock_metrics_.get()); |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 460 | |
| 461 | // But then on a second try the file no longer exists. |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 462 | EXPECT_CALL(*mock_metrics_, NotifyCorruptedProfile()).Times(0); |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 463 | ASSERT_FALSE(ProfileInitStorage(id, Profile::kOpenExisting, false, |
| 464 | Error::kNotFound)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 467 | TEST_F(ProfileTest, UpdateDevice) { |
| 468 | EXPECT_FALSE(profile_->UpdateDevice(NULL)); |
| 469 | } |
| 470 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 471 | } // namespace shill |