| OLD | NEW |
| (Empty) | |
| 1 /** |
| 2 * Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 var kDatabaseName = 'WebRTC-Database'; |
| 8 |
| 9 /** |
| 10 * The one and only |IDBDatabase| in this page. |
| 11 * @private |
| 12 */ |
| 13 var gDatabase = null; |
| 14 |
| 15 var gCertificate = null; |
| 16 var gCertificateClone = null; |
| 17 |
| 18 // Public interface to tests. These are expected to be called with |
| 19 // ExecuteJavascript invocations from the browser tests and will return answers |
| 20 // through the DOM automation controller. |
| 21 |
| 22 function openDatabase() { |
| 23 if (gDatabase !== null) |
| 24 throw failTest('The database is already open.'); |
| 25 var reqOpen = indexedDB.open(kDatabaseName); |
| 26 reqOpen.onupgradeneeded = function() { |
| 27 // This happens before |onsuccess| if the database is new or its version is |
| 28 // updated. Create object stores. |
| 29 var db = reqOpen.result; |
| 30 var certStore = db.createObjectStore('certificates', { keyPath: 'id' }); |
| 31 certStore.createIndex('by_id', 'id'); |
| 32 }; |
| 33 reqOpen.onsuccess = function() { |
| 34 if (gDatabase !== null) |
| 35 throw failTest('The database is already open.'); |
| 36 gDatabase = reqOpen.result; |
| 37 returnToTest('ok-database-opened'); |
| 38 } |
| 39 reqOpen.onerror = function() { |
| 40 throw failTest('The database could not be opened. Error: ' + reqOpen.error); |
| 41 } |
| 42 } |
| 43 |
| 44 function closeDatabase() { |
| 45 if (gDatabase === null) |
| 46 throw failTest('The database is already closed.'); |
| 47 gDatabase.close(); |
| 48 gDatabase = null; |
| 49 returnToTest('ok-database-closed'); |
| 50 } |
| 51 |
| 52 function deleteDatabase() { |
| 53 if (gDatabase !== null) |
| 54 throw failTest('The database should be closed before deleting.'); |
| 55 var reqDelete = indexedDB.deleteDatabase(kDatabaseName); |
| 56 reqDelete.onsuccess = function () { |
| 57 returnToTest('ok-database-deleted'); |
| 58 }; |
| 59 reqDelete.onerror = function () { |
| 60 throw failTest( |
| 61 'The database could not be deleted. Error: ' + reqDelete.error); |
| 62 }; |
| 63 } |
| 64 |
| 65 function generateAndCloneCertificate(keygenAlgorithm) { |
| 66 RTCPeerConnection.generateCertificate(keygenAlgorithm).then( |
| 67 function(certificate) { |
| 68 gCertificate = certificate; |
| 69 cloneCertificate(gCertificate, |
| 70 function(clone) { |
| 71 gCertificateClone = clone; |
| 72 if (!gCertificate.equals(gCertificateClone)) { |
| 73 throw failTest( |
| 74 'The cloned certificate is not equal to its original.'); |
| 75 } |
| 76 returnToTest('ok-generated-and-cloned'); |
| 77 }, |
| 78 function() { |
| 79 throw failTest('Error cloning certificate.'); |
| 80 }); |
| 81 }, |
| 82 function() { |
| 83 failTest('Certificate generation failed. keygenAlgorithm: ' + |
| 84 JSON.stringify(keygenAlgorithm)); |
| 85 }); |
| 86 } |
| 87 |
| 88 // Public helper functions to test functions. Expected to indirectly be called |
| 89 // with ExecuteJavascript from browser tests, but does not do |returnToTest|. |
| 90 |
| 91 function saveCertificate(certificate, onsuccess, onerror) { |
| 92 if (gDatabase === null) |
| 93 throw failTest('The database is not open.'); |
| 94 |
| 95 var certTrans = gDatabase.transaction('certificates', 'readwrite'); |
| 96 var certStore = certTrans.objectStore('certificates'); |
| 97 var certPut = certStore.put({ |
| 98 id:0, |
| 99 cert:certificate |
| 100 }); |
| 101 |
| 102 certPut.onsuccess = function() { |
| 103 onsuccess(); |
| 104 }; |
| 105 certPut.onerror = function() { |
| 106 onerror(certPut.error); |
| 107 }; |
| 108 } |
| 109 |
| 110 function loadCertificate(onsuccess, onerror) { |
| 111 if (gDatabase === null) |
| 112 throw failTest('The database is not open.'); |
| 113 |
| 114 var certTrans = gDatabase.transaction('certificates', 'readonly'); |
| 115 var certStore = certTrans.objectStore('certificates'); |
| 116 var certById = certStore.index('by_id'); |
| 117 |
| 118 var reqGet = certById.get(0); |
| 119 reqGet.onsuccess = function() { |
| 120 var match = reqGet.result; |
| 121 if (match !== undefined) { |
| 122 onsuccess(match.cert); |
| 123 } else { |
| 124 onsuccess(null); |
| 125 } |
| 126 }; |
| 127 reqGet.onerror = function() { |
| 128 onerror(reqGet.error); |
| 129 }; |
| 130 } |
| 131 |
| 132 function cloneCertificate(certificate, onsuccess, onerror) { |
| 133 saveCertificate(certificate, |
| 134 function() { |
| 135 // Certificate saved successfully, now load it. |
| 136 loadCertificate( |
| 137 function(clone) { |
| 138 // Load successful. |
| 139 if (clone === null) |
| 140 throw failTest('loadCertificate returned a null certificate.'); |
| 141 onsuccess(clone); |
| 142 }, |
| 143 onerror); |
| 144 }, |
| 145 onerror); |
| 146 } |
| OLD | NEW |