testSystemTools.cxx 29 KB

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