Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/service.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 11 | #include <base/bind.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 13 | #include <dbus-c++/dbus.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
| 15 | #include <gmock/gmock.h> |
| 16 | |
| 17 | #include "shill/dbus_adaptor.h" |
| 18 | #include "shill/ethernet_service.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 19 | #include "shill/event_dispatcher.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 21 | #include "shill/mock_adaptors.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/mock_control.h" |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 23 | #include "shill/mock_connection.h" |
| 24 | #include "shill/mock_device_info.h" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 25 | #include "shill/mock_manager.h" |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 26 | #include "shill/mock_profile.h" |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 27 | #include "shill/mock_store.h" |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 28 | #include "shill/property_store_inspector.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 29 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 30 | #include "shill/service_under_test.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 31 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 32 | using base::Bind; |
| 33 | using base::Unretained; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 34 | using std::map; |
| 35 | using std::string; |
| 36 | using std::vector; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 37 | using testing::_; |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 38 | using testing::AnyNumber; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 39 | using testing::AtLeast; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 40 | using testing::DoAll; |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 41 | using testing::Mock; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 42 | using testing::NiceMock; |
| 43 | using testing::Return; |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 44 | using testing::ReturnRef; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 45 | using testing::StrictMock; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 46 | using testing::SetArgumentPointee; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 47 | using testing::Test; |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 48 | using testing::Values; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 49 | |
| 50 | namespace shill { |
| 51 | |
| 52 | class ServiceTest : public PropertyStoreTest { |
| 53 | public: |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 54 | ServiceTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 55 | : mock_manager_(control_interface(), dispatcher(), metrics(), glib()), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 56 | service_(new ServiceUnderTest(control_interface(), |
| 57 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 58 | metrics(), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 59 | &mock_manager_)), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 60 | storage_id_(ServiceUnderTest::kStorageId) { |
| 61 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 62 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 63 | virtual ~ServiceTest() {} |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 64 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 65 | MOCK_METHOD1(TestCallback, void(const Error &error)); |
| 66 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 67 | protected: |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 68 | typedef scoped_refptr<MockProfile> MockProfileRefPtr; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 69 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 70 | MockManager mock_manager_; |
| 71 | scoped_refptr<ServiceUnderTest> service_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 72 | string storage_id_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 75 | TEST_F(ServiceTest, Constructor) { |
| 76 | EXPECT_TRUE(service_->save_credentials_); |
| 77 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 78 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 79 | EXPECT_FALSE(service_->has_ever_connected()); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 82 | TEST_F(ServiceTest, GetProperties) { |
| 83 | map<string, ::DBus::Variant> props; |
| 84 | Error error(Error::kInvalidProperty, ""); |
| 85 | { |
| 86 | ::DBus::Error dbus_error; |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 87 | string expected("true"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 88 | service_->mutable_store()->SetStringProperty(flimflam::kCheckPortalProperty, |
| 89 | expected, |
| 90 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 91 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 92 | ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end()); |
| 93 | EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(), |
| 94 | expected); |
| 95 | } |
| 96 | { |
| 97 | ::DBus::Error dbus_error; |
| 98 | bool expected = true; |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 99 | service_->set_favorite(true); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 100 | service_->mutable_store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 101 | expected, |
| 102 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 103 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 104 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 105 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 106 | expected); |
| 107 | } |
| 108 | { |
| 109 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 110 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 111 | ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end()); |
| 112 | EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false); |
| 113 | } |
| 114 | { |
| 115 | ::DBus::Error dbus_error; |
| 116 | int32 expected = 127; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 117 | service_->mutable_store()->SetInt32Property(flimflam::kPriorityProperty, |
| 118 | expected, |
| 119 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 120 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 121 | ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end()); |
| 122 | EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(), |
| 123 | expected); |
| 124 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 125 | { |
| 126 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 127 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 128 | ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end()); |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 129 | EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_path(), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 130 | string(ServiceUnderTest::kRpcId)); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 131 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 132 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 133 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 134 | TEST_F(ServiceTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 135 | { |
| 136 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 137 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 138 | flimflam::kSaveCredentialsProperty, |
| 139 | PropertyStoreTest::kBoolV, |
| 140 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 141 | } |
| 142 | { |
| 143 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 144 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 145 | flimflam::kPriorityProperty, |
| 146 | PropertyStoreTest::kInt32V, |
| 147 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 148 | } |
| 149 | { |
| 150 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 151 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 152 | flimflam::kEAPEAPProperty, |
| 153 | PropertyStoreTest::kStringV, |
| 154 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 155 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 156 | // Ensure that an attempt to write a R/O property returns InvalidArgs error. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 157 | { |
| 158 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 159 | EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 160 | flimflam::kFavoriteProperty, |
| 161 | PropertyStoreTest::kBoolV, |
| 162 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 163 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 164 | } |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 165 | { |
| 166 | ::DBus::Error error; |
| 167 | service_->set_favorite(true); |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 168 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 169 | flimflam::kAutoConnectProperty, |
| 170 | PropertyStoreTest::kBoolV, |
| 171 | &error)); |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 172 | } |
| 173 | { |
| 174 | ::DBus::Error error; |
| 175 | service_->set_favorite(false); |
Wade Guthrie | 4f28e8b | 2012-04-11 10:52:07 -0700 | [diff] [blame] | 176 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 177 | flimflam::kAutoConnectProperty, |
| 178 | PropertyStoreTest::kBoolV, |
| 179 | &error)); |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 180 | } |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 181 | // Ensure that we can perform a trivial set of the Name property (to its |
| 182 | // current value) but an attempt to set the property to a different value |
| 183 | // fails. |
| 184 | { |
| 185 | ::DBus::Error error; |
| 186 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 187 | flimflam::kNameProperty, |
| 188 | DBusAdaptor::StringToVariant( |
| 189 | service_->friendly_name()), |
| 190 | &error)); |
| 191 | } |
| 192 | { |
| 193 | ::DBus::Error error; |
| 194 | EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 195 | flimflam::kNameProperty, |
| 196 | PropertyStoreTest::kStringV, |
| 197 | &error)); |
| 198 | EXPECT_EQ(invalid_args(), error.name()); |
| 199 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 202 | TEST_F(ServiceTest, Load) { |
| 203 | NiceMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 204 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 205 | EXPECT_CALL(storage, GetString(storage_id_, _, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 206 | .Times(AtLeast(1)) |
| 207 | .WillRepeatedly(Return(true)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 208 | EXPECT_TRUE(service_->Load(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | TEST_F(ServiceTest, LoadFail) { |
| 212 | StrictMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 213 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(false)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 214 | EXPECT_FALSE(service_->Load(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | TEST_F(ServiceTest, SaveString) { |
| 218 | MockStore storage; |
| 219 | static const char kKey[] = "test-key"; |
| 220 | static const char kData[] = "test-data"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 221 | EXPECT_CALL(storage, SetString(storage_id_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 222 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 223 | service_->SaveString(&storage, storage_id_, kKey, kData, false, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(ServiceTest, SaveStringCrypted) { |
| 227 | MockStore storage; |
| 228 | static const char kKey[] = "test-key"; |
| 229 | static const char kData[] = "test-data"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 230 | EXPECT_CALL(storage, SetCryptedString(storage_id_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 231 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 232 | service_->SaveString(&storage, storage_id_, kKey, kData, true, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | TEST_F(ServiceTest, SaveStringDontSave) { |
| 236 | MockStore storage; |
| 237 | static const char kKey[] = "test-key"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 238 | EXPECT_CALL(storage, DeleteKey(storage_id_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 239 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 240 | service_->SaveString(&storage, storage_id_, kKey, "data", false, false); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | TEST_F(ServiceTest, SaveStringEmpty) { |
| 244 | MockStore storage; |
| 245 | static const char kKey[] = "test-key"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 246 | EXPECT_CALL(storage, DeleteKey(storage_id_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 247 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 248 | service_->SaveString(&storage, storage_id_, kKey, "", true, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | TEST_F(ServiceTest, Save) { |
| 252 | NiceMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 253 | EXPECT_CALL(storage, SetString(storage_id_, _, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 254 | .Times(AtLeast(1)) |
| 255 | .WillRepeatedly(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 256 | EXPECT_CALL(storage, DeleteKey(storage_id_, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 257 | .Times(AtLeast(1)) |
| 258 | .WillRepeatedly(Return(true)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 259 | EXPECT_TRUE(service_->Save(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 262 | TEST_F(ServiceTest, Unload) { |
| 263 | NiceMock<MockStore> storage; |
| 264 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 265 | static const string string_value("value"); |
| 266 | EXPECT_CALL(storage, GetString(storage_id_, _, _)) |
| 267 | .Times(AtLeast(1)) |
| 268 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(string_value), Return(true))); |
| 269 | ASSERT_TRUE(service_->Load(&storage)); |
| 270 | // TODO(pstew): A single string property in the service is tested as |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 271 | // a sentinel that properties are being set and reset at the right times. |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 272 | // However, since property load/store is essentially a manual process, |
| 273 | // it is error prone and should either be exhaustively unit-tested or |
| 274 | // a generic framework for registering loaded/stored properties should |
| 275 | // be created. crosbug.com/24859 |
| 276 | EXPECT_EQ(string_value, service_->ui_data_); |
| 277 | service_->Unload(); |
| 278 | EXPECT_EQ(string(""), service_->ui_data_); |
| 279 | } |
| 280 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 281 | TEST_F(ServiceTest, State) { |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 282 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 283 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
| 284 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 285 | ServiceRefPtr service_ref(service_); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 286 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 287 | EXPECT_CALL(*dynamic_cast<ServiceMockAdaptor *>(service_->adaptor_.get()), |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 288 | EmitStringChanged(flimflam::kStateProperty, _)).Times(7); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 289 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 290 | service_->SetState(Service::kStateConnected); |
| 291 | // A second state change shouldn't cause another update |
| 292 | service_->SetState(Service::kStateConnected); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 293 | EXPECT_EQ(Service::kStateConnected, service_->state()); |
| 294 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 295 | EXPECT_TRUE(service_->has_ever_connected_); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 296 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 297 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 298 | service_->SetState(Service::kStateDisconnected); |
| 299 | |
| 300 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 301 | service_->SetFailure(Service::kFailureOutOfRange); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 302 | EXPECT_TRUE(service_->IsFailed()); |
| 303 | EXPECT_GT(service_->failed_time_, 0); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 304 | EXPECT_EQ(Service::kStateFailure, service_->state()); |
| 305 | EXPECT_EQ(Service::kFailureOutOfRange, service_->failure()); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 306 | |
| 307 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 308 | service_->SetState(Service::kStateConnected); |
| 309 | EXPECT_FALSE(service_->IsFailed()); |
| 310 | EXPECT_EQ(service_->failed_time_, 0); |
| 311 | |
| 312 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 313 | service_->SetFailureSilent(Service::kFailurePinMissing); |
| 314 | EXPECT_TRUE(service_->IsFailed()); |
| 315 | EXPECT_GT(service_->failed_time_, 0); |
| 316 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
| 317 | EXPECT_EQ(Service::kFailurePinMissing, service_->failure()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 318 | |
| 319 | // If the Service has a Profile, the profile should be saved when |
| 320 | // the service enters kStateConnected. (The case where the service |
| 321 | // doesn't have a profile is tested above.) |
| 322 | MockProfileRefPtr mock_profile( |
| 323 | new MockProfile(control_interface(), &mock_manager_)); |
| 324 | NiceMock<MockStore> storage; |
| 325 | service_->set_profile(mock_profile); |
| 326 | service_->has_ever_connected_ = false; |
| 327 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 328 | EXPECT_CALL(*mock_profile, GetConstStorage()) |
| 329 | .WillOnce(Return(&storage)); |
| 330 | EXPECT_CALL(*mock_profile, UpdateService(service_ref)); |
| 331 | service_->SetState(Service::kStateConnected); |
| 332 | EXPECT_TRUE(service_->has_ever_connected_); |
| 333 | service_->set_profile(NULL); // Break reference cycle. |
| 334 | |
| 335 | // Similar to the above, but emulate an emphemeral profile, which |
| 336 | // has no storage. We can't update the service in the profile, but |
| 337 | // we should not crash. |
| 338 | service_->state_ = Service::kStateIdle; // Skips state change logic. |
| 339 | service_->set_profile(mock_profile); |
| 340 | service_->has_ever_connected_ = false; |
| 341 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 342 | EXPECT_CALL(*mock_profile, GetConstStorage()). |
| 343 | WillOnce(Return(static_cast<StoreInterface *>(NULL))); |
| 344 | service_->SetState(Service::kStateConnected); |
| 345 | EXPECT_TRUE(service_->has_ever_connected_); |
| 346 | service_->set_profile(NULL); // Break reference cycle. |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 349 | TEST_F(ServiceTest, ActivateCellularModem) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 350 | ResultCallback callback = |
| 351 | Bind(&ServiceTest::TestCallback, Unretained(this)); |
| 352 | EXPECT_CALL(*this, TestCallback(_)).Times(0); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 353 | Error error; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 354 | service_->ActivateCellularModem("Carrier", &error, callback); |
| 355 | EXPECT_TRUE(error.IsFailure()); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 356 | } |
| 357 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 358 | TEST_F(ServiceTest, MakeFavorite) { |
| 359 | EXPECT_FALSE(service_->favorite()); |
| 360 | EXPECT_FALSE(service_->auto_connect()); |
| 361 | |
| 362 | service_->MakeFavorite(); |
| 363 | EXPECT_TRUE(service_->favorite()); |
| 364 | EXPECT_TRUE(service_->auto_connect()); |
| 365 | } |
| 366 | |
| 367 | TEST_F(ServiceTest, ReMakeFavorite) { |
| 368 | service_->MakeFavorite(); |
| 369 | EXPECT_TRUE(service_->favorite()); |
| 370 | EXPECT_TRUE(service_->auto_connect()); |
| 371 | |
| 372 | service_->set_auto_connect(false); |
| 373 | service_->MakeFavorite(); |
| 374 | EXPECT_TRUE(service_->favorite()); |
| 375 | EXPECT_FALSE(service_->auto_connect()); |
| 376 | } |
| 377 | |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 378 | TEST_F(ServiceTest, IsAutoConnectable) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 379 | const char *reason; |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 380 | service_->set_connectable(true); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 381 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 382 | |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 383 | // We should not auto-connect to a Service that a user has |
| 384 | // deliberately disconnected. |
| 385 | Error error; |
| 386 | service_->Disconnect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 387 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 388 | EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 389 | |
| 390 | // But if the Service is reloaded, it is eligible for auto-connect |
| 391 | // again. |
| 392 | NiceMock<MockStore> storage; |
| 393 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 394 | EXPECT_TRUE(service_->Load(&storage)); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 395 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 396 | |
| 397 | // A deliberate Connect should also re-enable auto-connect. |
| 398 | service_->Disconnect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 399 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 400 | service_->Connect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 401 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 402 | |
| 403 | // TODO(quiche): After we have resume handling in place, test that |
| 404 | // we re-enable auto-connect on resume. crosbug.com/25213 |
| 405 | |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 406 | service_->SetState(Service::kStateConnected); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 407 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 408 | EXPECT_STREQ(Service::kAutoConnConnected, reason); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 409 | |
| 410 | service_->SetState(Service::kStateAssociating); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 411 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 412 | EXPECT_STREQ(Service::kAutoConnConnecting, reason); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 415 | TEST_F(ServiceTest, ConfigureBadProperty) { |
| 416 | KeyValueStore args; |
| 417 | args.SetString("XXXInvalid", "Value"); |
| 418 | Error error; |
| 419 | service_->Configure(args, &error); |
| 420 | EXPECT_FALSE(error.IsSuccess()); |
| 421 | } |
| 422 | |
| 423 | TEST_F(ServiceTest, ConfigureBoolProperty) { |
| 424 | service_->MakeFavorite(); |
| 425 | service_->set_auto_connect(false); |
| 426 | ASSERT_FALSE(service_->auto_connect()); |
| 427 | KeyValueStore args; |
| 428 | args.SetBool(flimflam::kAutoConnectProperty, true); |
| 429 | Error error; |
| 430 | service_->Configure(args, &error); |
| 431 | EXPECT_TRUE(error.IsSuccess()); |
| 432 | EXPECT_TRUE(service_->auto_connect()); |
| 433 | } |
| 434 | |
| 435 | TEST_F(ServiceTest, ConfigureStringProperty) { |
| 436 | const string kEAPManagement0 = "management_zero"; |
| 437 | const string kEAPManagement1 = "management_one"; |
| 438 | service_->SetEAPKeyManagement(kEAPManagement0); |
| 439 | ASSERT_EQ(kEAPManagement0, service_->GetEAPKeyManagement()); |
| 440 | KeyValueStore args; |
| 441 | args.SetString(flimflam::kEapKeyMgmtProperty, kEAPManagement1); |
| 442 | Error error; |
| 443 | service_->Configure(args, &error); |
| 444 | EXPECT_TRUE(error.IsSuccess()); |
| 445 | EXPECT_EQ(kEAPManagement1, service_->GetEAPKeyManagement()); |
| 446 | } |
| 447 | |
| 448 | TEST_F(ServiceTest, ConfigureIgnoredProperty) { |
| 449 | service_->MakeFavorite(); |
| 450 | service_->set_auto_connect(false); |
| 451 | ASSERT_FALSE(service_->auto_connect()); |
| 452 | KeyValueStore args; |
| 453 | args.SetBool(flimflam::kAutoConnectProperty, true); |
| 454 | Error error; |
| 455 | service_->IgnoreParameterForConfigure(flimflam::kAutoConnectProperty); |
| 456 | service_->Configure(args, &error); |
| 457 | EXPECT_TRUE(error.IsSuccess()); |
| 458 | EXPECT_FALSE(service_->auto_connect()); |
| 459 | } |
| 460 | |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 461 | TEST_F(ServiceTest, IsRemembered) { |
| 462 | ServiceConstRefPtr service_ref(service_); |
| 463 | service_->set_profile(NULL); |
| 464 | EXPECT_CALL(mock_manager_, IsServiceEphemeral(_)).Times(0); |
| 465 | EXPECT_FALSE(service_->IsRemembered()); |
| 466 | |
| 467 | scoped_refptr<MockProfile> profile( |
| 468 | new StrictMock<MockProfile>(control_interface(), manager())); |
| 469 | service_->set_profile(profile); |
| 470 | EXPECT_CALL(mock_manager_, IsServiceEphemeral(service_ref)) |
| 471 | .WillOnce(Return(true)) |
| 472 | .WillOnce(Return(false)); |
| 473 | EXPECT_FALSE(service_->IsRemembered()); |
| 474 | EXPECT_TRUE(service_->IsRemembered()); |
| 475 | } |
| 476 | |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 477 | TEST_F(ServiceTest, OnPropertyChanged) { |
| 478 | scoped_refptr<MockProfile> profile( |
| 479 | new StrictMock<MockProfile>(control_interface(), manager())); |
| 480 | service_->set_profile(NULL); |
| 481 | // Expect no crash. |
| 482 | service_->OnPropertyChanged(""); |
| 483 | |
| 484 | // Expect no call to Update if the profile has no storage. |
| 485 | service_->set_profile(profile); |
| 486 | EXPECT_CALL(*profile, UpdateService(_)).Times(0); |
| 487 | EXPECT_CALL(*profile, GetConstStorage()) |
| 488 | .WillOnce(Return(reinterpret_cast<StoreInterface *>(NULL))); |
| 489 | service_->OnPropertyChanged(""); |
| 490 | |
| 491 | // Expect call to Update if the profile has storage. |
| 492 | EXPECT_CALL(*profile, UpdateService(_)).Times(1); |
| 493 | NiceMock<MockStore> storage; |
| 494 | EXPECT_CALL(*profile, GetConstStorage()).WillOnce(Return(&storage)); |
| 495 | service_->OnPropertyChanged(""); |
| 496 | } |
| 497 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 498 | |
| 499 | TEST_F(ServiceTest, RecheckPortal) { |
| 500 | ServiceRefPtr service_ref(service_); |
| 501 | service_->state_ = Service::kStateIdle; |
| 502 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0); |
| 503 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 504 | |
| 505 | service_->state_ = Service::kStatePortal; |
| 506 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 507 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 508 | |
| 509 | service_->state_ = Service::kStateConnected; |
| 510 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 511 | service_->OnPropertyChanged(flimflam::kProxyConfigProperty); |
| 512 | |
| 513 | service_->state_ = Service::kStateOnline; |
| 514 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 515 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 516 | |
| 517 | service_->state_ = Service::kStatePortal; |
| 518 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0); |
| 519 | service_->OnPropertyChanged(flimflam::kEAPKeyIDProperty); |
| 520 | } |
| 521 | |
| 522 | TEST_F(ServiceTest, SetCheckPortal) { |
| 523 | ServiceRefPtr service_ref(service_); |
| 524 | { |
| 525 | Error error; |
| 526 | service_->SetCheckPortal("false", &error); |
| 527 | EXPECT_TRUE(error.IsSuccess()); |
| 528 | EXPECT_EQ(Service::kCheckPortalFalse, service_->check_portal_); |
| 529 | } |
| 530 | { |
| 531 | Error error; |
| 532 | service_->SetCheckPortal("true", &error); |
| 533 | EXPECT_TRUE(error.IsSuccess()); |
| 534 | EXPECT_EQ(Service::kCheckPortalTrue, service_->check_portal_); |
| 535 | } |
| 536 | { |
| 537 | Error error; |
| 538 | service_->SetCheckPortal("auto", &error); |
| 539 | EXPECT_TRUE(error.IsSuccess()); |
| 540 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 541 | } |
| 542 | { |
| 543 | Error error; |
| 544 | service_->SetCheckPortal("xxx", &error); |
| 545 | EXPECT_FALSE(error.IsSuccess()); |
| 546 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 547 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 548 | } |
| 549 | } |
| 550 | |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 551 | // Make sure a property is registered as a write only property |
| 552 | // by reading and comparing all string properties returned on the store. |
| 553 | // Subtle: We need to convert the test argument back and forth between |
| 554 | // string and ::DBus::Variant because this is the parameter type that |
| 555 | // our supeclass (PropertyStoreTest) is declared with. |
| 556 | class ReadOnlyServicePropertyTest : public ServiceTest {}; |
| 557 | TEST_P(ReadOnlyServicePropertyTest, PropertyWriteOnly) { |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 558 | string property(GetParam().reader().get_string()); |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 559 | PropertyStoreInspector inspector(&service_->store()); |
| 560 | EXPECT_FALSE(inspector.ContainsStringProperty(property)); |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | INSTANTIATE_TEST_CASE_P( |
| 564 | ReadOnlyServicePropertyTestInstance, |
| 565 | ReadOnlyServicePropertyTest, |
| 566 | Values( |
| 567 | DBusAdaptor::StringToVariant(flimflam::kEapPrivateKeyPasswordProperty), |
| 568 | DBusAdaptor::StringToVariant(flimflam::kEapPasswordProperty))); |
| 569 | |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 570 | |
| 571 | TEST_F(ServiceTest, GetIPConfigRpcIdentifier) { |
| 572 | { |
| 573 | Error error; |
| 574 | EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error)); |
| 575 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 576 | } |
| 577 | |
| 578 | scoped_ptr<MockDeviceInfo> mock_device_info( |
| 579 | new NiceMock<MockDeviceInfo>(control_interface(), dispatcher(), metrics(), |
| 580 | &mock_manager_)); |
| 581 | scoped_refptr<MockConnection> mock_connection( |
| 582 | new NiceMock<MockConnection>(mock_device_info.get())); |
| 583 | |
| 584 | service_->connection_ = mock_connection; |
| 585 | |
| 586 | { |
| 587 | Error error; |
| 588 | const string empty_string; |
| 589 | EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier()) |
| 590 | .WillOnce(ReturnRef(empty_string)); |
| 591 | EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error)); |
| 592 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 593 | } |
| 594 | |
| 595 | { |
| 596 | Error error; |
| 597 | const string nonempty_string("/ipconfig/path"); |
| 598 | EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier()) |
| 599 | .WillOnce(ReturnRef(nonempty_string)); |
| 600 | EXPECT_EQ(nonempty_string, service_->GetIPConfigRpcIdentifier(&error)); |
| 601 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 602 | } |
| 603 | |
| 604 | // Assure orderly destruction of the Connection before DeviceInfo. |
| 605 | service_->connection_ = NULL; |
| 606 | mock_connection = NULL; |
| 607 | mock_device_info.reset(); |
| 608 | } |
| 609 | |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 610 | class ServiceWithMockSetEap : public ServiceUnderTest { |
| 611 | public: |
| 612 | ServiceWithMockSetEap(ControlInterface *control_interface, |
| 613 | EventDispatcher *dispatcher, |
| 614 | Metrics *metrics, |
| 615 | Manager *manager) |
| 616 | : ServiceUnderTest(control_interface, dispatcher, metrics, manager), |
| 617 | is_8021x_(false) {} |
| 618 | MOCK_METHOD1(set_eap, void(const EapCredentials &eap)); |
| 619 | virtual bool Is8021x() const { return is_8021x_; } |
| 620 | void set_is_8021x(bool is_8021x) { is_8021x_ = is_8021x; } |
| 621 | |
| 622 | private: |
| 623 | bool is_8021x_; |
| 624 | }; |
| 625 | |
| 626 | TEST_F(ServiceTest, SetEAPCredentialsOverRPC) { |
| 627 | scoped_refptr<ServiceWithMockSetEap> service( |
| 628 | new ServiceWithMockSetEap(control_interface(), |
| 629 | dispatcher(), |
| 630 | metrics(), |
| 631 | &mock_manager_)); |
| 632 | string eap_credential_properties[] = { |
| 633 | flimflam::kEAPCertIDProperty, |
| 634 | flimflam::kEAPClientCertProperty, |
| 635 | flimflam::kEAPKeyIDProperty, |
| 636 | flimflam::kEAPPINProperty, |
| 637 | flimflam::kEapCaCertIDProperty, |
| 638 | flimflam::kEapIdentityProperty, |
| 639 | flimflam::kEapPasswordProperty, |
| 640 | flimflam::kEapPrivateKeyProperty |
| 641 | }; |
| 642 | string eap_non_credential_properties[] = { |
| 643 | flimflam::kEAPEAPProperty, |
| 644 | flimflam::kEapPhase2AuthProperty, |
| 645 | flimflam::kEapAnonymousIdentityProperty, |
| 646 | flimflam::kEapPrivateKeyPasswordProperty, |
| 647 | flimflam::kEapKeyMgmtProperty, |
| 648 | flimflam::kEapCaCertNssProperty, |
| 649 | flimflam::kEapUseSystemCAsProperty |
| 650 | }; |
| 651 | // While this is not an 802.1x-based service, none of these property |
| 652 | // changes should cause a call to set_eap(). |
| 653 | EXPECT_CALL(*service, set_eap(_)).Times(0); |
| 654 | for (size_t i = 0; i < arraysize(eap_credential_properties); ++i) |
| 655 | service->OnPropertyChanged(eap_credential_properties[i]); |
| 656 | for (size_t i = 0; i < arraysize(eap_non_credential_properties); ++i) |
| 657 | service->OnPropertyChanged(eap_non_credential_properties[i]); |
| 658 | |
| 659 | service->set_is_8021x(true); |
| 660 | |
| 661 | // When this is an 802.1x-based service, set_eap should be called for |
| 662 | // all credential-carrying properties. |
| 663 | for (size_t i = 0; i < arraysize(eap_credential_properties); ++i) { |
| 664 | EXPECT_CALL(*service, set_eap(_)).Times(1); |
| 665 | service->OnPropertyChanged(eap_credential_properties[i]); |
| 666 | Mock::VerifyAndClearExpectations(service.get()); |
| 667 | } |
| 668 | EXPECT_CALL(*service, set_eap(_)).Times(0); |
| 669 | for (size_t i = 0; i < arraysize(eap_non_credential_properties); ++i) |
| 670 | service->OnPropertyChanged(eap_non_credential_properties[i]); |
| 671 | } |
| 672 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 673 | } // namespace shill |