testSystemTools.cxx 29 KB
Newer Older
Kitware Robot's avatar
Kitware Robot committed
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
Kitware Robot's avatar
Kitware Robot committed
3
4
5
#include "kwsysPrivate.h"

#if defined(_MSC_VER)
Kitware Robot's avatar
Kitware Robot committed
6
#pragma warning(disable : 4786)
Kitware Robot's avatar
Kitware Robot committed
7
8
#endif

Kitware Robot's avatar
Kitware Robot committed
9
#include KWSYS_HEADER(FStream.hxx)
Kitware Robot's avatar
Kitware Robot committed
10
11
12
13
14
#include KWSYS_HEADER(SystemTools.hxx)

// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
Kitware Robot's avatar
Kitware Robot committed
15
#include "FStream.hxx.in"
Kitware Robot's avatar
Kitware Robot committed
16
#include "SystemTools.hxx.in"
Kitware Robot's avatar
Kitware Robot committed
17
18
19
20
21
22
#endif

// Include with <> instead of "" to avoid getting any in-source copy
// left on disk.
#include <testSystemTools.h>

Kitware Robot's avatar
Kitware Robot committed
23
#include <iostream>
Kitware Robot's avatar
Kitware Robot committed
24
#include <sstream>
Kitware Robot's avatar
Kitware Robot committed
25
#include <string.h> /* strcmp */
Kitware Robot's avatar
Kitware Robot committed
26
#if defined(_WIN32) && !defined(__CYGWIN__)
Kitware Robot's avatar
Kitware Robot committed
27
28
29
30
#include <io.h> /* _umask (MSVC) / umask (Borland) */
#ifdef _MSC_VER
#define umask _umask // Note this is still umask on Borland
#endif
Kitware Robot's avatar
Kitware Robot committed
31
32
33
#endif
#include <sys/stat.h> /* umask (POSIX), _S_I* constants (Windows) */
// Visual C++ does not define mode_t (note that Borland does, however).
Kitware Robot's avatar
Kitware Robot committed
34
#if defined(_MSC_VER)
Kitware Robot's avatar
Kitware Robot committed
35
36
typedef unsigned short mode_t;
#endif
Kitware Robot's avatar
Kitware Robot committed
37

Kitware Robot's avatar
Kitware Robot committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
static const char* toUnixPaths[][2] = {
  { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
  { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
  { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
  { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
  { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
  { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
  { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
  { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
  { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo\\ cal/bin/pa\\ sswd" },
  { 0, 0 }
Kitware Robot's avatar
Kitware Robot committed
55
56
};

Kitware Robot's avatar
Kitware Robot committed
57
58
static bool CheckConvertToUnixSlashes(std::string const& input,
                                      std::string const& output)
Kitware Robot's avatar
Kitware Robot committed
59
{
Kitware Robot's avatar
Kitware Robot committed
60
  std::string result = input;
Kitware Robot's avatar
Kitware Robot committed
61
  kwsys::SystemTools::ConvertToUnixSlashes(result);
Kitware Robot's avatar
Kitware Robot committed
62
63
64
  if (result != output) {
    std::cerr << "Problem with ConvertToUnixSlashes - input: " << input
              << " output: " << result << " expected: " << output << std::endl;
Kitware Robot's avatar
Kitware Robot committed
65
    return false;
Kitware Robot's avatar
Kitware Robot committed
66
  }
Kitware Robot's avatar
Kitware Robot committed
67
68
69
  return true;
}

Kitware Robot's avatar
Kitware Robot committed
70
71
72
73
static const char* checkEscapeChars[][4] = { { "1 foo 2 bar 2", "12", "\\",
                                               "\\1 foo \\2 bar \\2" },
                                             { " {} ", "{}", "#", " #{#} " },
                                             { 0, 0, 0, 0 } };
Kitware Robot's avatar
Kitware Robot committed
74

Kitware Robot's avatar
Kitware Robot committed
75
76
77
static bool CheckEscapeChars(std::string const& input,
                             const char* chars_to_escape, char escape_char,
                             std::string const& output)
Kitware Robot's avatar
Kitware Robot committed
78
{
Kitware Robot's avatar
Kitware Robot committed
79
  std::string result = kwsys::SystemTools::EscapeChars(
Kitware Robot's avatar
Kitware Robot committed
80
    input.c_str(), chars_to_escape, escape_char);
Kitware Robot's avatar
Kitware Robot committed
81
82
83
  if (result != output) {
    std::cerr << "Problem with CheckEscapeChars - input: " << input
              << " output: " << result << " expected: " << output << std::endl;
Kitware Robot's avatar
Kitware Robot committed
84
    return false;
Kitware Robot's avatar
Kitware Robot committed
85
  }
Kitware Robot's avatar
Kitware Robot committed
86
87
88
  return true;
}

Kitware Robot's avatar
Kitware Robot committed
89
static bool CheckFileOperations()
Kitware Robot's avatar
Kitware Robot committed
90
91
{
  bool res = true;
Kitware Robot's avatar
Kitware Robot committed
92
  const std::string testNonExistingFile(TEST_SYSTEMTOOLS_SOURCE_DIR
Kitware Robot's avatar
Kitware Robot committed
93
94
                                        "/testSystemToolsNonExistingFile");
  const std::string testDotFile(TEST_SYSTEMTOOLS_SOURCE_DIR "/.");
Kitware Robot's avatar
Kitware Robot committed
95
  const std::string testBinFile(TEST_SYSTEMTOOLS_SOURCE_DIR
Kitware Robot's avatar
Kitware Robot committed
96
                                "/testSystemTools.bin");
Kitware Robot's avatar
Kitware Robot committed
97
  const std::string testTxtFile(TEST_SYSTEMTOOLS_SOURCE_DIR
Kitware Robot's avatar
Kitware Robot committed
98
                                "/testSystemTools.cxx");
Kitware Robot's avatar
Kitware Robot committed
99
  const std::string testNewDir(TEST_SYSTEMTOOLS_BINARY_DIR
Kitware Robot's avatar
Kitware Robot committed
100
                               "/testSystemToolsNewDir");
Kitware Robot's avatar
Kitware Robot committed
101
  const std::string testNewFile(testNewDir + "/testNewFile.txt");
Kitware Robot's avatar
Kitware Robot committed
102

Kitware Robot's avatar
Kitware Robot committed
103
  if (kwsys::SystemTools::DetectFileType(testNonExistingFile.c_str()) !=
Kitware Robot's avatar
Kitware Robot committed
104
105
106
      kwsys::SystemTools::FileTypeUnknown) {
    std::cerr << "Problem with DetectFileType - failed to detect type of: "
              << testNonExistingFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
107
    res = false;
Kitware Robot's avatar
Kitware Robot committed
108
  }
Kitware Robot's avatar
Kitware Robot committed
109
110

  if (kwsys::SystemTools::DetectFileType(testDotFile.c_str()) !=
Kitware Robot's avatar
Kitware Robot committed
111
112
113
      kwsys::SystemTools::FileTypeUnknown) {
    std::cerr << "Problem with DetectFileType - failed to detect type of: "
              << testDotFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
114
    res = false;
Kitware Robot's avatar
Kitware Robot committed
115
  }
Kitware Robot's avatar
Kitware Robot committed
116

Kitware Robot's avatar
Kitware Robot committed
117
  if (kwsys::SystemTools::DetectFileType(testBinFile.c_str()) !=
Kitware Robot's avatar
Kitware Robot committed
118
119
120
      kwsys::SystemTools::FileTypeBinary) {
    std::cerr << "Problem with DetectFileType - failed to detect type of: "
              << testBinFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
121
    res = false;
Kitware Robot's avatar
Kitware Robot committed
122
  }
Kitware Robot's avatar
Kitware Robot committed
123

Kitware Robot's avatar
Kitware Robot committed
124
  if (kwsys::SystemTools::DetectFileType(testTxtFile.c_str()) !=
Kitware Robot's avatar
Kitware Robot committed
125
126
127
      kwsys::SystemTools::FileTypeText) {
    std::cerr << "Problem with DetectFileType - failed to detect type of: "
              << testTxtFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
128
    res = false;
Kitware Robot's avatar
Kitware Robot committed
129
  }
Kitware Robot's avatar
Kitware Robot committed
130

Kitware Robot's avatar
Kitware Robot committed
131
132
133
  if (kwsys::SystemTools::FileLength(testBinFile) != 766) {
    std::cerr << "Problem with FileLength - incorrect length for: "
              << testBinFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
134
    res = false;
Kitware Robot's avatar
Kitware Robot committed
135
  }
Kitware Robot's avatar
Kitware Robot committed
136

Kitware Robot's avatar
Kitware Robot committed
137
138
139
140
141
142
143
144
145
146
147
148
149
  kwsys::SystemTools::Stat_t buf;
  if (kwsys::SystemTools::Stat(testTxtFile.c_str(), &buf) != 0) {
    std::cerr << "Problem with Stat - unable to stat text file: "
              << testTxtFile << std::endl;
    res = false;
  }

  if (kwsys::SystemTools::Stat(testBinFile, &buf) != 0) {
    std::cerr << "Problem with Stat - unable to stat bin file: " << testBinFile
              << std::endl;
    res = false;
  }

Kitware Robot's avatar
Kitware Robot committed
150
151
  if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
    std::cerr << "Problem with MakeDirectory for: " << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
152
    res = false;
Kitware Robot's avatar
Kitware Robot committed
153
  }
Kitware Robot's avatar
Kitware Robot committed
154
  // calling it again should just return true
Kitware Robot's avatar
Kitware Robot committed
155
156
157
  if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
    std::cerr << "Problem with second call to MakeDirectory for: "
              << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
158
    res = false;
Kitware Robot's avatar
Kitware Robot committed
159
  }
Kitware Robot's avatar
Kitware Robot committed
160
  // calling with 0 pointer should return false
Kitware Robot's avatar
Kitware Robot committed
161
162
  if (kwsys::SystemTools::MakeDirectory(0)) {
    std::cerr << "Problem with MakeDirectory(0)" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
163
    res = false;
Kitware Robot's avatar
Kitware Robot committed
164
  }
Kitware Robot's avatar
Kitware Robot committed
165
  // calling with an empty string should return false
Kitware Robot's avatar
Kitware Robot committed
166
167
  if (kwsys::SystemTools::MakeDirectory(std::string())) {
    std::cerr << "Problem with MakeDirectory(std::string())" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
168
    res = false;
Kitware Robot's avatar
Kitware Robot committed
169
  }
Kitware Robot's avatar
Kitware Robot committed
170
  // check existence
Kitware Robot's avatar
Kitware Robot committed
171
172
173
  if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
    std::cerr << "Problem with FileExists as C string and not file for: "
              << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
174
    res = false;
Kitware Robot's avatar
Kitware Robot committed
175
  }
Kitware Robot's avatar
Kitware Robot committed
176
  // check existence
Kitware Robot's avatar
Kitware Robot committed
177
178
  if (!kwsys::SystemTools::PathExists(testNewDir)) {
    std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
179
    res = false;
Kitware Robot's avatar
Kitware Robot committed
180
  }
Kitware Robot's avatar
Kitware Robot committed
181
  // remove it
Kitware Robot's avatar
Kitware Robot committed
182
183
184
  if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
    std::cerr << "Problem with RemoveADirectory for: " << testNewDir
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
185
    res = false;
Kitware Robot's avatar
Kitware Robot committed
186
  }
Kitware Robot's avatar
Kitware Robot committed
187
  // check existence
Kitware Robot's avatar
Kitware Robot committed
188
189
190
191
  if (kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
    std::cerr << "After RemoveADirectory: "
              << "Problem with FileExists as C string and not file for: "
              << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
192
    res = false;
Kitware Robot's avatar
Kitware Robot committed
193
  }
Kitware Robot's avatar
Kitware Robot committed
194
  // check existence
Kitware Robot's avatar
Kitware Robot committed
195
196
197
  if (kwsys::SystemTools::PathExists(testNewDir)) {
    std::cerr << "After RemoveADirectory: "
              << "Problem with PathExists for: " << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
198
    res = false;
Kitware Robot's avatar
Kitware Robot committed
199
  }
Kitware Robot's avatar
Kitware Robot committed
200
  // create it using the char* version
Kitware Robot's avatar
Kitware Robot committed
201
202
203
  if (!kwsys::SystemTools::MakeDirectory(testNewDir.c_str())) {
    std::cerr << "Problem with second call to MakeDirectory as C string for: "
              << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
204
    res = false;
Kitware Robot's avatar
Kitware Robot committed
205
  }
Kitware Robot's avatar
Kitware Robot committed
206

Kitware Robot's avatar
Kitware Robot committed
207
208
  if (!kwsys::SystemTools::Touch(testNewFile.c_str(), true)) {
    std::cerr << "Problem with Touch for: " << testNewFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
209
    res = false;
Kitware Robot's avatar
Kitware Robot committed
210
  }
Kitware Robot's avatar
Kitware Robot committed
211
  // calling MakeDirectory with something that is no file should fail
Kitware Robot's avatar
Kitware Robot committed
212
213
214
  if (kwsys::SystemTools::MakeDirectory(testNewFile)) {
    std::cerr << "Problem with to MakeDirectory for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
215
    res = false;
Kitware Robot's avatar
Kitware Robot committed
216
  }
Kitware Robot's avatar
Kitware Robot committed
217
218

  // calling with 0 pointer should return false
Kitware Robot's avatar
Kitware Robot committed
219
220
  if (kwsys::SystemTools::FileExists(0)) {
    std::cerr << "Problem with FileExists(0)" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
221
    res = false;
Kitware Robot's avatar
Kitware Robot committed
222
223
224
  }
  if (kwsys::SystemTools::FileExists(0, true)) {
    std::cerr << "Problem with FileExists(0) as file" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
225
    res = false;
Kitware Robot's avatar
Kitware Robot committed
226
  }
Kitware Robot's avatar
Kitware Robot committed
227
  // calling with an empty string should return false
Kitware Robot's avatar
Kitware Robot committed
228
229
  if (kwsys::SystemTools::FileExists(std::string())) {
    std::cerr << "Problem with FileExists(std::string())" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
230
    res = false;
Kitware Robot's avatar
Kitware Robot committed
231
  }
Kitware Robot's avatar
Kitware Robot committed
232
  // FileExists(x, true) should return false on a directory
Kitware Robot's avatar
Kitware Robot committed
233
234
235
  if (kwsys::SystemTools::FileExists(testNewDir, true)) {
    std::cerr << "Problem with FileExists as file for: " << testNewDir
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
236
    res = false;
Kitware Robot's avatar
Kitware Robot committed
237
238
239
240
  }
  if (kwsys::SystemTools::FileExists(testNewDir.c_str(), true)) {
    std::cerr << "Problem with FileExists as C string and file for: "
              << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
241
    res = false;
Kitware Robot's avatar
Kitware Robot committed
242
  }
Kitware Robot's avatar
Kitware Robot committed
243
  // FileExists(x, false) should return true even on a directory
Kitware Robot's avatar
Kitware Robot committed
244
245
246
  if (!kwsys::SystemTools::FileExists(testNewDir, false)) {
    std::cerr << "Problem with FileExists as not file for: " << testNewDir
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
247
    res = false;
Kitware Robot's avatar
Kitware Robot committed
248
249
250
251
  }
  if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
    std::cerr << "Problem with FileExists as C string and not file for: "
              << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
252
    res = false;
Kitware Robot's avatar
Kitware Robot committed
253
  }
Kitware Robot's avatar
Kitware Robot committed
254
  // should work, was created as new file before
Kitware Robot's avatar
Kitware Robot committed
255
256
  if (!kwsys::SystemTools::FileExists(testNewFile)) {
    std::cerr << "Problem with FileExists for: " << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
257
    res = false;
Kitware Robot's avatar
Kitware Robot committed
258
259
260
261
  }
  if (!kwsys::SystemTools::FileExists(testNewFile.c_str())) {
    std::cerr << "Problem with FileExists as C string for: " << testNewDir
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
262
    res = false;
Kitware Robot's avatar
Kitware Robot committed
263
264
265
266
  }
  if (!kwsys::SystemTools::FileExists(testNewFile, true)) {
    std::cerr << "Problem with FileExists as file for: " << testNewDir
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
267
    res = false;
Kitware Robot's avatar
Kitware Robot committed
268
269
270
271
  }
  if (!kwsys::SystemTools::FileExists(testNewFile.c_str(), true)) {
    std::cerr << "Problem with FileExists as C string and file for: "
              << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
272
    res = false;
Kitware Robot's avatar
Kitware Robot committed
273
  }
Kitware Robot's avatar
Kitware Robot committed
274

Kitware Robot's avatar
Kitware Robot committed
275
  // calling with an empty string should return false
Kitware Robot's avatar
Kitware Robot committed
276
277
  if (kwsys::SystemTools::PathExists(std::string())) {
    std::cerr << "Problem with PathExists(std::string())" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
278
    res = false;
Kitware Robot's avatar
Kitware Robot committed
279
  }
Kitware Robot's avatar
Kitware Robot committed
280
  // PathExists(x) should return true on a directory
Kitware Robot's avatar
Kitware Robot committed
281
282
  if (!kwsys::SystemTools::PathExists(testNewDir)) {
    std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
283
    res = false;
Kitware Robot's avatar
Kitware Robot committed
284
  }
Kitware Robot's avatar
Kitware Robot committed
285
  // should work, was created as new file before
Kitware Robot's avatar
Kitware Robot committed
286
287
  if (!kwsys::SystemTools::PathExists(testNewFile)) {
    std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
Kitware Robot's avatar
Kitware Robot committed
288
    res = false;
Kitware Robot's avatar
Kitware Robot committed
289
  }
Kitware Robot's avatar
Kitware Robot committed
290

Kitware Robot's avatar
Kitware Robot committed
291
// Reset umask
Kitware Robot's avatar
Kitware Robot committed
292
293
294
295
296
297
298
299
300
301
302
#if defined(_WIN32) && !defined(__CYGWIN__)
  // NOTE:  Windows doesn't support toggling _S_IREAD.
  mode_t fullMask = _S_IWRITE;
#else
  // On a normal POSIX platform, we can toggle all permissions.
  mode_t fullMask = S_IRWXU | S_IRWXG | S_IRWXO;
#endif
  mode_t orig_umask = umask(fullMask);

  // Test file permissions without umask
  mode_t origPerm, thisPerm;
Kitware Robot's avatar
Kitware Robot committed
303
304
305
  if (!kwsys::SystemTools::GetPermissions(testNewFile, origPerm)) {
    std::cerr << "Problem with GetPermissions (1) for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
306
    res = false;
Kitware Robot's avatar
Kitware Robot committed
307
  }
Kitware Robot's avatar
Kitware Robot committed
308

Kitware Robot's avatar
Kitware Robot committed
309
310
311
  if (!kwsys::SystemTools::SetPermissions(testNewFile, 0)) {
    std::cerr << "Problem with SetPermissions (1) for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
312
    res = false;
Kitware Robot's avatar
Kitware Robot committed
313
  }
Kitware Robot's avatar
Kitware Robot committed
314

Kitware Robot's avatar
Kitware Robot committed
315
316
317
  if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
    std::cerr << "Problem with GetPermissions (2) for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
318
    res = false;
Kitware Robot's avatar
Kitware Robot committed
319
  }
Kitware Robot's avatar
Kitware Robot committed
320

Kitware Robot's avatar
Kitware Robot committed
321
322
323
324
  if ((thisPerm & fullMask) != 0) {
    std::cerr << "SetPermissions failed to set permissions (1) for: "
              << testNewFile << ": actual = " << thisPerm
              << "; expected = " << 0 << std::endl;
Kitware Robot's avatar
Kitware Robot committed
325
    res = false;
Kitware Robot's avatar
Kitware Robot committed
326
  }
Kitware Robot's avatar
Kitware Robot committed
327
328
329

  // While we're at it, check proper TestFileAccess functionality.
  if (kwsys::SystemTools::TestFileAccess(testNewFile,
Kitware Robot's avatar
Kitware Robot committed
330
                                         kwsys::TEST_FILE_WRITE)) {
Kitware Robot's avatar
Kitware Robot committed
331
    std::cerr
Kitware Robot's avatar
Kitware Robot committed
332
      << "TestFileAccess incorrectly indicated that this is a writable file:"
Kitware Robot's avatar
Kitware Robot committed
333
      << testNewFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
334
    res = false;
Kitware Robot's avatar
Kitware Robot committed
335
  }
Kitware Robot's avatar
Kitware Robot committed
336

Kitware Robot's avatar
Kitware Robot committed
337
  if (!kwsys::SystemTools::TestFileAccess(testNewFile, kwsys::TEST_FILE_OK)) {
Kitware Robot's avatar
Kitware Robot committed
338
    std::cerr
Kitware Robot's avatar
Kitware Robot committed
339
      << "TestFileAccess incorrectly indicated that this file does not exist:"
Kitware Robot's avatar
Kitware Robot committed
340
      << testNewFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
341
    res = false;
Kitware Robot's avatar
Kitware Robot committed
342
  }
Kitware Robot's avatar
Kitware Robot committed
343
344

  // Test restoring/setting full permissions.
Kitware Robot's avatar
Kitware Robot committed
345
346
347
  if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask)) {
    std::cerr << "Problem with SetPermissions (2) for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
348
    res = false;
Kitware Robot's avatar
Kitware Robot committed
349
  }
Kitware Robot's avatar
Kitware Robot committed
350

Kitware Robot's avatar
Kitware Robot committed
351
352
353
  if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
    std::cerr << "Problem with GetPermissions (3) for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
354
    res = false;
Kitware Robot's avatar
Kitware Robot committed
355
  }
Kitware Robot's avatar
Kitware Robot committed
356

Kitware Robot's avatar
Kitware Robot committed
357
358
359
360
  if ((thisPerm & fullMask) != fullMask) {
    std::cerr << "SetPermissions failed to set permissions (2) for: "
              << testNewFile << ": actual = " << thisPerm
              << "; expected = " << fullMask << std::endl;
Kitware Robot's avatar
Kitware Robot committed
361
    res = false;
Kitware Robot's avatar
Kitware Robot committed
362
  }
Kitware Robot's avatar
Kitware Robot committed
363
364

  // Test setting file permissions while honoring umask
Kitware Robot's avatar
Kitware Robot committed
365
366
367
  if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask, true)) {
    std::cerr << "Problem with SetPermissions (3) for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
368
    res = false;
Kitware Robot's avatar
Kitware Robot committed
369
  }
Kitware Robot's avatar
Kitware Robot committed
370

Kitware Robot's avatar
Kitware Robot committed
371
372
373
  if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
    std::cerr << "Problem with GetPermissions (4) for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
374
    res = false;
Kitware Robot's avatar
Kitware Robot committed
375
  }
Kitware Robot's avatar
Kitware Robot committed
376

Kitware Robot's avatar
Kitware Robot committed
377
378
379
380
  if ((thisPerm & fullMask) != 0) {
    std::cerr << "SetPermissions failed to honor umask for: " << testNewFile
              << ": actual = " << thisPerm << "; expected = " << 0
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
381
    res = false;
Kitware Robot's avatar
Kitware Robot committed
382
  }
Kitware Robot's avatar
Kitware Robot committed
383
384
385
386
387

  // Restore umask
  umask(orig_umask);

  // Restore file permissions
Kitware Robot's avatar
Kitware Robot committed
388
389
390
  if (!kwsys::SystemTools::SetPermissions(testNewFile, origPerm)) {
    std::cerr << "Problem with SetPermissions (4) for: " << testNewFile
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
391
    res = false;
Kitware Robot's avatar
Kitware Robot committed
392
  }
Kitware Robot's avatar
Kitware Robot committed
393
394

  // Remove the test file
Kitware Robot's avatar
Kitware Robot committed
395
396
  if (!kwsys::SystemTools::RemoveFile(testNewFile)) {
    std::cerr << "Problem with RemoveFile: " << testNewFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
397
    res = false;
Kitware Robot's avatar
Kitware Robot committed
398
  }
Kitware Robot's avatar
Kitware Robot committed
399

Kitware Robot's avatar
Kitware Robot committed
400
  std::string const testFileMissing(testNewDir + "/testMissingFile.txt");
Kitware Robot's avatar
Kitware Robot committed
401
  if (!kwsys::SystemTools::RemoveFile(testFileMissing)) {
Kitware Robot's avatar
Kitware Robot committed
402
    std::string const& msg = kwsys::SystemTools::GetLastSystemError();
Kitware Robot's avatar
Kitware Robot committed
403
404
    std::cerr << "RemoveFile(\"" << testFileMissing << "\") failed: " << msg
              << "\n";
Kitware Robot's avatar
Kitware Robot committed
405
    res = false;
Kitware Robot's avatar
Kitware Robot committed
406
  }
Kitware Robot's avatar
Kitware Robot committed
407

Kitware Robot's avatar
Kitware Robot committed
408
  std::string const testFileMissingDir(testNewDir + "/missing/file.txt");
Kitware Robot's avatar
Kitware Robot committed
409
  if (!kwsys::SystemTools::RemoveFile(testFileMissingDir)) {
Kitware Robot's avatar
Kitware Robot committed
410
    std::string const& msg = kwsys::SystemTools::GetLastSystemError();
Kitware Robot's avatar
Kitware Robot committed
411
412
    std::cerr << "RemoveFile(\"" << testFileMissingDir << "\") failed: " << msg
              << "\n";
Kitware Robot's avatar
Kitware Robot committed
413
    res = false;
Kitware Robot's avatar
Kitware Robot committed
414
  }
Kitware Robot's avatar
Kitware Robot committed
415

Kitware Robot's avatar
Kitware Robot committed
416
  kwsys::SystemTools::Touch(testNewFile.c_str(), true);
Kitware Robot's avatar
Kitware Robot committed
417
418
419
  if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
    std::cerr << "Problem with RemoveADirectory for: " << testNewDir
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
420
    res = false;
Kitware Robot's avatar
Kitware Robot committed
421
  }
Kitware Robot's avatar
Kitware Robot committed
422
423
424
425
426

#ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  // Perform the same file and directory creation and deletion tests but
  // with paths > 256 characters in length.

Kitware Robot's avatar
Kitware Robot committed
427
  const std::string testNewLongDir(
Kitware Robot's avatar
Kitware Robot committed
428
429
    TEST_SYSTEMTOOLS_BINARY_DIR
    "/"
Kitware Robot's avatar
Kitware Robot committed
430
431
432
433
434
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "01234567890123");
Kitware Robot's avatar
Kitware Robot committed
435
436
437
  const std::string testNewLongFile(
    testNewLongDir +
    "/"
Kitware Robot's avatar
Kitware Robot committed
438
439
440
441
442
443
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "0123456789.txt");

Kitware Robot's avatar
Kitware Robot committed
444
445
446
  if (!kwsys::SystemTools::MakeDirectory(testNewLongDir)) {
    std::cerr << "Problem with MakeDirectory for: " << testNewLongDir
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
447
    res = false;
Kitware Robot's avatar
Kitware Robot committed
448
  }
Kitware Robot's avatar
Kitware Robot committed
449

Kitware Robot's avatar
Kitware Robot committed
450
451
  if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true)) {
    std::cerr << "Problem with Touch for: " << testNewLongFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
452
    res = false;
Kitware Robot's avatar
Kitware Robot committed
453
  }
Kitware Robot's avatar
Kitware Robot committed
454

Kitware Robot's avatar
Kitware Robot committed
455
456
  if (!kwsys::SystemTools::RemoveFile(testNewLongFile)) {
    std::cerr << "Problem with RemoveFile: " << testNewLongFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
457
    res = false;
Kitware Robot's avatar
Kitware Robot committed
458
  }
Kitware Robot's avatar
Kitware Robot committed
459
460

  kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
Kitware Robot's avatar
Kitware Robot committed
461
462
463
  if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir)) {
    std::cerr << "Problem with RemoveADirectory for: " << testNewLongDir
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
464
    res = false;
Kitware Robot's avatar
Kitware Robot committed
465
  }
Kitware Robot's avatar
Kitware Robot committed
466
#endif
Kitware Robot's avatar
Kitware Robot committed
467
468
469
470

  return res;
}

Kitware Robot's avatar
Kitware Robot committed
471
static bool CheckStringOperations()
Kitware Robot's avatar
Kitware Robot committed
472
473
474
{
  bool res = true;

Kitware Robot's avatar
Kitware Robot committed
475
  std::string test = "mary had a little lamb.";
Kitware Robot's avatar
Kitware Robot committed
476
477
478
479
  if (kwsys::SystemTools::CapitalizedWords(test) !=
      "Mary Had A Little Lamb.") {
    std::cerr << "Problem with CapitalizedWords " << '"' << test << '"'
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
480
    res = false;
Kitware Robot's avatar
Kitware Robot committed
481
  }
Kitware Robot's avatar
Kitware Robot committed
482
483

  test = "Mary Had A Little Lamb.";
Kitware Robot's avatar
Kitware Robot committed
484
  if (kwsys::SystemTools::UnCapitalizedWords(test) !=
Kitware Robot's avatar
Kitware Robot committed
485
486
487
      "mary had a little lamb.") {
    std::cerr << "Problem with UnCapitalizedWords " << '"' << test << '"'
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
488
    res = false;
Kitware Robot's avatar
Kitware Robot committed
489
  }
Kitware Robot's avatar
Kitware Robot committed
490
491

  test = "MaryHadTheLittleLamb.";
Kitware Robot's avatar
Kitware Robot committed
492
  if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
Kitware Robot's avatar
Kitware Robot committed
493
494
495
      "Mary Had The Little Lamb.") {
    std::cerr << "Problem with AddSpaceBetweenCapitalizedWords " << '"' << test
              << '"' << std::endl;
Kitware Robot's avatar
Kitware Robot committed
496
    res = false;
Kitware Robot's avatar
Kitware Robot committed
497
  }
Kitware Robot's avatar
Kitware Robot committed
498

Kitware Robot's avatar
Kitware Robot committed
499
500
501
502
503
  char* cres =
    kwsys::SystemTools::AppendStrings("Mary Had A", " Little Lamb.");
  if (strcmp(cres, "Mary Had A Little Lamb.")) {
    std::cerr << "Problem with AppendStrings "
              << "\"Mary Had A\" \" Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
504
    res = false;
Kitware Robot's avatar
Kitware Robot committed
505
506
  }
  delete[] cres;
Kitware Robot's avatar
Kitware Robot committed
507

Kitware Robot's avatar
Kitware Robot committed
508
509
510
511
  cres = kwsys::SystemTools::AppendStrings("Mary Had", " A ", "Little Lamb.");
  if (strcmp(cres, "Mary Had A Little Lamb.")) {
    std::cerr << "Problem with AppendStrings "
              << "\"Mary Had\" \" A \" \"Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
512
    res = false;
Kitware Robot's avatar
Kitware Robot committed
513
514
  }
  delete[] cres;
Kitware Robot's avatar
Kitware Robot committed
515

Kitware Robot's avatar
Kitware Robot committed
516
517
518
  if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.", 'a') != 3) {
    std::cerr << "Problem with CountChar "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
519
    res = false;
Kitware Robot's avatar
Kitware Robot committed
520
  }
Kitware Robot's avatar
Kitware Robot committed
521

Kitware Robot's avatar
Kitware Robot committed
522
523
524
525
  cres = kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.", "aeiou");
  if (strcmp(cres, "Mry Hd A Lttl Lmb.")) {
    std::cerr << "Problem with RemoveChars "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
526
    res = false;
Kitware Robot's avatar
Kitware Robot committed
527
528
  }
  delete[] cres;
Kitware Robot's avatar
Kitware Robot committed
529

Kitware Robot's avatar
Kitware Robot committed
530
531
532
533
  cres = kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  if (strcmp(cres, "A")) {
    std::cerr << "Problem with RemoveCharsButUpperHex "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
534
    res = false;
Kitware Robot's avatar
Kitware Robot committed
535
536
  }
  delete[] cres;
Kitware Robot's avatar
Kitware Robot committed
537

Kitware Robot's avatar
Kitware Robot committed
538
539
540
541
542
543
  char* cres2 = new char[strlen("Mary Had A Little Lamb.") + 1];
  strcpy(cres2, "Mary Had A Little Lamb.");
  kwsys::SystemTools::ReplaceChars(cres2, "aeiou", 'X');
  if (strcmp(cres2, "MXry HXd A LXttlX LXmb.")) {
    std::cerr << "Problem with ReplaceChars "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
544
    res = false;
Kitware Robot's avatar
Kitware Robot committed
545
546
  }
  delete[] cres2;
Kitware Robot's avatar
Kitware Robot committed
547
548

  if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
Kitware Robot's avatar
Kitware Robot committed
549
550
551
                                            "Mary ")) {
    std::cerr << "Problem with StringStartsWith "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
552
    res = false;
Kitware Robot's avatar
Kitware Robot committed
553
  }
Kitware Robot's avatar
Kitware Robot committed
554
555

  if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
Kitware Robot's avatar
Kitware Robot committed
556
557
558
                                          " Lamb.")) {
    std::cerr << "Problem with StringEndsWith "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
559
    res = false;
Kitware Robot's avatar
Kitware Robot committed
560
  }
Kitware Robot's avatar
Kitware Robot committed
561
562

  cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
Kitware Robot's avatar
Kitware Robot committed
563
564
565
  if (strcmp(cres, "Mary Had A Little Lamb.")) {
    std::cerr << "Problem with DuplicateString "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
566
    res = false;
Kitware Robot's avatar
Kitware Robot committed
567
568
  }
  delete[] cres;
Kitware Robot's avatar
Kitware Robot committed
569
570

  test = "Mary Had A Little Lamb.";
Kitware Robot's avatar
Kitware Robot committed
571
572
573
  if (kwsys::SystemTools::CropString(test, 13) != "Mary ...Lamb.") {
    std::cerr << "Problem with CropString "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
574
    res = false;
Kitware Robot's avatar
Kitware Robot committed
575
  }
Kitware Robot's avatar
Kitware Robot committed
576

Kitware Robot's avatar
Kitware Robot committed
577
  std::vector<std::string> lines;
Kitware Robot's avatar
Kitware Robot committed
578
579
580
581
582
  kwsys::SystemTools::Split("Mary Had A Little Lamb.", lines, ' ');
  if (lines[0] != "Mary" || lines[1] != "Had" || lines[2] != "A" ||
      lines[3] != "Little" || lines[4] != "Lamb.") {
    std::cerr << "Problem with Split "
              << "\"Mary Had A Little Lamb.\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
583
    res = false;
Kitware Robot's avatar
Kitware Robot committed
584
  }
Kitware Robot's avatar
Kitware Robot committed
585

Kitware Robot's avatar
Kitware Robot committed
586
587
588
589
590
  if (kwsys::SystemTools::ConvertToWindowsOutputPath(
        "L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
      "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
    std::cerr << "Problem with ConvertToWindowsOutputPath "
              << "\"L://Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
591
    res = false;
Kitware Robot's avatar
Kitware Robot committed
592
  }
Kitware Robot's avatar
Kitware Robot committed
593

Kitware Robot's avatar
Kitware Robot committed
594
595
596
597
598
599
  if (kwsys::SystemTools::ConvertToWindowsOutputPath(
        "//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
      "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"") {
    std::cerr << "Problem with ConvertToWindowsOutputPath "
              << "\"//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo\""
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
600
    res = false;
Kitware Robot's avatar
Kitware Robot committed
601
  }
Kitware Robot's avatar
Kitware Robot committed
602

Kitware Robot's avatar
Kitware Robot committed
603
604
605
606
607
  if (kwsys::SystemTools::ConvertToUnixOutputPath(
        "//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
      "//Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo") {
    std::cerr << "Problem with ConvertToUnixOutputPath "
              << "\"//Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
608
    res = false;
Kitware Robot's avatar
Kitware Robot committed
609
  }
Kitware Robot's avatar
Kitware Robot committed
610
611
612
613

  return res;
}

Kitware Robot's avatar
Kitware Robot committed
614
615
static bool CheckPutEnv(const std::string& env, const char* name,
                        const char* value)
Kitware Robot's avatar
Kitware Robot committed
616
{
Kitware Robot's avatar
Kitware Robot committed
617
618
  if (!kwsys::SystemTools::PutEnv(env)) {
    std::cerr << "PutEnv(\"" << env << "\") failed!" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
619
    return false;
Kitware Robot's avatar
Kitware Robot committed
620
  }
Kitware Robot's avatar
Kitware Robot committed
621
622
  std::string v = "(null)";
  kwsys::SystemTools::GetEnv(name, v);
Kitware Robot's avatar
Kitware Robot committed
623
624
625
  if (v != value) {
    std::cerr << "GetEnv(\"" << name << "\") returned \"" << v << "\", not \""
              << value << "\"!" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
626
    return false;
Kitware Robot's avatar
Kitware Robot committed
627
  }
Kitware Robot's avatar
Kitware Robot committed
628
629
630
  return true;
}

Kitware Robot's avatar
Kitware Robot committed
631
static bool CheckUnPutEnv(const char* env, const char* name)
Kitware Robot's avatar
Kitware Robot committed
632
{
Kitware Robot's avatar
Kitware Robot committed
633
634
  if (!kwsys::SystemTools::UnPutEnv(env)) {
    std::cerr << "UnPutEnv(\"" << env << "\") failed!" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
635
    return false;
Kitware Robot's avatar
Kitware Robot committed
636
  }
Kitware Robot's avatar
Kitware Robot committed
637
  std::string v;
Kitware Robot's avatar
Kitware Robot committed
638
639
640
  if (kwsys::SystemTools::GetEnv(name, v)) {
    std::cerr << "GetEnv(\"" << name << "\") returned \"" << v
              << "\", not (null)!" << std::endl;
Kitware Robot's avatar
Kitware Robot committed
641
    return false;
Kitware Robot's avatar
Kitware Robot committed
642
  }
Kitware Robot's avatar
Kitware Robot committed
643
644
645
  return true;
}

Kitware Robot's avatar
Kitware Robot committed
646
static bool CheckEnvironmentOperations()
Kitware Robot's avatar
Kitware Robot committed
647
648
649
650
651
652
653
654
655
656
657
658
659
{
  bool res = true;
  res &= CheckPutEnv("A=B", "A", "B");
  res &= CheckPutEnv("B=C", "B", "C");
  res &= CheckPutEnv("C=D", "C", "D");
  res &= CheckPutEnv("D=E", "D", "E");
  res &= CheckUnPutEnv("A", "A");
  res &= CheckUnPutEnv("B=", "B");
  res &= CheckUnPutEnv("C=D", "C");
  /* Leave "D=E" in environment so a memory checker can test for leaks.  */
  return res;
}

Kitware Robot's avatar
Kitware Robot committed
660
661
662
static bool CheckRelativePath(const std::string& local,
                              const std::string& remote,
                              const std::string& expected)
Kitware Robot's avatar
Kitware Robot committed
663
{
Kitware Robot's avatar
Kitware Robot committed
664
  std::string result = kwsys::SystemTools::RelativePath(local, remote);
Kitware Robot's avatar
Kitware Robot committed
665
666
667
  if (!kwsys::SystemTools::ComparePath(expected, result)) {
    std::cerr << "RelativePath(" << local << ", " << remote << ")  yielded "
              << result << " instead of " << expected << std::endl;
Kitware Robot's avatar
Kitware Robot committed
668
    return false;
Kitware Robot's avatar
Kitware Robot committed
669
  }
Kitware Robot's avatar
Kitware Robot committed
670
671
672
673
674
675
676
677
678
  return true;
}

static bool CheckRelativePaths()
{
  bool res = true;
  res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
  res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
  res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
Kitware Robot's avatar
Kitware Robot committed
679
680
  res &=
    CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
Kitware Robot's avatar
Kitware Robot committed
681
682
683
684
  res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
  return res;
}

Kitware Robot's avatar
Kitware Robot committed
685
686
static bool CheckCollapsePath(const std::string& path,
                              const std::string& expected)
Kitware Robot's avatar
Kitware Robot committed
687
{
Kitware Robot's avatar
Kitware Robot committed
688
  std::string result = kwsys::SystemTools::CollapseFullPath(path);
Kitware Robot's avatar
Kitware Robot committed
689
690
691
  if (!kwsys::SystemTools::ComparePath(expected, result)) {
    std::cerr << "CollapseFullPath(" << path << ")  yielded " << result
              << " instead of " << expected << std::endl;
Kitware Robot's avatar
Kitware Robot committed
692
    return false;
Kitware Robot's avatar
Kitware Robot committed
693
  }
Kitware Robot's avatar
Kitware Robot committed
694
695
696
697
698
699
700
701
702
703
704
  return true;
}

static bool CheckCollapsePath()
{
  bool res = true;
  res &= CheckCollapsePath("/usr/share/*", "/usr/share/*");
  res &= CheckCollapsePath("C:/Windows/*", "C:/Windows/*");
  return res;
}

Kitware Robot's avatar
Kitware Robot committed
705
706
707
708
709
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();
Kitware Robot's avatar
Kitware Robot committed
710
711
       i != vec.end(); ++i) {
    if (i != vec.begin()) {
Kitware Robot's avatar
Kitware Robot committed
712
713
      ss << ", ";
    }
Kitware Robot's avatar
Kitware Robot committed
714
715
    ss << *i;
  }
Kitware Robot's avatar
Kitware Robot committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
  ss << ")";
  return ss.str();
}

static bool CheckGetPath()
{
  const char* envName = "S";
#ifdef _WIN32
  const char* envValue = "C:\\Somewhere\\something;D:\\Temp";
#else
  const char* envValue = "/Somewhere/something:/tmp";
#endif
  const char* registryPath = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp; MyKey]";

  std::vector<std::string> originalPathes;
  originalPathes.push_back(registryPath);

  std::vector<std::string> expectedPathes;
  expectedPathes.push_back(registryPath);
#ifdef _WIN32
  expectedPathes.push_back("C:/Somewhere/something");
  expectedPathes.push_back("D:/Temp");
#else
  expectedPathes.push_back("/Somewhere/something");
  expectedPathes.push_back("/tmp");
#endif

  bool res = true;
  res &= CheckPutEnv(std::string(envName) + "=" + envValue, envName, envValue);

  std::vector<std::string> pathes = originalPathes;
  kwsys::SystemTools::GetPath(pathes, envName);

Kitware Robot's avatar
Kitware Robot committed
749
750
751
752
753
  if (pathes != expectedPathes) {
    std::cerr << "GetPath(" << StringVectorToString(originalPathes) << ", "
              << envName << ")  yielded " << StringVectorToString(pathes)
              << " instead of " << StringVectorToString(expectedPathes)
              << std::endl;
Kitware Robot's avatar
Kitware Robot committed
754
    res = false;
Kitware Robot's avatar
Kitware Robot committed
755
  }
Kitware Robot's avatar
Kitware Robot committed
756
757
758
759
760

  res &= CheckUnPutEnv(envName, envName);
  return res;
}

Kitware Robot's avatar
Kitware Robot committed
761
762
763
764
static bool CheckFind()
{
  bool res = true;
  const std::string testFindFileName("testFindFile.txt");
Kitware Robot's avatar
Kitware Robot committed
765
766
  const std::string testFindFile(TEST_SYSTEMTOOLS_BINARY_DIR "/" +
                                 testFindFileName);
Kitware Robot's avatar
Kitware Robot committed
767

Kitware Robot's avatar
Kitware Robot committed
768
769
  if (!kwsys::SystemTools::Touch(testFindFile.c_str(), true)) {
    std::cerr << "Problem with Touch for: " << testFindFile << std::endl;
Kitware Robot's avatar
Kitware Robot committed
770
771
    // abort here as the existence of the file only makes the test meaningful
    return false;
Kitware Robot's avatar
Kitware Robot committed
772
  }
Kitware Robot's avatar
Kitware Robot committed
773
774
775

  std::vector<std::string> searchPaths;
  searchPaths.push_back(TEST_SYSTEMTOOLS_BINARY_DIR);
Kitware Robot's avatar
Kitware Robot committed
776
777
778
779
  if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, true)
        .empty()) {
    std::cerr << "Problem with FindFile without system paths for: "
              << testFindFileName << std::endl;
Kitware Robot's avatar
Kitware Robot committed
780
    res = false;
Kitware Robot's avatar
Kitware Robot committed
781
782
783
784
785
  }
  if (kwsys::SystemTools::FindFile(testFindFileName, searchPaths, false)
        .empty()) {
    std::cerr << "Problem with FindFile with system paths for: "
              << testFindFileName << std::endl;
Kitware Robot's avatar
Kitware Robot committed
786
    res = false;
Kitware Robot's avatar
Kitware Robot committed
787
  }
Kitware Robot's avatar
Kitware Robot committed
788
789
790
791

  return res;
}

Kitware Robot's avatar
Kitware Robot committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
static bool CheckGetLineFromStream()
{
  const std::string fileWithFiveCharsOnFirstLine(TEST_SYSTEMTOOLS_SOURCE_DIR
                                                 "/README.rst");

  kwsys::ifstream file(fileWithFiveCharsOnFirstLine.c_str(), std::ios::in);

  if (!file) {
    std::cerr << "Problem opening: " << fileWithFiveCharsOnFirstLine
              << std::endl;
    return false;
  }

  std::string line;
  bool has_newline = false;
  bool result;

  file.seekg(0, std::ios::beg);
  result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  if (!result || line.size() != 5) {
    std::cerr << "First line does not have five characters: " << line.size()
              << std::endl;
    return false;
  }

  file.seekg(0, std::ios::beg);
  result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline, -1);
  if (!result || line.size() != 5) {
    std::cerr << "First line does not have five characters after rewind: "
              << line.size() << std::endl;
    return false;
  }

  bool ret = true;

  for (size_t size = 1; size <= 5; ++size) {
    file.seekg(0, std::ios::beg);
    result = kwsys::SystemTools::GetLineFromStream(file, line, &has_newline,
                                                   static_cast<long>(size));
    if (!result || line.size() != size) {
      std::cerr << "Should have read " << size << " characters but got "
                << line.size() << std::endl;
      ret = false;
    }
  }

  return ret;
}

Kitware Robot's avatar
Kitware Robot committed
841
int testSystemTools(int, char* [])
Kitware Robot's avatar
Kitware Robot committed
842
843
844
845
{
  bool res = true;

  int cc;
Kitware Robot's avatar
Kitware Robot committed
846
  for (cc = 0; toUnixPaths[cc][0]; cc++) {
Kitware Robot's avatar
Kitware Robot committed
847
    res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
Kitware Robot's avatar
Kitware Robot committed
848
  }
Kitware Robot's avatar
Kitware Robot committed
849
850

  // Special check for ~
Kitware Robot's avatar
Kitware Robot committed
851
  std::string output;
Kitware Robot's avatar
Kitware Robot committed
852
  if (kwsys::SystemTools::GetEnv("HOME", output)) {
Kitware Robot's avatar
Kitware Robot committed
853
854
    output += "/foo bar/lala";
    res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
Kitware Robot's avatar
Kitware Robot committed
855
  }
Kitware Robot's avatar
Kitware Robot committed
856

Kitware Robot's avatar
Kitware Robot committed
857
  for (cc = 0; checkEscapeChars[cc][0]; cc++) {
Kitware Robot's avatar
Kitware Robot committed
858
    res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
Kitware Robot's avatar
Kitware Robot committed
859
                            *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
Kitware Robot's avatar
Kitware Robot committed
860
  }
Kitware Robot's avatar
Kitware Robot committed
861
862
863
864
865
866
867

  res &= CheckFileOperations();

  res &= CheckStringOperations();

  res &= CheckEnvironmentOperations();

Kitware Robot's avatar
Kitware Robot committed
868
869
870
871
  res &= CheckRelativePaths();

  res &= CheckCollapsePath();

Kitware Robot's avatar
Kitware Robot committed
872
873
  res &= CheckGetPath();

Kitware Robot's avatar
Kitware Robot committed
874
875
  res &= CheckFind();

Kitware Robot's avatar
Kitware Robot committed
876
877
  res &= CheckGetLineFromStream();

Kitware Robot's avatar
Kitware Robot committed
878
879
  return res ? 0 : 1;
}