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 | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 21 | #include "shill/mock_service.h" |
| 22 | #include "shill/property_store_unittest.h" |
| 23 | #include "shill/shill_event.h" |
| 24 | |
| 25 | using std::map; |
| 26 | using std::string; |
| 27 | using std::vector; |
| 28 | using ::testing::_; |
| 29 | using ::testing::NiceMock; |
| 30 | using ::testing::Return; |
| 31 | using ::testing::Test; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 32 | using ::testing::Values; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 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[]; |
| 40 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 41 | ServiceTest() |
| 42 | : service_(new MockService(&control_interface_, |
| 43 | &dispatcher_, |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 44 | kMockServiceName)) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 47 | virtual ~ServiceTest() {} |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 48 | |
| 49 | protected: |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 50 | scoped_refptr<MockService> service_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 53 | const char ServiceTest::kMockServiceName[] = "mock-service"; |
| 54 | |
| 55 | const char ServiceTest::kMockDeviceRpcId[] = "mock-device-rpc"; |
| 56 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 57 | TEST_F(ServiceTest, GetProperties) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 58 | EXPECT_CALL(*service_.get(), CalculateState()).WillRepeatedly(Return("")); |
| 59 | EXPECT_CALL(*service_.get(), GetDeviceRpcId()) |
| 60 | .WillRepeatedly(Return(ServiceTest::kMockDeviceRpcId)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 61 | map<string, ::DBus::Variant> props; |
| 62 | Error error(Error::kInvalidProperty, ""); |
| 63 | { |
| 64 | ::DBus::Error dbus_error; |
| 65 | string expected("portal_list"); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 66 | service_->store()->SetStringProperty(flimflam::kCheckPortalProperty, |
| 67 | expected, |
| 68 | &error); |
| 69 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 70 | ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end()); |
| 71 | EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(), |
| 72 | expected); |
| 73 | } |
| 74 | { |
| 75 | ::DBus::Error dbus_error; |
| 76 | bool expected = true; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 77 | service_->store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 78 | expected, |
| 79 | &error); |
| 80 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 81 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 82 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 83 | expected); |
| 84 | } |
| 85 | { |
| 86 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 87 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 88 | ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end()); |
| 89 | EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false); |
| 90 | } |
| 91 | { |
| 92 | ::DBus::Error dbus_error; |
| 93 | int32 expected = 127; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 94 | service_->store()->SetInt32Property(flimflam::kPriorityProperty, |
| 95 | expected, |
| 96 | &error); |
| 97 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 98 | ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end()); |
| 99 | EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(), |
| 100 | expected); |
| 101 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 102 | { |
| 103 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 104 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 105 | ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end()); |
| 106 | EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_string(), |
| 107 | string(ServiceTest::kMockDeviceRpcId)); |
| 108 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 109 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 110 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 111 | TEST_F(ServiceTest, Dispatch) { |
| 112 | { |
| 113 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 114 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 115 | flimflam::kSaveCredentialsProperty, |
| 116 | PropertyStoreTest::kBoolV, |
| 117 | &error)); |
| 118 | } |
| 119 | { |
| 120 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 121 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 122 | flimflam::kPriorityProperty, |
| 123 | PropertyStoreTest::kInt32V, |
| 124 | &error)); |
| 125 | } |
| 126 | { |
| 127 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 128 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 129 | flimflam::kEAPEAPProperty, |
| 130 | PropertyStoreTest::kStringV, |
| 131 | &error)); |
| 132 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 133 | // 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] | 134 | { |
| 135 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 136 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 137 | flimflam::kFavoriteProperty, |
| 138 | PropertyStoreTest::kBoolV, |
| 139 | &error)); |
| 140 | EXPECT_EQ(invalid_args_, error.name()); |
| 141 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | TEST_P(ServiceTest, TestProperty) { |
| 145 | // Ensure that an attempt to write unknown properties returns InvalidProperty. |
| 146 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame^] | 147 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 148 | "", |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 149 | GetParam(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 150 | &error)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 151 | EXPECT_EQ(invalid_prop_, error.name()); |
| 152 | } |
| 153 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 154 | INSTANTIATE_TEST_CASE_P( |
| 155 | ServiceTestInstance, |
| 156 | ServiceTest, |
| 157 | Values(PropertyStoreTest::kBoolV, |
| 158 | PropertyStoreTest::kByteV, |
| 159 | PropertyStoreTest::kStringV, |
| 160 | PropertyStoreTest::kInt16V, |
| 161 | PropertyStoreTest::kInt32V, |
| 162 | PropertyStoreTest::kUint16V, |
| 163 | PropertyStoreTest::kUint32V, |
| 164 | PropertyStoreTest::kStringsV, |
| 165 | PropertyStoreTest::kStringmapV, |
| 166 | PropertyStoreTest::kStringmapsV)); |
| 167 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 168 | } // namespace shill |