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