[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / test / data / indexeddb / corrupted_open_db_detection.html
blobd642d8182541fa4ed96d4878b837c5675304fffd
1 <!DOCTYPE html>
2 <html>
3 <!--
4 Copyright 2014 The Chromium Authors. All rights reserved.
5 Use of this source code is governed by a BSD-style license that can be
6 found in the LICENSE file.
7 -->
8 <head>
9 <title>IDB test that db's corrupted while open are properly handled Part 1 / 2</title>
10 <script type="text/javascript" src="common.js"></script>
11 <script>
13 var testType = 'get';
15 function test() {
16 testType = location.hash.substring(1);
17 if (testType == 'testCommon') {
18 fail('"testCommon" is a reserved test name');
19 return;
21 indexedDBTest(upgradeCallback, openCallback);
24 var numObjectsWrittenToDb = 5;
25 var numTransactions = 0;
26 var numTransactionErrors = 0;
27 var numTransactionAborts = 0;
28 var numKeys = 0;
29 var transaction;
30 var request;
31 var db;
32 var objectStore;
34 function upgradeCallback() {
35 db = event.target.result;
36 deleteAllObjectStores(db);
37 objectStore = db.createObjectStore('storeName', { autoIncrement : true });
39 var i;
40 var len = 80;
41 var data = Array(len);
42 for (i = 0; i < len; ++i) {
43 data[i] = i;
46 for (i = 0; i < numObjectsWrittenToDb; ++i) {
47 var key = 'key-' + i;
48 request = objectStore.add(data, key);
49 request.onerror = unexpectedErrorCallback;
50 request.onsuccess = upgradeTransactionComplete;
54 function upgradeTransactionComplete() {
55 ++numTransactions;
56 if (numTransactions === numObjectsWrittenToDb) {
57 debug("All transactions written");
61 function transactionError(event) {
62 if (event.target.error) {
63 numTransactionErrors += 1;
64 } else {
65 fail("Transaction onerror had no error");
69 function transactionAbort(event) {
70 if (event.target.error) {
71 numTransactionAborts += 1;
72 } else {
73 fail("Transaction onabort had no error");
77 function requestError(event) {
78 if (!event.target.error) {
79 fail("get request had no/invalid error");
83 function databaseClosed(event) {
84 shouldBe("numTransactionErrors", "1");
85 shouldBe("numTransactionAborts", "1");
87 done("Closed as expected");
90 function testXhr(url, onSuccess) {
91 var xmlhttp = new window.XMLHttpRequest();
92 xmlhttp.open("GET", url, /*async=*/false);
93 xmlhttp.onreadystatechange = function() {
94 if (xmlhttp.readyState === 4) {
95 if (xmlhttp.status === 200) {
96 onSuccess()
100 xmlhttp.send();
103 var tests = {
104 // Common setup tasks for the other tests in this object
105 testCommon: function(mode) {
106 transaction = db.transaction('storeName', mode);
107 db.onclose = databaseClosed;
108 transaction.onabort = transactionAbort;
109 transaction.onerror = transactionError;
110 objectStore = transaction.objectStore('storeName');
112 get: function() {
113 testXhr("/corrupt/test/fail?class=LevelDBTransaction&method=Get&instNum=1",
114 function() {
115 tests.testCommon('readonly');
116 request = objectStore.get('key-0');
117 request.onsuccess = unexpectedSuccessCallback;
118 request.onerror = requestError;
121 getAll: function() {
122 testXhr("/corrupt/test/fail?class=LevelDBTransaction&method=Get&instNum=1",
123 function() {
124 tests.testCommon('readonly');
125 request = objectStore.getAll('key-0');
126 request.onsuccess = unexpectedSuccessCallback;
127 request.onerror = requestError;
130 failTransactionCommit: function() {
131 tests.testCommon('readwrite');
132 db.onclose = function(event) {
133 shouldBe("numTransactionErrors", "0");
134 shouldBe("numTransactionAborts", "1");
136 done("Closed as expected");
138 testXhr("/corrupt/test/fail?class=LevelDBTransaction&method=Commit", function() {
139 request = objectStore.put("Any Value", 'key-0');
140 request.onerror = requestError;
143 failWebkitGetDatabaseNames: function() {
144 tests.testCommon('readonly');
145 gotRequestError = 0;
146 db.onclose = function(event) {
147 shouldBe("numTransactionErrors", "0");
148 shouldBe("numTransactionAborts", "1");
149 shouldBe("gotRequestError", "1");
151 done("Closed as expected");
153 testXhr("/corrupt/test/fail?class=LevelDBIterator&method=Seek", function() {
154 request = window.indexedDB.webkitGetDatabaseNames();
155 request.onsuccess = unexpectedSuccessCallback;
156 request.onerror = function(evt) {
157 gotRequestError += 1;
161 iterate: function() {
162 testXhr("/corrupt/test/corruptdb?storeName", function() {
163 tests.testCommon('readonly');
164 request = objectStore.openCursor();
165 request.onerror = requestError;
166 request.onsuccess = function (event){
167 var cursor = request.result;
168 if (cursor) {
169 // Get an object. Probably shouldn't get this far, but won't call this an error.
170 cursor.continue();
171 } else {
172 // Got the last object. We shouldn't get this far.
173 fail("Should *not* have been able to iterate over database.");
178 failGetBlobJournal: function() {
179 // Get() #1 is the put (GetNewVersionNumber)
180 // Get() #2 is the blob key generator (GetBlobKeyGeneratorCurrentNumber)
181 // Get() #3 is the journal (GetPrimaryBlobJournal)
182 testXhr("/corrupt/test/fail?class=LevelDBTransaction&method=Get&instNum=3", function() {
183 tests.testCommon('readwrite');
184 request = objectStore.put({blob: new Blob(['abc'])}, 'key-0');
185 request.onerror = requestError;
186 db.onclose = function databaseClosed(event) {
187 shouldBe("numTransactionErrors", "0");
188 shouldBe("numTransactionAborts", "1");
190 done("Closed as expected");
194 clearObjectStore: function() {
195 testXhr("/corrupt/test/corruptdb?storeName", function() {
196 tests.testCommon('readwrite');
197 request = objectStore.clear();
198 request.onerror = requestError;
199 request.onsuccess = unexpectedSuccessCallback
204 function openCallback() {
205 if (testType in tests)
206 tests[testType]();
207 else
208 fail('Unknown test: "' + testType + '"');
211 </script>
212 </head>
213 <body onLoad="test()">
214 <div id="status">Starting...</div>
215 </body>
216 </html>