dom/datastore/tests/file_keys.html

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4   <meta charset="utf-8">
     5   <title>Test for DataStore - string or unsigned long keys</title>
     6 </head>
     7 <body>
     8 <div id="container"></div>
     9   <script type="application/javascript;version=1.7">
    11   var gStore;
    12   var gEvent;
    13   var gChangeId;
    15   function is(a, b, msg) {
    16     alert((a === b ? 'OK' : 'KO') + ' ' + msg)
    17   }
    19   function ok(a, msg) {
    20     alert((a ? 'OK' : 'KO')+ ' ' + msg)
    21   }
    23   function cbError() {
    24     alert('KO error');
    25   }
    27   function finish() {
    28     alert('DONE');
    29   }
    31   function testGetDataStores() {
    32     navigator.getDataStores('foo').then(function(stores) {
    33       gStore = stores[0];
    34       runTest();
    35     }, cbError);
    36   }
    38   function testAdd_noKey(key) {
    39     gEvent = 'added';
    40     gChangeId = key;
    42     gStore.add({ a: 42 }).then(function(id) {
    43       is(id, key, "Id must be " + key + " received: " + id);
    44     });
    45   }
    47   function testAdd_withKey(key) {
    48     gEvent = 'added';
    49     gChangeId = key;
    51     gStore.add({ a: 42 }, key).then(function(id) {
    52       is(id, key, "Id must be " + key + " received: " + id);
    53     });
    54   }
    56   function testPut(key) {
    57     gEvent = 'updated';
    58     gChangeId = key;
    60     gStore.put({ a: 42 }, key).then(function(id) {
    61       is(id, key, "Id must be " + key + " received: " + id);
    62     });
    63   }
    65   function testGet(key) {
    66     gStore.get(key).then(function(value) {
    67       ok(value, "Object received!");
    68       is(value.a, 42, "Object received with right value!");
    69       runTest();
    70     });
    71   }
    73   function testArrayGet(key) {
    74     gStore.get.apply(gStore, key).then(function(values) {
    75       is(values.length, key.length, "Object received!");
    76       for (var i = 0; i < values.length; ++i) {
    77         is(values[i].a, 42, "Object received with right value!");
    78       }
    80       runTest();
    81     });
    82   }
    84   function testRemove(key, success) {
    85     gEvent = 'removed';
    86     gChangeId = key;
    88     gStore.remove(key).then(function(value) {
    89       is(value, success, "Status must be " + success + " received: " + value);
    90       if (value == false) {
    91         runTest();
    92         return;
    93       }
    94     });
    95   }
    97   function eventListener() {
    98     gStore.onchange = function(e) {
    99       is(e.operation, gEvent, "Operation matches: " + e.operation + " " + gEvent);
   100       ok(e.id === gChangeId, "Operation id matches");
   101       runTest();
   102     };
   104     runTest();
   105   }
   107   var tests = [
   108     // Test for GetDataStore
   109     testGetDataStores,
   111     // Event listener
   112     eventListener,
   114     // add
   115     function() { testAdd_noKey(1); },
   116     function() { testAdd_withKey(123); },
   117     function() { testAdd_noKey(124); },
   118     function() { testAdd_withKey('foobar'); },
   119     function() { testAdd_noKey(125); },
   120     function() { testAdd_withKey('125'); },
   121     function() { testAdd_withKey('126'); },
   122     function() { testAdd_noKey(126); },
   124     // put
   125     function() { testPut(42); },
   126     function() { testPut('42'); },
   128     // get
   129     function() { testGet('42'); },
   130     function() { testGet(42); },
   131     function() { testGet(1); },
   132     function() { testGet(123); },
   133     function() { testGet(124); },
   134     function() { testGet('foobar'); },
   135     function() { testGet(125); },
   136     function() { testGet('125'); },
   137     function() { testGet('126'); },
   138     function() { testGet(126); },
   139     function() { testArrayGet(['42', 42, 1, 123, 124, 'foobar', 125, '125', '126', 126]); },
   141     // remove
   142     function() { testRemove(42, true); },
   143     function() { testRemove('42', true); },
   144     function() { testRemove('43', false); },
   145     function() { testRemove(43, false); },
   146   ];
   148   function runTest() {
   149     if (!tests.length) {
   150       finish();
   151       return;
   152     }
   154     var test = tests.shift();
   155     test();
   156   }
   158   runTest();
   159   </script>
   160 </body>
   161 </html>

mercurial