testSystemTools.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. { 0, 0 }
  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] = { { "1 foo 2 bar 2", "12", "\\",
  64. "\\1 foo \\2 bar \\2" },
  65. { " {} ", "{}", "#", " #{#} " },
  66. { 0, 0, 0, 0 } };
  67. static bool CheckEscapeChars(std::string const& input,
  68. const char* chars_to_escape, char escape_char,
  69. std::string const& output)
  70. {
  71. std::string result = kwsys::SystemTools::EscapeChars(
  72. input.c_str(), chars_to_escape, escape_char);
  73. if (result != output) {
  74. std::cerr << "Problem with CheckEscapeChars - input: " << input
  75. << " output: " << result << " expected: " << output << std::endl;
  76. return false;
  77. }
  78. return true;
  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: " << testNewFile << std::endl;
  238. res = false;
  239. }
  240. if (!kwsys::SystemTools::FileExists(testNewFile.c_str())) {
  241. std::cerr << "Problem with FileExists as C string for: " << testNewFile
  242. << std::endl;
  243. res = false;
  244. }
  245. if (!kwsys::SystemTools::FileExists(testNewFile, true)) {
  246. std::cerr << "Problem with FileExists as file for: " << testNewFile
  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. << testNewFile << 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: " << testNewFile << 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. static bool CheckStringOperations()
  425. {
  426. bool res = true;
  427. std::string test = "mary had a little lamb.";
  428. if (kwsys::SystemTools::CapitalizedWords(test) !=
  429. "Mary Had A Little Lamb.") {
  430. std::cerr << "Problem with CapitalizedWords " << '"' << test << '"'
  431. << std::endl;
  432. res = false;
  433. }
  434. test = "Mary Had A Little Lamb.";
  435. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  436. "mary had a little lamb.") {
  437. std::cerr << "Problem with UnCapitalizedWords " << '"' << test << '"'
  438. << std::endl;
  439. res = false;
  440. }
  441. test = "MaryHadTheLittleLamb.";
  442. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  443. "Mary Had The Little Lamb.") {
  444. std::cerr << "Problem with AddSpaceBetweenCapitalizedWords " << '"' << test
  445. << '"' << std::endl;
  446. res = false;
  447. }
  448. char* cres =
  449. kwsys::SystemTools::AppendStrings("Mary Had A", " Little Lamb.");
  450. if (strcmp(cres, "Mary Had A Little Lamb.")) {
  451. std::cerr << "Problem with AppendStrings "
  452. << "\"Mary Had A\" \" Little Lamb.\"" << std::endl;
  453. res = false;
  454. }
  455. delete[] cres;
  456. cres = kwsys::SystemTools::AppendStrings("Mary Had", " A ", "Little Lamb.");
  457. if (strcmp(cres, "Mary Had A Little Lamb.")) {
  458. std::cerr << "Problem with AppendStrings "
  459. << "\"Mary Had\" \" A \" \"Little Lamb.\"" << std::endl;
  460. res = false;
  461. }
  462. delete[] cres;
  463. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.", 'a') != 3) {
  464. std::cerr << "Problem with CountChar "
  465. << "\"Mary Had A Little Lamb.\"" << std::endl;
  466. res = false;
  467. }
  468. cres = kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.", "aeiou");
  469. if (strcmp(cres, "Mry Hd A Lttl Lmb.")) {
  470. std::cerr << "Problem with RemoveChars "
  471. << "\"Mary Had A Little Lamb.\"" << std::endl;
  472. res = false;
  473. }
  474. delete[] cres;
  475. cres = kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  476. if (strcmp(cres, "A")) {
  477. std::cerr << "Problem with RemoveCharsButUpperHex "
  478. << "\"Mary Had A Little Lamb.\"" << std::endl;
  479. res = false;
  480. }
  481. delete[] cres;
  482. char* cres2 = strdup("Mary Had A Little Lamb.");
  483. kwsys::SystemTools::ReplaceChars(cres2, "aeiou", 'X');
  484. if (strcmp(cres2, "MXry HXd A LXttlX LXmb.")) {
  485. std::cerr << "Problem with ReplaceChars "
  486. << "\"Mary Had A Little Lamb.\"" << std::endl;
  487. res = false;
  488. }
  489. free(cres2);
  490. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  491. "Mary ")) {
  492. std::cerr << "Problem with StringStartsWith "
  493. << "\"Mary Had A Little Lamb.\"" << std::endl;
  494. res = false;
  495. }
  496. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  497. " Lamb.")) {
  498. std::cerr << "Problem with StringEndsWith "
  499. << "\"Mary Had A Little Lamb.\"" << std::endl;
  500. res = false;
  501. }
  502. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  503. if (strcmp(cres, "Mary Had A Little Lamb.")) {
  504. std::cerr << "Problem with DuplicateString "
  505. << "\"Mary Had A Little Lamb.\"" << std::endl;
  506. res = false;
  507. }
  508. delete[] cres;
  509. test = "Mary Had A Little Lamb.";
  510. if (kwsys::SystemTools::CropString(test, 13) != "Mary ...Lamb.") {
  511. std::cerr << "Problem with CropString "
  512. << "\"Mary Had A Little Lamb.\"" << std::endl;
  513. res = false;
  514. }
  515. std::vector<std::string> lines;
  516. kwsys::SystemTools::Split("Mary Had A Little Lamb.", lines, ' ');
  517. if (lines[0] != "Mary" || lines[1] != "Had" || lines[2] != "A" ||
  518. lines[3] != "Little" || lines[4] != "Lamb.") {
  519. std::cerr << "Problem with Split "
  520. << "\"Mary Had A Little Lamb.\"" << std::endl;
  521. res = false;
  522. }
  523. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  524. "L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  525. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  526. std::cerr << "Problem with ConvertToWindowsOutputPath "
  527. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  528. res = false;
  529. }
  530. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  531. "//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  532. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  533. std::cerr << "Problem with ConvertToWindowsOutputPath "
  534. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  535. << std::endl;
  536. res = false;
  537. }
  538. if (kwsys::SystemTools::ConvertToUnixOutputPath(
  539. "//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  540. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo") {
  541. std::cerr << "Problem with ConvertToUnixOutputPath "
  542. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  543. res = false;
  544. }
  545. return res;
  546. }
  547. static bool CheckPutEnv(const std::string& env, const char* name,
  548. const char* value)
  549. {
  550. if (!kwsys::SystemTools::PutEnv(env)) {
  551. std::cerr << "PutEnv(\"" << env << "\") failed!" << std::endl;
  552. return false;
  553. }
  554. std::string v = "(null)";
  555. kwsys::SystemTools::GetEnv(name, v);
  556. if (v != value) {
  557. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v << "\", not \""
  558. << value << "\"!" << std::endl;
  559. return false;
  560. }
  561. return true;
  562. }
  563. static bool CheckUnPutEnv(const char* env, const char* name)
  564. {
  565. if (!kwsys::SystemTools::UnPutEnv(env)) {
  566. std::cerr << "UnPutEnv(\"" << env << "\") failed!" << std::endl;
  567. return false;
  568. }
  569. std::string v;
  570. if (kwsys::SystemTools::GetEnv(name, v)) {
  571. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v
  572. << "\", not (null)!" << std::endl;
  573. return false;
  574. }
  575. return true;
  576. }
  577. static bool CheckEnvironmentOperations()
  578. {
  579. bool res = true;
  580. res &= CheckPutEnv("A=B", "A", "B");
  581. res &= CheckPutEnv("B=C", "B", "C");
  582. res &= CheckPutEnv("C=D", "C", "D");
  583. res &= CheckPutEnv("D=E", "D", "E");
  584. res &= CheckUnPutEnv("A", "A");
  585. res &= CheckUnPutEnv("B=", "B");
  586. res &= CheckUnPutEnv("C=D", "C");
  587. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  588. return res;
  589. }
  590. static bool CheckRelativePath(const std::string& local,
  591. const std::string& remote,
  592. const std::string& expected)
  593. {
  594. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  595. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  596. std::cerr << "RelativePath(" << local << ", " << remote << ") yielded "
  597. << result << " instead of " << expected << std::endl;
  598. return false;
  599. }
  600. return true;
  601. }
  602. static bool CheckRelativePaths()
  603. {
  604. bool res = true;
  605. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  606. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  607. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  608. res &=
  609. CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  610. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  611. return res;
  612. }
  613. static bool CheckCollapsePath(const std::string& path,
  614. const std::string& expected)
  615. {
  616. std::string result = kwsys::SystemTools::CollapseFullPath(path);
  617. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  618. std::cerr << "CollapseFullPath(" << path << ") yielded " << result
  619. << " instead of " << expected << std::endl;
  620. return false;
  621. }
  622. return true;
  623. }
  624. static bool CheckCollapsePath()
  625. {
  626. bool res = true;
  627. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  628. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  629. return res;
  630. }
  631. static std::string StringVectorToString(const std::vector<std::string>& vec)
  632. {
  633. std::stringstream ss;
  634. ss << "vector(";
  635. for (std::vector<std::string>::const_iterator i = vec.begin();
  636. i != vec.end(); ++i) {
  637. if (i != vec.begin()) {
  638. ss << ", ";
  639. }
  640. ss << *i;
  641. }
  642. ss << ")";
  643. return ss.str();
  644. }
  645. static bool CheckGetPath()
  646. {
  647. const char* envName = "S";
  648. #ifdef _WIN32
  649. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  650. #else
  651. const char* envValue = "/Somewhere/something:/tmp";
  652. #endif
  653. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  654. std::vector<std::string> originalPathes;
  655. originalPathes.push_back(registryPath);
  656. std::vector<std::string> expectedPathes;
  657. expectedPathes.push_back(registryPath);
  658. #ifdef _WIN32
  659. expectedPathes.push_back("C:/Somewhere/something");
  660. expectedPathes.push_back("D:/Temp");
  661. #else
  662. expectedPathes.push_back("/Somewhere/something");
  663. expectedPathes.push_back("/tmp");
  664. #endif
  665. bool res = true;
  666. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  667. std::vector<std::string> pathes = originalPathes;
  668. kwsys::SystemTools::GetPath(pathes, envName);
  669. if (pathes != expectedPathes) {
  670. std::cerr << "GetPath(" << StringVectorToString(originalPathes) << ", "
  671. << envName << ") yielded " << StringVectorToString(pathes)
  672. << " instead of " << StringVectorToString(expectedPathes)
  673. << std::endl;
  674. res = false;
  675. }
  676. res &= CheckUnPutEnv(envName, envName);
  677. return res;
  678. }
  679. static bool CheckGetFilenameName()
  680. {
  681. const char* windowsFilepath = "C:\\somewhere\\something";
  682. const char* unixFilepath = "/somewhere/something";
  683. std::string expectedFilename = "something";
  684. bool res = true;
  685. std::string filename = kwsys::SystemTools::GetFilenameName(windowsFilepath);
  686. if (filename != expectedFilename) {
  687. std::cerr << "GetFilenameName(" << windowsFilepath << ") yielded "
  688. << filename << " instead of " << expectedFilename << std::endl;
  689. res = false;
  690. }
  691. filename = kwsys::SystemTools::GetFilenameName(unixFilepath);
  692. if (filename != expectedFilename) {
  693. std::cerr << "GetFilenameName(" << unixFilepath << ") yielded " << filename
  694. << " instead of " << expectedFilename << std::endl;
  695. res = false;
  696. }
  697. return res;
  698. }
  699. static bool CheckFind()
  700. {
  701. bool res = true;
  702. const std::string testFindFileName("testFindFile.txt");
  703. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/" +
  704. testFindFileName);
  705. if (!kwsys::SystemTools::Touch(testFindFile.c_str(), true)) {
  706. std::cerr << "Problem with Touch for: " << testFindFile << std::endl;
  707. // abort here as the existence of the file only makes the test meaningful
  708. return false;
  709. }
  710. std::vector<std::string> searchPaths;
  711. searchPaths.push_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  712. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true)
  713. .empty()) {
  714. std::cerr << "Problem with FindFile without system paths for: "
  715. << testFindFileName << std::endl;
  716. res = false;
  717. }
  718. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, false)
  719. .empty()) {
  720. std::cerr << "Problem with FindFile with system paths for: "
  721. << testFindFileName << std::endl;
  722. res = false;
  723. }
  724. return res;
  725. }
  726. static bool CheckIsSubDirectory()
  727. {
  728. bool res = true;
  729. if (kwsys::SystemTools::IsSubDirectory("/foo", "/") == false) {
  730. std::cerr << "Problem with IsSubDirectory (root - unix): " << std::endl;
  731. res = false;
  732. }
  733. if (kwsys::SystemTools::IsSubDirectory("c:/foo", "c:/") == false) {
  734. std::cerr << "Problem with IsSubDirectory (root - dos): " << std::endl;
  735. res = false;
  736. }
  737. if (kwsys::SystemTools::IsSubDirectory("/foo/bar", "/foo") == false) {
  738. std::cerr << "Problem with IsSubDirectory (deep): " << std::endl;
  739. res = false;
  740. }
  741. if (kwsys::SystemTools::IsSubDirectory("/foo", "/foo") == true) {
  742. std::cerr << "Problem with IsSubDirectory (identity): " << std::endl;
  743. res = false;
  744. }
  745. if (kwsys::SystemTools::IsSubDirectory("/fooo", "/foo") == true) {
  746. std::cerr << "Problem with IsSubDirectory (substring): " << std::endl;
  747. res = false;
  748. }
  749. if (kwsys::SystemTools::IsSubDirectory("/foo/", "/foo") == true) {
  750. std::cerr << "Problem with IsSubDirectory (prepended slash): "
  751. << std::endl;
  752. res = false;
  753. }
  754. return res;
  755. }
  756. static bool CheckGetLineFromStream()
  757. {
  758. const std::string fileWithFiveCharsOnFirstLine(TEST_SYSTEMTOOLS_SOURCE_DIR
  759. "/README.rst");
  760. kwsys::ifstream file(fileWithFiveCharsOnFirstLine.c_str(), std::ios::in);
  761. if (!file) {
  762. std::cerr << "Problem opening: " << fileWithFiveCharsOnFirstLine
  763. << std::endl;
  764. return false;
  765. }
  766. std::string line;
  767. bool has_newline = false;
  768. bool result;
  769. file.seekg(0, std::ios::beg);
  770. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  771. if (!result || line.size() != 5) {
  772. std::cerr << "First line does not have five characters: " << line.size()
  773. << std::endl;
  774. return false;
  775. }
  776. file.seekg(0, std::ios::beg);
  777. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  778. if (!result || line.size() != 5) {
  779. std::cerr << "First line does not have five characters after rewind: "
  780. << line.size() << std::endl;
  781. return false;
  782. }
  783. bool ret = true;
  784. for (size_t size = 1; size <= 5; ++size) {
  785. file.seekg(0, std::ios::beg);
  786. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  787. static_cast<long>(size));
  788. if (!result || line.size() != size) {
  789. std::cerr << "Should have read " << size << " characters but got "
  790. << line.size() << std::endl;
  791. ret = false;
  792. }
  793. }
  794. return ret;
  795. }
  796. int testSystemTools(int, char* [])
  797. {
  798. bool res = true;
  799. int cc;
  800. for (cc = 0; toUnixPaths[cc][0]; cc++) {
  801. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  802. }
  803. // Special check for ~
  804. std::string output;
  805. if (kwsys::SystemTools::GetEnv("HOME", output)) {
  806. output += "/foo bar/lala";
  807. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  808. }
  809. for (cc = 0; checkEscapeChars[cc][0]; cc++) {
  810. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  811. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  812. }
  813. res &= CheckFileOperations();
  814. res &= CheckStringOperations();
  815. res &= CheckEnvironmentOperations();
  816. res &= CheckRelativePaths();
  817. res &= CheckCollapsePath();
  818. res &= CheckGetPath();
  819. res &= CheckFind();
  820. res &= CheckIsSubDirectory();
  821. res &= CheckGetLineFromStream();
  822. res &= CheckGetFilenameName();
  823. return res ? 0 : 1;
  824. }