Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -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 | |
| 5 | #include "shill/service.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <dbus-c++/dbus.h> |
| 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | #include <gmock/gmock.h> |
| 15 | |
| 16 | #include "shill/dbus_adaptor.h" |
| 17 | #include "shill/ethernet_service.h" |
| 18 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 19 | #include "shill/mock_adaptors.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #include "shill/mock_control.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 21 | #include "shill/mock_profile.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/mock_service.h" |
| 23 | #include "shill/property_store_unittest.h" |
| 24 | #include "shill/shill_event.h" |
| 25 | |
| 26 | using std::map; |
| 27 | using std::string; |
| 28 | using std::vector; |
| 29 | using ::testing::_; |
| 30 | using ::testing::NiceMock; |
| 31 | using ::testing::Return; |
| 32 | using ::testing::Test; |
| 33 | |
| 34 | namespace shill { |
| 35 | |
| 36 | class ServiceTest : public PropertyStoreTest { |
| 37 | public: |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 38 | static const char kMockServiceName[]; |
| 39 | static const char kMockDeviceRpcId[]; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 40 | static const char kProfileName[]; |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 41 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 42 | ServiceTest() |
| 43 | : service_(new MockService(&control_interface_, |
| 44 | &dispatcher_, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 45 | new MockProfile(&control_interface_, &glib_), |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 46 | kMockServiceName)) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 49 | virtual ~ServiceTest() {} |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 50 | |
| 51 | protected: |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 52 | scoped_refptr<MockService> service_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 55 | const char ServiceTest::kMockServiceName[] = "mock-service"; |
| 56 | |
| 57 | const char ServiceTest::kMockDeviceRpcId[] = "mock-device-rpc"; |
| 58 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 59 | const char ServiceTest::kProfileName[] = "profile"; |
| 60 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 61 | TEST_F(ServiceTest, GetProperties) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 62 | EXPECT_CALL(*service_.get(), CalculateState()).WillRepeatedly(Return("")); |
| 63 | EXPECT_CALL(*service_.get(), GetDeviceRpcId()) |
| 64 | .WillRepeatedly(Return(ServiceTest::kMockDeviceRpcId)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 65 | map<string, ::DBus::Variant> props; |
| 66 | Error error(Error::kInvalidProperty, ""); |
| 67 | { |
| 68 | ::DBus::Error dbus_error; |
| 69 | string expected("portal_list"); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 70 | service_->store()->SetStringProperty(flimflam::kCheckPortalProperty, |
| 71 | expected, |
| 72 | &error); |
| 73 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 74 | ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end()); |
| 75 | EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(), |
| 76 | expected); |
| 77 | } |
| 78 | { |
| 79 | ::DBus::Error dbus_error; |
| 80 | bool expected = true; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 81 | service_->store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 82 | expected, |
| 83 | &error); |
| 84 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 85 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 86 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 87 | expected); |
| 88 | } |
| 89 | { |
| 90 | ::DBus::Error dbus_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::kConnectableProperty) == props.end()); |
| 93 | EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false); |
| 94 | } |
| 95 | { |
| 96 | ::DBus::Error dbus_error; |
| 97 | int32 expected = 127; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 98 | service_->store()->SetInt32Property(flimflam::kPriorityProperty, |
| 99 | expected, |
| 100 | &error); |
| 101 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 102 | ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end()); |
| 103 | EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(), |
| 104 | expected); |
| 105 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 106 | { |
| 107 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 108 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 109 | ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end()); |
| 110 | EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_string(), |
| 111 | string(ServiceTest::kMockDeviceRpcId)); |
| 112 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 113 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 114 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 115 | TEST_F(ServiceTest, Dispatch) { |
| 116 | { |
| 117 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 118 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 119 | flimflam::kSaveCredentialsProperty, |
| 120 | PropertyStoreTest::kBoolV, |
| 121 | &error)); |
| 122 | } |
| 123 | { |
| 124 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 125 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 126 | flimflam::kPriorityProperty, |
| 127 | PropertyStoreTest::kInt32V, |
| 128 | &error)); |
| 129 | } |
| 130 | { |
| 131 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 132 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 133 | flimflam::kEAPEAPProperty, |
| 134 | PropertyStoreTest::kStringV, |
| 135 | &error)); |
| 136 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 137 | // 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] | 138 | { |
| 139 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 140 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 141 | flimflam::kFavoriteProperty, |
| 142 | PropertyStoreTest::kBoolV, |
| 143 | &error)); |
| 144 | EXPECT_EQ(invalid_args_, error.name()); |
| 145 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame^] | 148 | TEST_F(ServiceTest, MoveService) { |
| 149 | // I want to ensure that the Profiles are managing this Service object |
| 150 | // lifetime properly, so I can't hold a ref to it here. |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 151 | ProfileRefPtr profile(new Profile(&control_interface_, &glib_)); |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame^] | 152 | { |
| 153 | ServiceRefPtr s2( |
| 154 | new MockService(&control_interface_, |
| 155 | &dispatcher_, |
| 156 | new MockProfile(&control_interface_, &glib_), |
| 157 | kMockServiceName)); |
| 158 | profile->services_[kMockServiceName] = s2; |
| 159 | } |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 160 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame^] | 161 | // Now, move the service to another profile. |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 162 | ProfileRefPtr profile2(new Profile(&control_interface_, &glib_)); |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame^] | 163 | map<string, ServiceRefPtr>::iterator it = |
| 164 | profile->services_.find(kMockServiceName); |
| 165 | ASSERT_TRUE(it != profile->services_.end()); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 166 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame^] | 167 | profile2->AdoptService(it->first, it->second); |
| 168 | // Force destruction of the original Profile, to ensure that the Service |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 169 | // is kept alive and populated with data. |
| 170 | profile = NULL; |
| 171 | { |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame^] | 172 | map<string, ServiceRefPtr>::iterator it = |
| 173 | profile2->services_.find(kMockServiceName); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 174 | Error error(Error::kInvalidProperty, ""); |
| 175 | ::DBus::Error dbus_error; |
| 176 | map<string, ::DBus::Variant> props; |
| 177 | bool expected = true; |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame^] | 178 | it->second->store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 179 | expected, |
| 180 | &error); |
| 181 | DBusAdaptor::GetProperties(it->second->store(), &props, &dbus_error); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 182 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 183 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 184 | expected); |
| 185 | } |
| 186 | } |
| 187 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 188 | } // namespace shill |