testSystemTools.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. #include KWSYS_HEADER(ios/iostream)
  16. // Work-around CMake dependency scanning limitation. This must
  17. // duplicate the above list of headers.
  18. #if 0
  19. # include "SystemTools.hxx.in"
  20. # include "kwsys_ios_iostream.h.in"
  21. #endif
  22. // Include with <> instead of "" to avoid getting any in-source copy
  23. // left on disk.
  24. #include <testSystemTools.h>
  25. #include <string.h> /* strcmp */
  26. //----------------------------------------------------------------------------
  27. static const char* toUnixPaths[][2] =
  28. {
  29. { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
  30. { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
  31. { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  32. { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
  33. { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  34. { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  35. { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
  36. { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
  37. { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  38. { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
  39. { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  40. { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  41. { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
  42. { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
  43. { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo\\ cal/bin/pa\\ sswd" },
  44. {0, 0}
  45. };
  46. static bool CheckConvertToUnixSlashes(kwsys_stl::string input,
  47. kwsys_stl::string output)
  48. {
  49. kwsys_stl::string result = input;
  50. kwsys::SystemTools::ConvertToUnixSlashes(result);
  51. if ( result != output )
  52. {
  53. kwsys_ios::cerr
  54. << "Problem with ConvertToUnixSlashes - input: " << input
  55. << " output: " << result << " expected: " << output
  56. << kwsys_ios::endl;
  57. return false;
  58. }
  59. return true;
  60. }
  61. //----------------------------------------------------------------------------
  62. static const char* checkEscapeChars[][4] =
  63. {
  64. { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2"},
  65. { " {} ", "{}", "#", " #{#} "},
  66. {0, 0, 0, 0}
  67. };
  68. static bool CheckEscapeChars(kwsys_stl::string input,
  69. const char *chars_to_escape,
  70. char escape_char,
  71. kwsys_stl::string output)
  72. {
  73. kwsys_stl::string result = kwsys::SystemTools::EscapeChars(
  74. input.c_str(), chars_to_escape, escape_char);
  75. if (result != output)
  76. {
  77. kwsys_ios::cerr
  78. << "Problem with CheckEscapeChars - input: " << input
  79. << " output: " << result << " expected: " << output
  80. << kwsys_ios::endl;
  81. return false;
  82. }
  83. return true;
  84. }
  85. //----------------------------------------------------------------------------
  86. static bool CheckFileOperations()
  87. {
  88. bool res = true;
  89. const kwsys_stl::string testBinFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  90. "/testSystemTools.bin");
  91. const kwsys_stl::string testTxtFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  92. "/testSystemTools.cxx");
  93. const kwsys_stl::string testNewDir(TEST_SYSTEMTOOLS_BINARY_DIR
  94. "/testSystemToolsNewDir");
  95. const kwsys_stl::string testNewFile(testNewDir + "/testNewFile.txt");
  96. if (kwsys::SystemTools::DetectFileType(testBinFile.c_str()) !=
  97. kwsys::SystemTools::FileTypeBinary)
  98. {
  99. kwsys_ios::cerr
  100. << "Problem with DetectFileType - failed to detect type of: "
  101. << testBinFile << kwsys_ios::endl;
  102. res = false;
  103. }
  104. if (kwsys::SystemTools::DetectFileType(testTxtFile.c_str()) !=
  105. kwsys::SystemTools::FileTypeText)
  106. {
  107. kwsys_ios::cerr
  108. << "Problem with DetectFileType - failed to detect type of: "
  109. << testTxtFile << kwsys_ios::endl;
  110. res = false;
  111. }
  112. if (kwsys::SystemTools::FileLength(testBinFile) != 766)
  113. {
  114. kwsys_ios::cerr
  115. << "Problem with FileLength - incorrect length for: "
  116. << testBinFile << kwsys_ios::endl;
  117. res = false;
  118. }
  119. if (!kwsys::SystemTools::MakeDirectory(testNewDir))
  120. {
  121. kwsys_ios::cerr
  122. << "Problem with MakeDirectory for: "
  123. << testNewDir << kwsys_ios::endl;
  124. res = false;
  125. }
  126. if (!kwsys::SystemTools::Touch(testNewFile.c_str(), true))
  127. {
  128. kwsys_ios::cerr
  129. << "Problem with Touch for: "
  130. << testNewFile << kwsys_ios::endl;
  131. res = false;
  132. }
  133. if (!kwsys::SystemTools::RemoveFile(testNewFile))
  134. {
  135. kwsys_ios::cerr
  136. << "Problem with RemoveFile: "
  137. << testNewFile << kwsys_ios::endl;
  138. res = false;
  139. }
  140. kwsys::SystemTools::Touch(testNewFile.c_str(), true);
  141. if (!kwsys::SystemTools::RemoveADirectory(testNewDir))
  142. {
  143. kwsys_ios::cerr
  144. << "Problem with RemoveADirectory for: "
  145. << testNewDir << kwsys_ios::endl;
  146. res = false;
  147. }
  148. #ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  149. // Perform the same file and directory creation and deletion tests but
  150. // with paths > 256 characters in length.
  151. const kwsys_stl::string testNewLongDir(
  152. TEST_SYSTEMTOOLS_BINARY_DIR "/"
  153. "012345678901234567890123456789012345678901234567890123456789"
  154. "012345678901234567890123456789012345678901234567890123456789"
  155. "012345678901234567890123456789012345678901234567890123456789"
  156. "012345678901234567890123456789012345678901234567890123456789"
  157. "01234567890123");
  158. const kwsys_stl::string testNewLongFile(testNewLongDir + "/"
  159. "012345678901234567890123456789012345678901234567890123456789"
  160. "012345678901234567890123456789012345678901234567890123456789"
  161. "012345678901234567890123456789012345678901234567890123456789"
  162. "012345678901234567890123456789012345678901234567890123456789"
  163. "0123456789.txt");
  164. if (!kwsys::SystemTools::MakeDirectory(testNewLongDir))
  165. {
  166. kwsys_ios::cerr
  167. << "Problem with MakeDirectory for: "
  168. << testNewLongDir << kwsys_ios::endl;
  169. res = false;
  170. }
  171. if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true))
  172. {
  173. kwsys_ios::cerr
  174. << "Problem with Touch for: "
  175. << testNewLongFile << kwsys_ios::endl;
  176. res = false;
  177. }
  178. if (!kwsys::SystemTools::RemoveFile(testNewLongFile))
  179. {
  180. kwsys_ios::cerr
  181. << "Problem with RemoveFile: "
  182. << testNewLongFile << kwsys_ios::endl;
  183. res = false;
  184. }
  185. kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
  186. if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir))
  187. {
  188. kwsys_ios::cerr
  189. << "Problem with RemoveADirectory for: "
  190. << testNewLongDir << kwsys_ios::endl;
  191. res = false;
  192. }
  193. #endif
  194. return res;
  195. }
  196. //----------------------------------------------------------------------------
  197. static bool CheckStringOperations()
  198. {
  199. bool res = true;
  200. kwsys_stl::string test = "mary had a little lamb.";
  201. if (kwsys::SystemTools::CapitalizedWords(test) != "Mary Had A Little Lamb.")
  202. {
  203. kwsys_ios::cerr
  204. << "Problem with CapitalizedWords "
  205. << '"' << test << '"' << kwsys_ios::endl;
  206. res = false;
  207. }
  208. test = "Mary Had A Little Lamb.";
  209. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  210. "mary had a little lamb.")
  211. {
  212. kwsys_ios::cerr
  213. << "Problem with UnCapitalizedWords "
  214. << '"' << test << '"' << kwsys_ios::endl;
  215. res = false;
  216. }
  217. test = "MaryHadTheLittleLamb.";
  218. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  219. "Mary Had The Little Lamb.")
  220. {
  221. kwsys_ios::cerr
  222. << "Problem with AddSpaceBetweenCapitalizedWords "
  223. << '"' << test << '"' << kwsys_ios::endl;
  224. res = false;
  225. }
  226. char * cres =
  227. kwsys::SystemTools::AppendStrings("Mary Had A"," Little Lamb.");
  228. if (strcmp(cres,"Mary Had A Little Lamb."))
  229. {
  230. kwsys_ios::cerr
  231. << "Problem with AppendStrings "
  232. << "\"Mary Had A\" \" Little Lamb.\"" << kwsys_ios::endl;
  233. res = false;
  234. }
  235. delete [] cres;
  236. cres =
  237. kwsys::SystemTools::AppendStrings("Mary Had"," A ","Little Lamb.");
  238. if (strcmp(cres,"Mary Had A Little Lamb."))
  239. {
  240. kwsys_ios::cerr
  241. << "Problem with AppendStrings "
  242. << "\"Mary Had\" \" A \" \"Little Lamb.\"" << kwsys_ios::endl;
  243. res = false;
  244. }
  245. delete [] cres;
  246. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.",'a') != 3)
  247. {
  248. kwsys_ios::cerr
  249. << "Problem with CountChar "
  250. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  251. res = false;
  252. }
  253. cres =
  254. kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.","aeiou");
  255. if (strcmp(cres,"Mry Hd A Lttl Lmb."))
  256. {
  257. kwsys_ios::cerr
  258. << "Problem with RemoveChars "
  259. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  260. res = false;
  261. }
  262. delete [] cres;
  263. cres =
  264. kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  265. if (strcmp(cres,"A"))
  266. {
  267. kwsys_ios::cerr
  268. << "Problem with RemoveCharsButUpperHex "
  269. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  270. res = false;
  271. }
  272. delete [] cres;
  273. char *cres2 = new char [strlen("Mary Had A Little Lamb.")+1];
  274. strcpy(cres2,"Mary Had A Little Lamb.");
  275. kwsys::SystemTools::ReplaceChars(cres2,"aeiou",'X');
  276. if (strcmp(cres2,"MXry HXd A LXttlX LXmb."))
  277. {
  278. kwsys_ios::cerr
  279. << "Problem with ReplaceChars "
  280. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  281. res = false;
  282. }
  283. delete [] cres2;
  284. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  285. "Mary "))
  286. {
  287. kwsys_ios::cerr
  288. << "Problem with StringStartsWith "
  289. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  290. res = false;
  291. }
  292. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  293. " Lamb."))
  294. {
  295. kwsys_ios::cerr
  296. << "Problem with StringEndsWith "
  297. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  298. res = false;
  299. }
  300. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  301. if (strcmp(cres,"Mary Had A Little Lamb."))
  302. {
  303. kwsys_ios::cerr
  304. << "Problem with DuplicateString "
  305. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  306. res = false;
  307. }
  308. delete [] cres;
  309. test = "Mary Had A Little Lamb.";
  310. if (kwsys::SystemTools::CropString(test,13) !=
  311. "Mary ...Lamb.")
  312. {
  313. kwsys_ios::cerr
  314. << "Problem with CropString "
  315. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  316. res = false;
  317. }
  318. kwsys_stl::vector<kwsys_stl::string> lines;
  319. kwsys::SystemTools::Split("Mary Had A Little Lamb.",lines,' ');
  320. if (lines[0] != "Mary" || lines[1] != "Had" ||
  321. lines[2] != "A" || lines[3] != "Little" || lines[4] != "Lamb.")
  322. {
  323. kwsys_ios::cerr
  324. << "Problem with Split "
  325. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  326. res = false;
  327. }
  328. #ifdef _WIN32
  329. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  330. ("L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  331. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  332. {
  333. kwsys_ios::cerr
  334. << "Problem with ConvertToWindowsExtendedPath "
  335. << "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  336. << kwsys_ios::endl;
  337. res = false;
  338. }
  339. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  340. ("L:/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  341. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  342. {
  343. kwsys_ios::cerr
  344. << "Problem with ConvertToWindowsExtendedPath "
  345. << "\"L:/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  346. << kwsys_ios::endl;
  347. res = false;
  348. }
  349. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  350. ("\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  351. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  352. {
  353. kwsys_ios::cerr
  354. << "Problem with ConvertToWindowsExtendedPath "
  355. << "\"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  356. << kwsys_ios::endl;
  357. res = false;
  358. }
  359. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  360. ("//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  361. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  362. {
  363. kwsys_ios::cerr
  364. << "Problem with ConvertToWindowsExtendedPath "
  365. << "\"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  366. << kwsys_ios::endl;
  367. res = false;
  368. }
  369. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("//") !=
  370. L"//")
  371. {
  372. kwsys_ios::cerr
  373. << "Problem with ConvertToWindowsExtendedPath "
  374. << "\"//\""
  375. << kwsys_ios::endl;
  376. res = false;
  377. }
  378. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\") !=
  379. L"\\\\.\\")
  380. {
  381. kwsys_ios::cerr
  382. << "Problem with ConvertToWindowsExtendedPath "
  383. << "\"\\\\.\\\""
  384. << kwsys_ios::endl;
  385. res = false;
  386. }
  387. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X") !=
  388. L"\\\\.\\X")
  389. {
  390. kwsys_ios::cerr
  391. << "Problem with ConvertToWindowsExtendedPath "
  392. << "\"\\\\.\\X\""
  393. << kwsys_ios::endl;
  394. res = false;
  395. }
  396. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:") !=
  397. L"\\\\?\\X:")
  398. {
  399. kwsys_ios::cerr
  400. << "Problem with ConvertToWindowsExtendedPath "
  401. << "\"\\\\.\\X:\""
  402. << kwsys_ios::endl;
  403. res = false;
  404. }
  405. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:\\") !=
  406. L"\\\\?\\X:\\")
  407. {
  408. kwsys_ios::cerr
  409. << "Problem with ConvertToWindowsExtendedPath "
  410. << "\"\\\\.\\X:\\\""
  411. << kwsys_ios::endl;
  412. res = false;
  413. }
  414. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("NUL") !=
  415. L"\\\\.\\NUL")
  416. {
  417. kwsys_ios::cerr
  418. << "Problem with ConvertToWindowsExtendedPath "
  419. << "\"NUL\""
  420. << kwsys_ios::endl;
  421. res = false;
  422. }
  423. #endif
  424. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  425. ("L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  426. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  427. {
  428. kwsys_ios::cerr
  429. << "Problem with ConvertToWindowsOutputPath "
  430. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\""
  431. << kwsys_ios::endl;
  432. res = false;
  433. }
  434. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  435. ("//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  436. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  437. {
  438. kwsys_ios::cerr
  439. << "Problem with ConvertToWindowsOutputPath "
  440. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  441. << kwsys_ios::endl;
  442. res = false;
  443. }
  444. if (kwsys::SystemTools::ConvertToUnixOutputPath
  445. ("//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  446. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo")
  447. {
  448. kwsys_ios::cerr
  449. << "Problem with ConvertToUnixOutputPath "
  450. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\""
  451. << kwsys_ios::endl;
  452. res = false;
  453. }
  454. return res;
  455. }
  456. //----------------------------------------------------------------------------
  457. static bool CheckPutEnv(const kwsys_stl::string& env, const char* name, const char* value)
  458. {
  459. if(!kwsys::SystemTools::PutEnv(env))
  460. {
  461. kwsys_ios::cerr << "PutEnv(\"" << env
  462. << "\") failed!" << kwsys_ios::endl;
  463. return false;
  464. }
  465. const char* v = kwsys::SystemTools::GetEnv(name);
  466. v = v? v : "(null)";
  467. if(strcmp(v, value) != 0)
  468. {
  469. kwsys_ios::cerr << "GetEnv(\"" << name << "\") returned \""
  470. << v << "\", not \"" << value << "\"!" << kwsys_ios::endl;
  471. return false;
  472. }
  473. return true;
  474. }
  475. static bool CheckUnPutEnv(const char* env, const char* name)
  476. {
  477. if(!kwsys::SystemTools::UnPutEnv(env))
  478. {
  479. kwsys_ios::cerr << "UnPutEnv(\"" << env << "\") failed!"
  480. << kwsys_ios::endl;
  481. return false;
  482. }
  483. if(const char* v = kwsys::SystemTools::GetEnv(name))
  484. {
  485. kwsys_ios::cerr << "GetEnv(\"" << name << "\") returned \""
  486. << v << "\", not (null)!" << kwsys_ios::endl;
  487. return false;
  488. }
  489. return true;
  490. }
  491. static bool CheckEnvironmentOperations()
  492. {
  493. bool res = true;
  494. res &= CheckPutEnv("A=B", "A", "B");
  495. res &= CheckPutEnv("B=C", "B", "C");
  496. res &= CheckPutEnv("C=D", "C", "D");
  497. res &= CheckPutEnv("D=E", "D", "E");
  498. res &= CheckUnPutEnv("A", "A");
  499. res &= CheckUnPutEnv("B=", "B");
  500. res &= CheckUnPutEnv("C=D", "C");
  501. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  502. return res;
  503. }
  504. static bool CheckRelativePath(
  505. const kwsys_stl::string& local,
  506. const kwsys_stl::string& remote,
  507. const kwsys_stl::string& expected)
  508. {
  509. kwsys_stl::string result = kwsys::SystemTools::RelativePath(local, remote);
  510. if(expected != result)
  511. {
  512. kwsys_ios::cerr << "RelativePath(" << local << ", " << remote
  513. << ") yielded " << result << " instead of " << expected << kwsys_ios::endl;
  514. return false;
  515. }
  516. return true;
  517. }
  518. static bool CheckRelativePaths()
  519. {
  520. bool res = true;
  521. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  522. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  523. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  524. res &= CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  525. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  526. return res;
  527. }
  528. static bool CheckCollapsePath(
  529. const kwsys_stl::string& path,
  530. const kwsys_stl::string& expected)
  531. {
  532. kwsys_stl::string result = kwsys::SystemTools::CollapseFullPath(path);
  533. if(expected != result)
  534. {
  535. kwsys_ios::cerr << "CollapseFullPath(" << path
  536. << ") yielded " << result << " instead of " << expected << kwsys_ios::endl;
  537. return false;
  538. }
  539. return true;
  540. }
  541. static bool CheckCollapsePath()
  542. {
  543. bool res = true;
  544. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  545. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  546. return res;
  547. }
  548. //----------------------------------------------------------------------------
  549. int testSystemTools(int, char*[])
  550. {
  551. bool res = true;
  552. int cc;
  553. for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
  554. {
  555. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  556. }
  557. // Special check for ~
  558. kwsys_stl::string output;
  559. if(kwsys::SystemTools::GetEnv("HOME", output))
  560. {
  561. output += "/foo bar/lala";
  562. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  563. }
  564. for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
  565. {
  566. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  567. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  568. }
  569. res &= CheckFileOperations();
  570. res &= CheckStringOperations();
  571. res &= CheckEnvironmentOperations();
  572. res &= CheckRelativePaths();
  573. res &= CheckCollapsePath();
  574. return res ? 0 : 1;
  575. }