blob: e6a2ef4cac83c26d86ae49a5b45ba041c63155c8 [file] [log] [blame]
Jay Srinivasan43488792012-06-19 00:25:31 -07001// Copyright (c) 2012 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
5#include <base/logging.h>
6#include <chromeos/dbus/service_constants.h>
7#include <gtest/gtest.h>
8#include <string>
9
10#include "update_engine/connection_manager.h"
11#include "update_engine/mock_dbus_interface.h"
12#include "update_engine/mock_system_state.h"
13
14using std::set;
15using std::string;
16using testing::_;
17using testing::AnyNumber;
18using testing::Return;
19using testing::SetArgumentPointee;
20using testing::StrEq;
21
22namespace chromeos_update_engine {
23
24class ConnectionManagerTest : public ::testing::Test {
25 public:
26 ConnectionManagerTest()
27 : kMockFlimFlamManagerProxy_(NULL),
28 kMockFlimFlamServiceProxy_(NULL),
29 kServicePath_(NULL),
30 cmut_(&mock_system_state_) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080031 mock_system_state_.set_connection_manager(&cmut_);
Jay Srinivasan43488792012-06-19 00:25:31 -070032 }
33
34 protected:
35 void SetupMocks(const char* service_path);
36 void SetManagerReply(gconstpointer value, const GType& type);
37 void SetServiceReply(const char* service_type);
38 void TestWithServiceType(
39 const char* service_type, NetworkConnectionType expected_type);
40
41 static const char* kGetPropertiesMethod;
42 DBusGProxy* kMockFlimFlamManagerProxy_;
43 DBusGProxy* kMockFlimFlamServiceProxy_;
44 DBusGConnection* kMockSystemBus_;
45 const char* kServicePath_;
46 MockDbusGlib dbus_iface_;
47 ConnectionManager cmut_; // ConnectionManager under test.
48 MockSystemState mock_system_state_;
49};
50
51// static
52const char* ConnectionManagerTest::kGetPropertiesMethod = "GetProperties";
53
54void ConnectionManagerTest::SetupMocks(const char* service_path) {
55 int number = 1;
56 kMockSystemBus_ = reinterpret_cast<DBusGConnection*>(number++);
57 kMockFlimFlamManagerProxy_ = reinterpret_cast<DBusGProxy*>(number++);
58 kMockFlimFlamServiceProxy_ = reinterpret_cast<DBusGProxy*>(number++);
59 ASSERT_NE(kMockSystemBus_, reinterpret_cast<DBusGConnection*>(NULL));
60
61 kServicePath_ = service_path;
62
63 ON_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
64 .WillByDefault(Return(kMockSystemBus_));
65 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
66 .Times(AnyNumber());
67}
68
69void ConnectionManagerTest::SetManagerReply(gconstpointer reply_value,
70 const GType& reply_type) {
71 // Initialize return value for D-Bus call to Manager object.
72 // TODO (jaysri): Free the objects allocated here.
73 GHashTable* manager_hash_table = g_hash_table_new(g_str_hash, g_str_equal);
74
75 GArray* array = g_array_new(FALSE, FALSE, sizeof(const char*));
76 ASSERT_TRUE(array != NULL);
77
78 EXPECT_EQ(array, g_array_append_val(array, reply_value));
79 GValue* array_as_value = g_new0(GValue, 1);
80 EXPECT_EQ(array_as_value, g_value_init(array_as_value, reply_type));
81 g_value_take_boxed(array_as_value, array);
82 g_hash_table_insert(manager_hash_table,
83 const_cast<char*>("Services"),
84 array_as_value);
85
86 // Plumb return value into mock object.
87 EXPECT_CALL(dbus_iface_, ProxyCall(kMockFlimFlamManagerProxy_,
88 StrEq(kGetPropertiesMethod),
89 _,
90 G_TYPE_INVALID,
91 dbus_g_type_get_map("GHashTable",
92 G_TYPE_STRING,
93 G_TYPE_VALUE),
94 _,
95 G_TYPE_INVALID))
96 .WillOnce(DoAll(SetArgumentPointee<5>(manager_hash_table), Return(TRUE)));
97
98 // Set other expectations.
99 EXPECT_CALL(dbus_iface_,
100 ProxyNewForNameOwner(kMockSystemBus_,
101 StrEq(flimflam::kFlimflamServiceName),
102 StrEq(flimflam::kFlimflamServicePath),
103 StrEq(flimflam::kFlimflamManagerInterface),
104 _))
105 .WillOnce(Return(kMockFlimFlamManagerProxy_));
106 EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamManagerProxy_));
107 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
108 .RetiresOnSaturation();
109}
110
111void ConnectionManagerTest::SetServiceReply(const char* service_type) {
112 // Initialize return value for D-Bus call to Service object.
113 // TODO (jaysri): Free the objects allocated here.
114 GHashTable* service_hash_table = g_hash_table_new(g_str_hash, g_str_equal);
115
116 GValue* service_type_value = g_new0(GValue, 1);
117 EXPECT_EQ(service_type_value,
118 g_value_init(service_type_value, G_TYPE_STRING));
119 g_value_set_static_string(service_type_value, service_type);
120
121 g_hash_table_insert(service_hash_table,
122 const_cast<char*>("Type"),
123 service_type_value);
124
125 // Plumb return value into mock object.
126 EXPECT_CALL(dbus_iface_, ProxyCall(kMockFlimFlamServiceProxy_,
127 StrEq(kGetPropertiesMethod),
128 _,
129 G_TYPE_INVALID,
130 dbus_g_type_get_map("GHashTable",
131 G_TYPE_STRING,
132 G_TYPE_VALUE),
133 _,
134 G_TYPE_INVALID))
135 .WillOnce(DoAll(SetArgumentPointee<5>(service_hash_table), Return(TRUE)));
136
137 // Set other expectations.
138 EXPECT_CALL(dbus_iface_,
139 ProxyNewForNameOwner(kMockSystemBus_,
140 StrEq(flimflam::kFlimflamServiceName),
141 StrEq(kServicePath_),
142 StrEq(flimflam::kFlimflamServiceInterface),
143 _))
144 .WillOnce(Return(kMockFlimFlamServiceProxy_));
145 EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamServiceProxy_));
146 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
147 .RetiresOnSaturation();
148}
149
150void ConnectionManagerTest::TestWithServiceType(
151 const char* service_type,
152 NetworkConnectionType expected_type) {
153
154 SetupMocks("/service/guest-network");
155 SetManagerReply(kServicePath_, DBUS_TYPE_G_OBJECT_PATH_ARRAY);
156 SetServiceReply(service_type);
157
158 NetworkConnectionType type;
159 EXPECT_TRUE(cmut_.GetConnectionType(&dbus_iface_, &type));
160 EXPECT_EQ(expected_type, type);
161}
162
163TEST_F(ConnectionManagerTest, SimpleTest) {
164 TestWithServiceType(flimflam::kTypeEthernet, kNetEthernet);
165 TestWithServiceType(flimflam::kTypeWifi, kNetWifi);
166 TestWithServiceType(flimflam::kTypeWimax, kNetWimax);
167 TestWithServiceType(flimflam::kTypeBluetooth, kNetBluetooth);
168 TestWithServiceType(flimflam::kTypeCellular, kNetCellular);
169}
170
171TEST_F(ConnectionManagerTest, UnknownTest) {
172 TestWithServiceType("foo", kNetUnknown);
173}
174
175TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800176 EXPECT_CALL(mock_system_state_, device_policy()).Times(0);
Jay Srinivasan43488792012-06-19 00:25:31 -0700177
178 // Updates over Ethernet are allowed even if there's no policy.
179 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet));
180}
181
182TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800183 EXPECT_CALL(mock_system_state_, device_policy()).Times(0);
Jay Srinivasan43488792012-06-19 00:25:31 -0700184 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi));
185}
186
187TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800188 EXPECT_CALL(mock_system_state_, device_policy()).Times(0);
Jay Srinivasan43488792012-06-19 00:25:31 -0700189 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax));
190}
191
192TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800193 EXPECT_CALL(mock_system_state_, device_policy()).Times(0);
Jay Srinivasan43488792012-06-19 00:25:31 -0700194 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth));
195}
196
197TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
198 policy::MockDevicePolicy allow_3g_policy;
199
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800200 EXPECT_CALL(mock_system_state_, device_policy())
Jay Srinivasan43488792012-06-19 00:25:31 -0700201 .Times(1)
202 .WillOnce(Return(&allow_3g_policy));
203
204 // This test tests 3G being the only connection type being allowed.
205 set<string> allowed_set;
206 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
207
208 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
209 .Times(1)
210 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
211
212 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular));
213}
214
215TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
216 policy::MockDevicePolicy allow_3g_policy;
217
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800218 EXPECT_CALL(mock_system_state_, device_policy())
Jay Srinivasan43488792012-06-19 00:25:31 -0700219 .Times(1)
220 .WillOnce(Return(&allow_3g_policy));
221
222 // This test tests multiple connection types being allowed, with
223 // 3G one among them.
224 set<string> allowed_set;
225 allowed_set.insert(cmut_.StringForConnectionType(kNetEthernet));
226 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
227 allowed_set.insert(cmut_.StringForConnectionType(kNetWifi));
228
229 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
230 .Times(1)
231 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
232
233 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular));
234}
235
236TEST_F(ConnectionManagerTest, BlockUpdatesOver3GByDefaultTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800237 EXPECT_CALL(mock_system_state_, device_policy()).Times(1);
Jay Srinivasan43488792012-06-19 00:25:31 -0700238 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular));
239}
240
241TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
242 policy::MockDevicePolicy block_3g_policy;
243
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800244 EXPECT_CALL(mock_system_state_, device_policy())
Jay Srinivasan43488792012-06-19 00:25:31 -0700245 .Times(1)
246 .WillOnce(Return(&block_3g_policy));
247
248 // Test that updates for 3G are blocked while updates are allowed
249 // over several other types.
250 set<string> allowed_set;
251 allowed_set.insert(cmut_.StringForConnectionType(kNetEthernet));
252 allowed_set.insert(cmut_.StringForConnectionType(kNetWifi));
253 allowed_set.insert(cmut_.StringForConnectionType(kNetWimax));
254
255 EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
256 .Times(1)
257 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
258
259 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular));
260}
261
262TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) {
263 policy::MockDevicePolicy allow_3g_policy;
264
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800265 EXPECT_CALL(mock_system_state_, device_policy())
Jay Srinivasan43488792012-06-19 00:25:31 -0700266 .Times(1)
267 .WillOnce(Return(&allow_3g_policy));
268
269 set<string> allowed_set;
270 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
271
272 // Return false for GetAllowedConnectionTypesForUpdate and see
273 // that updates are still blocked for 3G despite the value being in
274 // the string set above.
275 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
276 .Times(1)
277 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(false)));
278
279 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular));
280}
281
282TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
283 EXPECT_STREQ(flimflam::kTypeEthernet,
284 cmut_.StringForConnectionType(kNetEthernet));
285 EXPECT_STREQ(flimflam::kTypeWifi,
286 cmut_.StringForConnectionType(kNetWifi));
287 EXPECT_STREQ(flimflam::kTypeWimax,
288 cmut_.StringForConnectionType(kNetWimax));
289 EXPECT_STREQ(flimflam::kTypeBluetooth,
290 cmut_.StringForConnectionType(kNetBluetooth));
291 EXPECT_STREQ(flimflam::kTypeCellular,
292 cmut_.StringForConnectionType(kNetCellular));
293 EXPECT_STREQ("Unknown",
294 cmut_.StringForConnectionType(kNetUnknown));
295 EXPECT_STREQ("Unknown",
296 cmut_.StringForConnectionType(
297 static_cast<NetworkConnectionType>(999999)));
298}
299
300TEST_F(ConnectionManagerTest, MalformedServiceList) {
301 SetupMocks("/service/guest-network");
302 string service_name(kServicePath_);
303 SetManagerReply(&service_name, DBUS_TYPE_G_STRING_ARRAY);
304
305 NetworkConnectionType type;
306 EXPECT_FALSE(cmut_.GetConnectionType(&dbus_iface_, &type));
307}
308
309} // namespace chromeos_update_engine