testSystemTools.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #include "kwsysPrivate.h"
  4. #if defined(_MSC_VER)
  5. #pragma warning(disable : 4786)
  6. #endif
  7. #include KWSYS_HEADER(SystemTools.hxx)
  8. // Work-around CMake dependency scanning limitation. This must
  9. // duplicate the above list of headers.
  10. #if 0
  11. #include "SystemTools.hxx.in"
  12. #endif
  13. // Include with <> instead of "" to avoid getting any in-source copy
  14. // left on disk.
  15. #include <testSystemTools.h>
  16. #include <iostream>
  17. #include <sstream>
  18. #include <string.h> /* strcmp */
  19. #if defined(_WIN32) && !defined(__CYGWIN__)
  20. #include <io.h> /* _umask (MSVC) / umask (Borland) */
  21. #ifdef _MSC_VER
  22. #define umask _umask // Note this is still umask on Borland
  23. #endif
  24. #endif
  25. #include <sys/stat.h> /* umask (POSIX), _S_I* constants (Windows) */
  26. // Visual C++ does not define mode_t (note that Borland does, however).
  27. #if defined(_MSC_VER)
  28. typedef unsigned short mode_t;
  29. #endif
  30. //----------------------------------------------------------------------------
  31. static const char* toUnixPaths[][2] = {
  32. { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
  33. { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
  34. { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  35. { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
  36. { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  37. { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  38. { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
  39. { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
  40. { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  41. { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
  42. { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  43. { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  44. { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
  45. { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
  46. { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo\\ cal/bin/pa\\ sswd" },
  47. { 0, 0 }
  48. };
  49. static bool CheckConvertToUnixSlashes(std::string input, std::string output)
  50. {
  51. std::string result = input;
  52. kwsys::SystemTools::ConvertToUnixSlashes(result);
  53. if (result != output) {
  54. std::cerr << "Problem with ConvertToUnixSlashes - input: " << input
  55. << " output: " << result << " expected: " << output << std::endl;
  56. return false;
  57. }
  58. return true;
  59. }
  60. //----------------------------------------------------------------------------
  61. static const char* checkEscapeChars[][4] = { { "1 foo 2 bar 2", "12", "\\",
  62. "\\1 foo \\2 bar \\2" },
  63. { " {} ", "{}", "#", " #{#} " },
  64. { 0, 0, 0, 0 } };
  65. static bool CheckEscapeChars(std::string input, const char* chars_to_escape,
  66. char escape_char, std::string output)
  67. {
  68. std::string result = kwsys::SystemTools::EscapeChars(
  69. input.c_str(), chars_to_escape, escape_char);
  70. if (result != output) {
  71. std::cerr << "Problem with CheckEscapeChars - input: " << input
  72. << " output: " << result << " expected: " << output << std::endl;
  73. return false;
  74. }
  75. return true;
  76. }
  77. //----------------------------------------------------------------------------
  78. static bool CheckFileOperations()
  79. {
  80. bool res = true;
  81. const std::string testNonExistingFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  82. "/testSystemToolsNonExistingFile");
  83. const std::string testDotFile(TEST_SYSTEMTOOLS_SOURCE_DIR "/.");
  84. const std::string testBinFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  85. "/testSystemTools.bin");
  86. const std::string testTxtFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  87. "/testSystemTools.cxx");
  88. const std::string testNewDir(TEST_SYSTEMTOOLS_BINARY_DIR
  89. "/testSystemToolsNewDir");
  90. const std::string testNewFile(testNewDir + "/testNewFile.txt");
  91. if (kwsys::SystemTools::DetectFileType(testNonExistingFile.c_str()) !=
  92. kwsys::SystemTools::FileTypeUnknown) {
  93. std::cerr << "Problem with DetectFileType - failed to detect type of: "
  94. << testNonExistingFile << std::endl;
  95. res = false;
  96. }
  97. if (kwsys::SystemTools::DetectFileType(testDotFile.c_str()) !=
  98. kwsys::SystemTools::FileTypeUnknown) {
  99. std::cerr << "Problem with DetectFileType - failed to detect type of: "
  100. << testDotFile << std::endl;
  101. res = false;
  102. }
  103. if (kwsys::SystemTools::DetectFileType(testBinFile.c_str()) !=
  104. kwsys::SystemTools::FileTypeBinary) {
  105. std::cerr << "Problem with DetectFileType - failed to detect type of: "
  106. << testBinFile << std::endl;
  107. res = false;
  108. }
  109. if (kwsys::SystemTools::DetectFileType(testTxtFile.c_str()) !=
  110. kwsys::SystemTools::FileTypeText) {
  111. std::cerr << "Problem with DetectFileType - failed to detect type of: "
  112. << testTxtFile << std::endl;
  113. res = false;
  114. }
  115. if (kwsys::SystemTools::FileLength(testBinFile) != 766) {
  116. std::cerr << "Problem with FileLength - incorrect length for: "
  117. << testBinFile << std::endl;
  118. res = false;
  119. }
  120. if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
  121. std::cerr << "Problem with MakeDirectory for: " << testNewDir << std::endl;
  122. res = false;
  123. }
  124. // calling it again should just return true
  125. if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
  126. std::cerr << "Problem with second call to MakeDirectory for: "
  127. << testNewDir << std::endl;
  128. res = false;
  129. }
  130. // calling with 0 pointer should return false
  131. if (kwsys::SystemTools::MakeDirectory(0)) {
  132. std::cerr << "Problem with MakeDirectory(0)" << std::endl;
  133. res = false;
  134. }
  135. // calling with an empty string should return false
  136. if (kwsys::SystemTools::MakeDirectory(std::string())) {
  137. std::cerr << "Problem with MakeDirectory(std::string())" << std::endl;
  138. res = false;
  139. }
  140. // check existence
  141. if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
  142. std::cerr << "Problem with FileExists as C string and not file for: "
  143. << testNewDir << std::endl;
  144. res = false;
  145. }
  146. // check existence
  147. if (!kwsys::SystemTools::PathExists(testNewDir)) {
  148. std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
  149. res = false;
  150. }
  151. // remove it
  152. if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
  153. std::cerr << "Problem with RemoveADirectory for: " << testNewDir
  154. << std::endl;
  155. res = false;
  156. }
  157. // check existence
  158. if (kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
  159. std::cerr << "After RemoveADirectory: "
  160. << "Problem with FileExists as C string and not file for: "
  161. << testNewDir << std::endl;
  162. res = false;
  163. }
  164. // check existence
  165. if (kwsys::SystemTools::PathExists(testNewDir)) {
  166. std::cerr << "After RemoveADirectory: "
  167. << "Problem with PathExists for: " << testNewDir << std::endl;
  168. res = false;
  169. }
  170. // create it using the char* version
  171. if (!kwsys::SystemTools::MakeDirectory(testNewDir.c_str())) {
  172. std::cerr << "Problem with second call to MakeDirectory as C string for: "
  173. << testNewDir << std::endl;
  174. res = false;
  175. }
  176. if (!kwsys::SystemTools::Touch(testNewFile.c_str(), true)) {
  177. std::cerr << "Problem with Touch for: " << testNewFile << std::endl;
  178. res = false;
  179. }
  180. // calling MakeDirectory with something that is no file should fail
  181. if (kwsys::SystemTools::MakeDirectory(testNewFile)) {
  182. std::cerr << "Problem with to MakeDirectory for: " << testNewFile
  183. << std::endl;
  184. res = false;
  185. }
  186. // calling with 0 pointer should return false
  187. if (kwsys::SystemTools::FileExists(0)) {
  188. std::cerr << "Problem with FileExists(0)" << std::endl;
  189. res = false;
  190. }
  191. if (kwsys::SystemTools::FileExists(0, true)) {
  192. std::cerr << "Problem with FileExists(0) as file" << std::endl;
  193. res = false;
  194. }
  195. // calling with an empty string should return false
  196. if (kwsys::SystemTools::FileExists(std::string())) {
  197. std::cerr << "Problem with FileExists(std::string())" << std::endl;
  198. res = false;
  199. }
  200. // FileExists(x, true) should return false on a directory
  201. if (kwsys::SystemTools::FileExists(testNewDir, true)) {
  202. std::cerr << "Problem with FileExists as file for: " << testNewDir
  203. << std::endl;
  204. res = false;
  205. }
  206. if (kwsys::SystemTools::FileExists(testNewDir.c_str(), true)) {
  207. std::cerr << "Problem with FileExists as C string and file for: "
  208. << testNewDir << std::endl;
  209. res = false;
  210. }
  211. // FileExists(x, false) should return true even on a directory
  212. if (!kwsys::SystemTools::FileExists(testNewDir, false)) {
  213. std::cerr << "Problem with FileExists as not file for: " << testNewDir
  214. << std::endl;
  215. res = false;
  216. }
  217. if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
  218. std::cerr << "Problem with FileExists as C string and not file for: "
  219. << testNewDir << std::endl;
  220. res = false;
  221. }
  222. // should work, was created as new file before
  223. if (!kwsys::SystemTools::FileExists(testNewFile)) {
  224. std::cerr << "Problem with FileExists for: " << testNewDir << std::endl;
  225. res = false;
  226. }
  227. if (!kwsys::SystemTools::FileExists(testNewFile.c_str())) {
  228. std::cerr << "Problem with FileExists as C string for: " << testNewDir
  229. << std::endl;
  230. res = false;
  231. }
  232. if (!kwsys::SystemTools::FileExists(testNewFile, true)) {
  233. std::cerr << "Problem with FileExists as file for: " << testNewDir
  234. << std::endl;
  235. res = false;
  236. }
  237. if (!kwsys::SystemTools::FileExists(testNewFile.c_str(), true)) {
  238. std::cerr << "Problem with FileExists as C string and file for: "
  239. << testNewDir << std::endl;
  240. res = false;
  241. }
  242. // calling with an empty string should return false
  243. if (kwsys::SystemTools::PathExists(std::string())) {
  244. std::cerr << "Problem with PathExists(std::string())" << std::endl;
  245. res = false;
  246. }
  247. // PathExists(x) should return true on a directory
  248. if (!kwsys::SystemTools::PathExists(testNewDir)) {
  249. std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
  250. res = false;
  251. }
  252. // should work, was created as new file before
  253. if (!kwsys::SystemTools::PathExists(testNewFile)) {
  254. std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
  255. res = false;
  256. }
  257. // Reset umask
  258. #if defined(_WIN32) && !defined(__CYGWIN__)
  259. // NOTE: Windows doesn't support toggling _S_IREAD.
  260. mode_t fullMask = _S_IWRITE;
  261. #else
  262. // On a normal POSIX platform, we can toggle all permissions.
  263. mode_t fullMask = S_IRWXU | S_IRWXG | S_IRWXO;
  264. #endif
  265. mode_t orig_umask = umask(fullMask);
  266. // Test file permissions without umask
  267. mode_t origPerm, thisPerm;
  268. if (!kwsys::SystemTools::GetPermissions(testNewFile, origPerm)) {
  269. std::cerr << "Problem with GetPermissions (1) for: " << testNewFile
  270. << std::endl;
  271. res = false;
  272. }
  273. if (!kwsys::SystemTools::SetPermissions(testNewFile, 0)) {
  274. std::cerr << "Problem with SetPermissions (1) for: " << testNewFile
  275. << std::endl;
  276. res = false;
  277. }
  278. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  279. std::cerr << "Problem with GetPermissions (2) for: " << testNewFile
  280. << std::endl;
  281. res = false;
  282. }
  283. if ((thisPerm & fullMask) != 0) {
  284. std::cerr << "SetPermissions failed to set permissions (1) for: "
  285. << testNewFile << ": actual = " << thisPerm
  286. << "; expected = " << 0 << std::endl;
  287. res = false;
  288. }
  289. // While we're at it, check proper TestFileAccess functionality.
  290. if (kwsys::SystemTools::TestFileAccess(testNewFile,
  291. kwsys::TEST_FILE_WRITE)) {
  292. std::cerr
  293. << "TestFileAccess incorrectly indicated that this is a writable file:"
  294. << testNewFile << std::endl;
  295. res = false;
  296. }
  297. if (!kwsys::SystemTools::TestFileAccess(testNewFile, kwsys::TEST_FILE_OK)) {
  298. std::cerr
  299. << "TestFileAccess incorrectly indicated that this file does not exist:"
  300. << testNewFile << std::endl;
  301. res = false;
  302. }
  303. // Test restoring/setting full permissions.
  304. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask)) {
  305. std::cerr << "Problem with SetPermissions (2) for: " << testNewFile
  306. << std::endl;
  307. res = false;
  308. }
  309. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  310. std::cerr << "Problem with GetPermissions (3) for: " << testNewFile
  311. << std::endl;
  312. res = false;
  313. }
  314. if ((thisPerm & fullMask) != fullMask) {
  315. std::cerr << "SetPermissions failed to set permissions (2) for: "
  316. << testNewFile << ": actual = " << thisPerm
  317. << "; expected = " << fullMask << std::endl;
  318. res = false;
  319. }
  320. // Test setting file permissions while honoring umask
  321. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask, true)) {
  322. std::cerr << "Problem with SetPermissions (3) for: " << testNewFile
  323. << std::endl;
  324. res = false;
  325. }
  326. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  327. std::cerr << "Problem with GetPermissions (4) for: " << testNewFile
  328. << std::endl;
  329. res = false;
  330. }
  331. if ((thisPerm & fullMask) != 0) {
  332. std::cerr << "SetPermissions failed to honor umask for: " << testNewFile
  333. << ": actual = " << thisPerm << "; expected = " << 0
  334. << std::endl;
  335. res = false;
  336. }
  337. // Restore umask
  338. umask(orig_umask);
  339. // Restore file permissions
  340. if (!kwsys::SystemTools::SetPermissions(testNewFile, origPerm)) {
  341. std::cerr << "Problem with SetPermissions (4) for: " << testNewFile
  342. << std::endl;
  343. res = false;
  344. }
  345. // Remove the test file
  346. if (!kwsys::SystemTools::RemoveFile(testNewFile)) {
  347. std::cerr << "Problem with RemoveFile: " << testNewFile << std::endl;
  348. res = false;
  349. }
  350. std::string const testFileMissing(testNewDir + "/testMissingFile.txt");
  351. if (!kwsys::SystemTools::RemoveFile(testFileMissing)) {
  352. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  353. std::cerr << "RemoveFile(\"" << testFileMissing << "\") failed: " << msg
  354. << "\n";
  355. res = false;
  356. }
  357. std::string const testFileMissingDir(testNewDir + "/missing/file.txt");
  358. if (!kwsys::SystemTools::RemoveFile(testFileMissingDir)) {
  359. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  360. std::cerr << "RemoveFile(\"" << testFileMissingDir << "\") failed: " << msg
  361. << "\n";
  362. res = false;
  363. }
  364. kwsys::SystemTools::Touch(testNewFile.c_str(), true);
  365. if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
  366. std::cerr << "Problem with RemoveADirectory for: " << testNewDir
  367. << std::endl;
  368. res = false;
  369. }
  370. #ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  371. // Perform the same file and directory creation and deletion tests but
  372. // with paths > 256 characters in length.
  373. const std::string testNewLongDir(
  374. TEST_SYSTEMTOOLS_BINARY_DIR
  375. "/"
  376. "012345678901234567890123456789012345678901234567890123456789"
  377. "012345678901234567890123456789012345678901234567890123456789"
  378. "012345678901234567890123456789012345678901234567890123456789"
  379. "012345678901234567890123456789012345678901234567890123456789"
  380. "01234567890123");
  381. const std::string testNewLongFile(
  382. testNewLongDir +
  383. "/"
  384. "012345678901234567890123456789012345678901234567890123456789"
  385. "012345678901234567890123456789012345678901234567890123456789"
  386. "012345678901234567890123456789012345678901234567890123456789"
  387. "012345678901234567890123456789012345678901234567890123456789"
  388. "0123456789.txt");
  389. if (!kwsys::SystemTools::MakeDirectory(testNewLongDir)) {
  390. std::cerr << "Problem with MakeDirectory for: " << testNewLongDir
  391. << std::endl;
  392. res = false;
  393. }
  394. if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true)) {
  395. std::cerr << "Problem with Touch for: " << testNewLongFile << std::endl;
  396. res = false;
  397. }
  398. if (!kwsys::SystemTools::RemoveFile(testNewLongFile)) {
  399. std::cerr << "Problem with RemoveFile: " << testNewLongFile << std::endl;
  400. res = false;
  401. }
  402. kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
  403. if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir)) {
  404. std::cerr << "Problem with RemoveADirectory for: " << testNewLongDir
  405. << std::endl;
  406. res = false;
  407. }
  408. #endif
  409. return res;
  410. }
  411. //----------------------------------------------------------------------------
  412. static bool CheckStringOperations()
  413. {
  414. bool res = true;
  415. std::string test = "mary had a little lamb.";
  416. if (kwsys::SystemTools::CapitalizedWords(test) !=
  417. "Mary Had A Little Lamb.") {
  418. std::cerr << "Problem with CapitalizedWords " << '"' << test << '"'
  419. << std::endl;
  420. res = false;
  421. }
  422. test = "Mary Had A Little Lamb.";
  423. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  424. "mary had a little lamb.") {
  425. std::cerr << "Problem with UnCapitalizedWords " << '"' << test << '"'
  426. << std::endl;
  427. res = false;
  428. }
  429. test = "MaryHadTheLittleLamb.";
  430. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  431. "Mary Had The Little Lamb.") {
  432. std::cerr << "Problem with AddSpaceBetweenCapitalizedWords " << '"' << test
  433. << '"' << std::endl;
  434. res = false;
  435. }
  436. char* cres =
  437. kwsys::SystemTools::AppendStrings("Mary Had A", " Little Lamb.");
  438. if (strcmp(cres, "Mary Had A Little Lamb.")) {
  439. std::cerr << "Problem with AppendStrings "
  440. << "\"Mary Had A\" \" Little Lamb.\"" << std::endl;
  441. res = false;
  442. }
  443. delete[] cres;
  444. cres = kwsys::SystemTools::AppendStrings("Mary Had", " A ", "Little Lamb.");
  445. if (strcmp(cres, "Mary Had A Little Lamb.")) {
  446. std::cerr << "Problem with AppendStrings "
  447. << "\"Mary Had\" \" A \" \"Little Lamb.\"" << std::endl;
  448. res = false;
  449. }
  450. delete[] cres;
  451. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.", 'a') != 3) {
  452. std::cerr << "Problem with CountChar "
  453. << "\"Mary Had A Little Lamb.\"" << std::endl;
  454. res = false;
  455. }
  456. cres = kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.", "aeiou");
  457. if (strcmp(cres, "Mry Hd A Lttl Lmb.")) {
  458. std::cerr << "Problem with RemoveChars "
  459. << "\"Mary Had A Little Lamb.\"" << std::endl;
  460. res = false;
  461. }
  462. delete[] cres;
  463. cres = kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  464. if (strcmp(cres, "A")) {
  465. std::cerr << "Problem with RemoveCharsButUpperHex "
  466. << "\"Mary Had A Little Lamb.\"" << std::endl;
  467. res = false;
  468. }
  469. delete[] cres;
  470. char* cres2 = new char[strlen("Mary Had A Little Lamb.") + 1];
  471. strcpy(cres2, "Mary Had A Little Lamb.");
  472. kwsys::SystemTools::ReplaceChars(cres2, "aeiou", 'X');
  473. if (strcmp(cres2, "MXry HXd A LXttlX LXmb.")) {
  474. std::cerr << "Problem with ReplaceChars "
  475. << "\"Mary Had A Little Lamb.\"" << std::endl;
  476. res = false;
  477. }
  478. delete[] cres2;
  479. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  480. "Mary ")) {
  481. std::cerr << "Problem with StringStartsWith "
  482. << "\"Mary Had A Little Lamb.\"" << std::endl;
  483. res = false;
  484. }
  485. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  486. " Lamb.")) {
  487. std::cerr << "Problem with StringEndsWith "
  488. << "\"Mary Had A Little Lamb.\"" << std::endl;
  489. res = false;
  490. }
  491. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  492. if (strcmp(cres, "Mary Had A Little Lamb.")) {
  493. std::cerr << "Problem with DuplicateString "
  494. << "\"Mary Had A Little Lamb.\"" << std::endl;
  495. res = false;
  496. }
  497. delete[] cres;
  498. test = "Mary Had A Little Lamb.";
  499. if (kwsys::SystemTools::CropString(test, 13) != "Mary ...Lamb.") {
  500. std::cerr << "Problem with CropString "
  501. << "\"Mary Had A Little Lamb.\"" << std::endl;
  502. res = false;
  503. }
  504. std::vector<std::string> lines;
  505. kwsys::SystemTools::Split("Mary Had A Little Lamb.", lines, ' ');
  506. if (lines[0] != "Mary" || lines[1] != "Had" || lines[2] != "A" ||
  507. lines[3] != "Little" || lines[4] != "Lamb.") {
  508. std::cerr << "Problem with Split "
  509. << "\"Mary Had A Little Lamb.\"" << std::endl;
  510. res = false;
  511. }
  512. #ifdef _WIN32
  513. if (kwsys::SystemTools::ConvertToWindowsExtendedPath(
  514. "L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  515. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
  516. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  517. << "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  518. << std::endl;
  519. res = false;
  520. }
  521. if (kwsys::SystemTools::ConvertToWindowsExtendedPath(
  522. "L:/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  523. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
  524. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  525. << "\"L:/Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  526. res = false;
  527. }
  528. if (kwsys::SystemTools::ConvertToWindowsExtendedPath(
  529. "\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  530. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
  531. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  532. << "\"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  533. << std::endl;
  534. res = false;
  535. }
  536. if (kwsys::SystemTools::ConvertToWindowsExtendedPath(
  537. "//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  538. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
  539. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  540. << "\"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  541. << std::endl;
  542. res = false;
  543. }
  544. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("//") != L"//") {
  545. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  546. << "\"//\"" << std::endl;
  547. res = false;
  548. }
  549. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\") !=
  550. L"\\\\.\\") {
  551. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  552. << "\"\\\\.\\\"" << std::endl;
  553. res = false;
  554. }
  555. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X") !=
  556. L"\\\\.\\X") {
  557. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  558. << "\"\\\\.\\X\"" << std::endl;
  559. res = false;
  560. }
  561. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:") !=
  562. L"\\\\?\\X:") {
  563. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  564. << "\"\\\\.\\X:\"" << std::endl;
  565. res = false;
  566. }
  567. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:\\") !=
  568. L"\\\\?\\X:\\") {
  569. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  570. << "\"\\\\.\\X:\\\"" << std::endl;
  571. res = false;
  572. }
  573. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("NUL") !=
  574. L"\\\\.\\NUL") {
  575. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  576. << "\"NUL\"" << std::endl;
  577. res = false;
  578. }
  579. #endif
  580. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  581. "L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  582. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  583. std::cerr << "Problem with ConvertToWindowsOutputPath "
  584. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  585. res = false;
  586. }
  587. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  588. "//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  589. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  590. std::cerr << "Problem with ConvertToWindowsOutputPath "
  591. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  592. << std::endl;
  593. res = false;
  594. }
  595. if (kwsys::SystemTools::ConvertToUnixOutputPath(
  596. "//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  597. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo") {
  598. std::cerr << "Problem with ConvertToUnixOutputPath "
  599. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  600. res = false;
  601. }
  602. return res;
  603. }
  604. //----------------------------------------------------------------------------
  605. static bool CheckPutEnv(const std::string& env, const char* name,
  606. const char* value)
  607. {
  608. if (!kwsys::SystemTools::PutEnv(env)) {
  609. std::cerr << "PutEnv(\"" << env << "\") failed!" << std::endl;
  610. return false;
  611. }
  612. std::string v = "(null)";
  613. kwsys::SystemTools::GetEnv(name, v);
  614. if (v != value) {
  615. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v << "\", not \""
  616. << value << "\"!" << std::endl;
  617. return false;
  618. }
  619. return true;
  620. }
  621. static bool CheckUnPutEnv(const char* env, const char* name)
  622. {
  623. if (!kwsys::SystemTools::UnPutEnv(env)) {
  624. std::cerr << "UnPutEnv(\"" << env << "\") failed!" << std::endl;
  625. return false;
  626. }
  627. std::string v;
  628. if (kwsys::SystemTools::GetEnv(name, v)) {
  629. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v
  630. << "\", not (null)!" << std::endl;
  631. return false;
  632. }
  633. return true;
  634. }
  635. static bool CheckEnvironmentOperations()
  636. {
  637. bool res = true;
  638. res &= CheckPutEnv("A=B", "A", "B");
  639. res &= CheckPutEnv("B=C", "B", "C");
  640. res &= CheckPutEnv("C=D", "C", "D");
  641. res &= CheckPutEnv("D=E", "D", "E");
  642. res &= CheckUnPutEnv("A", "A");
  643. res &= CheckUnPutEnv("B=", "B");
  644. res &= CheckUnPutEnv("C=D", "C");
  645. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  646. return res;
  647. }
  648. static bool CheckRelativePath(const std::string& local,
  649. const std::string& remote,
  650. const std::string& expected)
  651. {
  652. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  653. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  654. std::cerr << "RelativePath(" << local << ", " << remote << ") yielded "
  655. << result << " instead of " << expected << std::endl;
  656. return false;
  657. }
  658. return true;
  659. }
  660. static bool CheckRelativePaths()
  661. {
  662. bool res = true;
  663. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  664. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  665. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  666. res &=
  667. CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  668. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  669. return res;
  670. }
  671. static bool CheckCollapsePath(const std::string& path,
  672. const std::string& expected)
  673. {
  674. std::string result = kwsys::SystemTools::CollapseFullPath(path);
  675. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  676. std::cerr << "CollapseFullPath(" << path << ") yielded " << result
  677. << " instead of " << expected << std::endl;
  678. return false;
  679. }
  680. return true;
  681. }
  682. static bool CheckCollapsePath()
  683. {
  684. bool res = true;
  685. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  686. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  687. return res;
  688. }
  689. static std::string StringVectorToString(const std::vector<std::string>& vec)
  690. {
  691. std::stringstream ss;
  692. ss << "vector(";
  693. for (std::vector<std::string>::const_iterator i = vec.begin();
  694. i != vec.end(); ++i) {
  695. if (i != vec.begin()) {
  696. ss << ", ";
  697. }
  698. ss << *i;
  699. }
  700. ss << ")";
  701. return ss.str();
  702. }
  703. static bool CheckGetPath()
  704. {
  705. const char* envName = "S";
  706. #ifdef _WIN32
  707. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  708. #else
  709. const char* envValue = "/Somewhere/something:/tmp";
  710. #endif
  711. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  712. std::vector<std::string> originalPathes;
  713. originalPathes.push_back(registryPath);
  714. std::vector<std::string> expectedPathes;
  715. expectedPathes.push_back(registryPath);
  716. #ifdef _WIN32
  717. expectedPathes.push_back("C:/Somewhere/something");
  718. expectedPathes.push_back("D:/Temp");
  719. #else
  720. expectedPathes.push_back("/Somewhere/something");
  721. expectedPathes.push_back("/tmp");
  722. #endif
  723. bool res = true;
  724. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  725. std::vector<std::string> pathes = originalPathes;
  726. kwsys::SystemTools::GetPath(pathes, envName);
  727. if (pathes != expectedPathes) {
  728. std::cerr << "GetPath(" << StringVectorToString(originalPathes) << ", "
  729. << envName << ") yielded " << StringVectorToString(pathes)
  730. << " instead of " << StringVectorToString(expectedPathes)
  731. << std::endl;
  732. res = false;
  733. }
  734. res &= CheckUnPutEnv(envName, envName);
  735. return res;
  736. }
  737. static bool CheckFind()
  738. {
  739. bool res = true;
  740. const std::string testFindFileName("testFindFile.txt");
  741. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/" +
  742. testFindFileName);
  743. if (!kwsys::SystemTools::Touch(testFindFile.c_str(), true)) {
  744. std::cerr << "Problem with Touch for: " << testFindFile << std::endl;
  745. // abort here as the existence of the file only makes the test meaningful
  746. return false;
  747. }
  748. std::vector<std::string> searchPaths;
  749. searchPaths.push_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  750. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true)
  751. .empty()) {
  752. std::cerr << "Problem with FindFile without system paths for: "
  753. << testFindFileName << std::endl;
  754. res = false;
  755. }
  756. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, false)
  757. .empty()) {
  758. std::cerr << "Problem with FindFile with system paths for: "
  759. << testFindFileName << std::endl;
  760. res = false;
  761. }
  762. return res;
  763. }
  764. //----------------------------------------------------------------------------
  765. int testSystemTools(int, char* [])
  766. {
  767. bool res = true;
  768. int cc;
  769. for (cc = 0; toUnixPaths[cc][0]; cc++) {
  770. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  771. }
  772. // Special check for ~
  773. std::string output;
  774. if (kwsys::SystemTools::GetEnv("HOME", output)) {
  775. output += "/foo bar/lala";
  776. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  777. }
  778. for (cc = 0; checkEscapeChars[cc][0]; cc++) {
  779. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  780. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  781. }
  782. res &= CheckFileOperations();
  783. res &= CheckStringOperations();
  784. res &= CheckEnvironmentOperations();
  785. res &= CheckRelativePaths();
  786. res &= CheckCollapsePath();
  787. res &= CheckGetPath();
  788. res &= CheckFind();
  789. return res ? 0 : 1;
  790. }