OSDN Git Service

[unittests] Change std::sort to llvm::sort in response to r327219
[android-x86/external-llvm.git] / unittests / Support / ReplaceFileTest.cpp
1 //===- llvm/unittest/Support/ReplaceFileTest.cpp - unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/Errc.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Process.h"
16 #include "gtest/gtest.h"
17
18 using namespace llvm;
19 using namespace llvm::sys;
20
21 #define ASSERT_NO_ERROR(x)                                                 \
22   do {                                                                     \
23     if (std::error_code ASSERT_NO_ERROR_ec = x) {                          \
24       errs() << #x ": did not return errc::success.\n"                     \
25              << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"     \
26              << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
27     }                                                                      \
28   } while (false)
29
30 namespace {
31 std::error_code CreateFileWithContent(const SmallString<128> &FilePath,
32                                       const StringRef &content) {
33   int FD = 0;
34   if (std::error_code ec = fs::openFileForWrite(FilePath, FD, fs::F_None))
35     return ec;
36
37   const bool ShouldClose = true;
38   raw_fd_ostream OS(FD, ShouldClose);
39   OS << content;
40
41   return std::error_code();
42 }
43
44 class ScopedFD {
45   int FD;
46
47   ScopedFD(const ScopedFD &) = delete;
48   ScopedFD &operator=(const ScopedFD &) = delete;
49
50  public:
51   explicit ScopedFD(int Descriptor) : FD(Descriptor) {}
52   ~ScopedFD() { Process::SafelyCloseFileDescriptor(FD); }
53 };
54
55 bool FDHasContent(int FD, StringRef Content) {
56   auto Buffer = MemoryBuffer::getOpenFile(FD, "", -1);
57   assert(Buffer);
58   return Buffer.get()->getBuffer() == Content;
59 }
60
61 bool FileHasContent(StringRef File, StringRef Content) {
62   int FD = 0;
63   auto EC = fs::openFileForRead(File, FD);
64   (void)EC;
65   assert(!EC);
66   ScopedFD EventuallyCloseIt(FD);
67   return FDHasContent(FD, Content);
68 }
69
70 TEST(rename, FileOpenedForReadingCanBeReplaced) {
71   // Create unique temporary directory for this test.
72   SmallString<128> TestDirectory;
73   ASSERT_NO_ERROR(fs::createUniqueDirectory(
74       "FileOpenedForReadingCanBeReplaced-test", TestDirectory));
75
76   // Add a couple of files to the test directory.
77   SmallString<128> SourceFileName(TestDirectory);
78   path::append(SourceFileName, "source");
79
80   SmallString<128> TargetFileName(TestDirectory);
81   path::append(TargetFileName, "target");
82
83   ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
84   ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
85
86   {
87     // Open the target file for reading.
88     int ReadFD = 0;
89     ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, ReadFD));
90     ScopedFD EventuallyCloseIt(ReadFD);
91
92     // Confirm we can replace the file while it is open.
93     EXPECT_TRUE(!fs::rename(SourceFileName, TargetFileName));
94
95     // We should still be able to read the old data through the existing
96     // descriptor.
97     EXPECT_TRUE(FDHasContent(ReadFD, "!!target!!"));
98
99     // The source file should no longer exist
100     EXPECT_FALSE(fs::exists(SourceFileName));
101   }
102
103   // If we obtain a new descriptor for the target file, we should find that it
104   // contains the content that was in the source file.
105   EXPECT_TRUE(FileHasContent(TargetFileName, "!!source!!"));
106
107   // Rename the target file back to the source file name to confirm that rename
108   // still works if the destination does not already exist.
109   EXPECT_TRUE(!fs::rename(TargetFileName, SourceFileName));
110   EXPECT_FALSE(fs::exists(TargetFileName));
111   ASSERT_TRUE(fs::exists(SourceFileName));
112
113   // Clean up.
114   ASSERT_NO_ERROR(fs::remove(SourceFileName));
115   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
116 }
117
118 TEST(rename, ExistingTemp) {
119   // Test that existing .tmpN files don't get deleted by the Windows
120   // sys::fs::rename implementation.
121   SmallString<128> TestDirectory;
122   ASSERT_NO_ERROR(
123       fs::createUniqueDirectory("ExistingTemp-test", TestDirectory));
124
125   SmallString<128> SourceFileName(TestDirectory);
126   path::append(SourceFileName, "source");
127
128   SmallString<128> TargetFileName(TestDirectory);
129   path::append(TargetFileName, "target");
130
131   SmallString<128> TargetTmp0FileName(TestDirectory);
132   path::append(TargetTmp0FileName, "target.tmp0");
133
134   SmallString<128> TargetTmp1FileName(TestDirectory);
135   path::append(TargetTmp1FileName, "target.tmp1");
136
137   ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
138   ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
139   ASSERT_NO_ERROR(CreateFileWithContent(TargetTmp0FileName, "!!target.tmp0!!"));
140
141   {
142     // Use mapped_file_region to make sure that the destination file is mmap'ed.
143     // This will cause SetInformationByHandle to fail when renaming to the
144     // destination, and we will follow the code path that tries to give target
145     // a temporary name.
146     int TargetFD;
147     std::error_code EC;
148     ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, TargetFD));
149     ScopedFD X(TargetFD);
150     sys::fs::mapped_file_region MFR(
151         TargetFD, sys::fs::mapped_file_region::readonly, 10, 0, EC);
152     ASSERT_FALSE(EC);
153
154     ASSERT_NO_ERROR(fs::rename(SourceFileName, TargetFileName));
155
156 #ifdef _WIN32
157     // Make sure that target was temporarily renamed to target.tmp1 on Windows.
158     // This is signified by a permission denied error as opposed to no such file
159     // or directory when trying to open it.
160     int Tmp1FD;
161     EXPECT_EQ(errc::permission_denied,
162               fs::openFileForRead(TargetTmp1FileName, Tmp1FD));
163 #endif
164   }
165
166   EXPECT_TRUE(FileHasContent(TargetTmp0FileName, "!!target.tmp0!!"));
167
168   ASSERT_NO_ERROR(fs::remove(TargetFileName));
169   ASSERT_NO_ERROR(fs::remove(TargetTmp0FileName));
170   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
171 }
172
173 }  // anonymous namespace