Index: content/renderer/webcrypto_sha_digest_unittest.cc |
diff --git a/content/renderer/webcrypto_sha_digest_unittest.cc b/content/renderer/webcrypto_sha_digest_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..35e36ed6d72b8a3ba4c4555f5ba98826623887de |
--- /dev/null |
+++ b/content/renderer/webcrypto_sha_digest_unittest.cc |
@@ -0,0 +1,177 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "webcrypto_sha_digest.h" |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
+ |
+namespace content { |
+ |
+// Algorithms include SHA1, SHA224, SHA256, SHA384, and SHA512 |
+const int kNumAlgorithms = 5; |
eroman
2013/07/19 01:11:13
Instead of specifying this, let the compiler figur
Bryan Eyler
2013/07/24 00:33:53
Done.
|
+WebKit::WebCryptoAlgorithmId algorithm_ids[kNumAlgorithms] = { |
+ WebKit::WebCryptoAlgorithmIdSha1, |
+ WebKit::WebCryptoAlgorithmIdSha224, |
+ WebKit::WebCryptoAlgorithmIdSha256, |
+ WebKit::WebCryptoAlgorithmIdSha384, |
+ WebKit::WebCryptoAlgorithmIdSha512 |
+}; |
+ |
+class TestWebCryptoOperationResult : public WebKit::WebCryptoOperationResult { |
+ public: |
+ virtual void setArrayBuffer(const WebKit::WebArrayBuffer& array_buffer) { |
+ // Might as well just store the array_buffer in hex, since it's going to be |
+ // converted anyway for better error reporting. +1 for the nul terminator. |
+ array_buffer_data_hex_ = new char[array_buffer.byteLength() * 2 + 1]; |
eroman
2013/07/19 01:11:13
Where is this freed?
Try using a std::vector<> in
Bryan Eyler
2013/07/24 00:33:53
Using std::string instead since that's the return
|
+ |
+ unsigned char* array_buffer_ptr = |
+ static_cast<unsigned char*>(array_buffer.data()); |
+ |
+ for (unsigned i = 0; i < array_buffer.byteLength(); i++) { |
eroman
2013/07/19 01:11:13
use HexEncode() from base/strings/string_number_co
Bryan Eyler
2013/07/24 00:33:53
Done. I do a case-insensitive compare anyway.
|
+ array_buffer_data_hex_[i * 2] = |
+ array_buffer_ptr[i] / 16 >= 10 ? |
+ array_buffer_ptr[i] / 16 - 10 + 'a' : |
+ array_buffer_ptr[i] / 16 + '0'; |
+ array_buffer_data_hex_[i * 2 + 1] = |
+ array_buffer_ptr[i] % 16 >= 10 ? |
+ array_buffer_ptr[i] % 16 - 10 + 'a' : |
+ array_buffer_ptr[i] % 16 + '0'; |
+ } |
+ array_buffer_data_hex_[array_buffer.byteLength() * 2] = 0; |
+ } |
+ |
+ const char* array_buffer_data_hex() const { |
+ return array_buffer_data_hex_; |
+ } |
+ |
+ protected: |
+ virtual ~TestWebCryptoOperationResult() { } |
+ |
+ char* array_buffer_data_hex_; |
+}; |
+ |
+TEST(WebCryptoSHADigest, SampleEmptySet) { |
+ // For readability and maintainability of the results, the results are |
+ // stored here in hex format. |
+ const char* hex_result[kNumAlgorithms] = { |
eroman
2013/07/19 01:11:13
easier to let the compiler figure it out.
Bryan Eyler
2013/07/24 00:33:53
I made this, and future ones, refer back to arrays
|
+ // echo -n "" | sha1sum |
+ "da39a3ee5e6b4b0d3255bfef95601890afd80709", |
+ // echo -n "" | sha224sum |
+ "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", |
+ // echo -n "" | sha256sum |
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", |
+ // echo -n "" | sha384sum |
+ "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" |
+ "debfe76f65fbd51ad2f14898b95b", |
+ // echo -n "" | sha512sum |
+ "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" |
+ "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", |
+ }; |
+ |
+ for (int id_index = 0; id_index < kNumAlgorithms; id_index++) { |
+ WebCryptoSHADigest* digest = |
+ new WebCryptoSHADigest(algorithm_ids[id_index]); |
+ |
+ TestWebCryptoOperationResult* result = |
+ new TestWebCryptoOperationResult(); |
eroman
2013/07/19 01:11:13
I believe this is leaked.
Would be better to stack
Bryan Eyler
2013/07/24 00:33:53
Done.
|
+ |
+ digest->finish(result); |
+ |
+ // Ignore case, it's checking the hex value. |
+ EXPECT_STRCASEEQ( |
+ result->array_buffer_data_hex(), |
+ hex_result[id_index]);; |
+ } |
+} |
+ |
+TEST(WebCryptoSHADigest, SampleSimpleSet) { |
+ // For readability and maintainability of the results, the results are |
+ // stored here in hex format. |
+ const unsigned char* kInput = reinterpret_cast<const unsigned char*>("\0"); |
+ const size_t kInputLength = 1; |
+ const char* hex_result[kNumAlgorithms] = { |
eroman
2013/07/19 01:11:13
easier to let the compiler figure it out.
Bryan Eyler
2013/07/24 00:33:53
See above.
|
+ // echo -n -e "\000" | sha1sum |
+ "5ba93c9db0cff93f52b521d7420e43f6eda2784f", |
+ // echo -n -e "\000" | sha224sum |
+ "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", |
+ // echo -n -e "\000" | sha256sum |
+ "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", |
+ // echo -n -e "\000" | sha384sum |
+ "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" |
+ "ec2b413465966817a9c208a11717", |
+ // echo -n -e "\000" | sha512sum |
+ "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" |
+ "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", |
+ }; |
+ |
+ for (int id_index = 0; id_index < kNumAlgorithms; id_index++) { |
+ WebCryptoSHADigest* digest = |
+ new WebCryptoSHADigest(algorithm_ids[id_index]); |
+ |
+ digest->process(kInput, kInputLength); |
+ |
+ TestWebCryptoOperationResult* result = |
+ new TestWebCryptoOperationResult(); |
+ |
+ digest->finish(result); |
+ |
+ // Ignore case, it's checking the hex value. |
+ EXPECT_STRCASEEQ( |
+ result->array_buffer_data_hex(), |
+ hex_result[id_index]);; |
+ } |
+} |
+ |
+TEST(WebCryptoSHADigest, SampleMultipartSet) { |
+ // For readability and maintainability of the results, the results are |
+ // stored here in hex format. |
+ const int kNumInputSets = 6; |
eroman
2013/07/19 01:11:13
easier to let the compiler figure it out.
However
Bryan Eyler
2013/07/24 00:33:53
Done. Seems to be fine with just arraysize()?
|
+ struct { |
+ const unsigned char* input; |
+ size_t input_length; |
+ } input_set[kNumInputSets] = { |
+ { reinterpret_cast<const unsigned char*>("\x00"), 1}, |
+ { reinterpret_cast<const unsigned char*>("\x01"), 1}, |
+ { reinterpret_cast<const unsigned char*>("\x02"), 1}, |
+ { reinterpret_cast<const unsigned char*>("\x03"), 1}, |
+ { reinterpret_cast<const unsigned char*>("\x04"), 1}, |
+ { reinterpret_cast<const unsigned char*>("\x05"), 1}, |
+ }; |
+ const char* hex_result[kNumAlgorithms] = { |
+ // echo -n -e "\000\001\002\003\004\005" | sha1sum |
+ "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", |
+ // echo -n -e "\000\001\002\003\004\005" | sha224sum |
+ "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", |
+ // echo -n -e "\000\001\002\003\004\005" | sha256sum |
+ "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", |
+ // echo -n -e "\000\001\002\003\004\005" | sha384sum |
+ "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" |
+ "22780d7e1b95bfeaa86a678e4552", |
+ // echo -n -e "\000\001\002\003\004\005" | sha512sum |
+ "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" |
+ "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", |
+ }; |
+ |
+ for (int id_index = 0; id_index < kNumAlgorithms; id_index++) { |
+ WebCryptoSHADigest* digest = |
+ new WebCryptoSHADigest(algorithm_ids[id_index]); |
+ |
+ for (int i = 0; i < kNumInputSets; i++) { |
+ digest->process(input_set[i].input, input_set[i].input_length); |
+ } |
+ |
+ TestWebCryptoOperationResult* result = |
+ new TestWebCryptoOperationResult(); |
+ |
+ digest->finish(result); |
+ |
+ // Ignore case, it's checking the hex value. |
+ EXPECT_STRCASEEQ( |
+ result->array_buffer_data_hex(), |
+ hex_result[id_index]);; |
+ } |
+} |
+ |
+} // namespace content |