summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/test_file_put_deleted.html
blob: 32e197b1da8faac90676c31f8100d81535d809bf (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>Indexed Database Property Test</title>

  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

  <script type="text/javascript">
  /**
   * Test that a put of a file-backed Blob/File whose backing file has been
   * deleted results in a failure of that put failure.
   *
   * In order to create a file-backed Blob and ensure that we actually try and
   * copy its contents (rather than triggering a reference-count increment), we
   * use two separate databases.  This test is derived from
   * test_file_cross_database_copying.html.
   */
  function* testSteps()
  {
    const READ_WRITE = "readwrite";

    const databaseInfo = [
      { name: window.location.pathname + "1", source: true },
      { name: window.location.pathname + "2", source: false },
    ];

    const objectStoreName = "Blobs";

    const fileData = { key: 1, file: getRandomFile("random.bin", 10000) };

    SpecialPowers.pushPrefEnv({ set: [["dom.indexedDB.dataThreshold", -1]] },
                              continueToNextStep);
    yield undefined;

    // Open both databases, put the File in the source.
    let databases = [];
    for (let info of databaseInfo) {
      let request = indexedDB.open(info.name, 1);
      request.onerror = errorHandler;
      request.onupgradeneeded = grabEventAndContinueHandler;
      request.onsuccess = grabEventAndContinueHandler;
      let event = yield undefined;

      is(event.type, "upgradeneeded", "Got correct event type");

      let db = event.target.result;
      // We don't expect any errors yet for either database, but will later on.
      db.onerror = errorHandler;

      let objectStore = db.createObjectStore(objectStoreName, { });
      if (info.source) {
        objectStore.add(fileData.file, fileData.key);
      }

      event = yield undefined;

      is(event.type, "success", "Got correct event type");

      databases.push(db);
    }

    // Get a reference to the file-backed File.
    let fileBackedFile;
    for (let db of databases.slice(0, 1)) {
      let request = db.transaction([objectStoreName])
                      .objectStore(objectStoreName).get(fileData.key);
      request.onsuccess = grabEventAndContinueHandler;
      let event = yield undefined;

      let result = event.target.result;
      verifyBlob(result, fileData.file, 1);
      yield undefined;

      fileBackedFile = result;
    }

    // Delete the backing file...
    let fileFullPath = getFilePath(fileBackedFile);
    // (We want to chop off the profile root and the resulting path component
    // must not start with a directory separator.)
    let fileRelPath =
      fileFullPath.substring(fileFullPath.search(/[/\\]storage[/\\](default|private)[/\\]/) + 1);
    info("trying to delete: " + fileRelPath);
    // by using the existing SpecialPowers mechanism to create files and clean
    // them up.  We clobber our existing content, then trigger deletion to
    // clean up after it.
    SpecialPowers.createFiles(
    [{ name: fileRelPath, data: "" }],
      grabEventAndContinueHandler, errorCallbackHandler);
    yield undefined;
    // This is async without a callback because it's intended for cleanup.
    // Since IDB is PBackground, we can't depend on serial ordering, so we need
    // to use another async action.
    SpecialPowers.removeFiles();
    SpecialPowers.executeAfterFlushingMessageQueue(grabEventAndContinueHandler);
    yield undefined;
    // The file is now deleted!

    // Try and put the file-backed Blob in the database, expect failure on the
    // request and transaction.
    info("attempt to store deleted file-backed blob"); // context for NS_WARN_IF
    for (let i = 1; i < databases.length; i++) {
      let db = databases[i];

      let trans = db.transaction([objectStoreName], READ_WRITE);
      let objectStore = trans.objectStore(objectStoreName);

      let request = objectStore.add(fileBackedFile, 2);
      request.onsuccess = unexpectedSuccessHandler;
      request.onerror = expectedErrorHandler("UnknownError");
      trans.onsuccess = unexpectedSuccessHandler;
      trans.onerror = expectedErrorHandler("UnknownError");
      // the database will also throw an error.
      db.onerror = expectedErrorHandler("UnknownError");
      yield undefined;
      yield undefined;
      yield undefined;
      // the database shouldn't throw any more errors now.
      db.onerror = errorHandler;
    }

    // Ensure there's nothing with that key in the target database.
    info("now that the transaction failed, make sure our put got rolled back");
    for (let i = 1; i < databases.length; i++) {
      let db = databases[i];

      let objectStore = db.transaction([objectStoreName], "readonly")
                          .objectStore(objectStoreName);

      // Attempt to fetch the key to verify there's nothing in the DB rather
      // than the value which could return undefined as a misleading error.
      let request = objectStore.getKey(2);
      request.onsuccess = grabEventAndContinueHandler;
      request.onerror = errorHandler;
      let event = yield undefined;

      let result = event.target.result;
      is(result, undefined, "no key found"); // (the get returns undefined)
    }

    finishTest();
  }
  </script>
  <script type="text/javascript" src="file.js"></script>
  <script type="text/javascript" src="helpers.js"></script>

</head>

<body onload="runTest();"></body>

</html>