summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/stat.cc
blob: 0e914215d6c2eea97262253d06240fe82afa1c25 (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
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <unistd.h>

#include <string>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gtest/gtest.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "test/syscalls/linux/file_base.h"
#include "test/util/cleanup.h"
#include "test/util/file_descriptor.h"
#include "test/util/fs_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"

namespace gvisor {
namespace testing {

namespace {

class StatTest : public FileTest {};

TEST_F(StatTest, FstatatAbs) {
  struct stat st;

  // Check that the stat works.
  EXPECT_THAT(fstatat(AT_FDCWD, test_file_name_.c_str(), &st, 0),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(st.st_mode));
}

TEST_F(StatTest, FstatatEmptyPath) {
  struct stat st;
  const auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(test_file_name_, O_RDONLY));

  // Check that the stat works.
  EXPECT_THAT(fstatat(fd.get(), "", &st, AT_EMPTY_PATH), SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(st.st_mode));
}

TEST_F(StatTest, FstatatRel) {
  struct stat st;
  int dirfd;
  auto filename = std::string(Basename(test_file_name_));

  // Open the temporary directory read-only.
  ASSERT_THAT(dirfd = open(GetAbsoluteTestTmpdir().c_str(), O_RDONLY),
              SyscallSucceeds());

  // Check that the stat works.
  EXPECT_THAT(fstatat(dirfd, filename.c_str(), &st, 0), SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(st.st_mode));
  close(dirfd);
}

TEST_F(StatTest, FstatatSymlink) {
  struct stat st;

  // Check that the link is followed.
  EXPECT_THAT(fstatat(AT_FDCWD, "/proc/self", &st, 0), SyscallSucceeds());
  EXPECT_TRUE(S_ISDIR(st.st_mode));
  EXPECT_FALSE(S_ISLNK(st.st_mode));

  // Check that the flag works.
  EXPECT_THAT(fstatat(AT_FDCWD, "/proc/self", &st, AT_SYMLINK_NOFOLLOW),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISLNK(st.st_mode));
  EXPECT_FALSE(S_ISDIR(st.st_mode));
}

TEST_F(StatTest, Nlinks) {
  TempPath basedir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  // Directory is initially empty, it should contain 2 links (one from itself,
  // one from ".").
  EXPECT_THAT(Links(basedir.path()), IsPosixErrorOkAndHolds(2));

  // Create a file in the test directory. Files shouldn't increase the link
  // count on the base directory.
  TempPath file1 =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(basedir.path()));
  EXPECT_THAT(Links(basedir.path()), IsPosixErrorOkAndHolds(2));

  // Create subdirectories. This should increase the link count by 1 per
  // subdirectory.
  TempPath dir1 =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(basedir.path()));
  EXPECT_THAT(Links(basedir.path()), IsPosixErrorOkAndHolds(3));
  TempPath dir2 =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(basedir.path()));
  EXPECT_THAT(Links(basedir.path()), IsPosixErrorOkAndHolds(4));

  // Removing directories should reduce the link count.
  dir1.reset();
  EXPECT_THAT(Links(basedir.path()), IsPosixErrorOkAndHolds(3));
  dir2.reset();
  EXPECT_THAT(Links(basedir.path()), IsPosixErrorOkAndHolds(2));

  // Removing files should have no effect on link count.
  file1.reset();
  EXPECT_THAT(Links(basedir.path()), IsPosixErrorOkAndHolds(2));
}

TEST_F(StatTest, BlocksIncreaseOnWrite) {
  struct stat st;

  // Stat the empty file.
  ASSERT_THAT(fstat(test_file_fd_.get(), &st), SyscallSucceeds());

  const int initial_blocks = st.st_blocks;

  // Write to the file, making sure to exceed the block size.
  std::vector<char> buf(2 * st.st_blksize, 'a');
  ASSERT_THAT(write(test_file_fd_.get(), buf.data(), buf.size()),
              SyscallSucceedsWithValue(buf.size()));

  // Stat the file again, and verify that number of allocated blocks has
  // increased.
  ASSERT_THAT(fstat(test_file_fd_.get(), &st), SyscallSucceeds());
  EXPECT_GT(st.st_blocks, initial_blocks);
}

TEST_F(StatTest, PathNotCleaned) {
  TempPath basedir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  // Create a file in the basedir.
  TempPath file =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(basedir.path()));

  // Stating the file directly should succeed.
  struct stat buf;
  EXPECT_THAT(lstat(file.path().c_str(), &buf), SyscallSucceeds());

  // Try to stat the file using a directory that does not exist followed by
  // "..".  If the path is cleaned prior to stating (which it should not be)
  // then this will succeed.
  const std::string bad_path = JoinPath("/does_not_exist/..", file.path());
  EXPECT_THAT(lstat(bad_path.c_str(), &buf), SyscallFailsWithErrno(ENOENT));
}

TEST_F(StatTest, PathCanContainDotDot) {
  TempPath basedir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  TempPath subdir =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(basedir.path()));
  const std::string subdir_name = std::string(Basename(subdir.path()));

  // Create a file in the subdir.
  TempPath file =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(subdir.path()));
  const std::string file_name = std::string(Basename(file.path()));

  // Stat the file through a path that includes '..' and '.' but still resolves
  // to the file.
  const std::string good_path =
      JoinPath(basedir.path(), subdir_name, "..", subdir_name, ".", file_name);
  struct stat buf;
  EXPECT_THAT(lstat(good_path.c_str(), &buf), SyscallSucceeds());
}

TEST_F(StatTest, PathCanContainEmptyComponent) {
  TempPath basedir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  // Create a file in the basedir.
  TempPath file =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(basedir.path()));
  const std::string file_name = std::string(Basename(file.path()));

  // Stat the file through a path that includes an empty component.  We have to
  // build this ourselves because JoinPath automatically removes empty
  // components.
  const std::string good_path = absl::StrCat(basedir.path(), "//", file_name);
  struct stat buf;
  EXPECT_THAT(lstat(good_path.c_str(), &buf), SyscallSucceeds());
}

TEST_F(StatTest, TrailingSlashNotCleanedReturnsENOTDIR) {
  TempPath basedir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  // Create a file in the basedir.
  TempPath file =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(basedir.path()));

  // Stat the file with an extra "/" on the end of it.  Since file is not a
  // directory, this should return ENOTDIR.
  const std::string bad_path = absl::StrCat(file.path(), "/");
  struct stat buf;
  EXPECT_THAT(lstat(bad_path.c_str(), &buf), SyscallFailsWithErrno(ENOTDIR));
}

// Test fstatating a symlink directory.
TEST_F(StatTest, FstatatSymlinkDir) {
  // Create a directory and symlink to it.
  const auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  const std::string symlink_to_dir = NewTempAbsPath();
  EXPECT_THAT(symlink(dir.path().c_str(), symlink_to_dir.c_str()),
              SyscallSucceeds());
  auto cleanup = Cleanup([&symlink_to_dir]() {
    EXPECT_THAT(unlink(symlink_to_dir.c_str()), SyscallSucceeds());
  });

  // Fstatat the link with AT_SYMLINK_NOFOLLOW should return symlink data.
  struct stat st = {};
  EXPECT_THAT(
      fstatat(AT_FDCWD, symlink_to_dir.c_str(), &st, AT_SYMLINK_NOFOLLOW),
      SyscallSucceeds());
  EXPECT_FALSE(S_ISDIR(st.st_mode));
  EXPECT_TRUE(S_ISLNK(st.st_mode));

  // Fstatat the link should return dir data.
  EXPECT_THAT(fstatat(AT_FDCWD, symlink_to_dir.c_str(), &st, 0),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISDIR(st.st_mode));
  EXPECT_FALSE(S_ISLNK(st.st_mode));
}

// Test fstatating a symlink directory with trailing slash.
TEST_F(StatTest, FstatatSymlinkDirWithTrailingSlash) {
  // Create a directory and symlink to it.
  const auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  const std::string symlink_to_dir = NewTempAbsPath();
  EXPECT_THAT(symlink(dir.path().c_str(), symlink_to_dir.c_str()),
              SyscallSucceeds());
  auto cleanup = Cleanup([&symlink_to_dir]() {
    EXPECT_THAT(unlink(symlink_to_dir.c_str()), SyscallSucceeds());
  });

  // Fstatat on the symlink with a trailing slash should return the directory
  // data.
  struct stat st = {};
  EXPECT_THAT(
      fstatat(AT_FDCWD, absl::StrCat(symlink_to_dir, "/").c_str(), &st, 0),
      SyscallSucceeds());
  EXPECT_TRUE(S_ISDIR(st.st_mode));
  EXPECT_FALSE(S_ISLNK(st.st_mode));

  // Fstatat on the symlink with a trailing slash with AT_SYMLINK_NOFOLLOW
  // should return the directory data.
  // Symlink to directory with trailing slash will ignore AT_SYMLINK_NOFOLLOW.
  EXPECT_THAT(fstatat(AT_FDCWD, absl::StrCat(symlink_to_dir, "/").c_str(), &st,
                      AT_SYMLINK_NOFOLLOW),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISDIR(st.st_mode));
  EXPECT_FALSE(S_ISLNK(st.st_mode));
}

// Test fstatating a symlink directory with a trailing slash
// should return same stat data with fstatating directory.
TEST_F(StatTest, FstatatSymlinkDirWithTrailingSlashSameInode) {
  // Create a directory and symlink to it.
  const auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  // We are going to assert that the symlink inode id is the same as the linked
  // dir's inode id. In order for the inode id to be stable across
  // save/restore, it must be kept open. The FileDescriptor type will do that
  // for us automatically.
  auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(dir.path(), O_RDONLY | O_DIRECTORY));

  const std::string symlink_to_dir = NewTempAbsPath();
  EXPECT_THAT(symlink(dir.path().c_str(), symlink_to_dir.c_str()),
              SyscallSucceeds());
  auto cleanup = Cleanup([&symlink_to_dir]() {
    EXPECT_THAT(unlink(symlink_to_dir.c_str()), SyscallSucceeds());
  });

  // Fstatat on the symlink with a trailing slash should return the directory
  // data.
  struct stat st = {};
  EXPECT_THAT(fstatat(AT_FDCWD, absl::StrCat(symlink_to_dir, "/").c_str(), &st,
                      AT_SYMLINK_NOFOLLOW),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISDIR(st.st_mode));

  // Dir and symlink should point to same inode.
  struct stat st_dir = {};
  EXPECT_THAT(
      fstatat(AT_FDCWD, dir.path().c_str(), &st_dir, AT_SYMLINK_NOFOLLOW),
      SyscallSucceeds());
  EXPECT_EQ(st.st_ino, st_dir.st_ino);
}

TEST_F(StatTest, LeadingDoubleSlash) {
  // Create a file, and make sure we can stat it.
  TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  struct stat st;
  ASSERT_THAT(lstat(file.path().c_str(), &st), SyscallSucceeds());

  // Now add an extra leading slash.
  const std::string double_slash_path = absl::StrCat("/", file.path());
  ASSERT_TRUE(absl::StartsWith(double_slash_path, "//"));

  // We should be able to stat the new path, and it should resolve to the same
  // file (same device and inode).
  struct stat double_slash_st;
  ASSERT_THAT(lstat(double_slash_path.c_str(), &double_slash_st),
              SyscallSucceeds());
  EXPECT_EQ(st.st_dev, double_slash_st.st_dev);
  EXPECT_EQ(st.st_ino, double_slash_st.st_ino);
}

// Test that a rename doesn't change the underlying file.
TEST_F(StatTest, StatDoesntChangeAfterRename) {
  const TempPath old_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  const TempPath new_path(NewTempAbsPath());

  struct stat st_old = {};
  struct stat st_new = {};

  ASSERT_THAT(stat(old_dir.path().c_str(), &st_old), SyscallSucceeds());
  ASSERT_THAT(rename(old_dir.path().c_str(), new_path.path().c_str()),
              SyscallSucceeds());
  ASSERT_THAT(stat(new_path.path().c_str(), &st_new), SyscallSucceeds());

  EXPECT_EQ(st_old.st_nlink, st_new.st_nlink);
  EXPECT_EQ(st_old.st_dev, st_new.st_dev);
  EXPECT_EQ(st_old.st_ino, st_new.st_ino);
  EXPECT_EQ(st_old.st_mode, st_new.st_mode);
  EXPECT_EQ(st_old.st_uid, st_new.st_uid);
  EXPECT_EQ(st_old.st_gid, st_new.st_gid);
  EXPECT_EQ(st_old.st_size, st_new.st_size);
}

// Test link counts with a regular file as the child.
TEST_F(StatTest, LinkCountsWithRegularFileChild) {
  const TempPath dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  struct stat st_parent_before = {};
  ASSERT_THAT(stat(dir.path().c_str(), &st_parent_before), SyscallSucceeds());
  EXPECT_EQ(st_parent_before.st_nlink, 2);

  // Adding a regular file doesn't adjust the parent's link count.
  const TempPath child =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir.path()));

  struct stat st_parent_after = {};
  ASSERT_THAT(stat(dir.path().c_str(), &st_parent_after), SyscallSucceeds());
  EXPECT_EQ(st_parent_after.st_nlink, 2);

  // The child should have a single link from the parent.
  struct stat st_child = {};
  ASSERT_THAT(stat(child.path().c_str(), &st_child), SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(st_child.st_mode));
  EXPECT_EQ(st_child.st_nlink, 1);

  // Finally unlinking the child should not affect the parent's link count.
  ASSERT_THAT(unlink(child.path().c_str()), SyscallSucceeds());
  ASSERT_THAT(stat(dir.path().c_str(), &st_parent_after), SyscallSucceeds());
  EXPECT_EQ(st_parent_after.st_nlink, 2);
}

// This test verifies that inodes remain around when there is an open fd
// after link count hits 0.
TEST_F(StatTest, ZeroLinksOpenFdRegularFileChild_NoRandomSave) {
  // Setting the enviornment variable GVISOR_GOFER_UNCACHED to any value
  // will prevent this test from running, see the tmpfs lifecycle.
  //
  // We need to support this because when a file is unlinked and we forward
  // the stat to the gofer it would return ENOENT.
  const char* uncached_gofer = getenv("GVISOR_GOFER_UNCACHED");
  SKIP_IF(uncached_gofer != nullptr);

  // We don't support saving unlinked files.
  const DisableSave ds;

  const TempPath dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  const TempPath child = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
      dir.path(), "hello", TempPath::kDefaultFileMode));

  // The child should have a single link from the parent.
  struct stat st_child_before = {};
  ASSERT_THAT(stat(child.path().c_str(), &st_child_before), SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(st_child_before.st_mode));
  EXPECT_EQ(st_child_before.st_nlink, 1);
  EXPECT_EQ(st_child_before.st_size, 5);  // Hello is 5 bytes.

  // Open the file so we can fstat after unlinking.
  const FileDescriptor fd =
      ASSERT_NO_ERRNO_AND_VALUE(Open(child.path(), O_RDONLY));

  // Now a stat should return ENOENT but we should still be able to stat
  // via the open fd and fstat.
  ASSERT_THAT(unlink(child.path().c_str()), SyscallSucceeds());

  // Since the file has no more links stat should fail.
  struct stat st_child_after = {};
  ASSERT_THAT(stat(child.path().c_str(), &st_child_after),
              SyscallFailsWithErrno(ENOENT));

  // Fstat should still allow us to access the same file via the fd.
  struct stat st_child_fd = {};
  ASSERT_THAT(fstat(fd.get(), &st_child_fd), SyscallSucceeds());
  EXPECT_EQ(st_child_before.st_dev, st_child_fd.st_dev);
  EXPECT_EQ(st_child_before.st_ino, st_child_fd.st_ino);
  EXPECT_EQ(st_child_before.st_mode, st_child_fd.st_mode);
  EXPECT_EQ(st_child_before.st_uid, st_child_fd.st_uid);
  EXPECT_EQ(st_child_before.st_gid, st_child_fd.st_gid);
  EXPECT_EQ(st_child_before.st_size, st_child_fd.st_size);

  // TODO(b/34861058): This isn't ideal but since fstatfs(2) will always return
  // OVERLAYFS_SUPER_MAGIC we have no way to know if this fs is backed by a
  // gofer which doesn't support links.
  EXPECT_TRUE(st_child_fd.st_nlink == 0 || st_child_fd.st_nlink == 1);
}

// Test link counts with a directory as the child.
TEST_F(StatTest, LinkCountsWithDirChild) {
  const TempPath dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  // Before a child is added the two links are "." and the link from the parent.
  struct stat st_parent_before = {};
  ASSERT_THAT(stat(dir.path().c_str(), &st_parent_before), SyscallSucceeds());
  EXPECT_EQ(st_parent_before.st_nlink, 2);

  // Create a subdirectory and stat for the parent link counts.
  const TempPath sub_dir =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir.path()));

  // The three links are ".", the link from the parent, and the link from
  // the child as "..".
  struct stat st_parent_after = {};
  ASSERT_THAT(stat(dir.path().c_str(), &st_parent_after), SyscallSucceeds());
  EXPECT_EQ(st_parent_after.st_nlink, 3);

  // The child will have 1 link from the parent and 1 link which represents ".".
  struct stat st_child = {};
  ASSERT_THAT(stat(sub_dir.path().c_str(), &st_child), SyscallSucceeds());
  EXPECT_TRUE(S_ISDIR(st_child.st_mode));
  EXPECT_EQ(st_child.st_nlink, 2);

  // Finally delete the child dir and the parent link count should return to 2.
  ASSERT_THAT(rmdir(sub_dir.path().c_str()), SyscallSucceeds());
  ASSERT_THAT(stat(dir.path().c_str(), &st_parent_after), SyscallSucceeds());

  // Now we should only have links from the parent and "." since the subdir
  // has been removed.
  EXPECT_EQ(st_parent_after.st_nlink, 2);
}

// Test statting a child of a non-directory.
TEST_F(StatTest, ChildOfNonDir) {
  // Create a path that has a child of a regular file.
  const std::string filename = JoinPath(test_file_name_, "child");

  // Statting the path should return ENOTDIR.
  struct stat st;
  EXPECT_THAT(lstat(filename.c_str(), &st), SyscallFailsWithErrno(ENOTDIR));
}

// Test lstating a symlink directory.
TEST_F(StatTest, LstatSymlinkDir) {
  // Create a directory and symlink to it.
  const auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  const std::string symlink_to_dir = NewTempAbsPath();
  EXPECT_THAT(symlink(dir.path().c_str(), symlink_to_dir.c_str()),
              SyscallSucceeds());
  auto cleanup = Cleanup([&symlink_to_dir]() {
    EXPECT_THAT(unlink(symlink_to_dir.c_str()), SyscallSucceeds());
  });

  // Lstat on the symlink should return symlink data.
  struct stat st = {};
  ASSERT_THAT(lstat(symlink_to_dir.c_str(), &st), SyscallSucceeds());
  EXPECT_FALSE(S_ISDIR(st.st_mode));
  EXPECT_TRUE(S_ISLNK(st.st_mode));

  // Lstat on the symlink with a trailing slash should return the directory
  // data.
  ASSERT_THAT(lstat(absl::StrCat(symlink_to_dir, "/").c_str(), &st),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISDIR(st.st_mode));
  EXPECT_FALSE(S_ISLNK(st.st_mode));
}

// Verify that we get an ELOOP from too many symbolic links even when there
// are directories in the middle.
TEST_F(StatTest, LstatELOOPPath) {
  const TempPath dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  std::string subdir_base = "subdir";
  ASSERT_THAT(mkdir(JoinPath(dir.path(), subdir_base).c_str(), 0755),
              SyscallSucceeds());

  std::string target = JoinPath(dir.path(), subdir_base, subdir_base);
  std::string dst = JoinPath("..", subdir_base);
  ASSERT_THAT(symlink(dst.c_str(), target.c_str()), SyscallSucceeds());
  auto cleanup = Cleanup(
      [&target]() { EXPECT_THAT(unlink(target.c_str()), SyscallSucceeds()); });

  // Now build a path which is /subdir/subdir/... repeated many times so that
  // we can build a path that is shorter than PATH_MAX but can still cause
  // too many symbolic links. Note: Every other subdir is actually a directory
  // so we're not in a situation where it's a -> b -> a -> b, where a and b
  // are symbolic links.
  std::string path = dir.path();
  std::string subdir_append = absl::StrCat("/", subdir_base);
  do {
    absl::StrAppend(&path, subdir_append);
    // Keep appending /subdir until we would overflow PATH_MAX.
  } while ((path.size() + subdir_append.size()) < PATH_MAX);

  struct stat s = {};
  ASSERT_THAT(lstat(path.c_str(), &s), SyscallFailsWithErrno(ELOOP));
}

// Ensure that inode allocation for anonymous devices work correctly across
// save/restore. In particular, inode numbers should be unique across S/R.
TEST(SimpleStatTest, AnonDeviceAllocatesUniqueInodesAcrossSaveRestore) {
  // Use sockets as a convenient way to create inodes on an anonymous device.
  int fd;
  ASSERT_THAT(fd = socket(AF_UNIX, SOCK_STREAM, 0), SyscallSucceeds());
  FileDescriptor fd1(fd);
  MaybeSave();
  ASSERT_THAT(fd = socket(AF_UNIX, SOCK_STREAM, 0), SyscallSucceeds());
  FileDescriptor fd2(fd);

  struct stat st1;
  struct stat st2;
  ASSERT_THAT(fstat(fd1.get(), &st1), SyscallSucceeds());
  ASSERT_THAT(fstat(fd2.get(), &st2), SyscallSucceeds());

  // The two fds should have different inode numbers. Specifically, since fd2
  // was created later, it should have a higher inode number.
  EXPECT_GT(st2.st_ino, st1.st_ino);

  // Verify again after another S/R cycle. The inode numbers should remain the
  // same.
  MaybeSave();

  struct stat st1_after;
  struct stat st2_after;
  ASSERT_THAT(fstat(fd1.get(), &st1_after), SyscallSucceeds());
  ASSERT_THAT(fstat(fd2.get(), &st2_after), SyscallSucceeds());

  EXPECT_EQ(st1_after.st_ino, st1.st_ino);
  EXPECT_EQ(st2_after.st_ino, st2.st_ino);
}

#ifndef SYS_statx
#if defined(__x86_64__)
#define SYS_statx 397
#else
#error "Unknown architecture"
#endif
#endif  // SYS_statx

#ifndef STATX_ALL
#define STATX_ALL 0x00000fffU
#endif  // STATX_ALL

// struct kernel_statx_timestamp is a Linux statx_timestamp struct.
struct kernel_statx_timestamp {
  int64_t tv_sec;
  uint32_t tv_nsec;
  int32_t __reserved;
};

// struct kernel_statx is a Linux statx struct. Old versions of glibc do not
// expose it. See include/uapi/linux/stat.h
struct kernel_statx {
  uint32_t stx_mask;
  uint32_t stx_blksize;
  uint64_t stx_attributes;
  uint32_t stx_nlink;
  uint32_t stx_uid;
  uint32_t stx_gid;
  uint16_t stx_mode;
  uint16_t __spare0[1];
  uint64_t stx_ino;
  uint64_t stx_size;
  uint64_t stx_blocks;
  uint64_t stx_attributes_mask;
  struct kernel_statx_timestamp stx_atime;
  struct kernel_statx_timestamp stx_btime;
  struct kernel_statx_timestamp stx_ctime;
  struct kernel_statx_timestamp stx_mtime;
  uint32_t stx_rdev_major;
  uint32_t stx_rdev_minor;
  uint32_t stx_dev_major;
  uint32_t stx_dev_minor;
  uint64_t __spare2[14];
};

int statx(int dirfd, const char *pathname, int flags, unsigned int mask,
          struct kernel_statx *statxbuf) {
  return syscall(SYS_statx, dirfd, pathname, flags, mask, statxbuf);
}

TEST_F(StatTest, StatxAbsPath) {
  SKIP_IF(statx(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);

  struct kernel_statx stx;
  EXPECT_THAT(statx(-1, test_file_name_.c_str(), 0, STATX_ALL, &stx),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(stx.stx_mode));
}

TEST_F(StatTest, StatxRelPathDirFD) {
  SKIP_IF(statx(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);

  struct kernel_statx stx;
  auto const dirfd =
      ASSERT_NO_ERRNO_AND_VALUE(Open(GetAbsoluteTestTmpdir(), O_RDONLY));
  auto filename = std::string(Basename(test_file_name_));

  EXPECT_THAT(statx(dirfd.get(), filename.c_str(), 0, STATX_ALL, &stx),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(stx.stx_mode));
}

TEST_F(StatTest, StatxRelPathCwd) {
  SKIP_IF(statx(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);

  ASSERT_THAT(chdir(GetAbsoluteTestTmpdir().c_str()), SyscallSucceeds());
  auto filename = std::string(Basename(test_file_name_));
  struct kernel_statx stx;
  EXPECT_THAT(statx(AT_FDCWD, filename.c_str(), 0, STATX_ALL, &stx),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(stx.stx_mode));
}

TEST_F(StatTest, StatxEmptyPath) {
  SKIP_IF(statx(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);

  const auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(test_file_name_, O_RDONLY));
  struct kernel_statx stx;
  EXPECT_THAT(statx(fd.get(), "", AT_EMPTY_PATH, STATX_ALL, &stx),
              SyscallSucceeds());
  EXPECT_TRUE(S_ISREG(stx.stx_mode));
}

}  // namespace

}  // namespace testing
}  // namespace gvisor