OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium 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 "content/browser/geolocation/wifi_data_provider_linux.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "dbus/message.h" |
| 16 #include "dbus/mock_bus.h" |
| 17 #include "dbus/mock_object_proxy.h" |
| 18 #include "dbus/object_path.h" |
| 19 #include "dbus/object_proxy.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 using ::testing::_; |
| 24 using ::testing::Invoke; |
| 25 using ::testing::Return; |
| 26 using ::testing::Unused; |
| 27 |
| 28 namespace content { |
| 29 |
| 30 class GeolocationWifiDataProviderLinuxTest : public testing::Test { |
| 31 void SetUp() override { |
| 32 // Create a mock bus. |
| 33 dbus::Bus::Options options; |
| 34 options.bus_type = dbus::Bus::SYSTEM; |
| 35 mock_bus_ = new dbus::MockBus(options); |
| 36 |
| 37 // Create a mock proxy that behaves as NetworkManager. |
| 38 mock_network_manager_proxy_ = |
| 39 new dbus::MockObjectProxy( |
| 40 mock_bus_.get(), |
| 41 "org.freedesktop.NetworkManager", |
| 42 dbus::ObjectPath("/org/freedesktop/NetworkManager")); |
| 43 // Set an expectation so mock_network_manager_proxy_'s |
| 44 // CallMethodAndBlock() will use CreateNetworkManagerProxyResponse() |
| 45 // to return responses. |
| 46 EXPECT_CALL(*mock_network_manager_proxy_.get(), |
| 47 MockCallMethodAndBlock(_, _)) |
| 48 .WillRepeatedly(Invoke(this, |
| 49 &GeolocationWifiDataProviderLinuxTest:: |
| 50 CreateNetworkManagerProxyResponse)); |
| 51 |
| 52 // Create a mock proxy that behaves as NetworkManager/Devices/0. |
| 53 mock_device_proxy_ = |
| 54 new dbus::MockObjectProxy( |
| 55 mock_bus_.get(), |
| 56 "org.freedesktop.NetworkManager", |
| 57 dbus::ObjectPath("/org/freedesktop/NetworkManager/Devices/0")); |
| 58 EXPECT_CALL(*mock_device_proxy_.get(), MockCallMethodAndBlock(_, _)) |
| 59 .WillRepeatedly(Invoke( |
| 60 this, |
| 61 &GeolocationWifiDataProviderLinuxTest::CreateDeviceProxyResponse)); |
| 62 |
| 63 // Create a mock proxy that behaves as NetworkManager/AccessPoint/0. |
| 64 mock_access_point_proxy_ = |
| 65 new dbus::MockObjectProxy( |
| 66 mock_bus_.get(), |
| 67 "org.freedesktop.NetworkManager", |
| 68 dbus::ObjectPath("/org/freedesktop/NetworkManager/AccessPoint/0")); |
| 69 EXPECT_CALL(*mock_access_point_proxy_.get(), MockCallMethodAndBlock(_, _)) |
| 70 .WillRepeatedly(Invoke(this, |
| 71 &GeolocationWifiDataProviderLinuxTest:: |
| 72 CreateAccessPointProxyResponse)); |
| 73 |
| 74 // Set an expectation so mock_bus_'s GetObjectProxy() for the given |
| 75 // service name and the object path will return |
| 76 // mock_network_manager_proxy_. |
| 77 EXPECT_CALL( |
| 78 *mock_bus_.get(), |
| 79 GetObjectProxy("org.freedesktop.NetworkManager", |
| 80 dbus::ObjectPath("/org/freedesktop/NetworkManager"))) |
| 81 .WillOnce(Return(mock_network_manager_proxy_.get())); |
| 82 // Likewise, set an expectation for mock_device_proxy_. |
| 83 EXPECT_CALL( |
| 84 *mock_bus_.get(), |
| 85 GetObjectProxy( |
| 86 "org.freedesktop.NetworkManager", |
| 87 dbus::ObjectPath("/org/freedesktop/NetworkManager/Devices/0"))) |
| 88 .WillOnce(Return(mock_device_proxy_.get())) |
| 89 .WillOnce(Return(mock_device_proxy_.get())); |
| 90 // Likewise, set an expectation for mock_access_point_proxy_. |
| 91 EXPECT_CALL( |
| 92 *mock_bus_.get(), |
| 93 GetObjectProxy( |
| 94 "org.freedesktop.NetworkManager", |
| 95 dbus::ObjectPath("/org/freedesktop/NetworkManager/AccessPoint/0"))) |
| 96 .WillOnce(Return(mock_access_point_proxy_.get())); |
| 97 |
| 98 // ShutdownAndBlock() should be called. |
| 99 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); |
| 100 |
| 101 // Create the wlan API with the mock bus object injected. |
| 102 wifi_provider_linux_ = new WifiDataProviderLinux; |
| 103 wlan_api_.reset( |
| 104 wifi_provider_linux_->NewWlanApiForTesting(mock_bus_.get())); |
| 105 ASSERT_TRUE(wlan_api_.get()); |
| 106 } |
| 107 |
| 108 protected: |
| 109 // WifiDataProvider requires a task runner to be present. The |thread_bundle_| |
| 110 // is defined here, as it should outlive |wifi_provider_linux_|. |
| 111 TestBrowserThreadBundle thread_bundle_; |
| 112 scoped_refptr<dbus::MockBus> mock_bus_; |
| 113 scoped_refptr<dbus::MockObjectProxy> mock_network_manager_proxy_; |
| 114 scoped_refptr<dbus::MockObjectProxy> mock_access_point_proxy_; |
| 115 scoped_refptr<dbus::MockObjectProxy> mock_device_proxy_; |
| 116 scoped_refptr<WifiDataProviderLinux> wifi_provider_linux_; |
| 117 std::unique_ptr<WifiDataProviderCommon::WlanApiInterface> wlan_api_; |
| 118 |
| 119 private: |
| 120 // Creates a response for |mock_network_manager_proxy_|. |
| 121 dbus::Response* CreateNetworkManagerProxyResponse( |
| 122 dbus::MethodCall* method_call, |
| 123 Unused) { |
| 124 if (method_call->GetInterface() == "org.freedesktop.NetworkManager" && |
| 125 method_call->GetMember() == "GetDevices") { |
| 126 // The list of devices is asked. Return the object path. |
| 127 std::vector<dbus::ObjectPath> object_paths; |
| 128 object_paths.push_back( |
| 129 dbus::ObjectPath("/org/freedesktop/NetworkManager/Devices/0")); |
| 130 |
| 131 std::unique_ptr<dbus::Response> response = dbus::Response::CreateEmpty(); |
| 132 dbus::MessageWriter writer(response.get()); |
| 133 writer.AppendArrayOfObjectPaths(object_paths); |
| 134 return response.release(); |
| 135 } |
| 136 |
| 137 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 138 return NULL; |
| 139 } |
| 140 |
| 141 // Creates a response for |mock_device_proxy_|. |
| 142 dbus::Response* CreateDeviceProxyResponse(dbus::MethodCall* method_call, |
| 143 Unused) { |
| 144 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && |
| 145 method_call->GetMember() == "Get") { |
| 146 dbus::MessageReader reader(method_call); |
| 147 std::string interface_name; |
| 148 std::string property_name; |
| 149 if (reader.PopString(&interface_name) && |
| 150 reader.PopString(&property_name)) { |
| 151 // The device type is asked. Respond that the device type is wifi. |
| 152 std::unique_ptr<dbus::Response> response = |
| 153 dbus::Response::CreateEmpty(); |
| 154 dbus::MessageWriter writer(response.get()); |
| 155 // This matches NM_DEVICE_TYPE_WIFI in wifi_data_provider_linux.cc. |
| 156 const int kDeviceTypeWifi = 2; |
| 157 writer.AppendVariantOfUint32(kDeviceTypeWifi); |
| 158 return response.release(); |
| 159 } |
| 160 } else if (method_call->GetInterface() == |
| 161 "org.freedesktop.NetworkManager.Device.Wireless" && |
| 162 method_call->GetMember() == "GetAccessPoints") { |
| 163 // The list of access points is asked. Return the object path. |
| 164 std::unique_ptr<dbus::Response> response = dbus::Response::CreateEmpty(); |
| 165 dbus::MessageWriter writer(response.get()); |
| 166 std::vector<dbus::ObjectPath> object_paths; |
| 167 object_paths.push_back( |
| 168 dbus::ObjectPath("/org/freedesktop/NetworkManager/AccessPoint/0")); |
| 169 writer.AppendArrayOfObjectPaths(object_paths); |
| 170 return response.release(); |
| 171 } |
| 172 |
| 173 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 174 return NULL; |
| 175 } |
| 176 |
| 177 |
| 178 // Creates a response for |mock_access_point_proxy_|. |
| 179 dbus::Response* CreateAccessPointProxyResponse(dbus::MethodCall* method_call, |
| 180 Unused) { |
| 181 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && |
| 182 method_call->GetMember() == "Get") { |
| 183 dbus::MessageReader reader(method_call); |
| 184 |
| 185 std::string interface_name; |
| 186 std::string property_name; |
| 187 if (reader.PopString(&interface_name) && |
| 188 reader.PopString(&property_name)) { |
| 189 std::unique_ptr<dbus::Response> response = |
| 190 dbus::Response::CreateEmpty(); |
| 191 dbus::MessageWriter writer(response.get()); |
| 192 |
| 193 if (property_name == "Ssid") { |
| 194 const uint8_t kSsid[] = {0x74, 0x65, 0x73, 0x74}; // "test" |
| 195 dbus::MessageWriter variant_writer(response.get()); |
| 196 writer.OpenVariant("ay", &variant_writer); |
| 197 variant_writer.AppendArrayOfBytes(kSsid, arraysize(kSsid)); |
| 198 writer.CloseContainer(&variant_writer); |
| 199 } else if (property_name == "HwAddress") { |
| 200 // This will be converted to "00-11-22-33-44-55". |
| 201 const std::string kMacAddress = "00:11:22:33:44:55"; |
| 202 writer.AppendVariantOfString(kMacAddress); |
| 203 } else if (property_name == "Strength") { |
| 204 // This will be converted to -50. |
| 205 const uint8_t kStrength = 100; |
| 206 writer.AppendVariantOfByte(kStrength); |
| 207 } else if (property_name == "Frequency") { |
| 208 // This will be converted to channel 4. |
| 209 const uint32_t kFrequency = 2427; |
| 210 writer.AppendVariantOfUint32(kFrequency); |
| 211 } |
| 212 return response.release(); |
| 213 } |
| 214 } |
| 215 |
| 216 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 217 return NULL; |
| 218 } |
| 219 }; |
| 220 |
| 221 TEST_F(GeolocationWifiDataProviderLinuxTest, GetAccessPointData) { |
| 222 WifiData::AccessPointDataSet access_point_data_set; |
| 223 ASSERT_TRUE(wlan_api_->GetAccessPointData(&access_point_data_set)); |
| 224 |
| 225 ASSERT_EQ(1U, access_point_data_set.size()); |
| 226 AccessPointData access_point_data = *access_point_data_set.begin(); |
| 227 |
| 228 // Check the contents of the access point data. |
| 229 // The expected values come from CreateAccessPointProxyResponse() above. |
| 230 EXPECT_EQ("test", base::UTF16ToUTF8(access_point_data.ssid)); |
| 231 EXPECT_EQ("00-11-22-33-44-55", |
| 232 base::UTF16ToUTF8(access_point_data.mac_address)); |
| 233 EXPECT_EQ(-50, access_point_data.radio_signal_strength); |
| 234 EXPECT_EQ(4, access_point_data.channel); |
| 235 } |
| 236 |
| 237 } // namespace content |
OLD | NEW |