Home | History | Annotate | Download | only in Support
      1 //===- llvm/unittest/Support/Path.cpp - Path 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/FileSystem.h"
     11 #include "llvm/Support/PathV2.h"
     12 #include "llvm/Support/ErrorHandling.h"
     13 #include "llvm/Support/raw_ostream.h"
     14 
     15 #include "gtest/gtest.h"
     16 
     17 using namespace llvm;
     18 using namespace llvm::sys;
     19 
     20 #define ASSERT_NO_ERROR(x) \
     21   if (error_code ASSERT_NO_ERROR_ec = x) { \
     22     SmallString<128> MessageStorage; \
     23     raw_svector_ostream Message(MessageStorage); \
     24     Message << #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     GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
     28   } else {}
     29 
     30 namespace {
     31 
     32 TEST(is_separator, Works) {
     33   EXPECT_TRUE(path::is_separator('/'));
     34   EXPECT_FALSE(path::is_separator('\0'));
     35   EXPECT_FALSE(path::is_separator('-'));
     36   EXPECT_FALSE(path::is_separator(' '));
     37 
     38 #ifdef LLVM_ON_WIN32
     39   EXPECT_TRUE(path::is_separator('\\'));
     40 #else
     41   EXPECT_FALSE(path::is_separator('\\'));
     42 #endif
     43 }
     44 
     45 TEST(Support, Path) {
     46   SmallVector<StringRef, 40> paths;
     47   paths.push_back("");
     48   paths.push_back(".");
     49   paths.push_back("..");
     50   paths.push_back("foo");
     51   paths.push_back("/");
     52   paths.push_back("/foo");
     53   paths.push_back("foo/");
     54   paths.push_back("/foo/");
     55   paths.push_back("foo/bar");
     56   paths.push_back("/foo/bar");
     57   paths.push_back("//net");
     58   paths.push_back("//net/foo");
     59   paths.push_back("///foo///");
     60   paths.push_back("///foo///bar");
     61   paths.push_back("/.");
     62   paths.push_back("./");
     63   paths.push_back("/..");
     64   paths.push_back("../");
     65   paths.push_back("foo/.");
     66   paths.push_back("foo/..");
     67   paths.push_back("foo/./");
     68   paths.push_back("foo/./bar");
     69   paths.push_back("foo/..");
     70   paths.push_back("foo/../");
     71   paths.push_back("foo/../bar");
     72   paths.push_back("c:");
     73   paths.push_back("c:/");
     74   paths.push_back("c:foo");
     75   paths.push_back("c:/foo");
     76   paths.push_back("c:foo/");
     77   paths.push_back("c:/foo/");
     78   paths.push_back("c:/foo/bar");
     79   paths.push_back("prn:");
     80   paths.push_back("c:\\");
     81   paths.push_back("c:foo");
     82   paths.push_back("c:\\foo");
     83   paths.push_back("c:foo\\");
     84   paths.push_back("c:\\foo\\");
     85   paths.push_back("c:\\foo/");
     86   paths.push_back("c:/foo\\bar");
     87 
     88   for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
     89                                                   e = paths.end();
     90                                                   i != e;
     91                                                   ++i) {
     92     for (sys::path::const_iterator ci = sys::path::begin(*i),
     93                                    ce = sys::path::end(*i);
     94                                    ci != ce;
     95                                    ++ci) {
     96       ASSERT_FALSE(ci->empty());
     97     }
     98 
     99 #if 0 // Valgrind is whining about this.
    100     outs() << "    Reverse Iteration: [";
    101     for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
    102                                      ce = sys::path::rend(*i);
    103                                      ci != ce;
    104                                      ++ci) {
    105       outs() << *ci << ',';
    106     }
    107     outs() << "]\n";
    108 #endif
    109 
    110     path::has_root_path(*i);
    111     path::root_path(*i);
    112     path::has_root_name(*i);
    113     path::root_name(*i);
    114     path::has_root_directory(*i);
    115     path::root_directory(*i);
    116     path::has_parent_path(*i);
    117     path::parent_path(*i);
    118     path::has_filename(*i);
    119     path::filename(*i);
    120     path::has_stem(*i);
    121     path::stem(*i);
    122     path::has_extension(*i);
    123     path::extension(*i);
    124     path::is_absolute(*i);
    125     path::is_relative(*i);
    126 
    127     SmallString<128> temp_store;
    128     temp_store = *i;
    129     ASSERT_NO_ERROR(fs::make_absolute(temp_store));
    130     temp_store = *i;
    131     path::remove_filename(temp_store);
    132 
    133     temp_store = *i;
    134     path::replace_extension(temp_store, "ext");
    135     StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
    136     stem = path::stem(filename);
    137     ext  = path::extension(filename);
    138     EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
    139 
    140     path::native(*i, temp_store);
    141   }
    142 }
    143 
    144 class FileSystemTest : public testing::Test {
    145 protected:
    146   /// Unique temporary directory in which all created filesystem entities must
    147   /// be placed. It is recursively removed at the end of each test.
    148   SmallString<128> TestDirectory;
    149 
    150   virtual void SetUp() {
    151     int fd;
    152     ASSERT_NO_ERROR(
    153       fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
    154                       TestDirectory));
    155     // We don't care about this specific file.
    156     ::close(fd);
    157     TestDirectory = path::parent_path(TestDirectory);
    158     errs() << "Test Directory: " << TestDirectory << '\n';
    159     errs().flush();
    160   }
    161 
    162   virtual void TearDown() {
    163     uint32_t removed;
    164     ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
    165   }
    166 };
    167 
    168 TEST_F(FileSystemTest, TempFiles) {
    169   // Create a temp file.
    170   int FileDescriptor;
    171   SmallString<64> TempPath;
    172   ASSERT_NO_ERROR(
    173     fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
    174 
    175   // Make sure it exists.
    176   bool TempFileExists;
    177   ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
    178   EXPECT_TRUE(TempFileExists);
    179 
    180   // Create another temp tile.
    181   int FD2;
    182   SmallString<64> TempPath2;
    183   ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
    184   ASSERT_NE(TempPath.str(), TempPath2.str());
    185 
    186   // Try to copy the first to the second.
    187   EXPECT_EQ(
    188     fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
    189 
    190   ::close(FD2);
    191   // Try again with the proper options.
    192   ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
    193                                 fs::copy_option::overwrite_if_exists));
    194   // Remove Temp2.
    195   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
    196   EXPECT_TRUE(TempFileExists);
    197 
    198   // Make sure Temp2 doesn't exist.
    199   ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
    200   EXPECT_FALSE(TempFileExists);
    201 
    202   // Create a hard link to Temp1.
    203   ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
    204   bool equal;
    205   ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
    206   EXPECT_TRUE(equal);
    207 
    208   // Remove Temp1.
    209   ::close(FileDescriptor);
    210   ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
    211   EXPECT_TRUE(TempFileExists);
    212 
    213   // Remove the hard link.
    214   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
    215   EXPECT_TRUE(TempFileExists);
    216 
    217   // Make sure Temp1 doesn't exist.
    218   ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
    219   EXPECT_FALSE(TempFileExists);
    220 }
    221 
    222 TEST_F(FileSystemTest, DirectoryIteration) {
    223   error_code ec;
    224   for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
    225     ASSERT_NO_ERROR(ec);
    226 }
    227 
    228 TEST_F(FileSystemTest, Magic) {
    229   struct type {
    230     const char *filename;
    231     const char *magic_str;
    232     size_t      magic_str_len;
    233   } types [] = {{"magic.archive", "!<arch>\x0A", 8}};
    234 
    235   // Create some files filled with magic.
    236   for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
    237                                                                      ++i) {
    238     SmallString<128> file_pathname(TestDirectory);
    239     path::append(file_pathname, i->filename);
    240     std::string ErrMsg;
    241     raw_fd_ostream file(file_pathname.c_str(), ErrMsg,
    242                         raw_fd_ostream::F_Binary);
    243     ASSERT_FALSE(file.has_error());
    244     StringRef magic(i->magic_str, i->magic_str_len);
    245     file << magic;
    246     file.close();
    247     bool res = false;
    248     ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
    249     EXPECT_TRUE(res);
    250   }
    251 }
    252 
    253 } // anonymous namespace
    254