2009-03-23 17:44:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
#include "prio.h"
|
|
|
|
#include "prsystem.h"
|
|
|
|
|
|
|
|
#include "TestHarness.h"
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2009-03-23 17:44:37 -07:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
|
|
|
|
static const char* gFunction = "main";
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool VerifyResult(nsresult aRV, const char* aMsg)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
if (NS_FAILED(aRV)) {
|
|
|
|
fail("%s %s, rv=%x", gFunction, aMsg, aRV);
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
static already_AddRefed<nsIFile> NewFile(nsIFile* aBase)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file =
|
2009-03-23 17:44:37 -07:00
|
|
|
do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
|
2012-06-05 19:08:30 -07:00
|
|
|
VerifyResult(rv, "Creating nsIFile");
|
|
|
|
rv = file->InitWithFile(aBase);
|
2009-03-23 17:44:37 -07:00
|
|
|
VerifyResult(rv, "InitWithFile");
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsCString FixName(const char* aName)
|
|
|
|
{
|
|
|
|
nsCString name;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; aName[i]; ++i) {
|
2009-03-23 17:44:37 -07:00
|
|
|
char ch = aName[i];
|
|
|
|
// PR_GetPathSeparator returns the wrong value on Mac so don't use it
|
2014-02-10 14:57:01 -08:00
|
|
|
#if defined(XP_WIN)
|
2009-03-23 17:44:37 -07:00
|
|
|
if (ch == '/') {
|
|
|
|
ch = '\\';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
name.Append(ch);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test nsIFile::AppendNative, verifying that aName is not a valid file name
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool TestInvalidFileName(nsIFile* aBase, const char* aName)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestInvalidFileName";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aBase);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
nsCString name = FixName(aName);
|
|
|
|
nsresult rv = file->AppendNative(name);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
fail("%s AppendNative with invalid filename %s", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test nsIFile::Create, verifying that the file exists and did not exist before,
|
|
|
|
// and leaving it there for future tests
|
2012-08-22 08:56:38 -07:00
|
|
|
static bool TestCreate(nsIFile* aBase, const char* aName, int32_t aType, int32_t aPerm)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestCreate";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aBase);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
nsCString name = FixName(aName);
|
|
|
|
nsresult rv = file->AppendNative(name);
|
|
|
|
if (!VerifyResult(rv, "AppendNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool exists;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (before)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (exists) {
|
|
|
|
fail("%s File %s already exists", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = file->Create(aType, aPerm);
|
|
|
|
if (!VerifyResult(rv, "Create"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (after)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!exists) {
|
|
|
|
fail("%s File %s was not created", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2012-08-22 08:56:38 -07:00
|
|
|
static bool TestCreateUnique(nsIFile* aBase, const char* aName, int32_t aType, int32_t aPerm)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestCreateUnique";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aBase);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
nsCString name = FixName(aName);
|
|
|
|
nsresult rv = file->AppendNative(name);
|
|
|
|
if (!VerifyResult(rv, "AppendNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool existsBefore;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Exists(&existsBefore);
|
|
|
|
if (!VerifyResult(rv, "Exists (before)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
rv = file->CreateUnique(aType, aPerm);
|
|
|
|
if (!VerifyResult(rv, "Create"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool existsAfter;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Exists(&existsAfter);
|
|
|
|
if (!VerifyResult(rv, "Exists (after)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!existsAfter) {
|
|
|
|
fail("%s File %s was not created", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (existsBefore) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString leafName;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->GetNativeLeafName(leafName);
|
|
|
|
if (!VerifyResult(rv, "GetNativeLeafName"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (leafName.Equals(name)) {
|
|
|
|
fail("%s File %s was not given a new name by CreateUnique", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
// Test nsIFile::OpenNSPRFileDesc with DELETE_ON_CLOSE, verifying that the file exists
|
2009-03-23 17:44:37 -07:00
|
|
|
// and did not exist before, and leaving it there for future tests
|
2012-08-22 08:56:38 -07:00
|
|
|
static bool TestDeleteOnClose(nsIFile* aBase, const char* aName, int32_t aFlags, int32_t aPerm)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestDeleteOnClose";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aBase);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
nsCString name = FixName(aName);
|
|
|
|
nsresult rv = file->AppendNative(name);
|
|
|
|
if (!VerifyResult(rv, "AppendNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool exists;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (before)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (exists) {
|
|
|
|
fail("%s File %s already exists", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRFileDesc* fileDesc;
|
2012-06-05 19:08:30 -07:00
|
|
|
rv = file->OpenNSPRFileDesc(aFlags | nsIFile::DELETE_ON_CLOSE, aPerm, &fileDesc);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!VerifyResult(rv, "OpenNSPRFileDesc"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
PRStatus status = PR_Close(fileDesc);
|
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
fail("%s File %s could not be closed", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (after)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (exists) {
|
|
|
|
fail("%s File %s was not removed on close!", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
2011-05-01 11:59:24 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test nsIFile::Remove, verifying that the file does not exist and did before
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool TestRemove(nsIFile* aBase, const char* aName, bool aRecursive)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestDelete";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aBase);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
nsCString name = FixName(aName);
|
|
|
|
nsresult rv = file->AppendNative(name);
|
|
|
|
if (!VerifyResult(rv, "AppendNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool exists;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (before)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!exists) {
|
|
|
|
fail("%s File %s does not exist", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = file->Remove(aRecursive);
|
|
|
|
if (!VerifyResult(rv, "Remove"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (after)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (exists) {
|
|
|
|
fail("%s File %s was not removed", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool TestMove(nsIFile* aBase, nsIFile* aDestDir, const char* aName, const char* aNewName)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestMove";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aBase);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
nsCString name = FixName(aName);
|
|
|
|
nsresult rv = file->AppendNative(name);
|
|
|
|
if (!VerifyResult(rv, "AppendNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool exists;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (before)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!exists) {
|
|
|
|
fail("%s File %s does not exist", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> newFile = NewFile(file);
|
2009-03-23 17:44:37 -07:00
|
|
|
nsCString newName = FixName(aNewName);
|
|
|
|
rv = newFile->MoveToNative(aDestDir, newName);
|
|
|
|
if (!VerifyResult(rv, "MoveToNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (after)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (exists) {
|
|
|
|
fail("%s File %s was not moved", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
file = NewFile(aDestDir);
|
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->AppendNative(newName);
|
|
|
|
if (!VerifyResult(rv, "AppendNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equal;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Equals(newFile, &equal);
|
|
|
|
if (!VerifyResult(rv, "Equals"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!equal) {
|
|
|
|
fail("%s file object was not updated to destination", gFunction);
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (new after)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!exists) {
|
|
|
|
fail("%s Destination file %s was not created", gFunction, newName.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool TestCopy(nsIFile* aBase, nsIFile* aDestDir, const char* aName, const char* aNewName)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestCopy";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aBase);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
nsCString name = FixName(aName);
|
|
|
|
nsresult rv = file->AppendNative(name);
|
|
|
|
if (!VerifyResult(rv, "AppendNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool exists;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (before)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!exists) {
|
|
|
|
fail("%s File %s does not exist", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> newFile = NewFile(file);
|
2009-03-23 17:44:37 -07:00
|
|
|
nsCString newName = FixName(aNewName);
|
|
|
|
rv = newFile->CopyToNative(aDestDir, newName);
|
|
|
|
if (!VerifyResult(rv, "MoveToNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equal;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->Equals(newFile, &equal);
|
|
|
|
if (!VerifyResult(rv, "Equals"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!equal) {
|
|
|
|
fail("%s file object updated unexpectedly", gFunction);
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (after)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!exists) {
|
|
|
|
fail("%s File %s was removed", gFunction, name.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
file = NewFile(aDestDir);
|
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = file->AppendNative(newName);
|
|
|
|
if (!VerifyResult(rv, "AppendNative"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (!VerifyResult(rv, "Exists (new after)"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!exists) {
|
|
|
|
fail("%s Destination file %s was not created", gFunction, newName.get());
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test nsIFile::GetParent
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool TestParent(nsIFile* aBase, nsIFile* aStart)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestParent";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aStart);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
nsresult rv = file->GetParent(getter_AddRefs(parent));
|
|
|
|
VerifyResult(rv, "GetParent");
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equal;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = parent->Equals(aBase, &equal);
|
|
|
|
VerifyResult(rv, "Equals");
|
|
|
|
if (!equal) {
|
|
|
|
fail("%s Incorrect parent", gFunction);
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test nsIFile::Normalize and native path setting/getting
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool TestNormalizeNativePath(nsIFile* aBase, nsIFile* aStart)
|
2009-03-23 17:44:37 -07:00
|
|
|
{
|
|
|
|
gFunction = "TestNormalizeNativePath";
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = NewFile(aStart);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!file)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString path;
|
2009-03-23 17:44:37 -07:00
|
|
|
nsresult rv = file->GetNativePath(path);
|
|
|
|
VerifyResult(rv, "GetNativePath");
|
|
|
|
path.Append(FixName("/./.."));
|
|
|
|
rv = file->InitWithNativePath(path);
|
|
|
|
VerifyResult(rv, "InitWithNativePath");
|
|
|
|
rv = file->Normalize();
|
|
|
|
VerifyResult(rv, "Normalize");
|
|
|
|
rv = file->GetNativePath(path);
|
|
|
|
VerifyResult(rv, "GetNativePath (after normalization)");
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString basePath;
|
2009-03-23 17:44:37 -07:00
|
|
|
rv = aBase->GetNativePath(basePath);
|
|
|
|
VerifyResult(rv, "GetNativePath (base)");
|
|
|
|
|
|
|
|
if (!path.Equals(basePath)) {
|
|
|
|
fail("%s Incorrect normalization");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-03-23 17:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
ScopedXPCOM xpcom("nsLocalFile");
|
|
|
|
if (xpcom.failed())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> base;
|
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(base));
|
|
|
|
if (!VerifyResult(rv, "Getting temp directory"))
|
|
|
|
return 1;
|
|
|
|
rv = base->AppendNative(nsDependentCString("mozfiletests"));
|
|
|
|
if (!VerifyResult(rv, "Appending mozfiletests to temp directory name"))
|
|
|
|
return 1;
|
|
|
|
// Remove the directory in case tests failed and left it behind.
|
|
|
|
// don't check result since it might not be there
|
2011-10-17 07:59:28 -07:00
|
|
|
base->Remove(true);
|
2009-03-23 17:44:37 -07:00
|
|
|
|
|
|
|
// Now create the working directory we're going to use
|
|
|
|
rv = base->Create(nsIFile::DIRECTORY_TYPE, 0700);
|
|
|
|
if (!VerifyResult(rv, "Creating temp directory"))
|
|
|
|
return 1;
|
|
|
|
// Now we can safely normalize the path
|
|
|
|
rv = base->Normalize();
|
|
|
|
if (!VerifyResult(rv, "Normalizing temp directory name"))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Initialize subdir object for later use
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> subdir = NewFile(base);
|
2009-03-23 17:44:37 -07:00
|
|
|
if (!subdir)
|
|
|
|
return 1;
|
|
|
|
rv = subdir->AppendNative(nsDependentCString("subdir"));
|
|
|
|
if (!VerifyResult(rv, "Appending 'subdir' to test dir name"))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
passed("Setup");
|
|
|
|
|
|
|
|
// Test path parsing
|
|
|
|
if (TestInvalidFileName(base, "a/b")) {
|
|
|
|
passed("AppendNative with invalid file name");
|
|
|
|
}
|
|
|
|
if (TestParent(base, subdir)) {
|
|
|
|
passed("GetParent");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test file creation
|
|
|
|
if (TestCreate(base, "file.txt", nsIFile::NORMAL_FILE_TYPE, 0600)) {
|
|
|
|
passed("Create file");
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
if (TestRemove(base, "file.txt", false)) {
|
2009-03-23 17:44:37 -07:00
|
|
|
passed("Remove file");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test directory creation
|
|
|
|
if (TestCreate(base, "subdir", nsIFile::DIRECTORY_TYPE, 0700)) {
|
|
|
|
passed("Create directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test move and copy in the base directory
|
|
|
|
if (TestCreate(base, "file.txt", nsIFile::NORMAL_FILE_TYPE, 0600) &&
|
|
|
|
TestMove(base, base, "file.txt", "file2.txt")) {
|
|
|
|
passed("MoveTo rename file");
|
|
|
|
}
|
|
|
|
if (TestCopy(base, base, "file2.txt", "file3.txt")) {
|
|
|
|
passed("CopyTo copy file");
|
|
|
|
}
|
|
|
|
// Test moving across directories
|
|
|
|
if (TestMove(base, subdir, "file2.txt", "file2.txt")) {
|
|
|
|
passed("MoveTo move file");
|
|
|
|
}
|
|
|
|
// Test moving across directories and renaming at the same time
|
|
|
|
if (TestMove(subdir, base, "file2.txt", "file4.txt")) {
|
|
|
|
passed("MoveTo move and rename file");
|
|
|
|
}
|
|
|
|
// Test copying across directoreis
|
|
|
|
if (TestCopy(base, subdir, "file4.txt", "file5.txt")) {
|
|
|
|
passed("CopyTo copy file across directories");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run normalization tests while the directory exists
|
|
|
|
if (TestNormalizeNativePath(base, subdir)) {
|
|
|
|
passed("Normalize with native paths");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test recursive directory removal
|
2011-10-17 07:59:28 -07:00
|
|
|
if (TestRemove(base, "subdir", true)) {
|
2009-03-23 17:44:37 -07:00
|
|
|
passed("Remove directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TestCreateUnique(base, "foo", nsIFile::NORMAL_FILE_TYPE, 0600) &&
|
|
|
|
TestCreateUnique(base, "foo", nsIFile::NORMAL_FILE_TYPE, 0600)) {
|
|
|
|
passed("CreateUnique file");
|
|
|
|
}
|
|
|
|
if (TestCreateUnique(base, "bar.xx", nsIFile::DIRECTORY_TYPE, 0700) &&
|
|
|
|
TestCreateUnique(base, "bar.xx", nsIFile::DIRECTORY_TYPE, 0700)) {
|
|
|
|
passed("CreateUnique directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TestDeleteOnClose(base, "file7.txt", PR_RDWR | PR_CREATE_FILE, 0600)) {
|
|
|
|
passed("OpenNSPRFileDesc DELETE_ON_CLOSE");
|
|
|
|
}
|
|
|
|
|
|
|
|
gFunction = "main";
|
|
|
|
// Clean up temporary stuff
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = base->Remove(true);
|
2009-03-23 17:44:37 -07:00
|
|
|
VerifyResult(rv, "Cleaning up temp directory");
|
|
|
|
|
|
|
|
return gFailCount > 0;
|
|
|
|
}
|