File: file_iframe_apitest.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 (111 lines) | stat: -rw-r--r-- 4,132 bytes parent folder | download | duplicates (4)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/process_manager.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/test_extension_dir.h"
#include "net/base/filename_util.h"

class FileIFrameAPITest : public extensions::ExtensionBrowserTest {
 public:
  FileIFrameAPITest() = default;

  FileIFrameAPITest(const FileIFrameAPITest&) = delete;
  FileIFrameAPITest& operator=(const FileIFrameAPITest&) = delete;

  void set_has_all_urls(bool val) { has_all_urls_ = val; }
  void set_has_file_access(bool val) { has_file_access_ = val; }

  // Loads the extension and determines if the background page was able to load
  // a file iframe.
  void RunTest(bool expect_will_load_file_iframe) {
    WriteManifest();
    WriteBackgroundPage();

    ExtensionTestMessageListener listener;
    ASSERT_TRUE(LoadExtension(extension_dir_.UnpackedPath(),
                              {.allow_file_access = has_file_access_}));
    EXPECT_TRUE(listener.WaitUntilSatisfied());

    EXPECT_TRUE(listener.message() == "allowed" ||
                listener.message() == "denied")
        << "Unexpected message " << listener.message();
    bool allowed = listener.message() == "allowed";
    EXPECT_EQ(expect_will_load_file_iframe, allowed);

    // Sanity check the last committed url on the |file_iframe|.
    extensions::ExtensionHost* background_host =
        extensions::ProcessManager::Get(profile())
            ->GetBackgroundHostForExtension(last_loaded_extension_id());
    ASSERT_TRUE(background_host);
    content::RenderFrameHost* file_iframe = content::FrameMatchingPredicate(
        background_host->host_contents()->GetPrimaryPage(),
        base::BindRepeating(&content::FrameMatchesName, "file_iframe"));
    bool is_file_url = file_iframe->GetLastCommittedURL() == GURL("file:///");
    EXPECT_EQ(expect_will_load_file_iframe, is_file_url)
        << "Unexpected committed url: "
        << file_iframe->GetLastCommittedURL().spec();
  }

 private:
  void WriteManifest() {
    constexpr char manifest[] = R"(
      {
         "name": "Test extension",
         "version": "1",
         "manifest_version": 2,
         "permissions" : [%s],
         "background" : {
            "page" : "background.html"
         }
      }
    )";
    extension_dir_.WriteManifest(
        base::StringPrintf(manifest, has_all_urls_ ? "\"<all_urls>\"" : ""));
  }

  void WriteBackgroundPage() {
    base::ScopedAllowBlockingForTesting allow_blocking;
    CHECK(base::CopyDirectory(test_data_dir_.AppendASCII("file_iframe"),
                              extension_dir_.UnpackedPath(),
                              false /*recursive*/));
  }

  bool has_all_urls_ = false;
  bool has_file_access_ = false;
  extensions::TestExtensionDir extension_dir_;
};

// Tests that an extension frame can embed a file iframe if it has file access
// and "<all_urls>" host permissions.
IN_PROC_BROWSER_TEST_F(FileIFrameAPITest, FileAccessAllURLs) {
  set_has_all_urls(true);
  set_has_file_access(true);
  RunTest(true);
}

// Tests that an extension frame can't embed a file iframe if it has no file
// access even with the "<all_urls>" host permissions.
IN_PROC_BROWSER_TEST_F(FileIFrameAPITest, NoFileAccessAllURLs) {
  set_has_all_urls(true);
  set_has_file_access(false);
  RunTest(false);
}

// Tests that an extension frame can't embed a file iframe if it does not have
// host permissions to the file scheme even though it has file access.
IN_PROC_BROWSER_TEST_F(FileIFrameAPITest, FileAccessNoHosts) {
  set_has_all_urls(false);
  set_has_file_access(true);
  RunTest(false);
}