testSystemTools.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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_stl::string const testFileMissing(testNewDir + "/testMissingFile.txt");
  141. if (!kwsys::SystemTools::RemoveFile(testFileMissing))
  142. {
  143. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  144. kwsys_ios::cerr <<
  145. "RemoveFile(\"" << testFileMissing << "\") failed: " << msg << "\n";
  146. res = false;
  147. }
  148. kwsys_stl::string const testFileMissingDir(testNewDir + "/missing/file.txt");
  149. if (!kwsys::SystemTools::RemoveFile(testFileMissingDir))
  150. {
  151. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  152. kwsys_ios::cerr <<
  153. "RemoveFile(\"" << testFileMissingDir << "\") failed: " << msg << "\n";
  154. res = false;
  155. }
  156. kwsys::SystemTools::Touch(testNewFile.c_str(), true);
  157. if (!kwsys::SystemTools::RemoveADirectory(testNewDir))
  158. {
  159. kwsys_ios::cerr
  160. << "Problem with RemoveADirectory for: "
  161. << testNewDir << kwsys_ios::endl;
  162. res = false;
  163. }
  164. #ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  165. // Perform the same file and directory creation and deletion tests but
  166. // with paths > 256 characters in length.
  167. const kwsys_stl::string testNewLongDir(
  168. TEST_SYSTEMTOOLS_BINARY_DIR "/"
  169. "012345678901234567890123456789012345678901234567890123456789"
  170. "012345678901234567890123456789012345678901234567890123456789"
  171. "012345678901234567890123456789012345678901234567890123456789"
  172. "012345678901234567890123456789012345678901234567890123456789"
  173. "01234567890123");
  174. const kwsys_stl::string testNewLongFile(testNewLongDir + "/"
  175. "012345678901234567890123456789012345678901234567890123456789"
  176. "012345678901234567890123456789012345678901234567890123456789"
  177. "012345678901234567890123456789012345678901234567890123456789"
  178. "012345678901234567890123456789012345678901234567890123456789"
  179. "0123456789.txt");
  180. if (!kwsys::SystemTools::MakeDirectory(testNewLongDir))
  181. {
  182. kwsys_ios::cerr
  183. << "Problem with MakeDirectory for: "
  184. << testNewLongDir << kwsys_ios::endl;
  185. res = false;
  186. }
  187. if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true))
  188. {
  189. kwsys_ios::cerr
  190. << "Problem with Touch for: "
  191. << testNewLongFile << kwsys_ios::endl;
  192. res = false;
  193. }
  194. if (!kwsys::SystemTools::RemoveFile(testNewLongFile))
  195. {
  196. kwsys_ios::cerr
  197. << "Problem with RemoveFile: "
  198. << testNewLongFile << kwsys_ios::endl;
  199. res = false;
  200. }
  201. kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
  202. if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir))
  203. {
  204. kwsys_ios::cerr
  205. << "Problem with RemoveADirectory for: "
  206. << testNewLongDir << kwsys_ios::endl;
  207. res = false;
  208. }
  209. #endif
  210. return res;
  211. }
  212. //----------------------------------------------------------------------------
  213. static bool CheckStringOperations()
  214. {
  215. bool res = true;
  216. kwsys_stl::string test = "mary had a little lamb.";
  217. if (kwsys::SystemTools::CapitalizedWords(test) != "Mary Had A Little Lamb.")
  218. {
  219. kwsys_ios::cerr
  220. << "Problem with CapitalizedWords "
  221. << '"' << test << '"' << kwsys_ios::endl;
  222. res = false;
  223. }
  224. test = "Mary Had A Little Lamb.";
  225. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  226. "mary had a little lamb.")
  227. {
  228. kwsys_ios::cerr
  229. << "Problem with UnCapitalizedWords "
  230. << '"' << test << '"' << kwsys_ios::endl;
  231. res = false;
  232. }
  233. test = "MaryHadTheLittleLamb.";
  234. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  235. "Mary Had The Little Lamb.")
  236. {
  237. kwsys_ios::cerr
  238. << "Problem with AddSpaceBetweenCapitalizedWords "
  239. << '"' << test << '"' << kwsys_ios::endl;
  240. res = false;
  241. }
  242. char * cres =
  243. kwsys::SystemTools::AppendStrings("Mary Had A"," Little Lamb.");
  244. if (strcmp(cres,"Mary Had A Little Lamb."))
  245. {
  246. kwsys_ios::cerr
  247. << "Problem with AppendStrings "
  248. << "\"Mary Had A\" \" Little Lamb.\"" << kwsys_ios::endl;
  249. res = false;
  250. }
  251. delete [] cres;
  252. cres =
  253. kwsys::SystemTools::AppendStrings("Mary Had"," A ","Little Lamb.");
  254. if (strcmp(cres,"Mary Had A Little Lamb."))
  255. {
  256. kwsys_ios::cerr
  257. << "Problem with AppendStrings "
  258. << "\"Mary Had\" \" A \" \"Little Lamb.\"" << kwsys_ios::endl;
  259. res = false;
  260. }
  261. delete [] cres;
  262. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.",'a') != 3)
  263. {
  264. kwsys_ios::cerr
  265. << "Problem with CountChar "
  266. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  267. res = false;
  268. }
  269. cres =
  270. kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.","aeiou");
  271. if (strcmp(cres,"Mry Hd A Lttl Lmb."))
  272. {
  273. kwsys_ios::cerr
  274. << "Problem with RemoveChars "
  275. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  276. res = false;
  277. }
  278. delete [] cres;
  279. cres =
  280. kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  281. if (strcmp(cres,"A"))
  282. {
  283. kwsys_ios::cerr
  284. << "Problem with RemoveCharsButUpperHex "
  285. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  286. res = false;
  287. }
  288. delete [] cres;
  289. char *cres2 = new char [strlen("Mary Had A Little Lamb.")+1];
  290. strcpy(cres2,"Mary Had A Little Lamb.");
  291. kwsys::SystemTools::ReplaceChars(cres2,"aeiou",'X');
  292. if (strcmp(cres2,"MXry HXd A LXttlX LXmb."))
  293. {
  294. kwsys_ios::cerr
  295. << "Problem with ReplaceChars "
  296. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  297. res = false;
  298. }
  299. delete [] cres2;
  300. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  301. "Mary "))
  302. {
  303. kwsys_ios::cerr
  304. << "Problem with StringStartsWith "
  305. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  306. res = false;
  307. }
  308. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  309. " Lamb."))
  310. {
  311. kwsys_ios::cerr
  312. << "Problem with StringEndsWith "
  313. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  314. res = false;
  315. }
  316. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  317. if (strcmp(cres,"Mary Had A Little Lamb."))
  318. {
  319. kwsys_ios::cerr
  320. << "Problem with DuplicateString "
  321. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  322. res = false;
  323. }
  324. delete [] cres;
  325. test = "Mary Had A Little Lamb.";
  326. if (kwsys::SystemTools::CropString(test,13) !=
  327. "Mary ...Lamb.")
  328. {
  329. kwsys_ios::cerr
  330. << "Problem with CropString "
  331. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  332. res = false;
  333. }
  334. kwsys_stl::vector<kwsys_stl::string> lines;
  335. kwsys::SystemTools::Split("Mary Had A Little Lamb.",lines,' ');
  336. if (lines[0] != "Mary" || lines[1] != "Had" ||
  337. lines[2] != "A" || lines[3] != "Little" || lines[4] != "Lamb.")
  338. {
  339. kwsys_ios::cerr
  340. << "Problem with Split "
  341. << "\"Mary Had A Little Lamb.\"" << kwsys_ios::endl;
  342. res = false;
  343. }
  344. #ifdef _WIN32
  345. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  346. ("L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  347. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  348. {
  349. kwsys_ios::cerr
  350. << "Problem with ConvertToWindowsExtendedPath "
  351. << "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  352. << kwsys_ios::endl;
  353. res = false;
  354. }
  355. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  356. ("L:/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  357. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  358. {
  359. kwsys_ios::cerr
  360. << "Problem with ConvertToWindowsExtendedPath "
  361. << "\"L:/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  362. << kwsys_ios::endl;
  363. res = false;
  364. }
  365. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  366. ("\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  367. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  368. {
  369. kwsys_ios::cerr
  370. << "Problem with ConvertToWindowsExtendedPath "
  371. << "\"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  372. << kwsys_ios::endl;
  373. res = false;
  374. }
  375. if (kwsys::SystemTools::ConvertToWindowsExtendedPath
  376. ("//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  377. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo")
  378. {
  379. kwsys_ios::cerr
  380. << "Problem with ConvertToWindowsExtendedPath "
  381. << "\"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  382. << kwsys_ios::endl;
  383. res = false;
  384. }
  385. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("//") !=
  386. L"//")
  387. {
  388. kwsys_ios::cerr
  389. << "Problem with ConvertToWindowsExtendedPath "
  390. << "\"//\""
  391. << kwsys_ios::endl;
  392. res = false;
  393. }
  394. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\") !=
  395. L"\\\\.\\")
  396. {
  397. kwsys_ios::cerr
  398. << "Problem with ConvertToWindowsExtendedPath "
  399. << "\"\\\\.\\\""
  400. << kwsys_ios::endl;
  401. res = false;
  402. }
  403. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X") !=
  404. L"\\\\.\\X")
  405. {
  406. kwsys_ios::cerr
  407. << "Problem with ConvertToWindowsExtendedPath "
  408. << "\"\\\\.\\X\""
  409. << kwsys_ios::endl;
  410. res = false;
  411. }
  412. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:") !=
  413. L"\\\\?\\X:")
  414. {
  415. kwsys_ios::cerr
  416. << "Problem with ConvertToWindowsExtendedPath "
  417. << "\"\\\\.\\X:\""
  418. << kwsys_ios::endl;
  419. res = false;
  420. }
  421. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:\\") !=
  422. L"\\\\?\\X:\\")
  423. {
  424. kwsys_ios::cerr
  425. << "Problem with ConvertToWindowsExtendedPath "
  426. << "\"\\\\.\\X:\\\""
  427. << kwsys_ios::endl;
  428. res = false;
  429. }
  430. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("NUL") !=
  431. L"\\\\.\\NUL")
  432. {
  433. kwsys_ios::cerr
  434. << "Problem with ConvertToWindowsExtendedPath "
  435. << "\"NUL\""
  436. << kwsys_ios::endl;
  437. res = false;
  438. }
  439. #endif
  440. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  441. ("L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  442. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  443. {
  444. kwsys_ios::cerr
  445. << "Problem with ConvertToWindowsOutputPath "
  446. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\""
  447. << kwsys_ios::endl;
  448. res = false;
  449. }
  450. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  451. ("//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  452. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  453. {
  454. kwsys_ios::cerr
  455. << "Problem with ConvertToWindowsOutputPath "
  456. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  457. << kwsys_ios::endl;
  458. res = false;
  459. }
  460. if (kwsys::SystemTools::ConvertToUnixOutputPath
  461. ("//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  462. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo")
  463. {
  464. kwsys_ios::cerr
  465. << "Problem with ConvertToUnixOutputPath "
  466. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\""
  467. << kwsys_ios::endl;
  468. res = false;
  469. }
  470. return res;
  471. }
  472. //----------------------------------------------------------------------------
  473. static bool CheckPutEnv(const kwsys_stl::string& env, const char* name, const char* value)
  474. {
  475. if(!kwsys::SystemTools::PutEnv(env))
  476. {
  477. kwsys_ios::cerr << "PutEnv(\"" << env
  478. << "\") failed!" << kwsys_ios::endl;
  479. return false;
  480. }
  481. const char* v = kwsys::SystemTools::GetEnv(name);
  482. v = v? v : "(null)";
  483. if(strcmp(v, value) != 0)
  484. {
  485. kwsys_ios::cerr << "GetEnv(\"" << name << "\") returned \""
  486. << v << "\", not \"" << value << "\"!" << kwsys_ios::endl;
  487. return false;
  488. }
  489. return true;
  490. }
  491. static bool CheckUnPutEnv(const char* env, const char* name)
  492. {
  493. if(!kwsys::SystemTools::UnPutEnv(env))
  494. {
  495. kwsys_ios::cerr << "UnPutEnv(\"" << env << "\") failed!"
  496. << kwsys_ios::endl;
  497. return false;
  498. }
  499. if(const char* v = kwsys::SystemTools::GetEnv(name))
  500. {
  501. kwsys_ios::cerr << "GetEnv(\"" << name << "\") returned \""
  502. << v << "\", not (null)!" << kwsys_ios::endl;
  503. return false;
  504. }
  505. return true;
  506. }
  507. static bool CheckEnvironmentOperations()
  508. {
  509. bool res = true;
  510. res &= CheckPutEnv("A=B", "A", "B");
  511. res &= CheckPutEnv("B=C", "B", "C");
  512. res &= CheckPutEnv("C=D", "C", "D");
  513. res &= CheckPutEnv("D=E", "D", "E");
  514. res &= CheckUnPutEnv("A", "A");
  515. res &= CheckUnPutEnv("B=", "B");
  516. res &= CheckUnPutEnv("C=D", "C");
  517. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  518. return res;
  519. }
  520. static bool CheckRelativePath(
  521. const kwsys_stl::string& local,
  522. const kwsys_stl::string& remote,
  523. const kwsys_stl::string& expected)
  524. {
  525. kwsys_stl::string result = kwsys::SystemTools::RelativePath(local, remote);
  526. if(expected != result)
  527. {
  528. kwsys_ios::cerr << "RelativePath(" << local << ", " << remote
  529. << ") yielded " << result << " instead of " << expected << kwsys_ios::endl;
  530. return false;
  531. }
  532. return true;
  533. }
  534. static bool CheckRelativePaths()
  535. {
  536. bool res = true;
  537. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  538. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  539. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  540. res &= CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  541. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  542. return res;
  543. }
  544. static bool CheckCollapsePath(
  545. const kwsys_stl::string& path,
  546. const kwsys_stl::string& expected)
  547. {
  548. kwsys_stl::string result = kwsys::SystemTools::CollapseFullPath(path);
  549. if(expected != result)
  550. {
  551. kwsys_ios::cerr << "CollapseFullPath(" << path
  552. << ") yielded " << result << " instead of " << expected << kwsys_ios::endl;
  553. return false;
  554. }
  555. return true;
  556. }
  557. static bool CheckCollapsePath()
  558. {
  559. bool res = true;
  560. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  561. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  562. return res;
  563. }
  564. //----------------------------------------------------------------------------
  565. int testSystemTools(int, char*[])
  566. {
  567. bool res = true;
  568. int cc;
  569. for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
  570. {
  571. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  572. }
  573. // Special check for ~
  574. kwsys_stl::string output;
  575. if(kwsys::SystemTools::GetEnv("HOME", output))
  576. {
  577. output += "/foo bar/lala";
  578. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  579. }
  580. for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
  581. {
  582. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  583. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  584. }
  585. res &= CheckFileOperations();
  586. res &= CheckStringOperations();
  587. res &= CheckEnvironmentOperations();
  588. res &= CheckRelativePaths();
  589. res &= CheckCollapsePath();
  590. return res ? 0 : 1;
  591. }