testSystemTools.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #if defined(_MSC_VER)
  12. # pragma warning (disable:4786)
  13. #endif
  14. #include KWSYS_HEADER(SystemTools.hxx)
  15. // Work-around CMake dependency scanning limitation. This must
  16. // duplicate the above list of headers.
  17. #if 0
  18. # include "SystemTools.hxx.in"
  19. #endif
  20. // Include with <> instead of "" to avoid getting any in-source copy
  21. // left on disk.
  22. #include <testSystemTools.h>
  23. #include <iostream>
  24. #include <string.h> /* strcmp */
  25. #if defined(_WIN32) && !defined(__CYGWIN__)
  26. # include <io.h> /* _umask (MSVC) / umask (Borland) */
  27. # ifdef _MSC_VER
  28. # define umask _umask // Note this is still umask on Borland
  29. # endif
  30. #endif
  31. #include <sys/stat.h> /* umask (POSIX), _S_I* constants (Windows) */
  32. // Visual C++ does not define mode_t (note that Borland does, however).
  33. #if defined( _MSC_VER )
  34. typedef unsigned short mode_t;
  35. #endif
  36. //----------------------------------------------------------------------------
  37. static const char* toUnixPaths[][2] =
  38. {
  39. { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
  40. { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
  41. { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  42. { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
  43. { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  44. { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  45. { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
  46. { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
  47. { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  48. { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
  49. { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  50. { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  51. { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
  52. { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
  53. { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo\\ cal/bin/pa\\ sswd" },
  54. {0, 0}
  55. };
  56. static bool CheckConvertToUnixSlashes(std::string input,
  57. std::string output)
  58. {
  59. std::string result = input;
  60. kwsys::SystemTools::ConvertToUnixSlashes(result);
  61. if ( result != output )
  62. {
  63. std::cerr
  64. << "Problem with ConvertToUnixSlashes - input: " << input
  65. << " output: " << result << " expected: " << output
  66. << std::endl;
  67. return false;
  68. }
  69. return true;
  70. }
  71. //----------------------------------------------------------------------------
  72. static const char* checkEscapeChars[][4] =
  73. {
  74. { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2"},
  75. { " {} ", "{}", "#", " #{#} "},
  76. {0, 0, 0, 0}
  77. };
  78. static bool CheckEscapeChars(std::string input,
  79. const char *chars_to_escape,
  80. char escape_char,
  81. std::string output)
  82. {
  83. std::string result = kwsys::SystemTools::EscapeChars(
  84. input.c_str(), chars_to_escape, escape_char);
  85. if (result != output)
  86. {
  87. std::cerr
  88. << "Problem with CheckEscapeChars - input: " << input
  89. << " output: " << result << " expected: " << output
  90. << std::endl;
  91. return false;
  92. }
  93. return true;
  94. }
  95. //----------------------------------------------------------------------------
  96. static bool CheckFileOperations()
  97. {
  98. bool res = true;
  99. const std::string testNonExistingFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  100. "/testSystemToolsNonExistingFile");
  101. const std::string testDotFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  102. "/.");
  103. const std::string testBinFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  104. "/testSystemTools.bin");
  105. const std::string testTxtFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  106. "/testSystemTools.cxx");
  107. const std::string testNewDir(TEST_SYSTEMTOOLS_BINARY_DIR
  108. "/testSystemToolsNewDir");
  109. const std::string testNewFile(testNewDir + "/testNewFile.txt");
  110. if (kwsys::SystemTools::DetectFileType(testNonExistingFile.c_str()) !=
  111. kwsys::SystemTools::FileTypeUnknown)
  112. {
  113. std::cerr
  114. << "Problem with DetectFileType - failed to detect type of: "
  115. << testNonExistingFile << std::endl;
  116. res = false;
  117. }
  118. if (kwsys::SystemTools::DetectFileType(testDotFile.c_str()) !=
  119. kwsys::SystemTools::FileTypeUnknown)
  120. {
  121. std::cerr
  122. << "Problem with DetectFileType - failed to detect type of: "
  123. << testDotFile << std::endl;
  124. res = false;
  125. }
  126. if (kwsys::SystemTools::DetectFileType(testBinFile.c_str()) !=
  127. kwsys::SystemTools::FileTypeBinary)
  128. {
  129. std::cerr
  130. << "Problem with DetectFileType - failed to detect type of: "
  131. << testBinFile << std::endl;
  132. res = false;
  133. }
  134. if (kwsys::SystemTools::DetectFileType(testTxtFile.c_str()) !=
  135. kwsys::SystemTools::FileTypeText)
  136. {
  137. std::cerr
  138. << "Problem with DetectFileType - failed to detect type of: "
  139. << testTxtFile << std::endl;
  140. res = false;
  141. }
  142. if (kwsys::SystemTools::FileLength(testBinFile) != 766)
  143. {
  144. std::cerr
  145. << "Problem with FileLength - incorrect length for: "
  146. << testBinFile << std::endl;
  147. res = false;
  148. }
  149. if (!kwsys::SystemTools::MakeDirectory(testNewDir))
  150. {
  151. std::cerr
  152. << "Problem with MakeDirectory for: "
  153. << testNewDir << std::endl;
  154. res = false;
  155. }
  156. if (!kwsys::SystemTools::Touch(testNewFile.c_str(), true))
  157. {
  158. std::cerr
  159. << "Problem with Touch for: "
  160. << testNewFile << std::endl;
  161. res = false;
  162. }
  163. // Reset umask
  164. #if defined(_WIN32) && !defined(__CYGWIN__)
  165. // NOTE: Windows doesn't support toggling _S_IREAD.
  166. mode_t fullMask = _S_IWRITE;
  167. #else
  168. // On a normal POSIX platform, we can toggle all permissions.
  169. mode_t fullMask = S_IRWXU | S_IRWXG | S_IRWXO;
  170. #endif
  171. mode_t orig_umask = umask(fullMask);
  172. // Test file permissions without umask
  173. mode_t origPerm, thisPerm;
  174. if (!kwsys::SystemTools::GetPermissions(testNewFile, origPerm))
  175. {
  176. std::cerr
  177. << "Problem with GetPermissions (1) for: "
  178. << testNewFile << std::endl;
  179. res = false;
  180. }
  181. if (!kwsys::SystemTools::SetPermissions(testNewFile, 0))
  182. {
  183. std::cerr
  184. << "Problem with SetPermissions (1) for: "
  185. << testNewFile << std::endl;
  186. res = false;
  187. }
  188. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm))
  189. {
  190. std::cerr
  191. << "Problem with GetPermissions (2) for: "
  192. << testNewFile << std::endl;
  193. res = false;
  194. }
  195. if ((thisPerm & fullMask) != 0)
  196. {
  197. std::cerr
  198. << "SetPermissions failed to set permissions (1) for: "
  199. << testNewFile << ": actual = " << thisPerm << "; expected = "
  200. << 0 << std::endl;
  201. res = false;
  202. }
  203. // While we're at it, check proper TestFileAccess functionality.
  204. if (kwsys::SystemTools::TestFileAccess(testNewFile,
  205. kwsys::TEST_FILE_WRITE))
  206. {
  207. std::cerr
  208. << "TestFileAccess incorrectly indicated that this is a writable file:"
  209. << testNewFile << std::endl;
  210. res = false;
  211. }
  212. if (!kwsys::SystemTools::TestFileAccess(testNewFile,
  213. kwsys::TEST_FILE_OK))
  214. {
  215. std::cerr
  216. << "TestFileAccess incorrectly indicated that this file does not exist:"
  217. << testNewFile << std::endl;
  218. res = false;
  219. }
  220. // Test restoring/setting full permissions.
  221. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask))
  222. {
  223. std::cerr
  224. << "Problem with SetPermissions (2) for: "
  225. << testNewFile << std::endl;
  226. res = false;
  227. }
  228. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm))
  229. {
  230. std::cerr
  231. << "Problem with GetPermissions (3) for: "
  232. << testNewFile << std::endl;
  233. res = false;
  234. }
  235. if ((thisPerm & fullMask) != fullMask)
  236. {
  237. std::cerr
  238. << "SetPermissions failed to set permissions (2) for: "
  239. << testNewFile << ": actual = " << thisPerm << "; expected = "
  240. << fullMask << std::endl;
  241. res = false;
  242. }
  243. // Test setting file permissions while honoring umask
  244. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask, true))
  245. {
  246. std::cerr
  247. << "Problem with SetPermissions (3) for: "
  248. << testNewFile << std::endl;
  249. res = false;
  250. }
  251. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm))
  252. {
  253. std::cerr
  254. << "Problem with GetPermissions (4) for: "
  255. << testNewFile << std::endl;
  256. res = false;
  257. }
  258. if ((thisPerm & fullMask) != 0)
  259. {
  260. std::cerr
  261. << "SetPermissions failed to honor umask for: "
  262. << testNewFile << ": actual = " << thisPerm << "; expected = "
  263. << 0 << std::endl;
  264. res = false;
  265. }
  266. // Restore umask
  267. umask(orig_umask);
  268. // Restore file permissions
  269. if (!kwsys::SystemTools::SetPermissions(testNewFile, origPerm))
  270. {
  271. std::cerr
  272. << "Problem with SetPermissions (4) for: "
  273. << testNewFile << std::endl;
  274. res = false;
  275. }
  276. // Remove the test file
  277. if (!kwsys::SystemTools::RemoveFile(testNewFile))
  278. {
  279. std::cerr
  280. << "Problem with RemoveFile: "
  281. << testNewFile << std::endl;
  282. res = false;
  283. }
  284. std::string const testFileMissing(testNewDir + "/testMissingFile.txt");
  285. if (!kwsys::SystemTools::RemoveFile(testFileMissing))
  286. {
  287. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  288. std::cerr <<
  289. "RemoveFile(\"" << testFileMissing << "\") failed: " << msg << "\n";
  290. res = false;
  291. }
  292. std::string const testFileMissingDir(testNewDir + "/missing/file.txt");
  293. if (!kwsys::SystemTools::RemoveFile(testFileMissingDir))
  294. {
  295. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  296. std::cerr <<
  297. "RemoveFile(\"" << testFileMissingDir << "\") failed: " << msg << "\n";
  298. res = false;
  299. }
  300. kwsys::SystemTools::Touch(testNewFile.c_str(), true);
  301. if (!kwsys::SystemTools::RemoveADirectory(testNewDir))
  302. {
  303. std::cerr
  304. << "Problem with RemoveADirectory for: "
  305. << testNewDir << std::endl;
  306. res = false;
  307. }
  308. #ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  309. // Perform the same file and directory creation and deletion tests but
  310. // with paths > 256 characters in length.
  311. const std::string testNewLongDir(
  312. TEST_SYSTEMTOOLS_BINARY_DIR "/"
  313. "012345678901234567890123456789012345678901234567890123456789"
  314. "012345678901234567890123456789012345678901234567890123456789"
  315. "012345678901234567890123456789012345678901234567890123456789"
  316. "012345678901234567890123456789012345678901234567890123456789"
  317. "01234567890123");
  318. const std::string testNewLongFile(testNewLongDir + "/"
  319. "012345678901234567890123456789012345678901234567890123456789"
  320. "012345678901234567890123456789012345678901234567890123456789"
  321. "012345678901234567890123456789012345678901234567890123456789"
  322. "012345678901234567890123456789012345678901234567890123456789"
  323. "0123456789.txt");
  324. if (!kwsys::SystemTools::MakeDirectory(testNewLongDir))
  325. {
  326. std::cerr
  327. << "Problem with MakeDirectory for: "
  328. << testNewLongDir << std::endl;
  329. res = false;
  330. }
  331. if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true))
  332. {
  333. std::cerr
  334. << "Problem with Touch for: "
  335. << testNewLongFile << std::endl;
  336. res = false;
  337. }
  338. if (!kwsys::SystemTools::RemoveFile(testNewLongFile))
  339. {
  340. std::cerr
  341. << "Problem with RemoveFile: "
  342. << testNewLongFile << std::endl;
  343. res = false;
  344. }
  345. kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
  346. if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir))
  347. {
  348. std::cerr
  349. << "Problem with RemoveADirectory for: "
  350. << testNewLongDir << std::endl;
  351. res = false;
  352. }
  353. #endif
  354. return res;
  355. }
  356. //----------------------------------------------------------------------------
  357. static bool CheckStringOperations()
  358. {
  359. bool res = true;
  360. std::string test = "mary had a little lamb.";
  361. if (kwsys::SystemTools::CapitalizedWords(test) != "Mary Had A Little Lamb.")
  362. {
  363. std::cerr
  364. << "Problem with CapitalizedWords "
  365. << '"' << test << '"' << std::endl;
  366. res = false;
  367. }
  368. test = "Mary Had A Little Lamb.";
  369. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  370. "mary had a little lamb.")
  371. {
  372. std::cerr
  373. << "Problem with UnCapitalizedWords "
  374. << '"' << test << '"' << std::endl;
  375. res = false;
  376. }
  377. test = "MaryHadTheLittleLamb.";
  378. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  379. "Mary Had The Little Lamb.")
  380. {
  381. std::cerr
  382. << "Problem with AddSpaceBetweenCapitalizedWords "
  383. << '"' << test << '"' << std::endl;
  384. res = false;
  385. }
  386. char * cres =
  387. kwsys::SystemTools::AppendStrings("Mary Had A"," Little Lamb.");
  388. if (strcmp(cres,"Mary Had A Little Lamb."))
  389. {
  390. std::cerr
  391. << "Problem with AppendStrings "
  392. << "\"Mary Had A\" \" Little Lamb.\"" << std::endl;
  393. res = false;
  394. }
  395. delete [] cres;
  396. cres =
  397. kwsys::SystemTools::AppendStrings("Mary Had"," A ","Little Lamb.");
  398. if (strcmp(cres,"Mary Had A Little Lamb."))
  399. {
  400. std::cerr
  401. << "Problem with AppendStrings "
  402. << "\"Mary Had\" \" A \" \"Little Lamb.\"" << std::endl;
  403. res = false;
  404. }
  405. delete [] cres;
  406. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.",'a') != 3)
  407. {
  408. std::cerr
  409. << "Problem with CountChar "
  410. << "\"Mary Had A Little Lamb.\"" << std::endl;
  411. res = false;
  412. }
  413. cres =
  414. kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.","aeiou");
  415. if (strcmp(cres,"Mry Hd A Lttl Lmb."))
  416. {
  417. std::cerr
  418. << "Problem with RemoveChars "
  419. << "\"Mary Had A Little Lamb.\"" << std::endl;
  420. res = false;
  421. }
  422. delete [] cres;
  423. cres =
  424. kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  425. if (strcmp(cres,"A"))
  426. {
  427. std::cerr
  428. << "Problem with RemoveCharsButUpperHex "
  429. << "\"Mary Had A Little Lamb.\"" << std::endl;
  430. res = false;
  431. }
  432. delete [] cres;
  433. char *cres2 = new char [strlen("Mary Had A Little Lamb.")+1];
  434. strcpy(cres2,"Mary Had A Little Lamb.");
  435. kwsys::SystemTools::ReplaceChars(cres2,"aeiou",'X');
  436. if (strcmp(cres2,"MXry HXd A LXttlX LXmb."))
  437. {
  438. std::cerr
  439. << "Problem with ReplaceChars "
  440. << "\"Mary Had A Little Lamb.\"" << std::endl;
  441. res = false;
  442. }
  443. delete [] cres2;
  444. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  445. "Mary "))
  446. {
  447. std::cerr
  448. << "Problem with StringStartsWith "
  449. << "\"Mary Had A Little Lamb.\"" << std::endl;
  450. res = false;
  451. }
  452. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  453. " Lamb."))
  454. {
  455. std::cerr
  456. << "Problem with StringEndsWith "
  457. << "\"Mary Had A Little Lamb.\"" << std::endl;
  458. res = false;
  459. }
  460. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  461. if (strcmp(cres,"Mary Had A Little Lamb."))
  462. {
  463. std::cerr
  464. << "Problem with DuplicateString "
  465. << "\"Mary Had A Little Lamb.\"" << std::endl;
  466. res = false;
  467. }
  468. delete [] cres;
  469. test = "Mary Had A Little Lamb.";
  470. if (kwsys::SystemTools::CropString(test,13) !=
  471. "Mary ...Lamb.")
  472. {
  473. std::cerr
  474. << "Problem with CropString "
  475. << "\"Mary Had A Little Lamb.\"" << std::endl;
  476. res = false;
  477. }
  478. std::vector<std::string> lines;
  479. kwsys::SystemTools::Split("Mary Had A Little Lamb.",lines,' ');
  480. if (lines[0] != "Mary" || lines[1] != "Had" ||
  481. lines[2] != "A" || lines[3] != "Little" || lines[4] != "Lamb.")
  482. {
  483. std::cerr
  484. << "Problem with Split "
  485. << "\"Mary Had A Little Lamb.\"" << std::endl;
  486. res = false;
  487. }
  488. #ifdef _WIN32
  489. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  490. ("L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  491. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  492. {
  493. std::cerr
  494. << "Problem with ConvertToWindowsExtendedPath "
  495. << "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  496. << std::endl;
  497. res = false;
  498. }
  499. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  500. ("L:/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  501. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  502. {
  503. std::cerr
  504. << "Problem with ConvertToWindowsExtendedPath "
  505. << "\"L:/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  506. << std::endl;
  507. res = false;
  508. }
  509. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  510. ("\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  511. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  512. {
  513. std::cerr
  514. << "Problem with ConvertToWindowsExtendedPath "
  515. << "\"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  516. << std::endl;
  517. res = false;
  518. }
  519. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  520. ("//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  521. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  522. {
  523. std::cerr
  524. << "Problem with ConvertToWindowsExtendedPath "
  525. << "\"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  526. << std::endl;
  527. res = false;
  528. }
  529. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("//") !=
  530. L"//")
  531. {
  532. std::cerr
  533. << "Problem with ConvertToWindowsExtendedPath "
  534. << "\"//\""
  535. << std::endl;
  536. res = false;
  537. }
  538. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\") !=
  539. L"\\\\.\\")
  540. {
  541. std::cerr
  542. << "Problem with ConvertToWindowsExtendedPath "
  543. << "\"\\\\.\\\""
  544. << std::endl;
  545. res = false;
  546. }
  547. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X") !=
  548. L"\\\\.\\X")
  549. {
  550. std::cerr
  551. << "Problem with ConvertToWindowsExtendedPath "
  552. << "\"\\\\.\\X\""
  553. << std::endl;
  554. res = false;
  555. }
  556. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:") !=
  557. L"\\\\?\\X:")
  558. {
  559. std::cerr
  560. << "Problem with ConvertToWindowsExtendedPath "
  561. << "\"\\\\.\\X:\""
  562. << std::endl;
  563. res = false;
  564. }
  565. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:\\") !=
  566. L"\\\\?\\X:\\")
  567. {
  568. std::cerr
  569. << "Problem with ConvertToWindowsExtendedPath "
  570. << "\"\\\\.\\X:\\\""
  571. << std::endl;
  572. res = false;
  573. }
  574. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("NUL") !=
  575. L"\\\\.\\NUL")
  576. {
  577. std::cerr
  578. << "Problem with ConvertToWindowsExtendedPath "
  579. << "\"NUL\""
  580. << std::endl;
  581. res = false;
  582. }
  583. #endif
  584. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  585. ("L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  586. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  587. {
  588. std::cerr
  589. << "Problem with ConvertToWindowsOutputPath "
  590. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\""
  591. << std::endl;
  592. res = false;
  593. }
  594. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  595. ("//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  596. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  597. {
  598. std::cerr
  599. << "Problem with ConvertToWindowsOutputPath "
  600. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  601. << std::endl;
  602. res = false;
  603. }
  604. if (kwsys::SystemTools::ConvertToUnixOutputPath
  605. ("//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  606. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo")
  607. {
  608. std::cerr
  609. << "Problem with ConvertToUnixOutputPath "
  610. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\""
  611. << std::endl;
  612. res = false;
  613. }
  614. return res;
  615. }
  616. //----------------------------------------------------------------------------
  617. static bool CheckPutEnv(const std::string& env, const char* name, const char* value)
  618. {
  619. if(!kwsys::SystemTools::PutEnv(env))
  620. {
  621. std::cerr << "PutEnv(\"" << env
  622. << "\") failed!" << std::endl;
  623. return false;
  624. }
  625. const char* v = kwsys::SystemTools::GetEnv(name);
  626. v = v? v : "(null)";
  627. if(strcmp(v, value) != 0)
  628. {
  629. std::cerr << "GetEnv(\"" << name << "\") returned \""
  630. << v << "\", not \"" << value << "\"!" << std::endl;
  631. return false;
  632. }
  633. return true;
  634. }
  635. static bool CheckUnPutEnv(const char* env, const char* name)
  636. {
  637. if(!kwsys::SystemTools::UnPutEnv(env))
  638. {
  639. std::cerr << "UnPutEnv(\"" << env << "\") failed!"
  640. << std::endl;
  641. return false;
  642. }
  643. if(const char* v = kwsys::SystemTools::GetEnv(name))
  644. {
  645. std::cerr << "GetEnv(\"" << name << "\") returned \""
  646. << v << "\", not (null)!" << std::endl;
  647. return false;
  648. }
  649. return true;
  650. }
  651. static bool CheckEnvironmentOperations()
  652. {
  653. bool res = true;
  654. res &= CheckPutEnv("A=B", "A", "B");
  655. res &= CheckPutEnv("B=C", "B", "C");
  656. res &= CheckPutEnv("C=D", "C", "D");
  657. res &= CheckPutEnv("D=E", "D", "E");
  658. res &= CheckUnPutEnv("A", "A");
  659. res &= CheckUnPutEnv("B=", "B");
  660. res &= CheckUnPutEnv("C=D", "C");
  661. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  662. return res;
  663. }
  664. static bool CheckRelativePath(
  665. const std::string& local,
  666. const std::string& remote,
  667. const std::string& expected)
  668. {
  669. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  670. if(expected != result)
  671. {
  672. std::cerr << "RelativePath(" << local << ", " << remote
  673. << ") yielded " << result << " instead of " << expected << std::endl;
  674. return false;
  675. }
  676. return true;
  677. }
  678. static bool CheckRelativePaths()
  679. {
  680. bool res = true;
  681. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  682. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  683. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  684. res &= CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  685. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  686. return res;
  687. }
  688. static bool CheckCollapsePath(
  689. const std::string& path,
  690. const std::string& expected)
  691. {
  692. std::string result = kwsys::SystemTools::CollapseFullPath(path);
  693. if(expected != result)
  694. {
  695. std::cerr << "CollapseFullPath(" << path
  696. << ") yielded " << result << " instead of " << expected << std::endl;
  697. return false;
  698. }
  699. return true;
  700. }
  701. static bool CheckCollapsePath()
  702. {
  703. bool res = true;
  704. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  705. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  706. return res;
  707. }
  708. //----------------------------------------------------------------------------
  709. int testSystemTools(int, char*[])
  710. {
  711. bool res = true;
  712. int cc;
  713. for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
  714. {
  715. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  716. }
  717. // Special check for ~
  718. std::string output;
  719. if(kwsys::SystemTools::GetEnv("HOME", output))
  720. {
  721. output += "/foo bar/lala";
  722. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  723. }
  724. for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
  725. {
  726. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  727. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  728. }
  729. res &= CheckFileOperations();
  730. res &= CheckStringOperations();
  731. res &= CheckEnvironmentOperations();
  732. res &= CheckRelativePaths();
  733. res &= CheckCollapsePath();
  734. return res ? 0 : 1;
  735. }