Paul Stewart | 735eab5 | 2013-03-29 09:19:23 -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/supplicant_eap_state_handler.h" |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | #include "shill/mock_log.h" |
| 11 | #include "shill/wpa_supplicant.h" |
| 12 | |
| 13 | using std::string; |
| 14 | using testing::_; |
| 15 | using testing::EndsWith; |
| 16 | using testing::Mock; |
| 17 | |
| 18 | namespace shill { |
| 19 | |
| 20 | class SupplicantEAPStateHandlerTest : public testing::Test { |
| 21 | public: |
| 22 | SupplicantEAPStateHandlerTest() : failure_(Service::kFailureUnknown) {} |
| 23 | virtual ~SupplicantEAPStateHandlerTest() {} |
| 24 | |
| 25 | protected: |
| 26 | void StartEAP() { |
| 27 | EXPECT_CALL(log_, Log(logging::LOG_INFO, _, |
| 28 | EndsWith("Authentication starting."))); |
| 29 | EXPECT_FALSE(handler_.ParseStatus(WPASupplicant::kEAPStatusStarted, "", |
| 30 | &failure_)); |
| 31 | Mock::VerifyAndClearExpectations(&log_); |
| 32 | } |
| 33 | |
| 34 | const string &GetTLSError() { return handler_.tls_error_; } |
| 35 | |
| 36 | SupplicantEAPStateHandler handler_; |
| 37 | Service::ConnectFailure failure_; |
| 38 | ScopedMockLog log_; |
| 39 | }; |
| 40 | |
| 41 | TEST_F(SupplicantEAPStateHandlerTest, Construct) { |
| 42 | EXPECT_FALSE(handler_.is_eap_in_progress()); |
| 43 | EXPECT_EQ("", GetTLSError()); |
| 44 | } |
| 45 | |
| 46 | TEST_F(SupplicantEAPStateHandlerTest, AuthenticationStarting) { |
| 47 | StartEAP(); |
| 48 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 49 | EXPECT_EQ("", GetTLSError()); |
| 50 | EXPECT_EQ(Service::kFailureUnknown, failure_); |
| 51 | } |
| 52 | |
| 53 | TEST_F(SupplicantEAPStateHandlerTest, AcceptedMethod) { |
| 54 | StartEAP(); |
| 55 | const string kEAPMethod("EAP-ROCHAMBEAU"); |
| 56 | EXPECT_CALL(log_, Log(logging::LOG_INFO, _, |
| 57 | EndsWith("accepted method " + kEAPMethod))); |
| 58 | EXPECT_FALSE(handler_.ParseStatus( |
| 59 | WPASupplicant::kEAPStatusAcceptProposedMethod, kEAPMethod, &failure_)); |
| 60 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 61 | EXPECT_EQ("", GetTLSError()); |
| 62 | EXPECT_EQ(Service::kFailureUnknown, failure_); |
| 63 | } |
| 64 | |
| 65 | TEST_F(SupplicantEAPStateHandlerTest, SuccessfulCompletion) { |
| 66 | StartEAP(); |
| 67 | EXPECT_CALL(log_, Log(_, _, |
| 68 | EndsWith("Completed authentication successfully."))); |
| 69 | EXPECT_TRUE(handler_.ParseStatus(WPASupplicant::kEAPStatusCompletion, |
| 70 | WPASupplicant::kEAPParameterSuccess, |
| 71 | &failure_)); |
| 72 | EXPECT_FALSE(handler_.is_eap_in_progress()); |
| 73 | EXPECT_EQ("", GetTLSError()); |
| 74 | EXPECT_EQ(Service::kFailureUnknown, failure_); |
| 75 | } |
| 76 | |
| 77 | TEST_F(SupplicantEAPStateHandlerTest, EAPFailureGeneric) { |
| 78 | StartEAP(); |
| 79 | // An EAP failure without a previous TLS indication yields a generic failure. |
| 80 | EXPECT_FALSE(handler_.ParseStatus(WPASupplicant::kEAPStatusCompletion, |
| 81 | WPASupplicant::kEAPParameterFailure, |
| 82 | &failure_)); |
| 83 | |
| 84 | // Since it hasn't completed successfully, we must assume even in failure |
| 85 | // that wpa_supplicant is continuing the EAP authentication process. |
| 86 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 87 | EXPECT_EQ("", GetTLSError()); |
| 88 | EXPECT_EQ(Service::kFailureEAPAuthentication, failure_); |
| 89 | } |
| 90 | |
| 91 | TEST_F(SupplicantEAPStateHandlerTest, EAPFailureLocalTLSIndication) { |
| 92 | StartEAP(); |
| 93 | // A TLS indication should be stored but a failure should not be returned. |
| 94 | EXPECT_FALSE(handler_.ParseStatus(WPASupplicant::kEAPStatusLocalTLSAlert, "", |
| 95 | &failure_)); |
| 96 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 97 | EXPECT_EQ(WPASupplicant::kEAPStatusLocalTLSAlert, GetTLSError()); |
| 98 | EXPECT_EQ(Service::kFailureUnknown, failure_); |
| 99 | |
| 100 | // An EAP failure with a previous TLS indication yields a specific failure. |
| 101 | EXPECT_FALSE(handler_.ParseStatus(WPASupplicant::kEAPStatusCompletion, |
| 102 | WPASupplicant::kEAPParameterFailure, |
| 103 | &failure_)); |
| 104 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 105 | EXPECT_EQ(Service::kFailureEAPLocalTLS, failure_); |
| 106 | } |
| 107 | |
| 108 | TEST_F(SupplicantEAPStateHandlerTest, EAPFailureRemoteTLSIndication) { |
| 109 | StartEAP(); |
| 110 | // A TLS indication should be stored but a failure should not be returned. |
| 111 | EXPECT_FALSE(handler_.ParseStatus(WPASupplicant::kEAPStatusRemoteTLSAlert, "", |
| 112 | &failure_)); |
| 113 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 114 | EXPECT_EQ(WPASupplicant::kEAPStatusRemoteTLSAlert, GetTLSError()); |
| 115 | EXPECT_EQ(Service::kFailureUnknown, failure_); |
| 116 | |
| 117 | // An EAP failure with a previous TLS indication yields a specific failure. |
| 118 | EXPECT_FALSE(handler_.ParseStatus(WPASupplicant::kEAPStatusCompletion, |
| 119 | WPASupplicant::kEAPParameterFailure, |
| 120 | &failure_)); |
| 121 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 122 | EXPECT_EQ(Service::kFailureEAPRemoteTLS, failure_); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | TEST_F(SupplicantEAPStateHandlerTest, BadRemoteCertificateVerification) { |
| 127 | StartEAP(); |
| 128 | const string kStrangeParameter("ennui"); |
| 129 | EXPECT_CALL(log_, Log(logging::LOG_ERROR, _,EndsWith( |
| 130 | string("Unexpected ") + |
| 131 | WPASupplicant::kEAPStatusRemoteCertificateVerification + |
| 132 | " parameter: " + kStrangeParameter))); |
| 133 | EXPECT_FALSE(handler_.ParseStatus( |
| 134 | WPASupplicant::kEAPStatusRemoteCertificateVerification, kStrangeParameter, |
| 135 | &failure_)); |
| 136 | // Although we reported an error, this shouldn't mean failure. |
| 137 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 138 | EXPECT_EQ("", GetTLSError()); |
| 139 | EXPECT_EQ(Service::kFailureUnknown, failure_); |
| 140 | } |
| 141 | |
| 142 | TEST_F(SupplicantEAPStateHandlerTest, ParameterNeeded) { |
| 143 | StartEAP(); |
| 144 | const string kAuthenticationParameter("nudge nudge say no more"); |
| 145 | EXPECT_CALL(log_, Log(logging::LOG_ERROR, _,EndsWith( |
| 146 | string("aborted due to missing authentication parameter: ") + |
| 147 | kAuthenticationParameter))); |
| 148 | EXPECT_FALSE(handler_.ParseStatus( |
| 149 | WPASupplicant::kEAPStatusParameterNeeded, kAuthenticationParameter, |
| 150 | &failure_)); |
| 151 | EXPECT_TRUE(handler_.is_eap_in_progress()); |
| 152 | EXPECT_EQ("", GetTLSError()); |
| 153 | EXPECT_EQ(Service::kFailureEAPAuthentication, failure_); |
| 154 | } |
| 155 | |
| 156 | } // namespace shill |