From 3b073151cf3c20f94eba55446cdbcd2287aaa2ec Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Wed, 6 Jul 2011 09:35:12 -0700 Subject: [PATCH] Bug 668361: File ctor should fail if passed a directory. r=sicking --- content/base/src/nsDOMFile.cpp | 5 +++++ content/base/test/chrome/test_fileconstructor.xul | 11 ++++++++++- js/src/xpconnect/tests/unit/component-file.js | 12 ++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/content/base/src/nsDOMFile.cpp b/content/base/src/nsDOMFile.cpp index f76224377fd..16f066a39d1 100644 --- a/content/base/src/nsDOMFile.cpp +++ b/content/base/src/nsDOMFile.cpp @@ -424,6 +424,11 @@ nsDOMFile::Initialize(nsISupports* aOwner, NS_ENSURE_SUCCESS(rv, rv); 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; return NS_OK; } diff --git a/content/base/test/chrome/test_fileconstructor.xul b/content/base/test/chrome/test_fileconstructor.xul index 9c916358a16..39e6f75d0cd 100644 --- a/content/base/test/chrome/test_fileconstructor.xul +++ b/content/base/test/chrome/test_fileconstructor.xul @@ -64,9 +64,18 @@ try { var nonexistentfile = new File("i/sure/hope/this/does/not/exist/anywhere.txt"); ok(false, "This should never be reached!"); } 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."); +} ]]> diff --git a/js/src/xpconnect/tests/unit/component-file.js b/js/src/xpconnect/tests/unit/component-file.js index 09f9f0acf44..52e2cdbbacd 100644 --- a/js/src/xpconnect/tests/unit/component-file.js +++ b/js/src/xpconnect/tests/unit/component-file.js @@ -110,6 +110,18 @@ FileComponent.prototype = } 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; },