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