/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
template <typename char_type> static nsTString<char_type> FixName(const char_type* aName) {
nsTString<char_type> name; for (uint32_t i = 0; aName[i]; ++i) {
char_type ch = aName[i]; // PR_GetPathSeparator returns the wrong value on Mac so don't use it #ifdefined(XP_WIN) if (ch == '/') {
ch = '\\';
} #endif
name.Append(ch);
} return name;
}
// Test nsIFile::AppendNative, verifying that aName is not a valid file name staticbool TestInvalidFileName(nsIFile* aBase, constchar* aName) {
nsCOMPtr<nsIFile> file = NewFile(aBase); if (!file) returnfalse;
nsCString name = FixName(aName);
nsresult rv = file->AppendNative(name); if (NS_SUCCEEDED(rv)) {
EXPECT_NS_FAILED(rv) << "AppendNative with invalid filename " << name.get(); returnfalse;
}
returntrue;
}
// Test nsIFile::Create, verifying that the file exists and did not exist // before, and leaving it there for future tests staticbool TestCreate(nsIFile* aBase, constchar* aName, int32_t aType,
int32_t aPerm) {
nsCOMPtr<nsIFile> file = NewFile(aBase); if (!file) returnfalse;
nsCString name = FixName(aName);
nsresult rv = file->AppendNative(name); if (!VerifyResult(rv, "AppendNative")) returnfalse;
rv = file->Create(aType, aPerm); if (!VerifyResult(rv, "Create")) returnfalse;
rv = file->Exists(&exists); if (!VerifyResult(rv, "Exists (after)")) returnfalse;
EXPECT_TRUE(exists) << "File " << name.get() << " was not created"; if (!exists) { returnfalse;
}
returntrue;
}
// Test nsIFile::CreateUnique, verifying that the new file exists and if it // existed before, the new file has a different name. The new file is left in // place. staticbool TestCreateUnique(nsIFile* aBase, constchar* aName, int32_t aType,
int32_t aPerm) {
nsCOMPtr<nsIFile> file = NewFile(aBase); if (!file) returnfalse;
nsCString name = FixName(aName);
nsresult rv = file->AppendNative(name); if (!VerifyResult(rv, "AppendNative")) returnfalse;
bool existsBefore;
rv = file->Exists(&existsBefore); if (!VerifyResult(rv, "Exists (before)")) returnfalse;
rv = file->CreateUnique(aType, aPerm); if (!VerifyResult(rv, "Create")) returnfalse;
bool existsAfter;
rv = file->Exists(&existsAfter); if (!VerifyResult(rv, "Exists (after)")) returnfalse;
EXPECT_TRUE(existsAfter) << "File " << name.get() << " was not created"; if (!existsAfter) { returnfalse;
}
if (existsBefore) {
nsAutoCString leafName;
rv = file->GetNativeLeafName(leafName); if (!VerifyResult(rv, "GetNativeLeafName")) returnfalse;
EXPECT_FALSE(leafName.Equals(name))
<< "File " << name.get() << " was not given a new name by CreateUnique"; if (leafName.Equals(name)) { returnfalse;
}
}
returntrue;
}
// Test nsIFile::OpenNSPRFileDesc with DELETE_ON_CLOSE, verifying that the file // exists and did not exist before, and leaving it there for future tests staticbool TestDeleteOnClose(nsIFile* aBase, constchar* aName, int32_t aFlags,
int32_t aPerm) {
nsCOMPtr<nsIFile> file = NewFile(aBase); if (!file) returnfalse;
nsCString name = FixName(aName);
nsresult rv = file->AppendNative(name); if (!VerifyResult(rv, "AppendNative")) returnfalse;
PRFileDesc* fileDesc;
rv = file->OpenNSPRFileDesc(aFlags | nsIFile::DELETE_ON_CLOSE, aPerm,
&fileDesc); if (!VerifyResult(rv, "OpenNSPRFileDesc")) returnfalse;
PRStatus status = PR_Close(fileDesc);
EXPECT_EQ(status, PR_SUCCESS)
<< "File " << name.get() << " could not be closed"; if (status != PR_SUCCESS) { returnfalse;
}
rv = file->Exists(&exists); if (!VerifyResult(rv, "Exists (after)")) returnfalse;
EXPECT_FALSE(exists) << "File " << name.get() << " was not removed on close"; if (exists) { returnfalse;
}
returntrue;
}
// Test nsIFile::Remove, verifying that the file does not exist and did before staticbool TestRemove(nsIFile* aBase, constchar* aName, bool aRecursive,
uint32_t aExpectedRemoveCount = 1) {
nsCOMPtr<nsIFile> file = NewFile(aBase); if (!file) returnfalse;
nsCString name = FixName(aName);
nsresult rv = file->AppendNative(name); if (!VerifyResult(rv, "AppendNative")) returnfalse;
bool exists;
rv = file->Exists(&exists); if (!VerifyResult(rv, "Exists (before)")) returnfalse;
EXPECT_TRUE(exists); if (!exists) { returnfalse;
}
uint32_t removeCount = 0;
rv = file->Remove(aRecursive, &removeCount); if (!VerifyResult(rv, "Remove")) returnfalse;
EXPECT_EQ(removeCount, aExpectedRemoveCount) << "Removal count was wrong";
rv = file->Exists(&exists); if (!VerifyResult(rv, "Exists (after)")) returnfalse;
EXPECT_FALSE(exists) << "File " << name.get() << " was not removed"; if (exists) { returnfalse;
}
returntrue;
}
// Test nsIFile::MoveToNative, verifying that the file did not exist at the new // location before and does afterward, and that it does not exist at the old // location anymore staticbool TestMove(nsIFile* aBase, nsIFile* aDestDir, constchar* aName, constchar* aNewName) {
nsCOMPtr<nsIFile> file = NewFile(aBase); if (!file) returnfalse;
nsCString name = FixName(aName);
nsresult rv = file->AppendNative(name); if (!VerifyResult(rv, "AppendNative")) returnfalse;
bool exists;
rv = file->Exists(&exists); if (!VerifyResult(rv, "Exists (before)")) returnfalse;
EXPECT_TRUE(exists); if (!exists) { returnfalse;
}
rv = file->Exists(&exists); if (!VerifyResult(rv, "Exists (after)")) returnfalse;
EXPECT_FALSE(exists) << "File " << name.get() << " was not moved"; if (exists) { returnfalse;
}
file = NewFile(aDestDir); if (!file) returnfalse;
rv = file->AppendNative(newName); if (!VerifyResult(rv, "AppendNative")) returnfalse; bool equal;
rv = file->Equals(newFile, &equal); if (!VerifyResult(rv, "Equals")) returnfalse;
EXPECT_TRUE(equal) << "File object was not updated to destination"; if (!equal) { returnfalse;
}
rv = file->Exists(&exists); if (!VerifyResult(rv, "Exists (new after)")) returnfalse;
EXPECT_TRUE(exists) << "Destination file " << newName.get()
<< " was not created"; if (!exists) { returnfalse;
}
returntrue;
}
// Test nsIFile::CopyToNative, verifying that the file did not exist at the new // location before and does afterward, and that it does exist at the old // location too staticbool TestCopy(nsIFile* aBase, nsIFile* aDestDir, constchar* aName, constchar* aNewName) {
nsCOMPtr<nsIFile> file = NewFile(aBase); if (!file) returnfalse;
nsCString name = FixName(aName);
nsresult rv = file->AppendNative(name); if (!VerifyResult(rv, "AppendNative")) returnfalse;
bool exists;
rv = file->Exists(&exists); if (!VerifyResult(rv, "Exists (before)")) returnfalse;
EXPECT_TRUE(exists); if (!exists) { returnfalse;
}
// Remove the directory in case tests failed and left it behind. // don't check result since it might not be there
base->Remove(true);
// Now create the working directory we're going to use
rv = base->Create(nsIFile::DIRECTORY_TYPE, 0700);
ASSERT_TRUE(VerifyResult(rv, "Creating temp directory"));
// Now we can safely normalize the path if (aTestNormalize) {
rv = base->Normalize();
ASSERT_TRUE(VerifyResult(rv, "Normalizing temp directory name"));
}
// Initialize subdir object for later use
nsCOMPtr<nsIFile> subdir = NewFile(base);
ASSERT_TRUE(subdir);
rv = subdir->AppendNative(nsDependentCString("subdir"));
ASSERT_TRUE(VerifyResult(rv, "Appending 'subdir' to test dir name"));
// --------------- // End setup code. // ---------------
// Test directory creation
ASSERT_TRUE(TestCreate(base, "subdir", nsIFile::DIRECTORY_TYPE, 0700));
// Test move and copy in the base directory
ASSERT_TRUE(TestCreate(base, "file.txt", nsIFile::NORMAL_FILE_TYPE, 0600));
ASSERT_TRUE(TestMove(base, base, "file.txt", "file2.txt"));
ASSERT_TRUE(TestCopy(base, base, "file2.txt", "file3.txt"));
// Test moving across directories
ASSERT_TRUE(TestMove(base, subdir, "file2.txt", "file2.txt"));
// Test moving across directories and renaming at the same time
ASSERT_TRUE(TestMove(subdir, base, "file2.txt", "file4.txt"));
// Test copying across directories
ASSERT_TRUE(TestCopy(base, subdir, "file4.txt", "file5.txt"));
if (aTestNormalize) { // Run normalization tests while the directory exists
ASSERT_TRUE(TestNormalizeNativePath(base, subdir));
}
// Test recursive directory removal
ASSERT_TRUE(TestRemove(base, "subdir", true, 2));
// This simulates what QM_NewLocalFile does (NS_NewLocalFiles and then // SetUseDOSDevicePathSyntax if it's on Windows for NewFile)
TEST(TestFile, PrefixedOnWin)
{
SetupAndTestFunctions(u"mozfiletests"_ns, /* aTestCreateUnique */ true, /* aTestNormalize */ true);
}
TEST(TestFile, PrefixedOnWin_PathExceedsMaxPath)
{ // We want to verify if the prefix would allow as to create a file with over // 260 char for its path. However, on Windows, the maximum length of filename // is 255. Given the base file path and we are going append some other file // to the current base file, let's assume the file path will exceed 260 so // that we are able to verify if the prefix works or not.
nsString dirName;
dirName.AssignLiteral("mozfiletests"); for (uint32_t i = 255 - dirName.Length(); i > 0; --i) {
dirName.AppendLiteral("a");
}
// Bypass the test for CreateUnique because there is a check for the max // length of the root on all platforms.
SetupAndTestFunctions(dirName, /* aTestCreateUnique */ false, /* aTestNormalize */ true);
}
TEST(TestFile, PrefixedOnWin_ComponentEndsWithPeriod)
{ // Bypass the normalization for this because it would strip the trailing // period.
SetupAndTestFunctions(u"mozfiletests."_ns, /* aTestCreateUnique */ true, /* aTestNormalize */ false);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.