tor-browser

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

cache-storage.https.any.js (8265B)


      1 // META: title=CacheStorage
      2 // META: global=window,worker
      3 // META: script=./resources/test-helpers.js
      4 // META: timeout=long
      5 
      6 promise_test(function(t) {
      7    var cache_name = 'cache-storage/foo';
      8    return self.caches.delete(cache_name)
      9      .then(function() {
     10          return self.caches.open(cache_name);
     11        })
     12      .then(function(cache) {
     13          assert_true(cache instanceof Cache,
     14                      'CacheStorage.open should return a Cache.');
     15        });
     16  }, 'CacheStorage.open');
     17 
     18 promise_test(function(t) {
     19    var cache_name = 'cache-storage/bar';
     20    var first_cache = null;
     21    var second_cache = null;
     22    return self.caches.open(cache_name)
     23      .then(function(cache) {
     24          first_cache = cache;
     25          return self.caches.delete(cache_name);
     26        })
     27      .then(function() {
     28          return first_cache.add('./resources/simple.txt');
     29        })
     30      .then(function() {
     31          return self.caches.keys();
     32        })
     33      .then(function(cache_names) {
     34          assert_equals(cache_names.indexOf(cache_name), -1);
     35          return self.caches.open(cache_name);
     36        })
     37      .then(function(cache) {
     38          second_cache = cache;
     39          return second_cache.keys();
     40        })
     41      .then(function(keys) {
     42          assert_equals(keys.length, 0);
     43          return first_cache.keys();
     44        })
     45      .then(function(keys) {
     46          assert_equals(keys.length, 1);
     47          // Clean up
     48          return self.caches.delete(cache_name);
     49        });
     50  }, 'CacheStorage.delete dooms, but does not delete immediately');
     51 
     52 promise_test(function(t) {
     53    // Note that this test may collide with other tests running in the same
     54    // origin that also uses an empty cache name.
     55    var cache_name = '';
     56    return self.caches.delete(cache_name)
     57      .then(function() {
     58          return self.caches.open(cache_name);
     59        })
     60      .then(function(cache) {
     61          assert_true(cache instanceof Cache,
     62                      'CacheStorage.open should accept an empty name.');
     63        });
     64  }, 'CacheStorage.open with an empty name');
     65 
     66 promise_test(function(t) {
     67    return promise_rejects_js(
     68      t,
     69      TypeError,
     70      self.caches.open(),
     71      'CacheStorage.open should throw TypeError if called with no arguments.');
     72  }, 'CacheStorage.open with no arguments');
     73 
     74 promise_test(function(t) {
     75    var test_cases = [
     76      {
     77        name: 'cache-storage/lowercase',
     78        should_not_match:
     79          [
     80            'cache-storage/Lowercase',
     81            ' cache-storage/lowercase',
     82            'cache-storage/lowercase '
     83          ]
     84      },
     85      {
     86        name: 'cache-storage/has a space',
     87        should_not_match:
     88          [
     89            'cache-storage/has'
     90          ]
     91      },
     92      {
     93        name: 'cache-storage/has\000_in_the_name',
     94        should_not_match:
     95          [
     96            'cache-storage/has',
     97            'cache-storage/has_in_the_name'
     98          ]
     99      }
    100    ];
    101    return Promise.all(test_cases.map(function(testcase) {
    102        var cache_name = testcase.name;
    103        return self.caches.delete(cache_name)
    104          .then(function() {
    105              return self.caches.open(cache_name);
    106            })
    107          .then(function() {
    108              return self.caches.has(cache_name);
    109            })
    110          .then(function(result) {
    111              assert_true(result,
    112                          'CacheStorage.has should return true for existing ' +
    113                          'cache.');
    114            })
    115          .then(function() {
    116              return Promise.all(
    117                testcase.should_not_match.map(function(cache_name) {
    118                    return self.caches.has(cache_name)
    119                      .then(function(result) {
    120                          assert_false(result,
    121                                       'CacheStorage.has should only perform ' +
    122                                       'exact matches on cache names.');
    123                        });
    124                  }));
    125            })
    126          .then(function() {
    127              return self.caches.delete(cache_name);
    128            });
    129      }));
    130  }, 'CacheStorage.has with existing cache');
    131 
    132 promise_test(function(t) {
    133    return self.caches.has('cheezburger')
    134      .then(function(result) {
    135          assert_false(result,
    136                       'CacheStorage.has should return false for ' +
    137                       'nonexistent cache.');
    138        });
    139  }, 'CacheStorage.has with nonexistent cache');
    140 
    141 promise_test(function(t) {
    142    var cache_name = 'cache-storage/open';
    143    var cache;
    144    return self.caches.delete(cache_name)
    145      .then(function() {
    146          return self.caches.open(cache_name);
    147        })
    148      .then(function(result) {
    149          cache = result;
    150        })
    151      .then(function() {
    152          return cache.add('./resources/simple.txt');
    153        })
    154      .then(function() {
    155          return self.caches.open(cache_name);
    156        })
    157      .then(function(result) {
    158          assert_true(result instanceof Cache,
    159                      'CacheStorage.open should return a Cache object');
    160          assert_not_equals(result, cache,
    161                            'CacheStorage.open should return a new Cache ' +
    162                            'object each time its called.');
    163          return Promise.all([cache.keys(), result.keys()]);
    164        })
    165      .then(function(results) {
    166          var expected_urls = results[0].map(function(r) { return r.url });
    167          var actual_urls = results[1].map(function(r) { return r.url });
    168          assert_array_equals(actual_urls, expected_urls,
    169                              'CacheStorage.open should return a new Cache ' +
    170                              'object for the same backing store.');
    171        });
    172  }, 'CacheStorage.open with existing cache');
    173 
    174 promise_test(function(t) {
    175    var cache_name = 'cache-storage/delete';
    176 
    177    return self.caches.delete(cache_name)
    178      .then(function() {
    179          return self.caches.open(cache_name);
    180        })
    181      .then(function() { return self.caches.delete(cache_name); })
    182      .then(function(result) {
    183          assert_true(result,
    184                      'CacheStorage.delete should return true after ' +
    185                      'deleting an existing cache.');
    186        })
    187 
    188      .then(function() { return self.caches.has(cache_name); })
    189      .then(function(cache_exists) {
    190          assert_false(cache_exists,
    191                       'CacheStorage.has should return false after ' +
    192                       'fulfillment of CacheStorage.delete promise.');
    193        });
    194  }, 'CacheStorage.delete with existing cache');
    195 
    196 promise_test(function(t) {
    197    return self.caches.delete('cheezburger')
    198      .then(function(result) {
    199          assert_false(result,
    200                       'CacheStorage.delete should return false for a ' +
    201                       'nonexistent cache.');
    202        });
    203  }, 'CacheStorage.delete with nonexistent cache');
    204 
    205 promise_test(function(t) {
    206    var unpaired_name = 'unpaired\uD800';
    207    var converted_name = 'unpaired\uFFFD';
    208 
    209    // The test assumes that a cache with converted_name does not
    210    // exist, but if the implementation fails the test then such
    211    // a cache will be created. Start off in a fresh state by
    212    // deleting all caches.
    213    return delete_all_caches()
    214      .then(function() {
    215          return self.caches.has(converted_name);
    216      })
    217      .then(function(cache_exists) {
    218          assert_false(cache_exists,
    219                       'Test setup failure: cache should not exist');
    220      })
    221      .then(function() { return self.caches.open(unpaired_name); })
    222      .then(function() { return self.caches.keys(); })
    223      .then(function(keys) {
    224          assert_true(keys.indexOf(unpaired_name) !== -1,
    225                      'keys should include cache with bad name');
    226      })
    227      .then(function() { return self.caches.has(unpaired_name); })
    228      .then(function(cache_exists) {
    229          assert_true(cache_exists,
    230                      'CacheStorage names should be not be converted.');
    231        })
    232      .then(function() { return self.caches.has(converted_name); })
    233      .then(function(cache_exists) {
    234          assert_false(cache_exists,
    235                       'CacheStorage names should be not be converted.');
    236        });
    237  }, 'CacheStorage names are DOMStrings not USVStrings');
    238 
    239 done();