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