Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / crypto / resources / common.js
index d2335ab..8169e9b 100644 (file)
@@ -1,24 +1,6 @@
-function importTestKeys()
+function logError(error)
 {
-    var keyFormat = "raw";
-    var data = asciiToUint8Array("16 bytes of key!");
-    var extractable = true;
-    var keyUsages = ['wrapKey', 'unwrapKey', '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']);
-    // FIXME: use AES-CTR key type once it's implemented
-    var aesCtrPromise = crypto.subtle.importKey(keyFormat, data, {name: 'AES-CBC'}, extractable, keyUsages);
-
-    return Promise.all([hmacPromise, aesCbcPromise, aesCbcJustDecrypt, aesCtrPromise]).then(function(results) {
-        return {
-            hmacSha1: results[0],
-            aesCbc: results[1],
-            aesCbcJustDecrypt: results[2],
-            aesCtr: results[3],
-        };
-    });
+    debug("error is: " + error.toString());
 }
 
 // Verifies that the given "bytes" holds the same value as "expectedHexString".
@@ -83,66 +65,46 @@ function asciiToUint8Array(str)
 
 function failAndFinishJSTest(error)
 {
-    if (error)
-       debug(error);
+    testFailed('' + error);
     finishJSTest();
 }
 
-numOutstandingTasks = 0;
-
-function addTask(promise)
+// Returns a Promise for the cloned key.
+function cloneKey(key)
 {
-    numOutstandingTasks++;
-
-    function taskFinished()
-    {
-        numOutstandingTasks--;
-        completeTestWhenAllTasksDone();
+    // 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();
     }
 
-    promise.then(taskFinished, taskFinished);
+    return new Promise(function(resolve, reject) {
+        self.callbacks.push(resolve);
+        self.channel.port2.postMessage(key);
+    });
 }
 
-function completeTestWhenAllTasksDone()
+// Logging the serialized format ensures that if it changes it will break tests.
+function logSerializedKey(o)
 {
-    if (numOutstandingTasks == 0) {
-        finishJSTest();
+    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 shouldRejectPromiseWithNull(code)
+function shouldEvaluateAs(actual, expectedValue)
 {
-    var promise = eval(code);
-
-    function acceptCallback(result)
-    {
-        debug("FAIL: '" + code + "' accepted with " + result + " but should have been rejected");
-    }
-
-    function rejectCallback(result)
-    {
-        if (result == null)
-            debug("PASS: '" + code + "' rejected with null");
-        else
-            debug("FAIL: '" + code + "' rejected with " + result + " but was expecting null");
-    }
-
-    addTask(promise.then(acceptCallback, rejectCallback));
-}
-
-function shouldAcceptPromise(code)
-{
-    var promise = eval(code);
-
-    function acceptCallback(result)
-    {
-        debug("PASS: '" + code + "' accepted with " + result);
-    }
-
-    function rejectCallback(result)
-    {
-        debug("FAIL: '" + code + "' rejected with " + result);
-    }
-
-    addTask(promise.then(acceptCallback, rejectCallback));
+    if (typeof expectedValue == "string")
+        return shouldBeEqualToString(actual, expectedValue);
+    return shouldEvaluateTo(actual, expectedValue);
 }