Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: Source/testing/runner/MockWebCrypto.cpp

Issue 23537027: WebCrypto: [refactor] Use "unsigned" rather than "size_t" for buffer sizes. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: add an ifdef to coordinate with chromium side Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/testing/runner/MockWebCrypto.h ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 21 matching lines...) Expand all
32 32
33 #include "public/platform/WebCryptoAlgorithm.h" 33 #include "public/platform/WebCryptoAlgorithm.h"
34 #include <string> 34 #include <string>
35 35
36 using namespace WebKit; 36 using namespace WebKit;
37 37
38 namespace WebTestRunner { 38 namespace WebTestRunner {
39 39
40 namespace { 40 namespace {
41 41
42 std::string mockSign(const unsigned char* bytes, size_t size) 42 std::string mockSign(const unsigned char* bytes, unsigned size)
43 { 43 {
44 return "signed HMAC:" + std::string(reinterpret_cast<const char*>(bytes), si ze); 44 return "signed HMAC:" + std::string(reinterpret_cast<const char*>(bytes), si ze);
45 } 45 }
46 46
47 } // namespace 47 } // namespace
48 48
49 MockWebCrypto* MockWebCrypto::get() 49 MockWebCrypto* MockWebCrypto::get()
50 { 50 {
51 static MockWebCrypto crypto; 51 static MockWebCrypto crypto;
52 return &crypto; 52 return &crypto;
53 } 53 }
54 54
55 void MockWebCrypto::encrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, const unsigned char* data, size_t dataSize, WebKit::We bCryptoResult result) 55 void MockWebCrypto::encrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, const unsigned char* data, unsigned dataSize, WebKit:: WebCryptoResult result)
56 { 56 {
57 result.completeWithError(); 57 result.completeWithError();
58 } 58 }
59 59
60 void MockWebCrypto::decrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, const unsigned char* data, size_t dataSize, WebKit::We bCryptoResult result) 60 void MockWebCrypto::decrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, const unsigned char* data, unsigned dataSize, WebKit:: WebCryptoResult result)
61 { 61 {
62 result.completeWithError(); 62 result.completeWithError();
63 } 63 }
64 64
65 void MockWebCrypto::sign(const WebKit::WebCryptoAlgorithm& algorithm, const WebK it::WebCryptoKey& key, const unsigned char* data, size_t dataSize, WebKit::WebCr yptoResult result) 65 void MockWebCrypto::sign(const WebKit::WebCryptoAlgorithm& algorithm, const WebK it::WebCryptoKey& key, const unsigned char* data, unsigned dataSize, WebKit::Web CryptoResult result)
66 { 66 {
67 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac) 67 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac)
68 return result.completeWithError(); 68 return result.completeWithError();
69 69
70 std::string signedData = mockSign(data, dataSize); 70 std::string signedData = mockSign(data, dataSize);
71 result.completeWithBuffer(signedData.data(), signedData.size()); 71 result.completeWithBuffer(signedData.data(), signedData.size());
72 } 72 }
73 73
74 void MockWebCrypto::verifySignature(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, const unsigned char* signatureBytes, size_t si gnatureSize, const unsigned char* data, size_t dataSize, WebKit::WebCryptoResult result) 74 void MockWebCrypto::verifySignature(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, const unsigned char* signatureBytes, unsigned signatureSize, const unsigned char* data, unsigned dataSize, WebKit::WebCryptoRe sult result)
75 { 75 {
76 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac) 76 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac)
77 return result.completeWithError(); 77 return result.completeWithError();
78 78
79 std::string signature = std::string(reinterpret_cast<const char*>(signatureB ytes), signatureSize); 79 std::string signature = std::string(reinterpret_cast<const char*>(signatureB ytes), signatureSize);
80 std::string expectedSignature = mockSign(data, dataSize); 80 std::string expectedSignature = mockSign(data, dataSize);
81 result.completeWithBoolean(expectedSignature == signature); 81 result.completeWithBoolean(expectedSignature == signature);
82 } 82 }
83 83
84 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, const un signed char* data, size_t dataSize, WebKit::WebCryptoResult result) 84 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, const un signed char* data, unsigned dataSize, WebKit::WebCryptoResult result)
85 { 85 {
86 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdSha1) 86 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdSha1)
87 return result.completeWithError(); 87 return result.completeWithError();
88 88
89 std::string input = std::string(reinterpret_cast<const char*>(data), dataSiz e); 89 std::string input = std::string(reinterpret_cast<const char*>(data), dataSiz e);
90 90
91 if (input.empty()) { 91 if (input.empty()) {
92 const unsigned char resultBytes[] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0 x09}; 92 const unsigned char resultBytes[] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0 x09};
93 result.completeWithBuffer(resultBytes, sizeof(resultBytes)); 93 result.completeWithBuffer(resultBytes, sizeof(resultBytes));
94 } else if (input == std::string("\x00", 1)) { 94 } else if (input == std::string("\x00", 1)) {
95 const unsigned char resultBytes[] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0xf9, 0x3f, 0x52, 0xb5, 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0 x4f}; 95 const unsigned char resultBytes[] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0xf9, 0x3f, 0x52, 0xb5, 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0 x4f};
96 result.completeWithBuffer(resultBytes, sizeof(resultBytes)); 96 result.completeWithBuffer(resultBytes, sizeof(resultBytes));
97 } else if (input == std::string("\x00\x01\x02\x03\x04\x05", 6)) { 97 } else if (input == std::string("\x00\x01\x02\x03\x04\x05", 6)) {
98 const unsigned char resultBytes[] = {0x86, 0x84, 0x60, 0xd9, 0x8d, 0x09, 0xd8, 0xbb, 0xb9, 0x3d, 0x7b, 0x6c, 0xdd, 0x15, 0xcc, 0x7f, 0xbe, 0xc6, 0x76, 0 xb9}; 98 const unsigned char resultBytes[] = {0x86, 0x84, 0x60, 0xd9, 0x8d, 0x09, 0xd8, 0xbb, 0xb9, 0x3d, 0x7b, 0x6c, 0xdd, 0x15, 0xcc, 0x7f, 0xbe, 0xc6, 0x76, 0 xb9};
99 result.completeWithBuffer(resultBytes, sizeof(resultBytes)); 99 result.completeWithBuffer(resultBytes, sizeof(resultBytes));
100 } else { 100 } else {
101 result.completeWithError(); 101 result.completeWithError();
102 } 102 }
103 } 103 }
104 104
105 void MockWebCrypto::generateKey(const WebKit::WebCryptoAlgorithm& algorithm, boo l extractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoResult res ult) 105 void MockWebCrypto::generateKey(const WebKit::WebCryptoAlgorithm& algorithm, boo l extractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoResult res ult)
106 { 106 {
107 if (algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5) { 107 if (algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5) {
108 result.completeWithKeyPair(WebKit::WebCryptoKey::create(0, WebKit::WebCr yptoKeyTypePublic, extractable, algorithm, usages), WebKit::WebCryptoKey::create (0, WebKit::WebCryptoKeyTypePrivate, extractable, algorithm, usages)); 108 result.completeWithKeyPair(WebKit::WebCryptoKey::create(0, WebKit::WebCr yptoKeyTypePublic, extractable, algorithm, usages), WebKit::WebCryptoKey::create (0, WebKit::WebCryptoKeyTypePrivate, extractable, algorithm, usages));
109 } else { 109 } else {
110 result.completeWithKey(WebKit::WebCryptoKey::create(0, WebKit::WebCrypto KeyTypePrivate, extractable, algorithm, usages)); 110 result.completeWithKey(WebKit::WebCryptoKey::create(0, WebKit::WebCrypto KeyTypePrivate, extractable, algorithm, usages));
111 } 111 }
112 } 112 }
113 113
114 void MockWebCrypto::importKey(WebKit::WebCryptoKeyFormat, const unsigned char* k eyData, size_t keyDataSize, const WebKit::WebCryptoAlgorithm& algorithm, bool ex tractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoResult result) 114 void MockWebCrypto::importKey(WebKit::WebCryptoKeyFormat, const unsigned char* k eyData, unsigned keyDataSize, const WebKit::WebCryptoAlgorithm& algorithm, bool extractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoResult resul t)
115 { 115 {
116 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz e); 116 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz e);
117 117
118 if (keyDataString == "error") 118 if (keyDataString == "error")
119 return result.completeWithError(); 119 return result.completeWithError();
120 120
121 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePrivate; 121 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePrivate;
122 if (keyDataString == "public") 122 if (keyDataString == "public")
123 type = WebKit::WebCryptoKeyTypePublic; 123 type = WebKit::WebCryptoKeyTypePublic;
124 124
125 result.completeWithKey(WebKit::WebCryptoKey::create(0, type, extractable, al gorithm, usages)); 125 result.completeWithKey(WebKit::WebCryptoKey::create(0, type, extractable, al gorithm, usages));
126 } 126 }
127 127
128 void MockWebCrypto::exportKey(WebKit::WebCryptoKeyFormat format, const WebKit::W ebCryptoKey& key, WebKit::WebCryptoResult result) 128 void MockWebCrypto::exportKey(WebKit::WebCryptoKeyFormat format, const WebKit::W ebCryptoKey& key, WebKit::WebCryptoResult result)
129 { 129 {
130 std::string buffer; 130 std::string buffer;
131 131
132 if (format == WebKit::WebCryptoKeyFormatRaw) 132 if (format == WebKit::WebCryptoKeyFormatRaw)
133 buffer = "raw"; 133 buffer = "raw";
134 else if (format == WebKit::WebCryptoKeyFormatPkcs8) 134 else if (format == WebKit::WebCryptoKeyFormatPkcs8)
135 buffer = "pkcs8"; 135 buffer = "pkcs8";
136 136
137 result.completeWithBuffer(buffer.data(), buffer.size()); 137 result.completeWithBuffer(buffer.data(), buffer.size());
138 } 138 }
139 139
140 140
141 } // namespace WebTestRunner 141 } // namespace WebTestRunner
OLDNEW
« no previous file with comments | « Source/testing/runner/MockWebCrypto.h ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698