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