testSystemTools.cxx 32 KB

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