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 | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame^] | 15 | #include "shill/manager.h" |
| 16 | #include "shill/mock_control.h" |
| 17 | #include "shill/mock_device.h" |
| 18 | #include "shill/mock_service.h" |
| 19 | #include "shill/shill_event.h" |
| 20 | |
| 21 | using std::map; |
| 22 | using std::string; |
| 23 | using std::vector; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 24 | using ::testing::Test; |
| 25 | using ::testing::_; |
| 26 | using ::testing::NiceMock; |
| 27 | using ::testing::Return; |
| 28 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame^] | 29 | namespace shill { |
| 30 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 31 | class DBusAdaptorTest : public Test { |
| 32 | public: |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame^] | 33 | DBusAdaptorTest() |
| 34 | : ex_bool(true), |
| 35 | ex_byte(0xff), |
| 36 | ex_uint16(65535), |
| 37 | ex_uint32(2000000), |
| 38 | ex_int16(-32768), |
| 39 | ex_int32(-65536), |
| 40 | ex_string("something"), |
| 41 | ex_strings(1, ex_string), |
| 42 | manager_(&control_interface_, &dispatcher_), |
| 43 | device_(new MockDevice(&control_interface_, |
| 44 | &dispatcher_, |
| 45 | &manager_, |
| 46 | "mock", |
| 47 | 0)), |
| 48 | service_(new MockService(&control_interface_, |
| 49 | &dispatcher_, |
| 50 | device_, |
| 51 | "mock")) { |
| 52 | ex_stringmap[ex_string] = ex_string; |
| 53 | |
| 54 | bool_v = DBusAdaptor::BoolToVariant(ex_bool); |
| 55 | byte_v = DBusAdaptor::ByteToVariant(ex_byte); |
| 56 | uint16_v = DBusAdaptor::Uint16ToVariant(ex_uint16); |
| 57 | uint32_v = DBusAdaptor::Uint32ToVariant(ex_uint32); |
| 58 | int16_v = DBusAdaptor::Int16ToVariant(ex_int16); |
| 59 | int32_v = DBusAdaptor::Int32ToVariant(ex_int32); |
| 60 | string_v = DBusAdaptor::StringToVariant(ex_string); |
| 61 | stringmap_v = DBusAdaptor::StringmapToVariant(ex_stringmap); |
| 62 | strings_v = DBusAdaptor::StringsToVariant(ex_strings); |
| 63 | } |
| 64 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 65 | virtual ~DBusAdaptorTest() {} |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame^] | 66 | |
| 67 | bool ex_bool; |
| 68 | uint8 ex_byte; |
| 69 | uint16 ex_uint16; |
| 70 | uint32 ex_uint32; |
| 71 | int16 ex_int16; |
| 72 | int32 ex_int32; |
| 73 | string ex_string; |
| 74 | map<string, string> ex_stringmap; |
| 75 | vector<string> ex_strings; |
| 76 | |
| 77 | ::DBus::Variant bool_v; |
| 78 | ::DBus::Variant byte_v; |
| 79 | ::DBus::Variant uint16_v; |
| 80 | ::DBus::Variant uint32_v; |
| 81 | ::DBus::Variant int16_v; |
| 82 | ::DBus::Variant int32_v; |
| 83 | ::DBus::Variant string_v; |
| 84 | ::DBus::Variant stringmap_v; |
| 85 | ::DBus::Variant strings_v; |
| 86 | |
| 87 | protected: |
| 88 | MockControl control_interface_; |
| 89 | EventDispatcher dispatcher_; |
| 90 | Manager manager_; |
| 91 | DeviceRefPtr device_; |
| 92 | ServiceRefPtr service_; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | TEST_F(DBusAdaptorTest, Conversions) { |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame^] | 96 | EXPECT_EQ(0, DBusAdaptor::BoolToVariant(0).reader().get_bool()); |
| 97 | EXPECT_EQ(ex_bool, bool_v.reader().get_bool()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 98 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame^] | 99 | EXPECT_EQ(0, DBusAdaptor::ByteToVariant(0).reader().get_byte()); |
| 100 | EXPECT_EQ(ex_byte, byte_v.reader().get_byte()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 101 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame^] | 102 | EXPECT_EQ(0, DBusAdaptor::Uint16ToVariant(0).reader().get_uint16()); |
| 103 | EXPECT_EQ(ex_uint16, uint16_v.reader().get_uint16()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 104 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame^] | 105 | EXPECT_EQ(0, DBusAdaptor::Int16ToVariant(0).reader().get_int16()); |
| 106 | EXPECT_EQ(ex_int16, int16_v.reader().get_int16()); |
| 107 | |
| 108 | EXPECT_EQ(0, DBusAdaptor::Uint32ToVariant(0).reader().get_uint32()); |
| 109 | EXPECT_EQ(ex_uint32, uint32_v.reader().get_uint32()); |
| 110 | |
| 111 | EXPECT_EQ(0, DBusAdaptor::Int32ToVariant(0).reader().get_int32()); |
| 112 | EXPECT_EQ(ex_int32, int32_v.reader().get_int32()); |
| 113 | |
| 114 | EXPECT_EQ(string(""), DBusAdaptor::StringToVariant("").reader().get_string()); |
| 115 | EXPECT_EQ(ex_string, string_v.reader().get_string()); |
| 116 | |
| 117 | EXPECT_EQ(ex_stringmap[ex_string], |
| 118 | (stringmap_v.operator map<string, string>()[ex_string])); |
| 119 | EXPECT_EQ(ex_strings[0], strings_v.operator vector<string>()[0]); |
| 120 | } |
| 121 | |
| 122 | TEST_F(DBusAdaptorTest, Signatures) { |
| 123 | EXPECT_TRUE(DBusAdaptor::IsBool(bool_v.signature())); |
| 124 | EXPECT_TRUE(DBusAdaptor::IsByte(byte_v.signature())); |
| 125 | EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v.signature())); |
| 126 | EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v.signature())); |
| 127 | EXPECT_TRUE(DBusAdaptor::IsString(string_v.signature())); |
| 128 | EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v.signature())); |
| 129 | EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v.signature())); |
| 130 | EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v.signature())); |
| 131 | EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v.signature())); |
| 132 | |
| 133 | EXPECT_FALSE(DBusAdaptor::IsBool(byte_v.signature())); |
| 134 | EXPECT_FALSE(DBusAdaptor::IsStrings(string_v.signature())); |
| 135 | } |
| 136 | |
| 137 | TEST_F(DBusAdaptorTest, Dispatch) { |
| 138 | ::DBus::Error error; |
| 139 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&manager_, "", bool_v, error)); |
| 140 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&manager_, "", string_v, error)); |
| 141 | |
| 142 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", strings_v, error)); |
| 143 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", int16_v, error)); |
| 144 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", int32_v, error)); |
| 145 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", uint16_v, error)); |
| 146 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", uint32_v, error)); |
| 147 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", stringmap_v, error)); |
| 148 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", byte_v, error)); |
| 149 | |
| 150 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(), "", bool_v, error)); |
| 151 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(), "", string_v, error)); |
| 152 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(), "", int16_v, error)); |
| 153 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(), "", int32_v, error)); |
| 154 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(), "", uint16_v, error)); |
| 155 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(), "", uint32_v, error)); |
| 156 | |
| 157 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_.get(), "", byte_v, error)); |
| 158 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_.get(), |
| 159 | "", |
| 160 | strings_v, |
| 161 | error)); |
| 162 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_.get(), |
| 163 | "", |
| 164 | stringmap_v, |
| 165 | error)); |
| 166 | |
| 167 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_.get(), "", bool_v, error)); |
| 168 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_.get(), "", byte_v, error)); |
| 169 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_.get(), "", string_v, error)); |
| 170 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_.get(), "", int32_v, error)); |
| 171 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_.get(), |
| 172 | "", |
| 173 | stringmap_v, |
| 174 | error)); |
| 175 | |
| 176 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_.get(), "", int16_v, error)); |
| 177 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_.get(), |
| 178 | "", |
| 179 | uint16_v, |
| 180 | error)); |
| 181 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_.get(), |
| 182 | "", |
| 183 | uint32_v, |
| 184 | error)); |
| 185 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_.get(), |
| 186 | "", |
| 187 | strings_v, |
| 188 | error)); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } // namespace shill |