Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/chromeos/arc/arc_auth_service.h" | 13 #include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| 14 #include "chrome/browser/chromeos/arc/arc_session_manager.h" | 14 #include "chrome/browser/chromeos/arc/arc_session_manager.h" |
| 15 #include "chrome/browser/chromeos/arc/auth/arc_robot_auth_code_fetcher.h" | |
| 15 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | 16 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| 16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 17 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 17 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 18 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 18 #include "chrome/browser/policy/cloud/test_request_interceptor.h" | 19 #include "chrome/browser/policy/cloud/test_request_interceptor.h" |
| 19 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
| 20 #include "chromeos/chromeos_switches.h" | 21 #include "chromeos/chromeos_switches.h" |
| 21 #include "components/arc/arc_bridge_service.h" | |
| 22 #include "components/arc/arc_service_manager.h" | |
| 23 #include "components/arc/common/auth.mojom.h" | |
| 24 #include "components/policy/core/common/cloud/device_management_service.h" | 22 #include "components/policy/core/common/cloud/device_management_service.h" |
| 25 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | 23 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" |
| 26 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h" | 24 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h" |
| 27 #include "components/policy/core/common/policy_switches.h" | 25 #include "components/policy/core/common/policy_switches.h" |
| 28 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 29 #include "net/base/upload_bytes_element_reader.h" | 27 #include "net/base/upload_bytes_element_reader.h" |
| 30 #include "net/base/upload_data_stream.h" | 28 #include "net/base/upload_data_stream.h" |
| 31 #include "net/url_request/url_request_test_job.h" | 29 #include "net/url_request/url_request_test_job.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 31 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 59 response.mutable_service_api_access_response()->set_auth_code(kFakeAuthCode); | 57 response.mutable_service_api_access_response()->set_auth_code(kFakeAuthCode); |
| 60 | 58 |
| 61 std::string response_data; | 59 std::string response_data; |
| 62 EXPECT_TRUE(response.SerializeToString(&response_data)); | 60 EXPECT_TRUE(response.SerializeToString(&response_data)); |
| 63 | 61 |
| 64 return new net::URLRequestTestJob(request, network_delegate, | 62 return new net::URLRequestTestJob(request, network_delegate, |
| 65 net::URLRequestTestJob::test_headers(), | 63 net::URLRequestTestJob::test_headers(), |
| 66 response_data, true); | 64 response_data, true); |
| 67 } | 65 } |
| 68 | 66 |
| 69 class FakeAuthInstance : public arc::mojom::AuthInstance { | |
| 70 public: | |
| 71 void Init(arc::mojom::AuthHostPtr host_ptr) override {} | |
| 72 void OnAccountInfoReady(arc::mojom::AccountInfoPtr account_info) override { | |
| 73 ASSERT_FALSE(callback.is_null()); | |
| 74 callback.Run(account_info); | |
| 75 } | |
| 76 base::Callback<void(const arc::mojom::AccountInfoPtr&)> callback; | |
| 77 }; | |
| 78 | |
| 79 } // namespace | 67 } // namespace |
| 80 | 68 |
| 81 class ArcRobotAuthCodeFetcherBrowserTest : public InProcessBrowserTest { | 69 class ArcRobotAuthCodeFetcherBrowserTest : public InProcessBrowserTest { |
| 82 protected: | 70 protected: |
| 83 ArcRobotAuthCodeFetcherBrowserTest() = default; | 71 ArcRobotAuthCodeFetcherBrowserTest() = default; |
| 84 | 72 |
| 85 // InProcessBrowserTest: | 73 // InProcessBrowserTest: |
| 86 ~ArcRobotAuthCodeFetcherBrowserTest() override = default; | 74 ~ArcRobotAuthCodeFetcherBrowserTest() override = default; |
| 87 | 75 |
| 88 void SetUpCommandLine(base::CommandLine* command_line) override { | 76 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 89 InProcessBrowserTest::SetUpCommandLine(command_line); | 77 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 90 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | 78 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, |
| 91 "http://localhost"); | 79 "http://localhost"); |
| 92 command_line->AppendSwitch(chromeos::switches::kEnableArc); | 80 command_line->AppendSwitch(chromeos::switches::kEnableArc); |
| 93 } | 81 } |
| 94 | 82 |
| 95 void SetUpOnMainThread() override { | 83 void SetUpOnMainThread() override { |
| 96 interceptor_.reset(new policy::TestRequestInterceptor( | 84 interceptor_ = base::MakeUnique<policy::TestRequestInterceptor>( |
| 97 "localhost", content::BrowserThread::GetTaskRunnerForThread( | 85 "localhost", content::BrowserThread::GetTaskRunnerForThread( |
| 98 content::BrowserThread::IO))); | 86 content::BrowserThread::IO)); |
| 99 | 87 |
| 100 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler( | 88 user_manager_enabler_ = |
| 101 new chromeos::FakeChromeUserManager)); | 89 base::MakeUnique<chromeos::ScopedUserManagerEnabler>( |
| 90 new chromeos::FakeChromeUserManager()); | |
| 102 | 91 |
| 103 const AccountId account_id(AccountId::FromUserEmail(kFakeUserName)); | 92 const AccountId account_id(AccountId::FromUserEmail(kFakeUserName)); |
| 104 GetFakeUserManager()->AddArcKioskAppUser(account_id); | 93 GetFakeUserManager()->AddArcKioskAppUser(account_id); |
| 105 GetFakeUserManager()->LoginUser(account_id); | 94 GetFakeUserManager()->LoginUser(account_id); |
| 106 | 95 |
| 107 policy::BrowserPolicyConnectorChromeOS* const connector = | 96 policy::BrowserPolicyConnectorChromeOS* const connector = |
| 108 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | 97 g_browser_process->platform_part()->browser_policy_connector_chromeos(); |
| 109 policy::DeviceCloudPolicyManagerChromeOS* const cloud_policy_manager = | 98 policy::DeviceCloudPolicyManagerChromeOS* const cloud_policy_manager = |
| 110 connector->GetDeviceCloudPolicyManager(); | 99 connector->GetDeviceCloudPolicyManager(); |
| 111 | 100 |
| 112 cloud_policy_manager->StartConnection( | 101 cloud_policy_manager->StartConnection( |
| 113 base::MakeUnique<policy::MockCloudPolicyClient>(), | 102 base::MakeUnique<policy::MockCloudPolicyClient>(), |
| 114 connector->GetInstallAttributes()); | 103 connector->GetInstallAttributes()); |
| 115 | 104 |
| 116 policy::MockCloudPolicyClient* const cloud_policy_client = | 105 policy::MockCloudPolicyClient* const cloud_policy_client = |
| 117 static_cast<policy::MockCloudPolicyClient*>( | 106 static_cast<policy::MockCloudPolicyClient*>( |
| 118 cloud_policy_manager->core()->client()); | 107 cloud_policy_manager->core()->client()); |
| 119 cloud_policy_client->SetDMToken("fake-dm-token"); | 108 cloud_policy_client->SetDMToken("fake-dm-token"); |
| 120 cloud_policy_client->client_id_ = "client-id"; | 109 cloud_policy_client->client_id_ = "client-id"; |
| 121 | |
| 122 ArcBridgeService::Get()->auth()->SetInstance(&auth_instance_); | |
| 123 } | 110 } |
| 124 | 111 |
| 125 void TearDownOnMainThread() override { | 112 void TearDownOnMainThread() override { |
| 126 ArcBridgeService::Get()->auth()->SetInstance(nullptr); | |
| 127 ArcSessionManager::Get()->Shutdown(); | |
| 128 ArcServiceManager::Get()->Shutdown(); | |
| 129 user_manager_enabler_.reset(); | 113 user_manager_enabler_.reset(); |
| 130 | 114 |
| 131 // Verify that all the expected requests were handled. | 115 // Verify that all the expected requests were handled. |
| 132 EXPECT_EQ(0u, interceptor_->GetPendingSize()); | 116 EXPECT_EQ(0u, interceptor_->GetPendingSize()); |
| 133 interceptor_.reset(); | 117 interceptor_.reset(); |
| 134 } | 118 } |
| 135 | 119 |
| 136 chromeos::FakeChromeUserManager* GetFakeUserManager() const { | 120 chromeos::FakeChromeUserManager* GetFakeUserManager() const { |
| 137 return static_cast<chromeos::FakeChromeUserManager*>( | 121 return static_cast<chromeos::FakeChromeUserManager*>( |
| 138 user_manager::UserManager::Get()); | 122 user_manager::UserManager::Get()); |
| 139 } | 123 } |
| 140 | 124 |
| 141 std::unique_ptr<policy::TestRequestInterceptor> interceptor_; | 125 policy::TestRequestInterceptor* interceptor() { return interceptor_.get(); } |
| 142 FakeAuthInstance auth_instance_; | |
| 143 | 126 |
| 144 private: | 127 private: |
| 128 std::unique_ptr<policy::TestRequestInterceptor> interceptor_; | |
| 145 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | 129 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
| 146 | 130 |
| 147 DISALLOW_COPY_AND_ASSIGN(ArcRobotAuthCodeFetcherBrowserTest); | 131 DISALLOW_COPY_AND_ASSIGN(ArcRobotAuthCodeFetcherBrowserTest); |
| 148 }; | 132 }; |
| 149 | 133 |
| 150 IN_PROC_BROWSER_TEST_F(ArcRobotAuthCodeFetcherBrowserTest, | 134 IN_PROC_BROWSER_TEST_F(ArcRobotAuthCodeFetcherBrowserTest, |
| 151 RequestAccountInfoSuccess) { | 135 RequestAccountInfoSuccess) { |
| 152 interceptor_->PushJobCallback(base::Bind(&ResponseJob)); | 136 interceptor()->PushJobCallback(base::Bind(&ResponseJob)); |
| 153 | 137 |
| 154 auth_instance_.callback = | 138 auto robot_fetcher = base::MakeUnique<ArcRobotAuthCodeFetcher>(); |
| 155 base::Bind([](const mojom::AccountInfoPtr& account_info) { | 139 robot_fetcher->Fetch(base::Bind([](const std::string& auth_code) { |
| 156 EXPECT_EQ(kFakeAuthCode, account_info->auth_code.value()); | 140 EXPECT_EQ(kFakeAuthCode, auth_code); |
|
Luis Héctor Chávez
2016/12/06 19:08:56
nit: maybe have a std::string outside the closure
hidehiko
2016/12/08 14:29:33
Done.
| |
| 157 EXPECT_EQ(mojom::ChromeAccountType::ROBOT_ACCOUNT, | 141 })); |
| 158 account_info->account_type); | |
| 159 EXPECT_FALSE(account_info->is_managed); | |
| 160 }); | |
| 161 | |
| 162 ArcAuthService::GetForTest()->RequestAccountInfo(); | |
| 163 base::RunLoop().RunUntilIdle(); | 142 base::RunLoop().RunUntilIdle(); |
| 164 } | 143 } |
| 165 | 144 |
| 166 IN_PROC_BROWSER_TEST_F(ArcRobotAuthCodeFetcherBrowserTest, | 145 IN_PROC_BROWSER_TEST_F(ArcRobotAuthCodeFetcherBrowserTest, |
| 167 RequestAccountInfoError) { | 146 RequestAccountInfoError) { |
| 168 interceptor_->PushJobCallback( | 147 interceptor()->PushJobCallback( |
| 169 policy::TestRequestInterceptor::BadRequestJob()); | 148 policy::TestRequestInterceptor::BadRequestJob()); |
| 170 | 149 |
| 171 auth_instance_.callback = | 150 auto robot_fetcher = base::MakeUnique<ArcRobotAuthCodeFetcher>(); |
| 172 base::Bind([](const mojom::AccountInfoPtr&) { FAIL(); }); | 151 robot_fetcher->Fetch(base::Bind( |
| 173 | 152 [](const std::string& auth_code) { EXPECT_TRUE(auth_code.empty()); })); |
|
Luis Héctor Chávez
2016/12/06 19:08:56
maybe do something similar here, but also pass a b
hidehiko
2016/12/08 14:29:33
Did similar stuff. Instead of passing bool, initia
| |
| 174 ArcSessionManager::Get()->StartArc(); | 153 base::RunLoop().RunUntilIdle(); |
| 175 ArcAuthService::GetForTest()->RequestAccountInfo(); | |
| 176 // This MessageLoop will be stopped by AttemptUserExit(), that is called as | |
| 177 // a result of error of auth code fetching. | |
|
Luis Héctor Chávez
2016/12/06 19:08:56
Great! Having this Run() was kind of annoying :D
| |
| 178 base::RunLoop().Run(); | |
| 179 } | 154 } |
| 180 | 155 |
| 181 } // namespace arc | 156 } // namespace arc |
| OLD | NEW |