Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -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 | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 5 | #include "shill/dbus_adaptor.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 6 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 7 | #include <map> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 8 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 9 | #include <vector> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 10 | |
| 11 | #include <dbus-c++/dbus.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | #include <gmock/gmock.h> |
| 14 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame^] | 15 | #include "shill/entry.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 16 | #include "shill/manager.h" |
| 17 | #include "shill/mock_control.h" |
| 18 | #include "shill/mock_device.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame^] | 19 | #include "shill/mock_glib.h" |
| 20 | #include "shill/mock_profile.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 21 | #include "shill/mock_property_store.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 22 | #include "shill/mock_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 23 | #include "shill/property_store_unittest.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 24 | #include "shill/shill_event.h" |
| 25 | |
| 26 | using std::map; |
| 27 | using std::string; |
| 28 | using std::vector; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 29 | using ::testing::_; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 30 | using ::testing::Return; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 31 | using ::testing::StrEq; |
| 32 | using ::testing::Test; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 33 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 34 | namespace shill { |
| 35 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | class DBusAdaptorTest : public PropertyStoreTest { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 37 | public: |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 38 | DBusAdaptorTest() |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 39 | : ex_bool_(true), |
| 40 | ex_byte_(0xff), |
| 41 | ex_uint16_(65535), |
| 42 | ex_uint32_(2000000), |
| 43 | ex_int16_(-32768), |
| 44 | ex_int32_(-65536), |
| 45 | ex_string_("something"), |
| 46 | ex_strings_(1, ex_string_), |
| 47 | bool_v_(DBusAdaptor::BoolToVariant(ex_bool_)), |
| 48 | byte_v_(DBusAdaptor::ByteToVariant(ex_byte_)), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 49 | int16_v_(DBusAdaptor::Int16ToVariant(ex_int16_)), |
| 50 | int32_v_(DBusAdaptor::Int32ToVariant(ex_int32_)), |
| 51 | string_v_(DBusAdaptor::StringToVariant(ex_string_)), |
| 52 | strings_v_(DBusAdaptor::StringsToVariant(ex_strings_)), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 53 | uint16_v_(DBusAdaptor::Uint16ToVariant(ex_uint16_)), |
| 54 | uint32_v_(DBusAdaptor::Uint32ToVariant(ex_uint32_)), |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 55 | device_(new MockDevice(&control_interface_, |
| 56 | &dispatcher_, |
| 57 | &manager_, |
| 58 | "mock", |
| 59 | 0)), |
| 60 | service_(new MockService(&control_interface_, |
| 61 | &dispatcher_, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame^] | 62 | new MockProfile(&control_interface_, &glib_), |
| 63 | new Entry("mock"), |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 64 | "mock")) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 65 | ex_stringmap_[ex_string_] = ex_string_; |
| 66 | stringmap_v_ = DBusAdaptor::StringmapToVariant(ex_stringmap_); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 69 | virtual ~DBusAdaptorTest() {} |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 70 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 71 | protected: |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 72 | bool ex_bool_; |
| 73 | uint8 ex_byte_; |
| 74 | uint16 ex_uint16_; |
| 75 | uint32 ex_uint32_; |
| 76 | int16 ex_int16_; |
| 77 | int32 ex_int32_; |
| 78 | string ex_string_; |
| 79 | map<string, string> ex_stringmap_; |
| 80 | vector<map<string, string> > ex_stringmaps_; |
| 81 | vector<string> ex_strings_; |
| 82 | |
| 83 | ::DBus::Variant bool_v_; |
| 84 | ::DBus::Variant byte_v_; |
| 85 | ::DBus::Variant int16_v_; |
| 86 | ::DBus::Variant int32_v_; |
| 87 | ::DBus::Variant string_v_; |
| 88 | ::DBus::Variant stringmap_v_; |
| 89 | ::DBus::Variant stringmaps_v_; |
| 90 | ::DBus::Variant strings_v_; |
| 91 | ::DBus::Variant uint16_v_; |
| 92 | ::DBus::Variant uint32_v_; |
| 93 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 94 | DeviceRefPtr device_; |
| 95 | ServiceRefPtr service_; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | TEST_F(DBusAdaptorTest, Conversions) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 99 | EXPECT_EQ(0, PropertyStoreTest::kBoolV.reader().get_bool()); |
| 100 | EXPECT_EQ(ex_bool_, bool_v_.reader().get_bool()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 101 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 102 | EXPECT_EQ(0, PropertyStoreTest::kByteV.reader().get_byte()); |
| 103 | EXPECT_EQ(ex_byte_, byte_v_.reader().get_byte()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 104 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 105 | EXPECT_EQ(0, PropertyStoreTest::kUint16V.reader().get_uint16()); |
| 106 | EXPECT_EQ(ex_uint16_, uint16_v_.reader().get_uint16()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 107 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 108 | EXPECT_EQ(0, PropertyStoreTest::kInt16V.reader().get_int16()); |
| 109 | EXPECT_EQ(ex_int16_, int16_v_.reader().get_int16()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 110 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 111 | EXPECT_EQ(0, PropertyStoreTest::kUint32V.reader().get_uint32()); |
| 112 | EXPECT_EQ(ex_uint32_, uint32_v_.reader().get_uint32()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 113 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 114 | EXPECT_EQ(0, PropertyStoreTest::kInt32V.reader().get_int32()); |
| 115 | EXPECT_EQ(ex_int32_, int32_v_.reader().get_int32()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 116 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 117 | EXPECT_EQ(string(""), PropertyStoreTest::kStringV.reader().get_string()); |
| 118 | EXPECT_EQ(ex_string_, string_v_.reader().get_string()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 119 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 120 | EXPECT_EQ(ex_stringmap_[ex_string_], |
| 121 | (stringmap_v_.operator map<string, string>()[ex_string_])); |
| 122 | EXPECT_EQ(ex_strings_[0], strings_v_.operator vector<string>()[0]); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST_F(DBusAdaptorTest, Signatures) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 126 | EXPECT_TRUE(DBusAdaptor::IsBool(bool_v_.signature())); |
| 127 | EXPECT_TRUE(DBusAdaptor::IsByte(byte_v_.signature())); |
| 128 | EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v_.signature())); |
| 129 | EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v_.signature())); |
| 130 | EXPECT_TRUE(DBusAdaptor::IsString(string_v_.signature())); |
| 131 | EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v_.signature())); |
| 132 | EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v_.signature())); |
| 133 | EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v_.signature())); |
| 134 | EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v_.signature())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 135 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 136 | EXPECT_FALSE(DBusAdaptor::IsBool(byte_v_.signature())); |
| 137 | EXPECT_FALSE(DBusAdaptor::IsStrings(string_v_.signature())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST_F(DBusAdaptorTest, Dispatch) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 141 | MockPropertyStore store; |
mukesh agrawal | a903e41 | 2011-06-28 17:07:11 -0700 | [diff] [blame] | 142 | ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 143 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 144 | EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(true)); |
| 145 | EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(true)); |
| 146 | EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(true)); |
| 147 | EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(true)); |
| 148 | EXPECT_CALL(store, SetStringProperty("", _, _)) |
| 149 | .WillOnce(Return(true)); |
| 150 | EXPECT_CALL(store, SetStringmapProperty("", _, _)) |
| 151 | .WillOnce(Return(true)); |
| 152 | EXPECT_CALL(store, SetStringsProperty("", _, _)) |
| 153 | .WillOnce(Return(true)); |
| 154 | EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(true)); |
| 155 | EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(true)); |
| 156 | EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(true)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 157 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 158 | string string_path("/false/path"); |
| 159 | ::DBus::Path path(string_path); |
| 160 | ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path); |
| 161 | EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _)) |
| 162 | .WillOnce(Return(true)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 163 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 164 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", bool_v_, &e1)); |
| 165 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", path_v, &e2)); |
| 166 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", string_v_, &e3)); |
| 167 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", strings_v_, &e4)); |
| 168 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", int16_v_, &e5)); |
| 169 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", int32_v_, &e6)); |
| 170 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", uint16_v_, &e7)); |
| 171 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", uint32_v_, &e8)); |
| 172 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", stringmap_v_, &e9)); |
| 173 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", byte_v_, &e10)); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } // namespace shill |