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( |
| 83 | new Profile(&control_interface_, &glib_, &manager_, id, false)); |
| 84 | EXPECT_EQ(kIdentifier, profile->GetFriendlyName()); |
| 85 | id.user = kUser; |
| 86 | profile = new Profile(&control_interface_, &glib_, &manager_, id, false); |
| 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"; |
| 93 | FilePath path; |
| 94 | Profile::Identifier identifier; |
| 95 | identifier.identifier = kIdentifier; |
| 96 | EXPECT_TRUE(Profile::GetStoragePath(identifier, &path)); |
| 97 | EXPECT_EQ("/var/cache/flimflam/someprofile.profile", path.value()); |
| 98 | identifier.user = kUser; |
| 99 | EXPECT_TRUE(Profile::GetStoragePath(identifier, &path)); |
| 100 | EXPECT_EQ("/home/chronos/user/flimflam/someprofile.profile", path.value()); |
| 101 | } |
| 102 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 103 | TEST_F(ProfileTest, ServiceManagement) { |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 104 | string service1_name; |
| 105 | string service2_name; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 106 | { |
| 107 | ServiceRefPtr service1( |
| 108 | new StrictMock<MockService>(&control_interface_, |
| 109 | &dispatcher_, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 110 | &manager_)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 111 | ServiceRefPtr service2( |
| 112 | new StrictMock<MockService>(&control_interface_, |
| 113 | &dispatcher_, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 114 | &manager_)); |
| 115 | service1_name = service1->UniqueName(); |
| 116 | service2_name = service2->UniqueName(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 117 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 118 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 119 | ASSERT_FALSE(profile_->AdoptService(service1)); |
| 120 | } |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(profile_->FindService(service1_name).get() != NULL); |
| 122 | ASSERT_TRUE(profile_->FindService(service2_name).get() != NULL); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 123 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 124 | ASSERT_TRUE(profile_->AbandonService(service1_name)); |
| 125 | ASSERT_TRUE(profile_->FindService(service1_name).get() == NULL); |
| 126 | ASSERT_TRUE(profile_->FindService(service2_name).get() != NULL); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 127 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 128 | ASSERT_FALSE(profile_->AbandonService(service1_name)); |
| 129 | ASSERT_TRUE(profile_->AbandonService(service2_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 | } |
| 133 | |
| 134 | TEST_F(ProfileTest, EntryEnumeration) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 135 | scoped_refptr<MockService> service1( |
| 136 | new StrictMock<MockService>(&control_interface_, |
| 137 | &dispatcher_, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 138 | &manager_)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 139 | scoped_refptr<MockService> service2( |
| 140 | new StrictMock<MockService>(&control_interface_, |
| 141 | &dispatcher_, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 142 | &manager_)); |
| 143 | string service1_name(service1->UniqueName()); |
| 144 | string service2_name(service2->UniqueName()); |
| 145 | |
| 146 | EXPECT_CALL(*service1.get(), GetRpcIdentifier()) |
| 147 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 148 | EXPECT_CALL(*service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 149 | .WillRepeatedly(Return(service2_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 150 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 151 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 152 | |
| 153 | ASSERT_EQ(profile_->EnumerateEntries().size(), 2); |
| 154 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 155 | ASSERT_TRUE(profile_->AbandonService(service1_name)); |
| 156 | ASSERT_EQ(profile_->EnumerateEntries()[0], service2_name); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 157 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 158 | ASSERT_FALSE(profile_->AbandonService(service1_name)); |
| 159 | ASSERT_EQ(profile_->EnumerateEntries()[0], service2_name); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 160 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 161 | ASSERT_TRUE(profile_->AbandonService(service2_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 162 | ASSERT_EQ(profile_->EnumerateEntries().size(), 0); |
| 163 | } |
| 164 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 165 | } // namespace shill |