1
0

testSystemTools.cxx 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. std::string v = "(null)";
  774. kwsys::SystemTools::GetEnv(name, v);
  775. if(v != value)
  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. std::string v;
  792. if(kwsys::SystemTools::GetEnv(name, v))
  793. {
  794. std::cerr << "GetEnv(\"" << name << "\") returned \""
  795. << v << "\", not (null)!" << std::endl;
  796. return false;
  797. }
  798. return true;
  799. }
  800. static bool CheckEnvironmentOperations()
  801. {
  802. bool res = true;
  803. res &= CheckPutEnv("A=B", "A", "B");
  804. res &= CheckPutEnv("B=C", "B", "C");
  805. res &= CheckPutEnv("C=D", "C", "D");
  806. res &= CheckPutEnv("D=E", "D", "E");
  807. res &= CheckUnPutEnv("A", "A");
  808. res &= CheckUnPutEnv("B=", "B");
  809. res &= CheckUnPutEnv("C=D", "C");
  810. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  811. return res;
  812. }
  813. static bool CheckRelativePath(
  814. const std::string& local,
  815. const std::string& remote,
  816. const std::string& expected)
  817. {
  818. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  819. if(expected != result)
  820. {
  821. std::cerr << "RelativePath(" << local << ", " << remote
  822. << ") yielded " << result << " instead of " << expected << std::endl;
  823. return false;
  824. }
  825. return true;
  826. }
  827. static bool CheckRelativePaths()
  828. {
  829. bool res = true;
  830. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  831. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  832. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  833. res &= CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  834. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  835. return res;
  836. }
  837. static bool CheckCollapsePath(
  838. const std::string& path,
  839. const std::string& expected)
  840. {
  841. std::string result = kwsys::SystemTools::CollapseFullPath(path);
  842. if(expected != result)
  843. {
  844. std::cerr << "CollapseFullPath(" << path
  845. << ") yielded " << result << " instead of " << expected << std::endl;
  846. return false;
  847. }
  848. return true;
  849. }
  850. static bool CheckCollapsePath()
  851. {
  852. bool res = true;
  853. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  854. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  855. return res;
  856. }
  857. static std::string StringVectorToString(const std::vector<std::string>& vec)
  858. {
  859. std::stringstream ss;
  860. ss << "vector(";
  861. for (std::vector<std::string>::const_iterator i = vec.begin();
  862. i != vec.end(); ++i)
  863. {
  864. if (i != vec.begin())
  865. {
  866. ss << ", ";
  867. }
  868. ss << *i;
  869. }
  870. ss << ")";
  871. return ss.str();
  872. }
  873. static bool CheckGetPath()
  874. {
  875. const char* envName = "S";
  876. #ifdef _WIN32
  877. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  878. #else
  879. const char* envValue = "/Somewhere/something:/tmp";
  880. #endif
  881. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  882. std::vector<std::string> originalPathes;
  883. originalPathes.push_back(registryPath);
  884. std::vector<std::string> expectedPathes;
  885. expectedPathes.push_back(registryPath);
  886. #ifdef _WIN32
  887. expectedPathes.push_back("C:/Somewhere/something");
  888. expectedPathes.push_back("D:/Temp");
  889. #else
  890. expectedPathes.push_back("/Somewhere/something");
  891. expectedPathes.push_back("/tmp");
  892. #endif
  893. bool res = true;
  894. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  895. std::vector<std::string> pathes = originalPathes;
  896. kwsys::SystemTools::GetPath(pathes, envName);
  897. if (pathes != expectedPathes)
  898. {
  899. std::cerr <<
  900. "GetPath(" << StringVectorToString(originalPathes) <<
  901. ", " << envName << ") yielded " << StringVectorToString(pathes) <<
  902. " instead of " << StringVectorToString(expectedPathes) <<
  903. std::endl;
  904. res = false;
  905. }
  906. res &= CheckUnPutEnv(envName, envName);
  907. return res;
  908. }
  909. static bool CheckFind()
  910. {
  911. bool res = true;
  912. const std::string testFindFileName("testFindFile.txt");
  913. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/"
  914. + testFindFileName);
  915. if (!kwsys::SystemTools::Touch(testFindFile.c_str(), true))
  916. {
  917. std::cerr
  918. << "Problem with Touch for: "
  919. << testFindFile << std::endl;
  920. // abort here as the existence of the file only makes the test meaningful
  921. return false;
  922. }
  923. std::vector<std::string> searchPaths;
  924. searchPaths.push_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  925. if (kwsys::SystemTools::FindFile(testFindFileName,
  926. searchPaths, true).empty())
  927. {
  928. std::cerr
  929. << "Problem with FindFile without system paths for: "
  930. << testFindFileName << std::endl;
  931. res = false;
  932. }
  933. if (kwsys::SystemTools::FindFile(testFindFileName,
  934. searchPaths, false).empty())
  935. {
  936. std::cerr
  937. << "Problem with FindFile with system paths for: "
  938. << testFindFileName << std::endl;
  939. res = false;
  940. }
  941. return res;
  942. }
  943. //----------------------------------------------------------------------------
  944. int testSystemTools(int, char*[])
  945. {
  946. bool res = true;
  947. int cc;
  948. for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
  949. {
  950. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  951. }
  952. // Special check for ~
  953. std::string output;
  954. if(kwsys::SystemTools::GetEnv("HOME", output))
  955. {
  956. output += "/foo bar/lala";
  957. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  958. }
  959. for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
  960. {
  961. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  962. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  963. }
  964. res &= CheckFileOperations();
  965. res &= CheckStringOperations();
  966. res &= CheckEnvironmentOperations();
  967. res &= CheckRelativePaths();
  968. res &= CheckCollapsePath();
  969. res &= CheckGetPath();
  970. res &= CheckFind();
  971. return res ? 0 : 1;
  972. }