Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Unified Diff: chrome/test/data/webrtc/indexeddb.js

Issue 1962673002: WebRtcBrowserTest prep-CL for IndexedDB cloning of RTCCertificate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed nits Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/media/webrtc_browsertest_base.cc ('k') | chrome/test/data/webrtc/peerconnection.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/data/webrtc/indexeddb.js
diff --git a/chrome/test/data/webrtc/indexeddb.js b/chrome/test/data/webrtc/indexeddb.js
new file mode 100644
index 0000000000000000000000000000000000000000..96950c4287d18e24a765ddcc2f50b599e401f7bc
--- /dev/null
+++ b/chrome/test/data/webrtc/indexeddb.js
@@ -0,0 +1,155 @@
+/**
+ * Copyright 2016 The Chromium Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/** @private */
+var kDatabaseName = 'WebRTC-Database';
+
+/**
+ * The one and only |IDBDatabase| in this page.
+ * @private
+ */
+var gDatabase = null;
+
+/**
+ * Set by |generateAndCloneCertificate|.
+ */
+var gCertificate = null;
+var gCertificateClone = null;
+
+// Public interface to tests. These are expected to be called with
+// ExecuteJavascript invocations from the browser tests and will return answers
+// through the DOM automation controller.
+
+function openDatabase() {
+ if (gDatabase !== null)
+ throw failTest('The database is already open.');
+ var reqOpen = indexedDB.open(kDatabaseName);
+ reqOpen.onupgradeneeded = function() {
+ // This happens before |onsuccess| if the database is new or its version is
+ // updated. Create object stores.
+ var db = reqOpen.result;
+ var certStore = db.createObjectStore('certificates', { keyPath: 'id' });
+ };
+ reqOpen.onsuccess = function() {
+ if (gDatabase !== null)
+ failTest('The database is already open.');
+ gDatabase = reqOpen.result;
+ returnToTest('ok-database-opened');
+ }
+ reqOpen.onerror = function() {
+ failTest('The database could not be opened. Error: ' + reqOpen.error);
+ }
+}
+
+function closeDatabase() {
+ if (gDatabase === null)
+ throw failTest('The database is already closed.');
+ gDatabase.close();
+ gDatabase = null;
+ returnToTest('ok-database-closed');
+}
+
+function deleteDatabase() {
+ if (gDatabase !== null)
+ throw failTest('The database should be closed before deleting.');
+ var reqDelete = indexedDB.deleteDatabase(kDatabaseName);
+ reqDelete.onsuccess = function () {
+ returnToTest('ok-database-deleted');
+ };
+ reqDelete.onerror = function () {
+ failTest('The database could not be deleted. Error: ' + reqDelete.error);
+ };
+}
+
+/**
+ * Generates a certificate and clones it by saving and loading it to the
+ * database (requires database to be open, see |openDatabase|). After returning
+ * successfully to the test, the global variables |gCertificate| and
+ * |gCertificateClone| have been set.
+ * @param {!Object} keygenAlgorithm An |AlgorithmIdentifier| to be used as
+ * parameter to |RTCPeerConnection.generateCertificate|. The resulting
+ * certificate will be used by the peer connection.
+ */
+function generateAndCloneCertificate(keygenAlgorithm) {
+ RTCPeerConnection.generateCertificate(keygenAlgorithm).then(
+ function(certificate) {
+ gCertificate = certificate;
+ cloneCertificate_(gCertificate).then(
+ function(clone) {
+ gCertificateClone = clone;
+ // TODO(hbos): Verify that |gCertificate| is value-equal to
+ // |gCertificateClone|. crbug.com/609108
+ returnToTest('ok-generated-and-cloned');
+ },
+ function() {
+ failTest('Error cloning certificate.');
+ });
+ },
+ function() {
+ failTest('Certificate generation failed. keygenAlgorithm: ' +
+ JSON.stringify(keygenAlgorithm));
+ });
+}
+
+// Internals.
+
+/** @private */
+function saveCertificate_(certificate) {
+ return new Promise(function(resolve, reject) {
+ if (gDatabase === null)
+ throw failTest('The database is not open.');
+
+ var certTrans = gDatabase.transaction('certificates', 'readwrite');
+ var certStore = certTrans.objectStore('certificates');
+ var certPut = certStore.put({
+ id:0,
+ cert:certificate
+ });
+
+ certPut.onsuccess = function() {
+ resolve();
+ };
+ certPut.onerror = function() {
+ reject(certPut.error);
+ };
+ });
+}
+
+/** @private */
+function loadCertificate_() {
+ return new Promise(function(resolve, reject) {
+ if (gDatabase === null)
+ throw failTest('The database is not open.');
+
+ var certTrans = gDatabase.transaction('certificates', 'readonly');
+ var certStore = certTrans.objectStore('certificates');
+
+ var reqGet = certStore.get(0);
+ reqGet.onsuccess = function() {
+ var match = reqGet.result;
+ if (match !== undefined) {
+ resolve(match.cert);
+ } else {
+ resolve(null);
+ }
+ };
+ reqGet.onerror = function() {
+ reject(reqGet.error);
+ };
+ });
+}
+
+/** @private */
+function cloneCertificate_(certificate) {
+ return saveCertificate_(certificate)
+ .then(loadCertificate_)
+ .then(function(clone) {
+ // Save + load successful.
+ if (clone === null)
+ failTest('loadCertificate returned a null certificate.');
+ return clone;
+ });
+}
« no previous file with comments | « chrome/browser/media/webrtc_browsertest_base.cc ('k') | chrome/test/data/webrtc/peerconnection.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698