testSystemTools.cxx 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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. std::vector<std::string> linesToJoin = { "Mary", "Had", "A", "Little",
  564. "Lamb." };
  565. std::string joinResult = kwsys::SystemTools::Join(linesToJoin, " ");
  566. if (joinResult != "Mary Had A Little Lamb.") {
  567. std::cerr << "Problem with Join "
  568. "\"Mary Had A Little Lamb.\""
  569. << std::endl;
  570. res = false;
  571. }
  572. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  573. "L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  574. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  575. std::cerr << "Problem with ConvertToWindowsOutputPath "
  576. << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  577. res = false;
  578. }
  579. if (kwsys::SystemTools::ConvertToWindowsOutputPath(
  580. "//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  581. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
  582. std::cerr << "Problem with ConvertToWindowsOutputPath "
  583. << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
  584. << std::endl;
  585. res = false;
  586. }
  587. if (kwsys::SystemTools::ConvertToUnixOutputPath(
  588. "//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  589. "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo") {
  590. std::cerr << "Problem with ConvertToUnixOutputPath "
  591. << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
  592. res = false;
  593. }
  594. return res;
  595. }
  596. static bool CheckPutEnv(const std::string& env, const char* name,
  597. const char* value)
  598. {
  599. if (!kwsys::SystemTools::PutEnv(env)) {
  600. std::cerr << "PutEnv(\"" << env << "\") failed!" << std::endl;
  601. return false;
  602. }
  603. std::string v = "(null)";
  604. kwsys::SystemTools::GetEnv(name, v);
  605. if (v != value) {
  606. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v << "\", not \""
  607. << value << "\"!" << std::endl;
  608. return false;
  609. }
  610. return true;
  611. }
  612. static bool CheckUnPutEnv(const char* env, const char* name)
  613. {
  614. if (!kwsys::SystemTools::UnPutEnv(env)) {
  615. std::cerr << "UnPutEnv(\"" << env << "\") failed!" << std::endl;
  616. return false;
  617. }
  618. std::string v;
  619. if (kwsys::SystemTools::GetEnv(name, v)) {
  620. std::cerr << "GetEnv(\"" << name << "\") returned \"" << v
  621. << "\", not (null)!" << std::endl;
  622. return false;
  623. }
  624. return true;
  625. }
  626. static bool CheckEnvironmentOperations()
  627. {
  628. bool res = true;
  629. res &= CheckPutEnv("A=B", "A", "B");
  630. res &= CheckPutEnv("B=C", "B", "C");
  631. res &= CheckPutEnv("C=D", "C", "D");
  632. res &= CheckPutEnv("D=E", "D", "E");
  633. res &= CheckUnPutEnv("A", "A");
  634. res &= CheckUnPutEnv("B=", "B");
  635. res &= CheckUnPutEnv("C=D", "C");
  636. /* Leave "D=E" in environment so a memory checker can test for leaks. */
  637. return res;
  638. }
  639. static bool CheckRelativePath(const std::string& local,
  640. const std::string& remote,
  641. const std::string& expected)
  642. {
  643. std::string result = kwsys::SystemTools::RelativePath(local, remote);
  644. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  645. std::cerr << "RelativePath(" << local << ", " << remote << ") yielded "
  646. << result << " instead of " << expected << std::endl;
  647. return false;
  648. }
  649. return true;
  650. }
  651. static bool CheckRelativePaths()
  652. {
  653. bool res = true;
  654. res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  655. res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  656. res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
  657. res &=
  658. CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
  659. res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  660. return res;
  661. }
  662. static bool CheckCollapsePath(const std::string& path,
  663. const std::string& expected,
  664. const char* base = nullptr)
  665. {
  666. std::string result = kwsys::SystemTools::CollapseFullPath(path, base);
  667. if (!kwsys::SystemTools::ComparePath(expected, result)) {
  668. std::cerr << "CollapseFullPath(" << path << ") yielded " << result
  669. << " instead of " << expected << std::endl;
  670. return false;
  671. }
  672. return true;
  673. }
  674. static bool CheckCollapsePath()
  675. {
  676. bool res = true;
  677. res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  678. res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  679. res &= CheckCollapsePath("/usr/share/../lib", "/usr/lib");
  680. res &= CheckCollapsePath("/usr/share/./lib", "/usr/share/lib");
  681. res &= CheckCollapsePath("/usr/share/../../lib", "/lib");
  682. res &= CheckCollapsePath("/usr/share/.././../lib", "/lib");
  683. res &= CheckCollapsePath("/../lib", "/lib");
  684. res &= CheckCollapsePath("/../lib/", "/lib");
  685. res &= CheckCollapsePath("/", "/");
  686. res &= CheckCollapsePath("C:/", "C:/");
  687. res &= CheckCollapsePath("C:/../", "C:/");
  688. res &= CheckCollapsePath("C:/../../", "C:/");
  689. res &= CheckCollapsePath("../b", "../../b", "../");
  690. res &= CheckCollapsePath("../a/../b", "../b", "../rel");
  691. res &= CheckCollapsePath("a/../b", "../rel/b", "../rel");
  692. return res;
  693. }
  694. static std::string StringVectorToString(const std::vector<std::string>& vec)
  695. {
  696. std::stringstream ss;
  697. ss << "vector(";
  698. for (auto i = vec.begin(); i != vec.end(); ++i) {
  699. if (i != vec.begin()) {
  700. ss << ", ";
  701. }
  702. ss << *i;
  703. }
  704. ss << ")";
  705. return ss.str();
  706. }
  707. static bool CheckGetPath()
  708. {
  709. const char* envName = "S";
  710. #ifdef _WIN32
  711. const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
  712. #else
  713. const char* envValue = "/Somewhere/something:/tmp";
  714. #endif
  715. const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";
  716. std::vector<std::string> originalPaths;
  717. originalPaths.emplace_back(registryPath);
  718. std::vector<std::string> expectedPaths;
  719. expectedPaths.emplace_back(registryPath);
  720. #ifdef _WIN32
  721. expectedPaths.push_back("C:/Somewhere/something");
  722. expectedPaths.push_back("D:/Temp");
  723. #else
  724. expectedPaths.emplace_back("/Somewhere/something");
  725. expectedPaths.emplace_back("/tmp");
  726. #endif
  727. bool res = true;
  728. res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);
  729. std::vector<std::string> paths = originalPaths;
  730. kwsys::SystemTools::GetPath(paths, envName);
  731. if (paths != expectedPaths) {
  732. std::cerr << "GetPath(" << StringVectorToString(originalPaths) << ", "
  733. << envName << ") yielded " << StringVectorToString(paths)
  734. << " instead of " << StringVectorToString(expectedPaths)
  735. << std::endl;
  736. res = false;
  737. }
  738. res &= CheckUnPutEnv(envName, envName);
  739. return res;
  740. }
  741. static bool CheckGetFilenameName()
  742. {
  743. const char* windowsFilepath = "C:\\somewhere\\something";
  744. const char* unixFilepath = "/somewhere/something";
  745. #if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
  746. std::string expectedWindowsFilename = "something";
  747. #else
  748. std::string expectedWindowsFilename = "C:\\somewhere\\something";
  749. #endif
  750. std::string expectedUnixFilename = "something";
  751. bool res = true;
  752. std::string filename = kwsys::SystemTools::GetFilenameName(windowsFilepath);
  753. if (filename != expectedWindowsFilename) {
  754. std::cerr << "GetFilenameName(" << windowsFilepath << ") yielded "
  755. << filename << " instead of " << expectedWindowsFilename
  756. << std::endl;
  757. res = false;
  758. }
  759. filename = kwsys::SystemTools::GetFilenameName(unixFilepath);
  760. if (filename != expectedUnixFilename) {
  761. std::cerr << "GetFilenameName(" << unixFilepath << ") yielded " << filename
  762. << " instead of " << expectedUnixFilename << std::endl;
  763. res = false;
  764. }
  765. return res;
  766. }
  767. static bool CheckFind()
  768. {
  769. bool res = true;
  770. const std::string testFindFileName("testFindFile.txt");
  771. const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/" +
  772. testFindFileName);
  773. if (!kwsys::SystemTools::Touch(testFindFile, true)) {
  774. std::cerr << "Problem with Touch for: " << testFindFile << std::endl;
  775. // abort here as the existence of the file only makes the test meaningful
  776. return false;
  777. }
  778. std::vector<std::string> searchPaths;
  779. searchPaths.emplace_back(TEST_SYSTEMTOOLS_BINARY_DIR);
  780. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true)
  781. .empty()) {
  782. std::cerr << "Problem with FindFile without system paths for: "
  783. << testFindFileName << std::endl;
  784. res = false;
  785. }
  786. if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, false)
  787. .empty()) {
  788. std::cerr << "Problem with FindFile with system paths for: "
  789. << testFindFileName << std::endl;
  790. res = false;
  791. }
  792. return res;
  793. }
  794. static bool CheckIsSubDirectory()
  795. {
  796. bool res = true;
  797. if (kwsys::SystemTools::IsSubDirectory("/foo", "/") == false) {
  798. std::cerr << "Problem with IsSubDirectory (root - unix): " << std::endl;
  799. res = false;
  800. }
  801. if (kwsys::SystemTools::IsSubDirectory("c:/foo", "c:/") == false) {
  802. std::cerr << "Problem with IsSubDirectory (root - dos): " << std::endl;
  803. res = false;
  804. }
  805. if (kwsys::SystemTools::IsSubDirectory("/foo/bar", "/foo") == false) {
  806. std::cerr << "Problem with IsSubDirectory (deep): " << std::endl;
  807. res = false;
  808. }
  809. if (kwsys::SystemTools::IsSubDirectory("/foo", "/foo") == true) {
  810. std::cerr << "Problem with IsSubDirectory (identity): " << std::endl;
  811. res = false;
  812. }
  813. if (kwsys::SystemTools::IsSubDirectory("/fooo", "/foo") == true) {
  814. std::cerr << "Problem with IsSubDirectory (substring): " << std::endl;
  815. res = false;
  816. }
  817. if (kwsys::SystemTools::IsSubDirectory("/foo/", "/foo") == true) {
  818. std::cerr << "Problem with IsSubDirectory (prepended slash): "
  819. << std::endl;
  820. res = false;
  821. }
  822. return res;
  823. }
  824. static bool CheckGetLineFromStream()
  825. {
  826. const std::string fileWithFiveCharsOnFirstLine(TEST_SYSTEMTOOLS_SOURCE_DIR
  827. "/README.rst");
  828. kwsys::ifstream file(fileWithFiveCharsOnFirstLine.c_str(), std::ios::in);
  829. if (!file) {
  830. std::cerr << "Problem opening: " << fileWithFiveCharsOnFirstLine
  831. << std::endl;
  832. return false;
  833. }
  834. std::string line;
  835. bool has_newline = false;
  836. bool result;
  837. file.seekg(0, std::ios::beg);
  838. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  839. std::string::npos);
  840. if (!result || line.size() != 5) {
  841. std::cerr << "First line does not have five characters: " << line.size()
  842. << std::endl;
  843. return false;
  844. }
  845. file.seekg(0, std::ios::beg);
  846. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  847. std::string::npos);
  848. if (!result || line.size() != 5) {
  849. std::cerr << "First line does not have five characters after rewind: "
  850. << line.size() << std::endl;
  851. return false;
  852. }
  853. bool ret = true;
  854. for (std::string::size_type size = 1; size <= 5; ++size) {
  855. file.seekg(0, std::ios::beg);
  856. result =
  857. kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, size);
  858. if (!result || line.size() != size) {
  859. std::cerr << "Should have read " << size << " characters but got "
  860. << line.size() << std::endl;
  861. ret = false;
  862. }
  863. }
  864. return ret;
  865. }
  866. static bool CheckGetLineFromStreamLongLine()
  867. {
  868. const std::string fileWithLongLine("longlines.txt");
  869. std::string firstLine, secondLine;
  870. // First line: large buffer, containing a carriage return for some reason.
  871. firstLine.assign(2050, ' ');
  872. firstLine += "\rfirst";
  873. secondLine.assign(2050, 'y');
  874. secondLine += "second";
  875. // Create file with long lines.
  876. {
  877. kwsys::ofstream out(fileWithLongLine.c_str(), std::ios::binary);
  878. if (!out) {
  879. std::cerr << "Problem opening for write: " << fileWithLongLine
  880. << std::endl;
  881. return false;
  882. }
  883. out << firstLine << "\r\n\n" << secondLine << "\n";
  884. }
  885. kwsys::ifstream file(fileWithLongLine.c_str(), std::ios::binary);
  886. if (!file) {
  887. std::cerr << "Problem opening: " << fileWithLongLine << std::endl;
  888. return false;
  889. }
  890. std::string line;
  891. bool has_newline = false;
  892. bool result;
  893. // Read first line.
  894. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  895. std::string::npos);
  896. if (!result || line != firstLine) {
  897. std::cerr << "First line does not match, expected " << firstLine.size()
  898. << " characters, got " << line.size() << std::endl;
  899. return false;
  900. }
  901. if (!has_newline) {
  902. std::cerr << "Expected new line to be read from first line" << std::endl;
  903. return false;
  904. }
  905. // Read empty line.
  906. has_newline = false;
  907. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  908. std::string::npos);
  909. if (!result || !line.empty()) {
  910. std::cerr << "Expected successful read with an empty line, got "
  911. << line.size() << " characters" << std::endl;
  912. return false;
  913. }
  914. if (!has_newline) {
  915. std::cerr << "Expected new line to be read for an empty line" << std::endl;
  916. return false;
  917. }
  918. // Read second line.
  919. has_newline = false;
  920. result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
  921. std::string::npos);
  922. if (!result || line != secondLine) {
  923. std::cerr << "Second line does not match, expected " << secondLine.size()
  924. << " characters, got " << line.size() << std::endl;
  925. return false;
  926. }
  927. if (!has_newline) {
  928. std::cerr << "Expected new line to be read from second line" << std::endl;
  929. return false;
  930. }
  931. return true;
  932. }
  933. static bool writeFile(const char* fileName, const char* data)
  934. {
  935. kwsys::ofstream out(fileName, std::ios::binary);
  936. out << data;
  937. if (!out) {
  938. std::cerr << "Failed to write file: " << fileName << std::endl;
  939. return false;
  940. }
  941. return true;
  942. }
  943. static std::string readFile(const char* fileName)
  944. {
  945. kwsys::ifstream in(fileName, std::ios::binary);
  946. std::stringstream sstr;
  947. sstr << in.rdbuf();
  948. std::string data = sstr.str();
  949. if (!in) {
  950. std::cerr << "Failed to read file: " << fileName << std::endl;
  951. return std::string();
  952. }
  953. return data;
  954. }
  955. struct
  956. {
  957. const char* a;
  958. const char* b;
  959. bool differ;
  960. } diff_test_cases[] = { { "one", "one", false },
  961. { "one", "two", true },
  962. { "", "", false },
  963. { "\n", "\r\n", false },
  964. { "one\n", "one\n", false },
  965. { "one\r\n", "one\n", false },
  966. { "one\n", "one", false },
  967. { "one\ntwo", "one\ntwo", false },
  968. { "one\ntwo", "one\r\ntwo", false } };
  969. static bool CheckTextFilesDiffer()
  970. {
  971. const int num_test_cases =
  972. sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
  973. for (int i = 0; i < num_test_cases; ++i) {
  974. if (!writeFile("file_a", diff_test_cases[i].a) ||
  975. !writeFile("file_b", diff_test_cases[i].b)) {
  976. return false;
  977. }
  978. if (kwsys::SystemTools::TextFilesDiffer("file_a", "file_b") !=
  979. diff_test_cases[i].differ) {
  980. std::cerr << "Incorrect TextFilesDiffer result for test case " << i + 1
  981. << "." << std::endl;
  982. return false;
  983. }
  984. }
  985. return true;
  986. }
  987. static bool CheckCopyFileIfDifferent()
  988. {
  989. bool ret = true;
  990. const int num_test_cases =
  991. sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
  992. for (int i = 0; i < num_test_cases; ++i) {
  993. if (!writeFile("file_a", diff_test_cases[i].a) ||
  994. !writeFile("file_b", diff_test_cases[i].b)) {
  995. return false;
  996. }
  997. const char* cptarget =
  998. i < 4 ? TEST_SYSTEMTOOLS_BINARY_DIR "/file_b" : "file_b";
  999. if (!kwsys::SystemTools::CopyFileIfDifferent("file_a", cptarget)) {
  1000. std::cerr << "CopyFileIfDifferent() returned false for test case "
  1001. << i + 1 << "." << std::endl;
  1002. ret = false;
  1003. continue;
  1004. }
  1005. std::string bdata = readFile(cptarget);
  1006. if (diff_test_cases[i].a != bdata) {
  1007. std::cerr << "Incorrect CopyFileIfDifferent file contents in test case "
  1008. << i + 1 << "." << std::endl;
  1009. ret = false;
  1010. continue;
  1011. }
  1012. }
  1013. if (!kwsys::SystemTools::MakeDirectory("dir_a") ||
  1014. !kwsys::SystemTools::MakeDirectory("dir_b")) {
  1015. return false;
  1016. }
  1017. if (!kwsys::SystemTools::CopyFileIfDifferent("dir_a/", "dir_b")) {
  1018. ret = false;
  1019. }
  1020. return ret;
  1021. }
  1022. static bool CheckURLParsing()
  1023. {
  1024. bool ret = true;
  1025. std::string url = "http://user:pw@hostname:42/full/url.com";
  1026. std::string protocol, username, password, hostname, dataport, database;
  1027. kwsys::SystemTools::ParseURL(url, protocol, username, password, hostname,
  1028. dataport, database);
  1029. if (protocol != "http" || username != "user" || password != "pw" ||
  1030. hostname != "hostname" || dataport != "42" ||
  1031. database != "full/url.com") {
  1032. std::cerr << "Incorrect URL parsing" << std::endl;
  1033. ret = false;
  1034. }
  1035. std::string uri =
  1036. "file://hostname/path/to/"
  1037. "a%20file%20with%20str%C3%A0ng%C3%A8%20ch%40r%20and%20s%C2%B5aces";
  1038. kwsys::SystemTools::ParseURL(uri, protocol, username, password, hostname,
  1039. dataport, database, true);
  1040. if (protocol != "file" || hostname != "hostname" ||
  1041. database != "path/to/a file with stràngè ch@r and sµaces") {
  1042. std::cerr << "Incorrect URL parsing or decoding" << std::endl;
  1043. ret = false;
  1044. }
  1045. return ret;
  1046. }
  1047. static bool CheckSplitString()
  1048. {
  1049. bool ret = true;
  1050. auto check_split = [](std::string const& input,
  1051. std::initializer_list<const char*> expected) -> bool {
  1052. auto const components = kwsys::SystemTools::SplitString(input, '/');
  1053. if (components.size() != expected.size()) {
  1054. std::cerr << "Incorrect split count for " << input << ": "
  1055. << components.size() << std::endl;
  1056. return false;
  1057. }
  1058. size_t i = 0;
  1059. for (auto& part : expected) {
  1060. if (components[i] != part) {
  1061. std::cerr << "Incorrect split component " << i << " for " << input
  1062. << ": " << components[i] << std::endl;
  1063. return false;
  1064. }
  1065. ++i;
  1066. }
  1067. return true;
  1068. };
  1069. // No separators
  1070. ret &= check_split("nosep", { "nosep" });
  1071. // Simple
  1072. ret &= check_split("first/second", { "first", "second" });
  1073. // Separator at beginning
  1074. ret &= check_split("/starts/sep", { "", "starts", "sep" });
  1075. // Separator at end
  1076. ret &= check_split("ends/sep/", { "ends", "sep", "" });
  1077. return ret;
  1078. }
  1079. int testSystemTools(int, char* [])
  1080. {
  1081. bool res = true;
  1082. int cc;
  1083. for (cc = 0; toUnixPaths[cc][0]; cc++) {
  1084. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  1085. }
  1086. // Special check for ~
  1087. std::string output;
  1088. if (kwsys::SystemTools::GetEnv("HOME", output)) {
  1089. output += "/foo bar/lala";
  1090. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  1091. }
  1092. for (cc = 0; checkEscapeChars[cc][0]; cc++) {
  1093. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  1094. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  1095. }
  1096. res &= CheckFileOperations();
  1097. res &= CheckStringOperations();
  1098. res &= CheckEnvironmentOperations();
  1099. res &= CheckRelativePaths();
  1100. res &= CheckCollapsePath();
  1101. res &= CheckGetPath();
  1102. res &= CheckFind();
  1103. res &= CheckIsSubDirectory();
  1104. res &= CheckGetLineFromStream();
  1105. res &= CheckGetLineFromStreamLongLine();
  1106. res &= CheckGetFilenameName();
  1107. res &= CheckTextFilesDiffer();
  1108. res &= CheckCopyFileIfDifferent();
  1109. res &= CheckURLParsing();
  1110. res &= CheckSplitString();
  1111. return res ? 0 : 1;
  1112. }