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

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

Issue 12326090: Third Party authentication protocol. (Closed) Base URL: http://git.chromium.org/chromium/src.git@host_key_pair
Patch Set: Reviewer comments Created 7 years, 9 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 2013 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 "base/bind.h"
6 #include "net/base/net_errors.h"
7 #include "remoting/base/rsa_key_pair.h"
8 #include "remoting/protocol/authenticator_test_base.h"
9 #include "remoting/protocol/channel_authenticator.h"
10 #include "remoting/protocol/connection_tester.h"
11 #include "remoting/protocol/fake_authenticator.h"
12 #include "remoting/protocol/third_party_authenticator_base.h"
13 #include "remoting/protocol/third_party_client_authenticator.h"
14 #include "remoting/protocol/third_party_host_authenticator.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
18
19 using testing::_;
20 using testing::DeleteArg;
21 using testing::SaveArg;
22
23 namespace {
24
25 const int kMessageSize = 100;
26 const int kMessages = 1;
27
28 const char kTokenIssueUrl[] = "https://example.com/Issue";
29 const char kTokenVerificationUrl[] = "https://example.com/Verify";
30 const char kTokenScope[] = "host:a@b.com/1 client:a@b.com/2";
31 const char kToken[] = "abc123456xyz789";
32 const char kSharedSecret[] = "1234-1234-5678";
33 const char kSharedSecretBad[] = "0000-0000-0001";
34
35 } // namespace
36
37 namespace remoting {
38 namespace protocol {
39
40 class ThirdPartyAuthenticatorTest : public AuthenticatorTestBase {
41 class FakeTokenFetcher : public ThirdPartyClientAuthenticator::TokenFetcher {
42 public:
43 virtual void FetchThirdPartyToken(
44 const GURL& token_url,
45 const std::string& host_public_key,
46 const std::string& scope,
47 const TokenFetchedCallback& token_fetched_callback) {
Wez 2013/03/22 06:17:01 nit: Is there anything about the other parameters
rmsousa 2013/03/22 21:19:05 Done.
48 on_token_fetched_ = token_fetched_callback;
49 }
50
51 void OnTokenFetched(const std::string& token,
52 const std::string& shared_secret) {
53 on_token_fetched_.Run(token, shared_secret);
Sergey Ulanov 2013/03/22 05:58:43 ASSERT_FALSE(on_token_fetched_.is_null());
rmsousa 2013/03/22 21:19:05 Done.
54 on_token_fetched_.Reset();
Sergey Ulanov 2013/03/22 05:58:43 nit: it's always good idea to call callbacks last.
rmsousa 2013/03/22 21:19:05 Done.
55 }
56
57 private:
58 TokenFetchedCallback on_token_fetched_;
59 };
60
61 class FakeTokenValidator
62 : public ThirdPartyHostAuthenticator::TokenValidator {
63 public:
64 FakeTokenValidator()
65 : token_url_(kTokenIssueUrl),
66 token_scope_(kTokenScope) {}
67
68 virtual ~FakeTokenValidator() {}
69
70 virtual void ValidateThirdPartyToken(
71 const std::string& token,
72 const TokenValidatedCallback& token_validated_callback) {
73 on_token_validated_ = token_validated_callback;
Wez 2013/03/22 06:17:01 nit: indentation
rmsousa 2013/03/22 21:19:05 Done.
74 }
75
76 void OnTokenValidated(const std::string& shared_secret) {
77 on_token_validated_.Run(shared_secret);
Sergey Ulanov 2013/03/22 05:58:43 same as above
rmsousa 2013/03/22 21:19:05 Done.
78 on_token_validated_.Reset();
79 }
80
81 virtual const GURL& token_url() const OVERRIDE {
82 return token_url_;
83 }
84
85 virtual const std::string& token_scope() const OVERRIDE {
86 return token_scope_;
87 }
88
89 private:
90 GURL token_url_;
91 std::string token_scope_;
92 base::Callback<void(const std::string& shared_secret)> on_token_validated_;
93 };
94
95 public:
96 ThirdPartyAuthenticatorTest() {
97 }
Wez 2013/03/22 06:17:01 nit: {} can be on one line since they're empty.
rmsousa 2013/03/22 21:19:05 Done.
98 virtual ~ThirdPartyAuthenticatorTest() {
99 }
100
101 protected:
102 void InitAuthenticators() {
103 scoped_ptr<ThirdPartyHostAuthenticator::TokenValidator>
104 token_validator(new FakeTokenValidator());
105 token_validator_ = static_cast<FakeTokenValidator*>(token_validator.get());
106 host_.reset(new ThirdPartyHostAuthenticator(
107 host_cert_, key_pair_, token_validator.Pass()));
108 scoped_ptr<ThirdPartyClientAuthenticator::TokenFetcher>
109 token_fetcher(new FakeTokenFetcher());
110 token_fetcher_ = static_cast<FakeTokenFetcher*>(token_fetcher.get());
111 client_.reset(new ThirdPartyClientAuthenticator(
112 host_public_key_, token_fetcher.Pass()));
113 }
114
115 FakeTokenFetcher* token_fetcher_;
116 FakeTokenValidator* token_validator_;
117
118 private:
119 DISALLOW_COPY_AND_ASSIGN(ThirdPartyAuthenticatorTest);
120 };
121
122 TEST_F(ThirdPartyAuthenticatorTest, SuccessfulAuth) {
123 ASSERT_NO_FATAL_FAILURE(InitAuthenticators());
124 ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange());
125 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state());
126 ASSERT_NO_FATAL_FAILURE(token_fetcher_->OnTokenFetched(
127 kToken, kSharedSecret));
128 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, host_->state());
129 ASSERT_NO_FATAL_FAILURE(
130 token_validator_->OnTokenValidated(kSharedSecret));
Wez 2013/03/22 06:17:01 nit: blank line before the comment below
rmsousa 2013/03/22 21:19:05 Done.
131 // Both sides have finished.
132 ASSERT_EQ(Authenticator::ACCEPTED, host_->state());
133 ASSERT_EQ(Authenticator::ACCEPTED, client_->state());
134
135 // An authenticated channel can be created after the authentication.
136 client_auth_ = client_->CreateChannelAuthenticator();
137 host_auth_ = host_->CreateChannelAuthenticator();
138 RunChannelAuth(false);
139
140 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(),
141 kMessageSize, kMessages);
142
143 tester.Start();
144 message_loop_.Run();
145 tester.CheckResults();
146 }
147
148 TEST_F(ThirdPartyAuthenticatorTest, ClientNoSecret) {
149 ASSERT_NO_FATAL_FAILURE(InitAuthenticators());
150 ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange());
151 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state());
152 ASSERT_NO_FATAL_FAILURE(token_fetcher_->OnTokenFetched(kToken, ""));
Wez 2013/03/22 06:17:01 nit: blank line before the comment
rmsousa 2013/03/22 21:19:05 Done.
153 // The end result is that the client rejected the connection, since it
154 // couldn't fetch the secret.
155 ASSERT_EQ(Authenticator::REJECTED, client_->state());
156 }
157
158 TEST_F(ThirdPartyAuthenticatorTest, InvalidToken) {
159 ASSERT_NO_FATAL_FAILURE(InitAuthenticators());
160 ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange());
161 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state());
162 ASSERT_NO_FATAL_FAILURE(token_fetcher_->OnTokenFetched(
163 kToken, kSharedSecret));
164 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, host_->state());
165 ASSERT_NO_FATAL_FAILURE(token_validator_->OnTokenValidated(""));
166
167 // The end result is that the host rejected the token.
168 ASSERT_EQ(Authenticator::REJECTED, host_->state());
169 }
170
171 TEST_F(ThirdPartyAuthenticatorTest, CannotFetchToken) {
172 ASSERT_NO_FATAL_FAILURE(InitAuthenticators());
173 ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange());
174 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state());
175 ASSERT_NO_FATAL_FAILURE(token_fetcher_->OnTokenFetched("", ""));
176
177 // The end result is that the client rejected the connection, since it
178 // couldn't fetch the token.
179 ASSERT_EQ(Authenticator::REJECTED, client_->state());
180 }
181
182 // Test that negotiation stops when the fake authentication is rejected.
183 TEST_F(ThirdPartyAuthenticatorTest, HostBadSecret) {
184 ASSERT_NO_FATAL_FAILURE(InitAuthenticators());
185 ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange());
186
187 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state());
Wez 2013/03/22 06:17:01 nit: remove the blank line above, for consistency
rmsousa 2013/03/22 21:19:05 Done.
188 ASSERT_NO_FATAL_FAILURE(token_fetcher_->OnTokenFetched(
189 kToken, kSharedSecret));
190 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, host_->state());
191 ASSERT_NO_FATAL_FAILURE(
192 token_validator_->OnTokenValidated(kSharedSecretBad));
193
194 // The end result is that the host rejected the fake authentication.
195 ASSERT_EQ(Authenticator::REJECTED, client_->state());
196 }
197
198 TEST_F(ThirdPartyAuthenticatorTest, ClientBadSecret) {
199 ASSERT_NO_FATAL_FAILURE(InitAuthenticators());
200 ASSERT_NO_FATAL_FAILURE(RunHostInitiatedAuthExchange());
201
202 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, client_->state());
Wez 2013/03/22 06:17:01 nit: as above
rmsousa 2013/03/22 21:19:05 Done.
203 ASSERT_NO_FATAL_FAILURE(
204 token_fetcher_->OnTokenFetched(kToken, kSharedSecretBad));
205 ASSERT_EQ(Authenticator::PROCESSING_MESSAGE, host_->state());
206 ASSERT_NO_FATAL_FAILURE(
207 token_validator_->OnTokenValidated(kSharedSecret));
208
209 // The end result is that the host rejected the fake authentication.
210 ASSERT_EQ(Authenticator::REJECTED, client_->state());
211 }
212
213 } // namespace protocol
214 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698