testSystemTools.cxx 20 KB

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