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) { |
| 104 | const char kService1[] = "service1"; |
| 105 | const char kService2[] = "wifi_service2"; |
| 106 | { |
| 107 | ServiceRefPtr service1( |
| 108 | new StrictMock<MockService>(&control_interface_, |
| 109 | &dispatcher_, |
| 110 | &manager_, |
| 111 | kService1)); |
| 112 | ServiceRefPtr service2( |
| 113 | new StrictMock<MockService>(&control_interface_, |
| 114 | &dispatcher_, |
| 115 | &manager_, |
| 116 | kService2)); |
| 117 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 118 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 119 | ASSERT_FALSE(profile_->AdoptService(service1)); |
| 120 | } |
| 121 | ASSERT_TRUE(profile_->FindService(kService1).get() != NULL); |
| 122 | ASSERT_TRUE(profile_->FindService(kService2).get() != NULL); |
| 123 | |
| 124 | ASSERT_TRUE(profile_->AbandonService(kService1)); |
| 125 | ASSERT_TRUE(profile_->FindService(kService1).get() == NULL); |
| 126 | ASSERT_TRUE(profile_->FindService(kService2).get() != NULL); |
| 127 | |
| 128 | ASSERT_FALSE(profile_->AbandonService(kService1)); |
| 129 | ASSERT_TRUE(profile_->AbandonService(kService2)); |
| 130 | ASSERT_TRUE(profile_->FindService(kService1).get() == NULL); |
| 131 | ASSERT_TRUE(profile_->FindService(kService2).get() == NULL); |
| 132 | } |
| 133 | |
| 134 | TEST_F(ProfileTest, EntryEnumeration) { |
| 135 | const char *kServices[] = { "service1", |
| 136 | "wifi_service2" }; |
| 137 | scoped_refptr<MockService> service1( |
| 138 | new StrictMock<MockService>(&control_interface_, |
| 139 | &dispatcher_, |
| 140 | &manager_, |
| 141 | kServices[0])); |
| 142 | EXPECT_CALL(*service1.get(), GetRpcIdentifier()) |
| 143 | .WillRepeatedly(Return(kServices[0])); |
| 144 | scoped_refptr<MockService> service2( |
| 145 | new StrictMock<MockService>(&control_interface_, |
| 146 | &dispatcher_, |
| 147 | &manager_, |
| 148 | kServices[1])); |
| 149 | EXPECT_CALL(*service2.get(), GetRpcIdentifier()) |
| 150 | .WillRepeatedly(Return(kServices[1])); |
| 151 | ASSERT_TRUE(profile_->AdoptService(service1)); |
| 152 | ASSERT_TRUE(profile_->AdoptService(service2)); |
| 153 | |
| 154 | ASSERT_EQ(profile_->EnumerateEntries().size(), 2); |
| 155 | |
| 156 | ASSERT_TRUE(profile_->AbandonService(kServices[0])); |
| 157 | ASSERT_EQ(profile_->EnumerateEntries()[0], kServices[1]); |
| 158 | |
| 159 | ASSERT_FALSE(profile_->AbandonService(kServices[0])); |
| 160 | ASSERT_EQ(profile_->EnumerateEntries()[0], kServices[1]); |
| 161 | |
| 162 | ASSERT_TRUE(profile_->AbandonService(kServices[1])); |
| 163 | ASSERT_EQ(profile_->EnumerateEntries().size(), 0); |
| 164 | } |
| 165 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 166 | } // namespace shill |