1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/filename/filename.cpp
3 // Purpose: wxFileName unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2004 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include "wx/filename.h"
24 #include "wx/filefn.h"
25 #include "wx/stdpaths.h"
26 #include "wx/scopeguard.h"
29 #include "wx/msw/registry.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 static struct TestFileNameInfo
55 { "", "", "", "", "", false, wxPATH_UNIX
},
56 { "", "", "", "", "", false, wxPATH_DOS
},
57 { "", "", "", "", "", false, wxPATH_VMS
},
60 { "/usr/bin/ls", "", "/usr/bin", "ls", "", true, wxPATH_UNIX
},
61 { "/usr/bin/", "", "/usr/bin", "", "", true, wxPATH_UNIX
},
62 { "~/.zshrc", "", "~", ".zshrc", "", true, wxPATH_UNIX
},
63 { "../../foo", "", "../..", "foo", "", false, wxPATH_UNIX
},
64 { "foo.bar", "", "", "foo", "bar", false, wxPATH_UNIX
},
65 { "~/foo.bar", "", "~", "foo", "bar", true, wxPATH_UNIX
},
66 { "~user/foo.bar", "", "~user", "foo", "bar", true, wxPATH_UNIX
},
67 { "~user/", "", "~user", "", "", true, wxPATH_UNIX
},
68 { "/foo", "", "/", "foo", "", true, wxPATH_UNIX
},
69 { "Mahogany-0.60/foo.bar", "", "Mahogany-0.60", "foo", "bar", false, wxPATH_UNIX
},
70 { "/tmp/wxwin.tar.bz", "", "/tmp", "wxwin.tar", "bz", true, wxPATH_UNIX
},
73 { "foo.bar", "", "", "foo", "bar", false, wxPATH_DOS
},
74 { "\\foo.bar", "", "\\", "foo", "bar", false, wxPATH_DOS
},
75 { "c:foo.bar", "c", "", "foo", "bar", false, wxPATH_DOS
},
76 { "c:\\foo.bar", "c", "\\", "foo", "bar", true, wxPATH_DOS
},
77 { "c:\\Windows\\command.com", "c", "\\Windows", "command", "com", true, wxPATH_DOS
},
78 { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
79 "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\", "", "", true, wxPATH_DOS
},
80 { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\Program Files\\setup.exe",
81 "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\Program Files", "setup", "exe", true, wxPATH_DOS
},
84 // NB: when using the wxFileName::GetLongPath() function on these two
85 // strings, the program will hang for several seconds blocking inside
86 // Win32 GetLongPathName() function
87 { "\\\\server\\foo.bar", "server", "\\", "foo", "bar", true, wxPATH_DOS
},
88 { "\\\\server\\dir\\foo.bar", "server", "\\dir", "foo", "bar", true, wxPATH_DOS
},
91 // consecutive [back]slashes should be treated as single occurrences of
92 // them and not interpreted as share names if there is a volume name
93 { "c:\\aaa\\bbb\\ccc", "c", "\\aaa\\bbb", "ccc", "", true, wxPATH_DOS
},
94 { "c:\\\\aaa\\bbb\\ccc", "c", "\\\\aaa\\bbb", "ccc", "", true, wxPATH_DOS
},
96 // wxFileName support for Mac file names is broken currently
99 { "Volume:Dir:File", "Volume", "Dir", "File", "", true, wxPATH_MAC
},
100 { "Volume:Dir:Subdir:File", "Volume", "Dir:Subdir", "File", "", true, wxPATH_MAC
},
101 { "Volume:", "Volume", "", "", "", true, wxPATH_MAC
},
102 { ":Dir:File", "", "Dir", "File", "", false, wxPATH_MAC
},
103 { ":File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC
},
104 { "File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC
},
109 // NB: on Windows they have the same effect of the \\server\\ strings
110 // (see the note above)
111 { "device:[dir1.dir2.dir3]file.txt", "device", "dir1.dir2.dir3", "file", "txt", true, wxPATH_VMS
},
113 { "file.txt", "", "", "file", "txt", false, wxPATH_VMS
},
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 class FileNameTestCase
: public CppUnit::TestCase
123 FileNameTestCase() { }
126 CPPUNIT_TEST_SUITE( FileNameTestCase
);
127 CPPUNIT_TEST( TestConstruction
);
128 CPPUNIT_TEST( TestComparison
);
129 CPPUNIT_TEST( TestSplit
);
130 CPPUNIT_TEST( TestSetPath
);
131 CPPUNIT_TEST( TestStrip
);
132 CPPUNIT_TEST( TestNormalize
);
133 CPPUNIT_TEST( TestReplace
);
134 CPPUNIT_TEST( TestGetHumanReadable
);
136 CPPUNIT_TEST( TestShortLongPath
);
137 #endif // __WINDOWS__
138 CPPUNIT_TEST( TestUNC
);
139 CPPUNIT_TEST( TestVolumeUniqueName
);
140 CPPUNIT_TEST( TestCreateTempFileName
);
141 CPPUNIT_TEST( TestGetTimes
);
142 CPPUNIT_TEST( TestSetTimes
);
143 CPPUNIT_TEST( TestExists
);
144 CPPUNIT_TEST( TestIsSame
);
145 #if defined(__UNIX__)
146 CPPUNIT_TEST( TestSymlinks
);
148 CPPUNIT_TEST_SUITE_END();
150 void TestConstruction();
151 void TestComparison();
155 void TestNormalize();
157 void TestGetHumanReadable();
159 void TestShortLongPath();
160 #endif // __WINDOWS__
162 void TestVolumeUniqueName();
163 void TestCreateTempFileName();
168 #if defined(__UNIX__)
172 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
175 // register in the unnamed registry so that these tests are run by default
176 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
178 // also include in its own registry so that these tests can be run alone
179 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
181 void FileNameTestCase::TestConstruction()
183 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
185 const TestFileNameInfo
& fni
= filenames
[n
];
187 wxFileName
fn(fni
.fullname
, fni
.format
);
189 // the original full name could contain consecutive [back]slashes,
190 // squeeze them except for the double backslash in the beginning in
191 // Windows filenames where it has special meaning
192 wxString fullnameOrig
;
193 if ( fni
.format
== wxPATH_DOS
)
195 // copy the backslashes at beginning unchanged
196 const char *p
= fni
.fullname
;
198 fullnameOrig
+= *p
++;
200 // replace consecutive slashes with single ones in the rest
201 for ( char chPrev
= '\0'; *p
; p
++ )
203 if ( *p
== '\\' && chPrev
== '\\' )
207 fullnameOrig
+= chPrev
;
212 fullnameOrig
= fni
.fullname
;
215 fullnameOrig
.Replace("//", "/");
218 wxString fullname
= fn
.GetFullPath(fni
.format
);
219 CPPUNIT_ASSERT_EQUAL( fullnameOrig
, fullname
);
221 // notice that we use a dummy working directory to ensure that paths
222 // with "../.." in them could be normalized, otherwise this would fail
223 // if the test is run from root directory or its direct subdirectory
224 CPPUNIT_ASSERT_MESSAGE
226 (const char *)wxString::Format("Normalize(%s) failed", fni
.fullname
).mb_str(),
227 fn
.Normalize(wxPATH_NORM_ALL
, "/foo/bar/baz", fni
.format
)
230 if ( *fni
.volume
&& *fni
.path
)
232 // check that specifying the volume separately or as part of the
233 // path doesn't make any difference
234 wxString pathWithVolume
= fni
.volume
;
235 pathWithVolume
+= wxFileName::GetVolumeSeparator(fni
.format
);
236 pathWithVolume
+= fni
.path
;
238 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume
,
248 fn
.AssignDir(wxEmptyString
);
249 CPPUNIT_ASSERT( !fn
.IsOk() );
251 fn
.Assign(wxEmptyString
);
252 CPPUNIT_ASSERT( !fn
.IsOk() );
254 fn
.Assign(wxEmptyString
, wxEmptyString
);
255 CPPUNIT_ASSERT( !fn
.IsOk() );
257 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
);
258 CPPUNIT_ASSERT( !fn
.IsOk() );
260 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
, wxEmptyString
);
261 CPPUNIT_ASSERT( !fn
.IsOk() );
264 void FileNameTestCase::TestComparison()
266 wxFileName
fn1(wxT("/tmp/file1"));
267 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
270 CPPUNIT_ASSERT_EQUAL(fn1
.GetPath(), fn2
.GetPath());
273 void FileNameTestCase::TestSplit()
275 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
277 const TestFileNameInfo
& fni
= filenames
[n
];
278 wxString volume
, path
, name
, ext
;
279 wxFileName::SplitPath(fni
.fullname
,
280 &volume
, &path
, &name
, &ext
, fni
.format
);
282 CPPUNIT_ASSERT_EQUAL( wxString(fni
.volume
), volume
);
283 CPPUNIT_ASSERT_EQUAL( wxString(fni
.path
), path
);
284 CPPUNIT_ASSERT_EQUAL( wxString(fni
.name
), name
);
285 CPPUNIT_ASSERT_EQUAL( wxString(fni
.ext
), ext
);
288 // special case of empty extension
289 wxFileName
fn("foo.");
290 CPPUNIT_ASSERT_EQUAL( wxString("foo."), fn
.GetFullPath() );
293 void FileNameTestCase::TestSetPath()
295 wxFileName
fn("d:\\test\\foo.bar", wxPATH_DOS
);
296 fn
.SetPath("c:\\temp", wxPATH_DOS
);
297 CPPUNIT_ASSERT( fn
.SameAs(wxFileName("c:\\temp\\foo.bar", wxPATH_DOS
)) );
299 fn
= wxFileName("/usr/bin/ls", wxPATH_UNIX
);
300 fn
.SetPath("/usr/local/bin", wxPATH_UNIX
);
301 CPPUNIT_ASSERT( fn
.SameAs(wxFileName("/usr/local/bin/ls", wxPATH_UNIX
)) );
304 void FileNameTestCase::TestNormalize()
306 // prepare some data to be used later
307 wxString sep
= wxFileName::GetPathSeparator();
308 wxString cwd
= wxGetCwd();
309 wxString home
= wxGetUserHome();
311 cwd
.Replace(sep
, wxT("/"));
312 if (cwd
.Last() != wxT('/'))
314 home
.Replace(sep
, wxT("/"));
315 if (home
.Last() != wxT('/'))
318 // since we will always be testing paths using the wxPATH_UNIX
319 // format, we need to remove the volume, if present
320 if (home
.Contains(wxT(':')))
321 home
= home
.AfterFirst(wxT(':'));
322 if (cwd
.Contains(wxT(':')))
323 cwd
= cwd
.AfterFirst(wxT(':'));
325 static const struct FileNameTest
327 const char *original
;
329 const char *expected
;
333 // test wxPATH_NORM_ENV_VARS
335 { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS
, "abcdef/g/h/i", wxPATH_UNIX
},
337 { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS
, "abcdef/g/h/i", wxPATH_UNIX
},
340 // test wxPATH_NORM_DOTS
341 { "a/.././b/c/../../", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
342 { "", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
343 { "./foo", wxPATH_NORM_DOTS
, "foo", wxPATH_UNIX
},
344 { "b/../bar", wxPATH_NORM_DOTS
, "bar", wxPATH_UNIX
},
345 { "c/../../quux", wxPATH_NORM_DOTS
, "../quux", wxPATH_UNIX
},
346 { "/c/../../quux", wxPATH_NORM_DOTS
, "/quux", wxPATH_UNIX
},
348 // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially
349 // when it is the first character in the file name
350 { "/a/b/~", wxPATH_NORM_TILDE
, "/a/b/~", wxPATH_UNIX
},
351 { "/~/a/b", wxPATH_NORM_TILDE
, "/~/a/b", wxPATH_UNIX
},
352 { "~/a/b", wxPATH_NORM_TILDE
, "HOME/a/b", wxPATH_UNIX
},
354 // test wxPATH_NORM_CASE
355 { "Foo", wxPATH_NORM_CASE
, "Foo", wxPATH_UNIX
},
356 { "Foo", wxPATH_NORM_CASE
, "foo", wxPATH_DOS
},
357 { "C:\\Program Files\\wx", wxPATH_NORM_CASE
,
358 "c:\\program files\\wx", wxPATH_DOS
},
359 { "C:/Program Files/wx", wxPATH_NORM_ALL
| wxPATH_NORM_CASE
,
360 "c:\\program files\\wx", wxPATH_DOS
},
361 { "C:\\Users\\zeitlin", wxPATH_NORM_ALL
| wxPATH_NORM_CASE
,
362 "c:\\users\\zeitlin", wxPATH_DOS
},
364 // test wxPATH_NORM_ABSOLUTE
365 { "a/b/", wxPATH_NORM_ABSOLUTE
, "CWD/a/b/", wxPATH_UNIX
},
366 { "a/b/c.ext", wxPATH_NORM_ABSOLUTE
, "CWD/a/b/c.ext", wxPATH_UNIX
},
367 { "/a", wxPATH_NORM_ABSOLUTE
, "/a", wxPATH_UNIX
},
369 // test giving no flags at all to Normalize()
370 { "a/b/", 0, "a/b/", wxPATH_UNIX
},
371 { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX
},
372 { "/a", 0, "/a", wxPATH_UNIX
},
374 // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE
375 // for both existing and non-existent files (this is important under
376 // MSW where GetLongPathName() works only for the former)
377 { "./foo", wxPATH_NORM_LONG
, "./foo", wxPATH_UNIX
},
378 { "../foo", wxPATH_NORM_LONG
, "../foo", wxPATH_UNIX
},
379 { ".\\test.bkl", wxPATH_NORM_LONG
, ".\\test.bkl", wxPATH_DOS
},
380 { ".\\foo", wxPATH_NORM_LONG
, ".\\foo", wxPATH_DOS
},
381 { "..\\Makefile.in", wxPATH_NORM_LONG
, "..\\Makefile.in", wxPATH_DOS
},
382 { "..\\foo", wxPATH_NORM_LONG
, "..\\foo", wxPATH_DOS
},
385 // set the env var ABCDEF
386 wxSetEnv("ABCDEF", "abcdef");
388 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
390 const FileNameTest
& fnt
= tests
[i
];
391 wxFileName
fn(fnt
.original
, fnt
.fmt
);
393 // be sure this normalization does not fail
396 ("#%d: Normalize(%s) failed", (int)i
, fnt
.original
),
397 fn
.Normalize(fnt
.flags
, cwd
, fnt
.fmt
)
400 // compare result with expected string
401 wxString
expected(tests
[i
].expected
);
402 expected
.Replace("HOME/", home
);
403 expected
.Replace("CWD/", cwd
);
404 WX_ASSERT_EQUAL_MESSAGE
406 ("array element #%d", (int)i
),
407 expected
, fn
.GetFullPath(fnt
.fmt
)
411 // MSW-only test for wxPATH_NORM_LONG: notice that we only run it if short
412 // names generation is not disabled for this system as otherwise the file
413 // MKINST~1 doesn't exist at all and normalizing it fails (it's possible
414 // that we're on a FAT partition in which case the test would still succeed
415 // and also that the registry key was changed recently and didn't take
416 // effect yet but these are marginal cases which we consciously choose to
419 long shortNamesDisabled
;
423 "SYSTEM\\CurrentControlSet\\Control\\FileSystem"
424 ).QueryValue("NtfsDisable8dot3NameCreation", &shortNamesDisabled
) &&
425 !shortNamesDisabled
)
427 wxFileName
fn("..\\MKINST~1");
428 CPPUNIT_ASSERT( fn
.Normalize(wxPATH_NORM_LONG
, cwd
) );
429 CPPUNIT_ASSERT_EQUAL( "..\\mkinstalldirs", fn
.GetFullPath() );
431 //else: when in doubt, don't run the test
432 #endif // __WINDOWS__
435 void FileNameTestCase::TestReplace()
437 static const struct FileNameTest
439 const char *original
;
440 const char *env_contents
;
441 const char *replace_fmtstring
;
442 const char *expected
;
446 { "/usr/a/strange path/lib/someFile.ext", "/usr/a/strange path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX
},
447 { "/usr/a/path/lib/someFile.ext", "/usr/a/path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX
},
448 { "/usr/a/path/lib/someFile", "/usr/a/path/", "$%s", "$TEST_VARlib/someFile", wxPATH_UNIX
},
449 { "/usr/a/path/lib/", "/usr/a/path/", "$(%s)", "$(TEST_VAR)lib/", wxPATH_UNIX
},
450 { "/usr/a/path/lib/", "/usr/a/path/", "${{%s}}", "${{TEST_VAR}}lib/", wxPATH_UNIX
},
451 { "/usr/a/path/lib/", "/usr/a/path/", "%s", "TEST_VARlib/", wxPATH_UNIX
},
452 { "/usr/a/path/lib/", "/usr/a/path/", "%s//", "TEST_VAR/lib/", wxPATH_UNIX
},
453 // note: empty directory components are automatically removed by wxFileName thus
454 // using // in the replace format string has no effect
456 { "/usr/../a/path/lib/", "/usr/a/path/", "%s", "/usr/../a/path/lib/", wxPATH_UNIX
},
457 { "/usr/a/path/usr/usr", "/usr", "%s", "TEST_VAR/a/pathTEST_VAR/usr", wxPATH_UNIX
},
458 { "/usr/a/path/usr/usr", "/usr", "$%s", "$TEST_VAR/a/path$TEST_VAR/usr", wxPATH_UNIX
},
459 { "/a/b/c/d", "a/", "%s", "/TEST_VARb/c/d", wxPATH_UNIX
},
461 { "C:\\A\\Strange Path\\lib\\someFile", "C:\\A\\Strange Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN
},
462 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN
},
463 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "$(%s)", "$(TEST_VAR)\\lib\\someFile", wxPATH_WIN
}
466 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
468 const FileNameTest
& fnt
= tests
[i
];
469 wxFileName
fn(fnt
.original
, fnt
.fmt
);
471 // set the environment variable
472 wxSetEnv("TEST_VAR", fnt
.env_contents
);
474 // be sure this ReplaceEnvVariable does not fail
477 ("#%d: ReplaceEnvVariable(%s) failed", (int)i
, fnt
.replace_fmtstring
),
478 fn
.ReplaceEnvVariable("TEST_VAR", fnt
.replace_fmtstring
, fnt
.fmt
)
481 // compare result with expected string
482 wxString
expected(fnt
.expected
);
483 WX_ASSERT_EQUAL_MESSAGE
485 ("array element #%d", (int)i
),
486 expected
, fn
.GetFullPath(fnt
.fmt
)
490 // now test ReplaceHomeDir
492 wxFileName fn
= wxFileName::DirName(wxGetHomeDir());
493 fn
.AppendDir("test1");
494 fn
.AppendDir("test2");
495 fn
.AppendDir("test3");
496 fn
.SetName("some file");
500 ("ReplaceHomeDir(%s) failed", fn
.GetFullPath()),
504 CPPUNIT_ASSERT_EQUAL( wxString("~/test1/test2/test3/some file"),
505 fn
.GetFullPath(wxPATH_UNIX
) );
508 void FileNameTestCase::TestGetHumanReadable()
510 static const struct TestData
515 wxSizeConvention conv
;
518 { "NA", 0, 1, wxSIZE_CONV_TRADITIONAL
},
519 { "2.0 KB", 2000, 1, wxSIZE_CONV_TRADITIONAL
},
520 { "1.953 KiB", 2000, 3, wxSIZE_CONV_IEC
},
521 { "2.000 KB", 2000, 3, wxSIZE_CONV_SI
},
522 { "297 KB", 304351, 0, wxSIZE_CONV_TRADITIONAL
},
523 { "304 KB", 304351, 0, wxSIZE_CONV_SI
},
526 CLocaleSetter loc
; // we want to use "C" locale for LC_NUMERIC
527 // so that regardless of the system's locale
528 // the decimal point used by GetHumanReadableSize()
530 for ( unsigned n
= 0; n
< WXSIZEOF(testData
); n
++ )
532 const TestData
& td
= testData
[n
];
534 // take care of using the decimal point for the current locale before
535 // the actual comparison
539 wxFileName::GetHumanReadableSize(td
.size
, "NA", td
.prec
, td
.conv
)
543 // also test the default convention value
544 CPPUNIT_ASSERT_EQUAL( "1.4 MB", wxFileName::GetHumanReadableSize(1512993, "") );
547 void FileNameTestCase::TestStrip()
549 CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") );
550 CPPUNIT_ASSERT_EQUAL( ".", wxFileName::StripExtension(".") );
551 CPPUNIT_ASSERT_EQUAL( ".vimrc", wxFileName::StripExtension(".vimrc") );
552 CPPUNIT_ASSERT_EQUAL( "bad", wxFileName::StripExtension("bad") );
553 CPPUNIT_ASSERT_EQUAL( "good", wxFileName::StripExtension("good.wav") );
554 CPPUNIT_ASSERT_EQUAL( "good.wav", wxFileName::StripExtension("good.wav.wav") );
559 void FileNameTestCase::TestShortLongPath()
561 wxFileName
fn("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe");
563 // incredibly enough, GetLongPath() used to return different results during
564 // the first and subsequent runs, test for this
565 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
566 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
569 #endif // __WINDOWS__
571 void FileNameTestCase::TestUNC()
573 wxFileName
fn("//share/path/name.ext", wxPATH_DOS
);
574 CPPUNIT_ASSERT_EQUAL( "share", fn
.GetVolume() );
575 CPPUNIT_ASSERT_EQUAL( "\\path", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
577 fn
.Assign("\\\\share2\\path2\\name.ext", wxPATH_DOS
);
578 CPPUNIT_ASSERT_EQUAL( "share2", fn
.GetVolume() );
579 CPPUNIT_ASSERT_EQUAL( "\\path2", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
582 void FileNameTestCase::TestVolumeUniqueName()
584 wxFileName
fn("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
586 CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}",
588 CPPUNIT_ASSERT_EQUAL( "\\", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
589 CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
590 fn
.GetFullPath(wxPATH_DOS
) );
592 fn
.Assign("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\"
593 "Program Files\\setup.exe", wxPATH_DOS
);
594 CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}",
596 CPPUNIT_ASSERT_EQUAL( "\\Program Files",
597 fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
598 CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\"
599 "Program Files\\setup.exe",
600 fn
.GetFullPath(wxPATH_DOS
) );
603 void FileNameTestCase::TestCreateTempFileName()
605 static const struct TestData
608 const char *expectedFolder
;
612 { "", "$SYSTEM_TEMP", true },
613 { "foo", "$SYSTEM_TEMP", true },
614 { "..", "$SYSTEM_TEMP", true },
615 { "../bar", "..", true },
617 { "$USER_DOCS_DIR\\", "$USER_DOCS_DIR", true },
618 { "c:\\a\\directory\\which\\does\\not\\exist", "", false },
619 #elif defined( __UNIX__ )
620 { "$USER_DOCS_DIR/", "$USER_DOCS_DIR", true },
621 { "/tmp/foo", "/tmp", true },
622 { "/tmp/a/directory/which/does/not/exist", "", false },
626 for ( size_t n
= 0; n
< WXSIZEOF(testData
); n
++ )
628 wxString prefix
= testData
[n
].prefix
;
629 prefix
.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
631 std::string errDesc
= wxString::Format("failed on prefix '%s'", prefix
).ToStdString();
633 wxString path
= wxFileName::CreateTempFileName(prefix
);
634 CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc
, !testData
[n
].shouldSucceed
, path
.empty() );
636 if (testData
[n
].shouldSucceed
)
638 errDesc
+= "; path is " + path
.ToStdString();
640 // test the place where the temp file has been created
641 wxString expected
= testData
[n
].expectedFolder
;
642 expected
.Replace("$SYSTEM_TEMP", wxStandardPaths::Get().GetTempDir());
643 expected
.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
644 CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc
, expected
, wxFileName(path
).GetPath() );
646 // the temporary file is created with full permissions for the current process
647 // so we should always be able to remove it:
648 CPPUNIT_ASSERT_MESSAGE( errDesc
, wxRemoveFile(path
) );
653 void FileNameTestCase::TestGetTimes()
655 wxFileName
fn(wxFileName::CreateTempFileName("filenametest"));
656 CPPUNIT_ASSERT( fn
.IsOk() );
657 wxON_BLOCK_EXIT1( wxRemoveFile
, fn
.GetFullPath() );
659 wxDateTime dtAccess
, dtMod
, dtCreate
;
660 CPPUNIT_ASSERT( fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) );
662 // make sure all retrieved dates are equal to the current date&time
663 // with an accuracy up to 1 minute
664 CPPUNIT_ASSERT(dtCreate
.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));
665 CPPUNIT_ASSERT(dtMod
.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));
666 CPPUNIT_ASSERT(dtAccess
.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));
669 void FileNameTestCase::TestSetTimes()
671 wxFileName
fn(wxFileName::CreateTempFileName("filenametest"));
672 CPPUNIT_ASSERT( fn
.IsOk() );
673 wxON_BLOCK_EXIT1( wxRemoveFile
, fn
.GetFullPath() );
675 const wxDateTime
dtAccess(1, wxDateTime::Jan
, 2013);
676 const wxDateTime
dtModify(1, wxDateTime::Feb
, 2013);
677 const wxDateTime
dtCreate(1, wxDateTime::Mar
, 2013);
679 CPPUNIT_ASSERT( fn
.SetTimes(&dtAccess
, &dtModify
, &dtCreate
) );
681 wxDateTime dtAccess2
,
684 CPPUNIT_ASSERT( fn
.GetTimes(&dtAccess2
, &dtModify2
, &dtCreate2
) );
685 CPPUNIT_ASSERT_EQUAL( dtAccess
, dtAccess2
);
686 CPPUNIT_ASSERT_EQUAL( dtModify
, dtModify2
);
688 // Under Unix the creation time can't be set.
690 CPPUNIT_ASSERT_EQUAL( dtCreate
, dtCreate2
);
691 #endif // __WINDOWS__
694 void FileNameTestCase::TestExists()
696 wxFileName
fn(wxFileName::CreateTempFileName("filenametest"));
697 CPPUNIT_ASSERT( fn
.IsOk() );
698 wxON_BLOCK_EXIT1( wxRemoveFile
, fn
.GetFullPath() );
700 CPPUNIT_ASSERT( fn
.FileExists() );
701 CPPUNIT_ASSERT( !wxFileName::DirExists(fn
.GetFullPath()) );
703 // FIXME-VC6: This compiler crashes with
705 // fatal error C1001: INTERNAL COMPILER ERROR
706 // (compiler file 'msc1.cpp', line 1794)
708 // when compiling calls to Exists() with parameter for some reason, just
709 // disable these tests there.
711 CPPUNIT_ASSERT( fn
.Exists(wxFILE_EXISTS_REGULAR
) );
712 CPPUNIT_ASSERT( !fn
.Exists(wxFILE_EXISTS_DIR
) );
714 CPPUNIT_ASSERT( fn
.Exists() );
716 const wxString
& tempdir
= wxFileName::GetTempDir();
718 wxFileName
fileInTempDir(tempdir
, "bloordyblop");
719 CPPUNIT_ASSERT( !fileInTempDir
.Exists() );
720 CPPUNIT_ASSERT( fileInTempDir
.DirExists() );
722 wxFileName
dirTemp(wxFileName::DirName(tempdir
));
723 CPPUNIT_ASSERT( !dirTemp
.FileExists() );
724 CPPUNIT_ASSERT( dirTemp
.DirExists() );
727 CPPUNIT_ASSERT( dirTemp
.Exists(wxFILE_EXISTS_DIR
) );
728 CPPUNIT_ASSERT( !dirTemp
.Exists(wxFILE_EXISTS_REGULAR
) );
730 CPPUNIT_ASSERT( dirTemp
.Exists() );
733 CPPUNIT_ASSERT( !wxFileName::FileExists("/dev/null") );
734 CPPUNIT_ASSERT( !wxFileName::DirExists("/dev/null") );
735 CPPUNIT_ASSERT( wxFileName::Exists("/dev/null") );
736 CPPUNIT_ASSERT( wxFileName::Exists("/dev/null", wxFILE_EXISTS_DEVICE
) );
738 // These files are only guaranteed to exist under Linux.
739 // No need for wxFILE_EXISTS_NO_FOLLOW here; wxFILE_EXISTS_SYMLINK implies it
740 CPPUNIT_ASSERT( wxFileName::Exists("/dev/core", wxFILE_EXISTS_SYMLINK
) );
741 CPPUNIT_ASSERT( wxFileName::Exists("/dev/log", wxFILE_EXISTS_SOCKET
) );
744 wxString fifo
= dirTemp
.GetPath() + "/fifo";
745 if (mkfifo(fifo
.c_str(), 0600) == 0)
747 wxON_BLOCK_EXIT1(wxRemoveFile
, fifo
);
749 CPPUNIT_ASSERT( wxFileName::Exists(fifo
, wxFILE_EXISTS_FIFO
) );
755 void FileNameTestCase::TestIsSame()
757 wxFileName
fn1( wxFileName::CreateTempFileName( "filenametest1" ) );
758 CPPUNIT_ASSERT( fn1
.IsOk() );
759 wxON_BLOCK_EXIT1( wxRemoveFile
, fn1
.GetFullPath() );
761 wxFileName
fn2( wxFileName::CreateTempFileName( "filenametest2" ) );
762 CPPUNIT_ASSERT( fn2
.IsOk() );
763 wxON_BLOCK_EXIT1( wxRemoveFile
, fn2
.GetFullPath() );
765 CPPUNIT_ASSERT( fn1
.SameAs( fn1
) );
766 CPPUNIT_ASSERT( !fn1
.SameAs( fn2
) );
768 #if defined(__UNIX__)
769 // We need to create a temporary directory and a temporary link.
770 // Unfortunately we can't use wxFileName::CreateTempFileName() for neither
771 // as it creates plain files, so use tempnam() explicitly instead.
772 char* tn
= tempnam(NULL
, "wxfn1");
773 const wxString tempdir1
= wxString::From8BitData(tn
);
776 CPPUNIT_ASSERT( wxFileName::Mkdir(tempdir1
) );
777 // Unfortunately the casts are needed to select the overload we need here.
778 wxON_BLOCK_EXIT2( static_cast<bool (*)(const wxString
&, int)>(wxFileName::Rmdir
),
779 tempdir1
, static_cast<int>(wxPATH_RMDIR_RECURSIVE
) );
781 tn
= tempnam(NULL
, "wxfn2");
782 const wxString tempdir2
= wxString::From8BitData(tn
);
784 CPPUNIT_ASSERT_EQUAL( 0, symlink(tempdir1
.c_str(), tempdir2
.c_str()) );
785 wxON_BLOCK_EXIT1( wxRemoveFile
, tempdir2
);
788 wxFileName
fn3(tempdir1
, "foo");
789 wxFileName
fn4(tempdir2
, "foo");
791 // These files have different paths, hence are different.
792 CPPUNIT_ASSERT( !fn3
.SameAs(fn4
) );
794 // Create and close a file to trigger creating it.
795 wxFile(fn3
.GetFullPath(), wxFile::write
);
797 // Now that both files do exist we should be able to detect that they are
798 // actually the same file.
799 CPPUNIT_ASSERT( fn3
.SameAs(fn4
) );
803 #if defined(__UNIX__)
805 // Tests for functions that are changed by ShouldFollowLink()
806 void FileNameTestCase::TestSymlinks()
808 const wxString
tmpdir(wxStandardPaths::Get().GetTempDir());
810 wxFileName
tmpfn(wxFileName::DirName(tmpdir
));
812 wxDateTime dtAccessTmp
, dtModTmp
, dtCreateTmp
;
813 CPPUNIT_ASSERT(tmpfn
.GetTimes(&dtAccessTmp
, &dtModTmp
, &dtCreateTmp
));
815 // Create a temporary directory
817 wxString name
= tmpdir
+ ".filenametestXXXXXX]";
818 mkdir( name
.char_str() , 0222 );
819 wxString tempdir
= name
;
821 wxString name
= tmpdir
+ "/filenametestXXXXXX";
822 wxString tempdir
= wxString::From8BitData(mkdtemp(name
.char_str()));
823 tempdir
<< wxFileName::GetPathSeparator();
825 wxFileName
tempdirfn(wxFileName::DirName(tempdir
));
826 CPPUNIT_ASSERT(tempdirfn
.DirExists());
828 // Create a regular file in that dir, to act as a symlink target
829 wxFileName
targetfn(wxFileName::CreateTempFileName(tempdir
));
830 CPPUNIT_ASSERT(targetfn
.FileExists());
832 // Create a symlink to that file
833 wxFileName
linktofile(tempdir
, "linktofile");
834 CPPUNIT_ASSERT_EQUAL(0, symlink(targetfn
.GetFullPath().c_str(),
835 linktofile
.GetFullPath().c_str()));
837 // ... and another to the temporary directory
838 const wxString
linktodirName(tempdir
+ "/linktodir");
839 wxFileName
linktodir(wxFileName::DirName(linktodirName
));
840 CPPUNIT_ASSERT_EQUAL(0, symlink(tmpfn
.GetFullPath().c_str(),
841 linktodirName
.c_str()));
843 // And symlinks to both of those symlinks
844 wxFileName
linktofilelnk(tempdir
, "linktofilelnk");
845 CPPUNIT_ASSERT_EQUAL(0, symlink(linktofile
.GetFullPath().c_str(),
846 linktofilelnk
.GetFullPath().c_str()));
847 wxFileName
linktodirlnk(tempdir
, "linktodirlnk");
848 CPPUNIT_ASSERT_EQUAL(0, symlink(linktodir
.GetFullPath().c_str(),
849 linktodirlnk
.GetFullPath().c_str()));
851 // Run the tests twice: once in the default symlink following mode and the
852 // second time without following symlinks.
854 for ( int n
= 0; n
< 2; ++n
, deref
= !deref
)
856 const std::string
msg(deref
? " failed for the link target"
857 : " failed for the path itself");
861 linktofile
.DontFollowLink();
862 linktodir
.DontFollowLink();
863 linktofilelnk
.DontFollowLink();
864 linktodirlnk
.DontFollowLink();
868 CPPUNIT_ASSERT_EQUAL_MESSAGE
870 "Comparison with file" + msg
,
871 deref
, linktofile
.SameAs(targetfn
)
874 CPPUNIT_ASSERT_EQUAL_MESSAGE
876 "Comparison with directory" + msg
,
877 deref
, linktodir
.SameAs(tmpfn
)
880 // A link-to-a-link should dereference through to the final target
881 CPPUNIT_ASSERT_EQUAL_MESSAGE
883 "Comparison with link to a file" + msg
,
885 linktofilelnk
.SameAs(targetfn
)
887 CPPUNIT_ASSERT_EQUAL_MESSAGE
889 "Comparison with link to a directory" + msg
,
891 linktodirlnk
.SameAs(tmpfn
)
895 wxDateTime dtAccess
, dtMod
, dtCreate
;
896 CPPUNIT_ASSERT_MESSAGE
898 "Getting times of a directory" + msg
,
899 linktodir
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
)
902 // IsEqualTo() should be true only when dereferencing. Don't test each
903 // individually: accessing to create the link will have updated some
904 bool equal
= dtCreate
.IsEqualTo(dtCreateTmp
) &&
905 dtMod
.IsEqualTo(dtModTmp
) &&
906 dtAccess
.IsEqualTo(dtAccessTmp
);
907 CPPUNIT_ASSERT_EQUAL_MESSAGE
909 "Comparing directory times" + msg
,
914 // Test (File|Dir)Exists()
915 CPPUNIT_ASSERT_EQUAL_MESSAGE
917 "Testing file existence" + msg
,
919 linktofile
.FileExists()
921 CPPUNIT_ASSERT_EQUAL_MESSAGE
923 "Testing directory existence" + msg
,
925 linktodir
.DirExists()
928 // Test wxFileName::Exists
929 // The wxFILE_EXISTS_NO_FOLLOW flag should override DontFollowLink()
930 CPPUNIT_ASSERT_EQUAL_MESSAGE
932 "Testing file existence" + msg
,
934 linktofile
.Exists(wxFILE_EXISTS_REGULAR
| wxFILE_EXISTS_NO_FOLLOW
)
936 CPPUNIT_ASSERT_EQUAL_MESSAGE
938 "Testing directory existence" + msg
,
940 linktodir
.Exists(wxFILE_EXISTS_DIR
| wxFILE_EXISTS_NO_FOLLOW
)
942 // and the static versions
943 CPPUNIT_ASSERT_EQUAL_MESSAGE
945 "Testing file existence" + msg
,
947 wxFileName::Exists(linktofile
.GetFullPath(), wxFILE_EXISTS_REGULAR
| wxFILE_EXISTS_NO_FOLLOW
)
949 CPPUNIT_ASSERT_EQUAL_MESSAGE
951 "Testing file existence" + msg
,
953 wxFileName::Exists(linktofile
.GetFullPath(), wxFILE_EXISTS_REGULAR
)
955 CPPUNIT_ASSERT_EQUAL_MESSAGE
957 "Testing directory existence" + msg
,
959 wxFileName::Exists(linktodir
.GetFullPath(), wxFILE_EXISTS_DIR
| wxFILE_EXISTS_NO_FOLLOW
)
961 CPPUNIT_ASSERT_EQUAL_MESSAGE
963 "Testing directory existence" + msg
,
965 wxFileName::Exists(linktodir
.GetFullPath(), wxFILE_EXISTS_DIR
)
969 // Finally test Exists() after removing the file.
970 CPPUNIT_ASSERT(wxRemoveFile(targetfn
.GetFullPath()));
971 // This should succeed, as the symlink still exists and
972 // the default wxFILE_EXISTS_ANY implies wxFILE_EXISTS_NO_FOLLOW
973 CPPUNIT_ASSERT(wxFileName(tempdir
, "linktofile").Exists());
974 // So should this one, as wxFILE_EXISTS_SYMLINK does too
975 CPPUNIT_ASSERT(wxFileName(tempdir
, "linktofile").
976 Exists(wxFILE_EXISTS_SYMLINK
));
977 // but not this one, as the now broken symlink is followed
978 CPPUNIT_ASSERT(!wxFileName(tempdir
, "linktofile").
979 Exists(wxFILE_EXISTS_REGULAR
));
980 CPPUNIT_ASSERT(linktofile
.Exists());
982 // This is also a convenient place to test Rmdir() as we have things to
985 // First, check that removing a symlink to a directory fails.
986 CPPUNIT_ASSERT( !wxFileName::Rmdir(linktodirName
) );
988 // And recursively removing it only removes the symlink itself, not the
990 CPPUNIT_ASSERT( wxFileName::Rmdir(linktodirName
, wxPATH_RMDIR_RECURSIVE
) );
991 CPPUNIT_ASSERT( tmpfn
.Exists() );
993 // Finally removing the directory itself does remove everything.
994 CPPUNIT_ASSERT(tempdirfn
.Rmdir(wxPATH_RMDIR_RECURSIVE
));
995 CPPUNIT_ASSERT( !tempdirfn
.Exists() );