File: file_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (435 lines) | stat: -rw-r--r-- 18,185 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/fileapi/file.h"

#include "base/run_loop.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
#include "third_party/blink/public/mojom/file/file_utilities.mojom-blink.h"
#include "third_party/blink/public/mojom/filesystem/file_system.mojom-blink.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/testing/null_execution_context.h"
#include "third_party/blink/renderer/platform/blob/testing/fake_blob.h"
#include "third_party/blink/renderer/platform/file_metadata.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_mojo.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"

namespace blink {

namespace {

class MockBlob : public FakeBlob {
 public:
  static void Create(File* file, base::Time modified_time) {
    mojo::PendingRemote<mojom::blink::Blob> remote;
    PostCrossThreadTask(
        *base::ThreadPool::CreateSingleThreadTaskRunner({}), FROM_HERE,
        CrossThreadBindOnce(
            [](const String& uuid,
               mojo::PendingReceiver<mojom::blink::Blob> receiver,
               base::Time modified_time) {
              mojo::MakeSelfOwnedReceiver(
                  std::make_unique<MockBlob>(uuid, modified_time),
                  std::move(receiver));
            },
            file->Uuid(), remote.InitWithNewPipeAndPassReceiver(),
            modified_time));
    file->GetBlobDataHandle()->SetBlobRemoteForTesting(std::move(remote));
  }

  MockBlob(const String& uuid, base::Time modified_time)
      : FakeBlob(uuid), modified_time_(modified_time) {}

  void Clone(mojo::PendingReceiver<mojom::blink::Blob> receiver) override {
    mojo::MakeSelfOwnedReceiver(
        std::make_unique<MockBlob>(uuid_, modified_time_), std::move(receiver));
  }

  void CaptureSnapshot(CaptureSnapshotCallback callback) override {
    std::move(callback).Run(
        /*size=*/0, NullableTimeToOptionalTime(modified_time_));
  }

 private:
  base::Time modified_time_;
};

using MockRegisterBlobCallback = base::OnceCallback<
    void(const String&, const KURL&, uint64_t, std::optional<base::Time>)>;
class MockFileSystemManager : public mojom::blink::FileSystemManager {
 public:
  explicit MockFileSystemManager(
      const blink::BrowserInterfaceBrokerProxy& broker)
      : broker_(broker) {
    broker.SetBinderForTesting(
        mojom::blink::FileSystemManager::Name_,
        WTF::BindRepeating(&MockFileSystemManager::BindReceiver,
                           WTF::Unretained(this)));
  }

  ~MockFileSystemManager() override {
    broker_.SetBinderForTesting(mojom::blink::FileSystemManager::Name_, {});
  }

  // mojom::blink::FileSystem
  void Open(const scoped_refptr<const SecurityOrigin>& origin,
            mojom::blink::FileSystemType file_system_type,
            OpenCallback callback) override {}
  void ResolveURL(const KURL& filesystem_url,
                  ResolveURLCallback callback) override {}
  void Move(const KURL& src_path,
            const KURL& dest_path,
            MoveCallback callback) override {}
  void Copy(const KURL& src_path,
            const KURL& dest_path,
            CopyCallback callback) override {}
  void Remove(const KURL& path,
              bool recursive,
              RemoveCallback callback) override {}
  void ReadMetadata(const KURL& path, ReadMetadataCallback callback) override {}
  void Create(const KURL& path,
              bool exclusive,
              bool is_directory,
              bool recursive,
              CreateCallback callback) override {}
  void Exists(const KURL& path,
              bool is_directory,
              ExistsCallback callback) override {}
  void ReadDirectory(
      const KURL& path,
      mojo::PendingRemote<mojom::blink::FileSystemOperationListener>
          pending_listener) override {}
  void ReadDirectorySync(const KURL& path,
                         ReadDirectorySyncCallback callback) override {}
  void Write(const KURL& file_path,
             mojo::PendingRemote<mojom::blink::Blob> blob,
             int64_t position,
             mojo::PendingReceiver<mojom::blink::FileSystemCancellableOperation>
                 op_receiver,
             mojo::PendingRemote<mojom::blink::FileSystemOperationListener>
                 pending_listener) override {}
  void WriteSync(const KURL& file_path,
                 mojo::PendingRemote<mojom::blink::Blob> blob,
                 int64_t position,
                 WriteSyncCallback callback) override {}
  void Truncate(
      const KURL& file_path,
      int64_t length,
      mojo::PendingReceiver<mojom::blink::FileSystemCancellableOperation>
          op_receiver,
      TruncateCallback callback) override {}
  void TruncateSync(const KURL& file_path,
                    int64_t length,
                    TruncateSyncCallback callback) override {}
  void CreateSnapshotFile(const KURL& file_path,
                          CreateSnapshotFileCallback callback) override {}
  void GetPlatformPath(const KURL& file_path,
                       GetPlatformPathCallback callback) override {}
  void RegisterBlob(const String& content_type,
                    const KURL& url,
                    uint64_t length,
                    std::optional<base::Time> expected_modification_time,
                    RegisterBlobCallback callback) override {
    std::move(mock_register_blob_callback_)
        .Run(content_type, url, length, expected_modification_time);
    std::move(callback).Run(BlobDataHandle::Create());
  }

  void SetMockRegisterBlobCallback(
      MockRegisterBlobCallback mock_register_blob_callback) {
    mock_register_blob_callback_ = std::move(mock_register_blob_callback);
  }

 private:
  void BindReceiver(mojo::ScopedMessagePipeHandle handle) {
    receivers_.Add(this, mojo::PendingReceiver<mojom::blink::FileSystemManager>(
                             std::move(handle)));
  }

  const BrowserInterfaceBrokerProxy& broker_;
  mojo::ReceiverSet<mojom::blink::FileSystemManager> receivers_;
  MockRegisterBlobCallback mock_register_blob_callback_;
};

void ExpectTimestampIsNow(const File& file) {
  const base::Time now = base::Time::Now();
  const base::TimeDelta delta_now = now - base::Time::UnixEpoch();
  // Because lastModified() applies floor() internally, we should compare
  // integral millisecond values.
  EXPECT_GE(file.lastModified(), delta_now.InMilliseconds());
  EXPECT_GE(file.LastModifiedTime(), now);
}

}  // namespace

TEST(FileTest, NativeFileWithoutTimestamp) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  auto* const file = MakeGarbageCollected<File>(&context.GetExecutionContext(),
                                                "/native/path");
  MockBlob::Create(file, base::Time());

  EXPECT_TRUE(file->HasBackingFile());
  EXPECT_EQ("/native/path", file->GetPath());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
  ExpectTimestampIsNow(*file);
}

TEST(FileTest, NativeFileWithUnixEpochTimestamp) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  auto* const file = MakeGarbageCollected<File>(&context.GetExecutionContext(),
                                                "/native/path");
  MockBlob::Create(file, base::Time::UnixEpoch());

  EXPECT_TRUE(file->HasBackingFile());
  EXPECT_EQ(0, file->lastModified());
  EXPECT_EQ(base::Time::UnixEpoch(), file->LastModifiedTime());
}

TEST(FileTest, NativeFileWithApocalypseTimestamp) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  auto* const file = MakeGarbageCollected<File>(&context.GetExecutionContext(),
                                                "/native/path");
  MockBlob::Create(file, base::Time::Max());

  EXPECT_TRUE(file->HasBackingFile());

  EXPECT_EQ((base::Time::Max() - base::Time::UnixEpoch()).InMilliseconds(),
            file->lastModified());
  EXPECT_EQ(base::Time::Max(), file->LastModifiedTime());
}

TEST(FileTest, BlobBackingFileWithoutTimestamp) {
  test::TaskEnvironment task_environment;
  auto* const file = MakeGarbageCollected<File>("name", std::nullopt,
                                                BlobDataHandle::Create());
  EXPECT_FALSE(file->HasBackingFile());
  EXPECT_TRUE(file->GetPath().empty());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
  ExpectTimestampIsNow(*file);
}

TEST(FileTest, BlobBackingFileWithWindowsEpochTimestamp) {
  test::TaskEnvironment task_environment;
  auto* const file = MakeGarbageCollected<File>("name", base::Time(),
                                                BlobDataHandle::Create());
  EXPECT_FALSE(file->HasBackingFile());
  EXPECT_TRUE(file->GetPath().empty());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
  EXPECT_EQ((base::Time() - base::Time::UnixEpoch()).InMilliseconds(),
            file->lastModified());
  EXPECT_EQ(base::Time(), file->LastModifiedTime());
}

TEST(FileTest, BlobBackingFileWithUnixEpochTimestamp) {
  test::TaskEnvironment task_environment;
  const scoped_refptr<BlobDataHandle> blob_data_handle =
      BlobDataHandle::Create();
  auto* const file = MakeGarbageCollected<File>("name", base::Time::UnixEpoch(),
                                                blob_data_handle);
  EXPECT_FALSE(file->HasBackingFile());
  EXPECT_TRUE(file->GetPath().empty());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
  EXPECT_EQ(INT64_C(0), file->lastModified());
  EXPECT_EQ(base::Time::UnixEpoch(), file->LastModifiedTime());
}

TEST(FileTest, BlobBackingFileWithApocalypseTimestamp) {
  test::TaskEnvironment task_environment;
  constexpr base::Time kMaxTime = base::Time::Max();
  auto* const file =
      MakeGarbageCollected<File>("name", kMaxTime, BlobDataHandle::Create());
  EXPECT_FALSE(file->HasBackingFile());
  EXPECT_TRUE(file->GetPath().empty());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
  EXPECT_EQ((kMaxTime - base::Time::UnixEpoch()).InMilliseconds(),
            file->lastModified());
  EXPECT_EQ(kMaxTime, file->LastModifiedTime());
}

TEST(FileTest, fileSystemFileWithNativeSnapshot) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  FileMetadata metadata;
  metadata.platform_path = "/native/snapshot";
  File* const file = File::CreateForFileSystemFile(
      &context.GetExecutionContext(), "name", metadata, File::kIsUserVisible);
  EXPECT_TRUE(file->HasBackingFile());
  EXPECT_EQ("/native/snapshot", file->GetPath());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
}

TEST(FileTest, fileSystemFileWithNativeSnapshotAndSize) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  FileMetadata metadata;
  metadata.length = 1024ll;
  metadata.platform_path = "/native/snapshot";
  File* const file = File::CreateForFileSystemFile(
      &context.GetExecutionContext(), "name", metadata, File::kIsUserVisible);
  EXPECT_TRUE(file->HasBackingFile());
  EXPECT_EQ("/native/snapshot", file->GetPath());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
}

TEST(FileTest, FileSystemFileWithWindowsEpochTimestamp) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  FileMetadata metadata;
  metadata.length = INT64_C(1025);
  metadata.modification_time = base::Time();
  metadata.platform_path = "/native/snapshot";
  File* const file = File::CreateForFileSystemFile(
      &context.GetExecutionContext(), "name", metadata, File::kIsUserVisible);
  EXPECT_TRUE(file->HasBackingFile());
  EXPECT_EQ("/native/snapshot", file->GetPath());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
  EXPECT_EQ(UINT64_C(1025), file->size());
  EXPECT_EQ((base::Time() - base::Time::UnixEpoch()).InMilliseconds(),
            file->lastModified());
  EXPECT_EQ(base::Time(), file->LastModifiedTime());
}

TEST(FileTest, FileSystemFileWithUnixEpochTimestamp) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  FileMetadata metadata;
  metadata.length = INT64_C(1025);
  metadata.modification_time = base::Time::UnixEpoch();
  metadata.platform_path = "/native/snapshot";
  File* const file = File::CreateForFileSystemFile(
      &context.GetExecutionContext(), "name", metadata, File::kIsUserVisible);
  EXPECT_TRUE(file->HasBackingFile());
  EXPECT_EQ("/native/snapshot", file->GetPath());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
  EXPECT_EQ(UINT64_C(1025), file->size());
  EXPECT_EQ(INT64_C(0), file->lastModified());
  EXPECT_EQ(base::Time::UnixEpoch(), file->LastModifiedTime());
}

TEST(FileTest, FileSystemFileWithApocalypseTimestamp) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  constexpr base::Time kMaxTime = base::Time::Max();
  FileMetadata metadata;
  metadata.length = INT64_C(1025);
  metadata.modification_time = kMaxTime;
  metadata.platform_path = "/native/snapshot";
  File* const file = File::CreateForFileSystemFile(
      &context.GetExecutionContext(), "name", metadata, File::kIsUserVisible);
  EXPECT_TRUE(file->HasBackingFile());
  EXPECT_EQ("/native/snapshot", file->GetPath());
  EXPECT_TRUE(file->FileSystemURL().IsEmpty());
  EXPECT_EQ(UINT64_C(1025), file->size());
  EXPECT_EQ((kMaxTime - base::Time::UnixEpoch()).InMilliseconds(),
            file->lastModified());
  EXPECT_EQ(kMaxTime, file->LastModifiedTime());
}

TEST(FileTest, fileSystemFileWithoutNativeSnapshot) {
  test::TaskEnvironment task_environment;
  KURL url("filesystem:http://example.com/isolated/hash/non-native-file");
  FileMetadata metadata;
  metadata.length = 0;
  File* const file = File::CreateForFileSystemFile(
      url, metadata, File::kIsUserVisible, BlobDataHandle::Create());
  EXPECT_FALSE(file->HasBackingFile());
  EXPECT_TRUE(file->GetPath().empty());
  EXPECT_EQ(url, file->FileSystemURL());
}

TEST(FileTest, hsaSameSource) {
  test::TaskEnvironment task_environment;
  ScopedNullExecutionContext context;
  auto* const native_file_a1 = MakeGarbageCollected<File>(
      &context.GetExecutionContext(), "/native/pathA");
  auto* const native_file_a2 = MakeGarbageCollected<File>(
      &context.GetExecutionContext(), "/native/pathA");
  auto* const native_file_b = MakeGarbageCollected<File>(
      &context.GetExecutionContext(), "/native/pathB");

  const scoped_refptr<BlobDataHandle> blob_data_a = BlobDataHandle::Create();
  const scoped_refptr<BlobDataHandle> blob_data_b = BlobDataHandle::Create();
  const base::Time kEpoch = base::Time::UnixEpoch();
  auto* const blob_file_a1 =
      MakeGarbageCollected<File>("name", kEpoch, blob_data_a);
  auto* const blob_file_a2 =
      MakeGarbageCollected<File>("name", kEpoch, blob_data_a);
  auto* const blob_file_b =
      MakeGarbageCollected<File>("name", kEpoch, blob_data_b);

  KURL url_a("filesystem:http://example.com/isolated/hash/non-native-file-A");
  KURL url_b("filesystem:http://example.com/isolated/hash/non-native-file-B");
  FileMetadata metadata;
  metadata.length = 0;
  File* const file_system_file_a1 = File::CreateForFileSystemFile(
      url_a, metadata, File::kIsUserVisible, BlobDataHandle::Create());
  File* const file_system_file_a2 = File::CreateForFileSystemFile(
      url_a, metadata, File::kIsUserVisible, BlobDataHandle::Create());
  File* const file_system_file_b = File::CreateForFileSystemFile(
      url_b, metadata, File::kIsUserVisible, BlobDataHandle::Create());

  EXPECT_FALSE(native_file_a1->HasSameSource(*blob_file_a1));
  EXPECT_FALSE(blob_file_a1->HasSameSource(*file_system_file_a1));
  EXPECT_FALSE(file_system_file_a1->HasSameSource(*native_file_a1));

  EXPECT_TRUE(native_file_a1->HasSameSource(*native_file_a1));
  EXPECT_TRUE(native_file_a1->HasSameSource(*native_file_a2));
  EXPECT_FALSE(native_file_a1->HasSameSource(*native_file_b));

  EXPECT_TRUE(blob_file_a1->HasSameSource(*blob_file_a1));
  EXPECT_TRUE(blob_file_a1->HasSameSource(*blob_file_a2));
  EXPECT_FALSE(blob_file_a1->HasSameSource(*blob_file_b));

  EXPECT_TRUE(file_system_file_a1->HasSameSource(*file_system_file_a1));
  EXPECT_TRUE(file_system_file_a1->HasSameSource(*file_system_file_a2));
  EXPECT_FALSE(file_system_file_a1->HasSameSource(*file_system_file_b));
}

TEST(FileTest, createForFileSystem) {
  test::TaskEnvironment task_environment;
  V8TestingScope scope(KURL("http://example.com"));
  Document& document = scope.GetDocument();
  base::RunLoop run_loop;

  KURL filesystem_url(
      "filesystem:http://example.com/isolated/hash/non-native-file");
  FileMetadata metadata;
  metadata.length = 0;

  MockFileSystemManager manager(
      document.GetFrame()->GetBrowserInterfaceBroker());
  manager.SetMockRegisterBlobCallback(base::BindLambdaForTesting(
      [&](const String& content_type, const KURL& url, uint64_t length,
          std::optional<base::Time> expected_modification_time) {
        EXPECT_EQ(metadata.length, static_cast<int64_t>(length));
        EXPECT_EQ("", content_type);
        EXPECT_EQ(url, filesystem_url);
        run_loop.Quit();
      }));

  File* const file = File::CreateForFileSystemFile(
      *document.GetExecutionContext(), filesystem_url, metadata,
      File::kIsUserVisible);

  run_loop.Run();
  EXPECT_TRUE(file);
}
}  // namespace blink