+
+#if defined(__UNIX__)
+
+// Tests for functions that are changed by ShouldFollowLink()
+void FileNameTestCase::TestSymlinks()
+{
+ const wxString tmpdir(wxStandardPaths::Get().GetTempDir());
+
+ wxFileName tmpfn(wxFileName::DirName(tmpdir));
+
+ wxDateTime dtAccessTmp, dtModTmp, dtCreateTmp;
+ CPPUNIT_ASSERT(tmpfn.GetTimes(&dtAccessTmp, &dtModTmp, &dtCreateTmp));
+
+ // Create a temporary directory
+#ifdef __VMS
+ wxString name = tmpdir + ".filenametestXXXXXX]";
+ mkdir( name.char_str() , 0222 );
+ wxString tempdir = name;
+#else
+ wxString name = tmpdir + "/filenametestXXXXXX";
+ wxString tempdir = wxString::From8BitData(mkdtemp(name.char_str()));
+ tempdir << wxFileName::GetPathSeparator();
+#endif
+ wxFileName tempdirfn(wxFileName::DirName(tempdir));
+ CPPUNIT_ASSERT(tempdirfn.DirExists());
+
+ // Create a regular file in that dir, to act as a symlink target
+ wxFileName targetfn(wxFileName::CreateTempFileName(tempdir));
+ CPPUNIT_ASSERT(targetfn.FileExists());
+
+ // Create a symlink to that file
+ wxFileName linktofile(tempdir, "linktofile");
+ CPPUNIT_ASSERT_EQUAL(0, symlink(targetfn.GetFullPath().c_str(),
+ linktofile.GetFullPath().c_str()));
+
+ // ... and another to the temporary directory
+ const wxString linktodirName(tempdir + "/linktodir");
+ wxFileName linktodir(wxFileName::DirName(linktodirName));
+ CPPUNIT_ASSERT_EQUAL(0, symlink(tmpfn.GetFullPath().c_str(),
+ linktodirName.c_str()));
+
+ // And symlinks to both of those symlinks
+ wxFileName linktofilelnk(tempdir, "linktofilelnk");
+ CPPUNIT_ASSERT_EQUAL(0, symlink(linktofile.GetFullPath().c_str(),
+ linktofilelnk.GetFullPath().c_str()));
+ wxFileName linktodirlnk(tempdir, "linktodirlnk");
+ CPPUNIT_ASSERT_EQUAL(0, symlink(linktodir.GetFullPath().c_str(),
+ linktodirlnk.GetFullPath().c_str()));
+
+ // Run the tests twice: once in the default symlink following mode and the
+ // second time without following symlinks.
+ bool deref = true;
+ for ( int n = 0; n < 2; ++n, deref = !deref )
+ {
+ const std::string msg(deref ? " failed for the link target"
+ : " failed for the path itself");
+
+ if ( !deref )
+ {
+ linktofile.DontFollowLink();
+ linktodir.DontFollowLink();
+ linktofilelnk.DontFollowLink();
+ linktodirlnk.DontFollowLink();
+ }
+
+ // Test SameAs()
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Comparison with file" + msg,
+ deref, linktofile.SameAs(targetfn)
+ );
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Comparison with directory" + msg,
+ deref, linktodir.SameAs(tmpfn)
+ );
+
+ // A link-to-a-link should dereference through to the final target
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Comparison with link to a file" + msg,
+ deref,
+ linktofilelnk.SameAs(targetfn)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Comparison with link to a directory" + msg,
+ deref,
+ linktodirlnk.SameAs(tmpfn)
+ );
+
+ // Test GetTimes()
+ wxDateTime dtAccess, dtMod, dtCreate;
+ CPPUNIT_ASSERT_MESSAGE
+ (
+ "Getting times of a directory" + msg,
+ linktodir.GetTimes(&dtAccess, &dtMod, &dtCreate)
+ );
+
+ // IsEqualTo() should be true only when dereferencing. Don't test each
+ // individually: accessing to create the link will have updated some
+ bool equal = dtCreate.IsEqualTo(dtCreateTmp) &&
+ dtMod.IsEqualTo(dtModTmp) &&
+ dtAccess.IsEqualTo(dtAccessTmp);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Comparing directory times" + msg,
+ deref,
+ equal
+ );
+
+ // Test (File|Dir)Exists()
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Testing file existence" + msg,
+ deref,
+ linktofile.FileExists()
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Testing directory existence" + msg,
+ deref,
+ linktodir.DirExists()
+ );
+
+ // Test wxFileName::Exists
+ // The wxFILE_EXISTS_NO_FOLLOW flag should override DontFollowLink()
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Testing file existence" + msg,
+ false,
+ linktofile.Exists(wxFILE_EXISTS_REGULAR | wxFILE_EXISTS_NO_FOLLOW)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Testing directory existence" + msg,
+ false,
+ linktodir.Exists(wxFILE_EXISTS_DIR | wxFILE_EXISTS_NO_FOLLOW)
+ );
+ // and the static versions
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Testing file existence" + msg,
+ false,
+ wxFileName::Exists(linktofile.GetFullPath(), wxFILE_EXISTS_REGULAR | wxFILE_EXISTS_NO_FOLLOW)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Testing file existence" + msg,
+ true,
+ wxFileName::Exists(linktofile.GetFullPath(), wxFILE_EXISTS_REGULAR)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Testing directory existence" + msg,
+ false,
+ wxFileName::Exists(linktodir.GetFullPath(), wxFILE_EXISTS_DIR | wxFILE_EXISTS_NO_FOLLOW)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE
+ (
+ "Testing directory existence" + msg,
+ true,
+ wxFileName::Exists(linktodir.GetFullPath(), wxFILE_EXISTS_DIR)
+ );
+ }
+
+ // Finally test Exists() after removing the file.
+ CPPUNIT_ASSERT(wxRemoveFile(targetfn.GetFullPath()));
+ // This should succeed, as the symlink still exists and
+ // the default wxFILE_EXISTS_ANY implies wxFILE_EXISTS_NO_FOLLOW
+ CPPUNIT_ASSERT(wxFileName(tempdir, "linktofile").Exists());
+ // So should this one, as wxFILE_EXISTS_SYMLINK does too
+ CPPUNIT_ASSERT(wxFileName(tempdir, "linktofile").
+ Exists(wxFILE_EXISTS_SYMLINK));
+ // but not this one, as the now broken symlink is followed
+ CPPUNIT_ASSERT(!wxFileName(tempdir, "linktofile").
+ Exists(wxFILE_EXISTS_REGULAR));
+ CPPUNIT_ASSERT(linktofile.Exists());
+
+ // This is also a convenient place to test Rmdir() as we have things to
+ // remove.
+
+ // First, check that removing a symlink to a directory fails.
+ CPPUNIT_ASSERT( !wxFileName::Rmdir(linktodirName) );
+
+ // And recursively removing it only removes the symlink itself, not the
+ // directory.
+ CPPUNIT_ASSERT( wxFileName::Rmdir(linktodirName, wxPATH_RMDIR_RECURSIVE) );
+ CPPUNIT_ASSERT( tmpfn.Exists() );
+
+ // Finally removing the directory itself does remove everything.
+ CPPUNIT_ASSERT(tempdirfn.Rmdir(wxPATH_RMDIR_RECURSIVE));
+ CPPUNIT_ASSERT( !tempdirfn.Exists() );
+}
+
+#endif // __UNIX__