testing/web-platform/tests/kv-storage/cause-errors-via-idb.https.html
author Domenic Denicola <domenic@chromium.org>
Tue, 05 Mar 2019 12:17:40 +0000
changeset 464570 872d7defb5d40de2d006e27516d237ad1f0594cb
parent 464361 66b1aba3fb4372ec47986e728b1da9b32dd3d368
permissions -rw-r--r--
Bug 1529038 [wpt PR 15361] - KV storage: add schema checks when initializing, a=testonly Automatic update from web-platform-tests KV storage: add schema checks when initializing Follows https://github.com/WICG/kv-storage/pull/52. Bug: 931263 Change-Id: I4a943dd2d7dde28c98b88eff397cde769bb64e19 Reviewed-on: https://chromium-review.googlesource.com/c/1466865 Commit-Queue: Domenic Denicola <domenic@chromium.org> Reviewed-by: Victor Costan <pwnall@chromium.org> Cr-Commit-Position: refs/heads/master@{#632696} -- wpt-commits: 1fc9a5125f12c95954b4dc2e6ce599bde447f63e wpt-pr: 15361

<!DOCTYPE html>
<meta charset="utf-8">
<title>KV Storage: causing errors by directly manipulating the IDB</title>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/IndexedDB/support-promises.js"></script>

<script type="module">
import { testWithArea, testWithAreaNoCleanup } from "./helpers/kvs-tests.js";

const mustFail = {
  "set()": area => area.set(1, "value 1"),
  "get()": area => area.get(1),
  "delete()": area => area.delete(1),
  "keys()": area => {
    const iter = area.keys();
    return iter.next();
  },
  "values()": area => {
    const iter = area.values();
    return iter.next();
  },
  "entries()": area => {
    const iter = area.entries();
    return iter.next();
  }
};

for (const [method, testFn] of Object.entries(mustFail)) {
  testWithArea(async (area, t) => {
    const { database, version } = area.backingStore;
    const db = await migrateNamedDatabase(t, database, version + 1, () => {});

    const result = testFn(area);

    await promise_rejects(t, "VersionError", result);
  }, `${method}: upgrading the database must cause a "VersionError" DOMException`);

  testWithAreaNoCleanup(async (area, t) => {
    const { database } = area.backingStore;

    // Set up a new database with that name, but with no object stores!
    // NB: this depends on the fact that createNameDatabase sets the initial version to 1, which is
    // the same as the database version used/expected by KV Storage.
    const db = await createNamedDatabase(t, database, () => {});

    const result = testFn(area);

    await promise_rejects(t, "InvalidStateError", result);
  }, `${method}: creating a same-named database with no object store must cause an "InvalidStateError" DOMException`);

  testWithAreaNoCleanup(async (area, t) => {
    const { database } = area.backingStore;

    const db = await createNamedDatabase(t, database, db => {
      db.createObjectStore("wrongName");
    });

    const result = testFn(area);

    await promise_rejects(t, "InvalidStateError", result);
  }, `${method}: creating a same-named database with a single object store with the wrong name must cause an "InvalidStateError" DOMException`);

  testWithAreaNoCleanup(async (area, t) => {
    const { database, store } = area.backingStore;

    const db = await createNamedDatabase(t, database, db => {
      db.createObjectStore(store);
      db.createObjectStore("wrongName");
    });

    const result = testFn(area);

    await promise_rejects(t, "InvalidStateError", result);
  }, `${method}: creating a same-named database with more than one object store must cause an "InvalidStateError" DOMException`);

  testWithAreaNoCleanup(async (area, t) => {
    const { database, store } = area.backingStore;

    const db = await createNamedDatabase(t, database, db => {
      db.createObjectStore(store, { autoIncrement: true });
    });

    const result = testFn(area);

    await promise_rejects(t, "InvalidStateError", result);
  }, `${method}: creating a same-named database the right object store but a bad schema (autoIncrement = true) must cause an "InvalidStateError" DOMException`);

  testWithAreaNoCleanup(async (area, t) => {
    const { database, store } = area.backingStore;

    const db = await createNamedDatabase(t, database, db => {
      db.createObjectStore(store, { keyPath: "somekey" });
    });

    const result = testFn(area);

    await promise_rejects(t, "InvalidStateError", result);
  }, `${method}: creating a same-named database the right object store but a bad schema (keyPath != null) must cause an "InvalidStateError" DOMException`);

  testWithAreaNoCleanup(async (area, t) => {
    const { database, store } = area.backingStore;

    const db = await createNamedDatabase(t, database, db => {
      const s = db.createObjectStore(store);
      s.createIndex("index", "indexKey");
    });

    const result = testFn(area);

    await promise_rejects(t, "InvalidStateError", result);
  }, `${method}: creating a same-named database the right object store but a bad schema (has indices) must cause an "InvalidStateError" DOMException`);
}
</script>