Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / crypto / resources / common.js
index ba4563f..8169e9b 100644 (file)
@@ -1,21 +1,6 @@
-function importTestKeys()
+function logError(error)
 {
-    var keyFormat = "raw";
-    var data = asciiToUint8Array("16 bytes of key!");
-    var extractable = true;
-    var keyUsages = ['encrypt', 'decrypt', 'sign', 'verify'];
-
-    var hmacPromise = crypto.subtle.importKey(keyFormat, data, {name: 'hmac', hash: {name: 'sha-1'}}, extractable, keyUsages);
-    var aesCbcPromise = crypto.subtle.importKey(keyFormat, data, {name: 'AES-CBC'}, extractable, keyUsages);
-    var aesCbcJustDecrypt = crypto.subtle.importKey(keyFormat, data, {name: 'AES-CBC'}, false, ['decrypt']);
-
-    return Promise.all([hmacPromise, aesCbcPromise, aesCbcJustDecrypt]).then(function(results) {
-        return {
-            hmacSha1: results[0],
-            aesCbc: results[1],
-            aesCbcJustDecrypt: results[2],
-        };
-    });
+    debug("error is: " + error.toString());
 }
 
 // Verifies that the given "bytes" holds the same value as "expectedHexString".
@@ -80,7 +65,46 @@ function asciiToUint8Array(str)
 
 function failAndFinishJSTest(error)
 {
-    if (error)
-       debug(error);
+    testFailed('' + error);
     finishJSTest();
 }
+
+// Returns a Promise for the cloned key.
+function cloneKey(key)
+{
+    // Sending an object through a MessagePort implicitly clones it.
+    // Use a single MessageChannel so requests complete in FIFO order.
+    var self = cloneKey;
+    if (!self.channel) {
+        self.channel = new MessageChannel();
+        self.callbacks = [];
+        self.channel.port1.addEventListener('message', function(e) {
+            var callback = self.callbacks.shift();
+            callback(e.data);
+        }, false);
+        self.channel.port1.start();
+    }
+
+    return new Promise(function(resolve, reject) {
+        self.callbacks.push(resolve);
+        self.channel.port2.postMessage(key);
+    });
+}
+
+// Logging the serialized format ensures that if it changes it will break tests.
+function logSerializedKey(o)
+{
+    if (internals) {
+        // Removing the version tag from the output so serialization format changes don't need to update all the crypto tests.
+        var serialized = internals.serializeObject(o);
+        var serializedWithoutVersion = new Uint8Array(serialized, 2);
+        debug("Serialized key bytes: " + bytesToHexString(serializedWithoutVersion));
+    }
+}
+
+function shouldEvaluateAs(actual, expectedValue)
+{
+    if (typeof expectedValue == "string")
+        return shouldBeEqualToString(actual, expectedValue);
+    return shouldEvaluateTo(actual, expectedValue);
+}