OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <memory> |
| 6 #include <string> |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "remoting/protocol/authenticator.h" |
| 16 #include "remoting/protocol/protocol_mock_objects.h" |
| 17 #include "remoting/protocol/validating_authenticator.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 21 |
| 22 namespace remoting { |
| 23 namespace protocol { |
| 24 |
| 25 namespace { |
| 26 |
| 27 using testing::_; |
| 28 using testing::Return; |
| 29 |
| 30 typedef ValidatingAuthenticator::Result ValidationResult; |
| 31 |
| 32 const char kRemoteTestJid[] = "ficticious_jid_for_testing"; |
| 33 |
| 34 // testing::InvokeArgument<N> does not work with base::Callback, fortunately |
| 35 // gmock makes it simple to create action templates that do for the various |
| 36 // possible numbers of arguments. |
| 37 ACTION_TEMPLATE(InvokeCallbackArgument, |
| 38 HAS_1_TEMPLATE_PARAMS(int, k), |
| 39 AND_0_VALUE_PARAMS()) { |
| 40 ::std::tr1::get<k>(args).Run(); |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 class ValidatingAuthenticatorTest : public testing::Test { |
| 46 public: |
| 47 ValidatingAuthenticatorTest(); |
| 48 ~ValidatingAuthenticatorTest() override; |
| 49 |
| 50 void ValidateCallback( |
| 51 const std::string& remote_jid, |
| 52 const ValidatingAuthenticator::ResultCallback& callback); |
| 53 |
| 54 protected: |
| 55 // testing::Test overrides. |
| 56 void SetUp() override; |
| 57 |
| 58 // Calls ProcessMessage() on |validating_authenticator_| and blocks until |
| 59 // the result callback is called. |
| 60 void SendMessageAndWaitForCallback(); |
| 61 |
| 62 // Used to set up our mock behaviors on the MockAuthenticator object passed |
| 63 // to |validating_authenticator_|. Lifetime of the object is controlled by |
| 64 // |validating_authenticator_| so this pointer is no longer valid once |
| 65 // the owner is destroyed. |
| 66 MockAuthenticator* mock_authenticator_ = nullptr; |
| 67 |
| 68 // This member is used to drive behavior in |validating_authenticator_| when |
| 69 // it's validation complete callback is run. |
| 70 ValidationResult validation_result_ = ValidationResult::SUCCESS; |
| 71 |
| 72 // Tracks whether our ValidateCallback has been called or not. |
| 73 bool validate_complete_called_ = false; |
| 74 |
| 75 // The object under test. |
| 76 std::unique_ptr<ValidatingAuthenticator> validating_authenticator_; |
| 77 |
| 78 private: |
| 79 base::MessageLoop message_loop_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(ValidatingAuthenticatorTest); |
| 82 }; |
| 83 |
| 84 ValidatingAuthenticatorTest::ValidatingAuthenticatorTest() {} |
| 85 |
| 86 ValidatingAuthenticatorTest::~ValidatingAuthenticatorTest() {} |
| 87 |
| 88 void ValidatingAuthenticatorTest::ValidateCallback( |
| 89 const std::string& remote_jid, |
| 90 const ValidatingAuthenticator::ResultCallback& callback) { |
| 91 validate_complete_called_ = true; |
| 92 callback.Run(validation_result_); |
| 93 } |
| 94 |
| 95 void ValidatingAuthenticatorTest::SetUp() { |
| 96 mock_authenticator_ = new MockAuthenticator(); |
| 97 std::unique_ptr<Authenticator> authenticator(mock_authenticator_); |
| 98 |
| 99 validating_authenticator_.reset(new ValidatingAuthenticator( |
| 100 kRemoteTestJid, base::Bind(&ValidatingAuthenticatorTest::ValidateCallback, |
| 101 base::Unretained(this)), |
| 102 std::move(authenticator))); |
| 103 } |
| 104 |
| 105 void ValidatingAuthenticatorTest::SendMessageAndWaitForCallback() { |
| 106 base::RunLoop run_loop; |
| 107 std::unique_ptr<buzz::XmlElement> first_message( |
| 108 Authenticator::CreateEmptyAuthenticatorMessage()); |
| 109 validating_authenticator_->ProcessMessage(first_message.get(), |
| 110 run_loop.QuitClosure()); |
| 111 run_loop.Run(); |
| 112 } |
| 113 |
| 114 TEST_F(ValidatingAuthenticatorTest, ValidConnection_SingleMessage) { |
| 115 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)) |
| 116 .Times(1) |
| 117 .WillOnce(InvokeCallbackArgument<1>()); |
| 118 |
| 119 ON_CALL(*mock_authenticator_, state()) |
| 120 .WillByDefault(Return(Authenticator::ACCEPTED)); |
| 121 |
| 122 SendMessageAndWaitForCallback(); |
| 123 ASSERT_TRUE(validate_complete_called_); |
| 124 ASSERT_EQ(validating_authenticator_->state(), Authenticator::ACCEPTED); |
| 125 } |
| 126 |
| 127 TEST_F(ValidatingAuthenticatorTest, ValidConnection_TwoMessages) { |
| 128 // Send the first message to the authenticator, set the mock up to act |
| 129 // like it is waiting for a second message. |
| 130 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)) |
| 131 .Times(2) |
| 132 .WillRepeatedly(InvokeCallbackArgument<1>()); |
| 133 |
| 134 EXPECT_CALL(*mock_authenticator_, state()) |
| 135 .WillRepeatedly(Return(Authenticator::MESSAGE_READY)); |
| 136 |
| 137 SendMessageAndWaitForCallback(); |
| 138 ASSERT_TRUE(validate_complete_called_); |
| 139 ASSERT_EQ(validating_authenticator_->state(), Authenticator::MESSAGE_READY); |
| 140 |
| 141 // Now 'retrieve' the message for the client which resets the state. |
| 142 EXPECT_CALL(*mock_authenticator_, state()) |
| 143 .WillOnce(Return(Authenticator::MESSAGE_READY)) |
| 144 .WillRepeatedly(Return(Authenticator::WAITING_MESSAGE)); |
| 145 |
| 146 // This dance is needed because GMock doesn't handle unique_ptrs very well. |
| 147 // The mock method receives a raw pointer which it wraps and returns when |
| 148 // GetNextMessage() is called. |
| 149 std::unique_ptr<buzz::XmlElement> next_message( |
| 150 Authenticator::CreateEmptyAuthenticatorMessage()); |
| 151 EXPECT_CALL(*mock_authenticator_, GetNextMessagePtr()) |
| 152 .Times(1) |
| 153 .WillOnce(Return(next_message.release())); |
| 154 |
| 155 validating_authenticator_->GetNextMessage(); |
| 156 ASSERT_EQ(validating_authenticator_->state(), Authenticator::WAITING_MESSAGE); |
| 157 |
| 158 // Now send the second message for processing. |
| 159 EXPECT_CALL(*mock_authenticator_, state()) |
| 160 .WillRepeatedly(Return(Authenticator::ACCEPTED)); |
| 161 |
| 162 // Reset the callback state, we don't expect the validate function to be |
| 163 // called for the second message. |
| 164 validate_complete_called_ = false; |
| 165 SendMessageAndWaitForCallback(); |
| 166 ASSERT_FALSE(validate_complete_called_); |
| 167 ASSERT_EQ(validating_authenticator_->state(), Authenticator::ACCEPTED); |
| 168 } |
| 169 |
| 170 TEST_F(ValidatingAuthenticatorTest, InvalidConnection_RejectedByUser) { |
| 171 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)).Times(0); |
| 172 EXPECT_CALL(*mock_authenticator_, state()).Times(0); |
| 173 EXPECT_CALL(*mock_authenticator_, rejection_reason()).Times(0); |
| 174 |
| 175 validation_result_ = ValidationResult::ERROR_REJECTED_BY_USER; |
| 176 |
| 177 SendMessageAndWaitForCallback(); |
| 178 ASSERT_TRUE(validate_complete_called_); |
| 179 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED); |
| 180 ASSERT_EQ(validating_authenticator_->rejection_reason(), |
| 181 Authenticator::REJECTED_BY_USER); |
| 182 } |
| 183 |
| 184 TEST_F(ValidatingAuthenticatorTest, InvalidConnection_InvalidCredentials) { |
| 185 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)).Times(0); |
| 186 EXPECT_CALL(*mock_authenticator_, state()).Times(0); |
| 187 EXPECT_CALL(*mock_authenticator_, rejection_reason()).Times(0); |
| 188 |
| 189 validation_result_ = ValidationResult::ERROR_INVALID_CREDENTIALS; |
| 190 |
| 191 SendMessageAndWaitForCallback(); |
| 192 ASSERT_TRUE(validate_complete_called_); |
| 193 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED); |
| 194 ASSERT_EQ(validating_authenticator_->rejection_reason(), |
| 195 Authenticator::INVALID_CREDENTIALS); |
| 196 } |
| 197 |
| 198 TEST_F(ValidatingAuthenticatorTest, InvalidConnection_InvalidAccount) { |
| 199 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)).Times(0); |
| 200 EXPECT_CALL(*mock_authenticator_, state()).Times(0); |
| 201 EXPECT_CALL(*mock_authenticator_, rejection_reason()).Times(0); |
| 202 |
| 203 validation_result_ = ValidationResult::ERROR_INVALID_ACCOUNT; |
| 204 |
| 205 SendMessageAndWaitForCallback(); |
| 206 ASSERT_TRUE(validate_complete_called_); |
| 207 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED); |
| 208 ASSERT_EQ(validating_authenticator_->rejection_reason(), |
| 209 Authenticator::INVALID_ACCOUNT); |
| 210 } |
| 211 |
| 212 TEST_F(ValidatingAuthenticatorTest, |
| 213 WrappedAuthenticatorRejectsConnection_InvalidCredentials) { |
| 214 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)) |
| 215 .Times(1) |
| 216 .WillOnce(InvokeCallbackArgument<1>()); |
| 217 |
| 218 ON_CALL(*mock_authenticator_, state()) |
| 219 .WillByDefault(Return(Authenticator::REJECTED)); |
| 220 |
| 221 ON_CALL(*mock_authenticator_, rejection_reason()) |
| 222 .WillByDefault(Return(Authenticator::REJECTED_BY_USER)); |
| 223 |
| 224 SendMessageAndWaitForCallback(); |
| 225 ASSERT_TRUE(validate_complete_called_); |
| 226 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED); |
| 227 ASSERT_EQ(validating_authenticator_->rejection_reason(), |
| 228 Authenticator::REJECTED_BY_USER); |
| 229 } |
| 230 |
| 231 TEST_F(ValidatingAuthenticatorTest, |
| 232 WrappedAuthenticatorRejectsConnection_InvalidAccount) { |
| 233 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)) |
| 234 .Times(1) |
| 235 .WillOnce(InvokeCallbackArgument<1>()); |
| 236 |
| 237 ON_CALL(*mock_authenticator_, state()) |
| 238 .WillByDefault(Return(Authenticator::REJECTED)); |
| 239 |
| 240 ON_CALL(*mock_authenticator_, rejection_reason()) |
| 241 .WillByDefault(Return(Authenticator::INVALID_CREDENTIALS)); |
| 242 |
| 243 SendMessageAndWaitForCallback(); |
| 244 ASSERT_TRUE(validate_complete_called_); |
| 245 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED); |
| 246 ASSERT_EQ(validating_authenticator_->rejection_reason(), |
| 247 Authenticator::INVALID_CREDENTIALS); |
| 248 } |
| 249 |
| 250 TEST_F(ValidatingAuthenticatorTest, |
| 251 WrappedAuthenticatorRejectsConnection_PROTOCOL_ERROR) { |
| 252 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)) |
| 253 .Times(1) |
| 254 .WillOnce(InvokeCallbackArgument<1>()); |
| 255 |
| 256 ON_CALL(*mock_authenticator_, state()) |
| 257 .WillByDefault(Return(Authenticator::REJECTED)); |
| 258 |
| 259 ON_CALL(*mock_authenticator_, rejection_reason()) |
| 260 .WillByDefault(Return(Authenticator::PROTOCOL_ERROR)); |
| 261 |
| 262 SendMessageAndWaitForCallback(); |
| 263 ASSERT_TRUE(validate_complete_called_); |
| 264 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED); |
| 265 ASSERT_EQ(validating_authenticator_->rejection_reason(), |
| 266 Authenticator::PROTOCOL_ERROR); |
| 267 } |
| 268 |
| 269 } // namespace protocol |
| 270 } // namespace remoting |
OLD | NEW |