Bug 668361: File ctor should fail if passed a directory. r=sicking

This commit is contained in:
Kyle Huey 2011-07-06 09:35:12 -07:00
parent 5ad465dcb3
commit 3b073151cf
3 changed files with 27 additions and 1 deletions

View File

@ -424,6 +424,11 @@ nsDOMFile::Initialize(nsISupports* aOwner,
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(exists, NS_ERROR_FILE_NOT_FOUND); NS_ENSURE_TRUE(exists, NS_ERROR_FILE_NOT_FOUND);
PRBool isDir;
rv = file->IsDirectory(&isDir);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_FALSE(isDir, NS_ERROR_FILE_IS_DIRECTORY);
mFile = file; mFile = file;
return NS_OK; return NS_OK;
} }

View File

@ -64,9 +64,18 @@ try {
var nonexistentfile = new File("i/sure/hope/this/does/not/exist/anywhere.txt"); var nonexistentfile = new File("i/sure/hope/this/does/not/exist/anywhere.txt");
ok(false, "This should never be reached!"); ok(false, "This should never be reached!");
} catch (e) { } catch (e) {
ok(true, "Attempt to construct a non-existent file should fail.") ok(true, "Attempt to construct a non-existent file should fail.");
} }
try {
var dir = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("CurWorkD", Components.interfaces.nsIFile);
var dirfile = new File(dir);
ok(false, "This should never be reached!");
} catch (e) {
ok(true, "Attempt to construct a file from a directory should fail.");
}
]]> ]]>
</script> </script>

View File

@ -110,6 +110,18 @@ FileComponent.prototype =
} }
do_check_true(threw, "Passing a random object should fail"); do_check_true(threw, "Passing a random object should fail");
var threw = false
try {
// Directories fail
var dir = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("CurWorkD", Ci.nsIFile);
var f7 = File(dir)
} catch (e) {
threw = true;
}
do_check_true(threw, "Can't create a File object for a directory");
return true; return true;
}, },