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