Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 10 | #include <base/string_util.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 13 | #include "shill/mock_profile.h" |
| 14 | #include "shill/mock_service.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 15 | #include "shill/mock_store.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 16 | #include "shill/property_store_unittest.h" |
| 17 | |
| 18 | using std::set; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 19 | using std::string; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 20 | using std::vector; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 21 | using testing::_; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 22 | using testing::Return; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 23 | using testing::SetArgumentPointee; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 24 | using testing::StrictMock; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 25 | |
| 26 | namespace shill { |
| 27 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 28 | class ProfileTest : public PropertyStoreTest { |
| 29 | public: |
| 30 | ProfileTest() |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 31 | : profile_(new MockProfile(control_interface(), manager(), "")) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | MockService *CreateMockService() { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 35 | return new StrictMock<MockService>(control_interface(), |
| 36 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 37 | manager()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | protected: |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 41 | MockStore store_; |
| 42 | scoped_refptr<MockProfile> profile_; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | TEST_F(ProfileTest, IsValidIdentifierToken) { |
| 46 | EXPECT_FALSE(Profile::IsValidIdentifierToken("")); |
| 47 | EXPECT_FALSE(Profile::IsValidIdentifierToken(" ")); |
| 48 | EXPECT_FALSE(Profile::IsValidIdentifierToken("-")); |
| 49 | EXPECT_FALSE(Profile::IsValidIdentifierToken("~")); |
| 50 | EXPECT_FALSE(Profile::IsValidIdentifierToken("_")); |
| 51 | EXPECT_TRUE(Profile::IsValidIdentifierToken("a")); |
| 52 | EXPECT_TRUE(Profile::IsValidIdentifierToken("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); |
| 53 | EXPECT_TRUE(Profile::IsValidIdentifierToken("abcdefghijklmnopqrstuvwxyz")); |
| 54 | EXPECT_TRUE(Profile::IsValidIdentifierToken("0123456789")); |
| 55 | } |
| 56 | |
| 57 | TEST_F(ProfileTest, ParseIdentifier) { |
| 58 | Profile::Identifier identifier; |
| 59 | EXPECT_FALSE(Profile::ParseIdentifier("", &identifier)); |
| 60 | EXPECT_FALSE(Profile::ParseIdentifier("~", &identifier)); |
| 61 | EXPECT_FALSE(Profile::ParseIdentifier("~foo", &identifier)); |
| 62 | EXPECT_FALSE(Profile::ParseIdentifier("~/", &identifier)); |
| 63 | EXPECT_FALSE(Profile::ParseIdentifier("~bar/", &identifier)); |
| 64 | EXPECT_FALSE(Profile::ParseIdentifier("~/zoo", &identifier)); |
| 65 | EXPECT_FALSE(Profile::ParseIdentifier("~./moo", &identifier)); |
| 66 | EXPECT_FALSE(Profile::ParseIdentifier("~valid/?", &identifier)); |
| 67 | EXPECT_FALSE(Profile::ParseIdentifier("~no//no", &identifier)); |
| 68 | EXPECT_FALSE(Profile::ParseIdentifier("~no~no", &identifier)); |
| 69 | |
| 70 | static const char kUser[] = "user"; |
| 71 | static const char kIdentifier[] = "identifier"; |
| 72 | EXPECT_TRUE(Profile::ParseIdentifier( |
| 73 | base::StringPrintf("~%s/%s", kUser, kIdentifier), |
| 74 | &identifier)); |
| 75 | EXPECT_EQ(kUser, identifier.user); |
| 76 | EXPECT_EQ(kIdentifier, identifier.identifier); |
| 77 | |
| 78 | EXPECT_FALSE(Profile::ParseIdentifier("!", &identifier)); |
| 79 | EXPECT_FALSE(Profile::ParseIdentifier("/nope", &identifier)); |
| 80 | |
| 81 | static const char kIdentifier2[] = "something"; |
| 82 | EXPECT_TRUE(Profile::ParseIdentifier(kIdentifier2, &identifier)); |
| 83 | EXPECT_EQ("", identifier.user); |
| 84 | EXPECT_EQ(kIdentifier2, identifier.identifier); |
| 85 | } |
| 86 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 87 | TEST_F(ProfileTest, GetFriendlyName) { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 88 | static const char kUser[] = "theUser"; |
| 89 | static const char kIdentifier[] = "theIdentifier"; |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 90 | Profile::Identifier id; |
| 91 | id.identifier = kIdentifier; |
| 92 | ProfileRefPtr profile( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 93 | new Profile(control_interface(), manager(), id, "", false)); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 94 | EXPECT_EQ(kIdentifier, profile->GetFriendlyName()); |
| 95 | id.user = kUser; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 96 | profile = new Profile(control_interface(), manager(), id, "", false); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 97 | EXPECT_EQ(string(kUser) + "/" + kIdentifier, profile->GetFriendlyName()); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | TEST_F(ProfileTest, GetStoragePath) { |
| 101 | static const char kUser[] = "chronos"; |
| 102 | static const char kIdentifier[] = "someprofile"; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 103 | static const char kFormat[] = "/a/place/for/%s"; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 104 | FilePath path; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 105 | Profile::Identifier id; |
| 106 | id.identifier = kIdentifier; |
| 107 | ProfileRefPtr profile( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 108 | new Profile(control_interface(), manager(), id, "", false)); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 109 | EXPECT_FALSE(profile->GetStoragePath(&path)); |
| 110 | id.user = kUser; |
| 111 | profile = |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 112 | new Profile(control_interface(), manager(), id, kFormat, false); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 113 | EXPECT_TRUE(profile->GetStoragePath(&path)); |
| 114 | string suffix = base::StringPrintf("/%s.profile", kIdentifier); |
| 115 | EXPECT_EQ(base::StringPrintf(kFormat, kUser) + suffix, path.value()); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 118 | TEST_F(ProfileTest, ServiceManagement) { |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 119 | string service1_name; |
| 120 | string service2_name; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 121 | { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 122 | ServiceRefPtr service1(CreateMockService()); |
| 123 | ServiceRefPtr service2(CreateMockService()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 124 | service1_name = service1->UniqueName(); |
| 125 | service2_name = service2->UniqueName(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 126 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 127 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 128 | ASSERT_FALSE(profile_->AdoptService(service1)); |
| 129 | } |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 130 | ASSERT_TRUE(profile_->FindService(service1_name).get() != NULL); |
| 131 | ASSERT_TRUE(profile_->FindService(service2_name).get() != NULL); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 132 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 133 | ASSERT_TRUE(profile_->AbandonService(service1_name)); |
| 134 | ASSERT_TRUE(profile_->FindService(service1_name).get() == NULL); |
| 135 | ASSERT_TRUE(profile_->FindService(service2_name).get() != NULL); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 136 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 137 | ASSERT_FALSE(profile_->AbandonService(service1_name)); |
| 138 | ASSERT_TRUE(profile_->AbandonService(service2_name)); |
| 139 | ASSERT_TRUE(profile_->FindService(service1_name).get() == NULL); |
| 140 | ASSERT_TRUE(profile_->FindService(service2_name).get() == NULL); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 143 | TEST_F(ProfileTest, Finalize) { |
| 144 | scoped_refptr<MockService> service1(CreateMockService()); |
| 145 | scoped_refptr<MockService> service2(CreateMockService()); |
| 146 | EXPECT_CALL(*service1.get(), Save(_)).WillOnce(Return(true)); |
| 147 | EXPECT_CALL(*service2.get(), Save(_)).WillOnce(Return(true)); |
| 148 | |
| 149 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 150 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 151 | |
| 152 | profile_->Finalize(&store_); |
| 153 | } |
| 154 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 155 | TEST_F(ProfileTest, EntryEnumeration) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 156 | scoped_refptr<MockService> service1( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 157 | new StrictMock<MockService>(control_interface(), |
| 158 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 159 | manager())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 160 | scoped_refptr<MockService> service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 161 | new StrictMock<MockService>(control_interface(), |
| 162 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 163 | manager())); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 164 | string service1_name(service1->UniqueName()); |
| 165 | string service2_name(service2->UniqueName()); |
| 166 | |
| 167 | EXPECT_CALL(*service1.get(), GetRpcIdentifier()) |
| 168 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 169 | EXPECT_CALL(*service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 170 | .WillRepeatedly(Return(service2_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 171 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 172 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 173 | |
| 174 | ASSERT_EQ(profile_->EnumerateEntries().size(), 2); |
| 175 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 176 | ASSERT_TRUE(profile_->AbandonService(service1_name)); |
| 177 | ASSERT_EQ(profile_->EnumerateEntries()[0], service2_name); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 178 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 179 | ASSERT_FALSE(profile_->AbandonService(service1_name)); |
| 180 | ASSERT_EQ(profile_->EnumerateEntries()[0], service2_name); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 181 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 182 | ASSERT_TRUE(profile_->AbandonService(service2_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 183 | ASSERT_EQ(profile_->EnumerateEntries().size(), 0); |
| 184 | } |
| 185 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 186 | } // namespace shill |