File: management_api_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (424 lines) | stat: -rw-r--r-- 17,524 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/management/management_api.h"

#include "base/auto_reset.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/pattern.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/install_verifier.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/web_applications/extension_status_utils.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api/management/management_api_constants.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/extension_dialog_auto_confirm.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_host_test_helper.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_id.h"
#include "extensions/test/extension_test_message_listener.h"

namespace keys = extension_management_api_constants;

namespace extensions {
namespace {

bool ExpectChromeAppsDefaultEnabled() {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  return false;
#else
  return true;
#endif
}

}  // namespace

namespace test_utils = api_test_utils;

class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest {
 public:
  explicit ExtensionManagementApiBrowserTest(
      ContextType context_type = ContextType::kNone)
      : ExtensionBrowserTest(context_type) {}
  ~ExtensionManagementApiBrowserTest() override = default;
  ExtensionManagementApiBrowserTest(const ExtensionManagementApiBrowserTest&) =
      delete;
  ExtensionManagementApiBrowserTest& operator=(
      const ExtensionManagementApiBrowserTest&) = delete;

 protected:
  bool CrashEnabledExtension(const ExtensionId& extension_id) {
    ExtensionHost* background_host =
        ProcessManager::Get(browser()->profile())
            ->GetBackgroundHostForExtension(extension_id);
    if (!background_host)
      return false;
    content::CrashTab(background_host->host_contents());
    return true;
  }

 private:
  ScopedInstallVerifierBypassForTest install_verifier_bypass_;
};

using ContextType = extensions::browser_test_util::ContextType;

class ExtensionManagementApiTestWithBackgroundType
    : public ExtensionManagementApiBrowserTest,
      public ::testing::WithParamInterface<ContextType> {
 public:
  ExtensionManagementApiTestWithBackgroundType()
      : ExtensionManagementApiBrowserTest(GetParam()),
        enable_chrome_apps_(
            &extensions::testing::g_enable_chrome_apps_for_testing,
            true) {}
  ~ExtensionManagementApiTestWithBackgroundType() override = default;
  ExtensionManagementApiTestWithBackgroundType(
      const ExtensionManagementApiTestWithBackgroundType&) = delete;
  ExtensionManagementApiTestWithBackgroundType& operator=(
      const ExtensionManagementApiTestWithBackgroundType&) = delete;

 private:
  base::AutoReset<bool> enable_chrome_apps_;
};

INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         ExtensionManagementApiTestWithBackgroundType,
                         ::testing::Values(ContextType::kPersistentBackground));

INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         ExtensionManagementApiTestWithBackgroundType,
                         ::testing::Values(ContextType::kServiceWorker));

// We test this here instead of in an ExtensionApiTest because normal extensions
// are not allowed to call the install function.
IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
                       InstallEvent) {
  ExtensionTestMessageListener listener1("ready");
  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("management/install_event")));
  ASSERT_TRUE(listener1.WaitUntilSatisfied());

  ExtensionTestMessageListener listener2("got_event");
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("api_test/management/enabled_extension"),
      {.context_type = ContextType::kFromManifest}));
  ASSERT_TRUE(listener2.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
                       LaunchApp) {
  ExtensionTestMessageListener listener1("app_launched");
  ExtensionTestMessageListener listener2("got_expected_error");
  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("management/simple_extension"),
                    {.context_type = ContextType::kFromManifest}));
  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("management/packaged_app"),
                    {.context_type = ContextType::kFromManifest}));
  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("management/launch_app")));
  ASSERT_TRUE(listener1.WaitUntilSatisfied());
  ASSERT_TRUE(listener2.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
                       NoLaunchAppDeprecated) {
  extensions::testing::g_enable_chrome_apps_for_testing = false;
  const Extension* packaged_app =
      LoadExtension(test_data_dir_.AppendASCII("management/packaged_app"),
                    {.context_type = ContextType::kFromManifest});
  ASSERT_TRUE(packaged_app);
  EXPECT_TRUE(packaged_app->is_app());

  ExtensionTestMessageListener error("got_chrome_apps_error");
  ExtensionTestMessageListener launched("app_launched");
  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("management/launch_app")));
  if (ExpectChromeAppsDefaultEnabled()) {
    EXPECT_TRUE(launched.WaitUntilSatisfied());
    EXPECT_FALSE(error.was_satisfied());
  } else {
    EXPECT_TRUE(error.WaitUntilSatisfied());
    EXPECT_FALSE(launched.was_satisfied());
  }
}

IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
                       LaunchAppFromBackground) {
  ExtensionTestMessageListener listener1("success");
  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("management/packaged_app"),
                    {.context_type = ContextType::kFromManifest}));
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("management/launch_app_from_background")));
  ASSERT_TRUE(listener1.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
                       NoLaunchAppFromBackgroundDeprecated) {
  extensions::testing::g_enable_chrome_apps_for_testing = false;
  const Extension* packaged_app =
      LoadExtension(test_data_dir_.AppendASCII("management/packaged_app"),
                    {.context_type = ContextType::kFromManifest});
  ASSERT_TRUE(packaged_app);
  EXPECT_TRUE(packaged_app->is_app());

  // Also verify launching from background does not work. This helper is not an
  // app.
  ExtensionTestMessageListener error("got_chrome_apps_error");
  ExtensionTestMessageListener launched_failure("not_launched");
  ExtensionTestMessageListener success("success");
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("management/launch_app_from_background")));
  if (ExpectChromeAppsDefaultEnabled()) {
    EXPECT_TRUE(success.WaitUntilSatisfied());
    EXPECT_FALSE(error.was_satisfied());
    EXPECT_FALSE(launched_failure.was_satisfied());
  } else {
    EXPECT_TRUE(error.WaitUntilSatisfied());
    EXPECT_TRUE(launched_failure.WaitUntilSatisfied());
    EXPECT_FALSE(success.was_satisfied());
  }
}

IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
                       SelfUninstall) {
  // Wait for the helper script to finish before loading the primary
  // extension. This ensures that the onUninstall event listener is
  // added before we proceed to the uninstall step.
  ExtensionTestMessageListener listener1("ready");
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("management/self_uninstall_helper")));
  ASSERT_TRUE(listener1.WaitUntilSatisfied());
  ExtensionTestMessageListener listener2("success");
  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("management/self_uninstall")));
  ASSERT_TRUE(listener2.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
                       SelfUninstallNoPermissions) {
  // Wait for the helper script to finish before loading the primary
  // extension. This ensures that the onUninstall event listener is
  // added before we proceed to the uninstall step.
  ExtensionTestMessageListener listener1("ready");
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("management/self_uninstall_helper")));
  ASSERT_TRUE(listener1.WaitUntilSatisfied());
  ExtensionTestMessageListener listener2("success");
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("management/self_uninstall_noperm")));
  ASSERT_TRUE(listener2.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType, Get) {
  ExtensionTestMessageListener listener("success");
  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("management/simple_extension"),
                    {.context_type = ContextType::kFromManifest}));
  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("management/get")));
  ASSERT_TRUE(listener.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
                       GetSelfNoPermissions) {
  ExtensionTestMessageListener listener1("success");
  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("management/get_self")));
  ASSERT_TRUE(listener1.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest,
                       CreateAppShortcutConfirmDialog) {
  const Extension* app = InstallExtension(
      test_data_dir_.AppendASCII("api_test/management/packaged_app"), 1);
  ASSERT_TRUE(app);

  const extensions::ExtensionId app_id = app->id();

  scoped_refptr<ManagementCreateAppShortcutFunction> create_shortcut_function(
      new ManagementCreateAppShortcutFunction());
  create_shortcut_function->set_user_gesture(true);
  ManagementCreateAppShortcutFunction::SetAutoConfirmForTest(true);
  test_utils::RunFunctionAndReturnSingleResult(
      create_shortcut_function.get(),
      base::StringPrintf("[\"%s\"]", app_id.c_str()), browser()->profile());

  create_shortcut_function = new ManagementCreateAppShortcutFunction();
  create_shortcut_function->set_user_gesture(true);
  ManagementCreateAppShortcutFunction::SetAutoConfirmForTest(false);
  EXPECT_TRUE(base::MatchPattern(
      test_utils::RunFunctionAndReturnError(
          create_shortcut_function.get(),
          base::StringPrintf("[\"%s\"]", app_id.c_str()), browser()->profile()),
      keys::kCreateShortcutCanceledError));
}

IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest,
                       GetAllIncludesTerminated) {
  // Load an extension with a background page, so that we know it has a process
  // running.
  ExtensionTestMessageListener listener("ready");
  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("management/install_event"));
  ASSERT_TRUE(extension);
  ASSERT_TRUE(listener.WaitUntilSatisfied());

  // The management API should list this extension.
  scoped_refptr<ManagementGetAllFunction> function =
      base::MakeRefCounted<ManagementGetAllFunction>();
  std::optional<base::Value> result =
      test_utils::RunFunctionAndReturnSingleResult(function.get(), "[]",
                                                   browser()->profile());
  ASSERT_TRUE(result->is_list());
  EXPECT_EQ(1U, result->GetList().size());

  // And it should continue to do so even after it crashes.
  ASSERT_TRUE(CrashEnabledExtension(extension->id()));

  function = base::MakeRefCounted<ManagementGetAllFunction>();
  result = test_utils::RunFunctionAndReturnSingleResult(function.get(), "[]",
                                                        browser()->profile());
  ASSERT_TRUE(result->is_list());
  EXPECT_EQ(1U, result->GetList().size());
}

class ExtensionManagementApiEscalationTest :
    public ExtensionManagementApiBrowserTest {
 protected:
  // The id of the permissions escalation test extension we use.
  static const char kId[];

  void SetUpOnMainThread() override {
    ExtensionManagementApiBrowserTest::SetUpOnMainThread();
    EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
    base::FilePath pem_path = test_data_dir_.
        AppendASCII("permissions_increase").AppendASCII("permissions.pem");
    base::FilePath path_v1 = PackExtensionWithOptions(
        test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v1"),
        scoped_temp_dir_.GetPath().AppendASCII("permissions1.crx"), pem_path,
        base::FilePath());
    base::FilePath path_v2 = PackExtensionWithOptions(
        test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v2"),
        scoped_temp_dir_.GetPath().AppendASCII("permissions2.crx"), pem_path,
        base::FilePath());

    // Install low-permission version of the extension.
    ASSERT_TRUE(InstallExtension(path_v1, 1));
    EXPECT_TRUE(extension_registry()->enabled_extensions().GetByID(kId));

    // Update to a high-permission version - it should get disabled.
    EXPECT_FALSE(UpdateExtension(kId, path_v2, -1));
    EXPECT_FALSE(extension_registry()->enabled_extensions().GetByID(kId));
    EXPECT_TRUE(extension_registry()->disabled_extensions().GetByID(kId));
    EXPECT_TRUE(ExtensionPrefs::Get(browser()->profile())
                    ->DidExtensionEscalatePermissions(kId));
  }

  void SetEnabled(bool enabled,
                  bool user_gesture,
                  const std::string& expected_error,
                  scoped_refptr<const Extension> extension) {
    scoped_refptr<ManagementSetEnabledFunction> function(
        new ManagementSetEnabledFunction);
    function->set_extension(extension);
    if (user_gesture)
      function->set_user_gesture(true);
    function->SetRenderFrameHost(browser()
                                     ->tab_strip_model()
                                     ->GetActiveWebContents()
                                     ->GetPrimaryMainFrame());
    bool response = test_utils::RunFunction(
        function.get(),
        base::StringPrintf("[\"%s\", %s]", kId, base::ToString(enabled)),
        browser()->profile(), api_test_utils::FunctionMode::kNone);
    if (expected_error.empty()) {
      EXPECT_EQ(true, response);
    } else {
      EXPECT_TRUE(response == false);
      EXPECT_EQ(expected_error, function->GetError());
    }
  }


 private:
  base::ScopedTempDir scoped_temp_dir_;
};

const char ExtensionManagementApiEscalationTest::kId[] =
    "pgdpcfcocojkjfbgpiianjngphoopgmo";

IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest,
                       DisabledReason) {
  scoped_refptr<ManagementGetFunction> function =
      new ManagementGetFunction();
  base::Value::Dict dict =
      test_utils::ToDict(test_utils::RunFunctionAndReturnSingleResult(
          function.get(), base::StringPrintf("[\"%s\"]", kId),
          browser()->profile()));
  std::string reason =
      api_test_utils::GetString(dict, keys::kDisabledReasonKey);
  EXPECT_TRUE(base::IsStringASCII(reason));
  EXPECT_EQ(reason, std::string(keys::kDisabledReasonPermissionsIncrease));
}

IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest,
                       SetEnabled) {
  scoped_refptr<const Extension> source_extension =
      ExtensionBuilder("test").Build();

  // Expect an error about no gesture.
  SetEnabled(true, false, keys::kGestureNeededForEscalationError,
             source_extension);

  {
    // Expect an error that user cancelled the dialog.
    ScopedTestDialogAutoConfirm auto_confirm(
        ScopedTestDialogAutoConfirm::CANCEL);
    SetEnabled(true, true, keys::kUserDidNotReEnableError, source_extension);
  }

  {
    // The extension should load when the user accepts the dialog, triggering
    // a new ExtensionHost creation.
    ExtensionHostTestHelper host_helper(profile(), kId);
    ScopedTestDialogAutoConfirm auto_confirm(
        ScopedTestDialogAutoConfirm::ACCEPT);
    SetEnabled(true, true, std::string(), source_extension);
    host_helper.WaitForRenderProcessReady();
  }

  {
    // Crash the extension. Mock a reload by disabling and then enabling. The
    // extension should be reloaded and enabled.
    ScopedTestDialogAutoConfirm auto_confirm(
        ScopedTestDialogAutoConfirm::ACCEPT);
    ASSERT_TRUE(CrashEnabledExtension(kId));
    // Register the target extension with extension service.
    scoped_refptr<const Extension> target_extension =
        ExtensionBuilder("TargetExtension").SetID(kId).Build();
    extension_registrar()->AddExtension(target_extension);
    SetEnabled(false, true, std::string(), source_extension);
    SetEnabled(true, true, std::string(), source_extension);
    EXPECT_TRUE(extension_registry()->enabled_extensions().GetByID(kId));
  }
}

}  // namespace extensions