testSystemTools.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. #ifdef _WIN32
  526. if (kwsys::SystemTools::ConvertToWindowsExtendedPath(
  527. "L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  528. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
  529. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  530. << "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  531. << std::endl;
  532. res = false;
  533. }
  534. if (kwsys::SystemTools::ConvertToWindowsExtendedPath(
  535. "L:/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  536. L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
  537. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  538. << "\"L:/Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  539. res = false;
  540. }
  541. if (kwsys::SystemTools::ConvertToWindowsExtendedPath(
  542. "\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
  543. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
  544. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  545. << "\"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
  546. << std::endl;
  547. res = false;
  548. }
  549. if (kwsys::SystemTools::ConvertToWindowsExtendedPath(
  550. "//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  551. L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
  552. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  553. << "\"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  554. << std::endl;
  555. res = false;
  556. }
  557. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("//") != L"//") {
  558. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  559. << "\"//\"" << std::endl;
  560. res = false;
  561. }
  562. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\") !=
  563. L"\\\\.\\") {
  564. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  565. << "\"\\\\.\\\"" << std::endl;
  566. res = false;
  567. }
  568. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X") !=
  569. L"\\\\.\\X") {
  570. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  571. << "\"\\\\.\\X\"" << std::endl;
  572. res = false;
  573. }
  574. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:") !=
  575. L"\\\\?\\X:") {
  576. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  577. << "\"\\\\.\\X:\"" << std::endl;
  578. res = false;
  579. }
  580. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("\\\\.\\X:\\") !=
  581. L"\\\\?\\X:\\") {
  582. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  583. << "\"\\\\.\\X:\\\"" << std::endl;
  584. res = false;
  585. }
  586. if (kwsys::SystemTools::ConvertToWindowsExtendedPath("NUL") !=
  587. L"\\\\.\\NUL") {
  588. std::cerr << "Problem with ConvertToWindowsExtendedPath "
  589. << "\"NUL\"" << std::endl;
  590. res = false;
  591. }
  592. #endif
  593. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  594. "L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  595. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  596. std::cerr << "Problem with ConvertToWindowsOutputPath "
  597. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  598. res = false;
  599. }
  600. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  601. "//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  602. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  603. std::cerr << "Problem with ConvertToWindowsOutputPath "
  604. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  605. << std::endl;
  606. res = false;
  607. }
  608. if (kwsys::SystemTools::ConvertToUnixOutputPath(
  609. "//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  610. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo") {
  611. std::cerr << "Problem with ConvertToUnixOutputPath "
  612. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  613. res = false;
  614. }
  615. return res;
  616. }
  617. //----------------------------------------------------------------------------
  618. static bool CheckPutEnv(const std::string& env, const char* name,
  619. const char* value)
  620. {
  621. if (!kwsys::SystemTools::PutEnv(env)) {
  622. std::cerr << "PutEnv(\"" << env << "\") failed!" << std::endl;
  623. return false;
  624. }
  625. std::string v = "(null)";
  626. kwsys::SystemTools::GetEnv(name, v);
  627. if (v != value) {
  628. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v << "\", not \""
  629. << value << "\"!" << std::endl;
  630. return false;
  631. }
  632. return true;
  633. }
  634. static bool CheckUnPutEnv(const char* env, const char* name)
  635. {
  636. if (!kwsys::SystemTools::UnPutEnv(env)) {
  637. std::cerr << "UnPutEnv(\"" << env << "\") failed!" << std::endl;
  638. return false;
  639. }
  640. std::string v;
  641. if (kwsys::SystemTools::GetEnv(name, v)) {
  642. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v
  643. << "\", not (null)!" << std::endl;
  644. return false;
  645. }
  646. return true;
  647. }
  648. static bool CheckEnvironmentOperations()
  649. {
  650. bool res = true;
  651. res &= CheckPutEnv("A=B", "A", "B");
  652. res &= CheckPutEnv("B=C", "B", "C");
  653. res &= CheckPutEnv("C=D", "C", "D");
  654. res &= CheckPutEnv("D=E", "D", "E");
  655. res &= CheckUnPutEnv("A", "A");
  656. res &= CheckUnPutEnv("B=", "B");
  657. res &= CheckUnPutEnv("C=D", "C");
  658. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  659. return res;
  660. }
  661. static bool CheckRelativePath(const std::string& local,
  662. const std::string& remote,
  663. const std::string& expected)
  664. {
  665. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  666. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  667. std::cerr << "RelativePath(" << local << ", " << remote << ") yielded "
  668. << result << " instead of " << expected << std::endl;
  669. return false;
  670. }
  671. return true;
  672. }
  673. static bool CheckRelativePaths()
  674. {
  675. bool res = true;
  676. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  677. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  678. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  679. res &=
  680. CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  681. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  682. return res;
  683. }
  684. static bool CheckCollapsePath(const std::string& path,
  685. const std::string& expected)
  686. {
  687. std::string result = kwsys::SystemTools::CollapseFullPath(path);
  688. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  689. std::cerr << "CollapseFullPath(" << path << ") yielded " << result
  690. << " instead of " << expected << std::endl;
  691. return false;
  692. }
  693. return true;
  694. }
  695. static bool CheckCollapsePath()
  696. {
  697. bool res = true;
  698. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  699. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  700. return res;
  701. }
  702. static std::string StringVectorToString(const std::vector<std::string>& vec)
  703. {
  704. std::stringstream ss;
  705. ss << "vector(";
  706. for (std::vector<std::string>::const_iterator i = vec.begin();
  707. i != vec.end(); ++i) {
  708. if (i != vec.begin()) {
  709. ss << ", ";
  710. }
  711. ss << *i;
  712. }
  713. ss << ")";
  714. return ss.str();
  715. }
  716. static bool CheckGetPath()
  717. {
  718. const char* envName = "S";
  719. #ifdef _WIN32
  720. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  721. #else
  722. const char* envValue = "/Somewhere/something:/tmp";
  723. #endif
  724. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  725. std::vector<std::string> originalPathes;
  726. originalPathes.push_back(registryPath);
  727. std::vector<std::string> expectedPathes;
  728. expectedPathes.push_back(registryPath);
  729. #ifdef _WIN32
  730. expectedPathes.push_back("C:/Somewhere/something");
  731. expectedPathes.push_back("D:/Temp");
  732. #else
  733. expectedPathes.push_back("/Somewhere/something");
  734. expectedPathes.push_back("/tmp");
  735. #endif
  736. bool res = true;
  737. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  738. std::vector<std::string> pathes = originalPathes;
  739. kwsys::SystemTools::GetPath(pathes, envName);
  740. if (pathes != expectedPathes) {
  741. std::cerr << "GetPath(" << StringVectorToString(originalPathes) << ", "
  742. << envName << ") yielded " << StringVectorToString(pathes)
  743. << " instead of " << StringVectorToString(expectedPathes)
  744. << std::endl;
  745. res = false;
  746. }
  747. res &= CheckUnPutEnv(envName, envName);
  748. return res;
  749. }
  750. static bool CheckFind()
  751. {
  752. bool res = true;
  753. const std::string testFindFileName("testFindFile.txt");
  754. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/" +
  755. testFindFileName);
  756. if (!kwsys::SystemTools::Touch(testFindFile.c_str(), true)) {
  757. std::cerr << "Problem with Touch for: " << testFindFile << std::endl;
  758. // abort here as the existence of the file only makes the test meaningful
  759. return false;
  760. }
  761. std::vector<std::string> searchPaths;
  762. searchPaths.push_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  763. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true)
  764. .empty()) {
  765. std::cerr << "Problem with FindFile without system paths for: "
  766. << testFindFileName << std::endl;
  767. res = false;
  768. }
  769. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, false)
  770. .empty()) {
  771. std::cerr << "Problem with FindFile with system paths for: "
  772. << testFindFileName << std::endl;
  773. res = false;
  774. }
  775. return res;
  776. }
  777. static bool CheckGetLineFromStream()
  778. {
  779. const std::string fileWithFiveCharsOnFirstLine(TEST_SYSTEMTOOLS_SOURCE_DIR
  780. "/README.rst");
  781. kwsys::ifstream file(fileWithFiveCharsOnFirstLine.c_str(), std::ios::in);
  782. if (!file) {
  783. std::cerr << "Problem opening: " << fileWithFiveCharsOnFirstLine
  784. << std::endl;
  785. return false;
  786. }
  787. std::string line;
  788. bool has_newline = false;
  789. bool result;
  790. file.seekg(0, std::ios::beg);
  791. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  792. if (!result || line.size() != 5) {
  793. std::cerr << "First line does not have five characters: " << line.size()
  794. << std::endl;
  795. return false;
  796. }
  797. file.seekg(0, std::ios::beg);
  798. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  799. if (!result || line.size() != 5) {
  800. std::cerr << "First line does not have five characters after rewind: "
  801. << line.size() << std::endl;
  802. return false;
  803. }
  804. bool ret = true;
  805. for (size_t size = 1; size <= 5; ++size) {
  806. file.seekg(0, std::ios::beg);
  807. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  808. static_cast<long>(size));
  809. if (!result || line.size() != size) {
  810. std::cerr << "Should have read " << size << " characters but got "
  811. << line.size() << std::endl;
  812. ret = false;
  813. }
  814. }
  815. return ret;
  816. }
  817. //----------------------------------------------------------------------------
  818. int testSystemTools(int, char* [])
  819. {
  820. bool res = true;
  821. int cc;
  822. for (cc = 0; toUnixPaths[cc][0]; cc++) {
  823. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  824. }
  825. // Special check for ~
  826. std::string output;
  827. if (kwsys::SystemTools::GetEnv("HOME", output)) {
  828. output += "/foo bar/lala";
  829. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  830. }
  831. for (cc = 0; checkEscapeChars[cc][0]; cc++) {
  832. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  833. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  834. }
  835. res &= CheckFileOperations();
  836. res &= CheckStringOperations();
  837. res &= CheckEnvironmentOperations();
  838. res &= CheckRelativePaths();
  839. res &= CheckCollapsePath();
  840. res &= CheckGetPath();
  841. res &= CheckFind();
  842. res &= CheckGetLineFromStream();
  843. return res ? 0 : 1;
  844. }