summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/keypath-exceptions.htm
blob: c23d4d226818417479d0c85674b9d99a21cdcd37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!doctype html>
<meta charset=utf-8>
<title>IndexedDB: Exceptions in extracting keys from values (ES bindings)</title>
<meta name="help" href="https://w3c.github.io/IndexedDB/#extract-key-from-value">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/support.js"></script>
<script>

indexeddb_test(
  (t, db) => {
    db.createObjectStore('store', {autoIncrement: true, keyPath: 'a.b.c'});
  },
  (t, db) => {
    const tx = db.transaction('store', 'readwrite', {durability: 'relaxed'});
    assert_throws_dom('DataError', () => {
      tx.objectStore('store').put({a: {b: "foo"}});
    }, 'Put should throw if key can not be inserted at key path location.');
    t.done();
  },
  'The last element of keypath is validated'
);

const err = Error();
err.name = 'getter';

function throwingGetter() {
  throw err;
}

indexeddb_test(
  function(t, db) {
    const o = {};
    Object.defineProperty(o, 'throws', {get: throwingGetter,
        enumerable: false, configurable: true});

    // Value should be cloned before key path is evaluated,
    // and non-enumerable getter will be ignored. The clone
    // will have no such property, so key path evaluation
    // will fail.
    const s1 = db.createObjectStore('s1', {keyPath: 'throws'});
    assert_throws_dom('DataError', () => {
      s1.put(o);
    }, 'Key path failing to resolve should throw');

    // Value should be cloned before key path is evaluated,
    // and non-enumerable getter will be ignored. The clone
    // will have no such property, so key path evaluation
    // will fail.
    const s2 = db.createObjectStore('s2', {keyPath: 'throws.x'});
    assert_throws_dom('DataError', () => {
      s2.put(o);
    }, 'Key path failing to resolve should throw');

    // Value should be cloned before key path is evaluated,
    // and non-enumerable getter will be ignored. The clone
    // will have no such property, so generated key can be
    // inserted.
    const s3 = db.createObjectStore('s3',
        {keyPath: 'throws', autoIncrement: true});
    assert_class_string(s3.put(o), 'IDBRequest',
        'Key injectability test at throwing getter should succeed');

    // Value should be cloned before key path is evaluated,
    // and non-enumerable getter will be ignored. The clone
    // will have no such property, so intermediate object
    // and generated key can be inserted.
    const s4 = db.createObjectStore('s4',
        {keyPath: 'throws.x', autoIncrement: true});
    assert_class_string(s4.put(o), 'IDBRequest',
        'Key injectability test past throwing getter should succeed');
  },
  (t, db) => {
    t.done();
  },
  'Key path evaluation: Exceptions from non-enumerable getters'
);

indexeddb_test(
  function(t, db) {
    const o = {};
    Object.defineProperty(o, 'throws', {get: throwingGetter,
        enumerable: true, configurable: true});

    // Value should be cloned before key path is evaluated,
    // and enumerable getter will rethrow.
    const s1 = db.createObjectStore('s1', {keyPath: 'throws'});
    assert_throws_exactly(err, () => {
      s1.put(o);
    }, 'Key path resolving to throwing getter rethrows');

    // Value should be cloned before key path is evaluated,
    // and enumerable getter will rethrow.
    const s2 = db.createObjectStore('s2', {keyPath: 'throws.x'});
    assert_throws_exactly(err, () => {
      s2.put(o);
    }, 'Key path resolving past throwing getter rethrows');

    // Value should be cloned before key path is evaluated,
    // and enumerable getter will rethrow.
    const s3 = db.createObjectStore('s3',
        {keyPath: 'throws', autoIncrement: true});
    assert_throws_exactly(err, () => {
      s3.put(o);
    }, 'Key injectability test at throwing getter should rethrow');

    // Value should be cloned before key path is evaluated,
    // and enumerable getter will rethrow.
    const s4 = db.createObjectStore('s4',
        {keyPath: 'throws.x', autoIncrement: true});
    assert_throws_exactly(err, () => {
      s4.put(o);
    }, 'Key injectability test past throwing getter should rethrow');
  },
  (t, db) => {
    t.done();
  },
  'Key path evaluation: Exceptions from enumerable getters'
);

indexeddb_test(
  (t, db) => {
    // Implemented as function wrapper to clean up
    // immediately after use, otherwise it may
    // interfere with the test harness.
    function with_proto_getter(f) {
      return function() {
        Object.defineProperty(Object.prototype, 'throws', {
          get: throwingGetter,
          enumerable: false, configurable: true
        });
        try {
          f();
        } finally {
          delete Object.prototype['throws'];
        }
      };
    }

    // Value should be cloned before key path is evaluated,
    // and non-enumerable getter will be ignored. The clone
    // will have no own property, so key path evaluation will
    // fail and DataError should be thrown.
    const s1 = db.createObjectStore('s1', {keyPath: 'throws'});
    assert_throws_dom('DataError', with_proto_getter(function() {
      s1.put({});
    }), 'Key path resolving to no own property throws DataError');

    // Value should be cloned before key path is evaluated,
    // and non-enumerable getter will be ignored. The clone
    // will have no own property, so key path evaluation will
    // fail and DataError should be thrown.
    const s2 = db.createObjectStore('s2', {keyPath: 'throws.x'});
    assert_throws_dom('DataError', with_proto_getter(function() {
      s2.put({});
    }), 'Key path resolving past no own property throws DataError');

    // Value should be cloned before key path is evaluated,
    // and non-enumerable getter will be ignored. The clone
    // will have no own property, so key path evaluation will
    // fail and injection can succeed.
    const s3 = db.createObjectStore('s3',
        {keyPath: 'throws', autoIncrement: true});
    assert_equals(s3.put({}).readyState, 'pending',
                  'put should not throw due to inherited property');

    // Value should be cloned before key path is evaluated,
    // and non-enumerable getter will be ignored. The clone
    // will have no own property, so key path evaluation will
    // fail and injection can succeed.
    const s4 = db.createObjectStore('s4',
        {keyPath: 'throws.x', autoIncrement: true});
    assert_equals(s4.put({}).readyState, 'pending',
                  'put should not throw due to inherited property');
  },
  (t, db) => {
    t.done();
  },
  'Key path evaluation: Exceptions from non-enumerable getters on prototype'
);

indexeddb_test(
  (t, db) => {
    // Implemented as function wrapper to clean up
    // immediately after use, otherwise it may
    // interfere with the test harness.
    function with_proto_getter(f) {
      return () => {
        Object.defineProperty(Object.prototype, 'throws', {
          get: throwingGetter,
          enumerable: true, configurable: true
        });
        try {
          f();
        } finally {
          delete Object.prototype['throws'];
        }
      };
    }

    // Value should be cloned before key path is evaluated.
    // The clone will have no own property, so key path
    // evaluation will fail and DataError should be thrown.
    const s1 = db.createObjectStore('s1', {keyPath: 'throws'});
    assert_throws_dom('DataError', with_proto_getter(function() {
      s1.put({});
    }), 'Key path resolving to no own property throws DataError');

    // Value should be cloned before key path is evaluated.
    // The clone will have no own property, so key path
    // evaluation will fail and DataError should be thrown.
    const s2 = db.createObjectStore('s2', {keyPath: 'throws.x'});
    assert_throws_dom('DataError', with_proto_getter(function() {
      s2.put({});
    }), 'Key path resolving past throwing getter rethrows');

    // Value should be cloned before key path is evaluated.
    // The clone will have no own property, so key path
    // evaluation will fail and injection can succeed.
    var s3 = db.createObjectStore('s3',
        {keyPath: 'throws', autoIncrement: true});
    assert_equals(s3.put({}).readyState, 'pending',
                  'put should not throw due to inherited property');

    // Value should be cloned before key path is evaluated.
    // The clone will have no own property, so key path
    // evaluation will fail and injection can succeed.
    var s4 = db.createObjectStore('s4',
        {keyPath: 'throws.x', autoIncrement: true});
    assert_equals(s4.put({}).readyState, 'pending',
                  'put should not throw due to inherited property');
  },
  (t, db) => {
    t.done();
  },
  'Key path evaluation: Exceptions from enumerable getters on prototype'
);

indexeddb_test(
  (t, db) => {
    const store = db.createObjectStore('store');
    store.createIndex('index', 'index0');
  },
  (t, db) => {
    const tx = db.transaction('store', 'readwrite', {durability: 'relaxed'});

    const array = [];
    array[99] = 1;

    // Implemented as function wrapper to clean up
    // immediately after use, otherwise it may
    // interfere with the test harness.
    let getter_called = 0;
    function with_proto_getter(f) {
      const prop = '50';
      Object.defineProperty(Object.prototype, prop, {
        enumerable: true, configurable: true,
        get: () => {
          ++getter_called;
          return 'foo';
        }
      });
      try {
        return f();
      } finally {
        delete Object.prototype[prop];
      }
    }

    const request = with_proto_getter(
      () => tx.objectStore('store').put({index0: array}, 'key'));
    request.onerror = t.unreached_func('put should not fail');
    request.onsuccess = t.step_func(function() {
      assert_equals(getter_called, 0, 'Prototype getter should not be called');
      t.done();
    });
  },
  'Array key conversion should not invoke prototype getters'
);

</script>