diff options
Diffstat (limited to 'Source/kwsys/testSystemTools.cxx')
-rw-r--r-- | Source/kwsys/testSystemTools.cxx | 112 |
1 files changed, 93 insertions, 19 deletions
diff --git a/Source/kwsys/testSystemTools.cxx b/Source/kwsys/testSystemTools.cxx index 3f6eeb8c1..1d3461443 100644 --- a/Source/kwsys/testSystemTools.cxx +++ b/Source/kwsys/testSystemTools.cxx @@ -20,18 +20,18 @@ // left on disk. #include <testSystemTools.h> +#include <cstdlib> /* free */ +#include <cstring> /* strcmp */ #include <iostream> #include <sstream> -#include <stdlib.h> /* free */ -#include <string.h> /* strcmp */ #if defined(_WIN32) && !defined(__CYGWIN__) -# include <io.h> /* _umask (MSVC) / umask (Borland) */ +# include <io.h> /* _umask (MSVC) */ # ifdef _MSC_VER -# define umask _umask // Note this is still umask on Borland +# define umask _umask # endif #endif #include <sys/stat.h> /* umask (POSIX), _S_I* constants (Windows) */ -// Visual C++ does not define mode_t (note that Borland does, however). +// Visual C++ does not define mode_t. #if defined(_MSC_VER) typedef unsigned short mode_t; #endif @@ -328,7 +328,14 @@ static bool CheckFileOperations() } // While we're at it, check proper TestFileAccess functionality. - if (kwsys::SystemTools::TestFileAccess(testNewFile, + bool do_write_test = true; +#if defined(__linux__) + // If we are running as root on linux ignore this check, as + // root can always write to files + do_write_test = (getuid() != 0); +#endif + if (do_write_test && + kwsys::SystemTools::TestFileAccess(testNewFile, kwsys::TEST_FILE_WRITE)) { std::cerr << "TestFileAccess incorrectly indicated that this is a writable file:" @@ -500,7 +507,7 @@ static bool CheckStringOperations() char* cres = kwsys::SystemTools::AppendStrings("Mary Had A", " Little Lamb."); - if (strcmp(cres, "Mary Had A Little Lamb.")) { + if (strcmp(cres, "Mary Had A Little Lamb.") != 0) { std::cerr << "Problem with AppendStrings " << "\"Mary Had A\" \" Little Lamb.\"" << std::endl; res = false; @@ -508,7 +515,7 @@ static bool CheckStringOperations() delete[] cres; cres = kwsys::SystemTools::AppendStrings("Mary Had", " A ", "Little Lamb."); - if (strcmp(cres, "Mary Had A Little Lamb.")) { + if (strcmp(cres, "Mary Had A Little Lamb.") != 0) { std::cerr << "Problem with AppendStrings " << "\"Mary Had\" \" A \" \"Little Lamb.\"" << std::endl; res = false; @@ -522,7 +529,7 @@ static bool CheckStringOperations() } cres = kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.", "aeiou"); - if (strcmp(cres, "Mry Hd A Lttl Lmb.")) { + if (strcmp(cres, "Mry Hd A Lttl Lmb.") != 0) { std::cerr << "Problem with RemoveChars " << "\"Mary Had A Little Lamb.\"" << std::endl; res = false; @@ -530,7 +537,7 @@ static bool CheckStringOperations() delete[] cres; cres = kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb."); - if (strcmp(cres, "A")) { + if (strcmp(cres, "A") != 0) { std::cerr << "Problem with RemoveCharsButUpperHex " << "\"Mary Had A Little Lamb.\"" << std::endl; res = false; @@ -539,7 +546,7 @@ static bool CheckStringOperations() char* cres2 = strdup("Mary Had A Little Lamb."); kwsys::SystemTools::ReplaceChars(cres2, "aeiou", 'X'); - if (strcmp(cres2, "MXry HXd A LXttlX LXmb.")) { + if (strcmp(cres2, "MXry HXd A LXttlX LXmb.") != 0) { std::cerr << "Problem with ReplaceChars " << "\"Mary Had A Little Lamb.\"" << std::endl; res = false; @@ -561,7 +568,7 @@ static bool CheckStringOperations() } cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb."); - if (strcmp(cres, "Mary Had A Little Lamb.")) { + if (strcmp(cres, "Mary Had A Little Lamb.") != 0) { std::cerr << "Problem with DuplicateString " << "\"Mary Had A Little Lamb.\"" << std::endl; res = false; @@ -721,8 +728,7 @@ static std::string StringVectorToString(const std::vector<std::string>& vec) { std::stringstream ss; ss << "vector("; - for (std::vector<std::string>::const_iterator i = vec.begin(); - i != vec.end(); ++i) { + for (auto i = vec.begin(); i != vec.end(); ++i) { if (i != vec.begin()) { ss << ", "; } @@ -743,16 +749,16 @@ static bool CheckGetPath() const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]"; std::vector<std::string> originalPaths; - originalPaths.push_back(registryPath); + originalPaths.emplace_back(registryPath); std::vector<std::string> expectedPaths; - expectedPaths.push_back(registryPath); + expectedPaths.emplace_back(registryPath); #ifdef _WIN32 expectedPaths.push_back("C:/Somewhere/something"); expectedPaths.push_back("D:/Temp"); #else - expectedPaths.push_back("/Somewhere/something"); - expectedPaths.push_back("/tmp"); + expectedPaths.emplace_back("/Somewhere/something"); + expectedPaths.emplace_back("/tmp"); #endif bool res = true; @@ -817,7 +823,7 @@ static bool CheckFind() } std::vector<std::string> searchPaths; - searchPaths.push_back(TEST_SYSTEMTOOLS_BINARY_DIR); + searchPaths.emplace_back(TEST_SYSTEMTOOLS_BINARY_DIR); if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true) .empty()) { std::cerr << "Problem with FindFile without system paths for: " @@ -1086,6 +1092,70 @@ static bool CheckCopyFileIfDifferent() return ret; } +static bool CheckURLParsing() +{ + bool ret = true; + std::string url = "http://user:pw@hostname:42/full/url.com"; + + std::string protocol, username, password, hostname, dataport, database; + kwsys::SystemTools::ParseURL(url, protocol, username, password, hostname, + dataport, database); + if (protocol != "http" || username != "user" || password != "pw" || + hostname != "hostname" || dataport != "42" || + database != "full/url.com") { + std::cerr << "Incorrect URL parsing" << std::endl; + ret = false; + } + + std::string uri = + "file://hostname/path/to/" + "a%20file%20with%20str%C3%A0ng%C3%A8%20ch%40r%20and%20s%C2%B5aces"; + kwsys::SystemTools::ParseURL(uri, protocol, username, password, hostname, + dataport, database, true); + if (protocol != "file" || hostname != "hostname" || + database != "path/to/a file with stràngè ch@r and sµaces") { + std::cerr << "Incorrect URL parsing or decoding" << std::endl; + ret = false; + } + return ret; +} + +static bool CheckSplitString() +{ + bool ret = true; + + auto check_split = [](std::string const& input, + std::initializer_list<const char*> expected) -> bool { + auto const components = kwsys::SystemTools::SplitString(input, '/'); + if (components.size() != expected.size()) { + std::cerr << "Incorrect split count for " << input << ": " + << components.size() << std::endl; + return false; + } + size_t i = 0; + for (auto& part : expected) { + if (components[i] != part) { + std::cerr << "Incorrect split component " << i << " for " << input + << ": " << components[i] << std::endl; + return false; + } + ++i; + } + return true; + }; + + // No separators + ret &= check_split("nosep", { "nosep" }); + // Simple + ret &= check_split("first/second", { "first", "second" }); + // Separator at beginning + ret &= check_split("/starts/sep", { "", "starts", "sep" }); + // Separator at end + ret &= check_split("ends/sep/", { "ends", "sep", "" }); + + return ret; +} + int testSystemTools(int, char* []) { bool res = true; @@ -1133,5 +1203,9 @@ int testSystemTools(int, char* []) res &= CheckCopyFileIfDifferent(); + res &= CheckURLParsing(); + + res &= CheckSplitString(); + return res ? 0 : 1; } |