OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "MockWebCrypto.h" |
| 32 |
| 33 #include "public/platform/WebArrayBuffer.h" |
| 34 #include "public/platform/WebCryptoAlgorithm.h" |
| 35 #include <string> |
| 36 #include <string.h> |
| 37 |
| 38 using namespace WebKit; |
| 39 |
| 40 namespace WebTestRunner { |
| 41 |
| 42 namespace { |
| 43 |
| 44 class MockCryptoOperation : public WebKit::WebCryptoOperation { |
| 45 public: |
| 46 MockCryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, const WebKi
t::WebCryptoOperationResult& result) : m_algorithm(algorithm), m_result(result)
{ } |
| 47 |
| 48 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE |
| 49 { |
| 50 // Don't buffer too much data. |
| 51 if (m_data.size() + size > 6) |
| 52 m_result.completeWithError(); |
| 53 |
| 54 if (size) |
| 55 m_data.append(reinterpret_cast<const char*>(bytes), size); |
| 56 } |
| 57 |
| 58 virtual void abort() OVERRIDE |
| 59 { |
| 60 delete this; |
| 61 } |
| 62 |
| 63 virtual void finish() OVERRIDE |
| 64 { |
| 65 const unsigned char digest1[20] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0
x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x0
9}; |
| 66 const unsigned char digest2[20] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0
xf9, 0x3f, 0x52, 0xb5, 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0x4
f}; |
| 67 const unsigned char digest3[20] = {0x86, 0x84, 0x60, 0xd9, 0x8d, 0x09, 0
xd8, 0xbb, 0xb9, 0x3d, 0x7b, 0x6c, 0xdd, 0x15, 0xcc, 0x7f, 0xbe, 0xc6, 0x76, 0xb
9}; |
| 68 |
| 69 const unsigned char* result = 0; |
| 70 size_t resultSize; |
| 71 |
| 72 if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1) { |
| 73 resultSize = 20; |
| 74 |
| 75 if (m_data.empty()) { |
| 76 result = digest1; |
| 77 } else if (m_data == std::string("\x00", 1)) { |
| 78 result = digest2; |
| 79 } else if (m_data == std::string("\x00\x01\x02\x03\x04\x05", 6)) { |
| 80 result = digest3; |
| 81 } |
| 82 } |
| 83 |
| 84 if (result) { |
| 85 WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(resul
tSize, 1); |
| 86 memcpy(buffer.data(), result, resultSize); |
| 87 m_result.completeWithArrayBuffer(buffer); |
| 88 } else { |
| 89 m_result.completeWithError(); |
| 90 } |
| 91 delete this; |
| 92 } |
| 93 |
| 94 protected: |
| 95 WebKit::WebCryptoAlgorithm m_algorithm; |
| 96 WebKit::WebCryptoOperationResult m_result; |
| 97 std::string m_data; |
| 98 }; |
| 99 |
| 100 } // namespace |
| 101 |
| 102 MockWebCrypto* MockWebCrypto::get() |
| 103 { |
| 104 static MockWebCrypto crypto; |
| 105 return &crypto; |
| 106 } |
| 107 |
| 108 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::
WebCryptoOperationResult& result) |
| 109 { |
| 110 result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); |
| 111 } |
| 112 |
| 113 void MockWebCrypto::importKey(WebKit::WebCryptoKeyFormat, const unsigned char* k
eyData, size_t keyDataSize, const WebKit::WebCryptoAlgorithm& algorithm, bool ex
tractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoKeyOperationRe
sult& result) |
| 114 { |
| 115 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz
e); |
| 116 |
| 117 WebKit::WebCryptoKeyType type; |
| 118 if (keyDataString == "reject") { |
| 119 result.completeWithError(); |
| 120 } else if (keyDataString == "throw") { |
| 121 result.initializationFailed(); |
| 122 } else { |
| 123 if (keyDataString == "public") { |
| 124 type = WebKit::WebCryptoKeyTypePublic; |
| 125 } else if (keyDataString == "private") { |
| 126 type = WebKit::WebCryptoKeyTypePrivate; |
| 127 } |
| 128 result.completeWithKey(WebKit::WebCryptoKey::create(0, type, extractable
, algorithm, usages)); |
| 129 } |
| 130 } |
| 131 |
| 132 } // namespace WebTestRunner |
OLD | NEW |