testSystemTools.cxx 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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. mode_t testPerm = S_IREAD;
  277. #elif defined(_WIN32) && !defined(__CYGWIN__)
  278. // NOTE: Windows doesn't support toggling _S_IREAD.
  279. mode_t fullMask = _S_IWRITE;
  280. mode_t testPerm = 0;
  281. #else
  282. // On a normal POSIX platform, we can toggle all permissions.
  283. mode_t fullMask = S_IRWXU | S_IRWXG | S_IRWXO;
  284. mode_t testPerm = S_IRUSR;
  285. #endif
  286. // Test file permissions without umask
  287. mode_t origPerm, thisPerm;
  288. if (!kwsys::SystemTools::GetPermissions(testNewFile, origPerm)) {
  289. std::cerr << "Problem with GetPermissions (1) for: " << testNewFile
  290. << std::endl;
  291. res = false;
  292. }
  293. if (!kwsys::SystemTools::SetPermissions(testNewFile, testPerm)) {
  294. std::cerr << "Problem with SetPermissions (1) for: " << testNewFile
  295. << std::endl;
  296. res = false;
  297. }
  298. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  299. std::cerr << "Problem with GetPermissions (2) for: " << testNewFile
  300. << std::endl;
  301. res = false;
  302. }
  303. if ((thisPerm & fullMask) != testPerm) {
  304. std::cerr << "SetPermissions failed to set permissions (1) for: "
  305. << testNewFile << ": actual = " << thisPerm
  306. << "; expected = " << testPerm << std::endl;
  307. res = false;
  308. }
  309. // While we're at it, check proper TestFileAccess functionality.
  310. bool do_write_test = true;
  311. #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
  312. defined(__NetBSD__) || defined(__DragonFly__) || defined(__HOS_AIX__)
  313. // If we are running as root on POSIX-ish systems (Linux and the BSDs,
  314. // at least), ignore this check, as root can always write to files.
  315. do_write_test = (getuid() != 0);
  316. #endif
  317. if (do_write_test &&
  318. kwsys::SystemTools::TestFileAccess(testNewFile,
  319. kwsys::TEST_FILE_WRITE)) {
  320. std::cerr
  321. << "TestFileAccess incorrectly indicated that this is a writable file:"
  322. << testNewFile << std::endl;
  323. res = false;
  324. }
  325. if (!kwsys::SystemTools::TestFileAccess(testNewFile, kwsys::TEST_FILE_OK)) {
  326. std::cerr
  327. << "TestFileAccess incorrectly indicated that this file does not exist:"
  328. << testNewFile << std::endl;
  329. res = false;
  330. }
  331. // Test restoring/setting full permissions.
  332. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask)) {
  333. std::cerr << "Problem with SetPermissions (2) for: " << testNewFile
  334. << std::endl;
  335. res = false;
  336. }
  337. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  338. std::cerr << "Problem with GetPermissions (3) for: " << testNewFile
  339. << std::endl;
  340. res = false;
  341. }
  342. if ((thisPerm & fullMask) != fullMask) {
  343. std::cerr << "SetPermissions failed to set permissions (2) for: "
  344. << testNewFile << ": actual = " << thisPerm
  345. << "; expected = " << fullMask << std::endl;
  346. res = false;
  347. }
  348. mode_t orig_umask = umask(fullMask);
  349. // Test setting file permissions while honoring umask
  350. if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask, true)) {
  351. std::cerr << "Problem with SetPermissions (3) for: " << testNewFile
  352. << std::endl;
  353. res = false;
  354. }
  355. if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
  356. std::cerr << "Problem with GetPermissions (4) for: " << testNewFile
  357. << std::endl;
  358. res = false;
  359. }
  360. if ((thisPerm & fullMask) != 0) {
  361. std::cerr << "SetPermissions failed to honor umask for: " << testNewFile
  362. << ": actual = " << thisPerm << "; expected = " << 0
  363. << std::endl;
  364. res = false;
  365. }
  366. // Restore umask
  367. umask(orig_umask);
  368. // Restore file permissions
  369. if (!kwsys::SystemTools::SetPermissions(testNewFile, origPerm)) {
  370. std::cerr << "Problem with SetPermissions (4) for: " << testNewFile
  371. << std::endl;
  372. res = false;
  373. }
  374. // Remove the test file
  375. if (!kwsys::SystemTools::RemoveFile(testNewFile)) {
  376. std::cerr << "Problem with RemoveFile: " << testNewFile << std::endl;
  377. res = false;
  378. }
  379. std::string const testFileMissing(testNewDir + "/testMissingFile.txt");
  380. if (!kwsys::SystemTools::RemoveFile(testFileMissing)) {
  381. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  382. std::cerr << "RemoveFile(\"" << testFileMissing << "\") failed: " << msg
  383. << "\n";
  384. res = false;
  385. }
  386. std::string const testFileMissingDir(testNewDir + "/missing/file.txt");
  387. if (!kwsys::SystemTools::RemoveFile(testFileMissingDir)) {
  388. std::string const& msg = kwsys::SystemTools::GetLastSystemError();
  389. std::cerr << "RemoveFile(\"" << testFileMissingDir << "\") failed: " << msg
  390. << "\n";
  391. res = false;
  392. }
  393. std::string const testBadSymlink(testNewDir + "/badSymlink.txt");
  394. std::string const testBadSymlinkTgt(testNewDir + "/missing/symlinkTgt.txt");
  395. kwsys::Status const symlinkStatus =
  396. kwsys::SystemTools::CreateSymlink(testBadSymlinkTgt, testBadSymlink);
  397. #if defined(_WIN32)
  398. // Under Windows, the user may not have enough privileges to create symlinks
  399. if (symlinkStatus.GetWindows() != ERROR_PRIVILEGE_NOT_HELD)
  400. #endif
  401. {
  402. if (!symlinkStatus.IsSuccess()) {
  403. std::cerr << "CreateSymlink for: " << testBadSymlink << " -> "
  404. << testBadSymlinkTgt
  405. << " failed: " << symlinkStatus.GetString() << std::endl;
  406. res = false;
  407. }
  408. if (!kwsys::SystemTools::Touch(testBadSymlink, false)) {
  409. std::cerr << "Problem with Touch (no create) for: " << testBadSymlink
  410. << std::endl;
  411. res = false;
  412. }
  413. }
  414. if (!kwsys::SystemTools::Touch(testNewDir, false)) {
  415. std::cerr << "Problem with Touch (no create) for: " << testNewDir
  416. << std::endl;
  417. res = false;
  418. }
  419. kwsys::SystemTools::Touch(testNewFile, true);
  420. if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
  421. std::cerr << "Problem with RemoveADirectory for: " << testNewDir
  422. << std::endl;
  423. res = false;
  424. }
  425. #ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  426. // Perform the same file and directory creation and deletion tests but
  427. // with paths > 256 characters in length.
  428. const std::string testNewLongDir(
  429. TEST_SYSTEMTOOLS_BINARY_DIR
  430. "/"
  431. "012345678901234567890123456789012345678901234567890123456789"
  432. "012345678901234567890123456789012345678901234567890123456789"
  433. "012345678901234567890123456789012345678901234567890123456789"
  434. "012345678901234567890123456789012345678901234567890123456789"
  435. "01234567890123");
  436. const std::string testNewLongFile(
  437. testNewLongDir +
  438. "/"
  439. "012345678901234567890123456789012345678901234567890123456789"
  440. "012345678901234567890123456789012345678901234567890123456789"
  441. "012345678901234567890123456789012345678901234567890123456789"
  442. "012345678901234567890123456789012345678901234567890123456789"
  443. "0123456789.txt");
  444. if (!kwsys::SystemTools::MakeDirectory(testNewLongDir)) {
  445. std::cerr << "Problem with MakeDirectory for: " << testNewLongDir
  446. << std::endl;
  447. res = false;
  448. }
  449. if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true)) {
  450. std::cerr << "Problem with Touch for: " << testNewLongFile << std::endl;
  451. res = false;
  452. }
  453. if (!kwsys::SystemTools::RemoveFile(testNewLongFile)) {
  454. std::cerr << "Problem with RemoveFile: " << testNewLongFile << std::endl;
  455. res = false;
  456. }
  457. kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
  458. if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir)) {
  459. std::cerr << "Problem with RemoveADirectory for: " << testNewLongDir
  460. << std::endl;
  461. res = false;
  462. }
  463. #endif
  464. std::cerr << std::dec;
  465. return res;
  466. }
  467. static bool CheckStringOperations()
  468. {
  469. bool res = true;
  470. std::string test = "mary had a little lamb.";
  471. if (kwsys::SystemTools::CapitalizedWords(test) !=
  472. "Mary Had A Little Lamb.") {
  473. std::cerr << "Problem with CapitalizedWords " << '"' << test << '"'
  474. << std::endl;
  475. res = false;
  476. }
  477. test = "Mary Had A Little Lamb.";
  478. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  479. "mary had a little lamb.") {
  480. std::cerr << "Problem with UnCapitalizedWords " << '"' << test << '"'
  481. << std::endl;
  482. res = false;
  483. }
  484. test = "MaryHadTheLittleLamb.";
  485. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  486. "Mary Had The Little Lamb.") {
  487. std::cerr << "Problem with AddSpaceBetweenCapitalizedWords " << '"' << test
  488. << '"' << std::endl;
  489. res = false;
  490. }
  491. char* cres =
  492. kwsys::SystemTools::AppendStrings("Mary Had A", " Little Lamb.");
  493. if (strcmp(cres, "Mary Had A Little Lamb.") != 0) {
  494. std::cerr << "Problem with AppendStrings "
  495. << "\"Mary Had A\" \" Little Lamb.\"" << std::endl;
  496. res = false;
  497. }
  498. delete[] cres;
  499. cres = kwsys::SystemTools::AppendStrings("Mary Had", " A ", "Little Lamb.");
  500. if (strcmp(cres, "Mary Had A Little Lamb.") != 0) {
  501. std::cerr << "Problem with AppendStrings "
  502. << "\"Mary Had\" \" A \" \"Little Lamb.\"" << std::endl;
  503. res = false;
  504. }
  505. delete[] cres;
  506. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.", 'a') != 3) {
  507. std::cerr << "Problem with CountChar "
  508. << "\"Mary Had A Little Lamb.\"" << std::endl;
  509. res = false;
  510. }
  511. cres = kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.", "aeiou");
  512. if (strcmp(cres, "Mry Hd A Lttl Lmb.") != 0) {
  513. std::cerr << "Problem with RemoveChars "
  514. << "\"Mary Had A Little Lamb.\"" << std::endl;
  515. res = false;
  516. }
  517. delete[] cres;
  518. cres = kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  519. if (strcmp(cres, "A") != 0) {
  520. std::cerr << "Problem with RemoveCharsButUpperHex "
  521. << "\"Mary Had A Little Lamb.\"" << std::endl;
  522. res = false;
  523. }
  524. delete[] cres;
  525. char* cres2 = strdup("Mary Had A Little Lamb.");
  526. kwsys::SystemTools::ReplaceChars(cres2, "aeiou", 'X');
  527. if (strcmp(cres2, "MXry HXd A LXttlX LXmb.") != 0) {
  528. std::cerr << "Problem with ReplaceChars "
  529. << "\"Mary Had A Little Lamb.\"" << std::endl;
  530. res = false;
  531. }
  532. free(cres2);
  533. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  534. "Mary ")) {
  535. std::cerr << "Problem with StringStartsWith "
  536. << "\"Mary Had A Little Lamb.\"" << std::endl;
  537. res = false;
  538. }
  539. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  540. " Lamb.")) {
  541. std::cerr << "Problem with StringEndsWith "
  542. << "\"Mary Had A Little Lamb.\"" << std::endl;
  543. res = false;
  544. }
  545. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  546. if (strcmp(cres, "Mary Had A Little Lamb.") != 0) {
  547. std::cerr << "Problem with DuplicateString "
  548. << "\"Mary Had A Little Lamb.\"" << std::endl;
  549. res = false;
  550. }
  551. delete[] cres;
  552. test = "Mary Had A Little Lamb.";
  553. if (kwsys::SystemTools::CropString(test, 13) != "Mary ...Lamb.") {
  554. std::cerr << "Problem with CropString "
  555. << "\"Mary Had A Little Lamb.\"" << std::endl;
  556. res = false;
  557. }
  558. std::vector<std::string> lines;
  559. kwsys::SystemTools::Split("Mary Had A Little Lamb.", lines, ' ');
  560. if (lines[0] != "Mary" || lines[1] != "Had" || lines[2] != "A" ||
  561. lines[3] != "Little" || lines[4] != "Lamb.") {
  562. std::cerr << "Problem with Split "
  563. << "\"Mary Had A Little Lamb.\"" << std::endl;
  564. res = false;
  565. }
  566. std::vector<std::string> linesToJoin = { "Mary", "Had", "A", "Little",
  567. "Lamb." };
  568. std::string joinResult = kwsys::SystemTools::Join(linesToJoin, " ");
  569. if (joinResult != "Mary Had A Little Lamb.") {
  570. std::cerr << "Problem with Join "
  571. "\"Mary Had A Little Lamb.\""
  572. << std::endl;
  573. res = false;
  574. }
  575. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  576. "L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  577. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  578. std::cerr << "Problem with ConvertToWindowsOutputPath "
  579. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  580. res = false;
  581. }
  582. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  583. "//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  584. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  585. std::cerr << "Problem with ConvertToWindowsOutputPath "
  586. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  587. << std::endl;
  588. res = false;
  589. }
  590. if (kwsys::SystemTools::ConvertToUnixOutputPath(
  591. "//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  592. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo") {
  593. std::cerr << "Problem with ConvertToUnixOutputPath "
  594. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  595. res = false;
  596. }
  597. return res;
  598. }
  599. static bool CheckPutEnv(const std::string& env, const char* name,
  600. const char* value)
  601. {
  602. if (!kwsys::SystemTools::PutEnv(env)) {
  603. std::cerr << "PutEnv(\"" << env << "\") failed!" << std::endl;
  604. return false;
  605. }
  606. std::string v = "(null)";
  607. kwsys::SystemTools::GetEnv(name, v);
  608. if (v != value) {
  609. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v << "\", not \""
  610. << value << "\"!" << std::endl;
  611. return false;
  612. }
  613. return true;
  614. }
  615. static bool CheckUnPutEnv(const char* env, const char* name)
  616. {
  617. if (!kwsys::SystemTools::UnPutEnv(env)) {
  618. std::cerr << "UnPutEnv(\"" << env << "\") failed!" << std::endl;
  619. return false;
  620. }
  621. std::string v;
  622. if (kwsys::SystemTools::GetEnv(name, v)) {
  623. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v
  624. << "\", not (null)!" << std::endl;
  625. return false;
  626. }
  627. return true;
  628. }
  629. static bool CheckEnvironmentOperations()
  630. {
  631. bool res = true;
  632. res &= CheckPutEnv("A=B", "A", "B");
  633. res &= CheckPutEnv("B=C", "B", "C");
  634. res &= CheckPutEnv("C=D", "C", "D");
  635. res &= CheckPutEnv("D=E", "D", "E");
  636. res &= CheckUnPutEnv("A", "A");
  637. res &= CheckUnPutEnv("B=", "B");
  638. res &= CheckUnPutEnv("C=D", "C");
  639. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  640. return res;
  641. }
  642. static bool CheckRelativePath(const std::string& local,
  643. const std::string& remote,
  644. const std::string& expected)
  645. {
  646. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  647. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  648. std::cerr << "RelativePath(" << local << ", " << remote << ") yielded "
  649. << result << " instead of " << expected << std::endl;
  650. return false;
  651. }
  652. return true;
  653. }
  654. static bool CheckRelativePaths()
  655. {
  656. bool res = true;
  657. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  658. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  659. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  660. res &=
  661. CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  662. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  663. return res;
  664. }
  665. static bool CheckCollapsePath(const std::string& path,
  666. const std::string& expected,
  667. const char* base = nullptr)
  668. {
  669. std::string result = kwsys::SystemTools::CollapseFullPath(path, base);
  670. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  671. std::cerr << "CollapseFullPath(" << path << ") yielded " << result
  672. << " instead of " << expected << std::endl;
  673. return false;
  674. }
  675. return true;
  676. }
  677. static bool CheckCollapsePath()
  678. {
  679. bool res = true;
  680. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  681. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  682. res &= CheckCollapsePath("/usr/share/../lib", "/usr/lib");
  683. res &= CheckCollapsePath("/usr/share/./lib", "/usr/share/lib");
  684. res &= CheckCollapsePath("/usr/share/../../lib", "/lib");
  685. res &= CheckCollapsePath("/usr/share/.././../lib", "/lib");
  686. res &= CheckCollapsePath("/../lib", "/lib");
  687. res &= CheckCollapsePath("/../lib/", "/lib");
  688. res &= CheckCollapsePath("/", "/");
  689. res &= CheckCollapsePath("C:/", "C:/");
  690. res &= CheckCollapsePath("C:/../", "C:/");
  691. res &= CheckCollapsePath("C:/../../", "C:/");
  692. res &= CheckCollapsePath("../b", "../../b", "../");
  693. res &= CheckCollapsePath("../a/../b", "../b", "../rel");
  694. res &= CheckCollapsePath("a/../b", "../rel/b", "../rel");
  695. return res;
  696. }
  697. static std::string StringVectorToString(const std::vector<std::string>& vec)
  698. {
  699. std::stringstream ss;
  700. ss << "vector(";
  701. for (auto i = vec.begin(); i != vec.end(); ++i) {
  702. if (i != vec.begin()) {
  703. ss << ", ";
  704. }
  705. ss << *i;
  706. }
  707. ss << ")";
  708. return ss.str();
  709. }
  710. static bool CheckGetPath()
  711. {
  712. const char* envName = "S";
  713. #ifdef _WIN32
  714. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  715. #else
  716. const char* envValue = "/Somewhere/something:/tmp";
  717. #endif
  718. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  719. std::vector<std::string> originalPaths;
  720. originalPaths.emplace_back(registryPath);
  721. std::vector<std::string> expectedPaths;
  722. expectedPaths.emplace_back(registryPath);
  723. #ifdef _WIN32
  724. expectedPaths.push_back("C:/Somewhere/something");
  725. expectedPaths.push_back("D:/Temp");
  726. #else
  727. expectedPaths.emplace_back("/Somewhere/something");
  728. expectedPaths.emplace_back("/tmp");
  729. #endif
  730. bool res = true;
  731. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  732. std::vector<std::string> paths = originalPaths;
  733. kwsys::SystemTools::GetPath(paths, envName);
  734. if (paths != expectedPaths) {
  735. std::cerr << "GetPath(" << StringVectorToString(originalPaths) << ", "
  736. << envName << ") yielded " << StringVectorToString(paths)
  737. << " instead of " << StringVectorToString(expectedPaths)
  738. << std::endl;
  739. res = false;
  740. }
  741. res &= CheckUnPutEnv(envName, envName);
  742. return res;
  743. }
  744. static bool CheckGetFilenameName()
  745. {
  746. const char* windowsFilepath = "C:\\somewhere\\something";
  747. const char* unixFilepath = "/somewhere/something";
  748. #if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
  749. std::string expectedWindowsFilename = "something";
  750. #else
  751. std::string expectedWindowsFilename = "C:\\somewhere\\something";
  752. #endif
  753. std::string expectedUnixFilename = "something";
  754. bool res = true;
  755. std::string filename = kwsys::SystemTools::GetFilenameName(windowsFilepath);
  756. if (filename != expectedWindowsFilename) {
  757. std::cerr << "GetFilenameName(" << windowsFilepath << ") yielded "
  758. << filename << " instead of " << expectedWindowsFilename
  759. << std::endl;
  760. res = false;
  761. }
  762. filename = kwsys::SystemTools::GetFilenameName(unixFilepath);
  763. if (filename != expectedUnixFilename) {
  764. std::cerr << "GetFilenameName(" << unixFilepath << ") yielded " << filename
  765. << " instead of " << expectedUnixFilename << std::endl;
  766. res = false;
  767. }
  768. return res;
  769. }
  770. static bool CheckFind()
  771. {
  772. bool res = true;
  773. const std::string testFindFileName("testFindFile.txt");
  774. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/" +
  775. testFindFileName);
  776. if (!kwsys::SystemTools::Touch(testFindFile, true)) {
  777. std::cerr << "Problem with Touch for: " << testFindFile << std::endl;
  778. // abort here as the existence of the file only makes the test meaningful
  779. return false;
  780. }
  781. std::vector<std::string> searchPaths;
  782. searchPaths.emplace_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  783. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true)
  784. .empty()) {
  785. std::cerr << "Problem with FindFile without system paths for: "
  786. << testFindFileName << std::endl;
  787. res = false;
  788. }
  789. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, false)
  790. .empty()) {
  791. std::cerr << "Problem with FindFile with system paths for: "
  792. << testFindFileName << std::endl;
  793. res = false;
  794. }
  795. return res;
  796. }
  797. static bool CheckIsSubDirectory()
  798. {
  799. bool res = true;
  800. if (kwsys::SystemTools::IsSubDirectory("/foo", "/") == false) {
  801. std::cerr << "Problem with IsSubDirectory (root - unix): " << std::endl;
  802. res = false;
  803. }
  804. if (kwsys::SystemTools::IsSubDirectory("c:/foo", "c:/") == false) {
  805. std::cerr << "Problem with IsSubDirectory (root - dos): " << std::endl;
  806. res = false;
  807. }
  808. if (kwsys::SystemTools::IsSubDirectory("/foo/bar", "/foo") == false) {
  809. std::cerr << "Problem with IsSubDirectory (deep): " << std::endl;
  810. res = false;
  811. }
  812. if (kwsys::SystemTools::IsSubDirectory("/foo", "/foo") == true) {
  813. std::cerr << "Problem with IsSubDirectory (identity): " << std::endl;
  814. res = false;
  815. }
  816. if (kwsys::SystemTools::IsSubDirectory("/fooo", "/foo") == true) {
  817. std::cerr << "Problem with IsSubDirectory (substring): " << std::endl;
  818. res = false;
  819. }
  820. if (kwsys::SystemTools::IsSubDirectory("/foo/", "/foo") == true) {
  821. std::cerr << "Problem with IsSubDirectory (prepended slash): "
  822. << std::endl;
  823. res = false;
  824. }
  825. return res;
  826. }
  827. static bool CheckGetLineFromStream()
  828. {
  829. const std::string fileWithFiveCharsOnFirstLine(TEST_SYSTEMTOOLS_SOURCE_DIR
  830. "/README.rst");
  831. kwsys::ifstream file(fileWithFiveCharsOnFirstLine.c_str(), std::ios::in);
  832. if (!file) {
  833. std::cerr << "Problem opening: " << fileWithFiveCharsOnFirstLine
  834. << std::endl;
  835. return false;
  836. }
  837. std::string line;
  838. bool has_newline = false;
  839. bool result;
  840. file.seekg(0, std::ios::beg);
  841. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  842. std::string::npos);
  843. if (!result || line.size() != 5) {
  844. std::cerr << "First line does not have five characters: " << line.size()
  845. << std::endl;
  846. return false;
  847. }
  848. file.seekg(0, std::ios::beg);
  849. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  850. std::string::npos);
  851. if (!result || line.size() != 5) {
  852. std::cerr << "First line does not have five characters after rewind: "
  853. << line.size() << std::endl;
  854. return false;
  855. }
  856. bool ret = true;
  857. for (std::string::size_type size = 1; size <= 5; ++size) {
  858. file.seekg(0, std::ios::beg);
  859. result =
  860. kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, size);
  861. if (!result || line.size() != size) {
  862. std::cerr << "Should have read " << size << " characters but got "
  863. << line.size() << std::endl;
  864. ret = false;
  865. }
  866. }
  867. return ret;
  868. }
  869. static bool CheckGetLineFromStreamLongLine()
  870. {
  871. const std::string fileWithLongLine("longlines.txt");
  872. std::string firstLine, secondLine;
  873. // First line: large buffer, containing a carriage return for some reason.
  874. firstLine.assign(2050, ' ');
  875. firstLine += "\rfirst";
  876. secondLine.assign(2050, 'y');
  877. secondLine += "second";
  878. // Create file with long lines.
  879. {
  880. kwsys::ofstream out(fileWithLongLine.c_str(), std::ios::binary);
  881. if (!out) {
  882. std::cerr << "Problem opening for write: " << fileWithLongLine
  883. << std::endl;
  884. return false;
  885. }
  886. out << firstLine << "\r\n\n" << secondLine << "\n";
  887. }
  888. kwsys::ifstream file(fileWithLongLine.c_str(), std::ios::binary);
  889. if (!file) {
  890. std::cerr << "Problem opening: " << fileWithLongLine << std::endl;
  891. return false;
  892. }
  893. std::string line;
  894. bool has_newline = false;
  895. bool result;
  896. // Read first line.
  897. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  898. std::string::npos);
  899. if (!result || line != firstLine) {
  900. std::cerr << "First line does not match, expected " << firstLine.size()
  901. << " characters, got " << line.size() << std::endl;
  902. return false;
  903. }
  904. if (!has_newline) {
  905. std::cerr << "Expected new line to be read from first line" << std::endl;
  906. return false;
  907. }
  908. // Read empty line.
  909. has_newline = false;
  910. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  911. std::string::npos);
  912. if (!result || !line.empty()) {
  913. std::cerr << "Expected successful read with an empty line, got "
  914. << line.size() << " characters" << std::endl;
  915. return false;
  916. }
  917. if (!has_newline) {
  918. std::cerr << "Expected new line to be read for an empty line" << std::endl;
  919. return false;
  920. }
  921. // Read second line.
  922. has_newline = false;
  923. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  924. std::string::npos);
  925. if (!result || line != secondLine) {
  926. std::cerr << "Second line does not match, expected " << secondLine.size()
  927. << " characters, got " << line.size() << std::endl;
  928. return false;
  929. }
  930. if (!has_newline) {
  931. std::cerr << "Expected new line to be read from second line" << std::endl;
  932. return false;
  933. }
  934. return true;
  935. }
  936. static bool writeFile(const char* fileName, const char* data)
  937. {
  938. kwsys::ofstream out(fileName, std::ios::binary);
  939. out << data;
  940. if (!out) {
  941. std::cerr << "Failed to write file: " << fileName << std::endl;
  942. return false;
  943. }
  944. return true;
  945. }
  946. static std::string readFile(const char* fileName)
  947. {
  948. kwsys::ifstream in(fileName, std::ios::binary);
  949. std::stringstream sstr;
  950. sstr << in.rdbuf();
  951. std::string data = sstr.str();
  952. if (!in) {
  953. std::cerr << "Failed to read file: " << fileName << std::endl;
  954. return std::string();
  955. }
  956. return data;
  957. }
  958. struct
  959. {
  960. const char* a;
  961. const char* b;
  962. bool differ;
  963. } diff_test_cases[] = { { "one", "one", false },
  964. { "one", "two", true },
  965. { "", "", false },
  966. { "\n", "\r\n", false },
  967. { "one\n", "one\n", false },
  968. { "one\r\n", "one\n", false },
  969. { "one\n", "one", false },
  970. { "one\ntwo", "one\ntwo", false },
  971. { "one\ntwo", "one\r\ntwo", false } };
  972. static bool CheckTextFilesDiffer()
  973. {
  974. const int num_test_cases =
  975. sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
  976. for (int i = 0; i < num_test_cases; ++i) {
  977. if (!writeFile("file_a", diff_test_cases[i].a) ||
  978. !writeFile("file_b", diff_test_cases[i].b)) {
  979. return false;
  980. }
  981. if (kwsys::SystemTools::TextFilesDiffer("file_a", "file_b") !=
  982. diff_test_cases[i].differ) {
  983. std::cerr << "Incorrect TextFilesDiffer result for test case " << i + 1
  984. << "." << std::endl;
  985. return false;
  986. }
  987. }
  988. return true;
  989. }
  990. static bool CheckCopyFileIfDifferent()
  991. {
  992. bool ret = true;
  993. const int num_test_cases =
  994. sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
  995. for (int i = 0; i < num_test_cases; ++i) {
  996. if (!writeFile("file_a", diff_test_cases[i].a) ||
  997. !writeFile("file_b", diff_test_cases[i].b)) {
  998. return false;
  999. }
  1000. const char* cptarget =
  1001. i < 4 ? TEST_SYSTEMTOOLS_BINARY_DIR "/file_b" : "file_b";
  1002. if (!kwsys::SystemTools::CopyFileIfDifferent("file_a", cptarget)) {
  1003. std::cerr << "CopyFileIfDifferent() returned false for test case "
  1004. << i + 1 << "." << std::endl;
  1005. ret = false;
  1006. continue;
  1007. }
  1008. std::string bdata = readFile(cptarget);
  1009. if (diff_test_cases[i].a != bdata) {
  1010. std::cerr << "Incorrect CopyFileIfDifferent file contents in test case "
  1011. << i + 1 << "." << std::endl;
  1012. ret = false;
  1013. continue;
  1014. }
  1015. }
  1016. if (!kwsys::SystemTools::MakeDirectory("dir_a") ||
  1017. !kwsys::SystemTools::MakeDirectory("dir_b")) {
  1018. return false;
  1019. }
  1020. if (!kwsys::SystemTools::CopyFileIfDifferent("dir_a/", "dir_b")) {
  1021. ret = false;
  1022. }
  1023. return ret;
  1024. }
  1025. static bool CheckURLParsing()
  1026. {
  1027. bool ret = true;
  1028. std::string url = "http://user:pw@hostname:42/full/url.com";
  1029. std::string protocol, username, password, hostname, dataport, database;
  1030. kwsys::SystemTools::ParseURL(url, protocol, username, password, hostname,
  1031. dataport, database);
  1032. if (protocol != "http" || username != "user" || password != "pw" ||
  1033. hostname != "hostname" || dataport != "42" ||
  1034. database != "full/url.com") {
  1035. std::cerr << "Incorrect URL parsing" << std::endl;
  1036. ret = false;
  1037. }
  1038. std::string uri =
  1039. "file://hostname/path/to/"
  1040. "a%20file%20with%20str%C3%A0ng%C3%A8%20ch%40r%20and%20s%C2%B5aces";
  1041. kwsys::SystemTools::ParseURL(uri, protocol, username, password, hostname,
  1042. dataport, database, true);
  1043. if (protocol != "file" || hostname != "hostname" ||
  1044. database != "path/to/a file with stràngè ch@r and sµaces") {
  1045. std::cerr << "Incorrect URL parsing or decoding" << std::endl;
  1046. ret = false;
  1047. }
  1048. return ret;
  1049. }
  1050. static bool CheckSplitString()
  1051. {
  1052. bool ret = true;
  1053. auto check_split = [](std::string const& input,
  1054. std::initializer_list<const char*> expected) -> bool {
  1055. auto const components = kwsys::SystemTools::SplitString(input, '/');
  1056. if (components.size() != expected.size()) {
  1057. std::cerr << "Incorrect split count for " << input << ": "
  1058. << components.size() << std::endl;
  1059. return false;
  1060. }
  1061. size_t i = 0;
  1062. for (auto& part : expected) {
  1063. if (components[i] != part) {
  1064. std::cerr << "Incorrect split component " << i << " for " << input
  1065. << ": " << components[i] << std::endl;
  1066. return false;
  1067. }
  1068. ++i;
  1069. }
  1070. return true;
  1071. };
  1072. // No separators
  1073. ret &= check_split("nosep", { "nosep" });
  1074. // Simple
  1075. ret &= check_split("first/second", { "first", "second" });
  1076. // Separator at beginning
  1077. ret &= check_split("/starts/sep", { "", "starts", "sep" });
  1078. // Separator at end
  1079. ret &= check_split("ends/sep/", { "ends", "sep", "" });
  1080. return ret;
  1081. }
  1082. int testSystemTools(int, char* [])
  1083. {
  1084. bool res = true;
  1085. int cc;
  1086. for (cc = 0; toUnixPaths[cc][0]; cc++) {
  1087. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  1088. }
  1089. // Special check for ~
  1090. std::string output;
  1091. if (kwsys::SystemTools::GetEnv("HOME", output)) {
  1092. output += "/foo bar/lala";
  1093. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  1094. }
  1095. for (cc = 0; checkEscapeChars[cc][0]; cc++) {
  1096. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  1097. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  1098. }
  1099. res &= CheckFileOperations();
  1100. res &= CheckStringOperations();
  1101. res &= CheckEnvironmentOperations();
  1102. res &= CheckRelativePaths();
  1103. res &= CheckCollapsePath();
  1104. res &= CheckGetPath();
  1105. res &= CheckFind();
  1106. res &= CheckIsSubDirectory();
  1107. res &= CheckGetLineFromStream();
  1108. res &= CheckGetLineFromStreamLongLine();
  1109. res &= CheckGetFilenameName();
  1110. res &= CheckTextFilesDiffer();
  1111. res &= CheckCopyFileIfDifferent();
  1112. res &= CheckURLParsing();
  1113. res &= CheckSplitString();
  1114. return res ? 0 : 1;
  1115. }