blob: 6c6f6ac2f9793002967c55fbf508d5c13ce6af42 [file] [log] [blame]
Paul Stewartced3ad72013-04-03 13:39:25 -07001// 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 Stewart35eff132013-04-12 12:08:40 -070018#include "shill/mock_eap_credentials.h"
Paul Stewartced3ad72013-04-03 13:39:25 -070019#include "shill/mock_eap_listener.h"
20#include "shill/mock_event_dispatcher.h"
Paul Stewart35eff132013-04-12 12:08:40 -070021#include "shill/mock_ethernet_eap_provider.h"
Paul Stewart9413bcc2013-04-04 16:12:43 -070022#include "shill/mock_ethernet_service.h"
Paul Stewartced3ad72013-04-03 13:39:25 -070023#include "shill/mock_glib.h"
Paul Stewart9413bcc2013-04-04 16:12:43 -070024#include "shill/mock_log.h"
Paul Stewartced3ad72013-04-03 13:39:25 -070025#include "shill/mock_manager.h"
26#include "shill/mock_metrics.h"
27#include "shill/mock_rtnl_handler.h"
Paul Stewart35eff132013-04-12 12:08:40 -070028#include "shill/mock_service.h"
Paul Stewart9413bcc2013-04-04 16:12:43 -070029#include "shill/mock_supplicant_interface_proxy.h"
30#include "shill/mock_supplicant_process_proxy.h"
Paul Stewartced3ad72013-04-03 13:39:25 -070031#include "shill/nice_mock_control.h"
Paul Stewart9413bcc2013-04-04 16:12:43 -070032#include "shill/proxy_factory.h"
Paul Stewartced3ad72013-04-03 13:39:25 -070033
Paul Stewart9413bcc2013-04-04 16:12:43 -070034using std::string;
Paul Stewartced3ad72013-04-03 13:39:25 -070035using testing::_;
36using testing::AnyNumber;
Paul Stewart9413bcc2013-04-04 16:12:43 -070037using testing::EndsWith;
Gaurav Shah25ee2502013-04-08 17:55:01 -070038using testing::Eq;
Paul Stewartced3ad72013-04-03 13:39:25 -070039using testing::Mock;
Paul Stewart9413bcc2013-04-04 16:12:43 -070040using testing::NiceMock;
Paul Stewartced3ad72013-04-03 13:39:25 -070041using testing::Return;
Paul Stewart35eff132013-04-12 12:08:40 -070042using testing::ReturnRef;
Paul Stewart9413bcc2013-04-04 16:12:43 -070043using testing::StrEq;
Paul Stewartced3ad72013-04-03 13:39:25 -070044using testing::StrictMock;
Paul Stewart9413bcc2013-04-04 16:12:43 -070045using testing::Throw;
Paul Stewartced3ad72013-04-03 13:39:25 -070046
47namespace shill {
48
49class 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 Stewart9413bcc2013-04-04 16:12:43 -070064 eap_listener_(new MockEapListener()),
65 mock_service_(new MockEthernetService(&control_interface_, &metrics_)),
Paul Stewart35eff132013-04-12 12:08:40 -070066 mock_eap_service_(new MockService(&control_interface_,
67 &dispatcher_,
68 &metrics_,
69 &manager_)),
Paul Stewart9413bcc2013-04-04 16:12:43 -070070 proxy_factory_(this),
71 supplicant_interface_proxy_(
72 new NiceMock<MockSupplicantInterfaceProxy>()),
73 supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()) {}
Paul Stewartced3ad72013-04-03 13:39:25 -070074
75 virtual void SetUp() {
76 ethernet_->rtnl_handler_ = &rtnl_handler_;
Paul Stewart9413bcc2013-04-04 16:12:43 -070077 ethernet_->proxy_factory_ = &proxy_factory_;
Paul Stewartced3ad72013-04-03 13:39:25 -070078 // 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 Stewart35eff132013-04-12 12:08:40 -070084 EXPECT_CALL(manager_, ethernet_eap_provider())
85 .WillRepeatedly(Return(&ethernet_eap_provider_));
86 ethernet_eap_provider_.set_service(mock_eap_service_);
Paul Stewartced3ad72013-04-03 13:39:25 -070087 }
88
89 virtual void TearDown() {
Paul Stewart35eff132013-04-12 12:08:40 -070090 ethernet_eap_provider_.set_service(NULL);
Paul Stewartced3ad72013-04-03 13:39:25 -070091 ethernet_->set_dhcp_provider(NULL);
92 ethernet_->eap_listener_.reset();
93 }
94
95 protected:
Paul Stewart9413bcc2013-04-04 16:12:43 -070096 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 Stewartced3ad72013-04-03 13:39:25 -0700121 static const char kDeviceName[];
122 static const char kDeviceAddress[];
Paul Stewart9413bcc2013-04-04 16:12:43 -0700123 static const char kInterfacePath[];
Paul Stewartced3ad72013-04-03 13:39:25 -0700124 static const int kInterfaceIndex;
125
Paul Stewart9413bcc2013-04-04 16:12:43 -0700126 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 Stewartced3ad72013-04-03 13:39:25 -0700143 void StartEthernet() {
144 EXPECT_CALL(rtnl_handler_,
145 SetInterfaceFlags(kInterfaceIndex, IFF_UP, IFF_UP));
146 ethernet_->Start(NULL, EnabledStateChangedCallback());
147 }
Paul Stewart9413bcc2013-04-04 16:12:43 -0700148 const SupplicantInterfaceProxyInterface *GetSupplicantInterfaceProxy() {
149 return ethernet_->supplicant_interface_proxy_.get();
Paul Stewartced3ad72013-04-03 13:39:25 -0700150 }
Paul Stewart9413bcc2013-04-04 16:12:43 -0700151 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 Stewartced3ad72013-04-03 13:39:25 -0700183 void TriggerOnEapDetected() { ethernet_->OnEapDetected(); }
Paul Stewart9413bcc2013-04-04 16:12:43 -0700184 void TriggerCertification(const string &subject, uint32 depth) {
185 ethernet_->CertificationTask(subject, depth);
186 }
Paul Stewart35eff132013-04-12 12:08:40 -0700187 void TriggerTryEapAuthentication() {
188 ethernet_->TryEapAuthenticationTask();
189 }
Paul Stewartced3ad72013-04-03 13:39:25 -0700190
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 Stewart35eff132013-04-12 12:08:40 -0700198 MockEthernetEapProvider ethernet_eap_provider_;
Paul Stewartced3ad72013-04-03 13:39:25 -0700199 MockDHCPProvider dhcp_provider_;
200 scoped_refptr<MockDHCPConfig> dhcp_config_;
Paul Stewart9413bcc2013-04-04 16:12:43 -0700201
Paul Stewartced3ad72013-04-03 13:39:25 -0700202 // Owned by Ethernet instance, but tracked here for expectations.
203 MockEapListener *eap_listener_;
Paul Stewart9413bcc2013-04-04 16:12:43 -0700204
205 MockRTNLHandler rtnl_handler_;
206 scoped_refptr<MockEthernetService> mock_service_;
Paul Stewart35eff132013-04-12 12:08:40 -0700207 scoped_refptr<MockService> mock_eap_service_;
Paul Stewart9413bcc2013-04-04 16:12:43 -0700208 NiceMock<TestProxyFactory> proxy_factory_;
209 scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_;
210 scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_;
Paul Stewartced3ad72013-04-03 13:39:25 -0700211};
212
213// static
214const char EthernetTest::kDeviceName[] = "eth0";
215const char EthernetTest::kDeviceAddress[] = "000102030405";
Paul Stewart9413bcc2013-04-04 16:12:43 -0700216const char EthernetTest::kInterfacePath[] = "/interface/path";
Paul Stewartced3ad72013-04-03 13:39:25 -0700217const int EthernetTest::kInterfaceIndex = 123;
218
219TEST_F(EthernetTest, Construct) {
220 EXPECT_FALSE(GetLinkUp());
Paul Stewart9413bcc2013-04-04 16:12:43 -0700221 EXPECT_FALSE(GetIsEapAuthenticated());
Paul Stewartced3ad72013-04-03 13:39:25 -0700222 EXPECT_FALSE(GetIsEapDetected());
Paul Stewart9413bcc2013-04-04 16:12:43 -0700223 EXPECT_TRUE(GetStore().Contains(kEapAuthenticationCompletedProperty));
Paul Stewartced3ad72013-04-03 13:39:25 -0700224 EXPECT_TRUE(GetStore().Contains(kEapAuthenticatorDetectedProperty));
225 EXPECT_EQ(NULL, GetService().get());
226}
227
228TEST_F(EthernetTest, StartStop) {
Gaurav Shah25ee2502013-04-08 17:55:01 -0700229 StartEthernet();
230
Paul Stewartced3ad72013-04-03 13:39:25 -0700231 EXPECT_FALSE(GetService().get() == NULL);
232
Gaurav Shah25ee2502013-04-08 17:55:01 -0700233 Service* service = GetService().get();
234 EXPECT_CALL(manager_, DeregisterService(Eq(service)));
Paul Stewartced3ad72013-04-03 13:39:25 -0700235 ethernet_->Stop(NULL, EnabledStateChangedCallback());
236 EXPECT_EQ(NULL, GetService().get());
237}
238
239TEST_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 Stewart35eff132013-04-12 12:08:40 -0700268 // 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(&ethernet_eap_provider_));
272 EXPECT_CALL(ethernet_eap_provider_,
273 ClearCredentialChangeCallback(ethernet_.get()));
Paul Stewartced3ad72013-04-03 13:39:25 -0700274 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 Stewart9413bcc2013-04-04 16:12:43 -0700284TEST_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
298TEST_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 Stewartced3ad72013-04-03 13:39:25 -0700316TEST_F(EthernetTest, OnEapDetected) {
317 EXPECT_FALSE(GetIsEapDetected());
318 EXPECT_CALL(*eap_listener_, Stop());
Paul Stewart35eff132013-04-12 12:08:40 -0700319 EXPECT_CALL(ethernet_eap_provider_,
320 SetCredentialChangeCallback(ethernet_.get(), _));
321 EXPECT_CALL(dispatcher_, PostTask(_)); // Posts TryEapAuthenticationTask.
Paul Stewartced3ad72013-04-03 13:39:25 -0700322 TriggerOnEapDetected();
323 EXPECT_TRUE(GetIsEapDetected());
324}
325
Paul Stewart9413bcc2013-04-04 16:12:43 -0700326TEST_F(EthernetTest, TryEapAuthenticationNoService) {
Paul Stewart35eff132013-04-12 12:08:40 -0700327 EXPECT_CALL(*mock_eap_service_, Is8021xConnectable()).Times(0);
Paul Stewart9413bcc2013-04-04 16:12:43 -0700328 NiceScopedMockLog log;
329 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
330 EndsWith("Service is missing; "
331 "not doing EAP authentication.")));
Paul Stewart35eff132013-04-12 12:08:40 -0700332 TriggerTryEapAuthentication();
Paul Stewart9413bcc2013-04-04 16:12:43 -0700333}
334
335TEST_F(EthernetTest, TryEapAuthenticationNotConnectableNotAuthenticated) {
336 SetService(mock_service_);
Paul Stewart35eff132013-04-12 12:08:40 -0700337 EXPECT_CALL(*mock_eap_service_, Is8021xConnectable()).WillOnce(Return(false));
Paul Stewart9413bcc2013-04-04 16:12:43 -0700338 NiceScopedMockLog log;
339 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
Paul Stewart35eff132013-04-12 12:08:40 -0700340 EndsWith("EAP Service lacks 802.1X credentials; "
Paul Stewart9413bcc2013-04-04 16:12:43 -0700341 "not doing EAP authentication.")));
Paul Stewart35eff132013-04-12 12:08:40 -0700342 TriggerTryEapAuthentication();
343 SetService(NULL);
Paul Stewart9413bcc2013-04-04 16:12:43 -0700344}
345
346TEST_F(EthernetTest, TryEapAuthenticationNotConnectableAuthenticated) {
347 SetService(mock_service_);
348 SetIsEapAuthenticated(true);
Paul Stewart35eff132013-04-12 12:08:40 -0700349 EXPECT_CALL(*mock_eap_service_, Is8021xConnectable()).WillOnce(Return(false));
Paul Stewart9413bcc2013-04-04 16:12:43 -0700350 NiceScopedMockLog log;
Paul Stewart35eff132013-04-12 12:08:40 -0700351 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Paul Stewart9413bcc2013-04-04 16:12:43 -0700352 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
Paul Stewart35eff132013-04-12 12:08:40 -0700353 EndsWith("EAP Service lost 802.1X credentials; "
Paul Stewart9413bcc2013-04-04 16:12:43 -0700354 "terminating EAP authentication.")));
Paul Stewart35eff132013-04-12 12:08:40 -0700355 TriggerTryEapAuthentication();
Paul Stewart9413bcc2013-04-04 16:12:43 -0700356 EXPECT_FALSE(GetIsEapAuthenticated());
357}
358
359TEST_F(EthernetTest, TryEapAuthenticationEapNotDetected) {
360 SetService(mock_service_);
Paul Stewart35eff132013-04-12 12:08:40 -0700361 EXPECT_CALL(*mock_eap_service_, Is8021xConnectable()).WillOnce(Return(true));
Paul Stewart9413bcc2013-04-04 16:12:43 -0700362 NiceScopedMockLog log;
Paul Stewart35eff132013-04-12 12:08:40 -0700363 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
Paul Stewart9413bcc2013-04-04 16:12:43 -0700364 EndsWith("EAP authenticator not detected; "
365 "not doing EAP authentication.")));
Paul Stewart35eff132013-04-12 12:08:40 -0700366 TriggerTryEapAuthentication();
Paul Stewart9413bcc2013-04-04 16:12:43 -0700367}
368
369TEST_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
389TEST_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
405TEST_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
418TEST_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 Stewart35eff132013-04-12 12:08:40 -0700426 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 Stewart9413bcc2013-04-04 16:12:43 -0700430 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 Stewartbe9abfd2013-04-22 12:18:48 -0700436 EXPECT_CALL(*interface_proxy, EAPLogon()).Times(0);
Paul Stewart9413bcc2013-04-04 16:12:43 -0700437 EXPECT_FALSE(InvokeStartEapAuthentication());
438 Mock::VerifyAndClearExpectations(mock_service_);
Paul Stewart35eff132013-04-12 12:08:40 -0700439 Mock::VerifyAndClearExpectations(mock_eap_service_);
Paul Stewart9413bcc2013-04-04 16:12:43 -0700440 Mock::VerifyAndClearExpectations(interface_proxy);
441 EXPECT_EQ("", GetSupplicantNetworkPath());
442
443 EXPECT_CALL(*mock_service_, ClearEAPCertification());
444 EXPECT_CALL(*interface_proxy, RemoveNetwork(_)).Times(0);
Paul Stewart35eff132013-04-12 12:08:40 -0700445 EXPECT_CALL(*mock_eap_service_, eap())
446 .WillOnce(Return(&mock_eap_credentials));
447 EXPECT_CALL(mock_eap_credentials, PopulateSupplicantProperties(_, _, _, _));
Paul Stewart9413bcc2013-04-04 16:12:43 -0700448 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 Stewartbe9abfd2013-04-22 12:18:48 -0700452 EXPECT_CALL(*interface_proxy, EAPLogon());
Paul Stewart9413bcc2013-04-04 16:12:43 -0700453 EXPECT_TRUE(InvokeStartEapAuthentication());
454 Mock::VerifyAndClearExpectations(mock_service_);
Paul Stewart35eff132013-04-12 12:08:40 -0700455 Mock::VerifyAndClearExpectations(mock_eap_service_);
456 Mock::VerifyAndClearExpectations(&mock_eap_credentials);
Paul Stewart9413bcc2013-04-04 16:12:43 -0700457 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 Stewart35eff132013-04-12 12:08:40 -0700462 EXPECT_CALL(*mock_eap_service_, eap())
463 .WillOnce(Return(&mock_eap_credentials));
464 EXPECT_CALL(mock_eap_credentials, PopulateSupplicantProperties(_, _, _, _));
Paul Stewart9413bcc2013-04-04 16:12:43 -0700465 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 Stewartbe9abfd2013-04-22 12:18:48 -0700469 EXPECT_CALL(*interface_proxy, EAPLogon());
Paul Stewart9413bcc2013-04-04 16:12:43 -0700470 EXPECT_TRUE(InvokeStartEapAuthentication());
471 EXPECT_EQ(kSecondNetworkPath, GetSupplicantNetworkPath());
472}
473
474TEST_F(EthernetTest, StopSupplicant) {
475 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
Paul Stewartbe9abfd2013-04-22 12:18:48 -0700476 MockSupplicantInterfaceProxy *interface_proxy =
477 supplicant_interface_proxy_.get();
Paul Stewart9413bcc2013-04-04 16:12:43 -0700478 StartSupplicant();
479 SetIsEapAuthenticated(true);
480 SetSupplicantNetworkPath("/network/1");
Paul Stewartbe9abfd2013-04-22 12:18:48 -0700481 EXPECT_CALL(*interface_proxy, EAPLogoff());
Paul Stewart9413bcc2013-04-04 16:12:43 -0700482 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
491TEST_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 Stewartced3ad72013-04-03 13:39:25 -0700501} // namespace shill