Add explicit |forceOnlineSignin| to user pod status
[chromium-blink-merge.git] / remoting / host / token_validator_factory_impl_unittest.cc
blob18208fabc37c7e0370a1960688540de67e5000e9
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 // A set of unit tests for TokenValidatorFactoryImpl
7 #include <string>
9 #include "base/json/json_writer.h"
10 #include "base/values.h"
11 #include "net/http/http_status_code.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_status.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "remoting/base/rsa_key_pair.h"
16 #include "remoting/base/test_rsa_key_pair.h"
17 #include "remoting/host/token_validator_factory_impl.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h"
21 namespace {
23 const char kTokenUrl[] = "https://example.com/token";
24 const char kTokenValidationUrl[] = "https://example.com/validate";
25 const char kLocalJid[] = "user@example.com/local";
26 const char kRemoteJid[] = "user@example.com/remote";
27 const char kToken[] = "xyz123456";
28 const char kSharedSecret[] = "abcdefgh";
30 // Bad scope: no nonce element.
31 const char kBadScope[] =
32 "client:user@example.com/local host:user@example.com/remote";
34 } // namespace
36 namespace remoting {
38 class TokenValidatorFactoryImplTest : public testing::Test {
39 public:
40 TokenValidatorFactoryImplTest() : message_loop_(base::MessageLoop::TYPE_IO) {}
42 void SuccessCallback(const std::string& shared_secret) {
43 EXPECT_FALSE(shared_secret.empty());
44 message_loop_.Quit();
47 void FailureCallback(const std::string& shared_secret) {
48 EXPECT_TRUE(shared_secret.empty());
49 message_loop_.Quit();
52 void DeleteOnFailureCallback(const std::string& shared_secret) {
53 EXPECT_TRUE(shared_secret.empty());
54 token_validator_.reset();
55 message_loop_.Quit();
58 protected:
59 virtual void SetUp() OVERRIDE {
60 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair);
61 request_context_getter_ = new net::TestURLRequestContextGetter(
62 message_loop_.message_loop_proxy());
63 token_validator_factory_.reset(new TokenValidatorFactoryImpl(
64 GURL(kTokenUrl), GURL(kTokenValidationUrl), key_pair_,
65 request_context_getter_));
68 static std::string CreateResponse(const std::string& scope) {
69 base::DictionaryValue response_dict;
70 response_dict.SetString("access_token", kSharedSecret);
71 response_dict.SetString("token_type", "shared_secret");
72 response_dict.SetString("scope", scope);
73 std::string response;
74 base::JSONWriter::Write(&response_dict, &response);
75 return response;
78 static std::string CreateErrorResponse(const std::string& error) {
79 base::DictionaryValue response_dict;
80 response_dict.SetString("error", error);
81 std::string response;
82 base::JSONWriter::Write(&response_dict, &response);
83 return response;
86 base::MessageLoop message_loop_;
87 scoped_refptr<RsaKeyPair> key_pair_;
88 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
89 scoped_ptr<TokenValidatorFactoryImpl> token_validator_factory_;
90 scoped_ptr<protocol::ThirdPartyHostAuthenticator::TokenValidator>
91 token_validator_;
94 TEST_F(TokenValidatorFactoryImplTest, Success) {
95 net::FakeURLFetcherFactory factory(NULL);
96 token_validator_ = token_validator_factory_->CreateTokenValidator(
97 kLocalJid, kRemoteJid);
98 factory.SetFakeResponse(
99 GURL(kTokenValidationUrl),
100 CreateResponse(token_validator_->token_scope()),
101 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
102 token_validator_->ValidateThirdPartyToken(
103 kToken, base::Bind(&TokenValidatorFactoryImplTest::SuccessCallback,
104 base::Unretained(this)));
105 message_loop_.Run();
108 TEST_F(TokenValidatorFactoryImplTest, BadToken) {
109 net::FakeURLFetcherFactory factory(NULL);
110 token_validator_ = token_validator_factory_->CreateTokenValidator(
111 kLocalJid, kRemoteJid);
112 factory.SetFakeResponse(GURL(kTokenValidationUrl), std::string(),
113 net::HTTP_INTERNAL_SERVER_ERROR,
114 net::URLRequestStatus::FAILED);
115 token_validator_->ValidateThirdPartyToken(
116 kToken, base::Bind(&TokenValidatorFactoryImplTest::FailureCallback,
117 base::Unretained(this)));
118 message_loop_.Run();
121 TEST_F(TokenValidatorFactoryImplTest, BadScope) {
122 net::FakeURLFetcherFactory factory(NULL);
123 token_validator_ = token_validator_factory_->CreateTokenValidator(
124 kLocalJid, kRemoteJid);
125 factory.SetFakeResponse(
126 GURL(kTokenValidationUrl), CreateResponse(kBadScope), net::HTTP_OK,
127 net::URLRequestStatus::SUCCESS);
128 token_validator_->ValidateThirdPartyToken(
129 kToken, base::Bind(&TokenValidatorFactoryImplTest::FailureCallback,
130 base::Unretained(this)));
131 message_loop_.Run();
134 TEST_F(TokenValidatorFactoryImplTest, DeleteOnFailure) {
135 net::FakeURLFetcherFactory factory(NULL);
136 token_validator_ = token_validator_factory_->CreateTokenValidator(
137 kLocalJid, kRemoteJid);
138 factory.SetFakeResponse(GURL(kTokenValidationUrl),
139 std::string(),
140 net::HTTP_INTERNAL_SERVER_ERROR,
141 net::URLRequestStatus::FAILED);
142 token_validator_->ValidateThirdPartyToken(
143 kToken, base::Bind(
144 &TokenValidatorFactoryImplTest::DeleteOnFailureCallback,
145 base::Unretained(this)));
146 message_loop_.Run();
149 } // namespace remoting