Index: Tools/DumpRenderTree/chromium/TestRunner/src/MockWebCrypto.cpp |
diff --git a/Tools/DumpRenderTree/chromium/TestRunner/src/MockWebCrypto.cpp b/Tools/DumpRenderTree/chromium/TestRunner/src/MockWebCrypto.cpp |
index 4b6138d1ce9806c8ff977b36c021042521732ec0..7b9b4eda81bb3c3fda04eda0868211ae059e820c 100644 |
--- a/Tools/DumpRenderTree/chromium/TestRunner/src/MockWebCrypto.cpp |
+++ b/Tools/DumpRenderTree/chromium/TestRunner/src/MockWebCrypto.cpp |
@@ -41,9 +41,20 @@ namespace WebTestRunner { |
namespace { |
+enum Operation { |
+ Encrypt, |
+ Decrypt, |
+ Sign, |
+ Verify, |
+ Digest, |
+}; |
+ |
class MockCryptoOperation : public WebKit::WebCryptoOperation { |
public: |
- MockCryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoOperationResult& result) : m_algorithm(algorithm), m_result(result) { } |
+ MockCryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, Operation op, const WebKit::WebCryptoOperationResult& result) |
+ : m_algorithm(algorithm) |
+ , m_operation(op) |
+ , m_result(result) { } |
virtual void process(const unsigned char* bytes, size_t size) OVERRIDE |
{ |
@@ -65,39 +76,49 @@ public: |
virtual void finish() OVERRIDE |
{ |
- const unsigned char digest1[20] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09}; |
- const unsigned char digest2[20] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0xf9, 0x3f, 0x52, 0xb5, 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0x4f}; |
- const unsigned char digest3[20] = {0x86, 0x84, 0x60, 0xd9, 0x8d, 0x09, 0xd8, 0xbb, 0xb9, 0x3d, 0x7b, 0x6c, 0xdd, 0x15, 0xcc, 0x7f, 0xbe, 0xc6, 0x76, 0xb9}; |
- |
- const unsigned char* result = 0; |
- size_t resultSize; |
- |
- if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1) { |
- resultSize = 20; |
- |
+ if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1 && m_operation == Digest) { |
if (m_data.empty()) { |
- result = digest1; |
+ const unsigned char result[] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09}; |
+ completeWithArrayBuffer(result, sizeof(result)); |
} else if (m_data == std::string("\x00", 1)) { |
- result = digest2; |
+ const unsigned char result[] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0xf9, 0x3f, 0x52, 0xb5, 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0x4f}; |
+ completeWithArrayBuffer(result, sizeof(result)); |
} else if (m_data == std::string("\x00\x01\x02\x03\x04\x05", 6)) { |
- result = digest3; |
+ const unsigned char result[] = {0x86, 0x84, 0x60, 0xd9, 0x8d, 0x09, 0xd8, 0xbb, 0xb9, 0x3d, 0x7b, 0x6c, 0xdd, 0x15, 0xcc, 0x7f, 0xbe, 0xc6, 0x76, 0xb9}; |
+ completeWithArrayBuffer(result, sizeof(result)); |
+ } else { |
+ m_result.completeWithError(); |
} |
- } |
- |
- if (result) { |
- WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(resultSize, 1); |
- memcpy(buffer.data(), result, resultSize); |
- m_result.completeWithArrayBuffer(buffer); |
+ } else if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdHmac && m_operation == Sign) { |
+ std::string result = "signed HMAC:" + m_data; |
+ completeWithArrayBuffer(result.data(), result.size()); |
+ } else if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdHmac && m_operation == Verify) { |
+ std::string expectedSignature = "signed HMAC:" + m_data; |
+ m_result.completeWithBoolean(expectedSignature == m_signature); |
} else { |
m_result.completeWithError(); |
} |
delete this; |
} |
-protected: |
+ void setSignature(const unsigned char* signatureBytes, size_t signatureLength) |
+ { |
+ m_signature.assign(reinterpret_cast<const char*>(signatureBytes), signatureLength); |
+ } |
+ |
+private: |
+ void completeWithArrayBuffer(const void* data, size_t bytes) |
+ { |
+ WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(bytes, 1); |
+ memcpy(buffer.data(), data, bytes); |
+ m_result.completeWithArrayBuffer(buffer); |
+ } |
+ |
WebKit::WebCryptoAlgorithm m_algorithm; |
+ Operation m_operation; |
WebKit::WebCryptoOperationResult m_result; |
std::string m_data; |
+ std::string m_signature; |
}; |
} // namespace |
@@ -110,22 +131,29 @@ MockWebCrypto* MockWebCrypto::get() |
void MockWebCrypto::encrypt(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) |
{ |
- result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); |
+ result.initializationSucceeded(new MockCryptoOperation(algorithm, Encrypt, result)); |
} |
void MockWebCrypto::decrypt(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) |
{ |
- result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); |
+ result.initializationSucceeded(new MockCryptoOperation(algorithm, Decrypt, result)); |
} |
void MockWebCrypto::sign(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) |
{ |
- result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); |
+ result.initializationSucceeded(new MockCryptoOperation(algorithm, Sign, result)); |
+} |
+ |
+void MockWebCrypto::verifySignature(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, const unsigned char* signature, size_t signatureLength, WebKit::WebCryptoOperationResult& result) |
+{ |
+ MockCryptoOperation* op = new MockCryptoOperation(algorithm, Verify, result); |
+ op->setSignature(signature, signatureLength); |
+ result.initializationSucceeded(op); |
} |
void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::WebCryptoOperationResult& result) |
{ |
- result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); |
+ result.initializationSucceeded(new MockCryptoOperation(algorithm, Digest, result)); |
} |
void MockWebCrypto::importKey(WebKit::WebCryptoKeyFormat, const unsigned char* keyData, size_t keyDataSize, const WebKit::WebCryptoAlgorithm& algorithm, bool extractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoKeyOperationResult& result) |