testSystemTools.cxx 31 KB

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