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
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439

content / browser / file_system_access / file_system_access_file_writer_impl_browsertest.cc [blame]

// Copyright 2019 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/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "components/services/quarantine/test_support.h"
#include "content/browser/file_system_access/file_system_access_manager_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/file_system_chooser_test_helpers.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/features_generated.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/select_file_dialog_factory.h"
#include "ui/shell_dialogs/select_file_policy.h"

#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif

namespace content {

// This browser test implements end-to-end tests for
// FileSystemAccessFileWriterImpl.
class FileSystemAccessFileWriterBrowserTest : public ContentBrowserTest {
 public:
  FileSystemAccessFileWriterBrowserTest() {
    scoped_features_.InitAndEnableFeature(
        blink::features::kFileSystemAccessLocal);
  }

  void SetUp() override {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());

    ASSERT_TRUE(embedded_test_server()->Start());
    test_url_ = embedded_test_server()->GetURL("/title1.html");

    ContentBrowserTest::SetUp();
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    // Enable experimental web platform features to enable write access.
    command_line->AppendSwitch(
        switches::kEnableExperimentalWebPlatformFeatures);
  }

  void TearDown() override {
    ContentBrowserTest::TearDown();
    ASSERT_TRUE(temp_dir_.Delete());
    ui::SelectFileDialog::SetFactory(nullptr);
  }

  std::pair<base::FilePath, base::FilePath> CreateTestFilesAndEntry(
      const std::string& contents) {
    base::ScopedAllowBlockingForTesting allow_blocking;
    base::FilePath test_file;
    EXPECT_TRUE(
        base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_file));
    EXPECT_TRUE(base::WriteFile(test_file, contents));

    ui::SelectFileDialog::SetFactory(
        std::make_unique<FakeSelectFileDialogFactory>(
            std::vector<base::FilePath>{test_file}));
    EXPECT_TRUE(NavigateToURL(shell(), test_url_));
    EXPECT_EQ(test_file.BaseName().AsUTF8Unsafe(),
              EvalJs(shell(),
                     "(async () => {"
                     "  let [e] = await self.showOpenFilePicker();"
                     "  self.entry = e;"
                     "  self.writers = [];"
                     "  return e.name; })()"));

    const base::FilePath swap_file =
        base::FilePath(test_file).AddExtensionASCII(".crswap");
    return std::make_pair(test_file, swap_file);
  }

  std::pair<base::FilePath, base::FilePath>
  CreateQuarantineTestFilesAndEntry() {
    base::ScopedAllowBlockingForTesting allow_blocking;
    base::FilePath test_file =
        temp_dir_.GetPath().AppendASCII("to_be_quarantined.exe");
    std::string file_data = "hello world!";
    EXPECT_TRUE(base::WriteFile(test_file, file_data));

    ui::SelectFileDialog::SetFactory(
        std::make_unique<FakeSelectFileDialogFactory>(
            std::vector<base::FilePath>{test_file}));
    EXPECT_TRUE(NavigateToURL(shell(), test_url_));
    EXPECT_EQ(test_file.BaseName().AsUTF8Unsafe(),
              EvalJs(shell(),
                     "(async () => {"
                     "  let [e] = await self.showOpenFilePicker();"
                     "  self.entry = e;"
                     "  self.writers = [];"
                     "  return e.name; })()"));

    const base::FilePath swap_file =
        base::FilePath(test_file).AddExtensionASCII(".crswap");
    return std::make_pair(test_file, swap_file);
  }

 protected:
  base::ScopedTempDir temp_dir_;
  GURL test_url_;

 private:
  base::test::ScopedFeatureList scoped_features_;
};

IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       ContentsWrittenToSwapFileFirst) {
  auto [test_file, swap_file] = CreateTestFilesAndEntry("");
  const std::string file_contents = "file contents to write";

  EXPECT_EQ(0,
            EvalJs(shell(),
                   JsReplace("(async () => {"
                             "  const w = await self.entry.createWritable();"
                             "  await w.write(new Blob([$1]));"
                             "  self.writer = w;"
                             "  return (await self.entry.getFile()).size; })()",
                             file_contents)));
  {
    // Destination file should be empty, contents written in the swap file.
    base::ScopedAllowBlockingForTesting allow_blocking;
    std::string read_contents;
    EXPECT_TRUE(base::ReadFileToString(test_file, &read_contents));
    EXPECT_EQ("", read_contents);
    std::string swap_contents;
    EXPECT_TRUE(base::ReadFileToString(swap_file, &swap_contents));
    EXPECT_EQ(file_contents, swap_contents);
  }

  // Contents now in destination file.
  EXPECT_EQ(static_cast<int>(file_contents.size()),
            EvalJs(shell(),
                   "(async () => {"
                   "  await self.writer.close();"
                   "  return (await self.entry.getFile()).size; })()"));
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    std::string read_contents;
    EXPECT_TRUE(base::ReadFileToString(test_file, &read_contents));
    EXPECT_EQ(file_contents, read_contents);

    EXPECT_FALSE(base::PathExists(swap_file));
  }
}

IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       KeepExistingDataHasPreviousContent) {
  const std::string initial_contents = "fooks";
  const std::string expected_contents = "barks";
  auto [test_file, swap_file] = CreateTestFilesAndEntry(initial_contents);

  EXPECT_EQ(nullptr, EvalJs(shell(),
                            "(async () => {"
                            "    const w = await self.entry.createWritable({"
                            "      keepExistingData: true });"
                            "    self.writer = w;"
                            "})()"));
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    EXPECT_TRUE(base::PathExists(swap_file));
    std::string swap_contents;
    EXPECT_TRUE(base::ReadFileToString(swap_file, &swap_contents));
    EXPECT_EQ(initial_contents, swap_contents);
  }

  EXPECT_EQ(static_cast<int>(expected_contents.size()),
            EvalJs(shell(),
                   "(async () => {"
                   "  await self.writer.write(new Blob(['bar']));"
                   "  await self.writer.close();"
                   "  return (await self.entry.getFile()).size; })()"));
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    std::string read_contents;
    EXPECT_TRUE(base::ReadFileToString(test_file, &read_contents));
    EXPECT_EQ(expected_contents, read_contents);
  }
}

IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       CreateWriterNoKeepExistingWithEmptyFile) {
  const std::string initial_contents = "very long string";
  const std::string expected_contents = "bar";
  auto [test_file, swap_file] = CreateTestFilesAndEntry(initial_contents);

  EXPECT_EQ(nullptr, EvalJs(shell(),
                            "(async () => {"
                            "  const w = await self.entry.createWritable({"
                            "    keepExistingData: false });"
                            "  self.writer = w;"
                            "})()"));
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    EXPECT_TRUE(base::PathExists(swap_file));
    std::string swap_contents;
    EXPECT_TRUE(base::ReadFileToString(swap_file, &swap_contents));
    EXPECT_EQ("", swap_contents);
  }

  EXPECT_EQ(static_cast<int>(expected_contents.size()),
            EvalJs(shell(),
                   "(async () => {"
                   "  await self.writer.write(new Blob(['bar']));"
                   "  await self.writer.close();"
                   "  return (await self.entry.getFile()).size; })()"));
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    std::string read_contents;
    EXPECT_TRUE(base::ReadFileToString(test_file, &read_contents));
    EXPECT_EQ(expected_contents, read_contents);
  }
}

IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       EachWriterHasUniqueSwapFile) {
  auto [test_file, base_swap_file] = CreateTestFilesAndEntry("");

  int num_writers = 5;
  for (int index = 0; index < num_writers; index++) {
    EXPECT_EQ(nullptr, EvalJs(shell(),
                              "(async () => {"
                              "  const w = await self.entry.createWritable();"
                              "  self.writers.push(w);"
                              "})()"));
  }

  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    for (int index = 0; index < num_writers; index++) {
      base::FilePath swap_file = base_swap_file;
      if (index != 0) {
        swap_file = base::FilePath(test_file).AddExtensionASCII(
            base::StringPrintf(".%d.crswap", index));
      }
      EXPECT_TRUE(base::PathExists(swap_file));
    }
  }
}

IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       EachWriterHasUniqueSwapFileRacy) {
  auto [test_file, base_swap_file] = CreateTestFilesAndEntry("");

  int num_writers = 5;
  for (int index = 0; index < num_writers; index++) {
    EXPECT_EQ(
        nullptr,
        EvalJs(shell(),
               JsReplace("(async () => {"
                         "  for(let i = 0; i < $1; i++ ) {"
                         "    self.writers.push(self.entry.createWritable());"
                         "  }"
                         "  await Promise.all(self.writers);"
                         "})()",
                         num_writers)));
  }

  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    for (int index = 0; index < num_writers; index++) {
      base::FilePath swap_file = base_swap_file;
      if (index != 0) {
        swap_file = base::FilePath(test_file).AddExtensionASCII(
            base::StringPrintf(".%d.crswap", index));
      }
      EXPECT_TRUE(base::PathExists(swap_file));
    }
  }
}

IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       EachWriterHasUniqueSwapFileRacyKeepExistingData) {
  auto [test_file, base_swap_file] = CreateTestFilesAndEntry("");

  int num_writers = 5;
  for (int index = 0; index < num_writers; index++) {
    EXPECT_EQ(
        nullptr,
        EvalJs(shell(), JsReplace("(async () => {"
                                  "  for(let i = 0; i < $1; i++ ) {"
                                  "self.writers.push(self.entry.createWritable("
                                  "{keepExistingData: true}));"
                                  "  }"
                                  "  await Promise.all(self.writers);"
                                  "})()",
                                  num_writers)));
  }

  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    for (int index = 0; index < num_writers; index++) {
      base::FilePath swap_file = base_swap_file;
      if (index != 0) {
        swap_file = base::FilePath(test_file).AddExtensionASCII(
            base::StringPrintf(".%d.crswap", index));
      }
      EXPECT_TRUE(base::PathExists(swap_file));
    }
  }
}

IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       WriteOffsetAndSeekInSameWritable) {
  // Performing a second write operation with a valid offset, after performing
  // a seek operation occurs at the expected index (https://crbug.com/1427819).
  auto [test_file, base_swap_file] = CreateTestFilesAndEntry("");
  EXPECT_EQ("abc1234h",
            EvalJs(shell(),
                   "(async () => {"
                   "const w = await self.entry.createWritable();"
                   "await w.write('abcdefgh');"
                   "await w.seek(0);"
                   "await w.write({type:'write',data:'123',position:3});"
                   "await w.write('4');"
                   "await w.close();"
                   "return (await (await self.entry.getFile()).text());"
                   "})()"));
}

// Ideally this would be tested by WPTs, but the location of the swap file is
// not specified and not easily accessible.
IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       CannotCreateWritableToSwapFile) {
  {
    base::FilePath test_dir;
    base::ScopedAllowBlockingForTesting allow_blocking;
    EXPECT_TRUE(base::CreateTemporaryDirInDir(
        temp_dir_.GetPath(), FILE_PATH_LITERAL("parent"), &test_dir));

    ui::SelectFileDialog::SetFactory(
        std::make_unique<FakeSelectFileDialogFactory>(
            std::vector<base::FilePath>{test_dir}));
    EXPECT_TRUE(NavigateToURL(shell(), test_url_));
    EXPECT_EQ(test_dir.BaseName().AsUTF8Unsafe(),
              EvalJs(shell(),
                     "(async () => {"
                     "  let dir = await self.showDirectoryPicker();"
                     "  self.parent = dir;"
                     "  return dir.name; })()"));
  }

  // Unsuccessfully attempt to create a writer to the swap file, which is
  // locked.
  auto result = EvalJs(
      shell(),
      "(async () => {"
      "const file = await self.parent.getFileHandle('file.txt', {create:true});"
      "self.writer = await file.createWritable();"
      "await self.writer.write('abcdefgh');"
      "self.swapFile = await self.parent.getFileHandle('file.txt.crswap', "
      "{create:false});"
      "return (await self.swapFile.createWritable());"
      "})()");
  EXPECT_TRUE(base::Contains(result.error, "modifications are not allowed."))
      << result.error;

  auto close_result = EvalJs(shell(),
                             "(async () => {"
                             "await self.writer.close();"
                             "})()");
  EXPECT_TRUE(close_result.error.empty()) << close_result.error;
}

// TODO(crbug.com/40639570): Files are only quarantined on windows in
// browsertests unfortunately. Change this when more platforms are enabled.
#if BUILDFLAG(IS_WIN)
#define MAYBE_FileAnnotated FileAnnotated
#else
#define MAYBE_FileAnnotated DISABLED_FileAnnotated
#endif  // BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       MAYBE_FileAnnotated) {
  auto [test_file, swap_file] = CreateQuarantineTestFilesAndEntry();

  EXPECT_EQ(nullptr, EvalJs(shell(),
                            "(async () => {"
                            "  const w = await self.entry.createWritable("
                            "    {keepExistingData: true},"
                            "  );"
                            "  self.writer = w;"
                            "  await self.writer.close();"
                            "})()"));

  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    EXPECT_FALSE(base::PathExists(swap_file));
    EXPECT_TRUE(quarantine::IsFileQuarantined(test_file, GURL(), test_url_));
  }
}

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(FileSystemAccessFileWriterBrowserTest,
                       RespectOSPermissions) {
  auto [test_file, swap_file] = CreateTestFilesAndEntry("");

  // Make the file read-only.
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
#if BUILDFLAG(IS_POSIX)
    int mode = 0444;
    EXPECT_TRUE(base::SetPosixFilePermissions(test_file, mode));
#elif BUILDFLAG(IS_WIN)
    DWORD attributes = ::GetFileAttributes(test_file.value().c_str());
    ASSERT_NE(attributes, INVALID_FILE_ATTRIBUTES);
    attributes |= FILE_ATTRIBUTE_READONLY;
    EXPECT_TRUE(::SetFileAttributes(test_file.value().c_str(), attributes));
#endif  // BUILDFLAG(IS_POSIX)
  }

  auto result = EvalJs(shell(),
                       "(async () => {"
                       "  return (await self.entry.createWritable()); })()");
  EXPECT_TRUE(base::Contains(result.error, "Cannot write to a read-only file."))
      << result.error;
}
#endif  // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_WIN)

}  // namespace content