testSystemTools.cxx 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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 <cstdlib> /* free */
  19. #include <cstring> /* strcmp */
  20. #include <iostream>
  21. #include <sstream>
  22. #if defined(_WIN32) && !defined(__CYGWIN__)
  23. # include <io.h> /* _umask (MSVC) */
  24. # ifdef _MSC_VER
  25. # define umask _umask
  26. # endif
  27. # include <windows.h>
  28. #endif
  29. #include <sys/stat.h> /* umask (POSIX), _S_I* constants (Windows) */
  30. // Visual C++ does not define mode_t.
  31. #if defined(_MSC_VER)
  32. typedef unsigned short mode_t;
  33. #endif
  34. static const char* toUnixPaths[][2] = {
  35. { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
  36. { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
  37. { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo/ cal/bin/pa/ sswd" },
  38. { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
  39. { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  40. { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo/ cal/bin/pa/ sswd" },
  41. { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
  42. { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
  43. { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo/ cal/bin/pa/ sswd" },
  44. { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
  45. { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  46. { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo/ cal/bin/pa/ sswd" },
  47. { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
  48. { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
  49. { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo/ cal/bin/pa/ sswd" },
  50. { nullptr, nullptr }
  51. };
  52. static bool CheckConvertToUnixSlashes(std::string const& input,
  53. std::string const& output)
  54. {
  55. std::string result = input;
  56. kwsys::SystemTools::ConvertToUnixSlashes(result);
  57. if (result != output) {
  58. std::cerr << "Problem with ConvertToUnixSlashes - input: " << input
  59. << " output: " << result << " expected: " << output << std::endl;
  60. return false;
  61. }
  62. return true;
  63. }
  64. static const char* checkEscapeChars[][4] = {
  65. { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2" },
  66. { " {} ", "{}", "#", " #{#} " },
  67. { nullptr, nullptr, nullptr, nullptr }
  68. };
  69. static bool CheckEscapeChars(std::string const& input,
  70. const char* chars_to_escape, char escape_char,
  71. std::string const& output)
  72. {
  73. std::string result = kwsys::SystemTools::EscapeChars(
  74. input.c_str(), chars_to_escape, escape_char);
  75. if (result != output) {
  76. std::cerr << "Problem with CheckEscapeChars - input: " << input
  77. << " output: " << result << " expected: " << output << std::endl;
  78. return false;
  79. }
  80. return true;
  81. }
  82. static bool CheckFileOperations()
  83. {
  84. bool res = true;
  85. const std::string testNonExistingFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  86. "/testSystemToolsNonExistingFile");
  87. const std::string testDotFile(TEST_SYSTEMTOOLS_SOURCE_DIR "/.");
  88. const std::string testBinFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  89. "/testSystemTools.bin");
  90. const std::string testTxtFile(TEST_SYSTEMTOOLS_SOURCE_DIR
  91. "/testSystemTools.cxx");
  92. const std::string testNewDir(TEST_SYSTEMTOOLS_BINARY_DIR
  93. "/testSystemToolsNewDir");
  94. const std::string testNewFile(testNewDir + "/testNewFile.txt");
  95. if (kwsys::SystemTools::DetectFileType(testNonExistingFile.c_str()) !=
  96. kwsys::SystemTools::FileTypeUnknown) {
  97. std::cerr << "Problem with DetectFileType - failed to detect type of: "
  98. << testNonExistingFile << std::endl;
  99. res = false;
  100. }
  101. if (kwsys::SystemTools::DetectFileType(testDotFile.c_str()) !=
  102. kwsys::SystemTools::FileTypeUnknown) {
  103. std::cerr << "Problem with DetectFileType - failed to detect type of: "
  104. << testDotFile << std::endl;
  105. res = false;
  106. }
  107. if (kwsys::SystemTools::DetectFileType(testBinFile.c_str()) !=
  108. kwsys::SystemTools::FileTypeBinary) {
  109. std::cerr << "Problem with DetectFileType - failed to detect type of: "
  110. << testBinFile << std::endl;
  111. res = false;
  112. }
  113. if (kwsys::SystemTools::DetectFileType(testTxtFile.c_str()) !=
  114. kwsys::SystemTools::FileTypeText) {
  115. std::cerr << "Problem with DetectFileType - failed to detect type of: "
  116. << testTxtFile << std::endl;
  117. res = false;
  118. }
  119. if (kwsys::SystemTools::FileLength(testBinFile) != 766) {
  120. std::cerr << "Problem with FileLength - incorrect length for: "
  121. << testBinFile << std::endl;
  122. res = false;
  123. }
  124. kwsys::SystemTools::Stat_t buf;
  125. if (kwsys::SystemTools::Stat(testTxtFile.c_str(), &buf) != 0) {
  126. std::cerr << "Problem with Stat - unable to stat text file: "
  127. << testTxtFile << std::endl;
  128. res = false;
  129. }
  130. if (kwsys::SystemTools::Stat(testBinFile, &buf) != 0) {
  131. std::cerr << "Problem with Stat - unable to stat bin file: " << testBinFile
  132. << std::endl;
  133. res = false;
  134. }
  135. if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
  136. std::cerr << "Problem with MakeDirectory for: " << testNewDir << std::endl;
  137. res = false;
  138. }
  139. // calling it again should just return true
  140. if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
  141. std::cerr << "Problem with second call to MakeDirectory for: "
  142. << testNewDir << std::endl;
  143. res = false;
  144. }
  145. // calling with 0 pointer should return false
  146. if (kwsys::SystemTools::MakeDirectory(nullptr)) {
  147. std::cerr << "Problem with MakeDirectory(0)" << std::endl;
  148. res = false;
  149. }
  150. // calling with an empty string should return false
  151. if (kwsys::SystemTools::MakeDirectory(std::string())) {
  152. std::cerr << "Problem with MakeDirectory(std::string())" << std::endl;
  153. res = false;
  154. }
  155. // check existence
  156. if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
  157. std::cerr << "Problem with FileExists as C string and not file for: "
  158. << testNewDir << std::endl;
  159. res = false;
  160. }
  161. // check existence
  162. if (!kwsys::SystemTools::PathExists(testNewDir)) {
  163. std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
  164. res = false;
  165. }
  166. // remove it
  167. if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
  168. std::cerr << "Problem with RemoveADirectory for: " << testNewDir
  169. << std::endl;
  170. res = false;
  171. }
  172. // check existence
  173. if (kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
  174. std::cerr << "After RemoveADirectory: "
  175. << "Problem with FileExists as C string and not file for: "
  176. << testNewDir << std::endl;
  177. res = false;
  178. }
  179. // check existence
  180. if (kwsys::SystemTools::PathExists(testNewDir)) {
  181. std::cerr << "After RemoveADirectory: "
  182. << "Problem with PathExists for: " << testNewDir << std::endl;
  183. res = false;
  184. }
  185. // create it using the char* version
  186. if (!kwsys::SystemTools::MakeDirectory(testNewDir.c_str())) {
  187. std::cerr << "Problem with second call to MakeDirectory as C string for: "
  188. << testNewDir << std::endl;
  189. res = false;
  190. }
  191. if (!kwsys::SystemTools::Touch(testNewFile, true)) {
  192. std::cerr << "Problem with Touch for: " << testNewFile << std::endl;
  193. res = false;
  194. }
  195. // calling MakeDirectory with something that is no file should fail
  196. if (kwsys::SystemTools::MakeDirectory(testNewFile)) {
  197. std::cerr << "Problem with to MakeDirectory for: " << testNewFile
  198. << std::endl;
  199. res = false;
  200. }
  201. // calling with 0 pointer should return false
  202. if (kwsys::SystemTools::FileExists(nullptr)) {
  203. std::cerr << "Problem with FileExists(0)" << std::endl;
  204. res = false;
  205. }
  206. if (kwsys::SystemTools::FileExists(nullptr, true)) {
  207. std::cerr << "Problem with FileExists(0) as file" << std::endl;
  208. res = false;
  209. }
  210. // calling with an empty string should return false
  211. if (kwsys::SystemTools::FileExists(std::string())) {
  212. std::cerr << "Problem with FileExists(std::string())" << std::endl;
  213. res = false;
  214. }
  215. // FileExists(x, true) should return false on a directory
  216. if (kwsys::SystemTools::FileExists(testNewDir, true)) {
  217. std::cerr << "Problem with FileExists as file for: " << testNewDir
  218. << std::endl;
  219. res = false;
  220. }
  221. if (kwsys::SystemTools::FileExists(testNewDir.c_str(), true)) {
  222. std::cerr << "Problem with FileExists as C string and file for: "
  223. << testNewDir << std::endl;
  224. res = false;
  225. }
  226. // FileExists(x, false) should return true even on a directory
  227. if (!kwsys::SystemTools::FileExists(testNewDir, false)) {
  228. std::cerr << "Problem with FileExists as not file for: " << testNewDir
  229. << std::endl;
  230. res = false;
  231. }
  232. if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
  233. std::cerr << "Problem with FileExists as C string and not file for: "
  234. << testNewDir << std::endl;
  235. res = false;
  236. }
  237. // should work, was created as new file before
  238. if (!kwsys::SystemTools::FileExists(testNewFile)) {
  239. std::cerr << "Problem with FileExists for: " << testNewFile << std::endl;
  240. res = false;
  241. }
  242. if (!kwsys::SystemTools::FileExists(testNewFile.c_str())) {
  243. std::cerr << "Problem with FileExists as C string for: " << testNewFile
  244. << std::endl;
  245. res = false;
  246. }
  247. if (!kwsys::SystemTools::FileExists(testNewFile, true)) {
  248. std::cerr << "Problem with FileExists as file for: " << testNewFile
  249. << std::endl;
  250. res = false;
  251. }
  252. if (!kwsys::SystemTools::FileExists(testNewFile.c_str(), true)) {
  253. std::cerr << "Problem with FileExists as C string and file for: "
  254. << testNewFile << std::endl;
  255. res = false;
  256. }
  257. // calling with an empty string should return false
  258. if (kwsys::SystemTools::PathExists(std::string())) {
  259. std::cerr << "Problem with PathExists(std::string())" << std::endl;
  260. res = false;
  261. }
  262. // PathExists(x) should return true on a directory
  263. if (!kwsys::SystemTools::PathExists(testNewDir)) {
  264. std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
  265. res = false;
  266. }
  267. // should work, was created as new file before
  268. if (!kwsys::SystemTools::PathExists(testNewFile)) {
  269. std::cerr << "Problem with PathExists for: " << testNewFile << std::endl;
  270. res = false;
  271. }
  272. std::cerr << std::oct;
  273. // Reset umask
  274. #ifdef __MSYS__
  275. mode_t fullMask = S_IWRITE;
  276. #elif defined(_WIN32) && !defined(__CYGWIN__)
  277. // NOTE: Windows doesn't support toggling _S_IREAD.
  278. mode_t fullMask = _S_IWRITE;
  279. #else
  280. // On a normal POSIX platform, we can toggle all permissions.
  281. mode_t fullMask = S_IRWXU | S_IRWXG | S_IRWXO;
  282. #endif
  283. // Test file permissions without umask
  284. mode_t origPerm, thisPerm;
  285. if (!kwsys::SystemTools::GetPermissions(testNewFile, origPerm)) {
  286. std::cerr << "Problem with GetPermissions (1) for: " << testNewFile
  287. << std::endl;
  288. res = false;
  289. }
  290. if (!kwsys::SystemTools::SetPermissions(testNewFile, 0)) {
  291. std::cerr << "Problem with SetPermissions (1) for: " << testNewFile
  292. << std::endl;
  293. res = false;
  294. }
  295. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  296. std::cerr << "Problem with GetPermissions (2) for: " << testNewFile
  297. << std::endl;
  298. res = false;
  299. }
  300. if ((thisPerm & fullMask) != 0) {
  301. std::cerr << "SetPermissions failed to set permissions (1) for: "
  302. << testNewFile << ": actual = " << thisPerm
  303. << "; expected = " << 0 << std::endl;
  304. res = false;
  305. }
  306. // While we're at it, check proper TestFileAccess functionality.
  307. bool do_write_test = true;
  308. #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
  309. defined(__NetBSD__) || defined(__DragonFly__)
  310. // If we are running as root on POSIX-ish systems (Linux and the BSDs,
  311. // at least), ignore this check, as root can always write to files.
  312. do_write_test = (getuid() != 0);
  313. #endif
  314. if (do_write_test &&
  315. kwsys::SystemTools::TestFileAccess(testNewFile,
  316. kwsys::TEST_FILE_WRITE)) {
  317. std::cerr
  318. << "TestFileAccess incorrectly indicated that this is a writable file:"
  319. << testNewFile << std::endl;
  320. res = false;
  321. }
  322. if (!kwsys::SystemTools::TestFileAccess(testNewFile, kwsys::TEST_FILE_OK)) {
  323. std::cerr
  324. << "TestFileAccess incorrectly indicated that this file does not exist:"
  325. << testNewFile << std::endl;
  326. res = false;
  327. }
  328. // Test restoring/setting full permissions.
  329. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask)) {
  330. std::cerr << "Problem with SetPermissions (2) for: " << testNewFile
  331. << std::endl;
  332. res = false;
  333. }
  334. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  335. std::cerr << "Problem with GetPermissions (3) for: " << testNewFile
  336. << std::endl;
  337. res = false;
  338. }
  339. if ((thisPerm & fullMask) != fullMask) {
  340. std::cerr << "SetPermissions failed to set permissions (2) for: "
  341. << testNewFile << ": actual = " << thisPerm
  342. << "; expected = " << fullMask << std::endl;
  343. res = false;
  344. }
  345. mode_t orig_umask = umask(fullMask);
  346. // Test setting file permissions while honoring umask
  347. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask, true)) {
  348. std::cerr << "Problem with SetPermissions (3) for: " << testNewFile
  349. << std::endl;
  350. res = false;
  351. }
  352. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  353. std::cerr << "Problem with GetPermissions (4) for: " << testNewFile
  354. << std::endl;
  355. res = false;
  356. }
  357. if ((thisPerm & fullMask) != 0) {
  358. std::cerr << "SetPermissions failed to honor umask for: " << testNewFile
  359. << ": actual = " << thisPerm << "; expected = " << 0
  360. << std::endl;
  361. res = false;
  362. }
  363. // Restore umask
  364. umask(orig_umask);
  365. // Restore file permissions
  366. if (!kwsys::SystemTools::SetPermissions(testNewFile, origPerm)) {
  367. std::cerr << "Problem with SetPermissions (4) for: " << testNewFile
  368. << std::endl;
  369. res = false;
  370. }
  371. // Remove the test file
  372. if (!kwsys::SystemTools::RemoveFile(testNewFile)) {
  373. std::cerr << "Problem with RemoveFile: " << testNewFile << std::endl;
  374. res = false;
  375. }
  376. std::string const testFileMissing(testNewDir + "/testMissingFile.txt");
  377. if (!kwsys::SystemTools::RemoveFile(testFileMissing)) {
  378. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  379. std::cerr << "RemoveFile(\"" << testFileMissing << "\") failed: " << msg
  380. << "\n";
  381. res = false;
  382. }
  383. std::string const testFileMissingDir(testNewDir + "/missing/file.txt");
  384. if (!kwsys::SystemTools::RemoveFile(testFileMissingDir)) {
  385. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  386. std::cerr << "RemoveFile(\"" << testFileMissingDir << "\") failed: " << msg
  387. << "\n";
  388. res = false;
  389. }
  390. std::string const testBadSymlink(testNewDir + "/badSymlink.txt");
  391. std::string const testBadSymlinkTgt(testNewDir + "/missing/symlinkTgt.txt");
  392. kwsys::Status const symlinkStatus =
  393. kwsys::SystemTools::CreateSymlink(testBadSymlinkTgt, testBadSymlink);
  394. #if defined(_WIN32)
  395. // Under Windows, the user may not have enough privileges to create symlinks
  396. if (symlinkStatus.GetWindows() != ERROR_PRIVILEGE_NOT_HELD)
  397. #endif
  398. {
  399. if (!symlinkStatus.IsSuccess()) {
  400. std::cerr << "CreateSymlink for: " << testBadSymlink << " -> "
  401. << testBadSymlinkTgt
  402. << " failed: " << symlinkStatus.GetString() << std::endl;
  403. res = false;
  404. }
  405. if (!kwsys::SystemTools::Touch(testBadSymlink, false)) {
  406. std::cerr << "Problem with Touch (no create) for: " << testBadSymlink
  407. << std::endl;
  408. res = false;
  409. }
  410. }
  411. if (!kwsys::SystemTools::Touch(testNewDir, false)) {
  412. std::cerr << "Problem with Touch (no create) for: " << testNewDir
  413. << std::endl;
  414. res = false;
  415. }
  416. kwsys::SystemTools::Touch(testNewFile, true);
  417. if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
  418. std::cerr << "Problem with RemoveADirectory for: " << testNewDir
  419. << std::endl;
  420. res = false;
  421. }
  422. #ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  423. // Perform the same file and directory creation and deletion tests but
  424. // with paths > 256 characters in length.
  425. const std::string testNewLongDir(
  426. TEST_SYSTEMTOOLS_BINARY_DIR
  427. "/"
  428. "012345678901234567890123456789012345678901234567890123456789"
  429. "012345678901234567890123456789012345678901234567890123456789"
  430. "012345678901234567890123456789012345678901234567890123456789"
  431. "012345678901234567890123456789012345678901234567890123456789"
  432. "01234567890123");
  433. const std::string testNewLongFile(
  434. testNewLongDir +
  435. "/"
  436. "012345678901234567890123456789012345678901234567890123456789"
  437. "012345678901234567890123456789012345678901234567890123456789"
  438. "012345678901234567890123456789012345678901234567890123456789"
  439. "012345678901234567890123456789012345678901234567890123456789"
  440. "0123456789.txt");
  441. if (!kwsys::SystemTools::MakeDirectory(testNewLongDir)) {
  442. std::cerr << "Problem with MakeDirectory for: " << testNewLongDir
  443. << std::endl;
  444. res = false;
  445. }
  446. if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true)) {
  447. std::cerr << "Problem with Touch for: " << testNewLongFile << std::endl;
  448. res = false;
  449. }
  450. if (!kwsys::SystemTools::RemoveFile(testNewLongFile)) {
  451. std::cerr << "Problem with RemoveFile: " << testNewLongFile << std::endl;
  452. res = false;
  453. }
  454. kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
  455. if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir)) {
  456. std::cerr << "Problem with RemoveADirectory for: " << testNewLongDir
  457. << std::endl;
  458. res = false;
  459. }
  460. #endif
  461. std::cerr << std::dec;
  462. return res;
  463. }
  464. static bool CheckStringOperations()
  465. {
  466. bool res = true;
  467. std::string test = "mary had a little lamb.";
  468. if (kwsys::SystemTools::CapitalizedWords(test) !=
  469. "Mary Had A Little Lamb.") {
  470. std::cerr << "Problem with CapitalizedWords " << '"' << test << '"'
  471. << std::endl;
  472. res = false;
  473. }
  474. test = "Mary Had A Little Lamb.";
  475. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  476. "mary had a little lamb.") {
  477. std::cerr << "Problem with UnCapitalizedWords " << '"' << test << '"'
  478. << std::endl;
  479. res = false;
  480. }
  481. test = "MaryHadTheLittleLamb.";
  482. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  483. "Mary Had The Little Lamb.") {
  484. std::cerr << "Problem with AddSpaceBetweenCapitalizedWords " << '"' << test
  485. << '"' << std::endl;
  486. res = false;
  487. }
  488. char* cres =
  489. kwsys::SystemTools::AppendStrings("Mary Had A", " Little Lamb.");
  490. if (strcmp(cres, "Mary Had A Little Lamb.") != 0) {
  491. std::cerr << "Problem with AppendStrings "
  492. << "\"Mary Had A\" \" Little Lamb.\"" << std::endl;
  493. res = false;
  494. }
  495. delete[] cres;
  496. cres = kwsys::SystemTools::AppendStrings("Mary Had", " A ", "Little Lamb.");
  497. if (strcmp(cres, "Mary Had A Little Lamb.") != 0) {
  498. std::cerr << "Problem with AppendStrings "
  499. << "\"Mary Had\" \" A \" \"Little Lamb.\"" << std::endl;
  500. res = false;
  501. }
  502. delete[] cres;
  503. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.", 'a') != 3) {
  504. std::cerr << "Problem with CountChar "
  505. << "\"Mary Had A Little Lamb.\"" << std::endl;
  506. res = false;
  507. }
  508. cres = kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.", "aeiou");
  509. if (strcmp(cres, "Mry Hd A Lttl Lmb.") != 0) {
  510. std::cerr << "Problem with RemoveChars "
  511. << "\"Mary Had A Little Lamb.\"" << std::endl;
  512. res = false;
  513. }
  514. delete[] cres;
  515. cres = kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  516. if (strcmp(cres, "A") != 0) {
  517. std::cerr << "Problem with RemoveCharsButUpperHex "
  518. << "\"Mary Had A Little Lamb.\"" << std::endl;
  519. res = false;
  520. }
  521. delete[] cres;
  522. char* cres2 = strdup("Mary Had A Little Lamb.");
  523. kwsys::SystemTools::ReplaceChars(cres2, "aeiou", 'X');
  524. if (strcmp(cres2, "MXry HXd A LXttlX LXmb.") != 0) {
  525. std::cerr << "Problem with ReplaceChars "
  526. << "\"Mary Had A Little Lamb.\"" << std::endl;
  527. res = false;
  528. }
  529. free(cres2);
  530. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  531. "Mary ")) {
  532. std::cerr << "Problem with StringStartsWith "
  533. << "\"Mary Had A Little Lamb.\"" << std::endl;
  534. res = false;
  535. }
  536. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  537. " Lamb.")) {
  538. std::cerr << "Problem with StringEndsWith "
  539. << "\"Mary Had A Little Lamb.\"" << std::endl;
  540. res = false;
  541. }
  542. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  543. if (strcmp(cres, "Mary Had A Little Lamb.") != 0) {
  544. std::cerr << "Problem with DuplicateString "
  545. << "\"Mary Had A Little Lamb.\"" << std::endl;
  546. res = false;
  547. }
  548. delete[] cres;
  549. test = "Mary Had A Little Lamb.";
  550. if (kwsys::SystemTools::CropString(test, 13) != "Mary ...Lamb.") {
  551. std::cerr << "Problem with CropString "
  552. << "\"Mary Had A Little Lamb.\"" << std::endl;
  553. res = false;
  554. }
  555. std::vector<std::string> lines;
  556. kwsys::SystemTools::Split("Mary Had A Little Lamb.", lines, ' ');
  557. if (lines[0] != "Mary" || lines[1] != "Had" || lines[2] != "A" ||
  558. lines[3] != "Little" || lines[4] != "Lamb.") {
  559. std::cerr << "Problem with Split "
  560. << "\"Mary Had A Little Lamb.\"" << std::endl;
  561. res = false;
  562. }
  563. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  564. "L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  565. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  566. std::cerr << "Problem with ConvertToWindowsOutputPath "
  567. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  568. res = false;
  569. }
  570. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  571. "//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  572. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  573. std::cerr << "Problem with ConvertToWindowsOutputPath "
  574. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  575. << std::endl;
  576. res = false;
  577. }
  578. if (kwsys::SystemTools::ConvertToUnixOutputPath(
  579. "//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  580. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo") {
  581. std::cerr << "Problem with ConvertToUnixOutputPath "
  582. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  583. res = false;
  584. }
  585. return res;
  586. }
  587. static bool CheckPutEnv(const std::string& env, const char* name,
  588. const char* value)
  589. {
  590. if (!kwsys::SystemTools::PutEnv(env)) {
  591. std::cerr << "PutEnv(\"" << env << "\") failed!" << std::endl;
  592. return false;
  593. }
  594. std::string v = "(null)";
  595. kwsys::SystemTools::GetEnv(name, v);
  596. if (v != value) {
  597. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v << "\", not \""
  598. << value << "\"!" << std::endl;
  599. return false;
  600. }
  601. return true;
  602. }
  603. static bool CheckUnPutEnv(const char* env, const char* name)
  604. {
  605. if (!kwsys::SystemTools::UnPutEnv(env)) {
  606. std::cerr << "UnPutEnv(\"" << env << "\") failed!" << std::endl;
  607. return false;
  608. }
  609. std::string v;
  610. if (kwsys::SystemTools::GetEnv(name, v)) {
  611. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v
  612. << "\", not (null)!" << std::endl;
  613. return false;
  614. }
  615. return true;
  616. }
  617. static bool CheckEnvironmentOperations()
  618. {
  619. bool res = true;
  620. res &= CheckPutEnv("A=B", "A", "B");
  621. res &= CheckPutEnv("B=C", "B", "C");
  622. res &= CheckPutEnv("C=D", "C", "D");
  623. res &= CheckPutEnv("D=E", "D", "E");
  624. res &= CheckUnPutEnv("A", "A");
  625. res &= CheckUnPutEnv("B=", "B");
  626. res &= CheckUnPutEnv("C=D", "C");
  627. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  628. return res;
  629. }
  630. static bool CheckRelativePath(const std::string& local,
  631. const std::string& remote,
  632. const std::string& expected)
  633. {
  634. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  635. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  636. std::cerr << "RelativePath(" << local << ", " << remote << ") yielded "
  637. << result << " instead of " << expected << std::endl;
  638. return false;
  639. }
  640. return true;
  641. }
  642. static bool CheckRelativePaths()
  643. {
  644. bool res = true;
  645. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  646. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  647. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  648. res &=
  649. CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  650. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  651. return res;
  652. }
  653. static bool CheckCollapsePath(const std::string& path,
  654. const std::string& expected,
  655. const char* base = nullptr)
  656. {
  657. std::string result = kwsys::SystemTools::CollapseFullPath(path, base);
  658. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  659. std::cerr << "CollapseFullPath(" << path << ") yielded " << result
  660. << " instead of " << expected << std::endl;
  661. return false;
  662. }
  663. return true;
  664. }
  665. static bool CheckCollapsePath()
  666. {
  667. bool res = true;
  668. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  669. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  670. res &= CheckCollapsePath("/usr/share/../lib", "/usr/lib");
  671. res &= CheckCollapsePath("/usr/share/./lib", "/usr/share/lib");
  672. res &= CheckCollapsePath("/usr/share/../../lib", "/lib");
  673. res &= CheckCollapsePath("/usr/share/.././../lib", "/lib");
  674. res &= CheckCollapsePath("/../lib", "/lib");
  675. res &= CheckCollapsePath("/../lib/", "/lib");
  676. res &= CheckCollapsePath("/", "/");
  677. res &= CheckCollapsePath("C:/", "C:/");
  678. res &= CheckCollapsePath("C:/../", "C:/");
  679. res &= CheckCollapsePath("C:/../../", "C:/");
  680. res &= CheckCollapsePath("../b", "../../b", "../");
  681. res &= CheckCollapsePath("../a/../b", "../b", "../rel");
  682. res &= CheckCollapsePath("a/../b", "../rel/b", "../rel");
  683. return res;
  684. }
  685. static std::string StringVectorToString(const std::vector<std::string>& vec)
  686. {
  687. std::stringstream ss;
  688. ss << "vector(";
  689. for (auto i = vec.begin(); i != vec.end(); ++i) {
  690. if (i != vec.begin()) {
  691. ss << ", ";
  692. }
  693. ss << *i;
  694. }
  695. ss << ")";
  696. return ss.str();
  697. }
  698. static bool CheckGetPath()
  699. {
  700. const char* envName = "S";
  701. #ifdef _WIN32
  702. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  703. #else
  704. const char* envValue = "/Somewhere/something:/tmp";
  705. #endif
  706. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  707. std::vector<std::string> originalPaths;
  708. originalPaths.emplace_back(registryPath);
  709. std::vector<std::string> expectedPaths;
  710. expectedPaths.emplace_back(registryPath);
  711. #ifdef _WIN32
  712. expectedPaths.push_back("C:/Somewhere/something");
  713. expectedPaths.push_back("D:/Temp");
  714. #else
  715. expectedPaths.emplace_back("/Somewhere/something");
  716. expectedPaths.emplace_back("/tmp");
  717. #endif
  718. bool res = true;
  719. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  720. std::vector<std::string> paths = originalPaths;
  721. kwsys::SystemTools::GetPath(paths, envName);
  722. if (paths != expectedPaths) {
  723. std::cerr << "GetPath(" << StringVectorToString(originalPaths) << ", "
  724. << envName << ") yielded " << StringVectorToString(paths)
  725. << " instead of " << StringVectorToString(expectedPaths)
  726. << std::endl;
  727. res = false;
  728. }
  729. res &= CheckUnPutEnv(envName, envName);
  730. return res;
  731. }
  732. static bool CheckGetFilenameName()
  733. {
  734. const char* windowsFilepath = "C:\\somewhere\\something";
  735. const char* unixFilepath = "/somewhere/something";
  736. #if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
  737. std::string expectedWindowsFilename = "something";
  738. #else
  739. std::string expectedWindowsFilename = "C:\\somewhere\\something";
  740. #endif
  741. std::string expectedUnixFilename = "something";
  742. bool res = true;
  743. std::string filename = kwsys::SystemTools::GetFilenameName(windowsFilepath);
  744. if (filename != expectedWindowsFilename) {
  745. std::cerr << "GetFilenameName(" << windowsFilepath << ") yielded "
  746. << filename << " instead of " << expectedWindowsFilename
  747. << std::endl;
  748. res = false;
  749. }
  750. filename = kwsys::SystemTools::GetFilenameName(unixFilepath);
  751. if (filename != expectedUnixFilename) {
  752. std::cerr << "GetFilenameName(" << unixFilepath << ") yielded " << filename
  753. << " instead of " << expectedUnixFilename << std::endl;
  754. res = false;
  755. }
  756. return res;
  757. }
  758. static bool CheckFind()
  759. {
  760. bool res = true;
  761. const std::string testFindFileName("testFindFile.txt");
  762. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/" +
  763. testFindFileName);
  764. if (!kwsys::SystemTools::Touch(testFindFile, true)) {
  765. std::cerr << "Problem with Touch for: " << testFindFile << std::endl;
  766. // abort here as the existence of the file only makes the test meaningful
  767. return false;
  768. }
  769. std::vector<std::string> searchPaths;
  770. searchPaths.emplace_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  771. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true)
  772. .empty()) {
  773. std::cerr << "Problem with FindFile without system paths for: "
  774. << testFindFileName << std::endl;
  775. res = false;
  776. }
  777. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, false)
  778. .empty()) {
  779. std::cerr << "Problem with FindFile with system paths for: "
  780. << testFindFileName << std::endl;
  781. res = false;
  782. }
  783. return res;
  784. }
  785. static bool CheckIsSubDirectory()
  786. {
  787. bool res = true;
  788. if (kwsys::SystemTools::IsSubDirectory("/foo", "/") == false) {
  789. std::cerr << "Problem with IsSubDirectory (root - unix): " << std::endl;
  790. res = false;
  791. }
  792. if (kwsys::SystemTools::IsSubDirectory("c:/foo", "c:/") == false) {
  793. std::cerr << "Problem with IsSubDirectory (root - dos): " << std::endl;
  794. res = false;
  795. }
  796. if (kwsys::SystemTools::IsSubDirectory("/foo/bar", "/foo") == false) {
  797. std::cerr << "Problem with IsSubDirectory (deep): " << std::endl;
  798. res = false;
  799. }
  800. if (kwsys::SystemTools::IsSubDirectory("/foo", "/foo") == true) {
  801. std::cerr << "Problem with IsSubDirectory (identity): " << std::endl;
  802. res = false;
  803. }
  804. if (kwsys::SystemTools::IsSubDirectory("/fooo", "/foo") == true) {
  805. std::cerr << "Problem with IsSubDirectory (substring): " << std::endl;
  806. res = false;
  807. }
  808. if (kwsys::SystemTools::IsSubDirectory("/foo/", "/foo") == true) {
  809. std::cerr << "Problem with IsSubDirectory (prepended slash): "
  810. << std::endl;
  811. res = false;
  812. }
  813. return res;
  814. }
  815. static bool CheckGetLineFromStream()
  816. {
  817. const std::string fileWithFiveCharsOnFirstLine(TEST_SYSTEMTOOLS_SOURCE_DIR
  818. "/README.rst");
  819. kwsys::ifstream file(fileWithFiveCharsOnFirstLine.c_str(), std::ios::in);
  820. if (!file) {
  821. std::cerr << "Problem opening: " << fileWithFiveCharsOnFirstLine
  822. << std::endl;
  823. return false;
  824. }
  825. std::string line;
  826. bool has_newline = false;
  827. bool result;
  828. file.seekg(0, std::ios::beg);
  829. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  830. if (!result || line.size() != 5) {
  831. std::cerr << "First line does not have five characters: " << line.size()
  832. << std::endl;
  833. return false;
  834. }
  835. file.seekg(0, std::ios::beg);
  836. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  837. if (!result || line.size() != 5) {
  838. std::cerr << "First line does not have five characters after rewind: "
  839. << line.size() << std::endl;
  840. return false;
  841. }
  842. bool ret = true;
  843. for (size_t size = 1; size <= 5; ++size) {
  844. file.seekg(0, std::ios::beg);
  845. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  846. static_cast<long>(size));
  847. if (!result || line.size() != size) {
  848. std::cerr << "Should have read " << size << " characters but got "
  849. << line.size() << std::endl;
  850. ret = false;
  851. }
  852. }
  853. return ret;
  854. }
  855. static bool CheckGetLineFromStreamLongLine()
  856. {
  857. const std::string fileWithLongLine("longlines.txt");
  858. std::string firstLine, secondLine;
  859. // First line: large buffer, containing a carriage return for some reason.
  860. firstLine.assign(2050, ' ');
  861. firstLine += "\rfirst";
  862. secondLine.assign(2050, 'y');
  863. secondLine += "second";
  864. // Create file with long lines.
  865. {
  866. kwsys::ofstream out(fileWithLongLine.c_str(), std::ios::binary);
  867. if (!out) {
  868. std::cerr << "Problem opening for write: " << fileWithLongLine
  869. << std::endl;
  870. return false;
  871. }
  872. out << firstLine << "\r\n\n" << secondLine << "\n";
  873. }
  874. kwsys::ifstream file(fileWithLongLine.c_str(), std::ios::binary);
  875. if (!file) {
  876. std::cerr << "Problem opening: " << fileWithLongLine << std::endl;
  877. return false;
  878. }
  879. std::string line;
  880. bool has_newline = false;
  881. bool result;
  882. // Read first line.
  883. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  884. if (!result || line != firstLine) {
  885. std::cerr << "First line does not match, expected " << firstLine.size()
  886. << " characters, got " << line.size() << std::endl;
  887. return false;
  888. }
  889. if (!has_newline) {
  890. std::cerr << "Expected new line to be read from first line" << std::endl;
  891. return false;
  892. }
  893. // Read empty line.
  894. has_newline = false;
  895. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  896. if (!result || !line.empty()) {
  897. std::cerr << "Expected successful read with an empty line, got "
  898. << line.size() << " characters" << std::endl;
  899. return false;
  900. }
  901. if (!has_newline) {
  902. std::cerr << "Expected new line to be read for an empty line" << std::endl;
  903. return false;
  904. }
  905. // Read second line.
  906. has_newline = false;
  907. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  908. if (!result || line != secondLine) {
  909. std::cerr << "Second line does not match, expected " << secondLine.size()
  910. << " characters, got " << line.size() << std::endl;
  911. return false;
  912. }
  913. if (!has_newline) {
  914. std::cerr << "Expected new line to be read from second line" << std::endl;
  915. return false;
  916. }
  917. return true;
  918. }
  919. static bool writeFile(const char* fileName, const char* data)
  920. {
  921. kwsys::ofstream out(fileName, std::ios::binary);
  922. out << data;
  923. if (!out) {
  924. std::cerr << "Failed to write file: " << fileName << std::endl;
  925. return false;
  926. }
  927. return true;
  928. }
  929. static std::string readFile(const char* fileName)
  930. {
  931. kwsys::ifstream in(fileName, std::ios::binary);
  932. std::stringstream sstr;
  933. sstr << in.rdbuf();
  934. std::string data = sstr.str();
  935. if (!in) {
  936. std::cerr << "Failed to read file: " << fileName << std::endl;
  937. return std::string();
  938. }
  939. return data;
  940. }
  941. struct
  942. {
  943. const char* a;
  944. const char* b;
  945. bool differ;
  946. } diff_test_cases[] = { { "one", "one", false },
  947. { "one", "two", true },
  948. { "", "", false },
  949. { "\n", "\r\n", false },
  950. { "one\n", "one\n", false },
  951. { "one\r\n", "one\n", false },
  952. { "one\n", "one", false },
  953. { "one\ntwo", "one\ntwo", false },
  954. { "one\ntwo", "one\r\ntwo", false } };
  955. static bool CheckTextFilesDiffer()
  956. {
  957. const int num_test_cases =
  958. sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
  959. for (int i = 0; i < num_test_cases; ++i) {
  960. if (!writeFile("file_a", diff_test_cases[i].a) ||
  961. !writeFile("file_b", diff_test_cases[i].b)) {
  962. return false;
  963. }
  964. if (kwsys::SystemTools::TextFilesDiffer("file_a", "file_b") !=
  965. diff_test_cases[i].differ) {
  966. std::cerr << "Incorrect TextFilesDiffer result for test case " << i + 1
  967. << "." << std::endl;
  968. return false;
  969. }
  970. }
  971. return true;
  972. }
  973. static bool CheckCopyFileIfDifferent()
  974. {
  975. bool ret = true;
  976. const int num_test_cases =
  977. sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
  978. for (int i = 0; i < num_test_cases; ++i) {
  979. if (!writeFile("file_a", diff_test_cases[i].a) ||
  980. !writeFile("file_b", diff_test_cases[i].b)) {
  981. return false;
  982. }
  983. const char* cptarget =
  984. i < 4 ? TEST_SYSTEMTOOLS_BINARY_DIR "/file_b" : "file_b";
  985. if (!kwsys::SystemTools::CopyFileIfDifferent("file_a", cptarget)) {
  986. std::cerr << "CopyFileIfDifferent() returned false for test case "
  987. << i + 1 << "." << std::endl;
  988. ret = false;
  989. continue;
  990. }
  991. std::string bdata = readFile(cptarget);
  992. if (diff_test_cases[i].a != bdata) {
  993. std::cerr << "Incorrect CopyFileIfDifferent file contents in test case "
  994. << i + 1 << "." << std::endl;
  995. ret = false;
  996. continue;
  997. }
  998. }
  999. if (!kwsys::SystemTools::MakeDirectory("dir_a") ||
  1000. !kwsys::SystemTools::MakeDirectory("dir_b")) {
  1001. return false;
  1002. }
  1003. if (!kwsys::SystemTools::CopyFileIfDifferent("dir_a/", "dir_b")) {
  1004. ret = false;
  1005. }
  1006. return ret;
  1007. }
  1008. static bool CheckURLParsing()
  1009. {
  1010. bool ret = true;
  1011. std::string url = "http://user:pw@hostname:42/full/url.com";
  1012. std::string protocol, username, password, hostname, dataport, database;
  1013. kwsys::SystemTools::ParseURL(url, protocol, username, password, hostname,
  1014. dataport, database);
  1015. if (protocol != "http" || username != "user" || password != "pw" ||
  1016. hostname != "hostname" || dataport != "42" ||
  1017. database != "full/url.com") {
  1018. std::cerr << "Incorrect URL parsing" << std::endl;
  1019. ret = false;
  1020. }
  1021. std::string uri =
  1022. "file://hostname/path/to/"
  1023. "a%20file%20with%20str%C3%A0ng%C3%A8%20ch%40r%20and%20s%C2%B5aces";
  1024. kwsys::SystemTools::ParseURL(uri, protocol, username, password, hostname,
  1025. dataport, database, true);
  1026. if (protocol != "file" || hostname != "hostname" ||
  1027. database != "path/to/a file with stràngè ch@r and sµaces") {
  1028. std::cerr << "Incorrect URL parsing or decoding" << std::endl;
  1029. ret = false;
  1030. }
  1031. return ret;
  1032. }
  1033. static bool CheckSplitString()
  1034. {
  1035. bool ret = true;
  1036. auto check_split = [](std::string const& input,
  1037. std::initializer_list<const char*> expected) -> bool {
  1038. auto const components = kwsys::SystemTools::SplitString(input, '/');
  1039. if (components.size() != expected.size()) {
  1040. std::cerr << "Incorrect split count for " << input << ": "
  1041. << components.size() << std::endl;
  1042. return false;
  1043. }
  1044. size_t i = 0;
  1045. for (auto& part : expected) {
  1046. if (components[i] != part) {
  1047. std::cerr << "Incorrect split component " << i << " for " << input
  1048. << ": " << components[i] << std::endl;
  1049. return false;
  1050. }
  1051. ++i;
  1052. }
  1053. return true;
  1054. };
  1055. // No separators
  1056. ret &= check_split("nosep", { "nosep" });
  1057. // Simple
  1058. ret &= check_split("first/second", { "first", "second" });
  1059. // Separator at beginning
  1060. ret &= check_split("/starts/sep", { "", "starts", "sep" });
  1061. // Separator at end
  1062. ret &= check_split("ends/sep/", { "ends", "sep", "" });
  1063. return ret;
  1064. }
  1065. int testSystemTools(int, char* [])
  1066. {
  1067. bool res = true;
  1068. int cc;
  1069. for (cc = 0; toUnixPaths[cc][0]; cc++) {
  1070. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  1071. }
  1072. // Special check for ~
  1073. std::string output;
  1074. if (kwsys::SystemTools::GetEnv("HOME", output)) {
  1075. output += "/foo bar/lala";
  1076. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  1077. }
  1078. for (cc = 0; checkEscapeChars[cc][0]; cc++) {
  1079. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  1080. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  1081. }
  1082. res &= CheckFileOperations();
  1083. res &= CheckStringOperations();
  1084. res &= CheckEnvironmentOperations();
  1085. res &= CheckRelativePaths();
  1086. res &= CheckCollapsePath();
  1087. res &= CheckGetPath();
  1088. res &= CheckFind();
  1089. res &= CheckIsSubDirectory();
  1090. res &= CheckGetLineFromStream();
  1091. res &= CheckGetLineFromStreamLongLine();
  1092. res &= CheckGetFilenameName();
  1093. res &= CheckTextFilesDiffer();
  1094. res &= CheckCopyFileIfDifferent();
  1095. res &= CheckURLParsing();
  1096. res &= CheckSplitString();
  1097. return res ? 0 : 1;
  1098. }