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 "content/child/webcrypto/algorithm_dispatch.h"
8 #include "content/child/webcrypto/crypto_data.h"
9 #include "content/child/webcrypto/status.h"
10 #include "content/child/webcrypto/test/test_helpers.h"
11 #include "content/child/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"
23 TEST(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(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
), chunk
.size()));
73 chunk_index
= chunk_index
+ chunk_length
;
74 begin
= begin
+ chunk_length
;
76 EXPECT_TRUE(digestor
->finish(output
, output_length
));
77 EXPECT_BYTES_EQ(test_output
, CryptoData(output
, output_length
));
83 } // namespace webcrypto
85 } // namespace content