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 "base/sha1.h" | |
abarth-chromium
2013/07/23 06:22:07
You're not allowed to depend directly on base from
eroman
2013/07/23 06:53:41
I saw that DumpRenderTree already depended on "bas
| |
34 #include "public/platform/WebArrayBuffer.h" | |
35 #include "public/platform/WebCryptoAlgorithm.h" | |
36 #include <string> | |
37 #include <string.h> | |
38 | |
39 using namespace WebKit; | |
40 | |
41 namespace WebTestRunner { | |
42 | |
43 namespace { | |
44 | |
45 class MockCryptoOperation : public WebKit::WebCryptoOperation { | |
46 public: | |
47 MockCryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::Web CryptoOperationResult* result) : m_algorithm(algorithm), m_result(result) { } | |
48 | |
49 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE | |
50 { | |
51 // Don't buffer too much data. | |
52 if (m_data.size() + size > 100) | |
53 m_result->completeWithError(); | |
54 | |
55 if (size) | |
56 m_data.append(reinterpret_cast<const char*>(bytes), size); | |
57 } | |
58 | |
59 virtual void abort() OVERRIDE | |
60 { | |
61 delete this; | |
62 } | |
63 | |
64 virtual void finish() OVERRIDE | |
65 { | |
66 if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1) { | |
67 WebKit::WebArrayBuffer hash = WebKit::WebArrayBuffer::create(base::k SHA1Length, 1); | |
68 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(m_data.da ta()), m_data.size(), static_cast<unsigned char*>(hash.data())); | |
69 m_result->completeWithArrayBuffer(hash); | |
70 } else { | |
71 m_result->completeWithError(); | |
72 } | |
73 delete this; | |
74 } | |
75 | |
76 protected: | |
77 WebKit::WebCryptoAlgorithm m_algorithm; | |
78 WebKit::WebCryptoOperationResult* m_result; | |
79 std::string m_data; | |
80 }; | |
81 | |
82 } // namespace | |
83 | |
84 MockWebCrypto* MockWebCrypto::get() | |
85 { | |
86 static MockWebCrypto crypto; | |
87 return &crypto; | |
88 } | |
89 | |
90 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, WebKit:: WebCryptoOperationResult* result) | |
91 { | |
92 result->initializationSucceded(new MockCryptoOperation(algorithm, result)); | |
93 } | |
94 | |
95 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) | |
96 { | |
97 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz e); | |
98 | |
99 WebKit::WebCryptoKeyType type; | |
100 if (keyDataString == "reject") { | |
101 result->completeWithError(); | |
102 } else if (keyDataString == "throw") { | |
103 result->initializationFailed(); | |
104 } else { | |
105 if (keyDataString == "public") { | |
106 type = WebKit::WebCryptoKeyTypePublic; | |
107 } else if (keyDataString == "private") { | |
108 type = WebKit::WebCryptoKeyTypePrivate; | |
109 } | |
110 result->completeWithKey(WebKit::WebCryptoKey::create(0, type, extractabl e, algorithm, usages)); | |
111 } | |
112 } | |
113 | |
114 } // namespace WebTestRunner | |
OLD | NEW |