Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2010 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 <gtest/gtest.h> |
| 7 | |
| 8 | #include "update_engine/flimflam_proxy.h" |
| 9 | #include "update_engine/mock_dbus_interface.h" |
| 10 | |
| 11 | using ::testing::_; |
| 12 | using ::testing::Return; |
| 13 | using ::testing::SetArgumentPointee; |
| 14 | using ::testing::StrEq; |
| 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
| 18 | template<typename T, void F(T)> |
| 19 | class ScopedRelease { |
| 20 | public: |
| 21 | ScopedRelease(T obj) : obj_(obj) {} |
| 22 | ~ScopedRelease() { |
| 23 | F(obj_); |
| 24 | } |
| 25 | |
| 26 | private: |
| 27 | T obj_; |
| 28 | }; |
| 29 | |
| 30 | class FlimFlamProxyTest : public ::testing::Test { |
| 31 | protected: |
| 32 | void TestWithServiceType( |
| 33 | const char* service_type, NetworkConnectionType expected_type); |
| 34 | }; |
| 35 | |
| 36 | void FlimFlamProxyTest::TestWithServiceType( |
| 37 | const char* service_type, |
| 38 | NetworkConnectionType expected_type) { |
| 39 | int number = 1; |
| 40 | DBusGConnection* kMockSystemBus = |
| 41 | reinterpret_cast<DBusGConnection*>(number++); |
| 42 | DBusGProxy* kMockFlimFlamManagerProxy = |
| 43 | reinterpret_cast<DBusGProxy*>(number++); |
| 44 | DBusGProxy* kMockFlimFlamServiceProxy = |
| 45 | reinterpret_cast<DBusGProxy*>(number++); |
| 46 | ASSERT_NE(kMockSystemBus, reinterpret_cast<DBusGConnection*>(NULL)); |
| 47 | const char* kServicePath = "/foo/service"; |
| 48 | const char kGetPropertiesMethod[] = "GetProperties"; |
| 49 | |
| 50 | MockDbusGlib dbus_iface; |
| 51 | |
| 52 | EXPECT_CALL(dbus_iface, |
| 53 | ProxyNewForNameOwner(kMockSystemBus, |
| 54 | StrEq(kFlimFlamDbusService), |
| 55 | StrEq(kFlimFlamDbusManagerPath), |
| 56 | StrEq(kFlimFlamDbusManagerInterface), |
| 57 | _)) |
| 58 | .WillOnce(Return(kMockFlimFlamManagerProxy)); |
| 59 | EXPECT_CALL(dbus_iface, |
| 60 | ProxyNewForNameOwner(kMockSystemBus, |
| 61 | StrEq(kFlimFlamDbusService), |
| 62 | StrEq(kServicePath), |
| 63 | StrEq(kFlimFlamDbusServiceInterface), |
| 64 | _)) |
| 65 | .WillOnce(Return(kMockFlimFlamServiceProxy)); |
| 66 | |
| 67 | EXPECT_CALL(dbus_iface, ProxyUnref(kMockFlimFlamManagerProxy)); |
| 68 | EXPECT_CALL(dbus_iface, ProxyUnref(kMockFlimFlamServiceProxy)); |
| 69 | |
| 70 | EXPECT_CALL(dbus_iface, BusGet(DBUS_BUS_SYSTEM, _)) |
| 71 | .Times(2) |
| 72 | .WillRepeatedly(Return(kMockSystemBus)); |
| 73 | |
| 74 | // Set up return value for first dbus call (to Manager object). |
| 75 | GHashTable* manager_hash_table = g_hash_table_new(g_str_hash, g_str_equal); |
| 76 | ScopedRelease<GHashTable*, g_hash_table_unref> manager_hash_table_unref( |
| 77 | manager_hash_table); |
| 78 | |
| 79 | GArray* array = g_array_new(FALSE, FALSE, sizeof(const char*)); |
| 80 | ASSERT_TRUE(array != NULL); |
| 81 | |
| 82 | EXPECT_EQ(array, g_array_append_val(array, kServicePath)); |
| 83 | GValue array_value = {0, {{0}}}; |
| 84 | EXPECT_EQ(&array_value, g_value_init(&array_value, G_TYPE_ARRAY)); |
| 85 | g_value_take_boxed(&array_value, array); |
| 86 | g_hash_table_insert(manager_hash_table, |
| 87 | const_cast<char*>("Services"), |
| 88 | &array_value); |
| 89 | |
| 90 | // Set up return value for second dbus call (to Service object). |
| 91 | |
| 92 | GHashTable* service_hash_table = g_hash_table_new(g_str_hash, g_str_equal); |
| 93 | ScopedRelease<GHashTable*, g_hash_table_unref> service_hash_table_unref( |
| 94 | service_hash_table); |
| 95 | |
| 96 | GValue service_type_value = {0, {{0}}}; |
| 97 | EXPECT_EQ(&service_type_value, |
| 98 | g_value_init(&service_type_value, G_TYPE_STRING)); |
| 99 | g_value_set_static_string(&service_type_value, service_type); |
| 100 | |
| 101 | g_hash_table_insert(service_hash_table, |
| 102 | const_cast<char*>("Type"), |
| 103 | &service_type_value); |
| 104 | |
| 105 | EXPECT_CALL(dbus_iface, ProxyCall(kMockFlimFlamManagerProxy, |
| 106 | StrEq(kGetPropertiesMethod), |
| 107 | _, |
| 108 | G_TYPE_INVALID, |
| 109 | dbus_g_type_get_map("GHashTable", |
| 110 | G_TYPE_STRING, |
| 111 | G_TYPE_VALUE), |
| 112 | _, |
| 113 | G_TYPE_INVALID)) |
| 114 | .WillOnce(DoAll(SetArgumentPointee<5>(manager_hash_table), Return(TRUE))); |
| 115 | |
| 116 | EXPECT_CALL(dbus_iface, ProxyCall(kMockFlimFlamServiceProxy, |
| 117 | StrEq(kGetPropertiesMethod), |
| 118 | _, |
| 119 | G_TYPE_INVALID, |
| 120 | dbus_g_type_get_map("GHashTable", |
| 121 | G_TYPE_STRING, |
| 122 | G_TYPE_VALUE), |
| 123 | _, |
| 124 | G_TYPE_INVALID)) |
| 125 | .WillOnce(DoAll(SetArgumentPointee<5>(service_hash_table), Return(TRUE))); |
| 126 | |
| 127 | NetworkConnectionType type; |
| 128 | |
| 129 | EXPECT_TRUE(FlimFlamProxy::GetConnectionType(&dbus_iface, &type)); |
| 130 | EXPECT_EQ(expected_type, type); |
| 131 | } |
| 132 | |
| 133 | TEST_F(FlimFlamProxyTest, SimpleTest) { |
| 134 | TestWithServiceType(kFlimFlamNetTypeEthernet, kNetEthernet); |
| 135 | TestWithServiceType(kFlimFlamNetTypeWifi, kNetWifi); |
| 136 | TestWithServiceType(kFlimFlamNetTypeWimax, kNetWimax); |
| 137 | TestWithServiceType(kFlimFlamNetTypeBluetooth, kNetBluetooth); |
| 138 | TestWithServiceType(kFlimFlamNetTypeCellular, kNetCellular); |
| 139 | } |
| 140 | |
| 141 | TEST_F(FlimFlamProxyTest, UnknownTest) { |
| 142 | TestWithServiceType("foo", kNetUnknown); |
| 143 | } |
| 144 | |
| 145 | TEST_F(FlimFlamProxyTest, ExpensiveConnectionsTest) { |
| 146 | EXPECT_FALSE(FlimFlamProxy::IsExpensiveConnectionType(kNetEthernet)); |
| 147 | EXPECT_FALSE(FlimFlamProxy::IsExpensiveConnectionType(kNetWifi)); |
| 148 | EXPECT_TRUE(FlimFlamProxy::IsExpensiveConnectionType(kNetWimax)); |
| 149 | EXPECT_TRUE(FlimFlamProxy::IsExpensiveConnectionType(kNetBluetooth)); |
| 150 | EXPECT_TRUE(FlimFlamProxy::IsExpensiveConnectionType(kNetCellular)); |
| 151 | } |
| 152 | |
| 153 | TEST_F(FlimFlamProxyTest, StringForConnectionTypeTest) { |
| 154 | EXPECT_EQ(kFlimFlamNetTypeEthernet, |
| 155 | FlimFlamProxy::StringForConnectionType(kNetEthernet)); |
| 156 | EXPECT_EQ(kFlimFlamNetTypeWifi, |
| 157 | FlimFlamProxy::StringForConnectionType(kNetWifi)); |
| 158 | EXPECT_EQ(kFlimFlamNetTypeWimax, |
| 159 | FlimFlamProxy::StringForConnectionType(kNetWimax)); |
| 160 | EXPECT_EQ(kFlimFlamNetTypeBluetooth, |
| 161 | FlimFlamProxy::StringForConnectionType(kNetBluetooth)); |
| 162 | EXPECT_EQ(kFlimFlamNetTypeCellular, |
| 163 | FlimFlamProxy::StringForConnectionType(kNetCellular)); |
| 164 | EXPECT_EQ("Unknown", FlimFlamProxy::StringForConnectionType(kNetUnknown)); |
| 165 | EXPECT_EQ("Unknown", FlimFlamProxy::StringForConnectionType( |
| 166 | static_cast<NetworkConnectionType>(999999))); |
| 167 | } |
| 168 | |
| 169 | } // namespace chromeos_update_engine |