Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: remoting/protocol/validating_authenticator_unittest.cc

Issue 2277553002: Adding a new authenticator which can be used to validate the remote user (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@policy_change
Patch Set: Addressing Feedback Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 .WillRepeatedly(Return(Authenticator::WAITING_MESSAGE));
144
145 // This dance is needed because GMock doesn't handle unique_ptrs very well.
146 // The mock method receives a raw pointer which it wraps and returns when
147 // GetNextMessage() is called.
148 std::unique_ptr<buzz::XmlElement> next_message(
149 Authenticator::CreateEmptyAuthenticatorMessage());
150 EXPECT_CALL(*mock_authenticator_, GetNextMessagePtr())
151 .Times(1)
152 .WillOnce(Return(next_message.release()));
153
154 validating_authenticator_->GetNextMessage();
155 ASSERT_EQ(validating_authenticator_->state(), Authenticator::WAITING_MESSAGE);
156
157 // Now send the second message for processing.
158 EXPECT_CALL(*mock_authenticator_, state())
159 .WillRepeatedly(Return(Authenticator::ACCEPTED));
160
161 // Reset the callback state, we don't expect the validate function to be
162 // called for the second message.
163 validate_complete_called_ = false;
164 SendMessageAndWaitForCallback();
165 ASSERT_FALSE(validate_complete_called_);
166 ASSERT_EQ(validating_authenticator_->state(), Authenticator::ACCEPTED);
167 }
168
169 TEST_F(ValidatingAuthenticatorTest, InvalidConnection_RejectedByUser) {
170 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)).Times(0);
171 EXPECT_CALL(*mock_authenticator_, state()).Times(0);
172 EXPECT_CALL(*mock_authenticator_, rejection_reason()).Times(0);
173
174 validation_result_ = ValidationResult::ERROR_REJECTED_BY_USER;
175
176 SendMessageAndWaitForCallback();
177 ASSERT_TRUE(validate_complete_called_);
178 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED);
179 ASSERT_EQ(validating_authenticator_->rejection_reason(),
180 Authenticator::REJECTED_BY_USER);
181 }
182
183 TEST_F(ValidatingAuthenticatorTest, InvalidConnection_InvalidCredentials) {
184 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)).Times(0);
185 EXPECT_CALL(*mock_authenticator_, state()).Times(0);
186 EXPECT_CALL(*mock_authenticator_, rejection_reason()).Times(0);
187
188 validation_result_ = ValidationResult::ERROR_INVALID_CREDENTIALS;
189
190 SendMessageAndWaitForCallback();
191 ASSERT_TRUE(validate_complete_called_);
192 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED);
193 ASSERT_EQ(validating_authenticator_->rejection_reason(),
194 Authenticator::INVALID_CREDENTIALS);
195 }
196
197 TEST_F(ValidatingAuthenticatorTest, InvalidConnection_InvalidAccount) {
198 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _)).Times(0);
199 EXPECT_CALL(*mock_authenticator_, state()).Times(0);
200 EXPECT_CALL(*mock_authenticator_, rejection_reason()).Times(0);
201
202 validation_result_ = ValidationResult::ERROR_INVALID_ACCOUNT;
203
204 SendMessageAndWaitForCallback();
205 ASSERT_TRUE(validate_complete_called_);
206 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED);
207 ASSERT_EQ(validating_authenticator_->rejection_reason(),
208 Authenticator::INVALID_ACCOUNT);
209 }
210
211 TEST_F(ValidatingAuthenticatorTest,
212 WrappedAuthenticatorRejectsConnection_InvalidCredentials) {
213 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
214 .Times(1)
215 .WillOnce(InvokeCallbackArgument<1>());
216
217 ON_CALL(*mock_authenticator_, state())
218 .WillByDefault(Return(Authenticator::REJECTED));
219
220 ON_CALL(*mock_authenticator_, rejection_reason())
221 .WillByDefault(Return(Authenticator::REJECTED_BY_USER));
222
223 SendMessageAndWaitForCallback();
224 ASSERT_TRUE(validate_complete_called_);
225 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED);
226 ASSERT_EQ(validating_authenticator_->rejection_reason(),
227 Authenticator::REJECTED_BY_USER);
228 }
229
230 TEST_F(ValidatingAuthenticatorTest,
231 WrappedAuthenticatorRejectsConnection_InvalidAccount) {
232 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
233 .Times(1)
234 .WillOnce(InvokeCallbackArgument<1>());
235
236 ON_CALL(*mock_authenticator_, state())
237 .WillByDefault(Return(Authenticator::REJECTED));
238
239 ON_CALL(*mock_authenticator_, rejection_reason())
240 .WillByDefault(Return(Authenticator::INVALID_CREDENTIALS));
241
242 SendMessageAndWaitForCallback();
243 ASSERT_TRUE(validate_complete_called_);
244 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED);
245 ASSERT_EQ(validating_authenticator_->rejection_reason(),
246 Authenticator::INVALID_CREDENTIALS);
247 }
248
249 TEST_F(ValidatingAuthenticatorTest,
250 WrappedAuthenticatorRejectsConnection_PROTOCOL_ERROR) {
251 EXPECT_CALL(*mock_authenticator_, ProcessMessage(_, _))
252 .Times(1)
253 .WillOnce(InvokeCallbackArgument<1>());
254
255 ON_CALL(*mock_authenticator_, state())
256 .WillByDefault(Return(Authenticator::REJECTED));
257
258 ON_CALL(*mock_authenticator_, rejection_reason())
259 .WillByDefault(Return(Authenticator::PROTOCOL_ERROR));
260
261 SendMessageAndWaitForCallback();
262 ASSERT_TRUE(validate_complete_called_);
263 ASSERT_EQ(validating_authenticator_->state(), Authenticator::REJECTED);
264 ASSERT_EQ(validating_authenticator_->rejection_reason(),
265 Authenticator::PROTOCOL_ERROR);
266 }
267
268 } // namespace protocol
269 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698