testSystemTools.cxx 42 KB

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