Bug 738647 - DOMStorageImpl::GetKey shouldn't throw; r=honzab

This commit is contained in:
Ms2ger 2012-03-29 23:09:09 +02:00
parent 968425f97e
commit 8abebec458
6 changed files with 64 additions and 128 deletions

View File

@ -64,8 +64,7 @@ interface nsIDOMStorage : nsISupports
* Retrieve the name of the key at a particular index.
*
* @param index index of the item to retrieve
* @returns the key at index
* @throws INDEX_SIZE_ERR if there is no key at that index
* @returns the key at index, null if there is no key at that index
*/
DOMString key(in unsigned long index);

View File

@ -1131,10 +1131,6 @@ IndexFinder(nsSessionStorageEntry* aEntry, void* userArg)
nsresult
DOMStorageImpl::GetKey(bool aCallerSecure, PRUint32 aIndex, nsAString& aKey)
{
// XXXjst: This is as retarded as the DOM spec is, takes an unsigned
// int, but the spec talks about what to do if a negative value is
// passed in.
// XXX: This does a linear search for the key at index, which would
// suck if there's a large numer of indexes. Do we care? If so,
// maybe we need to have a lazily populated key array here or
@ -1148,8 +1144,9 @@ DOMStorageImpl::GetKey(bool aCallerSecure, PRUint32 aIndex, nsAString& aKey)
mItems.EnumerateEntries(IndexFinder, &data);
if (!data.mItem) {
// aIndex was larger than the number of accessible keys. Throw.
return NS_ERROR_DOM_INDEX_SIZE_ERR;
// aIndex was larger than the number of accessible keys. Return null.
aKey.SetIsVoid(true);
return NS_OK;
}
aKey = data.mItem->GetKey();

View File

@ -7,28 +7,13 @@
<script type="text/javascript">
var INDEX_SIZE_ERR = 1;
function checkException(func, exc)
{
var exceptionThrew = false;
try {
func();
}
catch (ex) {
exceptionThrew = true;
is(ex.code, exc, "Expected "+exc+" exception");
}
ok(exceptionThrew, "Exception "+exc+" threw");
}
function startTest()
{
// Initially check the localStorage is empty
is(localStorage.length, 0, "The storage is empty [1]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
is(localStorage["nonexisting"], null, "Nonexisting item is null (array access)");
is(localStorage.nonexisting, null, "Nonexisting item is null (property access)");
@ -62,8 +47,8 @@ function startTest()
localStorage.setItem("key1", "value1");
is(localStorage.length, 1, "The storage has one key-value pair");
is(localStorage.key(0), "key1");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
// check all access method give the correct result
// and are of the correct type
@ -78,7 +63,7 @@ function startTest()
// remove the previously added key and check the storage is empty
localStorage.removeItem("key1");
is(localStorage.length, 0, "The storage is empty [2]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), null, "\'key1\' removed");
is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object");
@ -107,8 +92,8 @@ function startTest()
is(localStorage.length, 2, "The storage has two key-value pairs");
is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
is(localStorage.key(1), secondKey);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(2);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1");
is(localStorage.getItem("key2"), "value2-2");
@ -117,8 +102,8 @@ function startTest()
is(localStorage.length, 2, "The storage has two key-value pairs");
is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
is(localStorage.key(1), secondKey);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(2);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1-2");
is(localStorage.getItem("key2"), "value2-2");
@ -126,8 +111,8 @@ function startTest()
localStorage.removeItem("key2");
is(localStorage.length, 1, "The storage has one key-value pair");
is(localStorage.key(0), "key1");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1-2");
// JS property test
@ -166,9 +151,9 @@ function startTest()
is("testB" in localStorage, false, "Keys are not in the JS scope of the storage");
is("testC" in localStorage, false, "Keys are not in the JS scope of the storage");
is(localStorage.length, 0, "The storage is empty [3]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR); // this is unspecified!
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null");
is(localStorage.getItem("key1"), null, "key1 removed");
is(localStorage.getItem("key2"), null, "key2 removed");

View File

@ -8,21 +8,6 @@
<script type="text/javascript">
var INDEX_SIZE_ERR = 1;
function checkException(func, exc)
{
var exceptionThrew = false;
try {
func();
}
catch (ex) {
exceptionThrew = true;
is(ex.code, exc, "Expected "+exc+" exception");
}
ok(exceptionThrew, "Exception "+exc+" threw");
}
function startTest()
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
@ -45,9 +30,9 @@ function doTest()
// Initially check the localStorage is empty
is(localStorage.length, 0, "The storage is empty [1]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
is(localStorage["nonexisting"], null, "Nonexisting item is null (array access)");
is(localStorage.nonexisting, null, "Nonexisting item is null (property access)");
@ -81,8 +66,8 @@ function doTest()
localStorage.setItem("key1", "value1");
is(localStorage.length, 1, "The storage has one key-value pair");
is(localStorage.key(0), "key1");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
// check all access method give the correct result
// and are of the correct type
@ -97,7 +82,7 @@ function doTest()
// remove the previously added key and check the storage is empty
localStorage.removeItem("key1");
is(localStorage.length, 0, "The storage is empty [2]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), null, "\'key1\' removed");
is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object");
@ -126,8 +111,8 @@ function doTest()
is(localStorage.length, 2, "The storage has two key-value pairs");
is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
is(localStorage.key(1), secondKey);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(2);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1");
is(localStorage.getItem("key2"), "value2-2");
@ -136,8 +121,8 @@ function doTest()
is(localStorage.length, 2, "The storage has two key-value pairs");
is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
is(localStorage.key(1), secondKey);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(2);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1-2");
is(localStorage.getItem("key2"), "value2-2");
@ -145,8 +130,8 @@ function doTest()
localStorage.removeItem("key2");
is(localStorage.length, 1, "The storage has one key-value pair");
is(localStorage.key(0), "key1");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1-2");
// JS property test
@ -183,9 +168,9 @@ function doTest()
// Clear the storage
localStorage.clear();
is(localStorage.length, 0, "The storage is empty [3]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR); // this is unspecified!
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null");
is(localStorage.getItem("key1"), null, "key1 removed");
is(localStorage.getItem("key2"), null, "key2 removed");

View File

@ -7,21 +7,6 @@
<script type="text/javascript">
var INDEX_SIZE_ERR = 1;
function checkException(func, exc)
{
var exceptionThrew = false;
try {
func();
}
catch (ex) {
exceptionThrew = true;
is(ex.code, exc, "Expected "+exc+" exception");
}
ok(exceptionThrew, "Exception "+exc+" threw");
}
function startTest()
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
@ -36,9 +21,9 @@ function startTest()
// Initially check the localStorage is empty
is(localStorage.length, 0, "The storage is empty [1]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
is(localStorage["nonexisting"], null, "Nonexisting item is null (array access)");
is(localStorage.nonexisting, null, "Nonexisting item is null (property access)");
@ -72,8 +57,8 @@ function startTest()
localStorage.setItem("key1", "value1");
is(localStorage.length, 1, "The storage has one key-value pair");
is(localStorage.key(0), "key1");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
// check all access method give the correct result
// and are of the correct type
@ -88,7 +73,7 @@ function startTest()
// remove the previously added key and check the storage is empty
localStorage.removeItem("key1");
is(localStorage.length, 0, "The storage is empty [2]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), null, "\'key1\' removed");
is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object");
@ -117,8 +102,8 @@ function startTest()
is(localStorage.length, 2, "The storage has two key-value pairs");
is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
is(localStorage.key(1), secondKey);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(2);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1");
is(localStorage.getItem("key2"), "value2-2");
@ -127,8 +112,8 @@ function startTest()
is(localStorage.length, 2, "The storage has two key-value pairs");
is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
is(localStorage.key(1), secondKey);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(2);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1-2");
is(localStorage.getItem("key2"), "value2-2");
@ -136,8 +121,8 @@ function startTest()
localStorage.removeItem("key2");
is(localStorage.length, 1, "The storage has one key-value pair");
is(localStorage.key(0), "key1");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("key1"), "value1-2");
// JS property test
@ -174,9 +159,9 @@ function startTest()
// Clear the storage
localStorage.clear();
is(localStorage.length, 0, "The storage is empty [3]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR); // this is unspecified!
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null");
is(localStorage.getItem("key1"), null, "key1 removed");
is(localStorage.getItem("key2"), null, "key2 removed");

View File

@ -7,30 +7,15 @@
<script type="text/javascript">
var INDEX_SIZE_ERR = 1;
function checkException(func, exc)
{
var exceptionThrew = false;
try {
func();
}
catch (ex) {
exceptionThrew = true;
is(ex.code, exc, "Expected "+exc+" exception");
}
ok(exceptionThrew, "Exception "+exc+" threw");
}
function startTest()
{
sessionStorage.clear();
// Initially check the sessionStorage is empty
is(sessionStorage.length, 0, "The storage is empty [1]");
checkException(function() {sessionStorage.key(0);}, INDEX_SIZE_ERR);
checkException(function() {sessionStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {sessionStorage.key(1);}, INDEX_SIZE_ERR);
is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
is(sessionStorage["nonexisting"], null, "Nonexisting item is null (array access)");
is(sessionStorage.nonexisting, null, "Nonexisting item is null (property access)");
@ -64,8 +49,8 @@ function startTest()
sessionStorage.setItem("key1", "value1");
is(sessionStorage.length, 1, "The storage has one key-value pair");
is(sessionStorage.key(0), "key1");
checkException(function() {sessionStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {sessionStorage.key(1);}, INDEX_SIZE_ERR);
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
// check all access method give the correct result
// and are of the correct type
@ -80,7 +65,7 @@ function startTest()
// remove the previously added key and check the storage is empty
sessionStorage.removeItem("key1");
is(sessionStorage.length, 0, "The storage is empty [2]");
checkException(function() {sessionStorage.key(0);}, INDEX_SIZE_ERR);
is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
is(sessionStorage.getItem("key1"), null, "\'key1\' removed");
is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object");
@ -109,8 +94,8 @@ function startTest()
is(sessionStorage.length, 2, "The storage has two key-value pairs");
is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
is(sessionStorage.key(1), secondKey);
checkException(function() {sessionStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {sessionStorage.key(2);}, INDEX_SIZE_ERR);
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
is(sessionStorage.getItem("key1"), "value1");
is(sessionStorage.getItem("key2"), "value2-2");
@ -119,8 +104,8 @@ function startTest()
is(sessionStorage.length, 2, "The storage has two key-value pairs");
is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
is(sessionStorage.key(1), secondKey);
checkException(function() {sessionStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {sessionStorage.key(2);}, INDEX_SIZE_ERR);
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
is(sessionStorage.getItem("key1"), "value1-2");
is(sessionStorage.getItem("key2"), "value2-2");
@ -128,16 +113,16 @@ function startTest()
sessionStorage.removeItem("key2");
is(sessionStorage.length, 1, "The storage has one key-value pair");
is(sessionStorage.key(0), "key1");
checkException(function() {sessionStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {sessionStorage.key(1);}, INDEX_SIZE_ERR);
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.getItem("key1"), "value1-2");
// Clear the storage
sessionStorage.clear();
is(sessionStorage.length, 0, "The storage is empty [3]");
checkException(function() {sessionStorage.key(0);}, INDEX_SIZE_ERR); // this is unspecified!
checkException(function() {sessionStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {sessionStorage.key(1);}, INDEX_SIZE_ERR);
is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null");
is(sessionStorage.getItem("key1"), null, "key1 removed");
is(sessionStorage.getItem("key2"), null, "key2 removed");