1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "remoting/protocol/authenticator.h"
#include "remoting/protocol/protocol_mock_objects.h"
#include "remoting/protocol/validating_authenticator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
namespace remoting::protocol {
namespace {
using testing::_;
using testing::Return;
typedef ValidatingAuthenticator::Result ValidationResult;
constexpr char kRemoteTestJid[] = "ficticious_jid_for_testing";
// testing::InvokeArgument<N> does not work with base::OnceCallback, fortunately
// gmock makes it simple to create action templates that do for the various
// possible numbers of arguments.
ACTION_TEMPLATE(InvokeCallbackArgument,
HAS_1_TEMPLATE_PARAMS(int, k),
AND_0_VALUE_PARAMS()) {
std::move(const_cast<base::OnceClosure&>(std::get<k>(args))).Run();
}
} // namespace
class ValidatingAuthenticatorTest : public testing::Test {
public:
ValidatingAuthenticatorTest();
ValidatingAuthenticatorTest(const ValidatingAuthenticatorTest&) = delete;
ValidatingAuthenticatorTest& operator=(const ValidatingAuthenticatorTest&) =
delete;
~ValidatingAuthenticatorTest() override;
void ValidateCallback(const std::string& remote_jid,
ValidatingAuthenticator::ResultCallback callback);
protected:
// testing::Test overrides.
void SetUp() override;
// Calls ProcessMessage() on |validating_authenticator_| and blocks until
// the result callback is called.
void SendMessageAndWaitForCallback();
// Used to set up our mock behaviors on the MockAuthenticator object passed
// to |validating_authenticator_|. Lifetime of the object is controlled by
// |validating_authenticator_| so this pointer is no longer valid once
// the owner is destroyed.
raw_ptr<testing::NiceMock<MockAuthenticator>, DanglingUntriaged>
mock_authenticator_ = nullptr;
// This member is used to drive behavior in |validating_authenticator_| when
// its validation complete callback is run.
ValidationResult validation_result_ = ValidationResult::SUCCESS;
// Tracks whether our validation callback has been called or not.
bool validate_complete_called_ = false;
// The object under test.
std::unique_ptr<ValidatingAuthenticator> validating_authenticator_;
private:
base::test::SingleThreadTaskEnvironment task_environment_;
};
ValidatingAuthenticatorTest::ValidatingAuthenticatorTest() = default;
ValidatingAuthenticatorTest::~ValidatingAuthenticatorTest() = default;
void ValidatingAuthenticatorTest::ValidateCallback(
const std::string& remote_jid,
ValidatingAuthenticator::ResultCallback callback) {
validate_complete_called_ = true;
std::move(callback).Run(validation_result_);
}
void ValidatingAuthenticatorTest::SetUp() {
mock_authenticator_ = new testing::NiceMock<MockAuthenticator>();
std::unique_ptr<Authenticator> authenticator(mock_authenticator_);
validating_authenticator_ = std::make_unique<ValidatingAuthenticator>(
kRemoteTestJid,
base::BindRepeating(&ValidatingAuthenticatorTest::ValidateCallback,
base::Unretained(this)),
std::move(authenticator));
}
void ValidatingAuthenticatorTest::SendMessageAndWaitForCallback() {
base::RunLoop run_loop;
std::unique_ptr<jingle_xmpp::XmlElement> first_message(
Authenticator::CreateEmptyAuthenticatorMessage());
validating_authenticator_->ProcessMessage(first_message.get(),
run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(ValidatingAuthenticatorTest, ValidConnection_SingleMessage) {
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillOnce(InvokeCallbackArgument<1>());
ON_CALL(*mock_authenticator_, state())
.WillByDefault(Return(Authenticator::ACCEPTED));
SendMessageAndWaitForCallback();
ASSERT_TRUE(validate_complete_called_);
ASSERT_EQ(Authenticator::ACCEPTED, validating_authenticator_->state());
}
TEST_F(ValidatingAuthenticatorTest, ValidConnection_TwoMessages) {
// Send the first message to the authenticator, set the mock up to act
// like it is waiting for a second message.
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(2)
.WillRepeatedly(InvokeCallbackArgument<1>());
EXPECT_CALL(*mock_authenticator_, state())
.WillRepeatedly(Return(Authenticator::MESSAGE_READY));
SendMessageAndWaitForCallback();
ASSERT_FALSE(validate_complete_called_);
ASSERT_EQ(Authenticator::MESSAGE_READY, validating_authenticator_->state());
// Now 'retrieve' the message for the client which resets the state.
EXPECT_CALL(*mock_authenticator_, state())
.WillRepeatedly(Return(Authenticator::WAITING_MESSAGE));
// This dance is needed because GMock doesn't handle unique_ptrs very well.
// The mock method receives a raw pointer which it wraps and returns when
// GetNextMessage() is called.
std::unique_ptr<jingle_xmpp::XmlElement> next_message(
Authenticator::CreateEmptyAuthenticatorMessage());
EXPECT_CALL(*mock_authenticator_, GetNextMessagePtr())
.Times(1)
.WillOnce(Return(next_message.release()));
validating_authenticator_->GetNextMessage();
ASSERT_EQ(Authenticator::WAITING_MESSAGE, validating_authenticator_->state());
// Now send the second message for processing.
EXPECT_CALL(*mock_authenticator_, state())
.WillRepeatedly(Return(Authenticator::ACCEPTED));
SendMessageAndWaitForCallback();
ASSERT_TRUE(validate_complete_called_);
ASSERT_EQ(Authenticator::ACCEPTED, validating_authenticator_->state());
}
TEST_F(ValidatingAuthenticatorTest, ValidConnection_SendBeforeAccept) {
// This test simulates an authenticator which needs to send a message before
// transitioning to the ACCEPTED state.
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillRepeatedly(InvokeCallbackArgument<1>());
EXPECT_CALL(*mock_authenticator_, state())
.WillOnce(Return(Authenticator::MESSAGE_READY))
.WillOnce(Return(Authenticator::ACCEPTED));
// This dance is needed because GMock doesn't handle unique_ptrs very well.
// The mock method receives a raw pointer which it wraps and returns when
// GetNextMessage() is called.
std::unique_ptr<jingle_xmpp::XmlElement> next_message(
Authenticator::CreateEmptyAuthenticatorMessage());
EXPECT_CALL(*mock_authenticator_, GetNextMessagePtr())
.Times(1)
.WillOnce(Return(next_message.release()));
SendMessageAndWaitForCallback();
ASSERT_TRUE(validate_complete_called_);
ASSERT_EQ(Authenticator::MESSAGE_READY, validating_authenticator_->state());
// Now 'retrieve' the message for the client which resets the state.
validating_authenticator_->GetNextMessage();
ASSERT_EQ(Authenticator::ACCEPTED, validating_authenticator_->state());
}
TEST_F(ValidatingAuthenticatorTest, ValidConnection_ErrorInvalidCredentials) {
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillOnce(InvokeCallbackArgument<1>());
ON_CALL(*mock_authenticator_, state())
.WillByDefault(Return(Authenticator::ACCEPTED));
validation_result_ = ValidationResult::ERROR_INVALID_CREDENTIALS;
SendMessageAndWaitForCallback();
ASSERT_TRUE(validate_complete_called_);
ASSERT_EQ(Authenticator::REJECTED, validating_authenticator_->state());
ASSERT_EQ(Authenticator::RejectionReason::INVALID_CREDENTIALS,
validating_authenticator_->rejection_reason());
}
TEST_F(ValidatingAuthenticatorTest, ValidConnection_ErrorRejectedByUser) {
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillOnce(InvokeCallbackArgument<1>());
ON_CALL(*mock_authenticator_, state())
.WillByDefault(Return(Authenticator::ACCEPTED));
validation_result_ = ValidationResult::ERROR_REJECTED_BY_USER;
SendMessageAndWaitForCallback();
ASSERT_TRUE(validate_complete_called_);
ASSERT_EQ(Authenticator::REJECTED, validating_authenticator_->state());
ASSERT_EQ(Authenticator::RejectionReason::REJECTED_BY_USER,
validating_authenticator_->rejection_reason());
}
TEST_F(ValidatingAuthenticatorTest,
ValidConnectionMessageWaiting_ErrorRejectedByUser) {
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillOnce(InvokeCallbackArgument<1>());
EXPECT_CALL(*mock_authenticator_, state())
.WillOnce(Return(Authenticator::MESSAGE_READY))
.WillOnce(Return(Authenticator::ACCEPTED));
// This dance is needed because GMock doesn't handle unique_ptrs very well.
// The mock method receives a raw pointer which it wraps and returns when
// GetNextMessage() is called.
std::unique_ptr<jingle_xmpp::XmlElement> next_message(
Authenticator::CreateEmptyAuthenticatorMessage());
EXPECT_CALL(*mock_authenticator_, GetNextMessagePtr())
.WillOnce(Return(next_message.release()));
validation_result_ = ValidationResult::ERROR_REJECTED_BY_USER;
SendMessageAndWaitForCallback();
ASSERT_TRUE(validate_complete_called_);
ASSERT_EQ(Authenticator::REJECTED, validating_authenticator_->state());
ASSERT_EQ(Authenticator::RejectionReason::REJECTED_BY_USER,
validating_authenticator_->rejection_reason());
}
TEST_F(ValidatingAuthenticatorTest, ValidConnection_ErrorTooManyConnections) {
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillOnce(InvokeCallbackArgument<1>());
ON_CALL(*mock_authenticator_, state())
.WillByDefault(Return(Authenticator::ACCEPTED));
validation_result_ = ValidationResult::ERROR_TOO_MANY_CONNECTIONS;
SendMessageAndWaitForCallback();
ASSERT_TRUE(validate_complete_called_);
ASSERT_EQ(Authenticator::REJECTED, validating_authenticator_->state());
ASSERT_EQ(Authenticator::RejectionReason::TOO_MANY_CONNECTIONS,
validating_authenticator_->rejection_reason());
}
TEST_F(ValidatingAuthenticatorTest, InvalidConnection_InvalidCredentials) {
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillOnce(InvokeCallbackArgument<1>());
ON_CALL(*mock_authenticator_, state())
.WillByDefault(Return(Authenticator::REJECTED));
ON_CALL(*mock_authenticator_, rejection_reason())
.WillByDefault(
Return(Authenticator::RejectionReason::INVALID_CREDENTIALS));
// Verify validation callback is not called for invalid connections.
SendMessageAndWaitForCallback();
ASSERT_FALSE(validate_complete_called_);
ASSERT_EQ(Authenticator::REJECTED, validating_authenticator_->state());
ASSERT_EQ(Authenticator::RejectionReason::INVALID_CREDENTIALS,
validating_authenticator_->rejection_reason());
}
TEST_F(ValidatingAuthenticatorTest, InvalidConnection_InvalidAccount) {
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillOnce(InvokeCallbackArgument<1>());
ON_CALL(*mock_authenticator_, state())
.WillByDefault(Return(Authenticator::REJECTED));
ON_CALL(*mock_authenticator_, rejection_reason())
.WillByDefault(
Return(Authenticator::RejectionReason::INVALID_ACCOUNT_ID));
// Verify validation callback is not called for invalid connections.
SendMessageAndWaitForCallback();
ASSERT_FALSE(validate_complete_called_);
ASSERT_EQ(Authenticator::REJECTED, validating_authenticator_->state());
ASSERT_EQ(Authenticator::RejectionReason::INVALID_ACCOUNT_ID,
validating_authenticator_->rejection_reason());
}
TEST_F(ValidatingAuthenticatorTest, InvalidConnection_InvalidState) {
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.Times(1)
.WillOnce(InvokeCallbackArgument<1>());
ON_CALL(*mock_authenticator_, state())
.WillByDefault(Return(Authenticator::REJECTED));
ON_CALL(*mock_authenticator_, rejection_reason())
.WillByDefault(Return(Authenticator::RejectionReason::INVALID_STATE));
// Verify validation callback is not called for invalid connections.
SendMessageAndWaitForCallback();
ASSERT_FALSE(validate_complete_called_);
ASSERT_EQ(Authenticator::REJECTED, validating_authenticator_->state());
ASSERT_EQ(Authenticator::RejectionReason::INVALID_STATE,
validating_authenticator_->rejection_reason());
}
TEST_F(ValidatingAuthenticatorTest, StateChangeAfterAccepted_Propagated) {
base::MockRepeatingClosure state_changed_after_accepted;
validating_authenticator_->set_state_change_after_accepted_callback(
state_changed_after_accepted.Get());
EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
.WillOnce(InvokeCallbackArgument<1>());
ON_CALL(*mock_authenticator_, state())
.WillByDefault(Return(Authenticator::ACCEPTED));
SendMessageAndWaitForCallback();
ASSERT_EQ(validating_authenticator_->state(), Authenticator::ACCEPTED);
EXPECT_CALL(*mock_authenticator_, state())
.WillOnce(Return(Authenticator::REJECTED));
EXPECT_CALL(*mock_authenticator_, rejection_reason())
.WillOnce(
Return(Authenticator::RejectionReason::REAUTHZ_POLICY_CHECK_FAILED));
EXPECT_CALL(state_changed_after_accepted, Run());
mock_authenticator_->NotifyStateChangeAfterAccepted();
ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED);
ASSERT_EQ(validating_authenticator_->rejection_reason(),
Authenticator::RejectionReason::REAUTHZ_POLICY_CHECK_FAILED);
}
} // namespace remoting::protocol
|