testSystemTools.cxx 25 KB

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