testSystemTools.cxx 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. { nullptr, 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. { nullptr, nullptr, nullptr, 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(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, 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(nullptr)) {
  202. std::cerr << "Problem with FileExists(0)" << std::endl;
  203. res = false;
  204. }
  205. if (kwsys::SystemTools::FileExists(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, 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. const char* base = nullptr)
  617. {
  618. std::string result = kwsys::SystemTools::CollapseFullPath(path, base);
  619. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  620. std::cerr << "CollapseFullPath(" << path << ") yielded " << result
  621. << " instead of " << expected << std::endl;
  622. return false;
  623. }
  624. return true;
  625. }
  626. static bool CheckCollapsePath()
  627. {
  628. bool res = true;
  629. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  630. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  631. res &= CheckCollapsePath("/usr/share/../lib", "/usr/lib");
  632. res &= CheckCollapsePath("/usr/share/./lib", "/usr/share/lib");
  633. res &= CheckCollapsePath("/usr/share/../../lib", "/lib");
  634. res &= CheckCollapsePath("/usr/share/.././../lib", "/lib");
  635. res &= CheckCollapsePath("/../lib", "/lib");
  636. res &= CheckCollapsePath("/../lib/", "/lib");
  637. res &= CheckCollapsePath("/", "/");
  638. res &= CheckCollapsePath("C:/", "C:/");
  639. res &= CheckCollapsePath("C:/../", "C:/");
  640. res &= CheckCollapsePath("C:/../../", "C:/");
  641. res &= CheckCollapsePath("../b", "../../b", "../");
  642. res &= CheckCollapsePath("../a/../b", "../b", "../rel");
  643. res &= CheckCollapsePath("a/../b", "../rel/b", "../rel");
  644. return res;
  645. }
  646. static std::string StringVectorToString(const std::vector<std::string>& vec)
  647. {
  648. std::stringstream ss;
  649. ss << "vector(";
  650. for (std::vector<std::string>::const_iterator i = vec.begin();
  651. i != vec.end(); ++i) {
  652. if (i != vec.begin()) {
  653. ss << ", ";
  654. }
  655. ss << *i;
  656. }
  657. ss << ")";
  658. return ss.str();
  659. }
  660. static bool CheckGetPath()
  661. {
  662. const char* envName = "S";
  663. #ifdef _WIN32
  664. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  665. #else
  666. const char* envValue = "/Somewhere/something:/tmp";
  667. #endif
  668. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  669. std::vector<std::string> originalPaths;
  670. originalPaths.push_back(registryPath);
  671. std::vector<std::string> expectedPaths;
  672. expectedPaths.push_back(registryPath);
  673. #ifdef _WIN32
  674. expectedPaths.push_back("C:/Somewhere/something");
  675. expectedPaths.push_back("D:/Temp");
  676. #else
  677. expectedPaths.push_back("/Somewhere/something");
  678. expectedPaths.push_back("/tmp");
  679. #endif
  680. bool res = true;
  681. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  682. std::vector<std::string> paths = originalPaths;
  683. kwsys::SystemTools::GetPath(paths, envName);
  684. if (paths != expectedPaths) {
  685. std::cerr << "GetPath(" << StringVectorToString(originalPaths) << ", "
  686. << envName << ") yielded " << StringVectorToString(paths)
  687. << " instead of " << StringVectorToString(expectedPaths)
  688. << std::endl;
  689. res = false;
  690. }
  691. res &= CheckUnPutEnv(envName, envName);
  692. return res;
  693. }
  694. static bool CheckGetFilenameName()
  695. {
  696. const char* windowsFilepath = "C:\\somewhere\\something";
  697. const char* unixFilepath = "/somewhere/something";
  698. #if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
  699. std::string expectedWindowsFilename = "something";
  700. #else
  701. std::string expectedWindowsFilename = "C:\\somewhere\\something";
  702. #endif
  703. std::string expectedUnixFilename = "something";
  704. bool res = true;
  705. std::string filename = kwsys::SystemTools::GetFilenameName(windowsFilepath);
  706. if (filename != expectedWindowsFilename) {
  707. std::cerr << "GetFilenameName(" << windowsFilepath << ") yielded "
  708. << filename << " instead of " << expectedWindowsFilename
  709. << std::endl;
  710. res = false;
  711. }
  712. filename = kwsys::SystemTools::GetFilenameName(unixFilepath);
  713. if (filename != expectedUnixFilename) {
  714. std::cerr << "GetFilenameName(" << unixFilepath << ") yielded " << filename
  715. << " instead of " << expectedUnixFilename << std::endl;
  716. res = false;
  717. }
  718. return res;
  719. }
  720. static bool CheckFind()
  721. {
  722. bool res = true;
  723. const std::string testFindFileName("testFindFile.txt");
  724. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/" +
  725. testFindFileName);
  726. if (!kwsys::SystemTools::Touch(testFindFile, true)) {
  727. std::cerr << "Problem with Touch for: " << testFindFile << std::endl;
  728. // abort here as the existence of the file only makes the test meaningful
  729. return false;
  730. }
  731. std::vector<std::string> searchPaths;
  732. searchPaths.push_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  733. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true)
  734. .empty()) {
  735. std::cerr << "Problem with FindFile without system paths for: "
  736. << testFindFileName << std::endl;
  737. res = false;
  738. }
  739. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, false)
  740. .empty()) {
  741. std::cerr << "Problem with FindFile with system paths for: "
  742. << testFindFileName << std::endl;
  743. res = false;
  744. }
  745. return res;
  746. }
  747. static bool CheckIsSubDirectory()
  748. {
  749. bool res = true;
  750. if (kwsys::SystemTools::IsSubDirectory("/foo", "/") == false) {
  751. std::cerr << "Problem with IsSubDirectory (root - unix): " << std::endl;
  752. res = false;
  753. }
  754. if (kwsys::SystemTools::IsSubDirectory("c:/foo", "c:/") == false) {
  755. std::cerr << "Problem with IsSubDirectory (root - dos): " << std::endl;
  756. res = false;
  757. }
  758. if (kwsys::SystemTools::IsSubDirectory("/foo/bar", "/foo") == false) {
  759. std::cerr << "Problem with IsSubDirectory (deep): " << std::endl;
  760. res = false;
  761. }
  762. if (kwsys::SystemTools::IsSubDirectory("/foo", "/foo") == true) {
  763. std::cerr << "Problem with IsSubDirectory (identity): " << std::endl;
  764. res = false;
  765. }
  766. if (kwsys::SystemTools::IsSubDirectory("/fooo", "/foo") == true) {
  767. std::cerr << "Problem with IsSubDirectory (substring): " << std::endl;
  768. res = false;
  769. }
  770. if (kwsys::SystemTools::IsSubDirectory("/foo/", "/foo") == true) {
  771. std::cerr << "Problem with IsSubDirectory (prepended slash): "
  772. << 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. static bool CheckGetLineFromStreamLongLine()
  818. {
  819. const std::string fileWithLongLine("longlines.txt");
  820. std::string firstLine, secondLine;
  821. // First line: large buffer, containing a carriage return for some reason.
  822. firstLine.assign(2050, ' ');
  823. firstLine += "\rfirst";
  824. secondLine.assign(2050, 'y');
  825. secondLine += "second";
  826. // Create file with long lines.
  827. {
  828. kwsys::ofstream out(fileWithLongLine.c_str(), std::ios::binary);
  829. if (!out) {
  830. std::cerr << "Problem opening for write: " << fileWithLongLine
  831. << std::endl;
  832. return false;
  833. }
  834. out << firstLine << "\r\n\n" << secondLine << "\n";
  835. }
  836. kwsys::ifstream file(fileWithLongLine.c_str(), std::ios::binary);
  837. if (!file) {
  838. std::cerr << "Problem opening: " << fileWithLongLine << std::endl;
  839. return false;
  840. }
  841. std::string line;
  842. bool has_newline = false;
  843. bool result;
  844. // Read first line.
  845. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  846. if (!result || line != firstLine) {
  847. std::cerr << "First line does not match, expected " << firstLine.size()
  848. << " characters, got " << line.size() << std::endl;
  849. return false;
  850. }
  851. if (!has_newline) {
  852. std::cerr << "Expected new line to be read from first line" << std::endl;
  853. return false;
  854. }
  855. // Read empty line.
  856. has_newline = false;
  857. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  858. if (!result || !line.empty()) {
  859. std::cerr << "Expected successful read with an empty line, got "
  860. << line.size() << " characters" << std::endl;
  861. return false;
  862. }
  863. if (!has_newline) {
  864. std::cerr << "Expected new line to be read for an empty line" << std::endl;
  865. return false;
  866. }
  867. // Read second line.
  868. has_newline = false;
  869. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  870. if (!result || line != secondLine) {
  871. std::cerr << "Second line does not match, expected " << secondLine.size()
  872. << " characters, got " << line.size() << std::endl;
  873. return false;
  874. }
  875. if (!has_newline) {
  876. std::cerr << "Expected new line to be read from second line" << std::endl;
  877. return false;
  878. }
  879. return true;
  880. }
  881. static bool writeFile(const char* fileName, const char* data)
  882. {
  883. kwsys::ofstream out(fileName, std::ios::binary);
  884. out << data;
  885. if (!out) {
  886. std::cerr << "Failed to write file: " << fileName << std::endl;
  887. return false;
  888. }
  889. return true;
  890. }
  891. static std::string readFile(const char* fileName)
  892. {
  893. kwsys::ifstream in(fileName, std::ios::binary);
  894. std::stringstream sstr;
  895. sstr << in.rdbuf();
  896. std::string data = sstr.str();
  897. if (!in) {
  898. std::cerr << "Failed to read file: " << fileName << std::endl;
  899. return std::string();
  900. }
  901. return data;
  902. }
  903. struct
  904. {
  905. const char* a;
  906. const char* b;
  907. bool differ;
  908. } diff_test_cases[] = { { "one", "one", false },
  909. { "one", "two", true },
  910. { "", "", false },
  911. { "\n", "\r\n", false },
  912. { "one\n", "one\n", false },
  913. { "one\r\n", "one\n", false },
  914. { "one\n", "one", false },
  915. { "one\ntwo", "one\ntwo", false },
  916. { "one\ntwo", "one\r\ntwo", false } };
  917. static bool CheckTextFilesDiffer()
  918. {
  919. const int num_test_cases =
  920. sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
  921. for (int i = 0; i < num_test_cases; ++i) {
  922. if (!writeFile("file_a", diff_test_cases[i].a) ||
  923. !writeFile("file_b", diff_test_cases[i].b)) {
  924. return false;
  925. }
  926. if (kwsys::SystemTools::TextFilesDiffer("file_a", "file_b") !=
  927. diff_test_cases[i].differ) {
  928. std::cerr << "Incorrect TextFilesDiffer result for test case " << i + 1
  929. << "." << std::endl;
  930. return false;
  931. }
  932. }
  933. return true;
  934. }
  935. static bool CheckCopyFileIfDifferent()
  936. {
  937. bool ret = true;
  938. const int num_test_cases =
  939. sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
  940. for (int i = 0; i < num_test_cases; ++i) {
  941. if (!writeFile("file_a", diff_test_cases[i].a) ||
  942. !writeFile("file_b", diff_test_cases[i].b)) {
  943. return false;
  944. }
  945. const char* cptarget =
  946. i < 4 ? TEST_SYSTEMTOOLS_BINARY_DIR "/file_b" : "file_b";
  947. if (!kwsys::SystemTools::CopyFileIfDifferent("file_a", cptarget)) {
  948. std::cerr << "CopyFileIfDifferent() returned false for test case "
  949. << i + 1 << "." << std::endl;
  950. ret = false;
  951. continue;
  952. }
  953. std::string bdata = readFile("file_b");
  954. if (diff_test_cases[i].a != bdata) {
  955. std::cerr << "Incorrect CopyFileIfDifferent file contents in test case "
  956. << i + 1 << "." << std::endl;
  957. ret = false;
  958. continue;
  959. }
  960. }
  961. if (!kwsys::SystemTools::MakeDirectory("dir_a") ||
  962. !kwsys::SystemTools::MakeDirectory("dir_b")) {
  963. return false;
  964. }
  965. if (!kwsys::SystemTools::CopyFileIfDifferent("dir_a/", "dir_b")) {
  966. ret = false;
  967. }
  968. return ret;
  969. }
  970. int testSystemTools(int, char* [])
  971. {
  972. bool res = true;
  973. int cc;
  974. for (cc = 0; toUnixPaths[cc][0]; cc++) {
  975. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  976. }
  977. // Special check for ~
  978. std::string output;
  979. if (kwsys::SystemTools::GetEnv("HOME", output)) {
  980. output += "/foo bar/lala";
  981. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  982. }
  983. for (cc = 0; checkEscapeChars[cc][0]; cc++) {
  984. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  985. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  986. }
  987. res &= CheckFileOperations();
  988. res &= CheckStringOperations();
  989. res &= CheckEnvironmentOperations();
  990. res &= CheckRelativePaths();
  991. res &= CheckCollapsePath();
  992. res &= CheckGetPath();
  993. res &= CheckFind();
  994. res &= CheckIsSubDirectory();
  995. res &= CheckGetLineFromStream();
  996. res &= CheckGetLineFromStreamLongLine();
  997. res &= CheckGetFilenameName();
  998. res &= CheckTextFilesDiffer();
  999. res &= CheckCopyFileIfDifferent();
  1000. return res ? 0 : 1;
  1001. }