Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2011 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 <errno.h>
      6 
      7 #include <string>
      8 
      9 #include "chrome/browser/chromeos/login/client_login_response_handler.h"
     10 #include "chrome/browser/chromeos/login/cookie_fetcher.h"
     11 #include "chrome/browser/chromeos/login/issue_response_handler.h"
     12 #include "chrome/browser/chromeos/login/mock_auth_response_handler.h"
     13 #include "chrome/common/net/url_fetcher.h"
     14 #include "chrome/test/testing_profile.h"
     15 #include "content/browser/browser_thread.h"
     16 #include "googleurl/src/gurl.h"
     17 #include "net/url_request/url_request_status.h"
     18 #include "testing/gmock/include/gmock/gmock.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 
     21 namespace chromeos {
     22 using ::testing::Return;
     23 using ::testing::Invoke;
     24 using ::testing::Unused;
     25 using ::testing::_;
     26 
     27 class CookieFetcherTest : public ::testing::Test {
     28  public:
     29   CookieFetcherTest()
     30       : iat_url_(AuthResponseHandler::kIssueAuthTokenUrl),
     31         ta_url_(AuthResponseHandler::kTokenAuthUrl),
     32         client_login_data_("SID n' LSID"),
     33         token_("auth token"),
     34         ui_thread_(BrowserThread::UI, &message_loop_) {
     35   }
     36 
     37   const GURL iat_url_;
     38   const GURL ta_url_;
     39   const std::string client_login_data_;
     40   const std::string token_;
     41   MessageLoopForUI message_loop_;
     42   BrowserThread ui_thread_;
     43   TestingProfile profile_;
     44 };
     45 
     46 // Check that successful HTTP responses from both end points results in
     47 // the browser window getting put up.
     48 TEST_F(CookieFetcherTest, SuccessfulFetchTest) {
     49   net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
     50 
     51   MockAuthResponseHandler* cl_handler =
     52       new MockAuthResponseHandler(iat_url_, status, kHttpSuccess, token_);
     53   MockAuthResponseHandler* i_handler =
     54       new MockAuthResponseHandler(ta_url_, status, kHttpSuccess, std::string());
     55 
     56   CookieFetcher* cf = new CookieFetcher(NULL, cl_handler, i_handler);
     57 
     58   EXPECT_CALL(*cl_handler, Handle(client_login_data_, cf))
     59       .Times(1);
     60 
     61   EXPECT_CALL(*i_handler, CanHandle(iat_url_))
     62       .WillOnce(Return(true));
     63   EXPECT_CALL(*i_handler, CanHandle(ta_url_))
     64       .WillOnce(Return(false));
     65   EXPECT_CALL(*i_handler, Handle(token_, cf))
     66       .Times(1);
     67 
     68   cf->AttemptFetch(client_login_data_);
     69   message_loop_.RunAllPending();
     70 }
     71 
     72 // Check that a network failure when trying IssueAuthToken results in us bailing
     73 // and putting up the browser window.
     74 TEST_F(CookieFetcherTest, IssueAuthTokenNetworkFailureTest) {
     75   net::URLRequestStatus failed(net::URLRequestStatus::FAILED, ECONNRESET);
     76 
     77   MockAuthResponseHandler* cl_handler =
     78       new MockAuthResponseHandler(iat_url_, failed, kHttpSuccess, token_);
     79   // I expect nothing in i_handler to get called anyway
     80   MockAuthResponseHandler* i_handler =
     81       new MockAuthResponseHandler(ta_url_, failed, kHttpSuccess, std::string());
     82 
     83   CookieFetcher* cf = new CookieFetcher(&profile_,
     84                                         cl_handler,
     85                                         i_handler);
     86 
     87   EXPECT_CALL(*cl_handler, Handle(client_login_data_, cf))
     88       .Times(1);
     89 
     90   cf->AttemptFetch(client_login_data_);
     91   message_loop_.RunAllPending();
     92 }
     93 
     94 // Check that a network failure when trying TokenAuth results in us bailing
     95 // and putting up the browser window.
     96 TEST_F(CookieFetcherTest, TokenAuthNetworkFailureTest) {
     97   net::URLRequestStatus success;
     98   net::URLRequestStatus failed(net::URLRequestStatus::FAILED, ECONNRESET);
     99 
    100   MockAuthResponseHandler* cl_handler =
    101       new MockAuthResponseHandler(iat_url_, success, kHttpSuccess, token_);
    102   MockAuthResponseHandler* i_handler =
    103       new MockAuthResponseHandler(ta_url_, failed, 0, std::string());
    104 
    105   CookieFetcher* cf = new CookieFetcher(&profile_,
    106                                         cl_handler,
    107                                         i_handler);
    108 
    109   EXPECT_CALL(*cl_handler, Handle(client_login_data_, cf))
    110       .Times(1);
    111 
    112   EXPECT_CALL(*i_handler, CanHandle(iat_url_))
    113       .WillOnce(Return(true));
    114   EXPECT_CALL(*i_handler, Handle(token_, cf))
    115       .Times(1);
    116 
    117   cf->AttemptFetch(client_login_data_);
    118   message_loop_.RunAllPending();
    119 }
    120 
    121 // Check that an unsuccessful HTTP response when trying IssueAuthToken results
    122 // in us bailing and putting up the browser window.
    123 TEST_F(CookieFetcherTest, IssueAuthTokenDeniedTest) {
    124   net::URLRequestStatus success;
    125 
    126   MockAuthResponseHandler* cl_handler =
    127       new MockAuthResponseHandler(iat_url_, success, 403, std::string());
    128   // I expect nothing in i_handler to get called anyway.
    129   MockAuthResponseHandler* i_handler =
    130       new MockAuthResponseHandler(ta_url_, success, 0, std::string());
    131 
    132   CookieFetcher* cf = new CookieFetcher(&profile_,
    133                                         cl_handler,
    134                                         i_handler);
    135 
    136   EXPECT_CALL(*cl_handler, Handle(client_login_data_, cf))
    137       .Times(1);
    138 
    139   cf->AttemptFetch(client_login_data_);
    140   message_loop_.RunAllPending();
    141 }
    142 
    143 // Check that an unsuccessful HTTP response when trying TokenAuth results
    144 // in us bailing and putting up the browser window.
    145 TEST_F(CookieFetcherTest, TokenAuthDeniedTest) {
    146   net::URLRequestStatus success;
    147 
    148   MockAuthResponseHandler* cl_handler =
    149       new MockAuthResponseHandler(iat_url_,
    150                                   success,
    151                                   kHttpSuccess,
    152                                   token_);
    153   MockAuthResponseHandler* i_handler =
    154       new MockAuthResponseHandler(ta_url_, success, 403, std::string());
    155 
    156   CookieFetcher* cf = new CookieFetcher(&profile_,
    157                                         cl_handler,
    158                                         i_handler);
    159 
    160   EXPECT_CALL(*cl_handler, Handle(client_login_data_, cf))
    161       .Times(1);
    162 
    163   EXPECT_CALL(*i_handler, CanHandle(iat_url_))
    164       .WillOnce(Return(true));
    165   EXPECT_CALL(*i_handler, Handle(token_, cf))
    166       .Times(1);
    167 
    168   cf->AttemptFetch(client_login_data_);
    169   message_loop_.RunAllPending();
    170 }
    171 
    172 TEST_F(CookieFetcherTest, ClientLoginResponseHandlerTest) {
    173   ClientLoginResponseHandler handler(NULL);
    174   std::string input("a\nb\n");
    175   std::string expected("a&b&");
    176   expected.append(ClientLoginResponseHandler::kService);
    177 
    178   scoped_ptr<URLFetcher> fetcher(handler.Handle(input, NULL));
    179   EXPECT_EQ(expected, handler.payload());
    180 }
    181 
    182 TEST_F(CookieFetcherTest, IssueResponseHandlerTest) {
    183   IssueResponseHandler handler(NULL);
    184   std::string input("a\n");
    185   std::string expected(IssueResponseHandler::kTokenAuthUrl);
    186   expected.append(input);
    187 
    188   scoped_ptr<URLFetcher> fetcher(handler.Handle(input, NULL));
    189   EXPECT_EQ(expected, handler.token_url());
    190 }
    191 
    192 }  // namespace chromeos
    193