testSystemTools.cxx 40 KB

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