Path.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. #include "llvm/Support/PathV2.h"
  10. #include "llvm/Support/ErrorHandling.h"
  11. #include "llvm/Support/FileSystem.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include "gtest/gtest.h"
  14. using namespace llvm;
  15. using namespace llvm::sys;
  16. #define ASSERT_NO_ERROR(x) \
  17. if (error_code ASSERT_NO_ERROR_ec = x) { \
  18. SmallString<128> MessageStorage; \
  19. raw_svector_ostream Message(MessageStorage); \
  20. Message << #x ": did not return errc::success.\n" \
  21. << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
  22. << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
  23. GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
  24. } else {}
  25. namespace {
  26. TEST(is_separator, Works) {
  27. EXPECT_TRUE(path::is_separator('/'));
  28. EXPECT_FALSE(path::is_separator('\0'));
  29. EXPECT_FALSE(path::is_separator('-'));
  30. EXPECT_FALSE(path::is_separator(' '));
  31. #ifdef LLVM_ON_WIN32
  32. EXPECT_TRUE(path::is_separator('\\'));
  33. #else
  34. EXPECT_FALSE(path::is_separator('\\'));
  35. #endif
  36. }
  37. TEST(Support, Path) {
  38. SmallVector<StringRef, 40> paths;
  39. paths.push_back("");
  40. paths.push_back(".");
  41. paths.push_back("..");
  42. paths.push_back("foo");
  43. paths.push_back("/");
  44. paths.push_back("/foo");
  45. paths.push_back("foo/");
  46. paths.push_back("/foo/");
  47. paths.push_back("foo/bar");
  48. paths.push_back("/foo/bar");
  49. paths.push_back("//net");
  50. paths.push_back("//net/foo");
  51. paths.push_back("///foo///");
  52. paths.push_back("///foo///bar");
  53. paths.push_back("/.");
  54. paths.push_back("./");
  55. paths.push_back("/..");
  56. paths.push_back("../");
  57. paths.push_back("foo/.");
  58. paths.push_back("foo/..");
  59. paths.push_back("foo/./");
  60. paths.push_back("foo/./bar");
  61. paths.push_back("foo/..");
  62. paths.push_back("foo/../");
  63. paths.push_back("foo/../bar");
  64. paths.push_back("c:");
  65. paths.push_back("c:/");
  66. paths.push_back("c:foo");
  67. paths.push_back("c:/foo");
  68. paths.push_back("c:foo/");
  69. paths.push_back("c:/foo/");
  70. paths.push_back("c:/foo/bar");
  71. paths.push_back("prn:");
  72. paths.push_back("c:\\");
  73. paths.push_back("c:foo");
  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. for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
  80. e = paths.end();
  81. i != e;
  82. ++i) {
  83. for (sys::path::const_iterator ci = sys::path::begin(*i),
  84. ce = sys::path::end(*i);
  85. ci != ce;
  86. ++ci) {
  87. ASSERT_FALSE(ci->empty());
  88. }
  89. #if 0 // Valgrind is whining about this.
  90. outs() << " Reverse Iteration: [";
  91. for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
  92. ce = sys::path::rend(*i);
  93. ci != ce;
  94. ++ci) {
  95. outs() << *ci << ',';
  96. }
  97. outs() << "]\n";
  98. #endif
  99. path::has_root_path(*i);
  100. path::root_path(*i);
  101. path::has_root_name(*i);
  102. path::root_name(*i);
  103. path::has_root_directory(*i);
  104. path::root_directory(*i);
  105. path::has_parent_path(*i);
  106. path::parent_path(*i);
  107. path::has_filename(*i);
  108. path::filename(*i);
  109. path::has_stem(*i);
  110. path::stem(*i);
  111. path::has_extension(*i);
  112. path::extension(*i);
  113. path::is_absolute(*i);
  114. path::is_relative(*i);
  115. SmallString<128> temp_store;
  116. temp_store = *i;
  117. ASSERT_NO_ERROR(fs::make_absolute(temp_store));
  118. temp_store = *i;
  119. path::remove_filename(temp_store);
  120. temp_store = *i;
  121. path::replace_extension(temp_store, "ext");
  122. StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
  123. stem = path::stem(filename);
  124. ext = path::extension(filename);
  125. EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
  126. path::native(*i, temp_store);
  127. }
  128. }
  129. class FileSystemTest : public testing::Test {
  130. protected:
  131. /// Unique temporary directory in which all created filesystem entities must
  132. /// be placed. It is recursively removed at the end of each test.
  133. SmallString<128> TestDirectory;
  134. virtual void SetUp() {
  135. int fd;
  136. ASSERT_NO_ERROR(
  137. fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
  138. TestDirectory));
  139. // We don't care about this specific file.
  140. ::close(fd);
  141. TestDirectory = path::parent_path(TestDirectory);
  142. errs() << "Test Directory: " << TestDirectory << '\n';
  143. errs().flush();
  144. }
  145. virtual void TearDown() {
  146. uint32_t removed;
  147. ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
  148. }
  149. };
  150. TEST_F(FileSystemTest, TempFiles) {
  151. // Create a temp file.
  152. int FileDescriptor;
  153. SmallString<64> TempPath;
  154. ASSERT_NO_ERROR(
  155. fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
  156. // Make sure it exists.
  157. bool TempFileExists;
  158. ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
  159. EXPECT_TRUE(TempFileExists);
  160. // Create another temp tile.
  161. int FD2;
  162. SmallString<64> TempPath2;
  163. ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
  164. ASSERT_NE(TempPath.str(), TempPath2.str());
  165. fs::file_status A, B;
  166. ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
  167. ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
  168. EXPECT_FALSE(fs::equivalent(A, B));
  169. // Try to copy the first to the second.
  170. EXPECT_EQ(
  171. fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
  172. ::close(FD2);
  173. // Try again with the proper options.
  174. ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
  175. fs::copy_option::overwrite_if_exists));
  176. // Remove Temp2.
  177. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
  178. EXPECT_TRUE(TempFileExists);
  179. // Make sure Temp2 doesn't exist.
  180. ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
  181. EXPECT_FALSE(TempFileExists);
  182. // Create a hard link to Temp1.
  183. ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
  184. bool equal;
  185. ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
  186. EXPECT_TRUE(equal);
  187. ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
  188. ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
  189. EXPECT_TRUE(fs::equivalent(A, B));
  190. // Remove Temp1.
  191. ::close(FileDescriptor);
  192. ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
  193. EXPECT_TRUE(TempFileExists);
  194. // Remove the hard link.
  195. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
  196. EXPECT_TRUE(TempFileExists);
  197. // Make sure Temp1 doesn't exist.
  198. ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
  199. EXPECT_FALSE(TempFileExists);
  200. }
  201. TEST_F(FileSystemTest, DirectoryIteration) {
  202. error_code ec;
  203. for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
  204. ASSERT_NO_ERROR(ec);
  205. // Create a known hierarchy to recurse over.
  206. bool existed;
  207. ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
  208. + "/recursive/a0/aa1", existed));
  209. ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
  210. + "/recursive/a0/ab1", existed));
  211. ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
  212. + "/recursive/dontlookhere/da1", existed));
  213. ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
  214. + "/recursive/z0/za1", existed));
  215. ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
  216. + "/recursive/pop/p1", existed));
  217. typedef std::vector<std::string> v_t;
  218. v_t visited;
  219. for (fs::recursive_directory_iterator i(Twine(TestDirectory)
  220. + "/recursive", ec), e; i != e; i.increment(ec)){
  221. ASSERT_NO_ERROR(ec);
  222. if (path::filename(i->path()) == "p1") {
  223. i.pop();
  224. // FIXME: recursive_directory_iterator should be more robust.
  225. if (i == e) break;
  226. }
  227. if (path::filename(i->path()) == "dontlookhere")
  228. i.no_push();
  229. visited.push_back(path::filename(i->path()));
  230. }
  231. v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
  232. v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
  233. v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
  234. v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
  235. "dontlookhere");
  236. v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
  237. v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
  238. v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
  239. v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
  240. v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
  241. // Make sure that each path was visited correctly.
  242. ASSERT_NE(a0, visited.end());
  243. ASSERT_NE(aa1, visited.end());
  244. ASSERT_NE(ab1, visited.end());
  245. ASSERT_NE(dontlookhere, visited.end());
  246. ASSERT_EQ(da1, visited.end()); // Not visited.
  247. ASSERT_NE(z0, visited.end());
  248. ASSERT_NE(za1, visited.end());
  249. ASSERT_NE(pop, visited.end());
  250. ASSERT_EQ(p1, visited.end()); // Not visited.
  251. // Make sure that parents were visited before children. No other ordering
  252. // guarantees can be made across siblings.
  253. ASSERT_LT(a0, aa1);
  254. ASSERT_LT(a0, ab1);
  255. ASSERT_LT(z0, za1);
  256. }
  257. TEST_F(FileSystemTest, Magic) {
  258. struct type {
  259. const char *filename;
  260. const char *magic_str;
  261. size_t magic_str_len;
  262. } types [] = {{"magic.archive", "!<arch>\x0A", 8}};
  263. // Create some files filled with magic.
  264. for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
  265. ++i) {
  266. SmallString<128> file_pathname(TestDirectory);
  267. path::append(file_pathname, i->filename);
  268. std::string ErrMsg;
  269. raw_fd_ostream file(file_pathname.c_str(), ErrMsg,
  270. raw_fd_ostream::F_Binary);
  271. ASSERT_FALSE(file.has_error());
  272. StringRef magic(i->magic_str, i->magic_str_len);
  273. file << magic;
  274. file.close();
  275. bool res = false;
  276. ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
  277. EXPECT_TRUE(res);
  278. }
  279. }
  280. #if !defined(_WIN32) // FIXME: Win32 has different permission schema.
  281. TEST_F(FileSystemTest, Permissions) {
  282. // Create a temp file.
  283. int FileDescriptor;
  284. SmallString<64> TempPath;
  285. ASSERT_NO_ERROR(
  286. fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
  287. // Mark file as read-only
  288. const fs::perms AllWrite = fs::owner_write|fs::group_write|fs::others_write;
  289. ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::remove_perms|AllWrite));
  290. // Verify file is read-only
  291. fs::file_status Status;
  292. ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
  293. bool AnyWriteBits = (Status.permissions() & AllWrite);
  294. EXPECT_FALSE(AnyWriteBits);
  295. // Mark file as read-write
  296. ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::add_perms|AllWrite));
  297. // Verify file is read-write
  298. ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
  299. AnyWriteBits = (Status.permissions() & AllWrite);
  300. EXPECT_TRUE(AnyWriteBits);
  301. }
  302. #endif
  303. TEST_F(FileSystemTest, FileMapping) {
  304. // Create a temp file.
  305. int FileDescriptor;
  306. SmallString<64> TempPath;
  307. ASSERT_NO_ERROR(
  308. fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
  309. // Map in temp file and add some content
  310. error_code EC;
  311. StringRef Val("hello there");
  312. {
  313. fs::mapped_file_region mfr(FileDescriptor,
  314. fs::mapped_file_region::readwrite,
  315. 4096,
  316. 0,
  317. EC);
  318. ASSERT_NO_ERROR(EC);
  319. std::copy(Val.begin(), Val.end(), mfr.data());
  320. // Explicitly add a 0.
  321. mfr.data()[Val.size()] = 0;
  322. // Unmap temp file
  323. }
  324. // Map it back in read-only
  325. fs::mapped_file_region mfr(Twine(TempPath),
  326. fs::mapped_file_region::readonly,
  327. 0,
  328. 0,
  329. EC);
  330. ASSERT_NO_ERROR(EC);
  331. // Verify content
  332. EXPECT_EQ(StringRef(mfr.const_data()), Val);
  333. // Unmap temp file
  334. #if LLVM_HAS_RVALUE_REFERENCES
  335. fs::mapped_file_region m(Twine(TempPath),
  336. fs::mapped_file_region::readonly,
  337. 0,
  338. 0,
  339. EC);
  340. ASSERT_NO_ERROR(EC);
  341. const char *Data = m.const_data();
  342. fs::mapped_file_region mfrrv(llvm_move(m));
  343. EXPECT_EQ(mfrrv.const_data(), Data);
  344. #endif
  345. }
  346. } // anonymous namespace