testSystemTools.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. // calling it again should just return true
  158. if (!kwsys::SystemTools::MakeDirectory(testNewDir))
  159. {
  160. std::cerr
  161. << "Problem with second call to MakeDirectory for: "
  162. << testNewDir << std::endl;
  163. res = false;
  164. }
  165. // calling with 0 pointer should return false
  166. if (kwsys::SystemTools::MakeDirectory(0))
  167. {
  168. std::cerr
  169. << "Problem with MakeDirectory(0)"
  170. << std::endl;
  171. res = false;
  172. }
  173. // calling with an empty string should return false
  174. if (kwsys::SystemTools::MakeDirectory(std::string()))
  175. {
  176. std::cerr
  177. << "Problem with MakeDirectory(std::string())"
  178. << std::endl;
  179. res = false;
  180. }
  181. // check existence
  182. if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false))
  183. {
  184. std::cerr
  185. << "Problem with FileExists as C string and not file for: "
  186. << testNewDir << std::endl;
  187. res = false;
  188. }
  189. // remove it
  190. if (!kwsys::SystemTools::RemoveADirectory(testNewDir))
  191. {
  192. std::cerr
  193. << "Problem with RemoveADirectory for: "
  194. << testNewDir << std::endl;
  195. res = false;
  196. }
  197. // check existence
  198. if (kwsys::SystemTools::FileExists(testNewDir.c_str(), false))
  199. {
  200. std::cerr
  201. << "After RemoveADirectory: "
  202. << "Problem with FileExists as C string and not file for: "
  203. << testNewDir << std::endl;
  204. res = false;
  205. }
  206. // create it using the char* version
  207. if (!kwsys::SystemTools::MakeDirectory(testNewDir.c_str()))
  208. {
  209. std::cerr
  210. << "Problem with second call to MakeDirectory as C string for: "
  211. << testNewDir << std::endl;
  212. res = false;
  213. }
  214. if (!kwsys::SystemTools::Touch(testNewFile.c_str(), true))
  215. {
  216. std::cerr
  217. << "Problem with Touch for: "
  218. << testNewFile << std::endl;
  219. res = false;
  220. }
  221. // calling MakeDirectory with something that is no file should fail
  222. if (kwsys::SystemTools::MakeDirectory(testNewFile))
  223. {
  224. std::cerr
  225. << "Problem with to MakeDirectory for: "
  226. << testNewFile << std::endl;
  227. res = false;
  228. }
  229. // calling with 0 pointer should return false
  230. if (kwsys::SystemTools::FileExists(0))
  231. {
  232. std::cerr
  233. << "Problem with FileExists(0)"
  234. << std::endl;
  235. res = false;
  236. }
  237. if (kwsys::SystemTools::FileExists(0, true))
  238. {
  239. std::cerr
  240. << "Problem with FileExists(0) as file"
  241. << std::endl;
  242. res = false;
  243. }
  244. // calling with an empty string should return false
  245. if (kwsys::SystemTools::FileExists(std::string()))
  246. {
  247. std::cerr
  248. << "Problem with FileExists(std::string())"
  249. << std::endl;
  250. res = false;
  251. }
  252. // FileExists(x, true) should return false on a directory
  253. if (kwsys::SystemTools::FileExists(testNewDir, true))
  254. {
  255. std::cerr
  256. << "Problem with FileExists as file for: "
  257. << testNewDir << std::endl;
  258. res = false;
  259. }
  260. if (kwsys::SystemTools::FileExists(testNewDir.c_str(), true))
  261. {
  262. std::cerr
  263. << "Problem with FileExists as C string and file for: "
  264. << testNewDir << std::endl;
  265. res = false;
  266. }
  267. // FileExists(x, false) should return true even on a directory
  268. if (!kwsys::SystemTools::FileExists(testNewDir, false))
  269. {
  270. std::cerr
  271. << "Problem with FileExists as not file for: "
  272. << testNewDir << std::endl;
  273. res = false;
  274. }
  275. if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false))
  276. {
  277. std::cerr
  278. << "Problem with FileExists as C string and not file for: "
  279. << testNewDir << std::endl;
  280. res = false;
  281. }
  282. // should work, was created as new file before
  283. if (!kwsys::SystemTools::FileExists(testNewFile))
  284. {
  285. std::cerr
  286. << "Problem with FileExists for: "
  287. << testNewDir << std::endl;
  288. res = false;
  289. }
  290. if (!kwsys::SystemTools::FileExists(testNewFile.c_str()))
  291. {
  292. std::cerr
  293. << "Problem with FileExists as C string for: "
  294. << testNewDir << std::endl;
  295. res = false;
  296. }
  297. if (!kwsys::SystemTools::FileExists(testNewFile, true))
  298. {
  299. std::cerr
  300. << "Problem with FileExists as file for: "
  301. << testNewDir << std::endl;
  302. res = false;
  303. }
  304. if (!kwsys::SystemTools::FileExists(testNewFile.c_str(), true))
  305. {
  306. std::cerr
  307. << "Problem with FileExists as C string and file for: "
  308. << testNewDir << std::endl;
  309. res = false;
  310. }
  311. // Reset umask
  312. #if defined(_WIN32) && !defined(__CYGWIN__)
  313. // NOTE: Windows doesn't support toggling _S_IREAD.
  314. mode_t fullMask = _S_IWRITE;
  315. #else
  316. // On a normal POSIX platform, we can toggle all permissions.
  317. mode_t fullMask = S_IRWXU | S_IRWXG | S_IRWXO;
  318. #endif
  319. mode_t orig_umask = umask(fullMask);
  320. // Test file permissions without umask
  321. mode_t origPerm, thisPerm;
  322. if (!kwsys::SystemTools::GetPermissions(testNewFile, origPerm))
  323. {
  324. std::cerr
  325. << "Problem with GetPermissions (1) for: "
  326. << testNewFile << std::endl;
  327. res = false;
  328. }
  329. if (!kwsys::SystemTools::SetPermissions(testNewFile, 0))
  330. {
  331. std::cerr
  332. << "Problem with SetPermissions (1) for: "
  333. << testNewFile << std::endl;
  334. res = false;
  335. }
  336. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm))
  337. {
  338. std::cerr
  339. << "Problem with GetPermissions (2) for: "
  340. << testNewFile << std::endl;
  341. res = false;
  342. }
  343. if ((thisPerm & fullMask) != 0)
  344. {
  345. std::cerr
  346. << "SetPermissions failed to set permissions (1) for: "
  347. << testNewFile << ": actual = " << thisPerm << "; expected = "
  348. << 0 << std::endl;
  349. res = false;
  350. }
  351. // While we're at it, check proper TestFileAccess functionality.
  352. if (kwsys::SystemTools::TestFileAccess(testNewFile,
  353. kwsys::TEST_FILE_WRITE))
  354. {
  355. std::cerr
  356. << "TestFileAccess incorrectly indicated that this is a writable file:"
  357. << testNewFile << std::endl;
  358. res = false;
  359. }
  360. if (!kwsys::SystemTools::TestFileAccess(testNewFile,
  361. kwsys::TEST_FILE_OK))
  362. {
  363. std::cerr
  364. << "TestFileAccess incorrectly indicated that this file does not exist:"
  365. << testNewFile << std::endl;
  366. res = false;
  367. }
  368. // Test restoring/setting full permissions.
  369. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask))
  370. {
  371. std::cerr
  372. << "Problem with SetPermissions (2) for: "
  373. << testNewFile << std::endl;
  374. res = false;
  375. }
  376. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm))
  377. {
  378. std::cerr
  379. << "Problem with GetPermissions (3) for: "
  380. << testNewFile << std::endl;
  381. res = false;
  382. }
  383. if ((thisPerm & fullMask) != fullMask)
  384. {
  385. std::cerr
  386. << "SetPermissions failed to set permissions (2) for: "
  387. << testNewFile << ": actual = " << thisPerm << "; expected = "
  388. << fullMask << std::endl;
  389. res = false;
  390. }
  391. // Test setting file permissions while honoring umask
  392. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask, true))
  393. {
  394. std::cerr
  395. << "Problem with SetPermissions (3) for: "
  396. << testNewFile << std::endl;
  397. res = false;
  398. }
  399. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm))
  400. {
  401. std::cerr
  402. << "Problem with GetPermissions (4) for: "
  403. << testNewFile << std::endl;
  404. res = false;
  405. }
  406. if ((thisPerm & fullMask) != 0)
  407. {
  408. std::cerr
  409. << "SetPermissions failed to honor umask for: "
  410. << testNewFile << ": actual = " << thisPerm << "; expected = "
  411. << 0 << std::endl;
  412. res = false;
  413. }
  414. // Restore umask
  415. umask(orig_umask);
  416. // Restore file permissions
  417. if (!kwsys::SystemTools::SetPermissions(testNewFile, origPerm))
  418. {
  419. std::cerr
  420. << "Problem with SetPermissions (4) for: "
  421. << testNewFile << std::endl;
  422. res = false;
  423. }
  424. // Remove the test file
  425. if (!kwsys::SystemTools::RemoveFile(testNewFile))
  426. {
  427. std::cerr
  428. << "Problem with RemoveFile: "
  429. << testNewFile << std::endl;
  430. res = false;
  431. }
  432. std::string const testFileMissing(testNewDir + "/testMissingFile.txt");
  433. if (!kwsys::SystemTools::RemoveFile(testFileMissing))
  434. {
  435. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  436. std::cerr <<
  437. "RemoveFile(\"" << testFileMissing << "\") failed: " << msg << "\n";
  438. res = false;
  439. }
  440. std::string const testFileMissingDir(testNewDir + "/missing/file.txt");
  441. if (!kwsys::SystemTools::RemoveFile(testFileMissingDir))
  442. {
  443. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  444. std::cerr <<
  445. "RemoveFile(\"" << testFileMissingDir << "\") failed: " << msg << "\n";
  446. res = false;
  447. }
  448. kwsys::SystemTools::Touch(testNewFile.c_str(), true);
  449. if (!kwsys::SystemTools::RemoveADirectory(testNewDir))
  450. {
  451. std::cerr
  452. << "Problem with RemoveADirectory for: "
  453. << testNewDir << std::endl;
  454. res = false;
  455. }
  456. #ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  457. // Perform the same file and directory creation and deletion tests but
  458. // with paths > 256 characters in length.
  459. const std::string testNewLongDir(
  460. TEST_SYSTEMTOOLS_BINARY_DIR "/"
  461. "012345678901234567890123456789012345678901234567890123456789"
  462. "012345678901234567890123456789012345678901234567890123456789"
  463. "012345678901234567890123456789012345678901234567890123456789"
  464. "012345678901234567890123456789012345678901234567890123456789"
  465. "01234567890123");
  466. const std::string testNewLongFile(testNewLongDir + "/"
  467. "012345678901234567890123456789012345678901234567890123456789"
  468. "012345678901234567890123456789012345678901234567890123456789"
  469. "012345678901234567890123456789012345678901234567890123456789"
  470. "012345678901234567890123456789012345678901234567890123456789"
  471. "0123456789.txt");
  472. if (!kwsys::SystemTools::MakeDirectory(testNewLongDir))
  473. {
  474. std::cerr
  475. << "Problem with MakeDirectory for: "
  476. << testNewLongDir << std::endl;
  477. res = false;
  478. }
  479. if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true))
  480. {
  481. std::cerr
  482. << "Problem with Touch for: "
  483. << testNewLongFile << std::endl;
  484. res = false;
  485. }
  486. if (!kwsys::SystemTools::RemoveFile(testNewLongFile))
  487. {
  488. std::cerr
  489. << "Problem with RemoveFile: "
  490. << testNewLongFile << std::endl;
  491. res = false;
  492. }
  493. kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
  494. if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir))
  495. {
  496. std::cerr
  497. << "Problem with RemoveADirectory for: "
  498. << testNewLongDir << std::endl;
  499. res = false;
  500. }
  501. #endif
  502. return res;
  503. }
  504. //----------------------------------------------------------------------------
  505. static bool CheckStringOperations()
  506. {
  507. bool res = true;
  508. std::string test = "mary had a little lamb.";
  509. if (kwsys::SystemTools::CapitalizedWords(test) != "Mary Had A Little Lamb.")
  510. {
  511. std::cerr
  512. << "Problem with CapitalizedWords "
  513. << '"' << test << '"' << std::endl;
  514. res = false;
  515. }
  516. test = "Mary Had A Little Lamb.";
  517. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  518. "mary had a little lamb.")
  519. {
  520. std::cerr
  521. << "Problem with UnCapitalizedWords "
  522. << '"' << test << '"' << std::endl;
  523. res = false;
  524. }
  525. test = "MaryHadTheLittleLamb.";
  526. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  527. "Mary Had The Little Lamb.")
  528. {
  529. std::cerr
  530. << "Problem with AddSpaceBetweenCapitalizedWords "
  531. << '"' << test << '"' << std::endl;
  532. res = false;
  533. }
  534. char * cres =
  535. kwsys::SystemTools::AppendStrings("Mary Had A"," Little Lamb.");
  536. if (strcmp(cres,"Mary Had A Little Lamb."))
  537. {
  538. std::cerr
  539. << "Problem with AppendStrings "
  540. << "\"Mary Had A\" \" Little Lamb.\"" << std::endl;
  541. res = false;
  542. }
  543. delete [] cres;
  544. cres =
  545. kwsys::SystemTools::AppendStrings("Mary Had"," A ","Little Lamb.");
  546. if (strcmp(cres,"Mary Had A Little Lamb."))
  547. {
  548. std::cerr
  549. << "Problem with AppendStrings "
  550. << "\"Mary Had\" \" A \" \"Little Lamb.\"" << std::endl;
  551. res = false;
  552. }
  553. delete [] cres;
  554. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.",'a') != 3)
  555. {
  556. std::cerr
  557. << "Problem with CountChar "
  558. << "\"Mary Had A Little Lamb.\"" << std::endl;
  559. res = false;
  560. }
  561. cres =
  562. kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.","aeiou");
  563. if (strcmp(cres,"Mry Hd A Lttl Lmb."))
  564. {
  565. std::cerr
  566. << "Problem with RemoveChars "
  567. << "\"Mary Had A Little Lamb.\"" << std::endl;
  568. res = false;
  569. }
  570. delete [] cres;
  571. cres =
  572. kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  573. if (strcmp(cres,"A"))
  574. {
  575. std::cerr
  576. << "Problem with RemoveCharsButUpperHex "
  577. << "\"Mary Had A Little Lamb.\"" << std::endl;
  578. res = false;
  579. }
  580. delete [] cres;
  581. char *cres2 = new char [strlen("Mary Had A Little Lamb.")+1];
  582. strcpy(cres2,"Mary Had A Little Lamb.");
  583. kwsys::SystemTools::ReplaceChars(cres2,"aeiou",'X');
  584. if (strcmp(cres2,"MXry HXd A LXttlX LXmb."))
  585. {
  586. std::cerr
  587. << "Problem with ReplaceChars "
  588. << "\"Mary Had A Little Lamb.\"" << std::endl;
  589. res = false;
  590. }
  591. delete [] cres2;
  592. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  593. "Mary "))
  594. {
  595. std::cerr
  596. << "Problem with StringStartsWith "
  597. << "\"Mary Had A Little Lamb.\"" << std::endl;
  598. res = false;
  599. }
  600. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  601. " Lamb."))
  602. {
  603. std::cerr
  604. << "Problem with StringEndsWith "
  605. << "\"Mary Had A Little Lamb.\"" << std::endl;
  606. res = false;
  607. }
  608. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  609. if (strcmp(cres,"Mary Had A Little Lamb."))
  610. {
  611. std::cerr
  612. << "Problem with DuplicateString "
  613. << "\"Mary Had A Little Lamb.\"" << std::endl;
  614. res = false;
  615. }
  616. delete [] cres;
  617. test = "Mary Had A Little Lamb.";
  618. if (kwsys::SystemTools::CropString(test,13) !=
  619. "Mary ...Lamb.")
  620. {
  621. std::cerr
  622. << "Problem with CropString "
  623. << "\"Mary Had A Little Lamb.\"" << std::endl;
  624. res = false;
  625. }
  626. std::vector<std::string> lines;
  627. kwsys::SystemTools::Split("Mary Had A Little Lamb.",lines,' ');
  628. if (lines[0] != "Mary" || lines[1] != "Had" ||
  629. lines[2] != "A" || lines[3] != "Little" || lines[4] != "Lamb.")
  630. {
  631. std::cerr
  632. << "Problem with Split "
  633. << "\"Mary Had A Little Lamb.\"" << std::endl;
  634. res = false;
  635. }
  636. #ifdef _WIN32
  637. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  638. ("L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  639. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  640. {
  641. std::cerr
  642. << "Problem with ConvertToWindowsExtendedPath "
  643. << "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  644. << std::endl;
  645. res = false;
  646. }
  647. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  648. ("L:/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  649. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  650. {
  651. std::cerr
  652. << "Problem with ConvertToWindowsExtendedPath "
  653. << "\"L:/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  654. << std::endl;
  655. res = false;
  656. }
  657. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  658. ("\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  659. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  660. {
  661. std::cerr
  662. << "Problem with ConvertToWindowsExtendedPath "
  663. << "\"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  664. << std::endl;
  665. res = false;
  666. }
  667. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  668. ("//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  669. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  670. {
  671. std::cerr
  672. << "Problem with ConvertToWindowsExtendedPath "
  673. << "\"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  674. << std::endl;
  675. res = false;
  676. }
  677. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("//") !=
  678. L"//")
  679. {
  680. std::cerr
  681. << "Problem with ConvertToWindowsExtendedPath "
  682. << "\"//\""
  683. << std::endl;
  684. res = false;
  685. }
  686. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\") !=
  687. L"\\\\.\\")
  688. {
  689. std::cerr
  690. << "Problem with ConvertToWindowsExtendedPath "
  691. << "\"\\\\.\\\""
  692. << std::endl;
  693. res = false;
  694. }
  695. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X") !=
  696. L"\\\\.\\X")
  697. {
  698. std::cerr
  699. << "Problem with ConvertToWindowsExtendedPath "
  700. << "\"\\\\.\\X\""
  701. << std::endl;
  702. res = false;
  703. }
  704. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:") !=
  705. L"\\\\?\\X:")
  706. {
  707. std::cerr
  708. << "Problem with ConvertToWindowsExtendedPath "
  709. << "\"\\\\.\\X:\""
  710. << std::endl;
  711. res = false;
  712. }
  713. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:\\") !=
  714. L"\\\\?\\X:\\")
  715. {
  716. std::cerr
  717. << "Problem with ConvertToWindowsExtendedPath "
  718. << "\"\\\\.\\X:\\\""
  719. << std::endl;
  720. res = false;
  721. }
  722. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("NUL") !=
  723. L"\\\\.\\NUL")
  724. {
  725. std::cerr
  726. << "Problem with ConvertToWindowsExtendedPath "
  727. << "\"NUL\""
  728. << std::endl;
  729. res = false;
  730. }
  731. #endif
  732. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  733. ("L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  734. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  735. {
  736. std::cerr
  737. << "Problem with ConvertToWindowsOutputPath "
  738. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\""
  739. << std::endl;
  740. res = false;
  741. }
  742. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  743. ("//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  744. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  745. {
  746. std::cerr
  747. << "Problem with ConvertToWindowsOutputPath "
  748. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  749. << std::endl;
  750. res = false;
  751. }
  752. if (kwsys::SystemTools::ConvertToUnixOutputPath
  753. ("//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  754. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo")
  755. {
  756. std::cerr
  757. << "Problem with ConvertToUnixOutputPath "
  758. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\""
  759. << std::endl;
  760. res = false;
  761. }
  762. return res;
  763. }
  764. //----------------------------------------------------------------------------
  765. static bool CheckPutEnv(const std::string& env, const char* name, const char* value)
  766. {
  767. if(!kwsys::SystemTools::PutEnv(env))
  768. {
  769. std::cerr << "PutEnv(\"" << env
  770. << "\") failed!" << std::endl;
  771. return false;
  772. }
  773. const char* v = kwsys::SystemTools::GetEnv(name);
  774. v = v? v : "(null)";
  775. if(strcmp(v, value) != 0)
  776. {
  777. std::cerr << "GetEnv(\"" << name << "\") returned \""
  778. << v << "\", not \"" << value << "\"!" << std::endl;
  779. return false;
  780. }
  781. return true;
  782. }
  783. static bool CheckUnPutEnv(const char* env, const char* name)
  784. {
  785. if(!kwsys::SystemTools::UnPutEnv(env))
  786. {
  787. std::cerr << "UnPutEnv(\"" << env << "\") failed!"
  788. << std::endl;
  789. return false;
  790. }
  791. if(const char* v = kwsys::SystemTools::GetEnv(name))
  792. {
  793. std::cerr << "GetEnv(\"" << name << "\") returned \""
  794. << v << "\", not (null)!" << std::endl;
  795. return false;
  796. }
  797. return true;
  798. }
  799. static bool CheckEnvironmentOperations()
  800. {
  801. bool res = true;
  802. res &= CheckPutEnv("A=B", "A", "B");
  803. res &= CheckPutEnv("B=C", "B", "C");
  804. res &= CheckPutEnv("C=D", "C", "D");
  805. res &= CheckPutEnv("D=E", "D", "E");
  806. res &= CheckUnPutEnv("A", "A");
  807. res &= CheckUnPutEnv("B=", "B");
  808. res &= CheckUnPutEnv("C=D", "C");
  809. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  810. return res;
  811. }
  812. static bool CheckRelativePath(
  813. const std::string& local,
  814. const std::string& remote,
  815. const std::string& expected)
  816. {
  817. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  818. if(expected != result)
  819. {
  820. std::cerr << "RelativePath(" << local << ", " << remote
  821. << ") yielded " << result << " instead of " << expected << std::endl;
  822. return false;
  823. }
  824. return true;
  825. }
  826. static bool CheckRelativePaths()
  827. {
  828. bool res = true;
  829. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  830. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  831. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  832. res &= CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  833. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  834. return res;
  835. }
  836. static bool CheckCollapsePath(
  837. const std::string& path,
  838. const std::string& expected)
  839. {
  840. std::string result = kwsys::SystemTools::CollapseFullPath(path);
  841. if(expected != result)
  842. {
  843. std::cerr << "CollapseFullPath(" << path
  844. << ") yielded " << result << " instead of " << expected << std::endl;
  845. return false;
  846. }
  847. return true;
  848. }
  849. static bool CheckCollapsePath()
  850. {
  851. bool res = true;
  852. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  853. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  854. return res;
  855. }
  856. static std::string StringVectorToString(const std::vector<std::string>& vec)
  857. {
  858. std::stringstream ss;
  859. ss << "vector(";
  860. for (std::vector<std::string>::const_iterator i = vec.begin();
  861. i != vec.end(); ++i)
  862. {
  863. if (i != vec.begin())
  864. {
  865. ss << ", ";
  866. }
  867. ss << *i;
  868. }
  869. ss << ")";
  870. return ss.str();
  871. }
  872. static bool CheckGetPath()
  873. {
  874. const char* envName = "S";
  875. #ifdef _WIN32
  876. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  877. #else
  878. const char* envValue = "/Somewhere/something:/tmp";
  879. #endif
  880. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  881. std::vector<std::string> originalPathes;
  882. originalPathes.push_back(registryPath);
  883. std::vector<std::string> expectedPathes;
  884. expectedPathes.push_back(registryPath);
  885. #ifdef _WIN32
  886. expectedPathes.push_back("C:/Somewhere/something");
  887. expectedPathes.push_back("D:/Temp");
  888. #else
  889. expectedPathes.push_back("/Somewhere/something");
  890. expectedPathes.push_back("/tmp");
  891. #endif
  892. bool res = true;
  893. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  894. std::vector<std::string> pathes = originalPathes;
  895. kwsys::SystemTools::GetPath(pathes, envName);
  896. if (pathes != expectedPathes)
  897. {
  898. std::cerr <<
  899. "GetPath(" << StringVectorToString(originalPathes) <<
  900. ", " << envName << ") yielded " << StringVectorToString(pathes) <<
  901. " instead of " << StringVectorToString(expectedPathes) <<
  902. std::endl;
  903. res = false;
  904. }
  905. res &= CheckUnPutEnv(envName, envName);
  906. return res;
  907. }
  908. static bool CheckFind()
  909. {
  910. bool res = true;
  911. const std::string testFindFileName("testFindFile.txt");
  912. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/"
  913. + testFindFileName);
  914. if (!kwsys::SystemTools::Touch(testFindFile.c_str(), true))
  915. {
  916. std::cerr
  917. << "Problem with Touch for: "
  918. << testFindFile << std::endl;
  919. // abort here as the existence of the file only makes the test meaningful
  920. return false;
  921. }
  922. std::vector<std::string> searchPaths;
  923. searchPaths.push_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  924. if (kwsys::SystemTools::FindFile(testFindFileName,
  925. searchPaths, true).empty())
  926. {
  927. std::cerr
  928. << "Problem with FindFile without system paths for: "
  929. << testFindFileName << std::endl;
  930. res = false;
  931. }
  932. if (kwsys::SystemTools::FindFile(testFindFileName,
  933. searchPaths, false).empty())
  934. {
  935. std::cerr
  936. << "Problem with FindFile with system paths for: "
  937. << testFindFileName << std::endl;
  938. res = false;
  939. }
  940. return res;
  941. }
  942. //----------------------------------------------------------------------------
  943. int testSystemTools(int, char*[])
  944. {
  945. bool res = true;
  946. int cc;
  947. for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
  948. {
  949. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  950. }
  951. // Special check for ~
  952. std::string output;
  953. if(kwsys::SystemTools::GetEnv("HOME", output))
  954. {
  955. output += "/foo bar/lala";
  956. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  957. }
  958. for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
  959. {
  960. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  961. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  962. }
  963. res &= CheckFileOperations();
  964. res &= CheckStringOperations();
  965. res &= CheckEnvironmentOperations();
  966. res &= CheckRelativePaths();
  967. res &= CheckCollapsePath();
  968. res &= CheckGetPath();
  969. res &= CheckFind();
  970. return res ? 0 : 1;
  971. }