Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: chrome/browser/chromeos/arc/auth/arc_robot_auth_browsertest.cc

Issue 2547073002: Fix race issue in ArcAuthService. (Closed)
Patch Set: Address comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <memory>
6 #include <string>
7
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chromeos/arc/arc_auth_service.h"
14 #include "chrome/browser/chromeos/arc/arc_session_manager.h"
15 #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/policy/browser_policy_connector_chromeos.h"
18 #include "chrome/browser/policy/cloud/test_request_interceptor.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #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"
25 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
26 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
27 #include "components/policy/core/common/policy_switches.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "net/base/upload_bytes_element_reader.h"
30 #include "net/base/upload_data_stream.h"
31 #include "net/url_request/url_request_test_job.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33
34 namespace arc {
35 namespace {
36
37 constexpr char kFakeUserName[] = "test@example.com";
38 constexpr char kFakeAuthCode[] = "fake-auth-code";
39
40 // JobCallback for the interceptor.
41 net::URLRequestJob* ResponseJob(net::URLRequest* request,
42 net::NetworkDelegate* network_delegate) {
43 const net::UploadDataStream* upload = request->get_upload();
44 if (!upload || !upload->GetElementReaders() ||
45 upload->GetElementReaders()->size() != 1 ||
46 !(*upload->GetElementReaders())[0]->AsBytesReader())
47 return nullptr;
48
49 const net::UploadBytesElementReader* bytes_reader =
50 (*upload->GetElementReaders())[0]->AsBytesReader();
51
52 enterprise_management::DeviceManagementRequest parsed_request;
53 EXPECT_TRUE(parsed_request.ParseFromArray(bytes_reader->bytes(),
54 bytes_reader->length()));
55 // Check if auth code is requested.
56 EXPECT_TRUE(parsed_request.has_service_api_access_request());
57
58 enterprise_management::DeviceManagementResponse response;
59 response.mutable_service_api_access_response()->set_auth_code(kFakeAuthCode);
60
61 std::string response_data;
62 EXPECT_TRUE(response.SerializeToString(&response_data));
63
64 return new net::URLRequestTestJob(request, network_delegate,
65 net::URLRequestTestJob::test_headers(),
66 response_data, true);
67 }
68
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
80
81 class ArcRobotAuthBrowserTest : public InProcessBrowserTest {
82 protected:
83 ArcRobotAuthBrowserTest() = default;
84
85 // InProcessBrowserTest:
86 ~ArcRobotAuthBrowserTest() override = default;
87
88 void SetUpCommandLine(base::CommandLine* command_line) override {
89 InProcessBrowserTest::SetUpCommandLine(command_line);
90 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl,
91 "http://localhost");
92 command_line->AppendSwitch(chromeos::switches::kEnableArc);
93 }
94
95 void SetUpOnMainThread() override {
96 interceptor_.reset(new policy::TestRequestInterceptor(
97 "localhost", content::BrowserThread::GetTaskRunnerForThread(
98 content::BrowserThread::IO)));
99
100 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler(
101 new chromeos::FakeChromeUserManager));
102
103 const AccountId account_id(AccountId::FromUserEmail(kFakeUserName));
104 GetFakeUserManager()->AddArcKioskAppUser(account_id);
105 GetFakeUserManager()->LoginUser(account_id);
106
107 policy::BrowserPolicyConnectorChromeOS* const connector =
108 g_browser_process->platform_part()->browser_policy_connector_chromeos();
109 policy::DeviceCloudPolicyManagerChromeOS* const cloud_policy_manager =
110 connector->GetDeviceCloudPolicyManager();
111
112 cloud_policy_manager->StartConnection(
113 base::MakeUnique<policy::MockCloudPolicyClient>(),
114 connector->GetInstallAttributes());
115
116 policy::MockCloudPolicyClient* const cloud_policy_client =
117 static_cast<policy::MockCloudPolicyClient*>(
118 cloud_policy_manager->core()->client());
119 cloud_policy_client->SetDMToken("fake-dm-token");
120 cloud_policy_client->client_id_ = "client-id";
121
122 ArcBridgeService::Get()->auth()->SetInstance(&auth_instance_);
123 }
124
125 void TearDownOnMainThread() override {
126 ArcBridgeService::Get()->auth()->SetInstance(nullptr);
127 ArcSessionManager::Get()->Shutdown();
128 ArcServiceManager::Get()->Shutdown();
129 user_manager_enabler_.reset();
130
131 // Verify that all the expected requests were handled.
132 EXPECT_EQ(0u, interceptor_->GetPendingSize());
133 interceptor_.reset();
134 }
135
136 chromeos::FakeChromeUserManager* GetFakeUserManager() const {
137 return static_cast<chromeos::FakeChromeUserManager*>(
138 user_manager::UserManager::Get());
139 }
140
141 std::unique_ptr<policy::TestRequestInterceptor> interceptor_;
142 FakeAuthInstance auth_instance_;
143
144 private:
145 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
146
147 DISALLOW_COPY_AND_ASSIGN(ArcRobotAuthBrowserTest);
148 };
149
150 IN_PROC_BROWSER_TEST_F(ArcRobotAuthBrowserTest, RequestAccountInfoSuccess) {
151 interceptor_->PushJobCallback(base::Bind(&ResponseJob));
152
153 auth_instance_.callback =
154 base::Bind([](const mojom::AccountInfoPtr& account_info) {
155 EXPECT_EQ(kFakeAuthCode, account_info->auth_code.value());
156 EXPECT_EQ(mojom::ChromeAccountType::ROBOT_ACCOUNT,
157 account_info->account_type);
158 EXPECT_FALSE(account_info->is_managed);
159 });
160
161 ArcAuthService::GetForTest()->RequestAccountInfo();
162 base::RunLoop().RunUntilIdle();
163 }
164
165 IN_PROC_BROWSER_TEST_F(ArcRobotAuthBrowserTest, RequestAccountInfoError) {
166 interceptor_->PushJobCallback(
167 policy::TestRequestInterceptor::BadRequestJob());
168
169 auth_instance_.callback =
170 base::Bind([](const mojom::AccountInfoPtr&) { FAIL(); });
171
172 ArcAuthService::GetForTest()->RequestAccountInfo();
173 // This MessageLoop will be stopped by AttemptUserExit(), that is called as
174 // a result of error of auth code fetching.
175 base::RunLoop().Run();
176 }
177
178 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/auth/arc_robot_auth.cc ('k') | chrome/browser/chromeos/arc/auth/arc_robot_auth_code_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698