testSystemTools.cxx 32 KB

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