OLD | NEW |
(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 <string> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/arc/arc_bridge_service.h" |
| 17 #include "components/arc/auth/arc_auth_fetcher.h" |
| 18 #include "components/arc/test/fake_arc_bridge_service.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "google_apis/gaia/gaia_constants.h" |
| 22 #include "google_apis/gaia/gaia_urls.h" |
| 23 #include "net/http/http_status_code.h" |
| 24 #include "net/url_request/test_url_fetcher_factory.h" |
| 25 #include "net/url_request/url_fetcher.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 |
| 28 namespace arc { |
| 29 |
| 30 namespace { |
| 31 |
| 32 const int kThreadOptions = content::TestBrowserThreadBundle::IO_MAINLOOP; |
| 33 const char kTestAuthCode[] = "4/Qa3CPIhh-WcMfWSf9HZaYcGUhEeax-F9sQK9CNRhZWs"; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class ArcAuthServiceTest : public testing::Test { |
| 38 public: |
| 39 ArcAuthServiceTest() |
| 40 : thread_bundle_(new content::TestBrowserThreadBundle(kThreadOptions)), |
| 41 url_fetcher_factory_( |
| 42 nullptr, |
| 43 base::Bind(&ArcAuthServiceTest::FakeURLFetcherCreator, |
| 44 base::Unretained(this))) {} |
| 45 ~ArcAuthServiceTest() override = default; |
| 46 |
| 47 void SetUp() override { |
| 48 ArcAuthService::DisableUIForTesting(); |
| 49 |
| 50 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 51 TestingProfile::Builder profile_builder; |
| 52 profile_builder.SetPath(temp_dir_.path().AppendASCII("TestArcProfile")); |
| 53 |
| 54 profile_ = profile_builder.Build(); |
| 55 bridge_service_.reset(new FakeArcBridgeService()); |
| 56 auth_service_.reset(new ArcAuthService(bridge_service_.get())); |
| 57 |
| 58 // Check initial conditions. |
| 59 EXPECT_EQ(bridge_service_.get(), ArcBridgeService::Get()); |
| 60 EXPECT_EQ(true, !ArcBridgeService::Get()->available()); |
| 61 EXPECT_EQ(ArcBridgeService::State::STOPPED, |
| 62 ArcBridgeService::Get()->state()); |
| 63 } |
| 64 |
| 65 void TearDown() override {} |
| 66 |
| 67 protected: |
| 68 Profile* profile() { return profile_.get(); } |
| 69 FakeArcBridgeService* bridge_service() { return bridge_service_.get(); } |
| 70 ArcAuthService* auth_service() { return auth_service_.get(); } |
| 71 net::FakeURLFetcherFactory& url_fetcher_factory() { |
| 72 return url_fetcher_factory_; |
| 73 } |
| 74 void set_cookie(const std::string& cookie) { rt_cookie_ = cookie; } |
| 75 |
| 76 private: |
| 77 scoped_ptr<net::FakeURLFetcher> FakeURLFetcherCreator( |
| 78 const GURL& url, |
| 79 net::URLFetcherDelegate* delegate, |
| 80 const std::string& response_data, |
| 81 net::HttpStatusCode response_code, |
| 82 net::URLRequestStatus::Status status) { |
| 83 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
| 84 url, delegate, response_data, response_code, status)); |
| 85 // Use cookie only once. |
| 86 if (!rt_cookie_.empty()) { |
| 87 net::ResponseCookies cookies; |
| 88 cookies.push_back(rt_cookie_); |
| 89 fetcher->set_cookies(cookies); |
| 90 rt_cookie_.clear(); |
| 91 } |
| 92 return fetcher; |
| 93 } |
| 94 |
| 95 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
| 96 net::FakeURLFetcherFactory url_fetcher_factory_; |
| 97 scoped_ptr<arc::FakeArcBridgeService> bridge_service_; |
| 98 scoped_ptr<arc::ArcAuthService> auth_service_; |
| 99 scoped_ptr<TestingProfile> profile_; |
| 100 base::ScopedTempDir temp_dir_; |
| 101 std::string rt_cookie_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest); |
| 104 }; |
| 105 |
| 106 TEST_F(ArcAuthServiceTest, Workflow) { |
| 107 ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state()); |
| 108 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 109 ASSERT_EQ(std::string(), auth_service()->GetAndResetAutoCode()); |
| 110 |
| 111 const GURL gaia_gurl = ArcAuthFetcher::CreateURL(); |
| 112 url_fetcher_factory().SetFakeResponse(gaia_gurl, std::string(), net::HTTP_OK, |
| 113 net::URLRequestStatus::SUCCESS); |
| 114 std::string cookie = "oauth_code="; |
| 115 cookie += kTestAuthCode; |
| 116 cookie += "; Path=/o/oauth2/programmatic_auth; Secure; HttpOnly"; |
| 117 set_cookie(cookie); |
| 118 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 119 |
| 120 // Setting profile initiates a code fetching process. |
| 121 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 122 |
| 123 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 124 base::RunLoop().RunUntilIdle(); |
| 125 |
| 126 ASSERT_EQ(ArcAuthService::State::ENABLE, auth_service()->state()); |
| 127 ASSERT_EQ(ArcBridgeService::State::READY, bridge_service()->state()); |
| 128 // Auth code valid only for one call. |
| 129 ASSERT_EQ(kTestAuthCode, auth_service()->GetAndResetAutoCode()); |
| 130 ASSERT_EQ(std::string(), auth_service()->GetAndResetAutoCode()); |
| 131 |
| 132 auth_service()->Shutdown(); |
| 133 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 134 ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state()); |
| 135 ASSERT_EQ(std::string(), auth_service()->GetAndResetAutoCode()); |
| 136 |
| 137 // Send profile and don't provide a code. |
| 138 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 139 |
| 140 // Setting profile initiates a code fetching process. |
| 141 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 142 |
| 143 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 144 base::RunLoop().RunUntilIdle(); |
| 145 |
| 146 // UI is disabled in unit tests and this code is unchanged. |
| 147 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 148 |
| 149 // Send error response. |
| 150 url_fetcher_factory().SetFakeResponse(gaia_gurl, std::string(), |
| 151 net::HTTP_BAD_REQUEST, |
| 152 net::URLRequestStatus::SUCCESS); |
| 153 auth_service()->Shutdown(); |
| 154 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 155 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 156 |
| 157 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 158 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 159 base::RunLoop().RunUntilIdle(); |
| 160 |
| 161 ASSERT_EQ(ArcAuthService::State::NO_CODE, auth_service()->state()); |
| 162 } |
| 163 |
| 164 } // namespace arc |
OLD | NEW |