blob: d7d2af2ec4cb3bc62e050c4c57add216946b5982 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkova4766822011-07-07 10:42:22 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masone7aa5f902011-07-11 11:13:35 -07005#include "shill/profile.h"
6
Chris Masone6791a432011-07-12 13:23:19 -07007#include <string>
8#include <vector>
9
Chris Masone6515aab2011-10-12 16:19:09 -070010#include <base/file_path.h>
Paul Stewart2ebc16d2012-08-23 10:38:39 -070011#include <base/file_util.h>
Chris Masoneb9c00592011-10-06 13:10:39 -070012#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050013#include <base/stringprintf.h>
Darin Petkova4766822011-07-07 10:42:22 -070014#include <base/string_util.h>
15#include <gtest/gtest.h>
16
Chris Masone6515aab2011-10-12 16:19:09 -070017#include "shill/glib.h"
18#include "shill/key_file_store.h"
Paul Stewart75225512012-01-26 22:51:33 -080019#include "shill/mock_manager.h"
Thieu Le5133b712013-02-19 14:47:21 -080020#include "shill/mock_metrics.h"
Chris Masone6791a432011-07-12 13:23:19 -070021#include "shill/mock_profile.h"
22#include "shill/mock_service.h"
Chris Masone9d779932011-08-25 16:33:41 -070023#include "shill/mock_store.h"
Chris Masone6791a432011-07-12 13:23:19 -070024#include "shill/property_store_unittest.h"
Chris Masone6515aab2011-10-12 16:19:09 -070025#include "shill/service_under_test.h"
Chris Masone6791a432011-07-12 13:23:19 -070026
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080027using base::FilePath;
Chris Masone6791a432011-07-12 13:23:19 -070028using std::set;
Darin Petkova4766822011-07-07 10:42:22 -070029using std::string;
Chris Masone6791a432011-07-12 13:23:19 -070030using std::vector;
Chris Masone9d779932011-08-25 16:33:41 -070031using testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070032using testing::Invoke;
Paul Stewart75225512012-01-26 22:51:33 -080033using testing::Mock;
Chris Masone6791a432011-07-12 13:23:19 -070034using testing::Return;
Chris Masone9d779932011-08-25 16:33:41 -070035using testing::SetArgumentPointee;
Chris Masone6791a432011-07-12 13:23:19 -070036using testing::StrictMock;
Darin Petkova4766822011-07-07 10:42:22 -070037
38namespace shill {
39
Chris Masone6791a432011-07-12 13:23:19 -070040class ProfileTest : public PropertyStoreTest {
41 public:
Thieu Le5133b712013-02-19 14:47:21 -080042 ProfileTest() : mock_metrics_(new MockMetrics(NULL)) {
Chris Masone6515aab2011-10-12 16:19:09 -070043 Profile::Identifier id("rather", "irrelevant");
Thieu Le5133b712013-02-19 14:47:21 -080044 profile_ =
45 new Profile(control_interface(), metrics(), manager(), id, "", false);
Chris Masone9d779932011-08-25 16:33:41 -070046 }
47
48 MockService *CreateMockService() {
Chris Masone2176a882011-09-14 22:29:15 -070049 return new StrictMock<MockService>(control_interface(),
50 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080051 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -070052 manager());
Chris Masone6791a432011-07-12 13:23:19 -070053 }
54
Chris Masone6515aab2011-10-12 16:19:09 -070055 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 Stewart5dc40aa2011-10-28 19:43:43 -070065 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 Le5133b712013-02-19 14:47:21 -080071 new Profile(control_interface(), mock_metrics_.get(), manager(), id,
72 storage_path(), false));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070073 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 Masone6791a432011-07-12 13:23:19 -070081 protected:
Chris Masone6515aab2011-10-12 16:19:09 -070082 GLib real_glib_;
Thieu Le5133b712013-02-19 14:47:21 -080083 scoped_ptr<MockMetrics> mock_metrics_;
Chris Masone6515aab2011-10-12 16:19:09 -070084 ProfileRefPtr profile_;
Darin Petkova4766822011-07-07 10:42:22 -070085};
86
Paul Stewart75225512012-01-26 22:51:33 -080087TEST_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 Stewart75225512012-01-26 22:51:33 -0800106
107 // If HandleProfileEntryDeletion() returns false, Profile should call
108 // DeleteGroup() itself.
Paul Stewart0756db92012-01-27 08:34:47 -0800109 EXPECT_CALL(*storage, ContainsGroup(kEntryName))
110 .WillOnce(Return(true));
Paul Stewart75225512012-01-26 22:51:33 -0800111 EXPECT_CALL(*manager.get(), HandleProfileEntryDeletion(_, kEntryName))
112 .WillOnce(Return(false));
113 EXPECT_CALL(*storage, DeleteGroup(kEntryName))
114 .WillOnce(Return(true));
Paul Stewart0756db92012-01-27 08:34:47 -0800115 EXPECT_CALL(*storage, Flush())
116 .WillOnce(Return(true));
Paul Stewart75225512012-01-26 22:51:33 -0800117 {
118 Error error;
119 profile_->DeleteEntry(kEntryName, &error);
120 EXPECT_TRUE(error.IsSuccess());
121 }
122
Paul Stewart0756db92012-01-27 08:34:47 -0800123 Mock::VerifyAndClearExpectations(storage);
124
Paul Stewart75225512012-01-26 22:51:33 -0800125 // If HandleProfileEntryDeletion() returns true, Profile should not call
126 // DeleteGroup() itself.
Paul Stewart0756db92012-01-27 08:34:47 -0800127 EXPECT_CALL(*storage, ContainsGroup(kEntryName))
128 .WillOnce(Return(true));
Paul Stewart75225512012-01-26 22:51:33 -0800129 EXPECT_CALL(*manager.get(), HandleProfileEntryDeletion(_, kEntryName))
130 .WillOnce(Return(true));
131 EXPECT_CALL(*storage, DeleteGroup(kEntryName))
132 .Times(0);
Paul Stewart0756db92012-01-27 08:34:47 -0800133 EXPECT_CALL(*storage, Flush())
134 .WillOnce(Return(true));
Paul Stewart75225512012-01-26 22:51:33 -0800135 {
136 Error error;
137 profile_->DeleteEntry(kEntryName, &error);
138 EXPECT_TRUE(error.IsSuccess());
139 }
140}
141
Darin Petkova4766822011-07-07 10:42:22 -0700142TEST_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
154TEST_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 Stewartf3eced92013-04-17 12:18:22 -0700184TEST_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 Masone7df0c672011-07-15 10:24:54 -0700194TEST_F(ProfileTest, GetFriendlyName) {
Darin Petkova4766822011-07-07 10:42:22 -0700195 static const char kUser[] = "theUser";
196 static const char kIdentifier[] = "theIdentifier";
Chris Masone6515aab2011-10-12 16:19:09 -0700197 Profile::Identifier id(kIdentifier);
Chris Masone7df0c672011-07-15 10:24:54 -0700198 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800199 new Profile(control_interface(), metrics(), manager(), id, "", false));
Chris Masone7df0c672011-07-15 10:24:54 -0700200 EXPECT_EQ(kIdentifier, profile->GetFriendlyName());
201 id.user = kUser;
Thieu Le5133b712013-02-19 14:47:21 -0800202 profile =
203 new Profile(control_interface(), metrics(), manager(), id, "", false);
Chris Masone7df0c672011-07-15 10:24:54 -0700204 EXPECT_EQ(string(kUser) + "/" + kIdentifier, profile->GetFriendlyName());
Darin Petkova4766822011-07-07 10:42:22 -0700205}
206
207TEST_F(ProfileTest, GetStoragePath) {
208 static const char kUser[] = "chronos";
209 static const char kIdentifier[] = "someprofile";
Chris Masone2ae797d2011-08-23 20:41:00 -0700210 static const char kFormat[] = "/a/place/for/%s";
Darin Petkova4766822011-07-07 10:42:22 -0700211 FilePath path;
Chris Masone6515aab2011-10-12 16:19:09 -0700212 Profile::Identifier id(kIdentifier);
Chris Masone2ae797d2011-08-23 20:41:00 -0700213 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800214 new Profile(control_interface(), metrics(), manager(), id, "", false));
Chris Masone2ae797d2011-08-23 20:41:00 -0700215 EXPECT_FALSE(profile->GetStoragePath(&path));
216 id.user = kUser;
217 profile =
Thieu Le5133b712013-02-19 14:47:21 -0800218 new Profile(control_interface(), metrics(), manager(), id, kFormat,
219 false);
Chris Masone2ae797d2011-08-23 20:41:00 -0700220 EXPECT_TRUE(profile->GetStoragePath(&path));
221 string suffix = base::StringPrintf("/%s.profile", kIdentifier);
222 EXPECT_EQ(base::StringPrintf(kFormat, kUser) + suffix, path.value());
Darin Petkova4766822011-07-07 10:42:22 -0700223}
224
Chris Masone6791a432011-07-12 13:23:19 -0700225TEST_F(ProfileTest, ServiceManagement) {
Chris Masone6515aab2011-10-12 16:19:09 -0700226 scoped_refptr<MockService> service1(CreateMockService());
227 scoped_refptr<MockService> service2(CreateMockService());
Chris Masone6791a432011-07-12 13:23:19 -0700228
Chris Masone6515aab2011-10-12 16:19:09 -0700229 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 Masone6791a432011-07-12 13:23:19 -0700233
Chris Masone6515aab2011-10-12 16:19:09 -0700234 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 Masone6791a432011-07-12 13:23:19 -0700255}
256
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700257TEST_F(ProfileTest, ServiceConfigure) {
Chris Masone6515aab2011-10-12 16:19:09 -0700258 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
259 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800260 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700261 manager()));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700262 // Change prioirty from default.
263 service1->SetPriority(service1->priority() + 1, NULL);
Chris Masone6515aab2011-10-12 16:19:09 -0700264 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 Le3426c8f2012-01-11 17:35:11 -0800271 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700272 manager()));
mukesh agrawal00917ce2011-11-22 23:56:55 +0000273 int32 orig_priority = service2->priority();
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700274 ASSERT_TRUE(profile_->ConfigureService(service2));
mukesh agrawal00917ce2011-11-22 23:56:55 +0000275 ASSERT_EQ(service1->priority(), service2->priority());
276 ASSERT_NE(orig_priority, service2->priority());
Chris Masone6515aab2011-10-12 16:19:09 -0700277
278 // Clean up.
279 ASSERT_TRUE(profile_->AbandonService(service1));
280 ASSERT_FALSE(profile_->ContainsService(service1));
281 ASSERT_FALSE(profile_->ContainsService(service2));
282}
283
284TEST_F(ProfileTest, Save) {
Chris Masone9d779932011-08-25 16:33:41 -0700285 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 Masone6515aab2011-10-12 16:19:09 -0700290 ASSERT_TRUE(profile_->AdoptService(service1));
291 ASSERT_TRUE(profile_->AdoptService(service2));
Chris Masone9d779932011-08-25 16:33:41 -0700292
Chris Masone6515aab2011-10-12 16:19:09 -0700293 profile_->Save();
Chris Masone9d779932011-08-25 16:33:41 -0700294}
295
Chris Masone6791a432011-07-12 13:23:19 -0700296TEST_F(ProfileTest, EntryEnumeration) {
Chris Masone6515aab2011-10-12 16:19:09 -0700297 scoped_refptr<MockService> service1(CreateMockService());
298 scoped_refptr<MockService> service2(CreateMockService());
Paul Stewart1b253142012-01-26 14:05:52 -0800299 string service1_storage_name = Technology::NameFromIdentifier(
300 Technology::kCellular) + "_1";
301 string service2_storage_name = Technology::NameFromIdentifier(
302 Technology::kCellular) + "_2";
Chris Masone6515aab2011-10-12 16:19:09 -0700303 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 Stewart1b253142012-01-26 14:05:52 -0800307 EXPECT_CALL(*service1.get(), GetStorageIdentifier())
308 .WillRepeatedly(Return(service1_storage_name));
309 EXPECT_CALL(*service2.get(), GetStorageIdentifier())
310 .WillRepeatedly(Return(service2_storage_name));
Chris Masone6515aab2011-10-12 16:19:09 -0700311
Darin Petkov457728b2013-01-09 09:49:08 +0100312 string service1_name(service1->unique_name());
313 string service2_name(service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700314
Chris Masone6791a432011-07-12 13:23:19 -0700315 ASSERT_TRUE(profile_->AdoptService(service1));
316 ASSERT_TRUE(profile_->AdoptService(service2));
317
Paul Stewart1b253142012-01-26 14:05:52 -0800318 Error error;
319 ASSERT_EQ(2, profile_->EnumerateEntries(&error).size());
Chris Masone6791a432011-07-12 13:23:19 -0700320
Chris Masone6515aab2011-10-12 16:19:09 -0700321 ASSERT_TRUE(profile_->AbandonService(service1));
Paul Stewart1b253142012-01-26 14:05:52 -0800322 ASSERT_EQ(service2_storage_name, profile_->EnumerateEntries(&error)[0]);
Chris Masone6791a432011-07-12 13:23:19 -0700323
Chris Masone6515aab2011-10-12 16:19:09 -0700324 ASSERT_TRUE(profile_->AbandonService(service2));
Paul Stewart1b253142012-01-26 14:05:52 -0800325 ASSERT_EQ(0, profile_->EnumerateEntries(&error).size());
Chris Masone6791a432011-07-12 13:23:19 -0700326}
327
Paul Stewart78af94c2013-04-17 16:02:06 -0700328TEST_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
361TEST_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 Stewart5dc40aa2011-10-28 19:43:43 -0700398TEST_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 Le5133b712013-02-19 14:47:21 -0800403 new Profile(control_interface(), metrics(), manager(), id, "", false));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700404 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
412TEST_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 Stewart2ebc16d2012-08-23 10:38:39 -0700446
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 Le5133b712013-02-19 14:47:21 -0800456 EXPECT_CALL(*mock_metrics_, NotifyCorruptedProfile());
Paul Stewart2ebc16d2012-08-23 10:38:39 -0700457 EXPECT_FALSE(ProfileInitStorage(id, Profile::kOpenExisting, false,
458 Error::kInternalError));
Thieu Le5133b712013-02-19 14:47:21 -0800459 Mock::VerifyAndClearExpectations(mock_metrics_.get());
Paul Stewart2ebc16d2012-08-23 10:38:39 -0700460
461 // But then on a second try the file no longer exists.
Thieu Le5133b712013-02-19 14:47:21 -0800462 EXPECT_CALL(*mock_metrics_, NotifyCorruptedProfile()).Times(0);
Paul Stewart2ebc16d2012-08-23 10:38:39 -0700463 ASSERT_FALSE(ProfileInitStorage(id, Profile::kOpenExisting, false,
464 Error::kNotFound));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700465}
466
Darin Petkove7c6ad32012-06-29 10:22:09 +0200467TEST_F(ProfileTest, UpdateDevice) {
468 EXPECT_FALSE(profile_->UpdateDevice(NULL));
469}
470
Paul Stewart7355ce12011-09-02 10:47:01 -0700471} // namespace shill