summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/rename.cc
blob: 561eed99f9e1b4c464bb753e448e3465924e524a (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
// 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 <fcntl.h>
#include <stdio.h>

#include <string>

#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "test/util/capability_util.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"

using ::testing::AnyOf;

namespace gvisor {
namespace testing {

namespace {

TEST(RenameTest, RootToAnything) {
  ASSERT_THAT(rename("/", "/bin"), SyscallFailsWithErrno(EBUSY));
}

TEST(RenameTest, AnythingToRoot) {
  ASSERT_THAT(rename("/bin", "/"), SyscallFailsWithErrno(EBUSY));
}

TEST(RenameTest, SourceIsAncestorOfTarget) {
  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto subdir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir.path()));
  ASSERT_THAT(rename(dir.path().c_str(), subdir.path().c_str()),
              SyscallFailsWithErrno(EINVAL));

  // Try an even deeper directory.
  auto deep_subdir =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(subdir.path()));
  ASSERT_THAT(rename(dir.path().c_str(), deep_subdir.path().c_str()),
              SyscallFailsWithErrno(EINVAL));
}

TEST(RenameTest, TargetIsAncestorOfSource) {
  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto subdir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir.path()));
  ASSERT_THAT(rename(subdir.path().c_str(), dir.path().c_str()),
              SyscallFailsWithErrno(ENOTEMPTY));

  // Try an even deeper directory.
  auto deep_subdir =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(subdir.path()));
  ASSERT_THAT(rename(deep_subdir.path().c_str(), dir.path().c_str()),
              SyscallFailsWithErrno(ENOTEMPTY));
}

TEST(RenameTest, FileToSelf) {
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  EXPECT_THAT(rename(f.path().c_str(), f.path().c_str()), SyscallSucceeds());
}

TEST(RenameTest, DirectoryToSelf) {
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  EXPECT_THAT(rename(f.path().c_str(), f.path().c_str()), SyscallSucceeds());
}

TEST(RenameTest, FileToSameDirectory) {
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  std::string const newpath = NewTempAbsPath();
  ASSERT_THAT(rename(f.path().c_str(), newpath.c_str()), SyscallSucceeds());
  std::string const oldpath = f.release();
  f.reset(newpath);
  EXPECT_THAT(Exists(oldpath), IsPosixErrorOkAndHolds(false));
  EXPECT_THAT(Exists(newpath), IsPosixErrorOkAndHolds(true));
}

TEST(RenameTest, DirectoryToSameDirectory) {
  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  std::string const newpath = NewTempAbsPath();
  ASSERT_THAT(rename(dir.path().c_str(), newpath.c_str()), SyscallSucceeds());
  std::string const oldpath = dir.release();
  dir.reset(newpath);
  EXPECT_THAT(Exists(oldpath), IsPosixErrorOkAndHolds(false));
  EXPECT_THAT(Exists(newpath), IsPosixErrorOkAndHolds(true));
}

TEST(RenameTest, FileToParentDirectory) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir1.path()));
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir2.path()));
  std::string const newpath = NewTempAbsPathInDir(dir1.path());
  ASSERT_THAT(rename(f.path().c_str(), newpath.c_str()), SyscallSucceeds());
  std::string const oldpath = f.release();
  f.reset(newpath);
  EXPECT_THAT(Exists(oldpath), IsPosixErrorOkAndHolds(false));
  EXPECT_THAT(Exists(newpath), IsPosixErrorOkAndHolds(true));
}

TEST(RenameTest, DirectoryToParentDirectory) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir1.path()));
  auto dir3 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir2.path()));
  EXPECT_THAT(IsDirectory(dir3.path()), IsPosixErrorOkAndHolds(true));
  std::string const newpath = NewTempAbsPathInDir(dir1.path());
  ASSERT_THAT(rename(dir3.path().c_str(), newpath.c_str()), SyscallSucceeds());
  std::string const oldpath = dir3.release();
  dir3.reset(newpath);
  EXPECT_THAT(Exists(oldpath), IsPosixErrorOkAndHolds(false));
  EXPECT_THAT(Exists(newpath), IsPosixErrorOkAndHolds(true));
  EXPECT_THAT(IsDirectory(newpath), IsPosixErrorOkAndHolds(true));
}

TEST(RenameTest, FileToChildDirectory) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir1.path()));
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir1.path()));
  std::string const newpath = NewTempAbsPathInDir(dir2.path());
  ASSERT_THAT(rename(f.path().c_str(), newpath.c_str()), SyscallSucceeds());
  std::string const oldpath = f.release();
  f.reset(newpath);
  EXPECT_THAT(Exists(oldpath), IsPosixErrorOkAndHolds(false));
  EXPECT_THAT(Exists(newpath), IsPosixErrorOkAndHolds(true));
}

TEST(RenameTest, DirectoryToChildDirectory) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir1.path()));
  auto dir3 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir1.path()));
  std::string const newpath = NewTempAbsPathInDir(dir2.path());
  ASSERT_THAT(rename(dir3.path().c_str(), newpath.c_str()), SyscallSucceeds());
  std::string const oldpath = dir3.release();
  dir3.reset(newpath);
  EXPECT_THAT(Exists(oldpath), IsPosixErrorOkAndHolds(false));
  EXPECT_THAT(Exists(newpath), IsPosixErrorOkAndHolds(true));
  EXPECT_THAT(IsDirectory(newpath), IsPosixErrorOkAndHolds(true));
}

TEST(RenameTest, DirectoryToOwnChildDirectory) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir1.path()));
  std::string const newpath = NewTempAbsPathInDir(dir2.path());
  ASSERT_THAT(rename(dir1.path().c_str(), newpath.c_str()),
              SyscallFailsWithErrno(EINVAL));
}

TEST(RenameTest, FileOverwritesFile) {
  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
      dir.path(), "first", TempPath::kDefaultFileMode));
  auto f2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
      dir.path(), "second", TempPath::kDefaultFileMode));
  ASSERT_THAT(rename(f1.path().c_str(), f2.path().c_str()), SyscallSucceeds());
  EXPECT_THAT(Exists(f1.path()), IsPosixErrorOkAndHolds(false));

  f1.release();
  std::string f2_contents;
  ASSERT_NO_ERRNO(GetContents(f2.path(), &f2_contents));
  EXPECT_EQ("first", f2_contents);
}

TEST(RenameTest, DirectoryOverwritesDirectoryLinkCount) {
  // Directory link counts are synthetic on overlay filesystems.
  SKIP_IF(ASSERT_NO_ERRNO_AND_VALUE(IsOverlayfs(GetAbsoluteTestTmpdir())));

  auto parent1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  EXPECT_THAT(Links(parent1.path()), IsPosixErrorOkAndHolds(2));

  auto parent2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  EXPECT_THAT(Links(parent2.path()), IsPosixErrorOkAndHolds(2));

  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(parent1.path()));
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(parent2.path()));

  EXPECT_THAT(Links(parent1.path()), IsPosixErrorOkAndHolds(3));
  EXPECT_THAT(Links(parent2.path()), IsPosixErrorOkAndHolds(3));

  ASSERT_THAT(rename(dir1.path().c_str(), dir2.path().c_str()),
              SyscallSucceeds());

  EXPECT_THAT(Links(parent1.path()), IsPosixErrorOkAndHolds(2));
  EXPECT_THAT(Links(parent2.path()), IsPosixErrorOkAndHolds(3));
}

TEST(RenameTest, FileDoesNotExist) {
  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  const std::string source = JoinPath(dir.path(), "source");
  const std::string dest = JoinPath(dir.path(), "dest");
  ASSERT_THAT(rename(source.c_str(), dest.c_str()),
              SyscallFailsWithErrno(ENOENT));
}

TEST(RenameTest, FileDoesNotOverwriteDirectory) {
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  ASSERT_THAT(rename(f.path().c_str(), dir.path().c_str()),
              SyscallFailsWithErrno(EISDIR));
}

TEST(RenameTest, DirectoryDoesNotOverwriteFile) {
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  ASSERT_THAT(rename(dir.path().c_str(), f.path().c_str()),
              SyscallFailsWithErrno(ENOTDIR));
}

TEST(RenameTest, DirectoryOverwritesEmptyDirectory) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir1.path()));
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  EXPECT_THAT(rename(dir1.path().c_str(), dir2.path().c_str()),
              SyscallSucceeds());
  EXPECT_THAT(Exists(dir1.path()), IsPosixErrorOkAndHolds(false));
  dir1.release();
  EXPECT_THAT(Exists(JoinPath(dir2.path(), Basename(f.path()))),
              IsPosixErrorOkAndHolds(true));
  f.release();
}

TEST(RenameTest, FailsWithDots) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto dir1_dot = absl::StrCat(dir1.path(), "/.");
  auto dir2_dot = absl::StrCat(dir2.path(), "/.");
  auto dir1_dot_dot = absl::StrCat(dir1.path(), "/..");
  auto dir2_dot_dot = absl::StrCat(dir2.path(), "/..");

  // Try with dot paths in the first argument
  EXPECT_THAT(rename(dir1_dot.c_str(), dir2.path().c_str()),
              SyscallFailsWithErrno(EBUSY));
  EXPECT_THAT(rename(dir1_dot_dot.c_str(), dir2.path().c_str()),
              SyscallFailsWithErrno(EBUSY));

  // Try with dot paths in the second argument
  EXPECT_THAT(rename(dir1.path().c_str(), dir2_dot.c_str()),
              SyscallFailsWithErrno(EBUSY));
  EXPECT_THAT(rename(dir1.path().c_str(), dir2_dot_dot.c_str()),
              SyscallFailsWithErrno(EBUSY));
}

TEST(RenameTest, DirectoryDoesNotOverwriteNonemptyDirectory) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir1.path()));
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir2.path()));
  ASSERT_THAT(rename(dir1.path().c_str(), dir2.path().c_str()),
              SyscallFailsWithErrno(ENOTEMPTY));
}

TEST(RenameTest, FailsWhenOldParentNotWritable) {
  // Drop capabilities that allow us to override file and directory permissions.
  AutoCapability cap1(CAP_DAC_OVERRIDE, false);
  AutoCapability cap2(CAP_DAC_READ_SEARCH, false);

  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir1.path()));
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  // dir1 is not writable.
  ASSERT_THAT(chmod(dir1.path().c_str(), 0555), SyscallSucceeds());

  std::string const newpath = NewTempAbsPathInDir(dir2.path());
  EXPECT_THAT(rename(f1.path().c_str(), newpath.c_str()),
              SyscallFailsWithErrno(EACCES));
}

TEST(RenameTest, FailsWhenNewParentNotWritable) {
  // Drop capabilities that allow us to override file and directory permissions.
  AutoCapability cap1(CAP_DAC_OVERRIDE, false);
  AutoCapability cap2(CAP_DAC_READ_SEARCH, false);

  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir1.path()));
  // dir2 is not writable.
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(
      TempPath::CreateDirWith(GetAbsoluteTestTmpdir(), 0555));

  std::string const newpath = NewTempAbsPathInDir(dir2.path());
  EXPECT_THAT(rename(f1.path().c_str(), newpath.c_str()),
              SyscallFailsWithErrno(EACCES));
}

// Equivalent to FailsWhenNewParentNotWritable, but with a destination file
// to overwrite.
TEST(RenameTest, OverwriteFailsWhenNewParentNotWritable) {
  // Drop capabilities that allow us to override file and directory permissions.
  AutoCapability cap1(CAP_DAC_OVERRIDE, false);
  AutoCapability cap2(CAP_DAC_READ_SEARCH, false);

  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir1.path()));

  // dir2 is not writable.
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir2.path()));
  ASSERT_THAT(chmod(dir2.path().c_str(), 0555), SyscallSucceeds());

  EXPECT_THAT(rename(f1.path().c_str(), f2.path().c_str()),
              SyscallFailsWithErrno(EACCES));
}

// If the parent directory of source is not accessible, rename returns EACCES
// because the user cannot determine if source exists.
TEST(RenameTest, FileDoesNotExistWhenNewParentNotExecutable) {
  // Drop capabilities that allow us to override file and directory permissions.
  AutoCapability cap1(CAP_DAC_OVERRIDE, false);
  AutoCapability cap2(CAP_DAC_READ_SEARCH, false);

  // No execute permission.
  auto dir = ASSERT_NO_ERRNO_AND_VALUE(
      TempPath::CreateDirWith(GetAbsoluteTestTmpdir(), 0400));

  const std::string source = JoinPath(dir.path(), "source");
  const std::string dest = JoinPath(dir.path(), "dest");
  ASSERT_THAT(rename(source.c_str(), dest.c_str()),
              SyscallFailsWithErrno(EACCES));
}

TEST(RenameTest, DirectoryWithOpenFdOverwritesEmptyDirectory) {
  auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir1.path()));
  auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());

  // Get an fd on dir1
  int fd;
  ASSERT_THAT(fd = open(dir1.path().c_str(), O_DIRECTORY), SyscallSucceeds());
  auto close_f = Cleanup([fd] {
    // Close the fd on f.
    EXPECT_THAT(close(fd), SyscallSucceeds());
  });

  EXPECT_THAT(rename(dir1.path().c_str(), dir2.path().c_str()),
              SyscallSucceeds());

  const std::string new_f_path = JoinPath(dir2.path(), Basename(f.path()));

  auto remove_f = Cleanup([&] {
    // Delete f in its new location.
    ASSERT_NO_ERRNO(Delete(new_f_path));
    f.release();
  });

  EXPECT_THAT(Exists(dir1.path()), IsPosixErrorOkAndHolds(false));
  dir1.release();
  EXPECT_THAT(Exists(new_f_path), IsPosixErrorOkAndHolds(true));
}

TEST(RenameTest, FileWithOpenFd) {
  TempPath root_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  TempPath dir1 =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(root_dir.path()));
  TempPath dir2 =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(root_dir.path()));
  TempPath dir3 =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(root_dir.path()));

  // Create file in dir1.
  constexpr char kContents[] = "foo";
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
      dir1.path(), kContents, TempPath::kDefaultFileMode));

  // Get fd on file.
  const FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(f.path(), O_RDWR));

  // Move f to dir2.
  const std::string path2 = NewTempAbsPathInDir(dir2.path());
  ASSERT_THAT(rename(f.path().c_str(), path2.c_str()), SyscallSucceeds());

  // Read f's kContents.
  char buf[sizeof(kContents)];
  EXPECT_THAT(PreadFd(fd.get(), &buf, sizeof(kContents), 0),
              SyscallSucceedsWithValue(sizeof(kContents) - 1));
  EXPECT_EQ(absl::string_view(buf, sizeof(buf) - 1), kContents);

  // Move f to dir3.
  const std::string path3 = NewTempAbsPathInDir(dir3.path());
  ASSERT_THAT(rename(path2.c_str(), path3.c_str()), SyscallSucceeds());

  // Read f's kContents.
  EXPECT_THAT(PreadFd(fd.get(), &buf, sizeof(kContents), 0),
              SyscallSucceedsWithValue(sizeof(kContents) - 1));
  EXPECT_EQ(absl::string_view(buf, sizeof(buf) - 1), kContents);
}

// Tests that calling rename with file path ending with . or .. causes EBUSY.
TEST(RenameTest, PathEndingWithDots) {
  TempPath root_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  TempPath dir1 =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(root_dir.path()));
  TempPath dir2 =
      ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(root_dir.path()));

  // Try to move dir1 into dir2 but mess up the paths.
  auto dir1Dot = JoinPath(dir1.path(), ".");
  auto dir2Dot = JoinPath(dir2.path(), ".");
  auto dir1DotDot = JoinPath(dir1.path(), "..");
  auto dir2DotDot = JoinPath(dir2.path(), "..");
  ASSERT_THAT(rename(dir1.path().c_str(), dir2Dot.c_str()),
              SyscallFailsWithErrno(EBUSY));
  ASSERT_THAT(rename(dir1.path().c_str(), dir2DotDot.c_str()),
              SyscallFailsWithErrno(EBUSY));
  ASSERT_THAT(rename(dir1Dot.c_str(), dir2.path().c_str()),
              SyscallFailsWithErrno(EBUSY));
  ASSERT_THAT(rename(dir1DotDot.c_str(), dir2.path().c_str()),
              SyscallFailsWithErrno(EBUSY));
}

// Calling rename with file path ending with . or .. causes EBUSY in sysfs.
TEST(RenameTest, SysfsPathEndingWithDots) {
  // If a non-root user tries to rename inside /sys then we get EPERM.
  SKIP_IF(geteuid() != 0);
  ASSERT_THAT(rename("/sys/devices/system/cpu/online", "/sys/."),
              SyscallFailsWithErrno(EBUSY));
  ASSERT_THAT(rename("/sys/devices/system/cpu/online", "/sys/.."),
              SyscallFailsWithErrno(EBUSY));
}

TEST(RenameTest, SysfsFileToSelf) {
  // If a non-root user tries to rename inside /sys then we get EPERM.
  SKIP_IF(geteuid() != 0);
  std::string const path = "/sys/devices/system/cpu/online";
  EXPECT_THAT(rename(path.c_str(), path.c_str()), SyscallSucceeds());
}

TEST(RenameTest, SysfsDirectoryToSelf) {
  // If a non-root user tries to rename inside /sys then we get EPERM.
  SKIP_IF(geteuid() != 0);
  std::string const path = "/sys/devices";
  EXPECT_THAT(rename(path.c_str(), path.c_str()), SyscallSucceeds());
}

#ifndef SYS_renameat2
#if defined(__x86_64__)
#define SYS_renameat2 316
#elif defined(__aarch64__)
#define SYS_renameat2 276
#else
#error "Unknown architecture"
#endif
#endif  // SYS_renameat2

#ifndef RENAME_NOREPLACE
#define RENAME_NOREPLACE (1 << 0)
#endif  // RENAME_NOREPLACE

int renameat2(int olddirfd, const char* oldpath, int newdirfd,
              const char* newpath, unsigned int flags) {
  return syscall(SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags);
}

TEST(Renameat2Test, NoReplaceSuccess) {
  auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  std::string const newpath = NewTempAbsPath();
  // renameat2 may fail with ENOSYS (if the syscall is unsupported) or EINVAL
  // (if flags are unsupported), or succeed (if RENAME_NOREPLACE is operating
  // correctly).
  EXPECT_THAT(
      renameat2(AT_FDCWD, f.path().c_str(), AT_FDCWD, newpath.c_str(),
                RENAME_NOREPLACE),
      AnyOf(SyscallFailsWithErrno(AnyOf(ENOSYS, EINVAL)), SyscallSucceeds()));
}

TEST(Renameat2Test, NoReplaceExisting) {
  auto f1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  auto f2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  // renameat2 may fail with ENOSYS (if the syscall is unsupported), EINVAL (if
  // flags are unsupported), or EEXIST (if RENAME_NOREPLACE is operating
  // correctly).
  EXPECT_THAT(renameat2(AT_FDCWD, f1.path().c_str(), AT_FDCWD,
                        f2.path().c_str(), RENAME_NOREPLACE),
              SyscallFailsWithErrno(AnyOf(ENOSYS, EINVAL, EEXIST)));
}

TEST(Renameat2Test, NoReplaceDot) {
  auto d1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  auto d2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
  // renameat2 may fail with ENOSYS (if the syscall is unsupported), EINVAL (if
  // flags are unsupported), or EEXIST (if RENAME_NOREPLACE is operating
  // correctly).
  EXPECT_THAT(
      renameat2(AT_FDCWD, d1.path().c_str(), AT_FDCWD,
                absl::StrCat(d2.path(), "/.").c_str(), RENAME_NOREPLACE),
      SyscallFailsWithErrno(AnyOf(ENOSYS, EINVAL, EEXIST)));
}

}  // namespace

}  // namespace testing
}  // namespace gvisor