1 //
2 // Copyright (C) 2016 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/dbus/chromeos_manager_dbus_adaptor.h"
18 
19 #include <memory>
20 
21 #include <brillo/errors/error.h>
22 #include <dbus/bus.h>
23 #include <dbus/message.h>
24 #include <dbus/mock_bus.h>
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 
28 #include "shill/dbus/mock_dbus_service_watcher.h"
29 #include "shill/dbus/mock_dbus_service_watcher_factory.h"
30 #include "shill/error.h"
31 #include "shill/mock_control.h"
32 #include "shill/mock_event_dispatcher.h"
33 #include "shill/mock_manager.h"
34 #include "shill/mock_metrics.h"
35 
36 using dbus::MockBus;
37 using dbus::Response;
38 using std::string;
39 using testing::_;
40 using testing::DoAll;
41 using testing::Invoke;
42 using testing::Return;
43 using testing::SetArgPointee;
44 using testing::Test;
45 using testing::WithArg;
46 
47 namespace shill {
48 
49 class ChromeosManagerDBusAdaptorTest : public Test {
50  public:
ChromeosManagerDBusAdaptorTest()51   ChromeosManagerDBusAdaptorTest()
52       : adaptor_bus_(new MockBus(dbus::Bus::Options())),
53         proxy_bus_(new MockBus(dbus::Bus::Options())),
54         metrics_(&dispatcher_),
55         manager_(&control_interface_, &dispatcher_, &metrics_),
56         manager_adaptor_(adaptor_bus_, proxy_bus_, &manager_) {}
57 
~ChromeosManagerDBusAdaptorTest()58   virtual ~ChromeosManagerDBusAdaptorTest() {}
59 
SetUp()60   virtual void SetUp() {
61     manager_adaptor_.dbus_service_watcher_factory_ =
62         &dbus_service_watcher_factory_;
63   }
64 
TearDown()65   virtual void TearDown() {}
66 
67  protected:
68   scoped_refptr<MockBus> adaptor_bus_;
69   scoped_refptr<MockBus> proxy_bus_;
70   MockControl control_interface_;
71   MockEventDispatcher dispatcher_;
72   MockMetrics metrics_;
73   MockManager manager_;
74   MockDBusServiceWatcherFactory dbus_service_watcher_factory_;
75   ChromeosManagerDBusAdaptor manager_adaptor_;
76 };
77 
SetErrorTypeSuccess(Error * error)78 void SetErrorTypeSuccess(Error* error) { error->Populate(Error::kSuccess); }
79 
SetErrorTypeFailure(Error * error)80 void SetErrorTypeFailure(Error* error) {
81   error->Populate(Error::kOperationFailed);
82 }
83 
TEST_F(ChromeosManagerDBusAdaptorTest,ClaimInterface)84 TEST_F(ChromeosManagerDBusAdaptorTest, ClaimInterface) {
85   brillo::ErrorPtr error;
86   string kDefaultClaimerName = "";
87   string kNonDefaultClaimerName = "test_claimer";
88   string kInterfaceName = "test_interface";
89   scoped_ptr<Response> message(Response::CreateEmpty());
90 
91   // Watcher for device claimer is not created when we fail to claim the device.
92   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
93   EXPECT_CALL(manager_, ClaimDevice(_, kInterfaceName, _))
94       .WillOnce(WithArg<2>(Invoke(SetErrorTypeFailure)));
95   EXPECT_CALL(dbus_service_watcher_factory_, CreateDBusServiceWatcher(_, _, _))
96       .Times(0);
97   manager_adaptor_.ClaimInterface(&error, message.get(), kNonDefaultClaimerName,
98                                   kInterfaceName);
99   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
100 
101   // Watcher for device claimer is not created when we succeed in claiming the
102   // device from the default claimer.
103   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
104   EXPECT_CALL(manager_, ClaimDevice(_, kInterfaceName, _))
105       .WillOnce(WithArg<2>(Invoke(SetErrorTypeSuccess)));
106   EXPECT_CALL(dbus_service_watcher_factory_, CreateDBusServiceWatcher(_, _, _))
107       .Times(0);
108   manager_adaptor_.ClaimInterface(&error, message.get(), kDefaultClaimerName,
109                                   kInterfaceName);
110   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
111 
112   // Watcher for device claimer is created when we succeed in claiming the
113   // device from a non-default claimer.
114   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
115   EXPECT_CALL(manager_, ClaimDevice(_, kInterfaceName, _))
116       .WillOnce(WithArg<2>(Invoke(SetErrorTypeSuccess)));
117   EXPECT_CALL(dbus_service_watcher_factory_, CreateDBusServiceWatcher(_, _, _))
118       .WillOnce(Return(new MockDBusServiceWatcher()));
119   manager_adaptor_.ClaimInterface(&error, message.get(), kNonDefaultClaimerName,
120                                   kInterfaceName);
121   EXPECT_NE(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
122 }
123 
TEST_F(ChromeosManagerDBusAdaptorTest,ReleaseInterface)124 TEST_F(ChromeosManagerDBusAdaptorTest, ReleaseInterface) {
125   brillo::ErrorPtr error;
126   string kClaimerName = "test_claimer";
127   string kInterfaceName = "test_interface";
128   scoped_ptr<Response> message(Response::CreateEmpty());
129 
130   // Setup watcher for device claimer.
131   manager_adaptor_.watcher_for_device_claimer_.reset(
132       new MockDBusServiceWatcher());
133 
134   // If the device claimer is not removed, do not reset the watcher for device
135   // claimer.
136   EXPECT_CALL(manager_, ReleaseDevice(_, kInterfaceName, _, _))
137       .WillOnce(SetArgPointee<2>(false));
138   manager_adaptor_.ReleaseInterface(&error, message.get(), kClaimerName,
139                                   kInterfaceName);
140   EXPECT_NE(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
141 
142   // If the device claimer is removed, reset the watcher for device claimer.
143   EXPECT_CALL(manager_, ReleaseDevice(_, kInterfaceName, _, _))
144       .WillOnce(SetArgPointee<2>(true));
145   manager_adaptor_.ReleaseInterface(&error, message.get(), kClaimerName,
146                                     kInterfaceName);
147   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
148 }
149 
TEST_F(ChromeosManagerDBusAdaptorTest,SetupApModeInterface)150 TEST_F(ChromeosManagerDBusAdaptorTest, SetupApModeInterface) {
151   brillo::ErrorPtr error;
152   string out_interface_name;
153   scoped_ptr<Response> message(Response::CreateEmpty());
154 
155 #if !defined(DISABLE_WIFI) && defined(__BRILLO__)
156   // Watcher for AP mode setter is not created when we fail to setup AP mode
157   // interface.
158   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_ap_mode_setter_.get());
159   EXPECT_CALL(manager_, SetupApModeInterface(&out_interface_name, _))
160       .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorTypeFailure)), Return(false)));
161   EXPECT_CALL(dbus_service_watcher_factory_, CreateDBusServiceWatcher(_, _, _))
162       .Times(0);
163   EXPECT_FALSE(manager_adaptor_.SetupApModeInterface(&error, message.get(),
164                                                      &out_interface_name));
165   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_ap_mode_setter_.get());
166 
167   // Watcher for AP mode setter is created when we succeed in AP mode
168   // interface setup.
169   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_ap_mode_setter_.get());
170   EXPECT_CALL(manager_, SetupApModeInterface(&out_interface_name, _))
171       .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorTypeSuccess)), Return(true)));
172   EXPECT_CALL(dbus_service_watcher_factory_, CreateDBusServiceWatcher(_, _, _))
173       .WillOnce(Return(new MockDBusServiceWatcher()));
174   EXPECT_TRUE(manager_adaptor_.SetupApModeInterface(&error, message.get(),
175                                                     &out_interface_name));
176   EXPECT_NE(nullptr, manager_adaptor_.watcher_for_ap_mode_setter_.get());
177 #else
178   EXPECT_CALL(dbus_service_watcher_factory_, CreateDBusServiceWatcher(_, _, _))
179       .Times(0);
180   EXPECT_FALSE(manager_adaptor_.SetupApModeInterface(&error, message.get(),
181                                                      &out_interface_name));
182 #endif  // !DISABLE_WIFI && __BRILLO__
183 }
184 
TEST_F(ChromeosManagerDBusAdaptorTest,SetupStationModeInterface)185 TEST_F(ChromeosManagerDBusAdaptorTest, SetupStationModeInterface) {
186   brillo::ErrorPtr error;
187   string out_interface_name;
188 
189 #if !defined(DISABLE_WIFI) && defined(__BRILLO__)
190   // Setup watcher for AP mode setter.
191   manager_adaptor_.watcher_for_ap_mode_setter_.reset(
192       new MockDBusServiceWatcher());
193 
194   // Reset watcher for AP mode setter after setting up station mode interface.
195   EXPECT_CALL(manager_, SetupStationModeInterface(&out_interface_name, _))
196       .WillOnce(Return(true));
197   EXPECT_TRUE(
198       manager_adaptor_.SetupStationModeInterface(&error, &out_interface_name));
199   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_ap_mode_setter_.get());
200 #else
201   EXPECT_FALSE(
202       manager_adaptor_.SetupStationModeInterface(&error, &out_interface_name));
203 #endif  // !DISABLE_WIFI && __BRILLO__
204 }
205 
TEST_F(ChromeosManagerDBusAdaptorTest,OnApModeSetterVanished)206 TEST_F(ChromeosManagerDBusAdaptorTest, OnApModeSetterVanished) {
207   // Setup watcher for AP mode setter.
208   manager_adaptor_.watcher_for_ap_mode_setter_.reset(
209       new MockDBusServiceWatcher());
210 
211   // Reset watcher for AP mode setter after AP mode setter vanishes.
212 #if !defined(DISABLE_WIFI) && defined(__BRILLO__)
213   EXPECT_CALL(manager_, OnApModeSetterVanished());
214 #endif  // !DISABLE_WIFI && __BRILLO__
215   manager_adaptor_.OnApModeSetterVanished();
216   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_ap_mode_setter_.get());
217 }
218 
TEST_F(ChromeosManagerDBusAdaptorTest,OnDeviceClaimerVanished)219 TEST_F(ChromeosManagerDBusAdaptorTest, OnDeviceClaimerVanished) {
220   // Setup watcher for device claimer.
221   manager_adaptor_.watcher_for_device_claimer_.reset(
222       new MockDBusServiceWatcher());
223 
224   // Reset watcher for device claimer after the device claimer vanishes.
225   EXPECT_CALL(manager_, OnDeviceClaimerVanished());
226   manager_adaptor_.OnDeviceClaimerVanished();
227   EXPECT_EQ(nullptr, manager_adaptor_.watcher_for_device_claimer_.get());
228 }
229 
230 }  // namespace shill
231