blob: 06a18aa283409b258c3b0072d77b16348d1a3ead [file] [log] [blame]
amistrydc1c91062016-05-12 00:59:521// 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 "base/bind.h"
6#include "base/run_loop.h"
7#include "base/strings/utf_string_conversions.h"
8#include "content/public/browser/browser_thread.h"
9#include "content/public/browser/utility_process_host.h"
10#include "content/public/browser/utility_process_host_client.h"
amistrydc1c91062016-05-12 00:59:5211#include "content/public/test/content_browser_test.h"
12#include "content/public/test/content_browser_test_utils.h"
benff461632016-10-06 14:27:3313#include "content/public/test/test_service.mojom.h"
amistrydc1c91062016-05-12 00:59:5214
15namespace content {
16
17class UtilityProcessHostImplBrowserTest : public ContentBrowserTest {
18 public:
amistryb6138912016-05-31 01:54:1219 void RunUtilityProcess(bool elevated) {
amistrydc1c91062016-05-12 00:59:5220 base::RunLoop run_loop;
21 done_closure_ = run_loop.QuitClosure();
22 BrowserThread::PostTask(
23 BrowserThread::IO, FROM_HERE,
tzike2aca992017-09-05 08:50:5424 base::BindOnce(
amistrydc1c91062016-05-12 00:59:5225 &UtilityProcessHostImplBrowserTest::RunUtilityProcessOnIOThread,
amistryb6138912016-05-31 01:54:1226 base::Unretained(this), elevated));
amistrydc1c91062016-05-12 00:59:5227 run_loop.Run();
28 }
29
30 protected:
amistryb6138912016-05-31 01:54:1231 void RunUtilityProcessOnIOThread(bool elevated) {
amistrydc1c91062016-05-12 00:59:5232 UtilityProcessHost* host = UtilityProcessHost::Create(nullptr, nullptr);
33 host->SetName(base::ASCIIToUTF16("TestProcess"));
amistryb6138912016-05-31 01:54:1234#if defined(OS_WIN)
35 if (elevated)
36 host->ElevatePrivileges();
37#endif
amistrydc1c91062016-05-12 00:59:5238 EXPECT_TRUE(host->Start());
39
bena06f7d62017-04-04 20:48:5240 BindInterface(host, &service_);
tzike2aca992017-09-05 08:50:5441 service_->DoSomething(
42 base::BindOnce(&UtilityProcessHostImplBrowserTest::OnSomething,
43 base::Unretained(this)));
amistrydc1c91062016-05-12 00:59:5244 }
45
46 void OnSomething() {
47 service_.reset();
48 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_);
49 }
50
benff461632016-10-06 14:27:3351 mojom::TestServicePtr service_;
amistrydc1c91062016-05-12 00:59:5252 base::Closure done_closure_;
53};
54
55IN_PROC_BROWSER_TEST_F(UtilityProcessHostImplBrowserTest, LaunchProcess) {
amistryb6138912016-05-31 01:54:1256 RunUtilityProcess(false);
amistrydc1c91062016-05-12 00:59:5257}
58
amistryb6138912016-05-31 01:54:1259#if defined(OS_WIN)
60IN_PROC_BROWSER_TEST_F(UtilityProcessHostImplBrowserTest,
61 LaunchElevatedProcess) {
62 RunUtilityProcess(true);
63}
64#endif
65
amistrydc1c91062016-05-12 00:59:5266} // namespace content