blob: 3eb5aaef8cb76df62e5ac10b9514096658ac69f3 [file] [log] [blame]
Chris Masoned0ceb8c2011-06-02 10:05:39 -07001// 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 Masone8fe2c7e2011-06-09 15:51:19 -07005#include "shill/dbus_adaptor.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -07006
Chris Masone8fe2c7e2011-06-09 15:51:19 -07007#include <map>
Chris Masoned0ceb8c2011-06-02 10:05:39 -07008#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <vector>
Chris Masoned0ceb8c2011-06-02 10:05:39 -070010
11#include <dbus-c++/dbus.h>
12#include <gtest/gtest.h>
13#include <gmock/gmock.h>
14
Chris Masone8fe2c7e2011-06-09 15:51:19 -070015#include "shill/manager.h"
16#include "shill/mock_control.h"
17#include "shill/mock_device.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070018#include "shill/mock_glib.h"
19#include "shill/mock_profile.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020#include "shill/mock_property_store.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070021#include "shill/mock_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022#include "shill/property_store_unittest.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023#include "shill/shill_event.h"
24
25using std::map;
26using std::string;
27using std::vector;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070028using ::testing::_;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070029using ::testing::Return;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070030using ::testing::StrEq;
31using ::testing::Test;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070032
Chris Masone8fe2c7e2011-06-09 15:51:19 -070033namespace shill {
34
Chris Masone3bd3c8c2011-06-13 08:20:26 -070035class DBusAdaptorTest : public PropertyStoreTest {
Chris Masoned0ceb8c2011-06-02 10:05:39 -070036 public:
Chris Masone8fe2c7e2011-06-09 15:51:19 -070037 DBusAdaptorTest()
Chris Masoneb925cc82011-06-22 15:39:57 -070038 : ex_bool_(true),
39 ex_byte_(0xff),
40 ex_uint16_(65535),
41 ex_uint32_(2000000),
42 ex_int16_(-32768),
43 ex_int32_(-65536),
44 ex_string_("something"),
45 ex_strings_(1, ex_string_),
46 bool_v_(DBusAdaptor::BoolToVariant(ex_bool_)),
47 byte_v_(DBusAdaptor::ByteToVariant(ex_byte_)),
Chris Masoneb925cc82011-06-22 15:39:57 -070048 int16_v_(DBusAdaptor::Int16ToVariant(ex_int16_)),
49 int32_v_(DBusAdaptor::Int32ToVariant(ex_int32_)),
50 string_v_(DBusAdaptor::StringToVariant(ex_string_)),
51 strings_v_(DBusAdaptor::StringsToVariant(ex_strings_)),
mukesh agrawalf60e4062011-05-27 13:13:41 -070052 uint16_v_(DBusAdaptor::Uint16ToVariant(ex_uint16_)),
53 uint32_v_(DBusAdaptor::Uint32ToVariant(ex_uint32_)),
Chris Masone8fe2c7e2011-06-09 15:51:19 -070054 device_(new MockDevice(&control_interface_,
55 &dispatcher_,
56 &manager_,
57 "mock",
58 0)),
59 service_(new MockService(&control_interface_,
60 &dispatcher_,
Chris Masone7aa5f902011-07-11 11:13:35 -070061 new MockProfile(&control_interface_, &glib_),
Chris Masone8fe2c7e2011-06-09 15:51:19 -070062 "mock")) {
Chris Masoneb925cc82011-06-22 15:39:57 -070063 ex_stringmap_[ex_string_] = ex_string_;
64 stringmap_v_ = DBusAdaptor::StringmapToVariant(ex_stringmap_);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070065 }
66
Chris Masoned0ceb8c2011-06-02 10:05:39 -070067 virtual ~DBusAdaptorTest() {}
Chris Masone8fe2c7e2011-06-09 15:51:19 -070068
Chris Masone8fe2c7e2011-06-09 15:51:19 -070069 protected:
Chris Masoneb925cc82011-06-22 15:39:57 -070070 bool ex_bool_;
71 uint8 ex_byte_;
72 uint16 ex_uint16_;
73 uint32 ex_uint32_;
74 int16 ex_int16_;
75 int32 ex_int32_;
76 string ex_string_;
77 map<string, string> ex_stringmap_;
78 vector<map<string, string> > ex_stringmaps_;
79 vector<string> ex_strings_;
80
81 ::DBus::Variant bool_v_;
82 ::DBus::Variant byte_v_;
83 ::DBus::Variant int16_v_;
84 ::DBus::Variant int32_v_;
85 ::DBus::Variant string_v_;
86 ::DBus::Variant stringmap_v_;
87 ::DBus::Variant stringmaps_v_;
88 ::DBus::Variant strings_v_;
89 ::DBus::Variant uint16_v_;
90 ::DBus::Variant uint32_v_;
91
Chris Masone8fe2c7e2011-06-09 15:51:19 -070092 DeviceRefPtr device_;
93 ServiceRefPtr service_;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070094};
95
96TEST_F(DBusAdaptorTest, Conversions) {
Chris Masoneb925cc82011-06-22 15:39:57 -070097 EXPECT_EQ(0, PropertyStoreTest::kBoolV.reader().get_bool());
98 EXPECT_EQ(ex_bool_, bool_v_.reader().get_bool());
Chris Masoned0ceb8c2011-06-02 10:05:39 -070099
Chris Masoneb925cc82011-06-22 15:39:57 -0700100 EXPECT_EQ(0, PropertyStoreTest::kByteV.reader().get_byte());
101 EXPECT_EQ(ex_byte_, byte_v_.reader().get_byte());
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700102
Chris Masoneb925cc82011-06-22 15:39:57 -0700103 EXPECT_EQ(0, PropertyStoreTest::kUint16V.reader().get_uint16());
104 EXPECT_EQ(ex_uint16_, uint16_v_.reader().get_uint16());
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700105
Chris Masoneb925cc82011-06-22 15:39:57 -0700106 EXPECT_EQ(0, PropertyStoreTest::kInt16V.reader().get_int16());
107 EXPECT_EQ(ex_int16_, int16_v_.reader().get_int16());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700108
Chris Masoneb925cc82011-06-22 15:39:57 -0700109 EXPECT_EQ(0, PropertyStoreTest::kUint32V.reader().get_uint32());
110 EXPECT_EQ(ex_uint32_, uint32_v_.reader().get_uint32());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700111
Chris Masoneb925cc82011-06-22 15:39:57 -0700112 EXPECT_EQ(0, PropertyStoreTest::kInt32V.reader().get_int32());
113 EXPECT_EQ(ex_int32_, int32_v_.reader().get_int32());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700114
Chris Masoneb925cc82011-06-22 15:39:57 -0700115 EXPECT_EQ(string(""), PropertyStoreTest::kStringV.reader().get_string());
116 EXPECT_EQ(ex_string_, string_v_.reader().get_string());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700117
Chris Masoneb925cc82011-06-22 15:39:57 -0700118 EXPECT_EQ(ex_stringmap_[ex_string_],
119 (stringmap_v_.operator map<string, string>()[ex_string_]));
120 EXPECT_EQ(ex_strings_[0], strings_v_.operator vector<string>()[0]);
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700121}
122
123TEST_F(DBusAdaptorTest, Signatures) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700124 EXPECT_TRUE(DBusAdaptor::IsBool(bool_v_.signature()));
125 EXPECT_TRUE(DBusAdaptor::IsByte(byte_v_.signature()));
126 EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v_.signature()));
127 EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v_.signature()));
128 EXPECT_TRUE(DBusAdaptor::IsString(string_v_.signature()));
129 EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v_.signature()));
130 EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v_.signature()));
131 EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v_.signature()));
132 EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v_.signature()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700133
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700134 EXPECT_FALSE(DBusAdaptor::IsBool(byte_v_.signature()));
135 EXPECT_FALSE(DBusAdaptor::IsStrings(string_v_.signature()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700136}
137
138TEST_F(DBusAdaptorTest, Dispatch) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700139 MockPropertyStore store;
mukesh agrawala903e412011-06-28 17:07:11 -0700140 ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700141
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700142 EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(true));
143 EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(true));
144 EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(true));
145 EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(true));
146 EXPECT_CALL(store, SetStringProperty("", _, _))
147 .WillOnce(Return(true));
148 EXPECT_CALL(store, SetStringmapProperty("", _, _))
149 .WillOnce(Return(true));
150 EXPECT_CALL(store, SetStringsProperty("", _, _))
151 .WillOnce(Return(true));
152 EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(true));
153 EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(true));
154 EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(true));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700155
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700156 string string_path("/false/path");
157 ::DBus::Path path(string_path);
158 ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path);
159 EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _))
160 .WillOnce(Return(true));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700161
Chris Masonea8a2c252011-06-27 22:16:30 -0700162 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", bool_v_, &e1));
163 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", path_v, &e2));
164 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", string_v_, &e3));
165 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", strings_v_, &e4));
166 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", int16_v_, &e5));
167 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", int32_v_, &e6));
168 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", uint16_v_, &e7));
169 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", uint32_v_, &e8));
170 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", stringmap_v_, &e9));
171 EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", byte_v_, &e10));
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700172}
173
174} // namespace shill