Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / webcrypto / algorithms / sha_unittest.cc
blobbc0d04f0c8caa0339fef1b42d1423f323a9eb039
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "base/stl_util.h"
7 #include "components/webcrypto/algorithm_dispatch.h"
8 #include "components/webcrypto/algorithms/test_helpers.h"
9 #include "components/webcrypto/crypto_data.h"
10 #include "components/webcrypto/status.h"
11 #include "components/webcrypto/webcrypto_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
15 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
17 namespace webcrypto {
19 namespace {
21 class WebCryptoShaTest : public WebCryptoTestBase {};
23 TEST_F(WebCryptoShaTest, DigestSampleSets) {
24 scoped_ptr<base::ListValue> tests;
25 ASSERT_TRUE(ReadJsonTestFileToList("sha.json", &tests));
27 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
28 SCOPED_TRACE(test_index);
29 base::DictionaryValue* test;
30 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
32 blink::WebCryptoAlgorithm test_algorithm =
33 GetDigestAlgorithm(test, "algorithm");
34 std::vector<uint8_t> test_input = GetBytesFromHexString(test, "input");
35 std::vector<uint8_t> test_output = GetBytesFromHexString(test, "output");
37 std::vector<uint8_t> output;
38 ASSERT_EQ(Status::Success(),
39 Digest(test_algorithm, CryptoData(test_input), &output));
40 EXPECT_BYTES_EQ(test_output, output);
44 TEST_F(WebCryptoShaTest, DigestSampleSetsInChunks) {
45 scoped_ptr<base::ListValue> tests;
46 ASSERT_TRUE(ReadJsonTestFileToList("sha.json", &tests));
48 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
49 SCOPED_TRACE(test_index);
50 base::DictionaryValue* test;
51 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
53 blink::WebCryptoAlgorithm test_algorithm =
54 GetDigestAlgorithm(test, "algorithm");
55 std::vector<uint8_t> test_input = GetBytesFromHexString(test, "input");
56 std::vector<uint8_t> test_output = GetBytesFromHexString(test, "output");
58 // Test the chunk version of the digest functions. Test with 129 byte chunks
59 // because the SHA-512 chunk size is 128 bytes.
60 unsigned char* output;
61 unsigned int output_length;
62 static const size_t kChunkSizeBytes = 129;
63 size_t length = test_input.size();
64 scoped_ptr<blink::WebCryptoDigestor> digestor(
65 CreateDigestor(test_algorithm.id()));
66 std::vector<uint8_t>::iterator begin = test_input.begin();
67 size_t chunk_index = 0;
68 while (begin != test_input.end()) {
69 size_t chunk_length = std::min(kChunkSizeBytes, length - chunk_index);
70 std::vector<uint8_t> chunk(begin, begin + chunk_length);
71 ASSERT_TRUE(chunk.size() > 0);
72 EXPECT_TRUE(digestor->consume(vector_as_array(&chunk),
73 static_cast<unsigned int>(chunk.size())));
74 chunk_index = chunk_index + chunk_length;
75 begin = begin + chunk_length;
77 EXPECT_TRUE(digestor->finish(output, output_length));
78 EXPECT_BYTES_EQ(test_output, CryptoData(output, output_length));
82 } // namespace
84 } // namespace webcrypto