tor-browser

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

idb-binary-key-roundtrip.any.js (4011B)


      1 // META: title=IndexedDB: Binary keys written to a database and read back
      2 // META: global=window,worker
      3 // META: timeout=long
      4 // META: script=resources/support.js
      5 
      6 'use strict';
      7 
      8 const sample = [0x44, 0x33, 0x22, 0x11, 0xFF, 0xEE, 0xDD, 0xCC];
      9 const buffer = new Uint8Array(sample).buffer;
     10 
     11 function assert_key_valid(a, message) {
     12  assert_equals(indexedDB.cmp(a, a), 0, message);
     13 }
     14 
     15 function assert_buffer_equals(a, b, message) {
     16  assert_array_equals(
     17      Array.from(new Uint8Array(a)), Array.from(new Uint8Array(b)), message);
     18 }
     19 
     20 // Verifies that a JavaScript value round-trips through IndexedDB as a key.
     21 function check_key_roundtrip_and_done(t, db, key, key_buffer) {
     22  const tx = db.transaction('store', 'readwrite');
     23  const store = tx.objectStore('store');
     24 
     25  // Verify put with key
     26  const put_request = store.put('value', key);
     27  put_request.onerror = t.unreached_func('put should succeed');
     28 
     29  // Verify get with key
     30  const get_request = store.get(key);
     31  get_request.onerror = t.unreached_func('get should succeed');
     32  get_request.onsuccess = t.step_func(() => {
     33    assert_equals(
     34        get_request.result, 'value',
     35        'get should retrieve the value given to put');
     36 
     37    // Verify iteration returning key
     38    const cursor_request = store.openCursor();
     39    cursor_request.onerror = t.unreached_func('openCursor should succeed');
     40    cursor_request.onsuccess = t.step_func(() => {
     41      assert_not_equals(
     42          cursor_request.result, null, 'cursor should be present');
     43      const retrieved_key = cursor_request.result.key;
     44      assert_true(
     45          retrieved_key instanceof ArrayBuffer,
     46          'IndexedDB binary keys should be returned in ArrayBuffer instances');
     47      assert_key_equals(
     48          retrieved_key, key,
     49          'The key returned by IndexedDB should equal the key given to put()');
     50      assert_buffer_equals(
     51          retrieved_key, key_buffer,
     52          'The ArrayBuffer returned by IndexedDB should equal the buffer ' +
     53              'backing the key given to put()');
     54 
     55      t.done();
     56    });
     57  });
     58 }
     59 
     60 // Checks that IndexedDB handles the given view type for binary keys correctly.
     61 function view_type_test(type) {
     62  indexeddb_test(
     63      (t, db) => {
     64        db.createObjectStore('store');
     65      },
     66      (t, db) => {
     67        const key = new self[type](buffer);
     68        assert_key_valid(key, `${type} should be usable as an IndexedDB key`);
     69        assert_key_equals(
     70            key, buffer,
     71            'Binary keys with the same data but different view types should be ' +
     72                ' equal');
     73        check_key_roundtrip_and_done(t, db, key, buffer);
     74      },
     75      `Binary keys can be supplied using the view type ${type}`,
     76  );
     77 }
     78 
     79 ['Uint8Array', 'Uint8ClampedArray', 'Int8Array', 'Uint16Array', 'Int16Array',
     80 'Uint32Array', 'Int32Array', 'Float16Array', 'Float32Array', 'Float64Array']
     81    .forEach((type) => {
     82      view_type_test(type);
     83    });
     84 
     85 // Checks that IndexedDB
     86 function value_test(value_description, value, value_buffer) {
     87  indexeddb_test(
     88      (t, db) => {
     89        db.createObjectStore('store');
     90      },
     91      (t, db) => {
     92        assert_key_valid(
     93            value, value_description + ' should be usable as an valid key');
     94        check_key_roundtrip_and_done(t, db, value, value_buffer);
     95      },
     96      `${value_description} can be used to supply a binary key`);
     97 }
     98 
     99 value_test('ArrayBuffer', buffer, buffer);
    100 value_test('DataView', new DataView(buffer), buffer);
    101 value_test(
    102    'DataView with explicit offset', new DataView(buffer, 3),
    103    new Uint8Array([0x11, 0xFF, 0xEE, 0xDD, 0xCC]).buffer);
    104 value_test(
    105    'DataView with explicit offset and length', new DataView(buffer, 3, 4),
    106    new Uint8Array([0x11, 0xFF, 0xEE, 0xDD]).buffer);
    107 value_test(
    108    'Uint8Array with explicit offset', new Uint8Array(buffer, 3),
    109    new Uint8Array([0x11, 0xFF, 0xEE, 0xDD, 0xCC]).buffer);
    110 value_test(
    111    'Uint8Array with explicit offset and length', new Uint8Array(buffer, 3, 4),
    112    new Uint8Array([0x11, 0xFF, 0xEE, 0xDD]).buffer);