testSystemTools.cxx 41 KB

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