summaryrefslogtreecommitdiff
path: root/unittest/test_Stat.cpp
blob: ffdb68095c1849a98d8a2eea9b6aedd3b8253d1b (plain)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// Copyright (C) 2019-2023 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include "../src/Finalizer.hpp"
#include "../src/Stat.hpp"
#include "../src/Util.hpp"
#include "TestUtil.hpp"

#include <core/exceptions.hpp>
#include <core/wincompat.hpp>
#include <util/file.hpp>

#include "third_party/doctest.h"

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#ifdef _WIN32
#  include <shlobj.h>
#endif

using TestUtil::TestContext;

namespace {

bool
running_under_wine()
{
#ifdef _WIN32
  static bool is_wine =
    GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "wine_get_version")
    != nullptr;
  return is_wine;
#else
  return false;
#endif
}

bool
symlinks_supported()
{
#ifdef _WIN32
  // Windows only supports symlinks if the user has the required privilege (e.g.
  // they're an admin) or if developer mode is enabled.

  // See: https://stackoverflow.com/a/41232108/192102
  const char* dev_mode_key =
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock";
  const char* dev_mode_value = "AllowDevelopmentWithoutDevLicense";

  DWORD dev_mode_enabled = 0;
  DWORD buf_size = sizeof(dev_mode_enabled);

  return !running_under_wine()
         && (IsUserAnAdmin()
             || (RegGetValueA(HKEY_LOCAL_MACHINE,
                              dev_mode_key,
                              dev_mode_value,
                              RRF_RT_DWORD,
                              nullptr,
                              &dev_mode_enabled,
                              &buf_size)
                   == ERROR_SUCCESS
                 && dev_mode_enabled));
#else
  return true;
#endif
}

#ifdef _WIN32
bool
win32_is_junction(const std::string& path)
{
  HANDLE handle =
    CreateFileA(path.c_str(),
                FILE_READ_ATTRIBUTES,
                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                nullptr,
                OPEN_EXISTING,
                FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
                nullptr);
  if (handle == INVALID_HANDLE_VALUE) {
    return false;
  }
  FILE_ATTRIBUTE_TAG_INFO reparse_info = {};
  bool is_junction =
    (GetFileType(handle) == FILE_TYPE_DISK)
    && GetFileInformationByHandleEx(
      handle, FileAttributeTagInfo, &reparse_info, sizeof(reparse_info))
    && (reparse_info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
    && (reparse_info.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT);
  CloseHandle(handle);
  return is_junction;
}

bool
win32_get_file_info(const std::string& path, BY_HANDLE_FILE_INFORMATION* info)
{
  HANDLE handle =
    CreateFileA(path.c_str(),
                FILE_READ_ATTRIBUTES,
                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                nullptr,
                OPEN_EXISTING,
                FILE_FLAG_BACKUP_SEMANTICS,
                nullptr);
  if (handle == INVALID_HANDLE_VALUE) {
    return false;
  }
  BOOL ret = GetFileInformationByHandle(handle, info);
  CloseHandle(handle);
  return ret;
}

struct timespec
win32_filetime_to_timespec(FILETIME ft)
{
  static const int64_t SECS_BETWEEN_EPOCHS = 11644473600;
  uint64_t v =
    (static_cast<uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;

  struct timespec ts = {};
  ts.tv_sec = (v / 10000000) - SECS_BETWEEN_EPOCHS;
  ts.tv_nsec = (v % 10000000) * 100;
  return ts;
}
#endif

} // namespace

TEST_SUITE_BEGIN("Stat");

TEST_CASE("Default constructor")
{
  Stat stat;
  CHECK(!stat);
  CHECK(stat.error_number() == -1);
  CHECK(stat.device() == 0);
  CHECK(stat.inode() == 0);
  CHECK(stat.mode() == 0);
  CHECK(stat.ctime().sec() == 0);
  CHECK(stat.ctime().nsec() == 0);
  CHECK(stat.mtime().sec() == 0);
  CHECK(stat.mtime().nsec() == 0);
  CHECK(stat.size() == 0);
  CHECK(stat.size_on_disk() == 0);
  CHECK(!stat.is_directory());
  CHECK(!stat.is_regular());
  CHECK(!stat.is_symlink());

#ifdef _WIN32
  CHECK(stat.file_attributes() == 0);
  CHECK(stat.reparse_tag() == 0);
#endif
}

TEST_CASE("Named constructors")
{
  CHECK(!Stat::stat("does_not_exist"));
  CHECK(!Stat::stat("does_not_exist", Stat::OnError::ignore));
  CHECK(!Stat::stat("does_not_exist", Stat::OnError::log));
  CHECK_THROWS_WITH(Stat::stat("does_not_exist", Stat::OnError::throw_error),
                    "failed to stat does_not_exist: No such file or directory");
}

TEST_CASE("Same i-node as")
{
  TestContext test_context;

  util::write_file("a", "");
  util::write_file("b", "");
  auto a_stat = Stat::stat("a");
  auto b_stat = Stat::stat("b");

  CHECK(a_stat.same_inode_as(a_stat));
  CHECK(!a_stat.same_inode_as(b_stat));

  util::write_file("a", "change size", util::InPlace::yes);
  auto new_a_stat = Stat::stat("a");
  CHECK(new_a_stat.same_inode_as(a_stat));

  CHECK(!Stat::stat("nonexistent").same_inode_as(Stat::stat("nonexistent")));
}

TEST_CASE("Get path")
{
  TestContext test_context;

  util::write_file("a", "");
  CHECK(Stat::stat("a").path() == "a");
  CHECK(Stat::stat("does_not_exist").path() == "does_not_exist");
}

TEST_CASE("Return values when file is missing")
{
  auto stat = Stat::stat("does_not_exist");
  CHECK(!stat);
  CHECK(stat.error_number() == ENOENT);
  CHECK(stat.device() == 0);
  CHECK(stat.inode() == 0);
  CHECK(stat.mode() == 0);
  CHECK(stat.ctime().sec() == 0);
  CHECK(stat.ctime().nsec() == 0);
  CHECK(stat.mtime().sec() == 0);
  CHECK(stat.mtime().nsec() == 0);
  CHECK(stat.size() == 0);
  CHECK(stat.size_on_disk() == 0);
  CHECK(!stat.is_directory());
  CHECK(!stat.is_regular());
  CHECK(!stat.is_symlink());

#ifdef _WIN32
  CHECK(stat.file_attributes() == 0);
  CHECK(stat.reparse_tag() == 0);
#endif
}

TEST_CASE("Return values when file exists")
{
  TestContext test_context;

  util::write_file("file", "1234567");

  auto stat = Stat::stat("file");
  CHECK(stat);
  CHECK(stat.error_number() == 0);
  CHECK(!stat.is_directory());
  CHECK(stat.is_regular());
  CHECK(!stat.is_symlink());
  CHECK(stat.size() == 7);

#ifdef _WIN32
  BY_HANDLE_FILE_INFORMATION info = {};
  CHECK(win32_get_file_info("file", &info));

  CHECK(stat.device() == info.dwVolumeSerialNumber);
  CHECK((stat.inode() >> 32) == info.nFileIndexHigh);
  CHECK((stat.inode() & ((1ULL << 32) - 1)) == info.nFileIndexLow);
  CHECK(S_ISREG(stat.mode()));
  CHECK((stat.mode() & ~S_IFMT) == 0666);

  struct timespec creation_time =
    win32_filetime_to_timespec(info.ftCreationTime);
  struct timespec last_write_time =
    win32_filetime_to_timespec(info.ftLastWriteTime);

  CHECK(stat.ctime().sec() == creation_time.tv_sec);
  CHECK(stat.ctime().nsec_decimal_part() == creation_time.tv_nsec);
  CHECK(stat.mtime().sec() == last_write_time.tv_sec);
  CHECK(stat.mtime().nsec_decimal_part() == last_write_time.tv_nsec);

  CHECK(stat.size_on_disk() == ((stat.size() + 4095) & ~4095));
  CHECK(stat.file_attributes() == info.dwFileAttributes);
  CHECK(stat.reparse_tag() == 0);

#else
  struct stat st;
  CHECK(::stat("file", &st) == 0);

  CHECK(stat.device() == st.st_dev);
  CHECK(stat.inode() == st.st_ino);
  CHECK(stat.mode() == st.st_mode);
  CHECK(stat.size_on_disk() == st.st_blocks * 512);

#  ifdef HAVE_STRUCT_STAT_ST_CTIM
  CHECK(stat.ctime().sec() == st.st_ctim.tv_sec);
  CHECK(stat.ctime().nsec_decimal_part() == st.st_ctim.tv_nsec);
#  elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
  CHECK(stat.ctime().sec() == st.st_ctimespec.tv_sec);
  CHECK(stat.ctime().nsec_decimal_part() == st.st_ctimespec.tv_nsec);
#  else
  CHECK(stat.ctime().sec() == st.st_ctime);
  CHECK(stat.ctime().nsec_decimal_part() == 0);
#  endif

#  ifdef HAVE_STRUCT_STAT_ST_MTIM
  CHECK(stat.mtime().sec() == st.st_mtim.tv_sec);
  CHECK(stat.mtime().nsec_decimal_part() == st.st_mtim.tv_nsec);
#  elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
  CHECK(stat.mtime().sec() == st.st_mtimespec.tv_sec);
  CHECK(stat.mtime().nsec_decimal_part() == st.st_mtimespec.tv_nsec);
#  else
  CHECK(stat.mtime().sec() == st.st_mtime);
  CHECK(stat.mtime().nsec_decimal_part() == 0);
#  endif
#endif
}

TEST_CASE("Directory")
{
  TestContext test_context;

  REQUIRE(mkdir("directory", 0456) == 0);
  auto stat = Stat::stat("directory");

  CHECK(stat);
  CHECK(stat.error_number() == 0);
  CHECK(stat.is_directory());
  CHECK(!stat.is_regular());
  CHECK(!stat.is_symlink());
  CHECK(S_ISDIR(stat.mode()));
#ifdef _WIN32
  CHECK((stat.mode() & ~S_IFMT) == 0777);
  CHECK((stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
  CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
  CHECK(stat.reparse_tag() == 0);
#endif
}

TEST_CASE("Symlinks" * doctest::skip(!symlinks_supported()))
{
  TestContext test_context;

  util::write_file("file", "1234567");

#ifdef _WIN32
  REQUIRE(CreateSymbolicLinkA(
    "symlink", "file", 0x2 /*SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE*/));
#else
  REQUIRE(symlink("file", "symlink") == 0);
#endif

  SUBCASE("file lstat")
  {
    auto stat = Stat::lstat("file", Stat::OnError::ignore);
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISREG(stat.mode()));
    CHECK(stat.size() == 7);
#ifdef _WIN32
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
    CHECK(stat.reparse_tag() == 0);
#endif
  }

  SUBCASE("file stat")
  {
    auto stat = Stat::stat("file", Stat::OnError::ignore);
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISREG(stat.mode()));
    CHECK(stat.size() == 7);
#ifdef _WIN32
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
    CHECK(stat.reparse_tag() == 0);
#endif
  }

  SUBCASE("symlink lstat")
  {
    auto stat = Stat::lstat("symlink", Stat::OnError::ignore);
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(!stat.is_regular());
    CHECK(stat.is_symlink());
    CHECK(S_ISLNK(stat.mode()));
#ifdef _WIN32
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
    CHECK((stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
    CHECK(stat.reparse_tag() == IO_REPARSE_TAG_SYMLINK);
#else
    CHECK(stat.size() == 4);
#endif
  }

  SUBCASE("symlink stat")
  {
    auto stat = Stat::stat("symlink", Stat::OnError::ignore);
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISREG(stat.mode()));
    CHECK(stat.size() == 7);
#ifdef _WIN32
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
    CHECK(stat.reparse_tag() == 0);
#endif
  }
}

TEST_CASE("Hard links")
{
  TestContext test_context;

  util::write_file("a", "");

#ifdef _WIN32
  REQUIRE(CreateHardLinkA("b", "a", nullptr));
#else
  REQUIRE(link("a", "b") == 0);
#endif

  auto stat_a = Stat::stat("a");
  CHECK(stat_a);
  CHECK(stat_a.error_number() == 0);
  CHECK(!stat_a.is_directory());
  CHECK(stat_a.is_regular());
  CHECK(!stat_a.is_symlink());
  CHECK(stat_a.size() == 0);

  auto stat_b = Stat::stat("b");
  CHECK(stat_b);
  CHECK(stat_b.error_number() == 0);
  CHECK(!stat_b.is_directory());
  CHECK(stat_b.is_regular());
  CHECK(!stat_b.is_symlink());
  CHECK(stat_b.size() == 0);

  CHECK(stat_a.device() == stat_b.device());
  CHECK(stat_a.inode() == stat_b.inode());
  CHECK(stat_a.same_inode_as(stat_b));

  util::write_file("a", "1234567", util::InPlace::yes);
  stat_a = Stat::stat("a");
  stat_b = Stat::stat("b");

  CHECK(stat_a.size() == 7);
  CHECK(stat_b.size() == 7);
}

TEST_CASE("Special" * doctest::skip(running_under_wine()))
{
  SUBCASE("tty")
  {
#ifdef _WIN32
    auto stat = Stat::stat("\\\\.\\CON");
#else
    auto stat = Stat::stat("/dev/tty");
#endif
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(!stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISCHR(stat.mode()));
#ifdef _WIN32
    CHECK(stat.file_attributes() == 0);
    CHECK(stat.reparse_tag() == 0);
#endif
  }

  SUBCASE("null")
  {
#ifdef _WIN32
    auto stat = Stat::stat("\\\\.\\NUL");
#else
    auto stat = Stat::stat("/dev/null");
#endif
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(!stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISCHR(stat.mode()));
#ifdef _WIN32
    CHECK(stat.file_attributes() == 0);
    CHECK(stat.reparse_tag() == 0);
#endif
  }

  SUBCASE("pipe")
  {
#ifdef _WIN32
    const char* pipe_path = "\\\\.\\pipe\\InitShutdown"; // Well-known pipe name
#else
    const char* pipe_path = "my_pipe";
    REQUIRE(mkfifo(pipe_path, 0600) == 0);
#endif

    auto stat = Stat::stat(pipe_path);
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(!stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISFIFO(stat.mode()));
#ifdef _WIN32
    CHECK(stat.file_attributes() == 0);
    CHECK(stat.reparse_tag() == 0);
#endif
  }

  SUBCASE("block device")
  {
#ifdef _WIN32
    auto stat = Stat::stat("\\\\.\\C:");
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(!stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISBLK(stat.mode()));
    CHECK(stat.file_attributes() == 0);
    CHECK(stat.reparse_tag() == 0);
#endif
  }
}

#ifdef _WIN32
TEST_CASE("Win32 Readonly File")
{
  TestContext test_context;

  util::write_file("file", "");

  DWORD prev_attrs = GetFileAttributesA("file");
  REQUIRE(prev_attrs != INVALID_FILE_ATTRIBUTES);
  REQUIRE(SetFileAttributesA("file", prev_attrs | FILE_ATTRIBUTE_READONLY));

  auto stat = Stat::stat("file");
  REQUIRE(SetFileAttributesA("file", prev_attrs));

  CHECK(stat);
  CHECK(stat.error_number() == 0);
  CHECK(S_ISREG(stat.mode()));
  CHECK((stat.mode() & ~S_IFMT) == 0444);
  CHECK((stat.file_attributes() & FILE_ATTRIBUTE_READONLY));
  CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
  CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
  CHECK(stat.reparse_tag() == 0);
}

TEST_CASE("Win32 Executable File")
{
  TestContext test_context;

  const char* comspec = getenv("COMSPEC");
  REQUIRE(comspec != nullptr);

  auto stat = Stat::stat(comspec);
  CHECK(stat);
  CHECK(stat.error_number() == 0);
  CHECK(!stat.is_directory());
  CHECK(stat.is_regular());
  CHECK(!stat.is_symlink());
  CHECK(S_ISREG(stat.mode()));
  CHECK((stat.mode() & ~S_IFMT) == 0777);
  CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
  CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
  CHECK(stat.reparse_tag() == 0);
}

TEST_CASE("Win32 Pending Delete" * doctest::skip(running_under_wine()))
{
  TestContext test_context;

  HANDLE handle =
    CreateFileA("file",
                GENERIC_READ | GENERIC_WRITE | DELETE,
                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                nullptr,
                CREATE_NEW,
                FILE_ATTRIBUTE_NORMAL,
                nullptr);
  REQUIRE_MESSAGE(handle != INVALID_HANDLE_VALUE, "err=" << GetLastError());
  Finalizer cleanup([&] { CloseHandle(handle); });

  // Mark file as deleted. This puts it into a "pending delete" state that
  // will persist until the handle is closed. Until the file is closed, new
  // handles cannot be created to the file; attempts to do so fail with
  // ERROR_ACCESS_DENIED/STATUS_DELETE_PENDING. Our stat implementation maps
  // these to ENOENT.
  FILE_DISPOSITION_INFO info{};
  info.DeleteFile = TRUE;
  REQUIRE_MESSAGE(SetFileInformationByHandle(
                    handle, FileDispositionInfo, &info, sizeof(info)),
                  "err=" << GetLastError());

  SUBCASE("stat file pending delete")
  {
    auto st = Stat::stat("file");
    CHECK(!st);
    CHECK(st.error_number() == ENOENT);
  }

  SUBCASE("lstat file pending delete")
  {
    auto st = Stat::lstat("file");
    CHECK(!st);
    CHECK(st.error_number() == ENOENT);
  }
}

// Our Win32 Stat implementation should open files using FILE_READ_ATTRIBUTES,
// which bypasses sharing restrictions.
TEST_CASE("Win32 No Sharing")
{
  TestContext test_context;

  HANDLE handle = CreateFileA("file",
                              GENERIC_READ | GENERIC_WRITE,
                              0 /* no sharing */,
                              nullptr,
                              CREATE_NEW,
                              FILE_ATTRIBUTE_NORMAL,
                              nullptr);
  REQUIRE_MESSAGE(handle != INVALID_HANDLE_VALUE, "err=" << GetLastError());
  Finalizer cleanup([&] { CloseHandle(handle); });

  // Sanity check we can't open the file for read/write access.
  REQUIRE(!util::read_file<std::string>("file"));

  SUBCASE("stat file no sharing")
  {
    auto stat = Stat::stat("file");
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISREG(stat.mode()));
    CHECK(stat.size() == 0);
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
    CHECK(stat.reparse_tag() == 0);
  }

  SUBCASE("lstat file no sharing")
  {
    auto stat = Stat::lstat("file");
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISREG(stat.mode()));
    CHECK(stat.size() == 0);
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
    CHECK(stat.reparse_tag() == 0);
  }
}

// Creating a directory junction for test purposes is tricky on Windows.
// Instead, test a well-known junction that has existed in all Windows versions
// since Vista. (Not present on Wine.)
TEST_CASE("Win32 Directory Junction"
          * doctest::skip(!win32_is_junction(Util::expand_environment_variables(
            "${ALLUSERSPROFILE}\\Application Data"))))
{
  TestContext test_context;

  SUBCASE("junction stat")
  {
    auto stat = Stat::stat(Util::expand_environment_variables(
      "${ALLUSERSPROFILE}\\Application Data"));
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(stat.is_directory());
    CHECK(!stat.is_regular());
    CHECK(!stat.is_symlink());
    CHECK(S_ISDIR(stat.mode()));
    CHECK((stat.mode() & ~S_IFMT) == 0777);
    CHECK((stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
    CHECK(!(stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
    CHECK(stat.reparse_tag() == 0);
  }

  SUBCASE("junction lstat")
  {
    auto stat = Stat::lstat(Util::expand_environment_variables(
      "${ALLUSERSPROFILE}\\Application Data"));
    CHECK(stat);
    CHECK(stat.error_number() == 0);
    CHECK(!stat.is_directory());
    CHECK(!stat.is_regular());
    CHECK(!stat.is_symlink()); // Should only be true for bona fide symlinks
    CHECK((stat.mode() & S_IFMT) == 0); // Not a symlink/file/directory
    CHECK((stat.mode() & ~S_IFMT) == 0777);
    CHECK((stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY));
    CHECK((stat.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT));
    CHECK(stat.reparse_tag() == IO_REPARSE_TAG_MOUNT_POINT);
  }
}
#endif

TEST_SUITE_END();