testSystemTools.cxx 40 KB

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