Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 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 "shill/ethernet.h" |
| 6 | |
| 7 | #include <netinet/ether.h> |
| 8 | #include <linux/if.h> // Needs definitions from netinet/ether.h |
| 9 | |
| 10 | #include <base/memory/ref_counted.h> |
| 11 | #include <base/memory/scoped_ptr.h> |
| 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
| 15 | #include "shill/mock_device_info.h" |
| 16 | #include "shill/mock_dhcp_config.h" |
| 17 | #include "shill/mock_dhcp_provider.h" |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 18 | #include "shill/mock_eap_credentials.h" |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 19 | #include "shill/mock_eap_listener.h" |
| 20 | #include "shill/mock_event_dispatcher.h" |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 21 | #include "shill/mock_ethernet_eap_provider.h" |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 22 | #include "shill/mock_ethernet_service.h" |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 23 | #include "shill/mock_glib.h" |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 24 | #include "shill/mock_log.h" |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 25 | #include "shill/mock_manager.h" |
| 26 | #include "shill/mock_metrics.h" |
| 27 | #include "shill/mock_rtnl_handler.h" |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 28 | #include "shill/mock_service.h" |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 29 | #include "shill/mock_supplicant_interface_proxy.h" |
| 30 | #include "shill/mock_supplicant_process_proxy.h" |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 31 | #include "shill/nice_mock_control.h" |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 32 | #include "shill/proxy_factory.h" |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 33 | |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 34 | using std::string; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 35 | using testing::_; |
| 36 | using testing::AnyNumber; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 37 | using testing::EndsWith; |
Gaurav Shah | 25ee250 | 2013-04-08 17:55:01 -0700 | [diff] [blame] | 38 | using testing::Eq; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 39 | using testing::Mock; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 40 | using testing::NiceMock; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 41 | using testing::Return; |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 42 | using testing::ReturnRef; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 43 | using testing::StrEq; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 44 | using testing::StrictMock; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 45 | using testing::Throw; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 46 | |
| 47 | namespace shill { |
| 48 | |
| 49 | class EthernetTest : public testing::Test { |
| 50 | public: |
| 51 | EthernetTest() |
| 52 | : metrics_(NULL), |
| 53 | manager_(&control_interface_, NULL, &metrics_, &glib_), |
| 54 | device_info_(&control_interface_, &dispatcher_, &metrics_, &manager_), |
| 55 | ethernet_(new Ethernet(&control_interface_, |
| 56 | &dispatcher_, |
| 57 | &metrics_, |
| 58 | &manager_, |
| 59 | kDeviceName, |
| 60 | kDeviceAddress, |
| 61 | kInterfaceIndex)), |
| 62 | dhcp_config_(new MockDHCPConfig(&control_interface_, |
| 63 | kDeviceName)), |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 64 | eap_listener_(new MockEapListener()), |
| 65 | mock_service_(new MockEthernetService(&control_interface_, &metrics_)), |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 66 | mock_eap_service_(new MockService(&control_interface_, |
| 67 | &dispatcher_, |
| 68 | &metrics_, |
| 69 | &manager_)), |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 70 | proxy_factory_(this), |
| 71 | supplicant_interface_proxy_( |
| 72 | new NiceMock<MockSupplicantInterfaceProxy>()), |
| 73 | supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()) {} |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 74 | |
| 75 | virtual void SetUp() { |
| 76 | ethernet_->rtnl_handler_ = &rtnl_handler_; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 77 | ethernet_->proxy_factory_ = &proxy_factory_; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 78 | // Transfers ownership. |
| 79 | ethernet_->eap_listener_.reset(eap_listener_); |
| 80 | |
| 81 | ethernet_->set_dhcp_provider(&dhcp_provider_); |
| 82 | ON_CALL(manager_, device_info()).WillByDefault(Return(&device_info_)); |
| 83 | EXPECT_CALL(manager_, UpdateEnabledTechnologies()).Times(AnyNumber()); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 84 | EXPECT_CALL(manager_, ethernet_eap_provider()) |
| 85 | .WillRepeatedly(Return(ðernet_eap_provider_)); |
| 86 | ethernet_eap_provider_.set_service(mock_eap_service_); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | virtual void TearDown() { |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 90 | ethernet_eap_provider_.set_service(NULL); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 91 | ethernet_->set_dhcp_provider(NULL); |
| 92 | ethernet_->eap_listener_.reset(); |
| 93 | } |
| 94 | |
| 95 | protected: |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 96 | class TestProxyFactory : public ProxyFactory { |
| 97 | public: |
| 98 | explicit TestProxyFactory(EthernetTest *test) : test_(test) {} |
| 99 | |
| 100 | virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy( |
| 101 | const char */*dbus_path*/, const char */*dbus_addr*/) { |
| 102 | return test_->supplicant_process_proxy_.release(); |
| 103 | } |
| 104 | |
| 105 | virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy( |
| 106 | SupplicantEventDelegateInterface */*delegate*/, |
| 107 | const DBus::Path &/*object_path*/, |
| 108 | const char */*dbus_addr*/) { |
| 109 | return test_->supplicant_interface_proxy_.release(); |
| 110 | } |
| 111 | |
| 112 | MOCK_METHOD2(CreateSupplicantNetworkProxy, |
| 113 | SupplicantNetworkProxyInterface *( |
| 114 | const DBus::Path &object_path, |
| 115 | const char *dbus_addr)); |
| 116 | |
| 117 | private: |
| 118 | EthernetTest *test_; |
| 119 | }; |
| 120 | |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 121 | static const char kDeviceName[]; |
| 122 | static const char kDeviceAddress[]; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 123 | static const char kInterfacePath[]; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 124 | static const int kInterfaceIndex; |
| 125 | |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 126 | bool GetIsEapAuthenticated() { return ethernet_->is_eap_authenticated_; } |
| 127 | void SetIsEapAuthenticated(bool is_eap_authenticated) { |
| 128 | ethernet_->is_eap_authenticated_ = is_eap_authenticated; |
| 129 | } |
| 130 | bool GetIsEapDetected() { return ethernet_->is_eap_detected_; } |
| 131 | void SetIsEapDetected(bool is_eap_detected) { |
| 132 | ethernet_->is_eap_detected_ = is_eap_detected; |
| 133 | } |
| 134 | bool GetLinkUp() { return ethernet_->link_up_; } |
| 135 | const ServiceRefPtr &GetSelectedService() { |
| 136 | return ethernet_->selected_service(); |
| 137 | } |
| 138 | ServiceRefPtr GetService() { return ethernet_->service_; } |
| 139 | void SetService(const EthernetServiceRefPtr &service) { |
| 140 | ethernet_->service_ = service; |
| 141 | } |
| 142 | const PropertyStore &GetStore() { return ethernet_->store(); } |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 143 | void StartEthernet() { |
| 144 | EXPECT_CALL(rtnl_handler_, |
| 145 | SetInterfaceFlags(kInterfaceIndex, IFF_UP, IFF_UP)); |
| 146 | ethernet_->Start(NULL, EnabledStateChangedCallback()); |
| 147 | } |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 148 | const SupplicantInterfaceProxyInterface *GetSupplicantInterfaceProxy() { |
| 149 | return ethernet_->supplicant_interface_proxy_.get(); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 150 | } |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 151 | const SupplicantProcessProxyInterface *GetSupplicantProcessProxy() { |
| 152 | return ethernet_->supplicant_process_proxy_.get(); |
| 153 | } |
| 154 | const string &GetSupplicantInterfacePath() { |
| 155 | return ethernet_->supplicant_interface_path_; |
| 156 | } |
| 157 | const string &GetSupplicantNetworkPath() { |
| 158 | return ethernet_->supplicant_network_path_; |
| 159 | } |
| 160 | void SetSupplicantNetworkPath(const string &network_path) { |
| 161 | ethernet_->supplicant_network_path_ = network_path; |
| 162 | } |
| 163 | bool InvokeStartSupplicant() { |
| 164 | return ethernet_->StartSupplicant(); |
| 165 | } |
| 166 | void InvokeStopSupplicant() { |
| 167 | return ethernet_->StopSupplicant(); |
| 168 | } |
| 169 | bool InvokeStartEapAuthentication() { |
| 170 | return ethernet_->StartEapAuthentication(); |
| 171 | } |
| 172 | void StartSupplicant() { |
| 173 | SupplicantInterfaceProxyInterface *interface = |
| 174 | supplicant_interface_proxy_.get(); |
| 175 | SupplicantProcessProxyInterface *process = supplicant_process_proxy_.get(); |
| 176 | EXPECT_CALL(*supplicant_process_proxy_.get(), CreateInterface(_)) |
| 177 | .WillOnce(Return(kInterfacePath)); |
| 178 | EXPECT_TRUE(InvokeStartSupplicant()); |
| 179 | EXPECT_EQ(interface, GetSupplicantInterfaceProxy()); |
| 180 | EXPECT_EQ(process, GetSupplicantProcessProxy()); |
| 181 | EXPECT_EQ(kInterfacePath, GetSupplicantInterfacePath()); |
| 182 | } |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 183 | void TriggerOnEapDetected() { ethernet_->OnEapDetected(); } |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 184 | void TriggerCertification(const string &subject, uint32 depth) { |
| 185 | ethernet_->CertificationTask(subject, depth); |
| 186 | } |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 187 | void TriggerTryEapAuthentication() { |
| 188 | ethernet_->TryEapAuthenticationTask(); |
| 189 | } |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 190 | |
| 191 | StrictMock<MockEventDispatcher> dispatcher_; |
| 192 | MockGLib glib_; |
| 193 | NiceMockControl control_interface_; |
| 194 | MockMetrics metrics_; |
| 195 | MockManager manager_; |
| 196 | MockDeviceInfo device_info_; |
| 197 | EthernetRefPtr ethernet_; |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 198 | MockEthernetEapProvider ethernet_eap_provider_; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 199 | MockDHCPProvider dhcp_provider_; |
| 200 | scoped_refptr<MockDHCPConfig> dhcp_config_; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 201 | |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 202 | // Owned by Ethernet instance, but tracked here for expectations. |
| 203 | MockEapListener *eap_listener_; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 204 | |
| 205 | MockRTNLHandler rtnl_handler_; |
| 206 | scoped_refptr<MockEthernetService> mock_service_; |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 207 | scoped_refptr<MockService> mock_eap_service_; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 208 | NiceMock<TestProxyFactory> proxy_factory_; |
| 209 | scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_; |
| 210 | scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | // static |
| 214 | const char EthernetTest::kDeviceName[] = "eth0"; |
| 215 | const char EthernetTest::kDeviceAddress[] = "000102030405"; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 216 | const char EthernetTest::kInterfacePath[] = "/interface/path"; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 217 | const int EthernetTest::kInterfaceIndex = 123; |
| 218 | |
| 219 | TEST_F(EthernetTest, Construct) { |
| 220 | EXPECT_FALSE(GetLinkUp()); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 221 | EXPECT_FALSE(GetIsEapAuthenticated()); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 222 | EXPECT_FALSE(GetIsEapDetected()); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 223 | EXPECT_TRUE(GetStore().Contains(kEapAuthenticationCompletedProperty)); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 224 | EXPECT_TRUE(GetStore().Contains(kEapAuthenticatorDetectedProperty)); |
| 225 | EXPECT_EQ(NULL, GetService().get()); |
| 226 | } |
| 227 | |
| 228 | TEST_F(EthernetTest, StartStop) { |
Gaurav Shah | 25ee250 | 2013-04-08 17:55:01 -0700 | [diff] [blame] | 229 | StartEthernet(); |
| 230 | |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 231 | EXPECT_FALSE(GetService().get() == NULL); |
| 232 | |
Gaurav Shah | 25ee250 | 2013-04-08 17:55:01 -0700 | [diff] [blame] | 233 | Service* service = GetService().get(); |
| 234 | EXPECT_CALL(manager_, DeregisterService(Eq(service))); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 235 | ethernet_->Stop(NULL, EnabledStateChangedCallback()); |
| 236 | EXPECT_EQ(NULL, GetService().get()); |
| 237 | } |
| 238 | |
| 239 | TEST_F(EthernetTest, LinkEvent) { |
| 240 | StartEthernet(); |
| 241 | |
| 242 | // Link-down event while already down. |
| 243 | EXPECT_CALL(manager_, DeregisterService(_)).Times(0); |
| 244 | EXPECT_CALL(*eap_listener_, Start()).Times(0); |
| 245 | ethernet_->LinkEvent(0, IFF_LOWER_UP); |
| 246 | EXPECT_FALSE(GetLinkUp()); |
| 247 | EXPECT_FALSE(GetIsEapDetected()); |
| 248 | Mock::VerifyAndClearExpectations(&manager_); |
| 249 | |
| 250 | // Link-up event while down. |
| 251 | EXPECT_CALL(manager_, RegisterService(GetService())); |
| 252 | EXPECT_CALL(*eap_listener_, Start()); |
| 253 | ethernet_->LinkEvent(IFF_LOWER_UP, 0); |
| 254 | EXPECT_TRUE(GetLinkUp()); |
| 255 | EXPECT_FALSE(GetIsEapDetected()); |
| 256 | Mock::VerifyAndClearExpectations(&manager_); |
| 257 | |
| 258 | // Link-up event while already up. |
| 259 | EXPECT_CALL(manager_, RegisterService(GetService())).Times(0); |
| 260 | EXPECT_CALL(*eap_listener_, Start()).Times(0); |
| 261 | ethernet_->LinkEvent(IFF_LOWER_UP, 0); |
| 262 | EXPECT_TRUE(GetLinkUp()); |
| 263 | EXPECT_FALSE(GetIsEapDetected()); |
| 264 | Mock::VerifyAndClearExpectations(&manager_); |
| 265 | |
| 266 | // Link-down event while up. |
| 267 | SetIsEapDetected(true); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 268 | // This is done in SetUp, but we have to reestablish this after calling |
| 269 | // VerifyAndClearExpectations() above. |
| 270 | EXPECT_CALL(manager_, ethernet_eap_provider()) |
| 271 | .WillRepeatedly(Return(ðernet_eap_provider_)); |
| 272 | EXPECT_CALL(ethernet_eap_provider_, |
| 273 | ClearCredentialChangeCallback(ethernet_.get())); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 274 | EXPECT_CALL(manager_, DeregisterService(GetService())); |
| 275 | EXPECT_CALL(*eap_listener_, Stop()); |
| 276 | ethernet_->LinkEvent(0, IFF_LOWER_UP); |
| 277 | EXPECT_FALSE(GetLinkUp()); |
| 278 | EXPECT_FALSE(GetIsEapDetected()); |
| 279 | |
| 280 | // Restore this expectation during shutdown. |
| 281 | EXPECT_CALL(manager_, UpdateEnabledTechnologies()).Times(AnyNumber()); |
| 282 | } |
| 283 | |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 284 | TEST_F(EthernetTest, ConnectToFailure) { |
| 285 | StartEthernet(); |
| 286 | SetService(mock_service_); |
| 287 | EXPECT_EQ(NULL, GetSelectedService().get()); |
| 288 | EXPECT_CALL(dhcp_provider_, CreateConfig(_, _, _, _)). |
| 289 | WillOnce(Return(dhcp_config_)); |
| 290 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).WillOnce(Return(false)); |
| 291 | EXPECT_CALL(dispatcher_, PostTask(_)); // Posts ConfigureStaticIPTask. |
| 292 | // Since we never called SelectService()... |
| 293 | EXPECT_CALL(*mock_service_, SetState(_)).Times(0); |
| 294 | ethernet_->ConnectTo(mock_service_); |
| 295 | EXPECT_EQ(NULL, GetSelectedService().get()); |
| 296 | } |
| 297 | |
| 298 | TEST_F(EthernetTest, ConnectToSuccess) { |
| 299 | StartEthernet(); |
| 300 | SetService(mock_service_); |
| 301 | EXPECT_EQ(NULL, GetSelectedService().get()); |
| 302 | EXPECT_CALL(dhcp_provider_, CreateConfig(_, _, _, _)). |
| 303 | WillOnce(Return(dhcp_config_)); |
| 304 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).WillOnce(Return(true)); |
| 305 | EXPECT_CALL(dispatcher_, PostTask(_)); // Posts ConfigureStaticIPTask. |
| 306 | EXPECT_CALL(*mock_service_, SetState(Service::kStateConfiguring)); |
| 307 | ethernet_->ConnectTo(mock_service_.get()); |
| 308 | EXPECT_EQ(GetService().get(), GetSelectedService().get()); |
| 309 | Mock::VerifyAndClearExpectations(mock_service_); |
| 310 | |
| 311 | EXPECT_CALL(*mock_service_, SetState(Service::kStateIdle)); |
| 312 | ethernet_->DisconnectFrom(mock_service_); |
| 313 | EXPECT_EQ(NULL, GetSelectedService().get()); |
| 314 | } |
| 315 | |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 316 | TEST_F(EthernetTest, OnEapDetected) { |
| 317 | EXPECT_FALSE(GetIsEapDetected()); |
| 318 | EXPECT_CALL(*eap_listener_, Stop()); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 319 | EXPECT_CALL(ethernet_eap_provider_, |
| 320 | SetCredentialChangeCallback(ethernet_.get(), _)); |
| 321 | EXPECT_CALL(dispatcher_, PostTask(_)); // Posts TryEapAuthenticationTask. |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 322 | TriggerOnEapDetected(); |
| 323 | EXPECT_TRUE(GetIsEapDetected()); |
| 324 | } |
| 325 | |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 326 | TEST_F(EthernetTest, TryEapAuthenticationNoService) { |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 327 | EXPECT_CALL(*mock_eap_service_, Is8021xConnectable()).Times(0); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 328 | NiceScopedMockLog log; |
| 329 | EXPECT_CALL(log, Log(logging::LOG_INFO, _, |
| 330 | EndsWith("Service is missing; " |
| 331 | "not doing EAP authentication."))); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 332 | TriggerTryEapAuthentication(); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | TEST_F(EthernetTest, TryEapAuthenticationNotConnectableNotAuthenticated) { |
| 336 | SetService(mock_service_); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 337 | EXPECT_CALL(*mock_eap_service_, Is8021xConnectable()).WillOnce(Return(false)); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 338 | NiceScopedMockLog log; |
| 339 | EXPECT_CALL(log, Log(logging::LOG_INFO, _, |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 340 | EndsWith("EAP Service lacks 802.1X credentials; " |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 341 | "not doing EAP authentication."))); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 342 | TriggerTryEapAuthentication(); |
| 343 | SetService(NULL); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | TEST_F(EthernetTest, TryEapAuthenticationNotConnectableAuthenticated) { |
| 347 | SetService(mock_service_); |
| 348 | SetIsEapAuthenticated(true); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 349 | EXPECT_CALL(*mock_eap_service_, Is8021xConnectable()).WillOnce(Return(false)); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 350 | NiceScopedMockLog log; |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 351 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 352 | EXPECT_CALL(log, Log(logging::LOG_INFO, _, |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 353 | EndsWith("EAP Service lost 802.1X credentials; " |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 354 | "terminating EAP authentication."))); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 355 | TriggerTryEapAuthentication(); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 356 | EXPECT_FALSE(GetIsEapAuthenticated()); |
| 357 | } |
| 358 | |
| 359 | TEST_F(EthernetTest, TryEapAuthenticationEapNotDetected) { |
| 360 | SetService(mock_service_); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 361 | EXPECT_CALL(*mock_eap_service_, Is8021xConnectable()).WillOnce(Return(true)); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 362 | NiceScopedMockLog log; |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 363 | EXPECT_CALL(log, Log(logging::LOG_WARNING, _, |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 364 | EndsWith("EAP authenticator not detected; " |
| 365 | "not doing EAP authentication."))); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 366 | TriggerTryEapAuthentication(); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | TEST_F(EthernetTest, StartSupplicant) { |
| 370 | // Save the mock proxy pointers before the Ethernet instance accepts it. |
| 371 | MockSupplicantInterfaceProxy *interface_proxy = |
| 372 | supplicant_interface_proxy_.get(); |
| 373 | MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get(); |
| 374 | |
| 375 | StartSupplicant(); |
| 376 | |
| 377 | // Starting it again should not invoke another call to create an interface. |
| 378 | Mock::VerifyAndClearExpectations(process_proxy); |
| 379 | EXPECT_CALL(*process_proxy, CreateInterface(_)).Times(0); |
| 380 | EXPECT_TRUE(InvokeStartSupplicant()); |
| 381 | |
| 382 | // Also, the mock pointers should remain; if the TestProxyFactory was |
| 383 | // invoked again, they would be NULL. |
| 384 | EXPECT_EQ(interface_proxy, GetSupplicantInterfaceProxy()); |
| 385 | EXPECT_EQ(process_proxy, GetSupplicantProcessProxy()); |
| 386 | EXPECT_EQ(kInterfacePath, GetSupplicantInterfacePath()); |
| 387 | } |
| 388 | |
| 389 | TEST_F(EthernetTest, StartSupplicantWithInterfaceExistsException) { |
| 390 | MockSupplicantInterfaceProxy *interface_proxy = |
| 391 | supplicant_interface_proxy_.get(); |
| 392 | MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get(); |
| 393 | EXPECT_CALL(*process_proxy, CreateInterface(_)) |
| 394 | .WillOnce(Throw(DBus::Error( |
| 395 | "fi.w1.wpa_supplicant1.InterfaceExists", |
| 396 | "test threw fi.w1.wpa_supplicant1.InterfaceExists"))); |
| 397 | EXPECT_CALL(*process_proxy, GetInterface(kDeviceName)) |
| 398 | .WillOnce(Return(kInterfacePath)); |
| 399 | EXPECT_TRUE(InvokeStartSupplicant()); |
| 400 | EXPECT_EQ(interface_proxy, GetSupplicantInterfaceProxy()); |
| 401 | EXPECT_EQ(process_proxy, GetSupplicantProcessProxy()); |
| 402 | EXPECT_EQ(kInterfacePath, GetSupplicantInterfacePath()); |
| 403 | } |
| 404 | |
| 405 | TEST_F(EthernetTest, StartSupplicantWithUnknownException) { |
| 406 | MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get(); |
| 407 | EXPECT_CALL(*process_proxy, CreateInterface(_)) |
| 408 | .WillOnce(Throw(DBus::Error( |
| 409 | "fi.w1.wpa_supplicant1.UnknownError", |
| 410 | "test threw fi.w1.wpa_supplicant1.UnknownError"))); |
| 411 | EXPECT_CALL(*process_proxy, GetInterface(kDeviceName)).Times(0); |
| 412 | EXPECT_FALSE(InvokeStartSupplicant()); |
| 413 | EXPECT_EQ(NULL, GetSupplicantInterfaceProxy()); |
| 414 | EXPECT_EQ(NULL, GetSupplicantProcessProxy()); |
| 415 | EXPECT_EQ("", GetSupplicantInterfacePath()); |
| 416 | } |
| 417 | |
| 418 | TEST_F(EthernetTest, StartEapAuthentication) { |
| 419 | MockSupplicantInterfaceProxy *interface_proxy = |
| 420 | supplicant_interface_proxy_.get(); |
| 421 | |
| 422 | StartSupplicant(); |
| 423 | SetService(mock_service_); |
| 424 | |
| 425 | EXPECT_CALL(*mock_service_, ClearEAPCertification()); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 426 | MockEapCredentials mock_eap_credentials; |
| 427 | EXPECT_CALL(*mock_eap_service_, eap()) |
| 428 | .WillOnce(Return(&mock_eap_credentials)); |
| 429 | EXPECT_CALL(mock_eap_credentials, PopulateSupplicantProperties(_, _, _, _)); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 430 | EXPECT_CALL(*interface_proxy, RemoveNetwork(_)).Times(0); |
| 431 | EXPECT_CALL(*interface_proxy, AddNetwork(_)) |
| 432 | .WillOnce(Throw(DBus::Error( |
| 433 | "fi.w1.wpa_supplicant1.UnknownError", |
| 434 | "test threw fi.w1.wpa_supplicant1.UnknownError"))); |
| 435 | EXPECT_CALL(*interface_proxy, SelectNetwork(_)).Times(0); |
Paul Stewart | be9abfd | 2013-04-22 12:18:48 -0700 | [diff] [blame] | 436 | EXPECT_CALL(*interface_proxy, EAPLogon()).Times(0); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 437 | EXPECT_FALSE(InvokeStartEapAuthentication()); |
| 438 | Mock::VerifyAndClearExpectations(mock_service_); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 439 | Mock::VerifyAndClearExpectations(mock_eap_service_); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 440 | Mock::VerifyAndClearExpectations(interface_proxy); |
| 441 | EXPECT_EQ("", GetSupplicantNetworkPath()); |
| 442 | |
| 443 | EXPECT_CALL(*mock_service_, ClearEAPCertification()); |
| 444 | EXPECT_CALL(*interface_proxy, RemoveNetwork(_)).Times(0); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 445 | EXPECT_CALL(*mock_eap_service_, eap()) |
| 446 | .WillOnce(Return(&mock_eap_credentials)); |
| 447 | EXPECT_CALL(mock_eap_credentials, PopulateSupplicantProperties(_, _, _, _)); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 448 | const char kFirstNetworkPath[] = "/network/first-path"; |
| 449 | EXPECT_CALL(*interface_proxy, AddNetwork(_)) |
| 450 | .WillOnce(Return(kFirstNetworkPath)); |
| 451 | EXPECT_CALL(*interface_proxy, SelectNetwork(StrEq(kFirstNetworkPath))); |
Paul Stewart | be9abfd | 2013-04-22 12:18:48 -0700 | [diff] [blame] | 452 | EXPECT_CALL(*interface_proxy, EAPLogon()); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 453 | EXPECT_TRUE(InvokeStartEapAuthentication()); |
| 454 | Mock::VerifyAndClearExpectations(mock_service_); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 455 | Mock::VerifyAndClearExpectations(mock_eap_service_); |
| 456 | Mock::VerifyAndClearExpectations(&mock_eap_credentials); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 457 | Mock::VerifyAndClearExpectations(interface_proxy); |
| 458 | EXPECT_EQ(kFirstNetworkPath, GetSupplicantNetworkPath()); |
| 459 | |
| 460 | EXPECT_CALL(*mock_service_, ClearEAPCertification()); |
| 461 | EXPECT_CALL(*interface_proxy, RemoveNetwork(StrEq(kFirstNetworkPath))); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 462 | EXPECT_CALL(*mock_eap_service_, eap()) |
| 463 | .WillOnce(Return(&mock_eap_credentials)); |
| 464 | EXPECT_CALL(mock_eap_credentials, PopulateSupplicantProperties(_, _, _, _)); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 465 | const char kSecondNetworkPath[] = "/network/second-path"; |
| 466 | EXPECT_CALL(*interface_proxy, AddNetwork(_)) |
| 467 | .WillOnce(Return(kSecondNetworkPath)); |
| 468 | EXPECT_CALL(*interface_proxy, SelectNetwork(StrEq(kSecondNetworkPath))); |
Paul Stewart | be9abfd | 2013-04-22 12:18:48 -0700 | [diff] [blame] | 469 | EXPECT_CALL(*interface_proxy, EAPLogon()); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 470 | EXPECT_TRUE(InvokeStartEapAuthentication()); |
| 471 | EXPECT_EQ(kSecondNetworkPath, GetSupplicantNetworkPath()); |
| 472 | } |
| 473 | |
| 474 | TEST_F(EthernetTest, StopSupplicant) { |
| 475 | MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get(); |
Paul Stewart | be9abfd | 2013-04-22 12:18:48 -0700 | [diff] [blame] | 476 | MockSupplicantInterfaceProxy *interface_proxy = |
| 477 | supplicant_interface_proxy_.get(); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 478 | StartSupplicant(); |
| 479 | SetIsEapAuthenticated(true); |
| 480 | SetSupplicantNetworkPath("/network/1"); |
Paul Stewart | be9abfd | 2013-04-22 12:18:48 -0700 | [diff] [blame] | 481 | EXPECT_CALL(*interface_proxy, EAPLogoff()); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 482 | EXPECT_CALL(*process_proxy, RemoveInterface(StrEq(kInterfacePath))); |
| 483 | InvokeStopSupplicant(); |
| 484 | EXPECT_EQ(NULL, GetSupplicantInterfaceProxy()); |
| 485 | EXPECT_EQ(NULL, GetSupplicantProcessProxy()); |
| 486 | EXPECT_EQ("", GetSupplicantInterfacePath()); |
| 487 | EXPECT_EQ("", GetSupplicantNetworkPath()); |
| 488 | EXPECT_FALSE(GetIsEapAuthenticated()); |
| 489 | } |
| 490 | |
| 491 | TEST_F(EthernetTest, Certification) { |
| 492 | const string kSubjectName("subject-name"); |
| 493 | const uint32 kDepth = 123; |
| 494 | // Should not crash due to no service_. |
| 495 | TriggerCertification(kSubjectName, kDepth); |
| 496 | EXPECT_CALL(*mock_service_, AddEAPCertification(kSubjectName, kDepth)); |
| 497 | SetService(mock_service_); |
| 498 | TriggerCertification(kSubjectName, kDepth); |
| 499 | } |
| 500 | |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 501 | } // namespace shill |