Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -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 | |
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 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 15 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 16 | #include "shill/key_value_store.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 17 | #include "shill/manager.h" |
| 18 | #include "shill/mock_control.h" |
| 19 | #include "shill/mock_device.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 20 | #include "shill/mock_glib.h" |
| 21 | #include "shill/mock_profile.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/mock_property_store.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 23 | #include "shill/mock_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 24 | #include "shill/property_store_unittest.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 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::_; |
Gaurav Shah | b790aa2 | 2012-10-23 12:51:12 -0700 | [diff] [blame] | 30 | using ::testing::ContainerEq; |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 31 | using ::testing::DoAll; |
| 32 | using ::testing::Invoke; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 33 | using ::testing::Return; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 34 | using ::testing::StrEq; |
| 35 | using ::testing::Test; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 36 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 37 | namespace shill { |
| 38 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | class DBusAdaptorTest : public PropertyStoreTest { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 40 | public: |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 41 | DBusAdaptorTest() |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 42 | : ex_bool_(true), |
| 43 | ex_byte_(0xff), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 44 | ex_bytearrays_{ByteArray()}, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 45 | ex_uint16_(65535), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 46 | ex_uint16s_{ex_uint16_}, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 47 | ex_uint32_(2000000), |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 48 | ex_uint64_(8589934591LL), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 49 | ex_int16_(-32768), |
| 50 | ex_int32_(-65536), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 51 | ex_path_("/"), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 52 | ex_paths_{ex_path_}, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 53 | ex_string_("something"), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 54 | ex_stringmap_{{ex_string_, ex_string_}}, |
| 55 | ex_stringmaps_{ex_stringmap_}, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 56 | ex_strings_(1, ex_string_), |
| 57 | bool_v_(DBusAdaptor::BoolToVariant(ex_bool_)), |
| 58 | byte_v_(DBusAdaptor::ByteToVariant(ex_byte_)), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 59 | bytearrays_v_(DBusAdaptor::ByteArraysToVariant(ex_bytearrays_)), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 60 | int16_v_(DBusAdaptor::Int16ToVariant(ex_int16_)), |
| 61 | int32_v_(DBusAdaptor::Int32ToVariant(ex_int32_)), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 62 | path_v_(DBusAdaptor::PathToVariant(ex_path_)), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 63 | paths_v_(DBusAdaptor::PathsToVariant(ex_paths_)), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 64 | string_v_(DBusAdaptor::StringToVariant(ex_string_)), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 65 | stringmap_v_(DBusAdaptor::StringmapToVariant(ex_stringmap_)), |
| 66 | stringmaps_v_(DBusAdaptor::StringmapsToVariant(ex_stringmaps_)), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 67 | strings_v_(DBusAdaptor::StringsToVariant(ex_strings_)), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 68 | uint16_v_(DBusAdaptor::Uint16ToVariant(ex_uint16_)), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 69 | uint16s_v_(DBusAdaptor::Uint16sToVariant(ex_uint16s_)), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 70 | uint32_v_(DBusAdaptor::Uint32ToVariant(ex_uint32_)), |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 71 | uint64_v_(DBusAdaptor::Uint64ToVariant(ex_uint64_)), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 72 | device_(new MockDevice(control_interface(), |
| 73 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 74 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 75 | manager(), |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 76 | "mock", |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 77 | "addr0", |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 78 | 0)), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 79 | service_(new MockService(control_interface(), |
| 80 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 81 | metrics(), |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 82 | manager())) {} |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 83 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 84 | virtual ~DBusAdaptorTest() {} |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 85 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 86 | protected: |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 87 | bool ex_bool_; |
| 88 | uint8 ex_byte_; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 89 | ByteArrays ex_bytearrays_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 90 | uint16 ex_uint16_; |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 91 | vector<uint16> ex_uint16s_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 92 | uint32 ex_uint32_; |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 93 | uint64 ex_uint64_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 94 | int16 ex_int16_; |
| 95 | int32 ex_int32_; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 96 | ::DBus::Path ex_path_; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 97 | vector< ::DBus::Path> ex_paths_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 98 | string ex_string_; |
| 99 | map<string, string> ex_stringmap_; |
| 100 | vector<map<string, string> > ex_stringmaps_; |
| 101 | vector<string> ex_strings_; |
| 102 | |
| 103 | ::DBus::Variant bool_v_; |
| 104 | ::DBus::Variant byte_v_; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 105 | ::DBus::Variant bytearrays_v_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 106 | ::DBus::Variant int16_v_; |
| 107 | ::DBus::Variant int32_v_; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 108 | ::DBus::Variant path_v_; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 109 | ::DBus::Variant paths_v_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 110 | ::DBus::Variant string_v_; |
| 111 | ::DBus::Variant stringmap_v_; |
| 112 | ::DBus::Variant stringmaps_v_; |
| 113 | ::DBus::Variant strings_v_; |
| 114 | ::DBus::Variant uint16_v_; |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 115 | ::DBus::Variant uint16s_v_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 116 | ::DBus::Variant uint32_v_; |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 117 | ::DBus::Variant uint64_v_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 118 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 119 | DeviceRefPtr device_; |
| 120 | ServiceRefPtr service_; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | TEST_F(DBusAdaptorTest, Conversions) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 124 | EXPECT_EQ(0, PropertyStoreTest::kBoolV.reader().get_bool()); |
| 125 | EXPECT_EQ(ex_bool_, bool_v_.reader().get_bool()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 126 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 127 | EXPECT_EQ(0, PropertyStoreTest::kByteV.reader().get_byte()); |
| 128 | EXPECT_EQ(ex_byte_, byte_v_.reader().get_byte()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 129 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 130 | EXPECT_TRUE(ex_bytearrays_ == bytearrays_v_.operator ByteArrays()); |
| 131 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 132 | EXPECT_EQ(0, PropertyStoreTest::kUint16V.reader().get_uint16()); |
| 133 | EXPECT_EQ(ex_uint16_, uint16_v_.reader().get_uint16()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 134 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 135 | EXPECT_EQ(0, PropertyStoreTest::kInt16V.reader().get_int16()); |
| 136 | EXPECT_EQ(ex_int16_, int16_v_.reader().get_int16()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 137 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 138 | EXPECT_EQ(0, PropertyStoreTest::kUint32V.reader().get_uint32()); |
| 139 | EXPECT_EQ(ex_uint32_, uint32_v_.reader().get_uint32()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 140 | |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 141 | EXPECT_EQ(0, PropertyStoreTest::kUint64V.reader().get_uint64()); |
| 142 | EXPECT_EQ(ex_uint64_, uint64_v_.reader().get_uint64()); |
| 143 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 144 | EXPECT_EQ(0, PropertyStoreTest::kInt32V.reader().get_int32()); |
| 145 | EXPECT_EQ(ex_int32_, int32_v_.reader().get_int32()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 146 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 147 | EXPECT_EQ(ex_path_, path_v_.reader().get_path()); |
| 148 | |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 149 | EXPECT_EQ(ex_path_, paths_v_.operator vector< ::DBus::Path>()[0]); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 150 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 151 | EXPECT_EQ(string(""), PropertyStoreTest::kStringV.reader().get_string()); |
| 152 | EXPECT_EQ(ex_string_, string_v_.reader().get_string()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 153 | |
Gaurav Shah | b790aa2 | 2012-10-23 12:51:12 -0700 | [diff] [blame] | 154 | EXPECT_THAT(ex_stringmap_, |
| 155 | ContainerEq(stringmap_v_.operator map<string, string>())); |
| 156 | EXPECT_THAT(ex_strings_, |
| 157 | ContainerEq(strings_v_.operator vector<string>())); |
| 158 | EXPECT_THAT( |
| 159 | ex_stringmaps_, |
| 160 | ContainerEq(stringmaps_v_.operator vector<map<string, string> >())); |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 161 | EXPECT_THAT(ex_uint16s_, |
| 162 | ContainerEq(uint16s_v_.operator vector<uint16>())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TEST_F(DBusAdaptorTest, Signatures) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 166 | EXPECT_TRUE(DBusAdaptor::IsBool(bool_v_.signature())); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 167 | EXPECT_TRUE(DBusAdaptor::IsByteArrays(bytearrays_v_.signature())); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(DBusAdaptor::IsByte(byte_v_.signature())); |
| 169 | EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v_.signature())); |
| 170 | EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v_.signature())); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 171 | EXPECT_TRUE(DBusAdaptor::IsPath(path_v_.signature())); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 172 | EXPECT_TRUE(DBusAdaptor::IsPaths(paths_v_.signature())); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 173 | EXPECT_TRUE(DBusAdaptor::IsString(string_v_.signature())); |
| 174 | EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v_.signature())); |
| 175 | EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v_.signature())); |
| 176 | EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v_.signature())); |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 177 | EXPECT_TRUE(DBusAdaptor::IsUint16s(uint16s_v_.signature())); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 178 | EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v_.signature())); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 179 | EXPECT_TRUE(DBusAdaptor::IsUint64(uint64_v_.signature())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 180 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 181 | EXPECT_FALSE(DBusAdaptor::IsBool(byte_v_.signature())); |
| 182 | EXPECT_FALSE(DBusAdaptor::IsStrings(string_v_.signature())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 183 | } |
| 184 | |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 185 | template <typename T> |
| 186 | class DBusAdaptorTypedTest : public DBusAdaptorTest { |
| 187 | public: |
| 188 | DBusAdaptorTypedTest() |
| 189 | : property_(T()) // value-initialize primitives |
| 190 | {} |
| 191 | |
| 192 | protected: |
| 193 | T *property() { return &property_; } |
| 194 | |
| 195 | private: |
| 196 | T property_; |
| 197 | }; |
| 198 | |
| 199 | TYPED_TEST_CASE(DBusAdaptorTypedTest, PropertyStoreTest::PropertyTypes); |
| 200 | |
| 201 | TYPED_TEST(DBusAdaptorTypedTest, GetProperties) { |
| 202 | MockPropertyStore store; |
| 203 | const string kPropertyName("some property"); |
| 204 | PropertyStoreTest::RegisterProperty(&store, kPropertyName, this->property()); |
| 205 | |
| 206 | map<string, ::DBus::Variant> properties; |
| 207 | ::DBus::Error error; |
| 208 | EXPECT_TRUE(DBusAdaptor::GetProperties(store, &properties, &error)); |
| 209 | EXPECT_TRUE(ContainsKey(properties, kPropertyName)); |
| 210 | } |
| 211 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 212 | TEST_F(DBusAdaptorTest, SetProperty) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 213 | MockPropertyStore store; |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 214 | ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 215 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 216 | EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(true)); |
| 217 | EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(true)); |
| 218 | EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(true)); |
| 219 | EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(true)); |
| 220 | EXPECT_CALL(store, SetStringProperty("", _, _)) |
| 221 | .WillOnce(Return(true)); |
| 222 | EXPECT_CALL(store, SetStringmapProperty("", _, _)) |
| 223 | .WillOnce(Return(true)); |
| 224 | EXPECT_CALL(store, SetStringsProperty("", _, _)) |
| 225 | .WillOnce(Return(true)); |
| 226 | EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(true)); |
| 227 | EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(true)); |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 228 | EXPECT_CALL(store, SetUint16sProperty("", _, _)).WillOnce(Return(true)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 229 | EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(true)); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 230 | EXPECT_CALL(store, SetUint64Property("", _, _)).WillOnce(Return(true)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 231 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 232 | string string_path("/false/path"); |
| 233 | ::DBus::Path path(string_path); |
| 234 | ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path); |
| 235 | EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _)) |
| 236 | .WillOnce(Return(true)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 237 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 238 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", bool_v_, &e1)); |
| 239 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", path_v, &e2)); |
| 240 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", string_v_, &e3)); |
| 241 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", strings_v_, &e4)); |
| 242 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", int16_v_, &e5)); |
| 243 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", int32_v_, &e6)); |
| 244 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint16_v_, &e7)); |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 245 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint16s_v_, &e8)); |
| 246 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint32_v_, &e9)); |
| 247 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint64_v_, &e10)); |
| 248 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", stringmap_v_, &e11)); |
| 249 | EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", byte_v_, &e12)); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 250 | } |
| 251 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 252 | // SetProperty method should propagate failures. This should happen |
| 253 | // even if error isn't set. (This is to accomodate the fact that, if |
| 254 | // the property already has the desired value, the store will return |
| 255 | // false, without setting an error.) |
| 256 | TEST_F(DBusAdaptorTest, SetPropertyFailure) { |
| 257 | MockPropertyStore store; |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 258 | ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 259 | |
| 260 | EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(false)); |
| 261 | EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(false)); |
| 262 | EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(false)); |
| 263 | EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(false)); |
| 264 | EXPECT_CALL(store, SetStringProperty("", _, _)) |
| 265 | .WillOnce(Return(false)); |
| 266 | EXPECT_CALL(store, SetStringmapProperty("", _, _)) |
| 267 | .WillOnce(Return(false)); |
| 268 | EXPECT_CALL(store, SetStringsProperty("", _, _)) |
| 269 | .WillOnce(Return(false)); |
| 270 | EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(false)); |
| 271 | EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(false)); |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 272 | EXPECT_CALL(store, SetUint16sProperty("", _, _)).WillOnce(Return(false)); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 273 | EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(false)); |
| 274 | EXPECT_CALL(store, SetUint64Property("", _, _)).WillOnce(Return(false)); |
| 275 | |
| 276 | string string_path("/false/path"); |
| 277 | ::DBus::Path path(string_path); |
| 278 | ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path); |
| 279 | EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _)) |
| 280 | .WillOnce(Return(false)); |
| 281 | |
| 282 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", bool_v_, &e1)); |
| 283 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", path_v, &e2)); |
| 284 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", string_v_, &e3)); |
| 285 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", strings_v_, &e4)); |
| 286 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", int16_v_, &e5)); |
| 287 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", int32_v_, &e6)); |
| 288 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint16_v_, &e7)); |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 289 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint16s_v_, &e8)); |
| 290 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint32_v_, &e9)); |
| 291 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint64_v_, &e10)); |
| 292 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", stringmap_v_, &e11)); |
| 293 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", byte_v_, &e12)); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 294 | } |
| 295 | |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 296 | void SetError(const string &/*name*/, Error *error) { |
| 297 | error->Populate(Error::kInvalidProperty); |
| 298 | } |
| 299 | |
| 300 | TEST_F(DBusAdaptorTest, ClearProperty) { |
| 301 | MockPropertyStore store; |
| 302 | ::DBus::Error e1, e2; |
| 303 | |
| 304 | EXPECT_CALL(store, ClearProperty("valid property", _)). |
| 305 | WillOnce(Return(true)); |
| 306 | EXPECT_CALL(store, ClearProperty("invalid property", _)). |
| 307 | WillOnce(DoAll(Invoke(SetError), |
| 308 | Return(false))); |
| 309 | EXPECT_TRUE(DBusAdaptor::ClearProperty(&store, "valid property", &e1)); |
| 310 | EXPECT_FALSE(DBusAdaptor::ClearProperty(&store, "invalid property", &e2)); |
| 311 | |
| 312 | } |
| 313 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 314 | TEST_F(DBusAdaptorTest, ArgsToKeyValueStore) { |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 315 | map<string, ::DBus::Variant> args; |
| 316 | KeyValueStore args_kv; |
| 317 | Error error; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 318 | |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 319 | const bool kBool = true; |
| 320 | const char kBoolKey[] = "bool_arg"; |
| 321 | args[kBoolKey].writer().append_bool(kBool); |
Paul Stewart | 4823183 | 2013-07-17 15:46:19 -0700 | [diff] [blame] | 322 | const int32 kInt32 = 123; |
| 323 | const char kInt32Key[] = "int32_arg"; |
| 324 | args[kInt32Key].writer().append_int32(kInt32); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 325 | const char kString[] = "string"; |
| 326 | const char kStringKey[] = "string_arg"; |
| 327 | args[kStringKey].writer().append_string(kString); |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 328 | const map<string, string> kStringmap{ { "key0", "value0" } }; |
| 329 | const char kStringmapKey[] = "stringmap_key"; |
| 330 | DBus::MessageIter writer = args[kStringmapKey].writer(); |
| 331 | writer << kStringmap; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 332 | const vector<string> kStrings{ "string0", "string1" }; |
| 333 | const char kStringsKey[] = "strings_key"; |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 334 | writer = args[kStringsKey].writer(); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 335 | writer << kStrings; |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 336 | DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &error); |
| 337 | EXPECT_TRUE(error.IsSuccess()); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 338 | EXPECT_EQ(kBool, args_kv.GetBool(kBoolKey)); |
Paul Stewart | 4823183 | 2013-07-17 15:46:19 -0700 | [diff] [blame] | 339 | EXPECT_EQ(kInt32, args_kv.GetInt(kInt32Key)); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 340 | EXPECT_EQ(kString, args_kv.GetString(kStringKey)); |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 341 | EXPECT_EQ(kStringmap, args_kv.GetStringmap(kStringmapKey)); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 342 | EXPECT_EQ(kStrings, args_kv.GetStrings(kStringsKey)); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | TEST_F(DBusAdaptorTest, KeyValueStoreToVariant) { |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 346 | const char kStringmapKey[] = "StringmapKey"; |
| 347 | const map<string, string> kStringmapValue{ { "key", "value" } }; |
| 348 | const char kStringsKey[] = "StringsKey"; |
| 349 | const vector<string> kStringsValue{ "string0", "string1" }; |
| 350 | const char kStringKey[] = "StringKey"; |
| 351 | const char kStringValue[] = "StringValue"; |
| 352 | const char kBoolKey[] = "BoolKey"; |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 353 | const bool kBoolValue = true; |
| 354 | KeyValueStore store; |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 355 | store.SetStringmap(kStringmapKey, kStringmapValue); |
| 356 | store.SetStrings(kStringsKey, kStringsValue); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 357 | store.SetString(kStringKey, kStringValue); |
| 358 | store.SetBool(kBoolKey, kBoolValue); |
| 359 | DBus::Variant var = DBusAdaptor::KeyValueStoreToVariant(store); |
| 360 | ASSERT_TRUE(DBusAdaptor::IsKeyValueStore(var.signature())); |
| 361 | DBusPropertiesMap props = var.operator DBusPropertiesMap(); |
| 362 | // Sanity test the result. |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 363 | EXPECT_EQ(4, props.size()); |
| 364 | map<string, string> stringmap_value; |
| 365 | EXPECT_TRUE( |
| 366 | DBusProperties::GetStringmap(props, kStringmapKey, &stringmap_value)); |
| 367 | EXPECT_EQ(kStringmapValue, stringmap_value); |
| 368 | vector<string> strings_value; |
| 369 | EXPECT_TRUE(DBusProperties::GetStrings(props, kStringsKey, &strings_value)); |
| 370 | EXPECT_EQ(kStringsValue, strings_value); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 371 | string string_value; |
| 372 | EXPECT_TRUE(DBusProperties::GetString(props, kStringKey, &string_value)); |
| 373 | EXPECT_EQ(kStringValue, string_value); |
| 374 | bool bool_value = !kBoolValue; |
| 375 | EXPECT_TRUE(DBusProperties::GetBool(props, kBoolKey, &bool_value)); |
| 376 | EXPECT_EQ(kBoolValue, bool_value); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 379 | } // namespace shill |