tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_sessionStorageBaseSessionOnly.html (10147B)


      1 <html xmlns="http://www.w3.org/1999/xhtml">
      2 <head>
      3 <title>sessionStorage basic test, while in sesison only mode</title>
      4 
      5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7 
      8 <script type="text/javascript">
      9 
     10 var testframe;
     11 function iframeOnload(aValue) {
     12  switch (aValue) {
     13    case 1:
     14      testframe.onload = test1;
     15      break;
     16    case 2:
     17      testframe.onload = test2;
     18      break;
     19    default:
     20      of(false, 'should not be reached');
     21      SimpleTest.finish();
     22      return;
     23  }
     24  /* After every permission change, an iframe has to be reloaded, 
     25     otherwise this test causes failures in b2g (oop) mochitest, because
     26     the permission changes don't seem to be always picked up
     27     by the code that excercises it */
     28  testframe.contentWindow.location.reload();
     29 }
     30 
     31 function startTest() {
     32  testframe = document.getElementById('testframe');
     33  SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, 'context': document}], function() { iframeOnload(1); });
     34 }
     35 
     36 function test1() {
     37 
     38  // Initially check the sessionStorage is empty
     39  is(sessionStorage.length, 0, "The storage is empty [1]");
     40  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
     41  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
     42  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
     43  is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
     44  is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (array access)");
     45  is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)");
     46  sessionStorage.removeItem("nonexisting"); // Just check there is no exception
     47 
     48  is(typeof sessionStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
     49  is(typeof sessionStorage.nonexisting, "undefined", "['nonexisting'] is undefined");
     50  is(typeof sessionStorage.nonexisting, "undefined", "nonexisting is undefined");
     51  is(typeof sessionStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
     52  is(typeof sessionStorage.nonexisting2, "undefined", "['nonexisting2'] is undefined");
     53  is(typeof sessionStorage.nonexisting2, "undefined", "nonexisting2 is undefined");
     54 
     55  // add an empty-value key
     56  sessionStorage.setItem("empty", "");
     57  is(sessionStorage.getItem("empty"), "", "Empty value (getItem())");
     58  is(sessionStorage.empty, "", "Empty value (array access)");
     59  is(sessionStorage.empty, "", "Empty value (property access)");
     60  is(typeof sessionStorage.getItem("empty"), "string", "getItem('empty') is string");
     61  is(typeof sessionStorage.empty, "string", "['empty'] is string");
     62  is(typeof sessionStorage.empty, "string", "empty is string");
     63  sessionStorage.removeItem("empty");
     64  is(sessionStorage.length, 0, "The storage has no keys");
     65  is(sessionStorage.getItem("empty"), null, "empty item is null (getItem())");
     66  is(sessionStorage.empty, undefined, "empty item is undefined (array access)");
     67  is(sessionStorage.empty, undefined, "empty item is undefined (property access)");
     68  is(typeof sessionStorage.getItem("empty"), "object", "getItem('empty') is object");
     69  is(typeof sessionStorage.empty, "undefined", "['empty'] is undefined");
     70  is(typeof sessionStorage.empty, "undefined", "empty is undefined");
     71 
     72  // add one key, check it is there
     73  sessionStorage.setItem("key1", "value1");
     74  is(sessionStorage.length, 1, "The storage has one key-value pair");
     75  is(sessionStorage.key(0), "key1");
     76  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
     77  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
     78 
     79  // check all access method give the correct result
     80  // and are of the correct type
     81  is(sessionStorage.getItem("key1"), "value1", "getItem('key1') == value1");
     82  is(sessionStorage.key1, "value1", "['key1'] == value1");
     83  is(sessionStorage.key1, "value1", "key1 == value1");
     84 
     85  is(typeof sessionStorage.getItem("key1"), "string", "getItem('key1') is string");
     86  is(typeof sessionStorage.key1, "string", "['key1'] is string");
     87  is(typeof sessionStorage.key1, "string", "key1 is string");
     88 
     89  // remove the previously added key and check the storage is empty
     90  sessionStorage.removeItem("key1");
     91  is(sessionStorage.length, 0, "The storage is empty [2]");
     92  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
     93  is(sessionStorage.getItem("key1"), null, "\'key1\' removed");
     94 
     95  is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object");
     96  is(typeof sessionStorage.key1, "undefined", "['key1'] is undefined");
     97  is(typeof sessionStorage.key1, "undefined", "key1 is undefined");
     98 
     99  // add one key, check it is there
    100  sessionStorage.setItem("key1", "value1");
    101  is(sessionStorage.length, 1, "The storage has one key-value pair");
    102  is(sessionStorage.key(0), "key1");
    103  is(sessionStorage.getItem("key1"), "value1");
    104 
    105  // add a second key
    106  sessionStorage.setItem("key2", "value2");
    107  is(sessionStorage.length, 2, "The storage has two key-value pairs");
    108  is(sessionStorage.getItem("key1"), "value1");
    109  is(sessionStorage.getItem("key2"), "value2");
    110  var firstKey = sessionStorage.key(0);
    111  var secondKey = sessionStorage.key(1);
    112  ok((firstKey == 'key1' && secondKey == 'key2') ||
    113     (firstKey == 'key2' && secondKey == 'key1'),
    114     'key() API works.');
    115 
    116  // change the second key
    117  sessionStorage.setItem("key2", "value2-2");
    118  is(sessionStorage.length, 2, "The storage has two key-value pairs");
    119  is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
    120  is(sessionStorage.key(1), secondKey);
    121  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    122  is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
    123  is(sessionStorage.getItem("key1"), "value1");
    124  is(sessionStorage.getItem("key2"), "value2-2");
    125 
    126  // change the first key
    127  sessionStorage.setItem("key1", "value1-2");
    128  is(sessionStorage.length, 2, "The storage has two key-value pairs");
    129  is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
    130  is(sessionStorage.key(1), secondKey);
    131  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    132  is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
    133  is(sessionStorage.getItem("key1"), "value1-2");
    134  is(sessionStorage.getItem("key2"), "value2-2");
    135 
    136  // remove the second key
    137  sessionStorage.removeItem("key2");
    138  is(sessionStorage.length, 1, "The storage has one key-value pair");
    139  is(sessionStorage.key(0), "key1");
    140  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    141  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
    142  is(sessionStorage.getItem("key1"), "value1-2");
    143 
    144  // JS property test
    145  sessionStorage.testA = "valueA";
    146  is(sessionStorage.testA, "valueA");
    147  is(sessionStorage.testA, "valueA");
    148  is(sessionStorage.getItem("testA"), "valueA");
    149 
    150  sessionStorage.testA = "valueA2";
    151  is(sessionStorage.testA, "valueA2");
    152  is(sessionStorage.testA, "valueA2");
    153  is(sessionStorage.getItem("testA"), "valueA2");
    154 
    155  sessionStorage.testB = "valueB";
    156  is(sessionStorage.testB, "valueB");
    157  is(sessionStorage.testB, "valueB");
    158  is(sessionStorage.getItem("testB"), "valueB");
    159 
    160  sessionStorage.testB = "valueB2";
    161  is(sessionStorage.testB, "valueB2");
    162  is(sessionStorage.testB, "valueB2");
    163  is(sessionStorage.getItem("testB"), "valueB2");
    164 
    165  sessionStorage.setItem("testC", "valueC");
    166  is(sessionStorage.testC, "valueC");
    167  is(sessionStorage.testC, "valueC");
    168  is(sessionStorage.getItem("testC"), "valueC");
    169 
    170  sessionStorage.setItem("testC", "valueC2");
    171  is(sessionStorage.testC, "valueC2");
    172  is(sessionStorage.testC, "valueC2");
    173  is(sessionStorage.getItem("testC"), "valueC2");
    174 
    175  sessionStorage.setItem("testC", null);
    176  is("testC" in sessionStorage, true);
    177  is(sessionStorage.getItem("testC"), "null");
    178  is(sessionStorage.testC, "null");
    179  is(sessionStorage.testC, "null");
    180 
    181  sessionStorage.removeItem("testC");
    182  sessionStorage.testC = null;
    183  is("testC" in sessionStorage, true);
    184  is(sessionStorage.getItem("testC"), "null");
    185  is(sessionStorage.testC, "null");
    186  is(sessionStorage.testC, "null");
    187 
    188  sessionStorage.setItem(null, "test");
    189  is("null" in sessionStorage, true);
    190  is(sessionStorage.getItem("null"), "test");
    191  is(sessionStorage.getItem(null), "test");
    192  is(sessionStorage.null, "test");
    193  sessionStorage.removeItem(null, "test");
    194  is("null" in sessionStorage, false);
    195 
    196  sessionStorage.setItem(null, "test");
    197  is("null" in sessionStorage, true);
    198  sessionStorage.removeItem("null", "test");
    199  is("null" in sessionStorage, false);
    200 
    201  // Clear the storage
    202  sessionStorage.clear();
    203  is(sessionStorage.length, 0, "The storage is empty [3]");
    204  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
    205  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    206  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
    207  is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null");
    208  is(sessionStorage.getItem("key1"), null, "key1 removed");
    209  is(sessionStorage.getItem("key2"), null, "key2 removed");
    210  sessionStorage.removeItem("nonexisting"); // Just check there is no exception
    211  sessionStorage.removeItem("key1"); // Just check there is no exception
    212  sessionStorage.removeItem("key2"); // Just check there is no exception
    213 
    214  SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT, 'context': document}], function() { iframeOnload(2); });
    215 }
    216 
    217 function test2() {
    218  sessionStorage.clear();
    219  SimpleTest.finish();
    220 }
    221 
    222 SimpleTest.waitForExplicitFinish();
    223 
    224 </script>
    225 
    226 </head>
    227 
    228 <body onload="startTest();">
    229 <iframe id="testframe" srcdoc="<meta charset=utf-8>"></iframe>
    230 </body>
    231 </html>